@orpc/shared 0.0.0-next.eb37cbe → 0.0.0-next.eb3d98a
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 +76 -0
- package/dist/index.d.mts +220 -0
- package/dist/index.d.ts +220 -0
- package/dist/index.mjs +494 -0
- package/package.json +12 -19
- package/dist/chunk-CCTAECMC.js +0 -88
- package/dist/error.js +0 -11
- package/dist/index.js +0 -180
- package/dist/src/constants.d.ts +0 -3
- package/dist/src/error.d.ts +0 -65
- package/dist/src/function.d.ts +0 -2
- package/dist/src/hook.d.ts +0 -42
- package/dist/src/index.d.ts +0 -11
- package/dist/src/json.d.ts +0 -2
- package/dist/src/object.d.ts +0 -8
- package/dist/src/proxy.d.ts +0 -3
- package/dist/src/value.d.ts +0 -4
package/dist/index.js
DELETED
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
convertToStandardError
|
|
3
|
-
} from "./chunk-CCTAECMC.js";
|
|
4
|
-
|
|
5
|
-
// src/constants.ts
|
|
6
|
-
var ORPC_HANDLER_HEADER = "x-orpc-handler";
|
|
7
|
-
var ORPC_HANDLER_VALUE = "orpc";
|
|
8
|
-
|
|
9
|
-
// src/hook.ts
|
|
10
|
-
async function executeWithHooks(options) {
|
|
11
|
-
const interceptors = convertToArray(options.hooks?.interceptor);
|
|
12
|
-
const onStarts = convertToArray(options.hooks?.onStart);
|
|
13
|
-
const onSuccesses = convertToArray(options.hooks?.onSuccess);
|
|
14
|
-
const onErrors = convertToArray(options.hooks?.onError);
|
|
15
|
-
const onFinishes = convertToArray(options.hooks?.onFinish);
|
|
16
|
-
let currentExecuteIndex = 0;
|
|
17
|
-
const next = async () => {
|
|
18
|
-
const execute = interceptors[currentExecuteIndex];
|
|
19
|
-
if (execute) {
|
|
20
|
-
currentExecuteIndex++;
|
|
21
|
-
return await execute(options.input, options.context, {
|
|
22
|
-
...options.meta,
|
|
23
|
-
next
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
let state = { status: "pending", input: options.input, output: void 0, error: void 0 };
|
|
27
|
-
try {
|
|
28
|
-
for (const onStart of onStarts) {
|
|
29
|
-
await onStart(state, options.context, options.meta);
|
|
30
|
-
}
|
|
31
|
-
const output = await options.execute();
|
|
32
|
-
state = { status: "success", input: options.input, output, error: void 0 };
|
|
33
|
-
for (let i = onSuccesses.length - 1; i >= 0; i--) {
|
|
34
|
-
await onSuccesses[i](state, options.context, options.meta);
|
|
35
|
-
}
|
|
36
|
-
} catch (e) {
|
|
37
|
-
state = { status: "error", input: options.input, error: convertToStandardError(e), output: void 0 };
|
|
38
|
-
for (let i = onErrors.length - 1; i >= 0; i--) {
|
|
39
|
-
try {
|
|
40
|
-
await onErrors[i](state, options.context, options.meta);
|
|
41
|
-
} catch (e2) {
|
|
42
|
-
state = { status: "error", input: options.input, error: convertToStandardError(e2), output: void 0 };
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
for (let i = onFinishes.length - 1; i >= 0; i--) {
|
|
47
|
-
try {
|
|
48
|
-
await onFinishes[i](state, options.context, options.meta);
|
|
49
|
-
} catch (e) {
|
|
50
|
-
state = { status: "error", input: options.input, error: convertToStandardError(e), output: void 0 };
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
if (state.status === "error") {
|
|
54
|
-
throw state.error;
|
|
55
|
-
}
|
|
56
|
-
return state.output;
|
|
57
|
-
};
|
|
58
|
-
return await next();
|
|
59
|
-
}
|
|
60
|
-
function convertToArray(value2) {
|
|
61
|
-
if (value2 === void 0) {
|
|
62
|
-
return [];
|
|
63
|
-
}
|
|
64
|
-
return Array.isArray(value2) ? value2 : [value2];
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// src/json.ts
|
|
68
|
-
function parseJSONSafely(text) {
|
|
69
|
-
if (text === "")
|
|
70
|
-
return void 0;
|
|
71
|
-
try {
|
|
72
|
-
return JSON.parse(text);
|
|
73
|
-
} catch {
|
|
74
|
-
return text;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// src/object.ts
|
|
79
|
-
import { isPlainObject } from "is-what";
|
|
80
|
-
function set(root, segments, value2) {
|
|
81
|
-
const ref = { root };
|
|
82
|
-
let currentRef = ref;
|
|
83
|
-
let preSegment = "root";
|
|
84
|
-
for (const segment of segments) {
|
|
85
|
-
currentRef = currentRef[preSegment];
|
|
86
|
-
preSegment = segment;
|
|
87
|
-
}
|
|
88
|
-
currentRef[preSegment] = value2;
|
|
89
|
-
return ref.root;
|
|
90
|
-
}
|
|
91
|
-
function get(root, segments) {
|
|
92
|
-
const ref = { root };
|
|
93
|
-
let currentRef = ref;
|
|
94
|
-
let preSegment = "root";
|
|
95
|
-
for (const segment of segments) {
|
|
96
|
-
if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) {
|
|
97
|
-
return void 0;
|
|
98
|
-
}
|
|
99
|
-
currentRef = currentRef[preSegment];
|
|
100
|
-
preSegment = segment;
|
|
101
|
-
}
|
|
102
|
-
if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) {
|
|
103
|
-
return void 0;
|
|
104
|
-
}
|
|
105
|
-
return currentRef[preSegment];
|
|
106
|
-
}
|
|
107
|
-
function findDeepMatches(check, payload, segments = [], maps = [], values = []) {
|
|
108
|
-
if (check(payload)) {
|
|
109
|
-
maps.push(segments);
|
|
110
|
-
values.push(payload);
|
|
111
|
-
} else if (Array.isArray(payload)) {
|
|
112
|
-
payload.forEach((v, i) => {
|
|
113
|
-
findDeepMatches(check, v, [...segments, i], maps, values);
|
|
114
|
-
});
|
|
115
|
-
} else if (isPlainObject(payload)) {
|
|
116
|
-
for (const key in payload) {
|
|
117
|
-
findDeepMatches(check, payload[key], [...segments, key], maps, values);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
return { maps, values };
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// src/proxy.ts
|
|
124
|
-
function createCallableObject(obj, handler) {
|
|
125
|
-
const proxy = new Proxy(handler, {
|
|
126
|
-
has(target, key) {
|
|
127
|
-
return Reflect.has(obj, key) || Reflect.has(target, key);
|
|
128
|
-
},
|
|
129
|
-
ownKeys(target) {
|
|
130
|
-
return Array.from(new Set(Reflect.ownKeys(obj).concat(...Reflect.ownKeys(target))));
|
|
131
|
-
},
|
|
132
|
-
get(target, key) {
|
|
133
|
-
if (!Reflect.has(target, key) || Reflect.has(obj, key)) {
|
|
134
|
-
return Reflect.get(obj, key);
|
|
135
|
-
}
|
|
136
|
-
return Reflect.get(target, key);
|
|
137
|
-
},
|
|
138
|
-
defineProperty(_, key, descriptor) {
|
|
139
|
-
return Reflect.defineProperty(obj, key, descriptor);
|
|
140
|
-
},
|
|
141
|
-
set(_, key, value2) {
|
|
142
|
-
return Reflect.set(obj, key, value2);
|
|
143
|
-
},
|
|
144
|
-
deleteProperty(target, key) {
|
|
145
|
-
return Reflect.deleteProperty(target, key) && Reflect.deleteProperty(obj, key);
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
return proxy;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// src/value.ts
|
|
152
|
-
function value(value2) {
|
|
153
|
-
if (typeof value2 === "function") {
|
|
154
|
-
return value2();
|
|
155
|
-
}
|
|
156
|
-
return value2;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// src/index.ts
|
|
160
|
-
import { isPlainObject as isPlainObject2 } from "is-what";
|
|
161
|
-
import { guard, mapEntries, mapValues, omit, trim } from "radash";
|
|
162
|
-
export {
|
|
163
|
-
ORPC_HANDLER_HEADER,
|
|
164
|
-
ORPC_HANDLER_VALUE,
|
|
165
|
-
convertToArray,
|
|
166
|
-
createCallableObject,
|
|
167
|
-
executeWithHooks,
|
|
168
|
-
findDeepMatches,
|
|
169
|
-
get,
|
|
170
|
-
guard,
|
|
171
|
-
isPlainObject2 as isPlainObject,
|
|
172
|
-
mapEntries,
|
|
173
|
-
mapValues,
|
|
174
|
-
omit,
|
|
175
|
-
parseJSONSafely,
|
|
176
|
-
set,
|
|
177
|
-
trim,
|
|
178
|
-
value
|
|
179
|
-
};
|
|
180
|
-
//# sourceMappingURL=index.js.map
|
package/dist/src/constants.d.ts
DELETED
package/dist/src/error.d.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
-
export declare const ORPC_ERROR_CODE_STATUSES: {
|
|
3
|
-
readonly BAD_REQUEST: 400;
|
|
4
|
-
readonly UNAUTHORIZED: 401;
|
|
5
|
-
readonly FORBIDDEN: 403;
|
|
6
|
-
readonly NOT_FOUND: 404;
|
|
7
|
-
readonly METHOD_NOT_SUPPORTED: 405;
|
|
8
|
-
readonly NOT_ACCEPTABLE: 406;
|
|
9
|
-
readonly TIMEOUT: 408;
|
|
10
|
-
readonly CONFLICT: 409;
|
|
11
|
-
readonly PRECONDITION_FAILED: 412;
|
|
12
|
-
readonly PAYLOAD_TOO_LARGE: 413;
|
|
13
|
-
readonly UNSUPPORTED_MEDIA_TYPE: 415;
|
|
14
|
-
readonly UNPROCESSABLE_CONTENT: 422;
|
|
15
|
-
readonly TOO_MANY_REQUESTS: 429;
|
|
16
|
-
readonly CLIENT_CLOSED_REQUEST: 499;
|
|
17
|
-
readonly INTERNAL_SERVER_ERROR: 500;
|
|
18
|
-
readonly NOT_IMPLEMENTED: 501;
|
|
19
|
-
readonly BAD_GATEWAY: 502;
|
|
20
|
-
readonly SERVICE_UNAVAILABLE: 503;
|
|
21
|
-
readonly GATEWAY_TIMEOUT: 504;
|
|
22
|
-
};
|
|
23
|
-
export type ORPCErrorCode = keyof typeof ORPC_ERROR_CODE_STATUSES;
|
|
24
|
-
export interface ORPCErrorJSON<TCode extends ORPCErrorCode, TData> {
|
|
25
|
-
code: TCode;
|
|
26
|
-
status: number;
|
|
27
|
-
message: string;
|
|
28
|
-
data: TData;
|
|
29
|
-
issues?: readonly StandardSchemaV1.Issue[];
|
|
30
|
-
}
|
|
31
|
-
export type ANY_ORPC_ERROR_JSON = ORPCErrorJSON<any, any>;
|
|
32
|
-
export type WELL_ORPC_ERROR_JSON = ORPCErrorJSON<ORPCErrorCode, unknown>;
|
|
33
|
-
export declare class ORPCError<TCode extends ORPCErrorCode, TData> extends Error {
|
|
34
|
-
zz$oe: {
|
|
35
|
-
code: TCode;
|
|
36
|
-
status?: number;
|
|
37
|
-
message?: string;
|
|
38
|
-
cause?: unknown;
|
|
39
|
-
issues?: readonly StandardSchemaV1.Issue[];
|
|
40
|
-
} & (undefined extends TData ? {
|
|
41
|
-
data?: TData;
|
|
42
|
-
} : {
|
|
43
|
-
data: TData;
|
|
44
|
-
});
|
|
45
|
-
constructor(zz$oe: {
|
|
46
|
-
code: TCode;
|
|
47
|
-
status?: number;
|
|
48
|
-
message?: string;
|
|
49
|
-
cause?: unknown;
|
|
50
|
-
issues?: readonly StandardSchemaV1.Issue[];
|
|
51
|
-
} & (undefined extends TData ? {
|
|
52
|
-
data?: TData;
|
|
53
|
-
} : {
|
|
54
|
-
data: TData;
|
|
55
|
-
}));
|
|
56
|
-
get code(): TCode;
|
|
57
|
-
get status(): number;
|
|
58
|
-
get data(): TData;
|
|
59
|
-
get issues(): readonly StandardSchemaV1.Issue[] | undefined;
|
|
60
|
-
toJSON(): ORPCErrorJSON<TCode, TData>;
|
|
61
|
-
static fromJSON(json: unknown): ORPCError<ORPCErrorCode, any> | undefined;
|
|
62
|
-
}
|
|
63
|
-
export type WELL_ORPC_ERROR = ORPCError<ORPCErrorCode, unknown>;
|
|
64
|
-
export declare function convertToStandardError(error: unknown): Error;
|
|
65
|
-
//# sourceMappingURL=error.d.ts.map
|
package/dist/src/function.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/index.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export * from './constants';
|
|
2
|
-
export * from './function';
|
|
3
|
-
export * from './hook';
|
|
4
|
-
export * from './json';
|
|
5
|
-
export * from './object';
|
|
6
|
-
export * from './proxy';
|
|
7
|
-
export * from './value';
|
|
8
|
-
export { isPlainObject } from 'is-what';
|
|
9
|
-
export { guard, mapEntries, mapValues, omit, trim } from 'radash';
|
|
10
|
-
export type * from 'type-fest';
|
|
11
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/json.d.ts
DELETED
package/dist/src/object.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export type Segment = string | number;
|
|
2
|
-
export declare function set(root: Readonly<Record<string, unknown> | unknown[]>, segments: Readonly<Segment[]>, value: unknown): unknown;
|
|
3
|
-
export declare function get(root: Readonly<Record<string, unknown> | unknown[]>, segments: Readonly<Segment[]>): unknown;
|
|
4
|
-
export declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): {
|
|
5
|
-
maps: Segment[][];
|
|
6
|
-
values: unknown[];
|
|
7
|
-
};
|
|
8
|
-
//# sourceMappingURL=object.d.ts.map
|
package/dist/src/proxy.d.ts
DELETED
package/dist/src/value.d.ts
DELETED