@orpc/client 0.0.0-next.b2e67f7 → 0.0.0-next.b3afdeb
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 +14 -2
- package/dist/adapters/fetch/index.d.mts +30 -0
- package/dist/adapters/fetch/index.d.ts +30 -0
- package/dist/adapters/fetch/index.mjs +37 -0
- package/dist/adapters/standard/index.d.mts +105 -0
- package/dist/adapters/standard/index.d.ts +105 -0
- package/dist/adapters/standard/index.mjs +5 -0
- package/dist/index.d.mts +150 -0
- package/dist/index.d.ts +150 -0
- package/dist/{index.js → index.mjs} +8 -35
- package/dist/plugins/index.d.mts +62 -0
- package/dist/plugins/index.d.ts +62 -0
- package/dist/plugins/index.mjs +127 -0
- package/dist/shared/client.3Q53fveR.mjs +334 -0
- package/dist/{chunk-X34KXUAJ.js → shared/client.BacCdg3F.mjs} +10 -119
- package/dist/shared/client.CupM8eRP.d.mts +30 -0
- package/dist/shared/client.CupM8eRP.d.ts +30 -0
- package/dist/shared/client.CvnV7_uV.mjs +12 -0
- package/dist/shared/client.DrOAzyMB.d.mts +45 -0
- package/dist/shared/client.aGal-uGY.d.ts +45 -0
- package/package.json +20 -25
- package/dist/chunk-4HZVK3GJ.js +0 -212
- package/dist/fetch.js +0 -128
- package/dist/openapi.js +0 -231
- package/dist/rpc.js +0 -10
- package/dist/src/adapters/fetch/index.d.ts +0 -3
- package/dist/src/adapters/fetch/rpc-link.d.ts +0 -98
- package/dist/src/adapters/fetch/types.d.ts +0 -5
- package/dist/src/client.d.ts +0 -9
- package/dist/src/dynamic-link.d.ts +0 -12
- package/dist/src/error.d.ts +0 -106
- package/dist/src/event-iterator-state.d.ts +0 -9
- package/dist/src/event-iterator.d.ts +0 -12
- package/dist/src/index.d.ts +0 -9
- package/dist/src/openapi/bracket-notation.d.ts +0 -9
- package/dist/src/openapi/index.d.ts +0 -4
- package/dist/src/openapi/json-serializer.d.ts +0 -7
- package/dist/src/openapi/serializer.d.ts +0 -11
- package/dist/src/rpc/index.d.ts +0 -3
- package/dist/src/rpc/json-serializer.d.ts +0 -12
- package/dist/src/rpc/serializer.d.ts +0 -9
- package/dist/src/types.d.ts +0 -29
- package/dist/src/utils.d.ts +0 -17
@@ -1,18 +1,8 @@
|
|
1
|
-
import {
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
fallbackORPCErrorMessage,
|
6
|
-
fallbackORPCErrorStatus,
|
7
|
-
isDefinedError,
|
8
|
-
mapEventIterator,
|
9
|
-
onEventIteratorStatusChange,
|
10
|
-
registerEventIteratorState,
|
11
|
-
toORPCError,
|
12
|
-
updateEventIteratorStatus
|
13
|
-
} from "./chunk-X34KXUAJ.js";
|
1
|
+
import { i as isDefinedError } from './shared/client.BacCdg3F.mjs';
|
2
|
+
export { C as COMMON_ORPC_ERROR_DEFS, O as ORPCError, a as fallbackORPCErrorMessage, f as fallbackORPCErrorStatus, m as mapEventIterator, t as toORPCError } from './shared/client.BacCdg3F.mjs';
|
3
|
+
export { onError, onFinish, onStart, onSuccess } from '@orpc/shared';
|
4
|
+
export { ErrorEvent } from '@orpc/standard-server';
|
14
5
|
|
15
|
-
// src/client.ts
|
16
6
|
function createORPCClient(link, options) {
|
17
7
|
const path = options?.path ?? [];
|
18
8
|
const procedureClient = async (...[input, options2]) => {
|
@@ -37,8 +27,7 @@ function createORPCClient(link, options) {
|
|
37
27
|
return recursive;
|
38
28
|
}
|
39
29
|
|
40
|
-
|
41
|
-
var DynamicLink = class {
|
30
|
+
class DynamicLink {
|
42
31
|
constructor(linkResolver) {
|
43
32
|
this.linkResolver = linkResolver;
|
44
33
|
}
|
@@ -47,9 +36,8 @@ var DynamicLink = class {
|
|
47
36
|
const output = await resolvedLink.call(path, input, options);
|
48
37
|
return output;
|
49
38
|
}
|
50
|
-
}
|
39
|
+
}
|
51
40
|
|
52
|
-
// src/utils.ts
|
53
41
|
async function safe(promise) {
|
54
42
|
try {
|
55
43
|
const output = await promise;
|
@@ -71,20 +59,5 @@ async function safe(promise) {
|
|
71
59
|
);
|
72
60
|
}
|
73
61
|
}
|
74
|
-
|
75
|
-
|
76
|
-
DynamicLink,
|
77
|
-
ORPCError,
|
78
|
-
createAutoRetryEventIterator,
|
79
|
-
createORPCClient,
|
80
|
-
fallbackORPCErrorMessage,
|
81
|
-
fallbackORPCErrorStatus,
|
82
|
-
isDefinedError,
|
83
|
-
mapEventIterator,
|
84
|
-
onEventIteratorStatusChange,
|
85
|
-
registerEventIteratorState,
|
86
|
-
safe,
|
87
|
-
toORPCError,
|
88
|
-
updateEventIteratorStatus
|
89
|
-
};
|
90
|
-
//# sourceMappingURL=index.js.map
|
62
|
+
|
63
|
+
export { DynamicLink, createORPCClient, isDefinedError, safe };
|
@@ -0,0 +1,62 @@
|
|
1
|
+
import { C as ClientPlugin, S as StandardLinkOptions } from '../shared/client.DrOAzyMB.mjs';
|
2
|
+
export { a as CompositeClientPlugin } from '../shared/client.DrOAzyMB.mjs';
|
3
|
+
import { Value } from '@orpc/shared';
|
4
|
+
import { C as ClientOptionsOut, a as ClientContext } from '../shared/client.CupM8eRP.mjs';
|
5
|
+
import '@orpc/standard-server';
|
6
|
+
|
7
|
+
interface ClientRetryPluginAttemptOptions {
|
8
|
+
lastEventRetry: number | undefined;
|
9
|
+
lastEventId: string | undefined;
|
10
|
+
attemptIndex: number;
|
11
|
+
error: unknown;
|
12
|
+
}
|
13
|
+
interface ClientRetryPluginContext {
|
14
|
+
/**
|
15
|
+
* Maximum retry attempts before throwing
|
16
|
+
* Use `Number.POSITIVE_INFINITY` for infinite retries (e.g., when handling Server-Sent Events).
|
17
|
+
*
|
18
|
+
* @default 0
|
19
|
+
*/
|
20
|
+
retry?: number;
|
21
|
+
/**
|
22
|
+
* Delay (in ms) before retrying.
|
23
|
+
*
|
24
|
+
* @default (o) => o.lastEventRetry ?? 2000
|
25
|
+
*/
|
26
|
+
retryDelay?: Value<number, [
|
27
|
+
attemptOptions: ClientRetryPluginAttemptOptions,
|
28
|
+
clientOptions: ClientOptionsOut<ClientRetryPluginContext>,
|
29
|
+
path: readonly string[],
|
30
|
+
input: unknown
|
31
|
+
]>;
|
32
|
+
/**
|
33
|
+
* Determine should retry or not.
|
34
|
+
*
|
35
|
+
* @default true
|
36
|
+
*/
|
37
|
+
shouldRetry?: Value<boolean, [
|
38
|
+
attemptOptions: ClientRetryPluginAttemptOptions,
|
39
|
+
clientOptions: ClientOptionsOut<ClientRetryPluginContext>,
|
40
|
+
path: readonly string[],
|
41
|
+
input: unknown
|
42
|
+
]>;
|
43
|
+
/**
|
44
|
+
* The hook called when retrying, and return the unsubscribe function.
|
45
|
+
*/
|
46
|
+
onRetry?: (options: ClientRetryPluginAttemptOptions, clientOptions: ClientOptionsOut<ClientRetryPluginContext>, path: readonly string[], input: unknown) => void | (() => void);
|
47
|
+
}
|
48
|
+
declare class ClientRetryPluginInvalidEventIteratorRetryResponse extends Error {
|
49
|
+
}
|
50
|
+
interface ClientRetryPluginOptions {
|
51
|
+
default?: ClientRetryPluginContext;
|
52
|
+
}
|
53
|
+
declare class ClientRetryPlugin<T extends ClientContext & ClientRetryPluginContext> implements ClientPlugin<T> {
|
54
|
+
private readonly defaultRetry;
|
55
|
+
private readonly defaultRetryDelay;
|
56
|
+
private readonly defaultShouldRetry;
|
57
|
+
private readonly defaultOnRetry;
|
58
|
+
constructor(options?: ClientRetryPluginOptions);
|
59
|
+
init(options: StandardLinkOptions<T>): void;
|
60
|
+
}
|
61
|
+
|
62
|
+
export { ClientPlugin, ClientRetryPlugin, type ClientRetryPluginAttemptOptions, type ClientRetryPluginContext, ClientRetryPluginInvalidEventIteratorRetryResponse, type ClientRetryPluginOptions };
|
@@ -0,0 +1,62 @@
|
|
1
|
+
import { C as ClientPlugin, S as StandardLinkOptions } from '../shared/client.aGal-uGY.js';
|
2
|
+
export { a as CompositeClientPlugin } from '../shared/client.aGal-uGY.js';
|
3
|
+
import { Value } from '@orpc/shared';
|
4
|
+
import { C as ClientOptionsOut, a as ClientContext } from '../shared/client.CupM8eRP.js';
|
5
|
+
import '@orpc/standard-server';
|
6
|
+
|
7
|
+
interface ClientRetryPluginAttemptOptions {
|
8
|
+
lastEventRetry: number | undefined;
|
9
|
+
lastEventId: string | undefined;
|
10
|
+
attemptIndex: number;
|
11
|
+
error: unknown;
|
12
|
+
}
|
13
|
+
interface ClientRetryPluginContext {
|
14
|
+
/**
|
15
|
+
* Maximum retry attempts before throwing
|
16
|
+
* Use `Number.POSITIVE_INFINITY` for infinite retries (e.g., when handling Server-Sent Events).
|
17
|
+
*
|
18
|
+
* @default 0
|
19
|
+
*/
|
20
|
+
retry?: number;
|
21
|
+
/**
|
22
|
+
* Delay (in ms) before retrying.
|
23
|
+
*
|
24
|
+
* @default (o) => o.lastEventRetry ?? 2000
|
25
|
+
*/
|
26
|
+
retryDelay?: Value<number, [
|
27
|
+
attemptOptions: ClientRetryPluginAttemptOptions,
|
28
|
+
clientOptions: ClientOptionsOut<ClientRetryPluginContext>,
|
29
|
+
path: readonly string[],
|
30
|
+
input: unknown
|
31
|
+
]>;
|
32
|
+
/**
|
33
|
+
* Determine should retry or not.
|
34
|
+
*
|
35
|
+
* @default true
|
36
|
+
*/
|
37
|
+
shouldRetry?: Value<boolean, [
|
38
|
+
attemptOptions: ClientRetryPluginAttemptOptions,
|
39
|
+
clientOptions: ClientOptionsOut<ClientRetryPluginContext>,
|
40
|
+
path: readonly string[],
|
41
|
+
input: unknown
|
42
|
+
]>;
|
43
|
+
/**
|
44
|
+
* The hook called when retrying, and return the unsubscribe function.
|
45
|
+
*/
|
46
|
+
onRetry?: (options: ClientRetryPluginAttemptOptions, clientOptions: ClientOptionsOut<ClientRetryPluginContext>, path: readonly string[], input: unknown) => void | (() => void);
|
47
|
+
}
|
48
|
+
declare class ClientRetryPluginInvalidEventIteratorRetryResponse extends Error {
|
49
|
+
}
|
50
|
+
interface ClientRetryPluginOptions {
|
51
|
+
default?: ClientRetryPluginContext;
|
52
|
+
}
|
53
|
+
declare class ClientRetryPlugin<T extends ClientContext & ClientRetryPluginContext> implements ClientPlugin<T> {
|
54
|
+
private readonly defaultRetry;
|
55
|
+
private readonly defaultRetryDelay;
|
56
|
+
private readonly defaultShouldRetry;
|
57
|
+
private readonly defaultOnRetry;
|
58
|
+
constructor(options?: ClientRetryPluginOptions);
|
59
|
+
init(options: StandardLinkOptions<T>): void;
|
60
|
+
}
|
61
|
+
|
62
|
+
export { ClientPlugin, ClientRetryPlugin, type ClientRetryPluginAttemptOptions, type ClientRetryPluginContext, ClientRetryPluginInvalidEventIteratorRetryResponse, type ClientRetryPluginOptions };
|
@@ -0,0 +1,127 @@
|
|
1
|
+
export { C as CompositeClientPlugin } from '../shared/client.CvnV7_uV.mjs';
|
2
|
+
import { isAsyncIteratorObject, value } from '@orpc/shared';
|
3
|
+
import { getEventMeta } from '@orpc/standard-server';
|
4
|
+
|
5
|
+
class ClientRetryPluginInvalidEventIteratorRetryResponse extends Error {
|
6
|
+
}
|
7
|
+
class ClientRetryPlugin {
|
8
|
+
defaultRetry;
|
9
|
+
defaultRetryDelay;
|
10
|
+
defaultShouldRetry;
|
11
|
+
defaultOnRetry;
|
12
|
+
constructor(options = {}) {
|
13
|
+
this.defaultRetry = options.default?.retry ?? 0;
|
14
|
+
this.defaultRetryDelay = options.default?.retryDelay ?? ((o) => o.lastEventRetry ?? 2e3);
|
15
|
+
this.defaultShouldRetry = options.default?.shouldRetry ?? true;
|
16
|
+
this.defaultOnRetry = options.default?.onRetry;
|
17
|
+
}
|
18
|
+
init(options) {
|
19
|
+
options.interceptors ??= [];
|
20
|
+
options.interceptors.push(async (interceptorOptions) => {
|
21
|
+
const maxAttempts = interceptorOptions.options.context.retry ?? this.defaultRetry;
|
22
|
+
const retryDelay = interceptorOptions.options.context.retryDelay ?? this.defaultRetryDelay;
|
23
|
+
const shouldRetry = interceptorOptions.options.context.shouldRetry ?? this.defaultShouldRetry;
|
24
|
+
const onRetry = interceptorOptions.options.context.onRetry ?? this.defaultOnRetry;
|
25
|
+
if (maxAttempts <= 0) {
|
26
|
+
return interceptorOptions.next();
|
27
|
+
}
|
28
|
+
let lastEventId = interceptorOptions.options.lastEventId;
|
29
|
+
let lastEventRetry;
|
30
|
+
let unsubscribe;
|
31
|
+
let attemptIndex = 0;
|
32
|
+
const next = async (initial) => {
|
33
|
+
let current = initial;
|
34
|
+
while (true) {
|
35
|
+
const newClientOptions = { ...interceptorOptions.options, lastEventId };
|
36
|
+
if (current) {
|
37
|
+
if (attemptIndex >= maxAttempts) {
|
38
|
+
throw current.error;
|
39
|
+
}
|
40
|
+
const attemptOptions = {
|
41
|
+
attemptIndex,
|
42
|
+
error: current.error,
|
43
|
+
lastEventId,
|
44
|
+
lastEventRetry
|
45
|
+
};
|
46
|
+
const shouldRetryBool = await value(
|
47
|
+
shouldRetry,
|
48
|
+
attemptOptions,
|
49
|
+
newClientOptions,
|
50
|
+
interceptorOptions.path,
|
51
|
+
interceptorOptions.input
|
52
|
+
);
|
53
|
+
if (!shouldRetryBool) {
|
54
|
+
throw current.error;
|
55
|
+
}
|
56
|
+
unsubscribe = onRetry?.(
|
57
|
+
attemptOptions,
|
58
|
+
newClientOptions,
|
59
|
+
interceptorOptions.path,
|
60
|
+
interceptorOptions.input
|
61
|
+
);
|
62
|
+
const retryDelayMs = await value(
|
63
|
+
retryDelay,
|
64
|
+
attemptOptions,
|
65
|
+
newClientOptions,
|
66
|
+
interceptorOptions.path,
|
67
|
+
interceptorOptions.input
|
68
|
+
);
|
69
|
+
await new Promise((resolve) => setTimeout(resolve, retryDelayMs));
|
70
|
+
attemptIndex++;
|
71
|
+
}
|
72
|
+
try {
|
73
|
+
const output2 = await interceptorOptions.next({
|
74
|
+
...interceptorOptions,
|
75
|
+
options: newClientOptions
|
76
|
+
});
|
77
|
+
return output2;
|
78
|
+
} catch (error) {
|
79
|
+
if (newClientOptions.signal?.aborted === true) {
|
80
|
+
throw error;
|
81
|
+
}
|
82
|
+
current = { error };
|
83
|
+
} finally {
|
84
|
+
unsubscribe?.();
|
85
|
+
unsubscribe = void 0;
|
86
|
+
}
|
87
|
+
}
|
88
|
+
};
|
89
|
+
const output = await next();
|
90
|
+
if (!isAsyncIteratorObject(output)) {
|
91
|
+
return output;
|
92
|
+
}
|
93
|
+
return async function* () {
|
94
|
+
let current = output;
|
95
|
+
try {
|
96
|
+
while (true) {
|
97
|
+
try {
|
98
|
+
const item = await current.next();
|
99
|
+
const meta = getEventMeta(item.value);
|
100
|
+
lastEventId = meta?.id ?? lastEventId;
|
101
|
+
lastEventRetry = meta?.retry ?? lastEventRetry;
|
102
|
+
if (item.done) {
|
103
|
+
return item.value;
|
104
|
+
}
|
105
|
+
yield item.value;
|
106
|
+
} catch (error) {
|
107
|
+
const meta = getEventMeta(error);
|
108
|
+
lastEventId = meta?.id ?? lastEventId;
|
109
|
+
lastEventRetry = meta?.retry ?? lastEventRetry;
|
110
|
+
const maybeEventIterator = await next({ error });
|
111
|
+
if (!isAsyncIteratorObject(maybeEventIterator)) {
|
112
|
+
throw new ClientRetryPluginInvalidEventIteratorRetryResponse(
|
113
|
+
"RetryPlugin: Expected an Event Iterator, got a non-Event Iterator"
|
114
|
+
);
|
115
|
+
}
|
116
|
+
current = maybeEventIterator;
|
117
|
+
}
|
118
|
+
}
|
119
|
+
} finally {
|
120
|
+
await current.return?.();
|
121
|
+
}
|
122
|
+
}();
|
123
|
+
});
|
124
|
+
}
|
125
|
+
}
|
126
|
+
|
127
|
+
export { ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse };
|
@@ -0,0 +1,334 @@
|
|
1
|
+
import { intercept, isObject, value, trim, isAsyncIteratorObject, stringifyJSON } from '@orpc/shared';
|
2
|
+
import { C as CompositeClientPlugin } from './client.CvnV7_uV.mjs';
|
3
|
+
import { ErrorEvent } from '@orpc/standard-server';
|
4
|
+
import { O as ORPCError, m as mapEventIterator, t as toORPCError } from './client.BacCdg3F.mjs';
|
5
|
+
|
6
|
+
class InvalidEventIteratorRetryResponse extends Error {
|
7
|
+
}
|
8
|
+
class StandardLink {
|
9
|
+
constructor(codec, sender, options = {}) {
|
10
|
+
this.codec = codec;
|
11
|
+
this.sender = sender;
|
12
|
+
const plugin = new CompositeClientPlugin(options.plugins);
|
13
|
+
plugin.init(options);
|
14
|
+
this.interceptors = options.interceptors ?? [];
|
15
|
+
this.clientInterceptors = options.clientInterceptors ?? [];
|
16
|
+
}
|
17
|
+
interceptors;
|
18
|
+
clientInterceptors;
|
19
|
+
call(path, input, options) {
|
20
|
+
return intercept(this.interceptors, { path, input, options }, async ({ path: path2, input: input2, options: options2 }) => {
|
21
|
+
const output = await this.#call(path2, input2, options2);
|
22
|
+
return output;
|
23
|
+
});
|
24
|
+
}
|
25
|
+
async #call(path, input, options) {
|
26
|
+
const request = await this.codec.encode(path, input, options);
|
27
|
+
const response = await intercept(
|
28
|
+
this.clientInterceptors,
|
29
|
+
{ request },
|
30
|
+
({ request: request2 }) => this.sender.call(request2, options, path, input)
|
31
|
+
);
|
32
|
+
const output = await this.codec.decode(response, options, path, input);
|
33
|
+
return output;
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
const STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES = {
|
38
|
+
BIGINT: 0,
|
39
|
+
DATE: 1,
|
40
|
+
NAN: 2,
|
41
|
+
UNDEFINED: 3,
|
42
|
+
URL: 4,
|
43
|
+
REGEXP: 5,
|
44
|
+
SET: 6,
|
45
|
+
MAP: 7
|
46
|
+
};
|
47
|
+
class StandardRPCJsonSerializer {
|
48
|
+
customSerializers;
|
49
|
+
constructor(options = {}) {
|
50
|
+
this.customSerializers = options.customJsonSerializers ?? [];
|
51
|
+
if (this.customSerializers.length !== new Set(this.customSerializers.map((custom) => custom.type)).size) {
|
52
|
+
throw new Error("Custom serializer type must be unique.");
|
53
|
+
}
|
54
|
+
}
|
55
|
+
serialize(data, segments = [], meta = [], maps = [], blobs = []) {
|
56
|
+
for (const custom of this.customSerializers) {
|
57
|
+
if (custom.condition(data)) {
|
58
|
+
const result = this.serialize(custom.serialize(data), segments, meta, maps, blobs);
|
59
|
+
meta.push([custom.type, ...segments]);
|
60
|
+
return result;
|
61
|
+
}
|
62
|
+
}
|
63
|
+
if (data instanceof Blob) {
|
64
|
+
maps.push(segments);
|
65
|
+
blobs.push(data);
|
66
|
+
return [data, meta, maps, blobs];
|
67
|
+
}
|
68
|
+
if (typeof data === "bigint") {
|
69
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.BIGINT, ...segments]);
|
70
|
+
return [data.toString(), meta, maps, blobs];
|
71
|
+
}
|
72
|
+
if (data instanceof Date) {
|
73
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.DATE, ...segments]);
|
74
|
+
if (Number.isNaN(data.getTime())) {
|
75
|
+
return [null, meta, maps, blobs];
|
76
|
+
}
|
77
|
+
return [data.toISOString(), meta, maps, blobs];
|
78
|
+
}
|
79
|
+
if (Number.isNaN(data)) {
|
80
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.NAN, ...segments]);
|
81
|
+
return [null, meta, maps, blobs];
|
82
|
+
}
|
83
|
+
if (data instanceof URL) {
|
84
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.URL, ...segments]);
|
85
|
+
return [data.toString(), meta, maps, blobs];
|
86
|
+
}
|
87
|
+
if (data instanceof RegExp) {
|
88
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.REGEXP, ...segments]);
|
89
|
+
return [data.toString(), meta, maps, blobs];
|
90
|
+
}
|
91
|
+
if (data instanceof Set) {
|
92
|
+
const result = this.serialize(Array.from(data), segments, meta, maps, blobs);
|
93
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.SET, ...segments]);
|
94
|
+
return result;
|
95
|
+
}
|
96
|
+
if (data instanceof Map) {
|
97
|
+
const result = this.serialize(Array.from(data.entries()), segments, meta, maps, blobs);
|
98
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.MAP, ...segments]);
|
99
|
+
return result;
|
100
|
+
}
|
101
|
+
if (Array.isArray(data)) {
|
102
|
+
const json = data.map((v, i) => {
|
103
|
+
if (v === void 0) {
|
104
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.UNDEFINED, ...segments, i]);
|
105
|
+
return v;
|
106
|
+
}
|
107
|
+
return this.serialize(v, [...segments, i], meta, maps, blobs)[0];
|
108
|
+
});
|
109
|
+
return [json, meta, maps, blobs];
|
110
|
+
}
|
111
|
+
if (isObject(data)) {
|
112
|
+
const json = {};
|
113
|
+
for (const k in data) {
|
114
|
+
if (k === "toJSON" && typeof data[k] === "function") {
|
115
|
+
continue;
|
116
|
+
}
|
117
|
+
json[k] = this.serialize(data[k], [...segments, k], meta, maps, blobs)[0];
|
118
|
+
}
|
119
|
+
return [json, meta, maps, blobs];
|
120
|
+
}
|
121
|
+
return [data, meta, maps, blobs];
|
122
|
+
}
|
123
|
+
deserialize(json, meta, maps, getBlob) {
|
124
|
+
const ref = { data: json };
|
125
|
+
if (maps && getBlob) {
|
126
|
+
maps.forEach((segments, i) => {
|
127
|
+
let currentRef = ref;
|
128
|
+
let preSegment = "data";
|
129
|
+
segments.forEach((segment) => {
|
130
|
+
currentRef = currentRef[preSegment];
|
131
|
+
preSegment = segment;
|
132
|
+
});
|
133
|
+
currentRef[preSegment] = getBlob(i);
|
134
|
+
});
|
135
|
+
}
|
136
|
+
for (const item of meta) {
|
137
|
+
const type = item[0];
|
138
|
+
let currentRef = ref;
|
139
|
+
let preSegment = "data";
|
140
|
+
for (let i = 1; i < item.length; i++) {
|
141
|
+
currentRef = currentRef[preSegment];
|
142
|
+
preSegment = item[i];
|
143
|
+
}
|
144
|
+
for (const custom of this.customSerializers) {
|
145
|
+
if (custom.type === type) {
|
146
|
+
currentRef[preSegment] = custom.deserialize(currentRef[preSegment]);
|
147
|
+
break;
|
148
|
+
}
|
149
|
+
}
|
150
|
+
switch (type) {
|
151
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.BIGINT:
|
152
|
+
currentRef[preSegment] = BigInt(currentRef[preSegment]);
|
153
|
+
break;
|
154
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.DATE:
|
155
|
+
currentRef[preSegment] = new Date(currentRef[preSegment] ?? "Invalid Date");
|
156
|
+
break;
|
157
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.NAN:
|
158
|
+
currentRef[preSegment] = Number.NaN;
|
159
|
+
break;
|
160
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.UNDEFINED:
|
161
|
+
currentRef[preSegment] = void 0;
|
162
|
+
break;
|
163
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.URL:
|
164
|
+
currentRef[preSegment] = new URL(currentRef[preSegment]);
|
165
|
+
break;
|
166
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.REGEXP: {
|
167
|
+
const [, pattern, flags] = currentRef[preSegment].match(/^\/(.*)\/([a-z]*)$/);
|
168
|
+
currentRef[preSegment] = new RegExp(pattern, flags);
|
169
|
+
break;
|
170
|
+
}
|
171
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.SET:
|
172
|
+
currentRef[preSegment] = new Set(currentRef[preSegment]);
|
173
|
+
break;
|
174
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.MAP:
|
175
|
+
currentRef[preSegment] = new Map(currentRef[preSegment]);
|
176
|
+
break;
|
177
|
+
}
|
178
|
+
}
|
179
|
+
return ref.data;
|
180
|
+
}
|
181
|
+
}
|
182
|
+
|
183
|
+
class StandardRPCLinkCodec {
|
184
|
+
constructor(serializer, options) {
|
185
|
+
this.serializer = serializer;
|
186
|
+
this.baseUrl = options.url;
|
187
|
+
this.maxUrlLength = options.maxUrlLength ?? 2083;
|
188
|
+
this.fallbackMethod = options.fallbackMethod ?? "POST";
|
189
|
+
this.expectedMethod = options.method ?? this.fallbackMethod;
|
190
|
+
this.headers = options.headers ?? {};
|
191
|
+
}
|
192
|
+
baseUrl;
|
193
|
+
maxUrlLength;
|
194
|
+
fallbackMethod;
|
195
|
+
expectedMethod;
|
196
|
+
headers;
|
197
|
+
async encode(path, input, options) {
|
198
|
+
const expectedMethod = await value(this.expectedMethod, options, path, input);
|
199
|
+
const headers = { ...await value(this.headers, options, path, input) };
|
200
|
+
const baseUrl = await value(this.baseUrl, options, path, input);
|
201
|
+
const url = new URL(`${trim(baseUrl.toString(), "/")}/${path.map(encodeURIComponent).join("/")}`);
|
202
|
+
if (options.lastEventId !== void 0) {
|
203
|
+
if (Array.isArray(headers["last-event-id"])) {
|
204
|
+
headers["last-event-id"] = [...headers["last-event-id"], options.lastEventId];
|
205
|
+
} else if (headers["last-event-id"] !== void 0) {
|
206
|
+
headers["last-event-id"] = [headers["last-event-id"], options.lastEventId];
|
207
|
+
} else {
|
208
|
+
headers["last-event-id"] = options.lastEventId;
|
209
|
+
}
|
210
|
+
}
|
211
|
+
const serialized = this.serializer.serialize(input);
|
212
|
+
if (expectedMethod === "GET" && !(serialized instanceof FormData) && !isAsyncIteratorObject(serialized)) {
|
213
|
+
const maxUrlLength = await value(this.maxUrlLength, options, path, input);
|
214
|
+
const getUrl = new URL(url);
|
215
|
+
getUrl.searchParams.append("data", stringifyJSON(serialized));
|
216
|
+
if (getUrl.toString().length <= maxUrlLength) {
|
217
|
+
return {
|
218
|
+
body: void 0,
|
219
|
+
method: expectedMethod,
|
220
|
+
headers,
|
221
|
+
url: getUrl,
|
222
|
+
signal: options.signal
|
223
|
+
};
|
224
|
+
}
|
225
|
+
}
|
226
|
+
return {
|
227
|
+
url,
|
228
|
+
method: expectedMethod === "GET" ? this.fallbackMethod : expectedMethod,
|
229
|
+
headers,
|
230
|
+
body: serialized,
|
231
|
+
signal: options.signal
|
232
|
+
};
|
233
|
+
}
|
234
|
+
async decode(response) {
|
235
|
+
const isOk = response.status >= 200 && response.status < 300;
|
236
|
+
const deserialized = await (async () => {
|
237
|
+
let isBodyOk = false;
|
238
|
+
try {
|
239
|
+
const body = await response.body();
|
240
|
+
isBodyOk = true;
|
241
|
+
return this.serializer.deserialize(body);
|
242
|
+
} catch (error) {
|
243
|
+
if (!isBodyOk) {
|
244
|
+
throw new Error("Cannot parse response body, please check the response body and content-type.", {
|
245
|
+
cause: error
|
246
|
+
});
|
247
|
+
}
|
248
|
+
throw new Error("Invalid RPC response format.", {
|
249
|
+
cause: error
|
250
|
+
});
|
251
|
+
}
|
252
|
+
})();
|
253
|
+
if (!isOk) {
|
254
|
+
if (ORPCError.isValidJSON(deserialized)) {
|
255
|
+
throw ORPCError.fromJSON(deserialized);
|
256
|
+
}
|
257
|
+
throw new Error("Invalid RPC error response format.", {
|
258
|
+
cause: deserialized
|
259
|
+
});
|
260
|
+
}
|
261
|
+
return deserialized;
|
262
|
+
}
|
263
|
+
}
|
264
|
+
|
265
|
+
class StandardRPCSerializer {
|
266
|
+
constructor(jsonSerializer) {
|
267
|
+
this.jsonSerializer = jsonSerializer;
|
268
|
+
}
|
269
|
+
serialize(data) {
|
270
|
+
if (isAsyncIteratorObject(data)) {
|
271
|
+
return mapEventIterator(data, {
|
272
|
+
value: async (value) => this.#serialize(value, false),
|
273
|
+
error: async (e) => {
|
274
|
+
return new ErrorEvent({
|
275
|
+
data: this.#serialize(toORPCError(e).toJSON(), false),
|
276
|
+
cause: e
|
277
|
+
});
|
278
|
+
}
|
279
|
+
});
|
280
|
+
}
|
281
|
+
return this.#serialize(data, true);
|
282
|
+
}
|
283
|
+
#serialize(data, enableFormData) {
|
284
|
+
const [json, meta_, maps, blobs] = this.jsonSerializer.serialize(data);
|
285
|
+
const meta = meta_.length === 0 ? void 0 : meta_;
|
286
|
+
if (!enableFormData || blobs.length === 0) {
|
287
|
+
return {
|
288
|
+
json,
|
289
|
+
meta
|
290
|
+
};
|
291
|
+
}
|
292
|
+
const form = new FormData();
|
293
|
+
form.set("data", stringifyJSON({ json, meta, maps }));
|
294
|
+
blobs.forEach((blob, i) => {
|
295
|
+
form.set(i.toString(), blob);
|
296
|
+
});
|
297
|
+
return form;
|
298
|
+
}
|
299
|
+
deserialize(data) {
|
300
|
+
if (isAsyncIteratorObject(data)) {
|
301
|
+
return mapEventIterator(data, {
|
302
|
+
value: async (value) => this.#deserialize(value),
|
303
|
+
error: async (e) => {
|
304
|
+
if (!(e instanceof ErrorEvent)) {
|
305
|
+
return e;
|
306
|
+
}
|
307
|
+
const deserialized = this.#deserialize(e.data);
|
308
|
+
if (ORPCError.isValidJSON(deserialized)) {
|
309
|
+
return ORPCError.fromJSON(deserialized, { cause: e });
|
310
|
+
}
|
311
|
+
return new ErrorEvent({
|
312
|
+
data: deserialized,
|
313
|
+
cause: e
|
314
|
+
});
|
315
|
+
}
|
316
|
+
});
|
317
|
+
}
|
318
|
+
return this.#deserialize(data);
|
319
|
+
}
|
320
|
+
#deserialize(data) {
|
321
|
+
if (!(data instanceof FormData)) {
|
322
|
+
return this.jsonSerializer.deserialize(data.json, data.meta ?? []);
|
323
|
+
}
|
324
|
+
const serialized = JSON.parse(data.get("data"));
|
325
|
+
return this.jsonSerializer.deserialize(
|
326
|
+
serialized.json,
|
327
|
+
serialized.meta ?? [],
|
328
|
+
serialized.maps,
|
329
|
+
(i) => data.get(i.toString())
|
330
|
+
);
|
331
|
+
}
|
332
|
+
}
|
333
|
+
|
334
|
+
export { InvalidEventIteratorRetryResponse as I, StandardLink as S, STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES as a, StandardRPCJsonSerializer as b, StandardRPCLinkCodec as c, StandardRPCSerializer as d };
|