@orpc/shared 0.13.0 → 0.15.0

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.
@@ -0,0 +1,92 @@
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
+ function convertToStandardError(error) {
70
+ if (error instanceof Error) {
71
+ return error;
72
+ }
73
+ if (typeof error === "string") {
74
+ return new Error(error, { cause: error });
75
+ }
76
+ if (typeof error === "object" && error !== null) {
77
+ if ("message" in error && typeof error.message === "string") {
78
+ return new Error(error.message, { cause: error });
79
+ }
80
+ if ("name" in error && typeof error.name === "string") {
81
+ return new Error(error.name, { cause: error });
82
+ }
83
+ }
84
+ return new Error("Unknown error", { cause: error });
85
+ }
86
+
87
+ export {
88
+ ORPC_ERROR_CODE_STATUSES,
89
+ ORPCError,
90
+ convertToStandardError
91
+ };
92
+ //# sourceMappingURL=chunk-4KZEIATV.js.map
package/dist/error.js CHANGED
@@ -1,73 +1,11 @@
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
- };
1
+ import {
2
+ ORPCError,
3
+ ORPC_ERROR_CODE_STATUSES,
4
+ convertToStandardError
5
+ } from "./chunk-4KZEIATV.js";
69
6
  export {
70
7
  ORPCError,
71
- ORPC_ERROR_CODE_STATUSES
8
+ ORPC_ERROR_CODE_STATUSES,
9
+ convertToStandardError
72
10
  };
73
11
  //# sourceMappingURL=error.js.map
package/dist/index.js CHANGED
@@ -1,3 +1,65 @@
1
+ import {
2
+ convertToStandardError
3
+ } from "./chunk-4KZEIATV.js";
4
+
5
+ // src/hook.ts
6
+ async function executeWithHooks(options) {
7
+ const executes = convertToArray(options.hooks?.execute);
8
+ const onStarts = convertToArray(options.hooks?.onStart);
9
+ const onSuccesses = convertToArray(options.hooks?.onSuccess);
10
+ const onErrors = convertToArray(options.hooks?.onError);
11
+ const onFinishes = convertToArray(options.hooks?.onFinish);
12
+ let currentExecuteIndex = 0;
13
+ const next = async () => {
14
+ const execute = executes[currentExecuteIndex];
15
+ if (execute) {
16
+ currentExecuteIndex++;
17
+ return await execute(options.input, options.context, {
18
+ ...options.meta,
19
+ next
20
+ });
21
+ }
22
+ let state = { status: "pending", input: options.input, output: void 0, error: void 0 };
23
+ try {
24
+ for (const onStart of onStarts) {
25
+ await onStart(state, options.context, options.meta);
26
+ }
27
+ const output = await options.execute();
28
+ state = { status: "success", input: options.input, output, error: void 0 };
29
+ for (let i = onSuccesses.length - 1; i >= 0; i--) {
30
+ await onSuccesses[i](state, options.context, options.meta);
31
+ }
32
+ } catch (e) {
33
+ state = { status: "error", input: options.input, error: convertToStandardError(e), output: void 0 };
34
+ for (let i = onErrors.length - 1; i >= 0; i--) {
35
+ try {
36
+ await onErrors[i](state, options.context, options.meta);
37
+ } catch (e2) {
38
+ state = { status: "error", input: options.input, error: convertToStandardError(e2), output: void 0 };
39
+ }
40
+ }
41
+ }
42
+ for (let i = onFinishes.length - 1; i >= 0; i--) {
43
+ try {
44
+ await onFinishes[i](state, options.context, options.meta);
45
+ } catch (e) {
46
+ state = { status: "error", input: options.input, error: convertToStandardError(e), output: void 0 };
47
+ }
48
+ }
49
+ if (state.status === "error") {
50
+ throw state.error;
51
+ }
52
+ return state.output;
53
+ };
54
+ return await next();
55
+ }
56
+ function convertToArray(value2) {
57
+ if (value2 === void 0) {
58
+ return [];
59
+ }
60
+ return Array.isArray(value2) ? value2 : [value2];
61
+ }
62
+
1
63
  // src/json.ts
2
64
  function parseJSONSafely(text) {
3
65
  if (text === "")
@@ -66,6 +128,8 @@ function value(value2) {
66
128
  import { isPlainObject as isPlainObject2 } from "is-what";
67
129
  import { guard, mapEntries, mapValues, omit, trim } from "radash";
68
130
  export {
131
+ convertToArray,
132
+ executeWithHooks,
69
133
  findDeepMatches,
70
134
  get,
71
135
  guard,
@@ -21,6 +21,15 @@ export declare const ORPC_ERROR_CODE_STATUSES: {
21
21
  readonly GATEWAY_TIMEOUT: 504;
22
22
  };
23
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?: ZodIssue[];
30
+ }
31
+ export type ANY_ORPC_ERROR_JSON = ORPCErrorJSON<any, any>;
32
+ export type WELL_ORPC_ERROR_JSON = ORPCErrorJSON<ORPCErrorCode, unknown>;
24
33
  export declare class ORPCError<TCode extends ORPCErrorCode, TData> extends Error {
25
34
  zz$oe: {
26
35
  code: TCode;
@@ -46,13 +55,9 @@ export declare class ORPCError<TCode extends ORPCErrorCode, TData> extends Error
46
55
  get status(): number;
47
56
  get data(): TData;
48
57
  get issues(): ZodIssue[] | undefined;
49
- toJSON(): {
50
- code: TCode;
51
- status: number;
52
- message: string;
53
- data: TData;
54
- issues?: ZodIssue[];
55
- };
58
+ toJSON(): ORPCErrorJSON<TCode, TData>;
56
59
  static fromJSON(json: unknown): ORPCError<ORPCErrorCode, any> | undefined;
57
60
  }
61
+ export type WELL_ORPC_ERROR = ORPCError<ORPCErrorCode, unknown>;
62
+ export declare function convertToStandardError(error: unknown): Error;
58
63
  //# 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
+ execute?: 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, unknown> & {
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,3 +1,5 @@
1
+ export * from './function';
2
+ export * from './hook';
1
3
  export * from './json';
2
4
  export * from './object';
3
5
  export * from './value';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/shared",
3
3
  "type": "module",
4
- "version": "0.13.0",
4
+ "version": "0.15.0",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -37,7 +37,7 @@
37
37
  "is-what": "^5.0.2",
38
38
  "radash": "^12.1.0",
39
39
  "type-fest": "^4.26.1",
40
- "zod": "^3.23.8"
40
+ "zod": "^3.24.1"
41
41
  },
42
42
  "scripts": {
43
43
  "build": "tsup --clean --sourcemap --entry.index=src/index.ts --entry.error=src/error.ts --format=esm --onSuccess='tsc -b --noCheck'",