@orpc/shared 0.0.0-next.db1f26d → 0.0.0-next.ee0aeaf

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,88 @@
1
+ // src/error.ts
2
+ var ORPC_ERROR_CODE_STATUSES = {
3
+ BAD_REQUEST: 400,
4
+ UNAUTHORIZED: 401,
5
+ FORBIDDEN: 403,
6
+ NOT_FOUND: 404,
7
+ METHOD_NOT_SUPPORTED: 405,
8
+ NOT_ACCEPTABLE: 406,
9
+ TIMEOUT: 408,
10
+ CONFLICT: 409,
11
+ PRECONDITION_FAILED: 412,
12
+ PAYLOAD_TOO_LARGE: 413,
13
+ UNSUPPORTED_MEDIA_TYPE: 415,
14
+ UNPROCESSABLE_CONTENT: 422,
15
+ TOO_MANY_REQUESTS: 429,
16
+ CLIENT_CLOSED_REQUEST: 499,
17
+ INTERNAL_SERVER_ERROR: 500,
18
+ NOT_IMPLEMENTED: 501,
19
+ BAD_GATEWAY: 502,
20
+ SERVICE_UNAVAILABLE: 503,
21
+ GATEWAY_TIMEOUT: 504
22
+ };
23
+ var ORPCError = class _ORPCError extends Error {
24
+ constructor(zz$oe) {
25
+ if (zz$oe.status && (zz$oe.status < 400 || zz$oe.status >= 600)) {
26
+ throw new Error("The ORPCError status code must be in the 400-599 range.");
27
+ }
28
+ super(zz$oe.message, { cause: zz$oe.cause });
29
+ this.zz$oe = zz$oe;
30
+ }
31
+ get code() {
32
+ return this.zz$oe.code;
33
+ }
34
+ get status() {
35
+ return this.zz$oe.status ?? ORPC_ERROR_CODE_STATUSES[this.code];
36
+ }
37
+ get data() {
38
+ return this.zz$oe.data;
39
+ }
40
+ get issues() {
41
+ return this.zz$oe.issues;
42
+ }
43
+ toJSON() {
44
+ return {
45
+ code: this.code,
46
+ status: this.status,
47
+ message: this.message,
48
+ data: this.data,
49
+ issues: this.issues
50
+ };
51
+ }
52
+ static fromJSON(json) {
53
+ 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)) {
54
+ return void 0;
55
+ }
56
+ return new _ORPCError({
57
+ code: json.code,
58
+ status: json.status,
59
+ message: json.message,
60
+ data: json.data,
61
+ issues: json.issues
62
+ });
63
+ }
64
+ };
65
+ function convertToStandardError(error) {
66
+ if (error instanceof Error) {
67
+ return error;
68
+ }
69
+ if (typeof error === "string") {
70
+ return new Error(error, { cause: error });
71
+ }
72
+ if (typeof error === "object" && error !== null) {
73
+ if ("message" in error && typeof error.message === "string") {
74
+ return new Error(error.message, { cause: error });
75
+ }
76
+ if ("name" in error && typeof error.name === "string") {
77
+ return new Error(error.name, { cause: error });
78
+ }
79
+ }
80
+ return new Error("Unknown error", { cause: error });
81
+ }
82
+
83
+ export {
84
+ ORPC_ERROR_CODE_STATUSES,
85
+ ORPCError,
86
+ convertToStandardError
87
+ };
88
+ //# sourceMappingURL=chunk-CCTAECMC.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-CCTAECMC.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,69 @@
1
+ import {
2
+ convertToStandardError
3
+ } from "./chunk-CCTAECMC.js";
4
+
5
+ // src/constants.ts
6
+ var ORPC_PROTOCOL_HEADER = "x-orpc-protocol";
7
+ var ORPC_PROTOCOL_VALUE = "orpc";
8
+
9
+ // src/hook.ts
10
+ async function executeWithHooks(options) {
11
+ const executes = convertToArray(options.hooks?.execute);
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 = executes[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
+
1
67
  // src/json.ts
2
68
  function parseJSONSafely(text) {
3
69
  if (text === "")
@@ -66,6 +132,10 @@ function value(value2) {
66
132
  import { isPlainObject as isPlainObject2 } from "is-what";
67
133
  import { guard, mapEntries, mapValues, omit, trim } from "radash";
68
134
  export {
135
+ ORPC_PROTOCOL_HEADER,
136
+ ORPC_PROTOCOL_VALUE,
137
+ convertToArray,
138
+ executeWithHooks,
69
139
  findDeepMatches,
70
140
  get,
71
141
  guard,
@@ -0,0 +1,3 @@
1
+ export declare const ORPC_PROTOCOL_HEADER = "x-orpc-protocol";
2
+ export declare const ORPC_PROTOCOL_VALUE = "orpc";
3
+ //# sourceMappingURL=constants.d.ts.map
@@ -1,4 +1,4 @@
1
- import { type ZodIssue } from 'zod';
1
+ import type { StandardSchemaV1 } from '@standard-schema/spec';
2
2
  export declare const ORPC_ERROR_CODE_STATUSES: {
3
3
  readonly BAD_REQUEST: 400;
4
4
  readonly UNAUTHORIZED: 401;
@@ -21,12 +21,22 @@ 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?: readonly StandardSchemaV1.Issue[];
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;
27
36
  status?: number;
28
37
  message?: string;
29
38
  cause?: unknown;
39
+ issues?: readonly StandardSchemaV1.Issue[];
30
40
  } & (undefined extends TData ? {
31
41
  data?: TData;
32
42
  } : {
@@ -37,6 +47,7 @@ export declare class ORPCError<TCode extends ORPCErrorCode, TData> extends Error
37
47
  status?: number;
38
48
  message?: string;
39
49
  cause?: unknown;
50
+ issues?: readonly StandardSchemaV1.Issue[];
40
51
  } & (undefined extends TData ? {
41
52
  data?: TData;
42
53
  } : {
@@ -45,14 +56,10 @@ export declare class ORPCError<TCode extends ORPCErrorCode, TData> extends Error
45
56
  get code(): TCode;
46
57
  get status(): number;
47
58
  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
- };
59
+ get issues(): readonly StandardSchemaV1.Issue[] | undefined;
60
+ toJSON(): ORPCErrorJSON<TCode, TData>;
56
61
  static fromJSON(json: unknown): ORPCError<ORPCErrorCode, any> | undefined;
57
62
  }
63
+ export type WELL_ORPC_ERROR = ORPCError<ORPCErrorCode, unknown>;
64
+ export declare function convertToStandardError(error: unknown): Error;
58
65
  //# 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,6 @@
1
+ export * from './constants';
2
+ export * from './function';
3
+ export * from './hook';
1
4
  export * from './json';
2
5
  export * from './object';
3
6
  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.0.0-next.db1f26d",
4
+ "version": "0.0.0-next.ee0aeaf",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -34,10 +34,10 @@
34
34
  "dist"
35
35
  ],
36
36
  "dependencies": {
37
+ "@standard-schema/spec": "1.0.0-beta.4",
37
38
  "is-what": "^5.0.2",
38
39
  "radash": "^12.1.0",
39
- "type-fest": "^4.26.1",
40
- "zod": "^3.23.8"
40
+ "type-fest": "^4.26.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'",