@orpc/shared 0.0.0-next.6acfc62 → 0.0.0-next.6c13765
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 +59 -175
- package/dist/src/function.d.ts +0 -1
- package/dist/src/index.d.ts +3 -7
- package/dist/src/interceptor.d.ts +0 -2
- package/dist/src/object.d.ts +4 -0
- 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,9 +1,55 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
// src/object.ts
|
|
2
|
+
function set(root, segments, value2) {
|
|
3
|
+
const ref = { root };
|
|
4
|
+
let currentRef = ref;
|
|
5
|
+
let preSegment = "root";
|
|
6
|
+
for (const segment of segments) {
|
|
7
|
+
currentRef = currentRef[preSegment];
|
|
8
|
+
preSegment = segment;
|
|
9
|
+
}
|
|
10
|
+
currentRef[preSegment] = value2;
|
|
11
|
+
return ref.root;
|
|
12
|
+
}
|
|
13
|
+
function get(root, segments) {
|
|
14
|
+
const ref = { root };
|
|
15
|
+
let currentRef = ref;
|
|
16
|
+
let preSegment = "root";
|
|
17
|
+
for (const segment of segments) {
|
|
18
|
+
if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) {
|
|
19
|
+
return void 0;
|
|
20
|
+
}
|
|
21
|
+
currentRef = currentRef[preSegment];
|
|
22
|
+
preSegment = segment;
|
|
23
|
+
}
|
|
24
|
+
if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) {
|
|
25
|
+
return void 0;
|
|
26
|
+
}
|
|
27
|
+
return currentRef[preSegment];
|
|
28
|
+
}
|
|
29
|
+
function findDeepMatches(check, payload, segments = [], maps = [], values = []) {
|
|
30
|
+
if (check(payload)) {
|
|
31
|
+
maps.push(segments);
|
|
32
|
+
values.push(payload);
|
|
33
|
+
} else if (Array.isArray(payload)) {
|
|
34
|
+
payload.forEach((v, i) => {
|
|
35
|
+
findDeepMatches(check, v, [...segments, i], maps, values);
|
|
36
|
+
});
|
|
37
|
+
} else if (isObject(payload)) {
|
|
38
|
+
for (const key in payload) {
|
|
39
|
+
findDeepMatches(check, payload[key], [...segments, key], maps, values);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return { maps, values };
|
|
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
|
+
}
|
|
4
51
|
|
|
5
52
|
// src/error.ts
|
|
6
|
-
import { isPlainObject } from "is-what";
|
|
7
53
|
function toError(error) {
|
|
8
54
|
if (error instanceof Error) {
|
|
9
55
|
return error;
|
|
@@ -11,7 +57,7 @@ function toError(error) {
|
|
|
11
57
|
if (typeof error === "string") {
|
|
12
58
|
return new Error(error, { cause: error });
|
|
13
59
|
}
|
|
14
|
-
if (
|
|
60
|
+
if (isObject(error)) {
|
|
15
61
|
if ("message" in error && typeof error.message === "string") {
|
|
16
62
|
return new Error(error.message, { cause: error });
|
|
17
63
|
}
|
|
@@ -22,77 +68,6 @@ function toError(error) {
|
|
|
22
68
|
return new Error("Unknown error", { cause: error });
|
|
23
69
|
}
|
|
24
70
|
|
|
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
|
-
|
|
38
|
-
// src/hook.ts
|
|
39
|
-
async function executeWithHooks(options) {
|
|
40
|
-
const interceptors = convertToArray(options.hooks?.interceptor);
|
|
41
|
-
const onStarts = convertToArray(options.hooks?.onStart);
|
|
42
|
-
const onSuccesses = convertToArray(options.hooks?.onSuccess);
|
|
43
|
-
const onErrors = convertToArray(options.hooks?.onError);
|
|
44
|
-
const onFinishes = convertToArray(options.hooks?.onFinish);
|
|
45
|
-
let currentExecuteIndex = 0;
|
|
46
|
-
const next = async () => {
|
|
47
|
-
const execute = interceptors[currentExecuteIndex];
|
|
48
|
-
if (execute) {
|
|
49
|
-
currentExecuteIndex++;
|
|
50
|
-
return await execute(options.input, options.context, {
|
|
51
|
-
...options.meta,
|
|
52
|
-
next
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
let state = { status: "pending", input: options.input, output: void 0, error: void 0 };
|
|
56
|
-
try {
|
|
57
|
-
for (const onStart2 of onStarts) {
|
|
58
|
-
await onStart2(state, options.context, options.meta);
|
|
59
|
-
}
|
|
60
|
-
const output = await options.execute();
|
|
61
|
-
state = { status: "success", input: options.input, output, error: void 0 };
|
|
62
|
-
for (let i = onSuccesses.length - 1; i >= 0; i--) {
|
|
63
|
-
await onSuccesses[i](state, options.context, options.meta);
|
|
64
|
-
}
|
|
65
|
-
} catch (e) {
|
|
66
|
-
state = { status: "error", input: options.input, error: toError(e), output: void 0 };
|
|
67
|
-
for (let i = onErrors.length - 1; i >= 0; i--) {
|
|
68
|
-
try {
|
|
69
|
-
await onErrors[i](state, options.context, options.meta);
|
|
70
|
-
} catch (e2) {
|
|
71
|
-
state = { status: "error", input: options.input, error: toError(e2), output: void 0 };
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
for (let i = onFinishes.length - 1; i >= 0; i--) {
|
|
76
|
-
try {
|
|
77
|
-
await onFinishes[i](state, options.context, options.meta);
|
|
78
|
-
} catch (e) {
|
|
79
|
-
state = { status: "error", input: options.input, error: toError(e), output: void 0 };
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
if (state.status === "error") {
|
|
83
|
-
throw state.error;
|
|
84
|
-
}
|
|
85
|
-
return state.output;
|
|
86
|
-
};
|
|
87
|
-
return await next();
|
|
88
|
-
}
|
|
89
|
-
function convertToArray(value2) {
|
|
90
|
-
if (value2 === void 0) {
|
|
91
|
-
return [];
|
|
92
|
-
}
|
|
93
|
-
return Array.isArray(value2) ? value2 : [value2];
|
|
94
|
-
}
|
|
95
|
-
|
|
96
71
|
// src/interceptor.ts
|
|
97
72
|
function onStart(callback) {
|
|
98
73
|
return async (options, ...rest) => {
|
|
@@ -134,101 +109,17 @@ function onFinish(callback) {
|
|
|
134
109
|
}
|
|
135
110
|
async function intercept(interceptors, options, main) {
|
|
136
111
|
let index = 0;
|
|
137
|
-
const next = async (
|
|
112
|
+
const next = async (options2) => {
|
|
138
113
|
const interceptor = interceptors[index++];
|
|
139
114
|
if (!interceptor) {
|
|
140
|
-
return await main(
|
|
115
|
+
return await main(options2);
|
|
141
116
|
}
|
|
142
117
|
return await interceptor({
|
|
143
|
-
...
|
|
144
|
-
next
|
|
118
|
+
...options2,
|
|
119
|
+
next: (newOptions = options2) => next(newOptions)
|
|
145
120
|
});
|
|
146
121
|
};
|
|
147
|
-
return await next();
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// src/json.ts
|
|
151
|
-
function parseJSONSafely(text) {
|
|
152
|
-
if (text === "")
|
|
153
|
-
return void 0;
|
|
154
|
-
try {
|
|
155
|
-
return JSON.parse(text);
|
|
156
|
-
} catch {
|
|
157
|
-
return text;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// src/object.ts
|
|
162
|
-
import { isPlainObject as isPlainObject2 } from "is-what";
|
|
163
|
-
function set(root, segments, value2) {
|
|
164
|
-
const ref = { root };
|
|
165
|
-
let currentRef = ref;
|
|
166
|
-
let preSegment = "root";
|
|
167
|
-
for (const segment of segments) {
|
|
168
|
-
currentRef = currentRef[preSegment];
|
|
169
|
-
preSegment = segment;
|
|
170
|
-
}
|
|
171
|
-
currentRef[preSegment] = value2;
|
|
172
|
-
return ref.root;
|
|
173
|
-
}
|
|
174
|
-
function get(root, segments) {
|
|
175
|
-
const ref = { root };
|
|
176
|
-
let currentRef = ref;
|
|
177
|
-
let preSegment = "root";
|
|
178
|
-
for (const segment of segments) {
|
|
179
|
-
if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) {
|
|
180
|
-
return void 0;
|
|
181
|
-
}
|
|
182
|
-
currentRef = currentRef[preSegment];
|
|
183
|
-
preSegment = segment;
|
|
184
|
-
}
|
|
185
|
-
if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) {
|
|
186
|
-
return void 0;
|
|
187
|
-
}
|
|
188
|
-
return currentRef[preSegment];
|
|
189
|
-
}
|
|
190
|
-
function findDeepMatches(check, payload, segments = [], maps = [], values = []) {
|
|
191
|
-
if (check(payload)) {
|
|
192
|
-
maps.push(segments);
|
|
193
|
-
values.push(payload);
|
|
194
|
-
} else if (Array.isArray(payload)) {
|
|
195
|
-
payload.forEach((v, i) => {
|
|
196
|
-
findDeepMatches(check, v, [...segments, i], maps, values);
|
|
197
|
-
});
|
|
198
|
-
} else if (isPlainObject2(payload)) {
|
|
199
|
-
for (const key in payload) {
|
|
200
|
-
findDeepMatches(check, payload[key], [...segments, key], maps, values);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
return { maps, values };
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
// src/proxy.ts
|
|
207
|
-
function createCallableObject(obj, handler) {
|
|
208
|
-
const proxy = new Proxy(handler, {
|
|
209
|
-
has(target, key) {
|
|
210
|
-
return Reflect.has(obj, key) || Reflect.has(target, key);
|
|
211
|
-
},
|
|
212
|
-
ownKeys(target) {
|
|
213
|
-
return Array.from(new Set(Reflect.ownKeys(obj).concat(...Reflect.ownKeys(target))));
|
|
214
|
-
},
|
|
215
|
-
get(target, key) {
|
|
216
|
-
if (!Reflect.has(target, key) || Reflect.has(obj, key)) {
|
|
217
|
-
return Reflect.get(obj, key);
|
|
218
|
-
}
|
|
219
|
-
return Reflect.get(target, key);
|
|
220
|
-
},
|
|
221
|
-
defineProperty(_, key, descriptor) {
|
|
222
|
-
return Reflect.defineProperty(obj, key, descriptor);
|
|
223
|
-
},
|
|
224
|
-
set(_, key, value2) {
|
|
225
|
-
return Reflect.set(obj, key, value2);
|
|
226
|
-
},
|
|
227
|
-
deleteProperty(target, key) {
|
|
228
|
-
return Reflect.deleteProperty(target, key) && Reflect.deleteProperty(obj, key);
|
|
229
|
-
}
|
|
230
|
-
});
|
|
231
|
-
return proxy;
|
|
122
|
+
return await next(options);
|
|
232
123
|
}
|
|
233
124
|
|
|
234
125
|
// src/value.ts
|
|
@@ -240,20 +131,14 @@ function value(value2, ...args) {
|
|
|
240
131
|
}
|
|
241
132
|
|
|
242
133
|
// src/index.ts
|
|
243
|
-
import {
|
|
244
|
-
import { group, guard, mapEntries, mapValues, omit, trim } from "radash";
|
|
134
|
+
import { group, guard, mapEntries, mapValues, omit, retry, trim } from "radash";
|
|
245
135
|
export {
|
|
246
|
-
ORPC_HANDLER_HEADER,
|
|
247
|
-
ORPC_HANDLER_VALUE,
|
|
248
|
-
convertToArray,
|
|
249
|
-
createCallableObject,
|
|
250
|
-
executeWithHooks,
|
|
251
136
|
findDeepMatches,
|
|
252
137
|
get,
|
|
253
138
|
group,
|
|
254
139
|
guard,
|
|
255
140
|
intercept,
|
|
256
|
-
|
|
141
|
+
isObject,
|
|
257
142
|
mapEntries,
|
|
258
143
|
mapValues,
|
|
259
144
|
omit,
|
|
@@ -261,8 +146,7 @@ export {
|
|
|
261
146
|
onFinish,
|
|
262
147
|
onStart,
|
|
263
148
|
onSuccess,
|
|
264
|
-
|
|
265
|
-
parseJSONSafely,
|
|
149
|
+
retry,
|
|
266
150
|
set,
|
|
267
151
|
toError,
|
|
268
152
|
trim,
|
package/dist/src/function.d.ts
CHANGED
package/dist/src/index.d.ts
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
export * from './chain';
|
|
2
|
-
export * from './constants';
|
|
3
2
|
export * from './error';
|
|
4
3
|
export * from './function';
|
|
5
|
-
export * from './hook';
|
|
6
4
|
export * from './interceptor';
|
|
7
|
-
export * from './json';
|
|
8
5
|
export * from './object';
|
|
9
|
-
export * from './
|
|
6
|
+
export * from './types';
|
|
10
7
|
export * from './value';
|
|
11
|
-
export {
|
|
12
|
-
export {
|
|
13
|
-
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';
|
|
14
10
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { Promisable } from 'type-fest';
|
|
2
|
-
import type { AnyFunction } from './function';
|
|
3
2
|
export type InterceptableOptions = Record<string, any>;
|
|
4
3
|
export type InterceptorOptions<TOptions extends InterceptableOptions, TResult> = Omit<TOptions, 'next'> & {
|
|
5
4
|
next(options?: TOptions): Promise<TResult>;
|
|
@@ -21,7 +20,6 @@ export declare function onStart<TOptions extends {
|
|
|
21
20
|
export declare function onSuccess<TOptions extends {
|
|
22
21
|
next(): any;
|
|
23
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']>>>;
|
|
24
|
-
export type InferError<T extends AnyFunction> = T extends Interceptor<any, any, infer TError> ? TError : unknown;
|
|
25
23
|
/**
|
|
26
24
|
* Can used for interceptors or middlewares
|
|
27
25
|
*/
|
package/dist/src/object.d.ts
CHANGED
|
@@ -5,4 +5,8 @@ export declare function findDeepMatches(check: (value: unknown) => boolean, payl
|
|
|
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.6c13765",
|
|
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