@orpc/client 0.0.0-next.c40d0c9 → 0.0.0-next.c462bb4
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 +22 -22
- package/dist/adapters/fetch/index.d.mts +31 -11
- package/dist/adapters/fetch/index.d.ts +31 -11
- package/dist/adapters/fetch/index.mjs +27 -13
- package/dist/adapters/message-port/index.d.mts +80 -0
- package/dist/adapters/message-port/index.d.ts +80 -0
- package/dist/adapters/message-port/index.mjs +87 -0
- package/dist/adapters/standard/index.d.mts +6 -5
- package/dist/adapters/standard/index.d.ts +6 -5
- package/dist/adapters/standard/index.mjs +4 -2
- package/dist/adapters/websocket/index.d.mts +29 -0
- package/dist/adapters/websocket/index.d.ts +29 -0
- package/dist/adapters/websocket/index.mjs +47 -0
- package/dist/index.d.mts +104 -29
- package/dist/index.d.ts +104 -29
- package/dist/index.mjs +58 -11
- package/dist/plugins/index.d.mts +209 -25
- package/dist/plugins/index.d.ts +209 -25
- package/dist/plugins/index.mjs +410 -56
- package/dist/shared/{client.DSPm2IGZ.mjs → client.A0J6mgVq.mjs} +90 -29
- package/dist/shared/client.BH1AYT_p.d.mts +83 -0
- package/dist/shared/client.BH1AYT_p.d.ts +83 -0
- package/dist/shared/client.BLtwTQUg.mjs +40 -0
- package/dist/shared/{client.D-jrSuDb.d.ts → client.BxV-mzeR.d.ts} +13 -25
- package/dist/shared/{client.grRbC25r.d.ts → client.CPgZaUox.d.mts} +21 -15
- package/dist/shared/{client.Bt94sjrK.d.mts → client.D8lMmWVC.d.mts} +13 -25
- package/dist/shared/client.DL-GilqA.mjs +171 -0
- package/dist/shared/{client.5813Ufvs.d.mts → client.De8SW4Kw.d.ts} +21 -15
- package/package.json +16 -5
- package/dist/shared/client.C0lT7w02.d.mts +0 -30
- package/dist/shared/client.C0lT7w02.d.ts +0 -30
- package/dist/shared/client.jKEwIsRd.mjs +0 -175
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { resolveMaybeOptionalOptions, getConstructor, isObject } from '@orpc/shared';
|
|
2
|
+
|
|
3
|
+
const ORPC_CLIENT_PACKAGE_NAME = "@orpc/client";
|
|
4
|
+
const ORPC_CLIENT_PACKAGE_VERSION = "0.0.0-next.c462bb4";
|
|
5
|
+
|
|
6
|
+
const COMMON_ORPC_ERROR_DEFS = {
|
|
7
|
+
BAD_REQUEST: {
|
|
8
|
+
status: 400,
|
|
9
|
+
message: "Bad Request"
|
|
10
|
+
},
|
|
11
|
+
UNAUTHORIZED: {
|
|
12
|
+
status: 401,
|
|
13
|
+
message: "Unauthorized"
|
|
14
|
+
},
|
|
15
|
+
FORBIDDEN: {
|
|
16
|
+
status: 403,
|
|
17
|
+
message: "Forbidden"
|
|
18
|
+
},
|
|
19
|
+
NOT_FOUND: {
|
|
20
|
+
status: 404,
|
|
21
|
+
message: "Not Found"
|
|
22
|
+
},
|
|
23
|
+
METHOD_NOT_SUPPORTED: {
|
|
24
|
+
status: 405,
|
|
25
|
+
message: "Method Not Supported"
|
|
26
|
+
},
|
|
27
|
+
NOT_ACCEPTABLE: {
|
|
28
|
+
status: 406,
|
|
29
|
+
message: "Not Acceptable"
|
|
30
|
+
},
|
|
31
|
+
TIMEOUT: {
|
|
32
|
+
status: 408,
|
|
33
|
+
message: "Request Timeout"
|
|
34
|
+
},
|
|
35
|
+
CONFLICT: {
|
|
36
|
+
status: 409,
|
|
37
|
+
message: "Conflict"
|
|
38
|
+
},
|
|
39
|
+
PRECONDITION_FAILED: {
|
|
40
|
+
status: 412,
|
|
41
|
+
message: "Precondition Failed"
|
|
42
|
+
},
|
|
43
|
+
PAYLOAD_TOO_LARGE: {
|
|
44
|
+
status: 413,
|
|
45
|
+
message: "Payload Too Large"
|
|
46
|
+
},
|
|
47
|
+
UNSUPPORTED_MEDIA_TYPE: {
|
|
48
|
+
status: 415,
|
|
49
|
+
message: "Unsupported Media Type"
|
|
50
|
+
},
|
|
51
|
+
UNPROCESSABLE_CONTENT: {
|
|
52
|
+
status: 422,
|
|
53
|
+
message: "Unprocessable Content"
|
|
54
|
+
},
|
|
55
|
+
TOO_MANY_REQUESTS: {
|
|
56
|
+
status: 429,
|
|
57
|
+
message: "Too Many Requests"
|
|
58
|
+
},
|
|
59
|
+
CLIENT_CLOSED_REQUEST: {
|
|
60
|
+
status: 499,
|
|
61
|
+
message: "Client Closed Request"
|
|
62
|
+
},
|
|
63
|
+
INTERNAL_SERVER_ERROR: {
|
|
64
|
+
status: 500,
|
|
65
|
+
message: "Internal Server Error"
|
|
66
|
+
},
|
|
67
|
+
NOT_IMPLEMENTED: {
|
|
68
|
+
status: 501,
|
|
69
|
+
message: "Not Implemented"
|
|
70
|
+
},
|
|
71
|
+
BAD_GATEWAY: {
|
|
72
|
+
status: 502,
|
|
73
|
+
message: "Bad Gateway"
|
|
74
|
+
},
|
|
75
|
+
SERVICE_UNAVAILABLE: {
|
|
76
|
+
status: 503,
|
|
77
|
+
message: "Service Unavailable"
|
|
78
|
+
},
|
|
79
|
+
GATEWAY_TIMEOUT: {
|
|
80
|
+
status: 504,
|
|
81
|
+
message: "Gateway Timeout"
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
function fallbackORPCErrorStatus(code, status) {
|
|
85
|
+
return status ?? COMMON_ORPC_ERROR_DEFS[code]?.status ?? 500;
|
|
86
|
+
}
|
|
87
|
+
function fallbackORPCErrorMessage(code, message) {
|
|
88
|
+
return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
|
|
89
|
+
}
|
|
90
|
+
const GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL = Symbol.for(`__${ORPC_CLIENT_PACKAGE_NAME}@${ORPC_CLIENT_PACKAGE_VERSION}/error/ORPC_ERROR_CONSTRUCTORS__`);
|
|
91
|
+
void (globalThis[GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL] ??= /* @__PURE__ */ new WeakSet());
|
|
92
|
+
const globalORPCErrorConstructors = globalThis[GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL];
|
|
93
|
+
class ORPCError extends Error {
|
|
94
|
+
defined;
|
|
95
|
+
code;
|
|
96
|
+
status;
|
|
97
|
+
data;
|
|
98
|
+
constructor(code, ...rest) {
|
|
99
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
100
|
+
if (options.status !== void 0 && !isORPCErrorStatus(options.status)) {
|
|
101
|
+
throw new Error("[ORPCError] Invalid error status code.");
|
|
102
|
+
}
|
|
103
|
+
const message = fallbackORPCErrorMessage(code, options.message);
|
|
104
|
+
super(message, options);
|
|
105
|
+
this.code = code;
|
|
106
|
+
this.status = fallbackORPCErrorStatus(code, options.status);
|
|
107
|
+
this.defined = options.defined ?? false;
|
|
108
|
+
this.data = options.data;
|
|
109
|
+
}
|
|
110
|
+
toJSON() {
|
|
111
|
+
return {
|
|
112
|
+
defined: this.defined,
|
|
113
|
+
code: this.code,
|
|
114
|
+
status: this.status,
|
|
115
|
+
message: this.message,
|
|
116
|
+
data: this.data
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Workaround for Next.js where different contexts use separate
|
|
121
|
+
* dependency graphs, causing multiple ORPCError constructors existing and breaking
|
|
122
|
+
* `instanceof` checks across contexts.
|
|
123
|
+
*
|
|
124
|
+
* This is particularly problematic with "Optimized SSR", where orpc-client
|
|
125
|
+
* executes in one context but is invoked from another. When an error is thrown
|
|
126
|
+
* in the execution context, `instanceof ORPCError` checks fail in the
|
|
127
|
+
* invocation context due to separate class constructors.
|
|
128
|
+
*
|
|
129
|
+
* @todo Remove this and related code if Next.js resolves the multiple dependency graph issue.
|
|
130
|
+
*/
|
|
131
|
+
static [Symbol.hasInstance](instance) {
|
|
132
|
+
if (globalORPCErrorConstructors.has(this)) {
|
|
133
|
+
const constructor = getConstructor(instance);
|
|
134
|
+
if (constructor && globalORPCErrorConstructors.has(constructor)) {
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return super[Symbol.hasInstance](instance);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
globalORPCErrorConstructors.add(ORPCError);
|
|
142
|
+
function isDefinedError(error) {
|
|
143
|
+
return error instanceof ORPCError && error.defined;
|
|
144
|
+
}
|
|
145
|
+
function toORPCError(error) {
|
|
146
|
+
return error instanceof ORPCError ? error : new ORPCError("INTERNAL_SERVER_ERROR", {
|
|
147
|
+
message: "Internal server error",
|
|
148
|
+
cause: error
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
function isORPCErrorStatus(status) {
|
|
152
|
+
return status < 200 || status >= 400;
|
|
153
|
+
}
|
|
154
|
+
function isORPCErrorJson(json) {
|
|
155
|
+
if (!isObject(json)) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
const validKeys = ["defined", "code", "status", "message", "data"];
|
|
159
|
+
if (Object.keys(json).some((k) => !validKeys.includes(k))) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
return "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "status" in json && typeof json.status === "number" && isORPCErrorStatus(json.status) && "message" in json && typeof json.message === "string";
|
|
163
|
+
}
|
|
164
|
+
function createORPCErrorFromJson(json, options = {}) {
|
|
165
|
+
return new ORPCError(json.code, {
|
|
166
|
+
...options,
|
|
167
|
+
...json
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export { COMMON_ORPC_ERROR_DEFS as C, ORPC_CLIENT_PACKAGE_NAME as O, ORPC_CLIENT_PACKAGE_VERSION as a, fallbackORPCErrorMessage as b, ORPCError as c, isORPCErrorStatus as d, isORPCErrorJson as e, fallbackORPCErrorStatus as f, createORPCErrorFromJson as g, isDefinedError as i, toORPCError as t };
|
|
@@ -1,6 +1,16 @@
|
|
|
1
|
-
import { Interceptor
|
|
1
|
+
import { Interceptor } from '@orpc/shared';
|
|
2
2
|
import { StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
|
|
3
|
-
import {
|
|
3
|
+
import { b as ClientContext, c as ClientOptions, C as ClientLink } from './client.BH1AYT_p.js';
|
|
4
|
+
|
|
5
|
+
interface StandardLinkPlugin<T extends ClientContext> {
|
|
6
|
+
order?: number;
|
|
7
|
+
init?(options: StandardLinkOptions<T>): void;
|
|
8
|
+
}
|
|
9
|
+
declare class CompositeStandardLinkPlugin<T extends ClientContext, TPlugin extends StandardLinkPlugin<T>> implements StandardLinkPlugin<T> {
|
|
10
|
+
protected readonly plugins: TPlugin[];
|
|
11
|
+
constructor(plugins?: readonly TPlugin[]);
|
|
12
|
+
init(options: StandardLinkOptions<T>): void;
|
|
13
|
+
}
|
|
4
14
|
|
|
5
15
|
interface StandardLinkCodec<T extends ClientContext> {
|
|
6
16
|
encode(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<StandardRequest>;
|
|
@@ -10,24 +20,19 @@ interface StandardLinkClient<T extends ClientContext> {
|
|
|
10
20
|
call(request: StandardRequest, options: ClientOptions<T>, path: readonly string[], input: unknown): Promise<StandardLazyResponse>;
|
|
11
21
|
}
|
|
12
22
|
|
|
13
|
-
|
|
23
|
+
interface StandardLinkInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
|
|
24
|
+
path: readonly string[];
|
|
25
|
+
input: unknown;
|
|
14
26
|
}
|
|
15
|
-
interface
|
|
16
|
-
|
|
27
|
+
interface StandardLinkClientInterceptorOptions<T extends ClientContext> extends StandardLinkInterceptorOptions<T> {
|
|
28
|
+
request: StandardRequest;
|
|
17
29
|
}
|
|
18
30
|
interface StandardLinkOptions<T extends ClientContext> {
|
|
19
|
-
interceptors?: Interceptor<
|
|
20
|
-
|
|
21
|
-
input: unknown;
|
|
22
|
-
options: ClientOptions<T>;
|
|
23
|
-
}, unknown, ThrowableError>[];
|
|
24
|
-
clientInterceptors?: Interceptor<{
|
|
25
|
-
request: StandardRequest;
|
|
26
|
-
}, StandardLazyResponse, ThrowableError>[];
|
|
31
|
+
interceptors?: Interceptor<StandardLinkInterceptorOptions<T>, Promise<unknown>>[];
|
|
32
|
+
clientInterceptors?: Interceptor<StandardLinkClientInterceptorOptions<T>, Promise<StandardLazyResponse>>[];
|
|
27
33
|
plugins?: StandardLinkPlugin<T>[];
|
|
28
34
|
}
|
|
29
35
|
declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
|
|
30
|
-
#private;
|
|
31
36
|
readonly codec: StandardLinkCodec<T>;
|
|
32
37
|
readonly sender: StandardLinkClient<T>;
|
|
33
38
|
private readonly interceptors;
|
|
@@ -36,4 +41,5 @@ declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
|
|
|
36
41
|
call(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<unknown>;
|
|
37
42
|
}
|
|
38
43
|
|
|
39
|
-
export {
|
|
44
|
+
export { CompositeStandardLinkPlugin as C, StandardLink as d };
|
|
45
|
+
export type { StandardLinkClientInterceptorOptions as S, StandardLinkPlugin as a, StandardLinkOptions as b, StandardLinkInterceptorOptions as c, StandardLinkCodec as e, StandardLinkClient as f };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/client",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.0-next.
|
|
4
|
+
"version": "0.0.0-next.c462bb4",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -33,18 +33,29 @@
|
|
|
33
33
|
"types": "./dist/adapters/fetch/index.d.mts",
|
|
34
34
|
"import": "./dist/adapters/fetch/index.mjs",
|
|
35
35
|
"default": "./dist/adapters/fetch/index.mjs"
|
|
36
|
+
},
|
|
37
|
+
"./websocket": {
|
|
38
|
+
"types": "./dist/adapters/websocket/index.d.mts",
|
|
39
|
+
"import": "./dist/adapters/websocket/index.mjs",
|
|
40
|
+
"default": "./dist/adapters/websocket/index.mjs"
|
|
41
|
+
},
|
|
42
|
+
"./message-port": {
|
|
43
|
+
"types": "./dist/adapters/message-port/index.d.mts",
|
|
44
|
+
"import": "./dist/adapters/message-port/index.mjs",
|
|
45
|
+
"default": "./dist/adapters/message-port/index.mjs"
|
|
36
46
|
}
|
|
37
47
|
},
|
|
38
48
|
"files": [
|
|
39
49
|
"dist"
|
|
40
50
|
],
|
|
41
51
|
"dependencies": {
|
|
42
|
-
"@orpc/
|
|
43
|
-
"@orpc/standard-server-fetch": "0.0.0-next.
|
|
44
|
-
"@orpc/
|
|
52
|
+
"@orpc/standard-server": "0.0.0-next.c462bb4",
|
|
53
|
+
"@orpc/standard-server-fetch": "0.0.0-next.c462bb4",
|
|
54
|
+
"@orpc/shared": "0.0.0-next.c462bb4",
|
|
55
|
+
"@orpc/standard-server-peer": "0.0.0-next.c462bb4"
|
|
45
56
|
},
|
|
46
57
|
"devDependencies": {
|
|
47
|
-
"zod": "^
|
|
58
|
+
"zod": "^4.1.12"
|
|
48
59
|
},
|
|
49
60
|
"scripts": {
|
|
50
61
|
"build": "unbuild",
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { PromiseWithError } from '@orpc/shared';
|
|
2
|
-
|
|
3
|
-
type HTTPPath = `/${string}`;
|
|
4
|
-
type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
5
|
-
type ClientContext = Record<PropertyKey, any>;
|
|
6
|
-
type FriendlyClientOptions<TClientContext extends ClientContext> = {
|
|
7
|
-
signal?: AbortSignal;
|
|
8
|
-
lastEventId?: string | undefined;
|
|
9
|
-
} & (Record<never, never> extends TClientContext ? {
|
|
10
|
-
context?: TClientContext;
|
|
11
|
-
} : {
|
|
12
|
-
context: TClientContext;
|
|
13
|
-
});
|
|
14
|
-
type ClientRest<TClientContext extends ClientContext, TInput> = Record<never, never> extends TClientContext ? undefined extends TInput ? [input?: TInput, options?: FriendlyClientOptions<TClientContext>] : [input: TInput, options?: FriendlyClientOptions<TClientContext>] : [input: TInput, options: FriendlyClientOptions<TClientContext>];
|
|
15
|
-
type ClientPromiseResult<TOutput, TError> = PromiseWithError<TOutput, TError>;
|
|
16
|
-
interface Client<TClientContext extends ClientContext, TInput, TOutput, TError> {
|
|
17
|
-
(...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
|
|
18
|
-
}
|
|
19
|
-
type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
|
|
20
|
-
[k: string]: NestedClient<TClientContext>;
|
|
21
|
-
};
|
|
22
|
-
type InferClientContext<T extends NestedClient<any>> = T extends NestedClient<infer U> ? U : never;
|
|
23
|
-
type ClientOptions<TClientContext extends ClientContext> = FriendlyClientOptions<TClientContext> & {
|
|
24
|
-
context: TClientContext;
|
|
25
|
-
};
|
|
26
|
-
interface ClientLink<TClientContext extends ClientContext> {
|
|
27
|
-
call: (path: readonly string[], input: unknown, options: ClientOptions<TClientContext>) => Promise<unknown>;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export type { ClientLink as C, FriendlyClientOptions as F, HTTPPath as H, InferClientContext as I, NestedClient as N, ClientContext as a, ClientOptions as b, ClientPromiseResult as c, HTTPMethod as d, ClientRest as e, Client as f };
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { PromiseWithError } from '@orpc/shared';
|
|
2
|
-
|
|
3
|
-
type HTTPPath = `/${string}`;
|
|
4
|
-
type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
5
|
-
type ClientContext = Record<PropertyKey, any>;
|
|
6
|
-
type FriendlyClientOptions<TClientContext extends ClientContext> = {
|
|
7
|
-
signal?: AbortSignal;
|
|
8
|
-
lastEventId?: string | undefined;
|
|
9
|
-
} & (Record<never, never> extends TClientContext ? {
|
|
10
|
-
context?: TClientContext;
|
|
11
|
-
} : {
|
|
12
|
-
context: TClientContext;
|
|
13
|
-
});
|
|
14
|
-
type ClientRest<TClientContext extends ClientContext, TInput> = Record<never, never> extends TClientContext ? undefined extends TInput ? [input?: TInput, options?: FriendlyClientOptions<TClientContext>] : [input: TInput, options?: FriendlyClientOptions<TClientContext>] : [input: TInput, options: FriendlyClientOptions<TClientContext>];
|
|
15
|
-
type ClientPromiseResult<TOutput, TError> = PromiseWithError<TOutput, TError>;
|
|
16
|
-
interface Client<TClientContext extends ClientContext, TInput, TOutput, TError> {
|
|
17
|
-
(...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
|
|
18
|
-
}
|
|
19
|
-
type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
|
|
20
|
-
[k: string]: NestedClient<TClientContext>;
|
|
21
|
-
};
|
|
22
|
-
type InferClientContext<T extends NestedClient<any>> = T extends NestedClient<infer U> ? U : never;
|
|
23
|
-
type ClientOptions<TClientContext extends ClientContext> = FriendlyClientOptions<TClientContext> & {
|
|
24
|
-
context: TClientContext;
|
|
25
|
-
};
|
|
26
|
-
interface ClientLink<TClientContext extends ClientContext> {
|
|
27
|
-
call: (path: readonly string[], input: unknown, options: ClientOptions<TClientContext>) => Promise<unknown>;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export type { ClientLink as C, FriendlyClientOptions as F, HTTPPath as H, InferClientContext as I, NestedClient as N, ClientContext as a, ClientOptions as b, ClientPromiseResult as c, HTTPMethod as d, ClientRest as e, Client as f };
|
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
import { isObject, isTypescriptObject } from '@orpc/shared';
|
|
2
|
-
import { getEventMeta, withEventMeta } from '@orpc/standard-server';
|
|
3
|
-
|
|
4
|
-
const COMMON_ORPC_ERROR_DEFS = {
|
|
5
|
-
BAD_REQUEST: {
|
|
6
|
-
status: 400,
|
|
7
|
-
message: "Bad Request"
|
|
8
|
-
},
|
|
9
|
-
UNAUTHORIZED: {
|
|
10
|
-
status: 401,
|
|
11
|
-
message: "Unauthorized"
|
|
12
|
-
},
|
|
13
|
-
FORBIDDEN: {
|
|
14
|
-
status: 403,
|
|
15
|
-
message: "Forbidden"
|
|
16
|
-
},
|
|
17
|
-
NOT_FOUND: {
|
|
18
|
-
status: 404,
|
|
19
|
-
message: "Not Found"
|
|
20
|
-
},
|
|
21
|
-
METHOD_NOT_SUPPORTED: {
|
|
22
|
-
status: 405,
|
|
23
|
-
message: "Method Not Supported"
|
|
24
|
-
},
|
|
25
|
-
NOT_ACCEPTABLE: {
|
|
26
|
-
status: 406,
|
|
27
|
-
message: "Not Acceptable"
|
|
28
|
-
},
|
|
29
|
-
TIMEOUT: {
|
|
30
|
-
status: 408,
|
|
31
|
-
message: "Request Timeout"
|
|
32
|
-
},
|
|
33
|
-
CONFLICT: {
|
|
34
|
-
status: 409,
|
|
35
|
-
message: "Conflict"
|
|
36
|
-
},
|
|
37
|
-
PRECONDITION_FAILED: {
|
|
38
|
-
status: 412,
|
|
39
|
-
message: "Precondition Failed"
|
|
40
|
-
},
|
|
41
|
-
PAYLOAD_TOO_LARGE: {
|
|
42
|
-
status: 413,
|
|
43
|
-
message: "Payload Too Large"
|
|
44
|
-
},
|
|
45
|
-
UNSUPPORTED_MEDIA_TYPE: {
|
|
46
|
-
status: 415,
|
|
47
|
-
message: "Unsupported Media Type"
|
|
48
|
-
},
|
|
49
|
-
UNPROCESSABLE_CONTENT: {
|
|
50
|
-
status: 422,
|
|
51
|
-
message: "Unprocessable Content"
|
|
52
|
-
},
|
|
53
|
-
TOO_MANY_REQUESTS: {
|
|
54
|
-
status: 429,
|
|
55
|
-
message: "Too Many Requests"
|
|
56
|
-
},
|
|
57
|
-
CLIENT_CLOSED_REQUEST: {
|
|
58
|
-
status: 499,
|
|
59
|
-
message: "Client Closed Request"
|
|
60
|
-
},
|
|
61
|
-
INTERNAL_SERVER_ERROR: {
|
|
62
|
-
status: 500,
|
|
63
|
-
message: "Internal Server Error"
|
|
64
|
-
},
|
|
65
|
-
NOT_IMPLEMENTED: {
|
|
66
|
-
status: 501,
|
|
67
|
-
message: "Not Implemented"
|
|
68
|
-
},
|
|
69
|
-
BAD_GATEWAY: {
|
|
70
|
-
status: 502,
|
|
71
|
-
message: "Bad Gateway"
|
|
72
|
-
},
|
|
73
|
-
SERVICE_UNAVAILABLE: {
|
|
74
|
-
status: 503,
|
|
75
|
-
message: "Service Unavailable"
|
|
76
|
-
},
|
|
77
|
-
GATEWAY_TIMEOUT: {
|
|
78
|
-
status: 504,
|
|
79
|
-
message: "Gateway Timeout"
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
function fallbackORPCErrorStatus(code, status) {
|
|
83
|
-
return status ?? COMMON_ORPC_ERROR_DEFS[code]?.status ?? 500;
|
|
84
|
-
}
|
|
85
|
-
function fallbackORPCErrorMessage(code, message) {
|
|
86
|
-
return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
|
|
87
|
-
}
|
|
88
|
-
class ORPCError extends Error {
|
|
89
|
-
defined;
|
|
90
|
-
code;
|
|
91
|
-
status;
|
|
92
|
-
data;
|
|
93
|
-
constructor(code, ...[options]) {
|
|
94
|
-
if (options?.status && !isORPCErrorStatus(options.status)) {
|
|
95
|
-
throw new Error("[ORPCError] Invalid error status code.");
|
|
96
|
-
}
|
|
97
|
-
const message = fallbackORPCErrorMessage(code, options?.message);
|
|
98
|
-
super(message, options);
|
|
99
|
-
this.code = code;
|
|
100
|
-
this.status = fallbackORPCErrorStatus(code, options?.status);
|
|
101
|
-
this.defined = options?.defined ?? false;
|
|
102
|
-
this.data = options?.data;
|
|
103
|
-
}
|
|
104
|
-
toJSON() {
|
|
105
|
-
return {
|
|
106
|
-
defined: this.defined,
|
|
107
|
-
code: this.code,
|
|
108
|
-
status: this.status,
|
|
109
|
-
message: this.message,
|
|
110
|
-
data: this.data
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
static fromJSON(json, options) {
|
|
114
|
-
return new ORPCError(json.code, {
|
|
115
|
-
...options,
|
|
116
|
-
...json
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
static isValidJSON(json) {
|
|
120
|
-
if (!isObject(json)) {
|
|
121
|
-
return false;
|
|
122
|
-
}
|
|
123
|
-
const validKeys = ["defined", "code", "status", "message", "data"];
|
|
124
|
-
if (Object.keys(json).some((k) => !validKeys.includes(k))) {
|
|
125
|
-
return false;
|
|
126
|
-
}
|
|
127
|
-
return "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "status" in json && typeof json.status === "number" && "message" in json && typeof json.message === "string";
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
function isDefinedError(error) {
|
|
131
|
-
return error instanceof ORPCError && error.defined;
|
|
132
|
-
}
|
|
133
|
-
function toORPCError(error) {
|
|
134
|
-
return error instanceof ORPCError ? error : new ORPCError("INTERNAL_SERVER_ERROR", {
|
|
135
|
-
message: "Internal server error",
|
|
136
|
-
cause: error
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
function isORPCErrorStatus(status) {
|
|
140
|
-
return status < 200 || status >= 400;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
function mapEventIterator(iterator, maps) {
|
|
144
|
-
return async function* () {
|
|
145
|
-
try {
|
|
146
|
-
while (true) {
|
|
147
|
-
const { done, value } = await iterator.next();
|
|
148
|
-
let mappedValue = await maps.value(value, done);
|
|
149
|
-
if (mappedValue !== value) {
|
|
150
|
-
const meta = getEventMeta(value);
|
|
151
|
-
if (meta && isTypescriptObject(mappedValue)) {
|
|
152
|
-
mappedValue = withEventMeta(mappedValue, meta);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
if (done) {
|
|
156
|
-
return mappedValue;
|
|
157
|
-
}
|
|
158
|
-
yield mappedValue;
|
|
159
|
-
}
|
|
160
|
-
} catch (error) {
|
|
161
|
-
let mappedError = await maps.error(error);
|
|
162
|
-
if (mappedError !== error) {
|
|
163
|
-
const meta = getEventMeta(error);
|
|
164
|
-
if (meta && isTypescriptObject(mappedError)) {
|
|
165
|
-
mappedError = withEventMeta(mappedError, meta);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
throw mappedError;
|
|
169
|
-
} finally {
|
|
170
|
-
await iterator.return?.();
|
|
171
|
-
}
|
|
172
|
-
}();
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
export { COMMON_ORPC_ERROR_DEFS as C, ORPCError as O, fallbackORPCErrorMessage as a, isORPCErrorStatus as b, fallbackORPCErrorStatus as f, isDefinedError as i, mapEventIterator as m, toORPCError as t };
|