@orpc/shared 0.0.0-next.ed15210 → 0.0.0-next.ed87680

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 DELETED
@@ -1,198 +0,0 @@
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
- // src/object.ts
95
- import { isPlainObject as isPlainObject2 } from "is-what";
96
- function set(root, segments, value2) {
97
- const ref = { root };
98
- let currentRef = ref;
99
- let preSegment = "root";
100
- for (const segment of segments) {
101
- currentRef = currentRef[preSegment];
102
- preSegment = segment;
103
- }
104
- currentRef[preSegment] = value2;
105
- return ref.root;
106
- }
107
- function get(root, segments) {
108
- const ref = { root };
109
- let currentRef = ref;
110
- let preSegment = "root";
111
- for (const segment of segments) {
112
- if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) {
113
- return void 0;
114
- }
115
- currentRef = currentRef[preSegment];
116
- preSegment = segment;
117
- }
118
- if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) {
119
- return void 0;
120
- }
121
- return currentRef[preSegment];
122
- }
123
- function findDeepMatches(check, payload, segments = [], maps = [], values = []) {
124
- if (check(payload)) {
125
- maps.push(segments);
126
- values.push(payload);
127
- } else if (Array.isArray(payload)) {
128
- payload.forEach((v, i) => {
129
- findDeepMatches(check, v, [...segments, i], maps, values);
130
- });
131
- } else if (isPlainObject2(payload)) {
132
- for (const key in payload) {
133
- findDeepMatches(check, payload[key], [...segments, key], maps, values);
134
- }
135
- }
136
- return { maps, values };
137
- }
138
-
139
- // src/proxy.ts
140
- function createCallableObject(obj, handler) {
141
- const proxy = new Proxy(handler, {
142
- has(target, key) {
143
- return Reflect.has(obj, key) || Reflect.has(target, key);
144
- },
145
- ownKeys(target) {
146
- return Array.from(new Set(Reflect.ownKeys(obj).concat(...Reflect.ownKeys(target))));
147
- },
148
- get(target, key) {
149
- if (!Reflect.has(target, key) || Reflect.has(obj, key)) {
150
- return Reflect.get(obj, key);
151
- }
152
- return Reflect.get(target, key);
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);
162
- }
163
- });
164
- return proxy;
165
- }
166
-
167
- // src/value.ts
168
- function value(value2, ...args) {
169
- if (typeof value2 === "function") {
170
- return value2(...args);
171
- }
172
- return value2;
173
- }
174
-
175
- // src/index.ts
176
- import { isPlainObject as isPlainObject3 } from "is-what";
177
- import { group, guard, mapEntries, mapValues, omit, trim } from "radash";
178
- export {
179
- ORPC_HANDLER_HEADER,
180
- ORPC_HANDLER_VALUE,
181
- convertToArray,
182
- createCallableObject,
183
- executeWithHooks,
184
- findDeepMatches,
185
- get,
186
- group,
187
- guard,
188
- isPlainObject3 as isPlainObject,
189
- mapEntries,
190
- mapValues,
191
- omit,
192
- parseJSONSafely,
193
- set,
194
- toError,
195
- trim,
196
- value
197
- };
198
- //# sourceMappingURL=index.js.map
@@ -1,3 +0,0 @@
1
- export declare const ORPC_HANDLER_HEADER = "x-orpc-handler";
2
- export declare const ORPC_HANDLER_VALUE = "orpc";
3
- //# sourceMappingURL=constants.d.ts.map
@@ -1,2 +0,0 @@
1
- export declare function toError(error: unknown): Error;
2
- //# sourceMappingURL=error.d.ts.map
@@ -1,2 +0,0 @@
1
- export type AnyFunction = (...args: any[]) => any;
2
- //# sourceMappingURL=function.d.ts.map
@@ -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
@@ -1,12 +0,0 @@
1
- export * from './constants';
2
- export * from './error';
3
- export * from './function';
4
- export * from './hook';
5
- export * from './json';
6
- export * from './object';
7
- export * from './proxy';
8
- export * from './value';
9
- export { isPlainObject } from 'is-what';
10
- export { group, guard, mapEntries, mapValues, omit, trim } from 'radash';
11
- export type * from 'type-fest';
12
- //# sourceMappingURL=index.d.ts.map
@@ -1,2 +0,0 @@
1
- export declare function parseJSONSafely(text: string): unknown;
2
- //# sourceMappingURL=json.d.ts.map
@@ -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
@@ -1,3 +0,0 @@
1
- import type { AnyFunction } from './function';
2
- export declare function createCallableObject<TObject extends object, THandler extends AnyFunction>(obj: TObject, handler: THandler): TObject & THandler;
3
- //# sourceMappingURL=proxy.d.ts.map
@@ -1,4 +0,0 @@
1
- import type { Promisable } from 'type-fest';
2
- export type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => Promisable<T>);
3
- export declare function value<T extends Value<any, TArgs>, TArgs extends any[] = []>(value: T, ...args: TArgs): Promise<T extends Value<infer U, any> ? U : never>;
4
- //# sourceMappingURL=value.d.ts.map