@orpc/client 0.0.0-next.ea0903c → 0.0.0-next.ea1d4fd
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 +23 -22
- package/dist/adapters/fetch/index.d.mts +31 -15
- package/dist/adapters/fetch/index.d.ts +31 -15
- package/dist/adapters/fetch/index.mjs +27 -17
- 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 +7 -5
- package/dist/adapters/standard/index.d.ts +7 -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 +106 -27
- package/dist/index.d.ts +106 -27
- package/dist/index.mjs +61 -14
- package/dist/plugins/index.d.mts +209 -21
- package/dist/plugins/index.d.ts +209 -21
- package/dist/plugins/index.mjs +412 -53
- package/dist/shared/client.BDbd8GMX.mjs +171 -0
- 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.CZFIVTQj.d.ts → client.BxV-mzeR.d.ts} +13 -25
- package/dist/shared/{client.D9lmRwGB.d.mts → client.CPgZaUox.d.mts} +20 -14
- package/dist/shared/{client.BC0T26HA.d.mts → client.D8lMmWVC.d.mts} +13 -25
- package/dist/shared/{client.BaocqKnn.d.ts → client.De8SW4Kw.d.ts} +20 -14
- package/dist/shared/{client.F0iTxiCl.mjs → client._xOn23bS.mjs} +99 -39
- package/package.json +16 -5
- package/dist/shared/client.87WXDX8t.d.mts +0 -32
- package/dist/shared/client.87WXDX8t.d.ts +0 -32
- package/dist/shared/client.BacCdg3F.mjs +0 -172
|
@@ -1,36 +1,79 @@
|
|
|
1
|
-
import { toArray, intercept, isObject, value,
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { toArray, runWithSpan, ORPC_NAME, isAsyncIteratorObject, asyncIteratorWithSpan, intercept, getGlobalOtelConfig, isObject, value, stringifyJSON } from '@orpc/shared';
|
|
2
|
+
import { mergeStandardHeaders, ErrorEvent } from '@orpc/standard-server';
|
|
3
|
+
import { C as COMMON_ORPC_ERROR_DEFS, d as isORPCErrorStatus, e as isORPCErrorJson, g as createORPCErrorFromJson, c as ORPCError, t as toORPCError } from './client.BDbd8GMX.mjs';
|
|
4
|
+
import { toStandardHeaders as toStandardHeaders$1 } from '@orpc/standard-server-fetch';
|
|
5
|
+
import { m as mapEventIterator } from './client.BLtwTQUg.mjs';
|
|
4
6
|
|
|
5
|
-
class
|
|
7
|
+
class CompositeStandardLinkPlugin {
|
|
8
|
+
plugins;
|
|
9
|
+
constructor(plugins = []) {
|
|
10
|
+
this.plugins = [...plugins].sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
11
|
+
}
|
|
12
|
+
init(options) {
|
|
13
|
+
for (const plugin of this.plugins) {
|
|
14
|
+
plugin.init?.(options);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
6
17
|
}
|
|
18
|
+
|
|
7
19
|
class StandardLink {
|
|
8
20
|
constructor(codec, sender, options = {}) {
|
|
9
21
|
this.codec = codec;
|
|
10
22
|
this.sender = sender;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
23
|
+
const plugin = new CompositeStandardLinkPlugin(options.plugins);
|
|
24
|
+
plugin.init(options);
|
|
14
25
|
this.interceptors = toArray(options.interceptors);
|
|
15
26
|
this.clientInterceptors = toArray(options.clientInterceptors);
|
|
16
27
|
}
|
|
17
28
|
interceptors;
|
|
18
29
|
clientInterceptors;
|
|
19
30
|
call(path, input, options) {
|
|
20
|
-
return
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
return runWithSpan(
|
|
32
|
+
{ name: `${ORPC_NAME}.${path.join("/")}`, signal: options.signal },
|
|
33
|
+
(span) => {
|
|
34
|
+
span?.setAttribute("rpc.system", ORPC_NAME);
|
|
35
|
+
span?.setAttribute("rpc.method", path.join("."));
|
|
36
|
+
if (isAsyncIteratorObject(input)) {
|
|
37
|
+
input = asyncIteratorWithSpan(
|
|
38
|
+
{ name: "consume_event_iterator_input", signal: options.signal },
|
|
39
|
+
input
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
return intercept(this.interceptors, { ...options, path, input }, async ({ path: path2, input: input2, ...options2 }) => {
|
|
43
|
+
const otelConfig = getGlobalOtelConfig();
|
|
44
|
+
let otelContext;
|
|
45
|
+
const currentSpan = otelConfig?.trace.getActiveSpan() ?? span;
|
|
46
|
+
if (currentSpan && otelConfig) {
|
|
47
|
+
otelContext = otelConfig?.trace.setSpan(otelConfig.context.active(), currentSpan);
|
|
48
|
+
}
|
|
49
|
+
const request = await runWithSpan(
|
|
50
|
+
{ name: "encode_request", context: otelContext },
|
|
51
|
+
() => this.codec.encode(path2, input2, options2)
|
|
52
|
+
);
|
|
53
|
+
const response = await intercept(
|
|
54
|
+
this.clientInterceptors,
|
|
55
|
+
{ ...options2, input: input2, path: path2, request },
|
|
56
|
+
({ input: input3, path: path3, request: request2, ...options3 }) => {
|
|
57
|
+
return runWithSpan(
|
|
58
|
+
{ name: "send_request", signal: options3.signal, context: otelContext },
|
|
59
|
+
() => this.sender.call(request2, options3, path3, input3)
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
);
|
|
63
|
+
const output = await runWithSpan(
|
|
64
|
+
{ name: "decode_response", context: otelContext },
|
|
65
|
+
() => this.codec.decode(response, options2, path2, input2)
|
|
66
|
+
);
|
|
67
|
+
if (isAsyncIteratorObject(output)) {
|
|
68
|
+
return asyncIteratorWithSpan(
|
|
69
|
+
{ name: "consume_event_iterator_output", signal: options2.signal },
|
|
70
|
+
output
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
return output;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
31
76
|
);
|
|
32
|
-
const output = await this.codec.decode(response, options, path, input);
|
|
33
|
-
return output;
|
|
34
77
|
}
|
|
35
78
|
}
|
|
36
79
|
|
|
@@ -183,6 +226,15 @@ class StandardRPCJsonSerializer {
|
|
|
183
226
|
function toHttpPath(path) {
|
|
184
227
|
return `/${path.map(encodeURIComponent).join("/")}`;
|
|
185
228
|
}
|
|
229
|
+
function toStandardHeaders(headers) {
|
|
230
|
+
if (typeof headers.forEach === "function") {
|
|
231
|
+
return toStandardHeaders$1(headers);
|
|
232
|
+
}
|
|
233
|
+
return headers;
|
|
234
|
+
}
|
|
235
|
+
function getMalformedResponseErrorCode(status) {
|
|
236
|
+
return Object.entries(COMMON_ORPC_ERROR_DEFS).find(([, def]) => def.status === status)?.[0] ?? "MALFORMED_ORPC_ERROR_RESPONSE";
|
|
237
|
+
}
|
|
186
238
|
|
|
187
239
|
class StandardRPCLinkCodec {
|
|
188
240
|
constructor(serializer, options) {
|
|
@@ -199,19 +251,14 @@ class StandardRPCLinkCodec {
|
|
|
199
251
|
expectedMethod;
|
|
200
252
|
headers;
|
|
201
253
|
async encode(path, input, options) {
|
|
202
|
-
|
|
203
|
-
const headers = { ...await value(this.headers, options, path, input) };
|
|
204
|
-
const baseUrl = await value(this.baseUrl, options, path, input);
|
|
205
|
-
const url = new URL(`${baseUrl.toString().replace(/\/$/, "")}${toHttpPath(path)}`);
|
|
254
|
+
let headers = toStandardHeaders(await value(this.headers, options, path, input));
|
|
206
255
|
if (options.lastEventId !== void 0) {
|
|
207
|
-
|
|
208
|
-
headers["last-event-id"] = [...headers["last-event-id"], options.lastEventId];
|
|
209
|
-
} else if (headers["last-event-id"] !== void 0) {
|
|
210
|
-
headers["last-event-id"] = [headers["last-event-id"], options.lastEventId];
|
|
211
|
-
} else {
|
|
212
|
-
headers["last-event-id"] = options.lastEventId;
|
|
213
|
-
}
|
|
256
|
+
headers = mergeStandardHeaders(headers, { "last-event-id": options.lastEventId });
|
|
214
257
|
}
|
|
258
|
+
const expectedMethod = await value(this.expectedMethod, options, path, input);
|
|
259
|
+
const baseUrl = await value(this.baseUrl, options, path, input);
|
|
260
|
+
const url = new URL(baseUrl);
|
|
261
|
+
url.pathname = `${url.pathname.replace(/\/$/, "")}${toHttpPath(path)}`;
|
|
215
262
|
const serialized = this.serializer.serialize(input);
|
|
216
263
|
if (expectedMethod === "GET" && !(serialized instanceof FormData) && !isAsyncIteratorObject(serialized)) {
|
|
217
264
|
const maxUrlLength = await value(this.maxUrlLength, options, path, input);
|
|
@@ -236,7 +283,7 @@ class StandardRPCLinkCodec {
|
|
|
236
283
|
};
|
|
237
284
|
}
|
|
238
285
|
async decode(response) {
|
|
239
|
-
const isOk = response.status
|
|
286
|
+
const isOk = !isORPCErrorStatus(response.status);
|
|
240
287
|
const deserialized = await (async () => {
|
|
241
288
|
let isBodyOk = false;
|
|
242
289
|
try {
|
|
@@ -255,11 +302,12 @@ class StandardRPCLinkCodec {
|
|
|
255
302
|
}
|
|
256
303
|
})();
|
|
257
304
|
if (!isOk) {
|
|
258
|
-
if (
|
|
259
|
-
throw
|
|
305
|
+
if (isORPCErrorJson(deserialized)) {
|
|
306
|
+
throw createORPCErrorFromJson(deserialized);
|
|
260
307
|
}
|
|
261
|
-
throw new
|
|
262
|
-
|
|
308
|
+
throw new ORPCError(getMalformedResponseErrorCode(response.status), {
|
|
309
|
+
status: response.status,
|
|
310
|
+
data: { ...response, body: deserialized }
|
|
263
311
|
});
|
|
264
312
|
}
|
|
265
313
|
return deserialized;
|
|
@@ -309,8 +357,8 @@ class StandardRPCSerializer {
|
|
|
309
357
|
return e;
|
|
310
358
|
}
|
|
311
359
|
const deserialized = this.#deserialize(e.data);
|
|
312
|
-
if (
|
|
313
|
-
return
|
|
360
|
+
if (isORPCErrorJson(deserialized)) {
|
|
361
|
+
return createORPCErrorFromJson(deserialized, { cause: e });
|
|
314
362
|
}
|
|
315
363
|
return new ErrorEvent({
|
|
316
364
|
data: deserialized,
|
|
@@ -322,6 +370,9 @@ class StandardRPCSerializer {
|
|
|
322
370
|
return this.#deserialize(data);
|
|
323
371
|
}
|
|
324
372
|
#deserialize(data) {
|
|
373
|
+
if (data === void 0) {
|
|
374
|
+
return void 0;
|
|
375
|
+
}
|
|
325
376
|
if (!(data instanceof FormData)) {
|
|
326
377
|
return this.jsonSerializer.deserialize(data.json, data.meta ?? []);
|
|
327
378
|
}
|
|
@@ -335,4 +386,13 @@ class StandardRPCSerializer {
|
|
|
335
386
|
}
|
|
336
387
|
}
|
|
337
388
|
|
|
338
|
-
|
|
389
|
+
class StandardRPCLink extends StandardLink {
|
|
390
|
+
constructor(linkClient, options) {
|
|
391
|
+
const jsonSerializer = new StandardRPCJsonSerializer(options);
|
|
392
|
+
const serializer = new StandardRPCSerializer(jsonSerializer);
|
|
393
|
+
const linkCodec = new StandardRPCLinkCodec(serializer, options);
|
|
394
|
+
super(linkCodec, linkClient, options);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
export { CompositeStandardLinkPlugin as C, StandardLink as S, STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES as a, StandardRPCJsonSerializer as b, StandardRPCLink as c, StandardRPCLinkCodec as d, StandardRPCSerializer as e, toStandardHeaders as f, getMalformedResponseErrorCode as g, toHttpPath as t };
|
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.ea1d4fd",
|
|
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
|
|
44
|
-
"@orpc/standard-server": "0.0.0-next.
|
|
52
|
+
"@orpc/standard-server-fetch": "0.0.0-next.ea1d4fd",
|
|
53
|
+
"@orpc/standard-server": "0.0.0-next.ea1d4fd",
|
|
54
|
+
"@orpc/standard-server-peer": "0.0.0-next.ea1d4fd",
|
|
55
|
+
"@orpc/shared": "0.0.0-next.ea1d4fd"
|
|
45
56
|
},
|
|
46
57
|
"devDependencies": {
|
|
47
|
-
"zod": "^
|
|
58
|
+
"zod": "^4.1.12"
|
|
48
59
|
},
|
|
49
60
|
"scripts": {
|
|
50
61
|
"build": "unbuild",
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
type HTTPPath = `/${string}`;
|
|
2
|
-
type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
3
|
-
type ClientContext = Record<string, any>;
|
|
4
|
-
type FriendlyClientOptions<TClientContext extends ClientContext> = {
|
|
5
|
-
signal?: AbortSignal;
|
|
6
|
-
lastEventId?: string | undefined;
|
|
7
|
-
} & (Record<never, never> extends TClientContext ? {
|
|
8
|
-
context?: TClientContext;
|
|
9
|
-
} : {
|
|
10
|
-
context: TClientContext;
|
|
11
|
-
});
|
|
12
|
-
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>];
|
|
13
|
-
type ClientPromiseResult<TOutput, TError extends Error> = Promise<TOutput> & {
|
|
14
|
-
__error?: {
|
|
15
|
-
type: TError;
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
|
-
interface Client<TClientContext extends ClientContext, TInput, TOutput, TError extends Error> {
|
|
19
|
-
(...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
|
|
20
|
-
}
|
|
21
|
-
type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
|
|
22
|
-
[k: string]: NestedClient<TClientContext>;
|
|
23
|
-
};
|
|
24
|
-
type InferClientContext<T extends NestedClient<any>> = T extends NestedClient<infer U> ? U : never;
|
|
25
|
-
type ClientOptions<TClientContext extends ClientContext> = FriendlyClientOptions<TClientContext> & {
|
|
26
|
-
context: TClientContext;
|
|
27
|
-
};
|
|
28
|
-
interface ClientLink<TClientContext extends ClientContext> {
|
|
29
|
-
call: (path: readonly string[], input: unknown, options: ClientOptions<TClientContext>) => Promise<unknown>;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
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,32 +0,0 @@
|
|
|
1
|
-
type HTTPPath = `/${string}`;
|
|
2
|
-
type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
3
|
-
type ClientContext = Record<string, any>;
|
|
4
|
-
type FriendlyClientOptions<TClientContext extends ClientContext> = {
|
|
5
|
-
signal?: AbortSignal;
|
|
6
|
-
lastEventId?: string | undefined;
|
|
7
|
-
} & (Record<never, never> extends TClientContext ? {
|
|
8
|
-
context?: TClientContext;
|
|
9
|
-
} : {
|
|
10
|
-
context: TClientContext;
|
|
11
|
-
});
|
|
12
|
-
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>];
|
|
13
|
-
type ClientPromiseResult<TOutput, TError extends Error> = Promise<TOutput> & {
|
|
14
|
-
__error?: {
|
|
15
|
-
type: TError;
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
|
-
interface Client<TClientContext extends ClientContext, TInput, TOutput, TError extends Error> {
|
|
19
|
-
(...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
|
|
20
|
-
}
|
|
21
|
-
type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
|
|
22
|
-
[k: string]: NestedClient<TClientContext>;
|
|
23
|
-
};
|
|
24
|
-
type InferClientContext<T extends NestedClient<any>> = T extends NestedClient<infer U> ? U : never;
|
|
25
|
-
type ClientOptions<TClientContext extends ClientContext> = FriendlyClientOptions<TClientContext> & {
|
|
26
|
-
context: TClientContext;
|
|
27
|
-
};
|
|
28
|
-
interface ClientLink<TClientContext extends ClientContext> {
|
|
29
|
-
call: (path: readonly string[], input: unknown, options: ClientOptions<TClientContext>) => Promise<unknown>;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
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,172 +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 && (options.status < 400 || options.status >= 600)) {
|
|
95
|
-
throw new Error("[ORPCError] The error status code must be in the 400-599 range.");
|
|
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
|
-
|
|
140
|
-
function mapEventIterator(iterator, maps) {
|
|
141
|
-
return async function* () {
|
|
142
|
-
try {
|
|
143
|
-
while (true) {
|
|
144
|
-
const { done, value } = await iterator.next();
|
|
145
|
-
let mappedValue = await maps.value(value, done);
|
|
146
|
-
if (mappedValue !== value) {
|
|
147
|
-
const meta = getEventMeta(value);
|
|
148
|
-
if (meta && isTypescriptObject(mappedValue)) {
|
|
149
|
-
mappedValue = withEventMeta(mappedValue, meta);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
if (done) {
|
|
153
|
-
return mappedValue;
|
|
154
|
-
}
|
|
155
|
-
yield mappedValue;
|
|
156
|
-
}
|
|
157
|
-
} catch (error) {
|
|
158
|
-
let mappedError = await maps.error(error);
|
|
159
|
-
if (mappedError !== error) {
|
|
160
|
-
const meta = getEventMeta(error);
|
|
161
|
-
if (meta && isTypescriptObject(mappedError)) {
|
|
162
|
-
mappedError = withEventMeta(mappedError, meta);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
throw mappedError;
|
|
166
|
-
} finally {
|
|
167
|
-
await iterator.return?.();
|
|
168
|
-
}
|
|
169
|
-
}();
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
export { COMMON_ORPC_ERROR_DEFS as C, ORPCError as O, fallbackORPCErrorMessage as a, fallbackORPCErrorStatus as f, isDefinedError as i, mapEventIterator as m, toORPCError as t };
|