@orpc/shared 0.0.0-next.cba521d → 0.0.0-next.ccd4e42
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 +76 -2
- package/dist/src/chain.d.ts +5 -0
- package/dist/src/function.d.ts +1 -0
- package/dist/src/hook.d.ts +1 -1
- package/dist/src/index.d.ts +2 -0
- package/dist/src/interceptor.d.ts +50 -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,61 @@ 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(interceptable, options, main) {
|
|
136
|
+
const interceptors = interceptable.interceptors ?? [];
|
|
137
|
+
let index = 0;
|
|
138
|
+
const next = async (nextOptions = options) => {
|
|
139
|
+
const interceptor = interceptors[index++];
|
|
140
|
+
if (!interceptor) {
|
|
141
|
+
return await main(nextOptions);
|
|
142
|
+
}
|
|
143
|
+
return await interceptor({
|
|
144
|
+
...nextOptions,
|
|
145
|
+
next
|
|
146
|
+
});
|
|
147
|
+
};
|
|
148
|
+
return await next();
|
|
149
|
+
}
|
|
150
|
+
|
|
83
151
|
// src/json.ts
|
|
84
152
|
function parseJSONSafely(text) {
|
|
85
153
|
if (text === "")
|
|
@@ -185,10 +253,16 @@ export {
|
|
|
185
253
|
get,
|
|
186
254
|
group,
|
|
187
255
|
guard,
|
|
256
|
+
intercept,
|
|
188
257
|
isPlainObject3 as isPlainObject,
|
|
189
258
|
mapEntries,
|
|
190
259
|
mapValues,
|
|
191
260
|
omit,
|
|
261
|
+
onError,
|
|
262
|
+
onFinish,
|
|
263
|
+
onStart,
|
|
264
|
+
onSuccess,
|
|
265
|
+
once,
|
|
192
266
|
parseJSONSafely,
|
|
193
267
|
set,
|
|
194
268
|
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/hook.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ export type OnErrorState<TInput> = {
|
|
|
18
18
|
error: Error;
|
|
19
19
|
};
|
|
20
20
|
export interface BaseHookMeta<TOutput> {
|
|
21
|
-
next
|
|
21
|
+
next(): Promise<TOutput>;
|
|
22
22
|
}
|
|
23
23
|
export interface Hooks<TInput, TOutput, TContext, TMeta extends (Record<string, any> & {
|
|
24
24
|
next?: never;
|
package/dist/src/index.d.ts
CHANGED
|
@@ -0,0 +1,50 @@
|
|
|
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
|
+
export interface Interceptable<TOptions extends InterceptableOptions, TResult, TError> {
|
|
13
|
+
interceptors?: Interceptor<TOptions, TResult, TError>[];
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Can used for interceptors or middlewares
|
|
17
|
+
*/
|
|
18
|
+
export declare function onStart<TOptions extends {
|
|
19
|
+
next(): any;
|
|
20
|
+
}, TRest extends any[]>(callback: NoInfer<(options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
21
|
+
/**
|
|
22
|
+
* Can used for interceptors or middlewares
|
|
23
|
+
*/
|
|
24
|
+
export declare function onSuccess<TOptions extends {
|
|
25
|
+
next(): any;
|
|
26
|
+
}, 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']>>>;
|
|
27
|
+
export type InferError<T extends AnyFunction> = T extends Interceptor<any, any, infer TError> ? TError : unknown;
|
|
28
|
+
/**
|
|
29
|
+
* Can used for interceptors or middlewares
|
|
30
|
+
*/
|
|
31
|
+
export declare function onError<TError, TOptions extends {
|
|
32
|
+
next(): any;
|
|
33
|
+
}, TRest extends any[]>(callback: NoInfer<(error: TError, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>> & {
|
|
34
|
+
__error?: {
|
|
35
|
+
type: TError;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
export type OnFinishState<TResult, TError> = [TResult, undefined, 'success'] | [undefined, TError, 'error'];
|
|
39
|
+
/**
|
|
40
|
+
* Can used for interceptors or middlewares
|
|
41
|
+
*/
|
|
42
|
+
export declare function onFinish<TError, TOptions extends {
|
|
43
|
+
next(): any;
|
|
44
|
+
}, 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']>>> & {
|
|
45
|
+
__error?: {
|
|
46
|
+
type: TError;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
export declare function intercept<TOptions extends InterceptableOptions, TResult, TError>(interceptable: Interceptable<TOptions, TResult, TError>, options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => Promisable<TResult>>): Promise<TResult>;
|
|
50
|
+
//# 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[][];
|