diff options
author | Vincent LE GOFF <g_n_s@hotmail.fr> | 2019-11-12 21:51:14 +0100 |
---|---|---|
committer | Ry Dahl <ry@tinyclouds.org> | 2019-11-12 15:51:14 -0500 |
commit | ee1b8dc883db1531d913f7b10a8d1160f3209d93 (patch) | |
tree | 3ba17c471d465555fbffc7aa9027503b901ccc26 /std/node/_utils.ts | |
parent | 0f33bf68859c162eb3d65ab53a7c71092585e1d1 (diff) |
feat: std/node (#3319)
Diffstat (limited to 'std/node/_utils.ts')
-rw-r--r-- | std/node/_utils.ts | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/std/node/_utils.ts b/std/node/_utils.ts new file mode 100644 index 000000000..f0808e82b --- /dev/null +++ b/std/node/_utils.ts @@ -0,0 +1,35 @@ +export function notImplemented(msg?: string): never { + const message = msg ? `Not implemented: ${msg}` : "Not implemented"; + throw new Error(message); +} + +// API helpers + +export type MaybeNull<T> = T | null; +export type MaybeDefined<T> = T | undefined; +export type MaybeEmpty<T> = T | null | undefined; + +export function intoCallbackAPI<T>( + /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ + func: (...args: any[]) => Promise<T>, + cb: MaybeEmpty<(err: MaybeNull<Error>, value: MaybeEmpty<T>) => void>, + /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ + ...args: any[] +): void { + func(...args) + .then(value => cb && cb(null, value)) + .catch(err => cb && cb(err, null)); +} + +export function intoCallbackAPIWithIntercept<T1, T2>( + /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ + func: (...args: any[]) => Promise<T1>, + interceptor: (v: T1) => T2, + cb: MaybeEmpty<(err: MaybeNull<Error>, value: MaybeEmpty<T2>) => void>, + /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ + ...args: any[] +): void { + func(...args) + .then(value => cb && cb(null, interceptor(value))) + .catch(err => cb && cb(err, null)); +} |