Skip to content

Commit

Permalink
refactor: Fix request type definition
Browse files Browse the repository at this point in the history
We now distinguish between the callback variant and the promise
  • Loading branch information
aron committed Mar 15, 2021
1 parent 183f676 commit b7f89aa
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/lib/request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ import { MetricsCollector } from '../metrics';
import * as needle from 'needle';

// A hybrid async function: both returns a promise and takes a callback
export = async (
async function requestWrapper(
payload: any,
): Promise<{ res: needle.NeedleResponse; body: any }>;
async function requestWrapper(
payload: any,
callback: (err: Error | null, res?, body?) => void,
): Promise<void>;
async function requestWrapper(
payload: any,
callback?: (err: Error | null, res?, body?) => void,
): Promise<void | { res: needle.NeedleResponse; body: any }> => {
): Promise<void | { res: needle.NeedleResponse; body: any }> {
const totalNetworkTimeTimer = MetricsCollector.NETWORK_TIME.createInstance();
totalNetworkTimeTimer.start();
try {
Expand All @@ -28,4 +35,6 @@ export = async (
} finally {
totalNetworkTimeTimer.stop();
}
};
}

export = requestWrapper;

0 comments on commit b7f89aa

Please sign in to comment.