@orpc/shared
Advanced tools
Comparing version 0.34.0 to 0.35.0
@@ -25,2 +25,15 @@ // src/constants.ts | ||
// src/function.ts | ||
function once(fn) { | ||
let cached; | ||
return () => { | ||
if (cached) { | ||
return cached.result; | ||
} | ||
const result = fn(); | ||
cached = { result }; | ||
return result; | ||
}; | ||
} | ||
// src/hook.ts | ||
@@ -45,4 +58,4 @@ async function executeWithHooks(options) { | ||
try { | ||
for (const onStart of onStarts) { | ||
await onStart(state, options.context, options.meta); | ||
for (const onStart2 of onStarts) { | ||
await onStart2(state, options.context, options.meta); | ||
} | ||
@@ -85,2 +98,56 @@ const output = await options.execute(); | ||
// src/interceptor.ts | ||
function onStart(callback) { | ||
return async (options, ...rest) => { | ||
await callback(options, ...rest); | ||
return await options.next(); | ||
}; | ||
} | ||
function onSuccess(callback) { | ||
return async (options, ...rest) => { | ||
const result = await options.next(); | ||
await callback(result, options, ...rest); | ||
return result; | ||
}; | ||
} | ||
function onError(callback) { | ||
return async (options, ...rest) => { | ||
try { | ||
return await options.next(); | ||
} catch (error) { | ||
await callback(error, options, ...rest); | ||
throw error; | ||
} | ||
}; | ||
} | ||
function onFinish(callback) { | ||
let state; | ||
return async (options, ...rest) => { | ||
try { | ||
const result = await options.next(); | ||
state = [result, void 0, "success"]; | ||
return result; | ||
} catch (error) { | ||
state = [void 0, error, "error"]; | ||
throw error; | ||
} finally { | ||
await callback(state, options, ...rest); | ||
} | ||
}; | ||
} | ||
async function intercept(interceptors, options, main) { | ||
let index = 0; | ||
const next = async (nextOptions = options) => { | ||
const interceptor = interceptors[index++]; | ||
if (!interceptor) { | ||
return await main(nextOptions); | ||
} | ||
return await interceptor({ | ||
...nextOptions, | ||
next | ||
}); | ||
}; | ||
return await next(); | ||
} | ||
// src/json.ts | ||
@@ -191,2 +258,3 @@ function parseJSONSafely(text) { | ||
guard, | ||
intercept, | ||
isPlainObject3 as isPlainObject, | ||
@@ -196,2 +264,7 @@ mapEntries, | ||
omit, | ||
onError, | ||
onFinish, | ||
onStart, | ||
onSuccess, | ||
once, | ||
parseJSONSafely, | ||
@@ -198,0 +271,0 @@ set, |
export type AnyFunction = (...args: any[]) => any; | ||
export declare function once<T extends () => any>(fn: T): () => ReturnType<T>; | ||
//# sourceMappingURL=function.d.ts.map |
@@ -6,2 +6,3 @@ export * from './chain'; | ||
export * from './hook'; | ||
export * from './interceptor'; | ||
export * from './json'; | ||
@@ -8,0 +9,0 @@ export * from './object'; |
export type Segment = string | number; | ||
export declare function set(root: Readonly<Record<string, unknown> | unknown[]>, segments: Readonly<Segment[]>, value: unknown): unknown; | ||
export declare function set(root: unknown, segments: Readonly<Segment[]>, value: unknown): unknown; | ||
export declare function get(root: Readonly<Record<string, unknown> | unknown[]>, segments: Readonly<Segment[]>): unknown; | ||
@@ -4,0 +4,0 @@ export declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): { |
{ | ||
"name": "@orpc/shared", | ||
"type": "module", | ||
"version": "0.34.0", | ||
"version": "0.35.0", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "homepage": "https://orpc.unnoq.com", |
15726
14
383