@orpc/shared 0.33.0 → 0.35.0
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 +75 -2
- package/dist/src/function.d.ts +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/interceptor.d.ts +47 -0
- package/dist/src/object.d.ts +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -22,6 +22,19 @@ function toError(error) {
|
|
|
22
22
|
return new Error("Unknown error", { cause: error });
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
// src/function.ts
|
|
26
|
+
function once(fn) {
|
|
27
|
+
let cached;
|
|
28
|
+
return () => {
|
|
29
|
+
if (cached) {
|
|
30
|
+
return cached.result;
|
|
31
|
+
}
|
|
32
|
+
const result = fn();
|
|
33
|
+
cached = { result };
|
|
34
|
+
return result;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
25
38
|
// src/hook.ts
|
|
26
39
|
async function executeWithHooks(options) {
|
|
27
40
|
const interceptors = convertToArray(options.hooks?.interceptor);
|
|
@@ -41,8 +54,8 @@ async function executeWithHooks(options) {
|
|
|
41
54
|
}
|
|
42
55
|
let state = { status: "pending", input: options.input, output: void 0, error: void 0 };
|
|
43
56
|
try {
|
|
44
|
-
for (const
|
|
45
|
-
await
|
|
57
|
+
for (const onStart2 of onStarts) {
|
|
58
|
+
await onStart2(state, options.context, options.meta);
|
|
46
59
|
}
|
|
47
60
|
const output = await options.execute();
|
|
48
61
|
state = { status: "success", input: options.input, output, error: void 0 };
|
|
@@ -80,6 +93,60 @@ function convertToArray(value2) {
|
|
|
80
93
|
return Array.isArray(value2) ? value2 : [value2];
|
|
81
94
|
}
|
|
82
95
|
|
|
96
|
+
// src/interceptor.ts
|
|
97
|
+
function onStart(callback) {
|
|
98
|
+
return async (options, ...rest) => {
|
|
99
|
+
await callback(options, ...rest);
|
|
100
|
+
return await options.next();
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function onSuccess(callback) {
|
|
104
|
+
return async (options, ...rest) => {
|
|
105
|
+
const result = await options.next();
|
|
106
|
+
await callback(result, options, ...rest);
|
|
107
|
+
return result;
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function onError(callback) {
|
|
111
|
+
return async (options, ...rest) => {
|
|
112
|
+
try {
|
|
113
|
+
return await options.next();
|
|
114
|
+
} catch (error) {
|
|
115
|
+
await callback(error, options, ...rest);
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
function onFinish(callback) {
|
|
121
|
+
let state;
|
|
122
|
+
return async (options, ...rest) => {
|
|
123
|
+
try {
|
|
124
|
+
const result = await options.next();
|
|
125
|
+
state = [result, void 0, "success"];
|
|
126
|
+
return result;
|
|
127
|
+
} catch (error) {
|
|
128
|
+
state = [void 0, error, "error"];
|
|
129
|
+
throw error;
|
|
130
|
+
} finally {
|
|
131
|
+
await callback(state, options, ...rest);
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
async function intercept(interceptors, options, main) {
|
|
136
|
+
let index = 0;
|
|
137
|
+
const next = async (nextOptions = options) => {
|
|
138
|
+
const interceptor = interceptors[index++];
|
|
139
|
+
if (!interceptor) {
|
|
140
|
+
return await main(nextOptions);
|
|
141
|
+
}
|
|
142
|
+
return await interceptor({
|
|
143
|
+
...nextOptions,
|
|
144
|
+
next
|
|
145
|
+
});
|
|
146
|
+
};
|
|
147
|
+
return await next();
|
|
148
|
+
}
|
|
149
|
+
|
|
83
150
|
// src/json.ts
|
|
84
151
|
function parseJSONSafely(text) {
|
|
85
152
|
if (text === "")
|
|
@@ -185,10 +252,16 @@ export {
|
|
|
185
252
|
get,
|
|
186
253
|
group,
|
|
187
254
|
guard,
|
|
255
|
+
intercept,
|
|
188
256
|
isPlainObject3 as isPlainObject,
|
|
189
257
|
mapEntries,
|
|
190
258
|
mapValues,
|
|
191
259
|
omit,
|
|
260
|
+
onError,
|
|
261
|
+
onFinish,
|
|
262
|
+
onStart,
|
|
263
|
+
onSuccess,
|
|
264
|
+
once,
|
|
192
265
|
parseJSONSafely,
|
|
193
266
|
set,
|
|
194
267
|
toError,
|
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[][];
|