@orpc/shared 0.0.0-next.c6c659d → 0.0.0-next.caefe3a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +64 -51
- package/dist/src/chain.d.ts +5 -0
- package/dist/src/function.d.ts +1 -0
- package/dist/src/index.d.ts +2 -1
- package/dist/src/interceptor.d.ts +47 -0
- package/dist/src/object.d.ts +1 -1
- package/package.json +1 -1
- package/dist/src/hook.d.ts +0 -42
package/dist/index.js
CHANGED
|
@@ -22,63 +22,72 @@ function toError(error) {
|
|
|
22
22
|
return new Error("Unknown error", { cause: error });
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
// src/
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const onFinishes = convertToArray(options.hooks?.onFinish);
|
|
32
|
-
let currentExecuteIndex = 0;
|
|
33
|
-
const next = async () => {
|
|
34
|
-
const execute = interceptors[currentExecuteIndex];
|
|
35
|
-
if (execute) {
|
|
36
|
-
currentExecuteIndex++;
|
|
37
|
-
return await execute(options.input, options.context, {
|
|
38
|
-
...options.meta,
|
|
39
|
-
next
|
|
40
|
-
});
|
|
25
|
+
// src/function.ts
|
|
26
|
+
function once(fn) {
|
|
27
|
+
let cached;
|
|
28
|
+
return () => {
|
|
29
|
+
if (cached) {
|
|
30
|
+
return cached.result;
|
|
41
31
|
}
|
|
42
|
-
|
|
32
|
+
const result = fn();
|
|
33
|
+
cached = { result };
|
|
34
|
+
return result;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// src/interceptor.ts
|
|
39
|
+
function onStart(callback) {
|
|
40
|
+
return async (options, ...rest) => {
|
|
41
|
+
await callback(options, ...rest);
|
|
42
|
+
return await options.next();
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function onSuccess(callback) {
|
|
46
|
+
return async (options, ...rest) => {
|
|
47
|
+
const result = await options.next();
|
|
48
|
+
await callback(result, options, ...rest);
|
|
49
|
+
return result;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function onError(callback) {
|
|
53
|
+
return async (options, ...rest) => {
|
|
43
54
|
try {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
state = { status: "success", input: options.input, output, error: void 0 };
|
|
49
|
-
for (let i = onSuccesses.length - 1; i >= 0; i--) {
|
|
50
|
-
await onSuccesses[i](state, options.context, options.meta);
|
|
51
|
-
}
|
|
52
|
-
} catch (e) {
|
|
53
|
-
state = { status: "error", input: options.input, error: toError(e), output: void 0 };
|
|
54
|
-
for (let i = onErrors.length - 1; i >= 0; i--) {
|
|
55
|
-
try {
|
|
56
|
-
await onErrors[i](state, options.context, options.meta);
|
|
57
|
-
} catch (e2) {
|
|
58
|
-
state = { status: "error", input: options.input, error: toError(e2), output: void 0 };
|
|
59
|
-
}
|
|
60
|
-
}
|
|
55
|
+
return await options.next();
|
|
56
|
+
} catch (error) {
|
|
57
|
+
await callback(error, options, ...rest);
|
|
58
|
+
throw error;
|
|
61
59
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function onFinish(callback) {
|
|
63
|
+
let state;
|
|
64
|
+
return async (options, ...rest) => {
|
|
65
|
+
try {
|
|
66
|
+
const result = await options.next();
|
|
67
|
+
state = [result, void 0, "success"];
|
|
68
|
+
return result;
|
|
69
|
+
} catch (error) {
|
|
70
|
+
state = [void 0, error, "error"];
|
|
71
|
+
throw error;
|
|
72
|
+
} finally {
|
|
73
|
+
await callback(state, options, ...rest);
|
|
68
74
|
}
|
|
69
|
-
|
|
70
|
-
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
async function intercept(interceptors, options, main) {
|
|
78
|
+
let index = 0;
|
|
79
|
+
const next = async (nextOptions = options) => {
|
|
80
|
+
const interceptor = interceptors[index++];
|
|
81
|
+
if (!interceptor) {
|
|
82
|
+
return await main(nextOptions);
|
|
71
83
|
}
|
|
72
|
-
return
|
|
84
|
+
return await interceptor({
|
|
85
|
+
...nextOptions,
|
|
86
|
+
next
|
|
87
|
+
});
|
|
73
88
|
};
|
|
74
89
|
return await next();
|
|
75
90
|
}
|
|
76
|
-
function convertToArray(value2) {
|
|
77
|
-
if (value2 === void 0) {
|
|
78
|
-
return [];
|
|
79
|
-
}
|
|
80
|
-
return Array.isArray(value2) ? value2 : [value2];
|
|
81
|
-
}
|
|
82
91
|
|
|
83
92
|
// src/json.ts
|
|
84
93
|
function parseJSONSafely(text) {
|
|
@@ -178,17 +187,21 @@ import { group, guard, mapEntries, mapValues, omit, trim } from "radash";
|
|
|
178
187
|
export {
|
|
179
188
|
ORPC_HANDLER_HEADER,
|
|
180
189
|
ORPC_HANDLER_VALUE,
|
|
181
|
-
convertToArray,
|
|
182
190
|
createCallableObject,
|
|
183
|
-
executeWithHooks,
|
|
184
191
|
findDeepMatches,
|
|
185
192
|
get,
|
|
186
193
|
group,
|
|
187
194
|
guard,
|
|
195
|
+
intercept,
|
|
188
196
|
isPlainObject3 as isPlainObject,
|
|
189
197
|
mapEntries,
|
|
190
198
|
mapValues,
|
|
191
199
|
omit,
|
|
200
|
+
onError,
|
|
201
|
+
onFinish,
|
|
202
|
+
onStart,
|
|
203
|
+
onSuccess,
|
|
204
|
+
once,
|
|
192
205
|
parseJSONSafely,
|
|
193
206
|
set,
|
|
194
207
|
toError,
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { AnyFunction } from './function';
|
|
2
|
+
export type OmitChainMethodDeep<T extends object, K extends keyof any> = {
|
|
3
|
+
[P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
|
|
4
|
+
};
|
|
5
|
+
//# sourceMappingURL=chain.d.ts.map
|
package/dist/src/function.d.ts
CHANGED
package/dist/src/index.d.ts
CHANGED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Promisable } from 'type-fest';
|
|
2
|
+
import type { AnyFunction } from './function';
|
|
3
|
+
export type InterceptableOptions = Record<string, any>;
|
|
4
|
+
export type InterceptorOptions<TOptions extends InterceptableOptions, TResult> = Omit<TOptions, 'next'> & {
|
|
5
|
+
next(options?: TOptions): Promise<TResult>;
|
|
6
|
+
};
|
|
7
|
+
export type Interceptor<TOptions extends InterceptableOptions, TResult, TError> = (options: InterceptorOptions<TOptions, TResult>) => Promise<TResult> & {
|
|
8
|
+
__error?: {
|
|
9
|
+
type: TError;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Can used for interceptors or middlewares
|
|
14
|
+
*/
|
|
15
|
+
export declare function onStart<TOptions extends {
|
|
16
|
+
next(): any;
|
|
17
|
+
}, TRest extends any[]>(callback: NoInfer<(options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
18
|
+
/**
|
|
19
|
+
* Can used for interceptors or middlewares
|
|
20
|
+
*/
|
|
21
|
+
export declare function onSuccess<TOptions extends {
|
|
22
|
+
next(): any;
|
|
23
|
+
}, TRest extends any[]>(callback: NoInfer<(result: Awaited<ReturnType<TOptions['next']>>, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
24
|
+
export type InferError<T extends AnyFunction> = T extends Interceptor<any, any, infer TError> ? TError : unknown;
|
|
25
|
+
/**
|
|
26
|
+
* Can used for interceptors or middlewares
|
|
27
|
+
*/
|
|
28
|
+
export declare function onError<TError, TOptions extends {
|
|
29
|
+
next(): any;
|
|
30
|
+
}, TRest extends any[]>(callback: NoInfer<(error: TError, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>> & {
|
|
31
|
+
__error?: {
|
|
32
|
+
type: TError;
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
export type OnFinishState<TResult, TError> = [TResult, undefined, 'success'] | [undefined, TError, 'error'];
|
|
36
|
+
/**
|
|
37
|
+
* Can used for interceptors or middlewares
|
|
38
|
+
*/
|
|
39
|
+
export declare function onFinish<TError, TOptions extends {
|
|
40
|
+
next(): any;
|
|
41
|
+
}, TRest extends any[]>(callback: NoInfer<(state: OnFinishState<Awaited<ReturnType<TOptions['next']>>, TError>, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>> & {
|
|
42
|
+
__error?: {
|
|
43
|
+
type: TError;
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
export declare function intercept<TOptions extends InterceptableOptions, TResult, TError>(interceptors: Interceptor<TOptions, TResult, TError>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => Promisable<TResult>>): Promise<TResult>;
|
|
47
|
+
//# sourceMappingURL=interceptor.d.ts.map
|
package/dist/src/object.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type Segment = string | number;
|
|
2
|
-
export declare function set(root:
|
|
2
|
+
export declare function set(root: unknown, segments: Readonly<Segment[]>, value: unknown): unknown;
|
|
3
3
|
export declare function get(root: Readonly<Record<string, unknown> | unknown[]>, segments: Readonly<Segment[]>): unknown;
|
|
4
4
|
export declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): {
|
|
5
5
|
maps: Segment[][];
|
package/package.json
CHANGED
package/dist/src/hook.d.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import type { Arrayable, Promisable } from 'type-fest';
|
|
2
|
-
export type OnStartState<TInput> = {
|
|
3
|
-
status: 'pending';
|
|
4
|
-
input: TInput;
|
|
5
|
-
output: undefined;
|
|
6
|
-
error: undefined;
|
|
7
|
-
};
|
|
8
|
-
export type OnSuccessState<TInput, TOutput> = {
|
|
9
|
-
status: 'success';
|
|
10
|
-
input: TInput;
|
|
11
|
-
output: TOutput;
|
|
12
|
-
error: undefined;
|
|
13
|
-
};
|
|
14
|
-
export type OnErrorState<TInput> = {
|
|
15
|
-
status: 'error';
|
|
16
|
-
input: TInput;
|
|
17
|
-
output: undefined;
|
|
18
|
-
error: Error;
|
|
19
|
-
};
|
|
20
|
-
export interface BaseHookMeta<TOutput> {
|
|
21
|
-
next: () => Promise<TOutput>;
|
|
22
|
-
}
|
|
23
|
-
export interface Hooks<TInput, TOutput, TContext, TMeta extends (Record<string, any> & {
|
|
24
|
-
next?: never;
|
|
25
|
-
}) | undefined> {
|
|
26
|
-
interceptor?: Arrayable<(input: TInput, context: TContext, meta: (TMeta extends undefined ? unknown : TMeta) & BaseHookMeta<TOutput>) => Promise<TOutput>>;
|
|
27
|
-
onStart?: Arrayable<(state: OnStartState<TInput>, context: TContext, meta: TMeta) => Promisable<void>>;
|
|
28
|
-
onSuccess?: Arrayable<(state: OnSuccessState<TInput, TOutput>, context: TContext, meta: TMeta) => Promisable<void>>;
|
|
29
|
-
onError?: Arrayable<(state: OnErrorState<TInput>, context: TContext, meta: TMeta) => Promisable<void>>;
|
|
30
|
-
onFinish?: Arrayable<(state: OnSuccessState<TInput, TOutput> | OnErrorState<TInput>, context: TContext, meta: TMeta) => Promisable<void>>;
|
|
31
|
-
}
|
|
32
|
-
export declare function executeWithHooks<TInput, TOutput, TContext, TMeta extends (Record<string, any> & {
|
|
33
|
-
next?: never;
|
|
34
|
-
}) | undefined>(options: {
|
|
35
|
-
hooks?: Hooks<TInput, TOutput, TContext, TMeta>;
|
|
36
|
-
input: TInput;
|
|
37
|
-
context: TContext;
|
|
38
|
-
meta: TMeta;
|
|
39
|
-
execute: BaseHookMeta<TOutput>['next'];
|
|
40
|
-
}): Promise<TOutput>;
|
|
41
|
-
export declare function convertToArray<T>(value: undefined | T | readonly T[]): readonly T[];
|
|
42
|
-
//# sourceMappingURL=hook.d.ts.map
|