@orpc/shared 0.0.0-next.ef3ba82 → 0.0.0-next.f22c7ec

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 CHANGED
@@ -1,3 +1,85 @@
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
+
1
83
  // src/json.ts
2
84
  function parseJSONSafely(text) {
3
85
  if (text === "")
@@ -10,7 +92,7 @@ function parseJSONSafely(text) {
10
92
  }
11
93
 
12
94
  // src/object.ts
13
- import { isPlainObject } from "is-what";
95
+ import { isPlainObject as isPlainObject2 } from "is-what";
14
96
  function set(root, segments, value2) {
15
97
  const ref = { root };
16
98
  let currentRef = ref;
@@ -46,7 +128,7 @@ function findDeepMatches(check, payload, segments = [], maps = [], values = [])
46
128
  payload.forEach((v, i) => {
47
129
  findDeepMatches(check, v, [...segments, i], maps, values);
48
130
  });
49
- } else if (isPlainObject(payload)) {
131
+ } else if (isPlainObject2(payload)) {
50
132
  for (const key in payload) {
51
133
  findDeepMatches(check, payload[key], [...segments, key], maps, values);
52
134
  }
@@ -54,27 +136,63 @@ function findDeepMatches(check, payload, segments = [], maps = [], values = [])
54
136
  return { maps, values };
55
137
  }
56
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
+
57
167
  // src/value.ts
58
- function value(value2) {
168
+ function value(value2, ...args) {
59
169
  if (typeof value2 === "function") {
60
- return value2();
170
+ return value2(...args);
61
171
  }
62
172
  return value2;
63
173
  }
64
174
 
65
175
  // src/index.ts
66
- import { isPlainObject as isPlainObject2 } from "is-what";
67
- import { guard, mapEntries, mapValues, omit, trim } from "radash";
176
+ import { isPlainObject as isPlainObject3 } from "is-what";
177
+ import { group, guard, mapEntries, mapValues, omit, trim } from "radash";
68
178
  export {
179
+ ORPC_HANDLER_HEADER,
180
+ ORPC_HANDLER_VALUE,
181
+ convertToArray,
182
+ createCallableObject,
183
+ executeWithHooks,
69
184
  findDeepMatches,
70
185
  get,
186
+ group,
71
187
  guard,
72
- isPlainObject2 as isPlainObject,
188
+ isPlainObject3 as isPlainObject,
73
189
  mapEntries,
74
190
  mapValues,
75
191
  omit,
76
192
  parseJSONSafely,
77
193
  set,
194
+ toError,
78
195
  trim,
79
196
  value
80
197
  };
198
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
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,57 +1,2 @@
1
- import { type ZodIssue } from 'zod';
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 declare class ORPCError<TCode extends ORPCErrorCode, TData> extends Error {
25
- zz$oe: {
26
- code: TCode;
27
- status?: number;
28
- message?: string;
29
- cause?: unknown;
30
- } & (undefined extends TData ? {
31
- data?: TData;
32
- } : {
33
- data: TData;
34
- });
35
- constructor(zz$oe: {
36
- code: TCode;
37
- status?: number;
38
- message?: string;
39
- cause?: unknown;
40
- } & (undefined extends TData ? {
41
- data?: TData;
42
- } : {
43
- data: TData;
44
- }));
45
- get code(): TCode;
46
- get status(): number;
47
- get data(): TData;
48
- get issues(): ZodIssue[] | undefined;
49
- toJSON(): {
50
- code: TCode;
51
- status: number;
52
- message: string;
53
- data: TData;
54
- issues?: ZodIssue[];
55
- };
56
- static fromJSON(json: unknown): ORPCError<ORPCErrorCode, any> | undefined;
57
- }
1
+ export declare function toError(error: unknown): Error;
2
+ //# sourceMappingURL=error.d.ts.map
@@ -0,0 +1,2 @@
1
+ export type AnyFunction = (...args: any[]) => any;
2
+ //# sourceMappingURL=function.d.ts.map
@@ -0,0 +1,42 @@
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,6 +1,12 @@
1
+ export * from './constants';
2
+ export * from './error';
3
+ export * from './function';
4
+ export * from './hook';
1
5
  export * from './json';
2
6
  export * from './object';
7
+ export * from './proxy';
3
8
  export * from './value';
4
9
  export { isPlainObject } from 'is-what';
5
- export { guard, mapEntries, mapValues, omit, trim } from 'radash';
10
+ export { group, guard, mapEntries, mapValues, omit, trim } from 'radash';
6
11
  export type * from 'type-fest';
12
+ //# sourceMappingURL=index.d.ts.map
@@ -1 +1,2 @@
1
1
  export declare function parseJSONSafely(text: string): unknown;
2
+ //# sourceMappingURL=json.d.ts.map
@@ -5,3 +5,4 @@ export declare function findDeepMatches(check: (value: unknown) => boolean, payl
5
5
  maps: Segment[][];
6
6
  values: unknown[];
7
7
  };
8
+ //# sourceMappingURL=object.d.ts.map
@@ -0,0 +1,3 @@
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,3 +1,4 @@
1
1
  import type { Promisable } from 'type-fest';
2
- export type Value<T> = T | (() => Promisable<T>);
3
- export declare function value<T extends Value<any>>(value: T): Promise<T extends Value<infer U> ? U : never>;
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
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.ef3ba82",
4
+ "version": "0.0.0-next.f22c7ec",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -19,27 +19,23 @@
19
19
  "import": "./dist/index.js",
20
20
  "default": "./dist/index.js"
21
21
  },
22
- "./error": {
23
- "types": "./dist/src/error.d.ts",
24
- "import": "./dist/error.js",
25
- "default": "./dist/error.js"
26
- },
27
22
  "./🔒/*": {
28
23
  "types": "./dist/src/*.d.ts"
29
24
  }
30
25
  },
31
26
  "files": [
32
- "!dist/*.tsbuildinfo",
27
+ "!**/*.map",
28
+ "!**/*.tsbuildinfo",
33
29
  "dist"
34
30
  ],
35
31
  "dependencies": {
32
+ "@standard-schema/spec": "1.0.0-beta.4",
36
33
  "is-what": "^5.0.2",
37
34
  "radash": "^12.1.0",
38
- "type-fest": "^4.26.1",
39
- "zod": "^3.23.8"
35
+ "type-fest": "^4.26.1"
40
36
  },
41
37
  "scripts": {
42
- "build": "tsup --clean --entry.index=src/index.ts --entry.error=src/error.ts --format=esm --onSuccess='tsc -b --noCheck'",
38
+ "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
43
39
  "build:watch": "pnpm run build --watch",
44
40
  "type:check": "tsc -b"
45
41
  }
package/dist/error.js DELETED
@@ -1,72 +0,0 @@
1
- // src/error.ts
2
- import { ZodError } from "zod";
3
- var ORPC_ERROR_CODE_STATUSES = {
4
- BAD_REQUEST: 400,
5
- UNAUTHORIZED: 401,
6
- FORBIDDEN: 403,
7
- NOT_FOUND: 404,
8
- METHOD_NOT_SUPPORTED: 405,
9
- NOT_ACCEPTABLE: 406,
10
- TIMEOUT: 408,
11
- CONFLICT: 409,
12
- PRECONDITION_FAILED: 412,
13
- PAYLOAD_TOO_LARGE: 413,
14
- UNSUPPORTED_MEDIA_TYPE: 415,
15
- UNPROCESSABLE_CONTENT: 422,
16
- TOO_MANY_REQUESTS: 429,
17
- CLIENT_CLOSED_REQUEST: 499,
18
- INTERNAL_SERVER_ERROR: 500,
19
- NOT_IMPLEMENTED: 501,
20
- BAD_GATEWAY: 502,
21
- SERVICE_UNAVAILABLE: 503,
22
- GATEWAY_TIMEOUT: 504
23
- };
24
- var ORPCError = class _ORPCError extends Error {
25
- constructor(zz$oe) {
26
- if (zz$oe.status && (zz$oe.status < 400 || zz$oe.status >= 600)) {
27
- throw new Error("The ORPCError status code must be in the 400-599 range.");
28
- }
29
- super(zz$oe.message, { cause: zz$oe.cause });
30
- this.zz$oe = zz$oe;
31
- }
32
- get code() {
33
- return this.zz$oe.code;
34
- }
35
- get status() {
36
- return this.zz$oe.status ?? ORPC_ERROR_CODE_STATUSES[this.code];
37
- }
38
- get data() {
39
- return this.zz$oe.data;
40
- }
41
- get issues() {
42
- if (this.code === "BAD_REQUEST" && this.zz$oe.cause instanceof ZodError) {
43
- return this.zz$oe.cause.issues;
44
- }
45
- return void 0;
46
- }
47
- toJSON() {
48
- return {
49
- code: this.code,
50
- status: this.status,
51
- message: this.message,
52
- data: this.data,
53
- issues: this.issues
54
- };
55
- }
56
- static fromJSON(json) {
57
- if (typeof json !== "object" || json === null || !("code" in json) || !Object.keys(ORPC_ERROR_CODE_STATUSES).find((key) => json.code === key) || !("status" in json) || typeof json.status !== "number" || "message" in json && json.message !== void 0 && typeof json.message !== "string" || "issues" in json && json.issues !== void 0 && !Array.isArray(json.issues)) {
58
- return void 0;
59
- }
60
- return new _ORPCError({
61
- code: json.code,
62
- status: json.status,
63
- message: Reflect.get(json, "message"),
64
- data: Reflect.get(json, "data"),
65
- cause: "issues" in json ? new ZodError(json.issues) : void 0
66
- });
67
- }
68
- };
69
- export {
70
- ORPCError,
71
- ORPC_ERROR_CODE_STATUSES
72
- };