@orpc/shared 0.0.0-next.68378b4 → 0.0.0-next.69674a7
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/README.md +5 -0
- package/dist/index.d.mts +3 -4
- package/dist/index.d.ts +3 -4
- package/dist/index.mjs +41 -66
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
> [!WARNING]
|
|
2
|
+
>
|
|
3
|
+
> `@orpc/shared` is an internal dependency of oRPC packages. It does not follow semver and may change at any time without notice.
|
|
4
|
+
> Please do not use it in your project.
|
|
5
|
+
|
|
1
6
|
<div align="center">
|
|
2
7
|
<image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" />
|
|
3
8
|
</div>
|
package/dist/index.d.mts
CHANGED
|
@@ -42,7 +42,7 @@ declare function onError<TError, TOptions extends {
|
|
|
42
42
|
type: TError;
|
|
43
43
|
};
|
|
44
44
|
};
|
|
45
|
-
type OnFinishState<TResult, TError> = [TResult,
|
|
45
|
+
type OnFinishState<TResult, TError> = [TResult, null, 'success'] | [undefined, TError, 'error'];
|
|
46
46
|
/**
|
|
47
47
|
* Can used for interceptors or middlewares
|
|
48
48
|
*/
|
|
@@ -61,8 +61,6 @@ declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
|
61
61
|
declare function stringifyJSON<T>(value: T): undefined extends T ? undefined | string : string;
|
|
62
62
|
|
|
63
63
|
type Segment = string | number;
|
|
64
|
-
declare function set(root: unknown, segments: Readonly<Segment[]>, value: unknown): unknown;
|
|
65
|
-
declare function get(root: Readonly<Record<string, unknown> | unknown[]>, segments: Readonly<Segment[]>): unknown;
|
|
66
64
|
declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): {
|
|
67
65
|
maps: Segment[][];
|
|
68
66
|
values: unknown[];
|
|
@@ -75,6 +73,7 @@ declare function isObject(value: unknown): value is Record<PropertyKey, unknown>
|
|
|
75
73
|
* Check if the value satisfy a `object` type in typescript
|
|
76
74
|
*/
|
|
77
75
|
declare function isTypescriptObject(value: unknown): value is object & Record<PropertyKey, unknown>;
|
|
76
|
+
declare function clone<T>(value: T): T;
|
|
78
77
|
|
|
79
78
|
type MaybeOptionalOptions<TOptions> = Record<never, never> extends TOptions ? [options?: TOptions] : [options: TOptions];
|
|
80
79
|
type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
@@ -82,4 +81,4 @@ type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
|
82
81
|
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => Promisable<T>);
|
|
83
82
|
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): Promise<T extends Value<infer U, any> ? U : never>;
|
|
84
83
|
|
|
85
|
-
export { type AnyFunction, type InterceptableOptions, type Interceptor, type InterceptorOptions, type MaybeOptionalOptions, type OmitChainMethodDeep, type OnFinishState, type Segment, type SetOptional, type Value,
|
|
84
|
+
export { type AnyFunction, type InterceptableOptions, type Interceptor, type InterceptorOptions, type MaybeOptionalOptions, type OmitChainMethodDeep, type OnFinishState, type Segment, type SetOptional, type Value, clone, findDeepMatches, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, stringifyJSON, toError, value };
|
package/dist/index.d.ts
CHANGED
|
@@ -42,7 +42,7 @@ declare function onError<TError, TOptions extends {
|
|
|
42
42
|
type: TError;
|
|
43
43
|
};
|
|
44
44
|
};
|
|
45
|
-
type OnFinishState<TResult, TError> = [TResult,
|
|
45
|
+
type OnFinishState<TResult, TError> = [TResult, null, 'success'] | [undefined, TError, 'error'];
|
|
46
46
|
/**
|
|
47
47
|
* Can used for interceptors or middlewares
|
|
48
48
|
*/
|
|
@@ -61,8 +61,6 @@ declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
|
61
61
|
declare function stringifyJSON<T>(value: T): undefined extends T ? undefined | string : string;
|
|
62
62
|
|
|
63
63
|
type Segment = string | number;
|
|
64
|
-
declare function set(root: unknown, segments: Readonly<Segment[]>, value: unknown): unknown;
|
|
65
|
-
declare function get(root: Readonly<Record<string, unknown> | unknown[]>, segments: Readonly<Segment[]>): unknown;
|
|
66
64
|
declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): {
|
|
67
65
|
maps: Segment[][];
|
|
68
66
|
values: unknown[];
|
|
@@ -75,6 +73,7 @@ declare function isObject(value: unknown): value is Record<PropertyKey, unknown>
|
|
|
75
73
|
* Check if the value satisfy a `object` type in typescript
|
|
76
74
|
*/
|
|
77
75
|
declare function isTypescriptObject(value: unknown): value is object & Record<PropertyKey, unknown>;
|
|
76
|
+
declare function clone<T>(value: T): T;
|
|
78
77
|
|
|
79
78
|
type MaybeOptionalOptions<TOptions> = Record<never, never> extends TOptions ? [options?: TOptions] : [options: TOptions];
|
|
80
79
|
type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
@@ -82,4 +81,4 @@ type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
|
82
81
|
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => Promisable<T>);
|
|
83
82
|
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): Promise<T extends Value<infer U, any> ? U : never>;
|
|
84
83
|
|
|
85
|
-
export { type AnyFunction, type InterceptableOptions, type Interceptor, type InterceptorOptions, type MaybeOptionalOptions, type OmitChainMethodDeep, type OnFinishState, type Segment, type SetOptional, type Value,
|
|
84
|
+
export { type AnyFunction, type InterceptableOptions, type Interceptor, type InterceptorOptions, type MaybeOptionalOptions, type OmitChainMethodDeep, type OnFinishState, type Segment, type SetOptional, type Value, clone, findDeepMatches, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, stringifyJSON, toError, value };
|
package/dist/index.mjs
CHANGED
|
@@ -1,73 +1,9 @@
|
|
|
1
1
|
export { group, guard, mapEntries, mapValues, omit, retry, trim } from 'radash';
|
|
2
2
|
|
|
3
|
-
function set(root, segments, value) {
|
|
4
|
-
const ref = { root };
|
|
5
|
-
let currentRef = ref;
|
|
6
|
-
let preSegment = "root";
|
|
7
|
-
for (const segment of segments) {
|
|
8
|
-
currentRef = currentRef[preSegment];
|
|
9
|
-
preSegment = segment;
|
|
10
|
-
}
|
|
11
|
-
currentRef[preSegment] = value;
|
|
12
|
-
return ref.root;
|
|
13
|
-
}
|
|
14
|
-
function get(root, segments) {
|
|
15
|
-
const ref = { root };
|
|
16
|
-
let currentRef = ref;
|
|
17
|
-
let preSegment = "root";
|
|
18
|
-
for (const segment of segments) {
|
|
19
|
-
if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) {
|
|
20
|
-
return void 0;
|
|
21
|
-
}
|
|
22
|
-
currentRef = currentRef[preSegment];
|
|
23
|
-
preSegment = segment;
|
|
24
|
-
}
|
|
25
|
-
if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) {
|
|
26
|
-
return void 0;
|
|
27
|
-
}
|
|
28
|
-
return currentRef[preSegment];
|
|
29
|
-
}
|
|
30
|
-
function findDeepMatches(check, payload, segments = [], maps = [], values = []) {
|
|
31
|
-
if (check(payload)) {
|
|
32
|
-
maps.push(segments);
|
|
33
|
-
values.push(payload);
|
|
34
|
-
} else if (Array.isArray(payload)) {
|
|
35
|
-
payload.forEach((v, i) => {
|
|
36
|
-
findDeepMatches(check, v, [...segments, i], maps, values);
|
|
37
|
-
});
|
|
38
|
-
} else if (isObject(payload)) {
|
|
39
|
-
for (const key in payload) {
|
|
40
|
-
findDeepMatches(check, payload[key], [...segments, key], maps, values);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return { maps, values };
|
|
44
|
-
}
|
|
45
|
-
function isObject(value) {
|
|
46
|
-
if (!value || typeof value !== "object") {
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
const proto = Object.getPrototypeOf(value);
|
|
50
|
-
return proto === Object.prototype || !proto || !proto.constructor;
|
|
51
|
-
}
|
|
52
|
-
function isTypescriptObject(value) {
|
|
53
|
-
return !!value && (typeof value === "object" || typeof value === "function");
|
|
54
|
-
}
|
|
55
|
-
|
|
56
3
|
function toError(error) {
|
|
57
4
|
if (error instanceof Error) {
|
|
58
5
|
return error;
|
|
59
6
|
}
|
|
60
|
-
if (typeof error === "string") {
|
|
61
|
-
return new Error(error, { cause: error });
|
|
62
|
-
}
|
|
63
|
-
if (isObject(error)) {
|
|
64
|
-
if ("message" in error && typeof error.message === "string") {
|
|
65
|
-
return new Error(error.message, { cause: error });
|
|
66
|
-
}
|
|
67
|
-
if ("name" in error && typeof error.name === "string") {
|
|
68
|
-
return new Error(error.name, { cause: error });
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
7
|
return new Error("Unknown error", { cause: error });
|
|
72
8
|
}
|
|
73
9
|
|
|
@@ -111,7 +47,7 @@ function onFinish(callback) {
|
|
|
111
47
|
return async (options, ...rest) => {
|
|
112
48
|
try {
|
|
113
49
|
const result = await options.next();
|
|
114
|
-
state = [result,
|
|
50
|
+
state = [result, null, "success"];
|
|
115
51
|
return result;
|
|
116
52
|
} catch (error) {
|
|
117
53
|
state = [void 0, error, "error"];
|
|
@@ -153,6 +89,45 @@ function stringifyJSON(value) {
|
|
|
153
89
|
return JSON.stringify(value);
|
|
154
90
|
}
|
|
155
91
|
|
|
92
|
+
function findDeepMatches(check, payload, segments = [], maps = [], values = []) {
|
|
93
|
+
if (check(payload)) {
|
|
94
|
+
maps.push(segments);
|
|
95
|
+
values.push(payload);
|
|
96
|
+
} else if (Array.isArray(payload)) {
|
|
97
|
+
payload.forEach((v, i) => {
|
|
98
|
+
findDeepMatches(check, v, [...segments, i], maps, values);
|
|
99
|
+
});
|
|
100
|
+
} else if (isObject(payload)) {
|
|
101
|
+
for (const key in payload) {
|
|
102
|
+
findDeepMatches(check, payload[key], [...segments, key], maps, values);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return { maps, values };
|
|
106
|
+
}
|
|
107
|
+
function isObject(value) {
|
|
108
|
+
if (!value || typeof value !== "object") {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
const proto = Object.getPrototypeOf(value);
|
|
112
|
+
return proto === Object.prototype || !proto || !proto.constructor;
|
|
113
|
+
}
|
|
114
|
+
function isTypescriptObject(value) {
|
|
115
|
+
return !!value && (typeof value === "object" || typeof value === "function");
|
|
116
|
+
}
|
|
117
|
+
function clone(value) {
|
|
118
|
+
if (Array.isArray(value)) {
|
|
119
|
+
return value.map(clone);
|
|
120
|
+
}
|
|
121
|
+
if (isObject(value)) {
|
|
122
|
+
const result = {};
|
|
123
|
+
for (const key in value) {
|
|
124
|
+
result[key] = clone(value[key]);
|
|
125
|
+
}
|
|
126
|
+
return result;
|
|
127
|
+
}
|
|
128
|
+
return value;
|
|
129
|
+
}
|
|
130
|
+
|
|
156
131
|
function value(value2, ...args) {
|
|
157
132
|
if (typeof value2 === "function") {
|
|
158
133
|
return value2(...args);
|
|
@@ -160,4 +135,4 @@ function value(value2, ...args) {
|
|
|
160
135
|
return value2;
|
|
161
136
|
}
|
|
162
137
|
|
|
163
|
-
export {
|
|
138
|
+
export { clone, findDeepMatches, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, stringifyJSON, toError, value };
|