@orpc/shared 0.0.0-next.f56d2b3 → 0.0.0-next.fd0ca3d
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 +86 -129
- package/dist/src/index.d.ts +4 -7
- package/dist/src/interceptor.d.ts +45 -0
- package/dist/src/object.d.ts +5 -1
- package/dist/src/types.d.ts +3 -0
- package/dist/src/value.d.ts +1 -1
- package/package.json +1 -3
- package/dist/src/constants.d.ts +0 -3
- package/dist/src/hook.d.ts +0 -42
- package/dist/src/json.d.ts +0 -2
- package/dist/src/proxy.d.ts +0 -3
package/dist/index.js
CHANGED
|
@@ -1,98 +1,4 @@
|
|
|
1
|
-
// src/constants.ts
|
|
2
|
-
var ORPC_HANDLER_HEADER = "x-orpc-handler";
|
|
3
|
-
var ORPC_HANDLER_VALUE = "orpc";
|
|
4
|
-
|
|
5
|
-
// src/error.ts
|
|
6
|
-
import { isPlainObject } from "is-what";
|
|
7
|
-
function toError(error) {
|
|
8
|
-
if (error instanceof Error) {
|
|
9
|
-
return error;
|
|
10
|
-
}
|
|
11
|
-
if (typeof error === "string") {
|
|
12
|
-
return new Error(error, { cause: error });
|
|
13
|
-
}
|
|
14
|
-
if (isPlainObject(error)) {
|
|
15
|
-
if ("message" in error && typeof error.message === "string") {
|
|
16
|
-
return new Error(error.message, { cause: error });
|
|
17
|
-
}
|
|
18
|
-
if ("name" in error && typeof error.name === "string") {
|
|
19
|
-
return new Error(error.name, { cause: error });
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
return new Error("Unknown error", { cause: error });
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// src/hook.ts
|
|
26
|
-
async function executeWithHooks(options) {
|
|
27
|
-
const interceptors = convertToArray(options.hooks?.interceptor);
|
|
28
|
-
const onStarts = convertToArray(options.hooks?.onStart);
|
|
29
|
-
const onSuccesses = convertToArray(options.hooks?.onSuccess);
|
|
30
|
-
const onErrors = convertToArray(options.hooks?.onError);
|
|
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
|
-
});
|
|
41
|
-
}
|
|
42
|
-
let state = { status: "pending", input: options.input, output: void 0, error: void 0 };
|
|
43
|
-
try {
|
|
44
|
-
for (const onStart of onStarts) {
|
|
45
|
-
await onStart(state, options.context, options.meta);
|
|
46
|
-
}
|
|
47
|
-
const output = await options.execute();
|
|
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
|
-
}
|
|
61
|
-
}
|
|
62
|
-
for (let i = onFinishes.length - 1; i >= 0; i--) {
|
|
63
|
-
try {
|
|
64
|
-
await onFinishes[i](state, options.context, options.meta);
|
|
65
|
-
} catch (e) {
|
|
66
|
-
state = { status: "error", input: options.input, error: toError(e), output: void 0 };
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
if (state.status === "error") {
|
|
70
|
-
throw state.error;
|
|
71
|
-
}
|
|
72
|
-
return state.output;
|
|
73
|
-
};
|
|
74
|
-
return await next();
|
|
75
|
-
}
|
|
76
|
-
function convertToArray(value2) {
|
|
77
|
-
if (value2 === void 0) {
|
|
78
|
-
return [];
|
|
79
|
-
}
|
|
80
|
-
return Array.isArray(value2) ? value2 : [value2];
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// src/json.ts
|
|
84
|
-
function parseJSONSafely(text) {
|
|
85
|
-
if (text === "")
|
|
86
|
-
return void 0;
|
|
87
|
-
try {
|
|
88
|
-
return JSON.parse(text);
|
|
89
|
-
} catch {
|
|
90
|
-
return text;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
1
|
// src/object.ts
|
|
95
|
-
import { isPlainObject as isPlainObject2 } from "is-what";
|
|
96
2
|
function set(root, segments, value2) {
|
|
97
3
|
const ref = { root };
|
|
98
4
|
let currentRef = ref;
|
|
@@ -128,40 +34,92 @@ function findDeepMatches(check, payload, segments = [], maps = [], values = [])
|
|
|
128
34
|
payload.forEach((v, i) => {
|
|
129
35
|
findDeepMatches(check, v, [...segments, i], maps, values);
|
|
130
36
|
});
|
|
131
|
-
} else if (
|
|
37
|
+
} else if (isObject(payload)) {
|
|
132
38
|
for (const key in payload) {
|
|
133
39
|
findDeepMatches(check, payload[key], [...segments, key], maps, values);
|
|
134
40
|
}
|
|
135
41
|
}
|
|
136
42
|
return { maps, values };
|
|
137
43
|
}
|
|
44
|
+
function isObject(value2) {
|
|
45
|
+
if (!value2 || typeof value2 !== "object") {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
const proto = Object.getPrototypeOf(value2);
|
|
49
|
+
return proto === Object.prototype || !proto || !proto.constructor;
|
|
50
|
+
}
|
|
138
51
|
|
|
139
|
-
// src/
|
|
140
|
-
function
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
return
|
|
153
|
-
},
|
|
154
|
-
defineProperty(_, key, descriptor) {
|
|
155
|
-
return Reflect.defineProperty(obj, key, descriptor);
|
|
156
|
-
},
|
|
157
|
-
set(_, key, value2) {
|
|
158
|
-
return Reflect.set(obj, key, value2);
|
|
159
|
-
},
|
|
160
|
-
deleteProperty(target, key) {
|
|
161
|
-
return Reflect.deleteProperty(target, key) && Reflect.deleteProperty(obj, key);
|
|
52
|
+
// src/error.ts
|
|
53
|
+
function toError(error) {
|
|
54
|
+
if (error instanceof Error) {
|
|
55
|
+
return error;
|
|
56
|
+
}
|
|
57
|
+
if (typeof error === "string") {
|
|
58
|
+
return new Error(error, { cause: error });
|
|
59
|
+
}
|
|
60
|
+
if (isObject(error)) {
|
|
61
|
+
if ("message" in error && typeof error.message === "string") {
|
|
62
|
+
return new Error(error.message, { cause: error });
|
|
63
|
+
}
|
|
64
|
+
if ("name" in error && typeof error.name === "string") {
|
|
65
|
+
return new Error(error.name, { cause: error });
|
|
162
66
|
}
|
|
163
|
-
}
|
|
164
|
-
return
|
|
67
|
+
}
|
|
68
|
+
return new Error("Unknown error", { cause: error });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// src/interceptor.ts
|
|
72
|
+
function onStart(callback) {
|
|
73
|
+
return async (options, ...rest) => {
|
|
74
|
+
await callback(options, ...rest);
|
|
75
|
+
return await options.next();
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function onSuccess(callback) {
|
|
79
|
+
return async (options, ...rest) => {
|
|
80
|
+
const result = await options.next();
|
|
81
|
+
await callback(result, options, ...rest);
|
|
82
|
+
return result;
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function onError(callback) {
|
|
86
|
+
return async (options, ...rest) => {
|
|
87
|
+
try {
|
|
88
|
+
return await options.next();
|
|
89
|
+
} catch (error) {
|
|
90
|
+
await callback(error, options, ...rest);
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function onFinish(callback) {
|
|
96
|
+
let state;
|
|
97
|
+
return async (options, ...rest) => {
|
|
98
|
+
try {
|
|
99
|
+
const result = await options.next();
|
|
100
|
+
state = [result, void 0, "success"];
|
|
101
|
+
return result;
|
|
102
|
+
} catch (error) {
|
|
103
|
+
state = [void 0, error, "error"];
|
|
104
|
+
throw error;
|
|
105
|
+
} finally {
|
|
106
|
+
await callback(state, options, ...rest);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
async function intercept(interceptors, options, main) {
|
|
111
|
+
let index = 0;
|
|
112
|
+
const next = async (options2) => {
|
|
113
|
+
const interceptor = interceptors[index++];
|
|
114
|
+
if (!interceptor) {
|
|
115
|
+
return await main(options2);
|
|
116
|
+
}
|
|
117
|
+
return await interceptor({
|
|
118
|
+
...options2,
|
|
119
|
+
next: (newOptions = options2) => next(newOptions)
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
return await next(options);
|
|
165
123
|
}
|
|
166
124
|
|
|
167
125
|
// src/value.ts
|
|
@@ -173,23 +131,22 @@ function value(value2, ...args) {
|
|
|
173
131
|
}
|
|
174
132
|
|
|
175
133
|
// src/index.ts
|
|
176
|
-
import {
|
|
177
|
-
import { group, guard, mapEntries, mapValues, omit, trim } from "radash";
|
|
134
|
+
import { group, guard, mapEntries, mapValues, omit, retry, trim } from "radash";
|
|
178
135
|
export {
|
|
179
|
-
ORPC_HANDLER_HEADER,
|
|
180
|
-
ORPC_HANDLER_VALUE,
|
|
181
|
-
convertToArray,
|
|
182
|
-
createCallableObject,
|
|
183
|
-
executeWithHooks,
|
|
184
136
|
findDeepMatches,
|
|
185
137
|
get,
|
|
186
138
|
group,
|
|
187
139
|
guard,
|
|
188
|
-
|
|
140
|
+
intercept,
|
|
141
|
+
isObject,
|
|
189
142
|
mapEntries,
|
|
190
143
|
mapValues,
|
|
191
144
|
omit,
|
|
192
|
-
|
|
145
|
+
onError,
|
|
146
|
+
onFinish,
|
|
147
|
+
onStart,
|
|
148
|
+
onSuccess,
|
|
149
|
+
retry,
|
|
193
150
|
set,
|
|
194
151
|
toError,
|
|
195
152
|
trim,
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
export * from './chain';
|
|
2
|
-
export * from './constants';
|
|
3
2
|
export * from './error';
|
|
4
3
|
export * from './function';
|
|
5
|
-
export * from './
|
|
6
|
-
export * from './json';
|
|
4
|
+
export * from './interceptor';
|
|
7
5
|
export * from './object';
|
|
8
|
-
export * from './
|
|
6
|
+
export * from './types';
|
|
9
7
|
export * from './value';
|
|
10
|
-
export {
|
|
11
|
-
export {
|
|
12
|
-
export type * from 'type-fest';
|
|
8
|
+
export { group, guard, mapEntries, mapValues, omit, retry, trim } from 'radash';
|
|
9
|
+
export type { IsEqual, IsNever, PartialDeep, Promisable } from 'type-fest';
|
|
13
10
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Promisable } from 'type-fest';
|
|
2
|
+
export type InterceptableOptions = Record<string, any>;
|
|
3
|
+
export type InterceptorOptions<TOptions extends InterceptableOptions, TResult> = Omit<TOptions, 'next'> & {
|
|
4
|
+
next(options?: TOptions): Promise<TResult>;
|
|
5
|
+
};
|
|
6
|
+
export type Interceptor<TOptions extends InterceptableOptions, TResult, TError> = (options: InterceptorOptions<TOptions, TResult>) => Promise<TResult> & {
|
|
7
|
+
__error?: {
|
|
8
|
+
type: TError;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Can used for interceptors or middlewares
|
|
13
|
+
*/
|
|
14
|
+
export declare function onStart<TOptions extends {
|
|
15
|
+
next(): any;
|
|
16
|
+
}, TRest extends any[]>(callback: NoInfer<(options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
17
|
+
/**
|
|
18
|
+
* Can used for interceptors or middlewares
|
|
19
|
+
*/
|
|
20
|
+
export declare function onSuccess<TOptions extends {
|
|
21
|
+
next(): any;
|
|
22
|
+
}, 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']>>>;
|
|
23
|
+
/**
|
|
24
|
+
* Can used for interceptors or middlewares
|
|
25
|
+
*/
|
|
26
|
+
export declare function onError<TError, TOptions extends {
|
|
27
|
+
next(): any;
|
|
28
|
+
}, TRest extends any[]>(callback: NoInfer<(error: TError, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>> & {
|
|
29
|
+
__error?: {
|
|
30
|
+
type: TError;
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
export type OnFinishState<TResult, TError> = [TResult, undefined, 'success'] | [undefined, TError, 'error'];
|
|
34
|
+
/**
|
|
35
|
+
* Can used for interceptors or middlewares
|
|
36
|
+
*/
|
|
37
|
+
export declare function onFinish<TError, TOptions extends {
|
|
38
|
+
next(): any;
|
|
39
|
+
}, 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']>>> & {
|
|
40
|
+
__error?: {
|
|
41
|
+
type: TError;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
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>;
|
|
45
|
+
//# sourceMappingURL=interceptor.d.ts.map
|
package/dist/src/object.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
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[][];
|
|
6
6
|
values: unknown[];
|
|
7
7
|
};
|
|
8
|
+
/**
|
|
9
|
+
* Check if the value is an object even it created by `Object.create(null)` or more tricky way.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isObject(value: unknown): value is Record<PropertyKey, unknown>;
|
|
8
12
|
//# sourceMappingURL=object.d.ts.map
|
package/dist/src/value.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { Promisable } from 'type-fest';
|
|
2
2
|
export type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => Promisable<T>);
|
|
3
|
-
export declare function value<T
|
|
3
|
+
export declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): Promise<T extends Value<infer U, any> ? U : never>;
|
|
4
4
|
//# sourceMappingURL=value.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/shared",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.0-next.
|
|
4
|
+
"version": "0.0.0-next.fd0ca3d",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -29,8 +29,6 @@
|
|
|
29
29
|
"dist"
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@standard-schema/spec": "1.0.0-beta.4",
|
|
33
|
-
"is-what": "^5.0.2",
|
|
34
32
|
"radash": "^12.1.0",
|
|
35
33
|
"type-fest": "^4.26.1"
|
|
36
34
|
},
|
package/dist/src/constants.d.ts
DELETED
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
|
package/dist/src/json.d.ts
DELETED
package/dist/src/proxy.d.ts
DELETED