@orpc/client 0.0.0-next.85df466 → 0.0.0-next.8900489
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 +5 -1
- package/dist/adapters/fetch/index.d.mts +22 -96
- package/dist/adapters/fetch/index.d.ts +22 -96
- package/dist/adapters/fetch/index.mjs +26 -111
- package/dist/adapters/standard/index.d.mts +160 -13
- package/dist/adapters/standard/index.d.ts +160 -13
- package/dist/adapters/standard/index.mjs +2 -2
- package/dist/index.d.mts +4 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.mjs +3 -3
- package/dist/shared/client.CPmBUYbj.mjs +351 -0
- package/dist/shared/{client.Ly4zGQrc.mjs → client.XAn8cDTM.mjs} +3 -2
- package/package.json +5 -5
- package/dist/shared/client.DHJ8vRIG.mjs +0 -192
|
@@ -1,22 +1,169 @@
|
|
|
1
|
-
import { Segment } from '@orpc/shared';
|
|
1
|
+
import { Value, Interceptor, Segment } from '@orpc/shared';
|
|
2
|
+
import { StandardRequest, StandardLazyResponse, StandardHeaders } from '@orpc/standard-server';
|
|
3
|
+
import { C as ClientContext, a as ClientOptionsOut, E as EventIteratorReconnectOptions, b as ClientLink } from '../../shared/client.D_CzLDyB.js';
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
serialize(data: unknown, segments?: Segment[], meta?: RPCJsonSerializedMeta, maps?: Segment[][], blobs?: Blob[]): RPCJsonSerialized;
|
|
10
|
-
deserialize(json: unknown, meta: RPCJsonSerializedMeta): unknown;
|
|
11
|
-
deserialize(json: unknown, meta: RPCJsonSerializedMeta, maps: Segment[][], getBlob: (index: number) => Blob): unknown;
|
|
5
|
+
interface StandardLinkCodec<T extends ClientContext> {
|
|
6
|
+
encode(path: readonly string[], input: unknown, options: ClientOptionsOut<any>): Promise<StandardRequest>;
|
|
7
|
+
decode(response: StandardLazyResponse, options: ClientOptionsOut<T>, path: readonly string[], input: unknown): Promise<unknown>;
|
|
8
|
+
}
|
|
9
|
+
interface StandardLinkClient<T extends ClientContext> {
|
|
10
|
+
call(request: StandardRequest, options: ClientOptionsOut<T>, path: readonly string[], input: unknown): Promise<StandardLazyResponse>;
|
|
12
11
|
}
|
|
13
12
|
|
|
14
|
-
declare class
|
|
13
|
+
declare class InvalidEventIteratorRetryResponse extends Error {
|
|
14
|
+
}
|
|
15
|
+
interface StandardLinkOptions<T extends ClientContext> {
|
|
16
|
+
/**
|
|
17
|
+
* Maximum retry attempts for **consecutive failures** before throwing
|
|
18
|
+
*
|
|
19
|
+
* @default 5
|
|
20
|
+
*/
|
|
21
|
+
eventIteratorMaxRetries?: Value<number, [
|
|
22
|
+
reconnectOptions: EventIteratorReconnectOptions,
|
|
23
|
+
options: ClientOptionsOut<T>,
|
|
24
|
+
path: readonly string[],
|
|
25
|
+
input: unknown
|
|
26
|
+
]>;
|
|
27
|
+
/**
|
|
28
|
+
* Delay (in ms) before retrying an event iterator call.
|
|
29
|
+
*
|
|
30
|
+
* @default (o) => o.lastRetry ?? (1000 * 2 ** o.retryTimes)
|
|
31
|
+
*/
|
|
32
|
+
eventIteratorRetryDelay?: Value<number, [
|
|
33
|
+
reconnectOptions: EventIteratorReconnectOptions,
|
|
34
|
+
options: ClientOptionsOut<T>,
|
|
35
|
+
path: readonly string[],
|
|
36
|
+
input: unknown
|
|
37
|
+
]>;
|
|
38
|
+
/**
|
|
39
|
+
* Function to determine if an error is retryable.
|
|
40
|
+
*
|
|
41
|
+
* @default true
|
|
42
|
+
*/
|
|
43
|
+
eventIteratorShouldRetry?: Value<boolean, [
|
|
44
|
+
reconnectOptions: EventIteratorReconnectOptions,
|
|
45
|
+
options: ClientOptionsOut<T>,
|
|
46
|
+
path: readonly string[],
|
|
47
|
+
input: unknown
|
|
48
|
+
]>;
|
|
49
|
+
interceptors?: Interceptor<{
|
|
50
|
+
path: readonly string[];
|
|
51
|
+
input: unknown;
|
|
52
|
+
options: ClientOptionsOut<T>;
|
|
53
|
+
}, unknown, unknown>[];
|
|
54
|
+
clientInterceptors?: Interceptor<{
|
|
55
|
+
request: StandardRequest;
|
|
56
|
+
}, StandardLazyResponse, unknown>[];
|
|
57
|
+
}
|
|
58
|
+
declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
|
|
59
|
+
#private;
|
|
60
|
+
readonly codec: StandardLinkCodec<T>;
|
|
61
|
+
readonly sender: StandardLinkClient<T>;
|
|
62
|
+
private readonly eventIteratorMaxRetries;
|
|
63
|
+
private readonly eventIteratorRetryDelay;
|
|
64
|
+
private readonly eventIteratorShouldRetry;
|
|
65
|
+
private readonly interceptors;
|
|
66
|
+
private readonly clientInterceptors;
|
|
67
|
+
constructor(codec: StandardLinkCodec<T>, sender: StandardLinkClient<T>, options: StandardLinkOptions<T>);
|
|
68
|
+
call(path: readonly string[], input: unknown, options: ClientOptionsOut<T>): Promise<unknown>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
declare const STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES: {
|
|
72
|
+
readonly BIGINT: 0;
|
|
73
|
+
readonly DATE: 1;
|
|
74
|
+
readonly NAN: 2;
|
|
75
|
+
readonly UNDEFINED: 3;
|
|
76
|
+
readonly URL: 4;
|
|
77
|
+
readonly REGEXP: 5;
|
|
78
|
+
readonly SET: 6;
|
|
79
|
+
readonly MAP: 7;
|
|
80
|
+
};
|
|
81
|
+
type StandardRPCJsonSerializedMetaItem = readonly [type: number, ...path: Segment[]];
|
|
82
|
+
type StandardRPCJsonSerialized = [json: unknown, meta: StandardRPCJsonSerializedMetaItem[], maps: Segment[][], blobs: Blob[]];
|
|
83
|
+
interface StandardRPCCustomJsonSerializer {
|
|
84
|
+
type: number;
|
|
85
|
+
condition(data: unknown): boolean;
|
|
86
|
+
serialize(data: any): unknown;
|
|
87
|
+
deserialize(serialized: any): unknown;
|
|
88
|
+
}
|
|
89
|
+
interface StandardRPCJsonSerializerOptions {
|
|
90
|
+
customJsonSerializers?: readonly StandardRPCCustomJsonSerializer[];
|
|
91
|
+
}
|
|
92
|
+
declare class StandardRPCJsonSerializer {
|
|
93
|
+
private readonly customSerializers;
|
|
94
|
+
constructor(options?: StandardRPCJsonSerializerOptions);
|
|
95
|
+
serialize(data: unknown, segments?: Segment[], meta?: StandardRPCJsonSerializedMetaItem[], maps?: Segment[][], blobs?: Blob[]): StandardRPCJsonSerialized;
|
|
96
|
+
deserialize(json: unknown, meta: readonly StandardRPCJsonSerializedMetaItem[]): unknown;
|
|
97
|
+
deserialize(json: unknown, meta: readonly StandardRPCJsonSerializedMetaItem[], maps: readonly Segment[][], getBlob: (index: number) => Blob): unknown;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
declare class StandardRPCSerializer {
|
|
15
101
|
#private;
|
|
16
102
|
private readonly jsonSerializer;
|
|
17
|
-
constructor(jsonSerializer
|
|
103
|
+
constructor(jsonSerializer: StandardRPCJsonSerializer);
|
|
18
104
|
serialize(data: unknown): unknown;
|
|
19
105
|
deserialize(data: unknown): unknown;
|
|
20
106
|
}
|
|
21
107
|
|
|
22
|
-
|
|
108
|
+
type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
109
|
+
interface StandardRPCLinkCodecOptions<T extends ClientContext> {
|
|
110
|
+
/**
|
|
111
|
+
* Base url for all requests.
|
|
112
|
+
*/
|
|
113
|
+
url: Value<string | URL, [
|
|
114
|
+
options: ClientOptionsOut<T>,
|
|
115
|
+
path: readonly string[],
|
|
116
|
+
input: unknown
|
|
117
|
+
]>;
|
|
118
|
+
/**
|
|
119
|
+
* The maximum length of the URL.
|
|
120
|
+
*
|
|
121
|
+
* @default 2083
|
|
122
|
+
*/
|
|
123
|
+
maxUrlLength?: Value<number, [
|
|
124
|
+
options: ClientOptionsOut<T>,
|
|
125
|
+
path: readonly string[],
|
|
126
|
+
input: unknown
|
|
127
|
+
]>;
|
|
128
|
+
/**
|
|
129
|
+
* The method used to make the request.
|
|
130
|
+
*
|
|
131
|
+
* @default 'POST'
|
|
132
|
+
*/
|
|
133
|
+
method?: Value<HTTPMethod, [
|
|
134
|
+
options: ClientOptionsOut<T>,
|
|
135
|
+
path: readonly string[],
|
|
136
|
+
input: unknown
|
|
137
|
+
]>;
|
|
138
|
+
/**
|
|
139
|
+
* The method to use when the payload cannot safely pass to the server with method return from method function.
|
|
140
|
+
* GET is not allowed, it's very dangerous.
|
|
141
|
+
*
|
|
142
|
+
* @default 'POST'
|
|
143
|
+
*/
|
|
144
|
+
fallbackMethod?: Exclude<HTTPMethod, 'GET'>;
|
|
145
|
+
/**
|
|
146
|
+
* Inject headers to the request.
|
|
147
|
+
*/
|
|
148
|
+
headers?: Value<StandardHeaders, [
|
|
149
|
+
options: ClientOptionsOut<T>,
|
|
150
|
+
path: readonly string[],
|
|
151
|
+
input: unknown
|
|
152
|
+
]>;
|
|
153
|
+
}
|
|
154
|
+
declare class StandardRPCLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
|
|
155
|
+
private readonly serializer;
|
|
156
|
+
private readonly baseUrl;
|
|
157
|
+
private readonly maxUrlLength;
|
|
158
|
+
private readonly fallbackMethod;
|
|
159
|
+
private readonly expectedMethod;
|
|
160
|
+
private readonly headers;
|
|
161
|
+
constructor(serializer: StandardRPCSerializer, options: StandardRPCLinkCodecOptions<T>);
|
|
162
|
+
encode(path: readonly string[], input: unknown, options: ClientOptionsOut<any>): Promise<StandardRequest>;
|
|
163
|
+
decode(response: StandardLazyResponse): Promise<unknown>;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
interface StandardRPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, StandardRPCLinkCodecOptions<T>, StandardRPCJsonSerializerOptions {
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export { InvalidEventIteratorRetryResponse, STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES, StandardLink, type StandardLinkClient, type StandardLinkCodec, type StandardLinkOptions, type StandardRPCCustomJsonSerializer, type StandardRPCJsonSerialized, type StandardRPCJsonSerializedMetaItem, StandardRPCJsonSerializer, type StandardRPCJsonSerializerOptions, StandardRPCLinkCodec, type StandardRPCLinkCodecOptions, type StandardRPCLinkOptions, StandardRPCSerializer };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { I as InvalidEventIteratorRetryResponse, a as STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES, S as StandardLink, b as StandardRPCJsonSerializer, c as StandardRPCLinkCodec, d as StandardRPCSerializer } from '../../shared/client.CPmBUYbj.mjs';
|
|
2
2
|
import '@orpc/shared';
|
|
3
|
+
import '../../shared/client.XAn8cDTM.mjs';
|
|
3
4
|
import '@orpc/standard-server';
|
|
4
|
-
import '../../shared/client.Ly4zGQrc.mjs';
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { N as NestedClient, b as ClientLink, I as InferClientContext, C as ClientContext, a as ClientOptionsOut, c as ClientPromiseResult } from './shared/client.D_CzLDyB.mjs';
|
|
2
2
|
export { g as Client, e as ClientOptions, f as ClientRest, E as EventIteratorReconnectOptions, d as createAutoRetryEventIterator, m as mapEventIterator } from './shared/client.D_CzLDyB.mjs';
|
|
3
3
|
import { Promisable, MaybeOptionalOptions } from '@orpc/shared';
|
|
4
|
+
export { onError, onFinish, onStart, onSuccess } from '@orpc/shared';
|
|
4
5
|
export { ErrorEvent } from '@orpc/standard-server';
|
|
5
6
|
|
|
6
7
|
interface createORPCClientOptions {
|
|
@@ -133,7 +134,9 @@ interface EventIteratorState {
|
|
|
133
134
|
}
|
|
134
135
|
declare function registerEventIteratorState(iterator: AsyncIteratorObject<unknown, unknown, void>, state: EventIteratorState): void;
|
|
135
136
|
declare function updateEventIteratorStatus(state: EventIteratorState, status: ConnectionStatus): void;
|
|
136
|
-
declare function onEventIteratorStatusChange(iterator: AsyncIteratorObject<unknown, unknown, void>, callback: (status: ConnectionStatus) => void,
|
|
137
|
+
declare function onEventIteratorStatusChange(iterator: AsyncIteratorObject<unknown, unknown, void>, callback: (status: ConnectionStatus) => void, options?: {
|
|
138
|
+
notifyImmediately?: boolean;
|
|
139
|
+
}): () => void;
|
|
137
140
|
|
|
138
141
|
type SafeResult<TOutput, TError extends Error> = [error: null, data: TOutput, isDefined: false] & {
|
|
139
142
|
error: null;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { N as NestedClient, b as ClientLink, I as InferClientContext, C as ClientContext, a as ClientOptionsOut, c as ClientPromiseResult } from './shared/client.D_CzLDyB.js';
|
|
2
2
|
export { g as Client, e as ClientOptions, f as ClientRest, E as EventIteratorReconnectOptions, d as createAutoRetryEventIterator, m as mapEventIterator } from './shared/client.D_CzLDyB.js';
|
|
3
3
|
import { Promisable, MaybeOptionalOptions } from '@orpc/shared';
|
|
4
|
+
export { onError, onFinish, onStart, onSuccess } from '@orpc/shared';
|
|
4
5
|
export { ErrorEvent } from '@orpc/standard-server';
|
|
5
6
|
|
|
6
7
|
interface createORPCClientOptions {
|
|
@@ -133,7 +134,9 @@ interface EventIteratorState {
|
|
|
133
134
|
}
|
|
134
135
|
declare function registerEventIteratorState(iterator: AsyncIteratorObject<unknown, unknown, void>, state: EventIteratorState): void;
|
|
135
136
|
declare function updateEventIteratorStatus(state: EventIteratorState, status: ConnectionStatus): void;
|
|
136
|
-
declare function onEventIteratorStatusChange(iterator: AsyncIteratorObject<unknown, unknown, void>, callback: (status: ConnectionStatus) => void,
|
|
137
|
+
declare function onEventIteratorStatusChange(iterator: AsyncIteratorObject<unknown, unknown, void>, callback: (status: ConnectionStatus) => void, options?: {
|
|
138
|
+
notifyImmediately?: boolean;
|
|
139
|
+
}): () => void;
|
|
137
140
|
|
|
138
141
|
type SafeResult<TOutput, TError extends Error> = [error: null, data: TOutput, isDefined: false] & {
|
|
139
142
|
error: null;
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { i as isDefinedError } from './shared/client.
|
|
2
|
-
export { C as COMMON_ORPC_ERROR_DEFS, O as ORPCError, c as createAutoRetryEventIterator, a as fallbackORPCErrorMessage, f as fallbackORPCErrorStatus, m as mapEventIterator, o as onEventIteratorStatusChange, r as registerEventIteratorState, t as toORPCError, u as updateEventIteratorStatus } from './shared/client.
|
|
1
|
+
import { i as isDefinedError } from './shared/client.XAn8cDTM.mjs';
|
|
2
|
+
export { C as COMMON_ORPC_ERROR_DEFS, O as ORPCError, c as createAutoRetryEventIterator, a as fallbackORPCErrorMessage, f as fallbackORPCErrorStatus, m as mapEventIterator, o as onEventIteratorStatusChange, r as registerEventIteratorState, t as toORPCError, u as updateEventIteratorStatus } from './shared/client.XAn8cDTM.mjs';
|
|
3
|
+
export { onError, onFinish, onStart, onSuccess } from '@orpc/shared';
|
|
3
4
|
export { ErrorEvent } from '@orpc/standard-server';
|
|
4
|
-
import '@orpc/shared';
|
|
5
5
|
|
|
6
6
|
function createORPCClient(link, options) {
|
|
7
7
|
const path = options?.path ?? [];
|
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
import { intercept, isAsyncIteratorObject, value, isObject, trim, stringifyJSON } from '@orpc/shared';
|
|
2
|
+
import { c as createAutoRetryEventIterator, O as ORPCError, m as mapEventIterator, t as toORPCError } from './client.XAn8cDTM.mjs';
|
|
3
|
+
import { ErrorEvent } from '@orpc/standard-server';
|
|
4
|
+
|
|
5
|
+
class InvalidEventIteratorRetryResponse extends Error {
|
|
6
|
+
}
|
|
7
|
+
class StandardLink {
|
|
8
|
+
constructor(codec, sender, options) {
|
|
9
|
+
this.codec = codec;
|
|
10
|
+
this.sender = sender;
|
|
11
|
+
this.eventIteratorMaxRetries = options.eventIteratorMaxRetries ?? 5;
|
|
12
|
+
this.eventIteratorRetryDelay = options.eventIteratorRetryDelay ?? ((o) => o.lastRetry ?? 1e3 * 2 ** o.retryTimes);
|
|
13
|
+
this.eventIteratorShouldRetry = options.eventIteratorShouldRetry ?? true;
|
|
14
|
+
this.interceptors = options.interceptors ?? [];
|
|
15
|
+
this.clientInterceptors = options.clientInterceptors ?? [];
|
|
16
|
+
}
|
|
17
|
+
eventIteratorMaxRetries;
|
|
18
|
+
eventIteratorRetryDelay;
|
|
19
|
+
eventIteratorShouldRetry;
|
|
20
|
+
interceptors;
|
|
21
|
+
clientInterceptors;
|
|
22
|
+
call(path, input, options) {
|
|
23
|
+
return intercept(this.interceptors, { path, input, options }, async ({ path: path2, input: input2, options: options2 }) => {
|
|
24
|
+
const output = await this.#call(path2, input2, options2);
|
|
25
|
+
if (!isAsyncIteratorObject(output)) {
|
|
26
|
+
return output;
|
|
27
|
+
}
|
|
28
|
+
return createAutoRetryEventIterator(output, async (reconnectOptions) => {
|
|
29
|
+
const maxRetries = await value(this.eventIteratorMaxRetries, reconnectOptions, options2, path2, input2);
|
|
30
|
+
if (options2.signal?.aborted || reconnectOptions.retryTimes > maxRetries) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
const shouldRetry = await value(this.eventIteratorShouldRetry, reconnectOptions, options2, path2, input2);
|
|
34
|
+
if (!shouldRetry) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
const retryDelay = await value(this.eventIteratorRetryDelay, reconnectOptions, options2, path2, input2);
|
|
38
|
+
await new Promise((resolve) => setTimeout(resolve, retryDelay));
|
|
39
|
+
const updatedOptions = { ...options2, lastEventId: reconnectOptions.lastEventId };
|
|
40
|
+
const maybeIterator = await this.#call(path2, input2, updatedOptions);
|
|
41
|
+
if (!isAsyncIteratorObject(maybeIterator)) {
|
|
42
|
+
throw new InvalidEventIteratorRetryResponse("Invalid Event Iterator retry response");
|
|
43
|
+
}
|
|
44
|
+
return maybeIterator;
|
|
45
|
+
}, options2.lastEventId);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
async #call(path, input, options) {
|
|
49
|
+
const request = await this.codec.encode(path, input, options);
|
|
50
|
+
const response = await intercept(
|
|
51
|
+
this.clientInterceptors,
|
|
52
|
+
{ request },
|
|
53
|
+
({ request: request2 }) => this.sender.call(request2, options, path, input)
|
|
54
|
+
);
|
|
55
|
+
const output = await this.codec.decode(response, options, path, input);
|
|
56
|
+
return output;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES = {
|
|
61
|
+
BIGINT: 0,
|
|
62
|
+
DATE: 1,
|
|
63
|
+
NAN: 2,
|
|
64
|
+
UNDEFINED: 3,
|
|
65
|
+
URL: 4,
|
|
66
|
+
REGEXP: 5,
|
|
67
|
+
SET: 6,
|
|
68
|
+
MAP: 7
|
|
69
|
+
};
|
|
70
|
+
class StandardRPCJsonSerializer {
|
|
71
|
+
customSerializers;
|
|
72
|
+
constructor(options = {}) {
|
|
73
|
+
this.customSerializers = options.customJsonSerializers ?? [];
|
|
74
|
+
if (this.customSerializers.length !== new Set(this.customSerializers.map((custom) => custom.type)).size) {
|
|
75
|
+
throw new Error("Custom serializer type must be unique.");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
serialize(data, segments = [], meta = [], maps = [], blobs = []) {
|
|
79
|
+
for (const custom of this.customSerializers) {
|
|
80
|
+
if (custom.condition(data)) {
|
|
81
|
+
const result = this.serialize(custom.serialize(data), segments, meta, maps, blobs);
|
|
82
|
+
meta.push([custom.type, ...segments]);
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (data instanceof Blob) {
|
|
87
|
+
maps.push(segments);
|
|
88
|
+
blobs.push(data);
|
|
89
|
+
return [data, meta, maps, blobs];
|
|
90
|
+
}
|
|
91
|
+
if (typeof data === "bigint") {
|
|
92
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.BIGINT, ...segments]);
|
|
93
|
+
return [data.toString(), meta, maps, blobs];
|
|
94
|
+
}
|
|
95
|
+
if (data instanceof Date) {
|
|
96
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.DATE, ...segments]);
|
|
97
|
+
if (Number.isNaN(data.getTime())) {
|
|
98
|
+
return [null, meta, maps, blobs];
|
|
99
|
+
}
|
|
100
|
+
return [data.toISOString(), meta, maps, blobs];
|
|
101
|
+
}
|
|
102
|
+
if (Number.isNaN(data)) {
|
|
103
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.NAN, ...segments]);
|
|
104
|
+
return [null, meta, maps, blobs];
|
|
105
|
+
}
|
|
106
|
+
if (data instanceof URL) {
|
|
107
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.URL, ...segments]);
|
|
108
|
+
return [data.toString(), meta, maps, blobs];
|
|
109
|
+
}
|
|
110
|
+
if (data instanceof RegExp) {
|
|
111
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.REGEXP, ...segments]);
|
|
112
|
+
return [data.toString(), meta, maps, blobs];
|
|
113
|
+
}
|
|
114
|
+
if (data instanceof Set) {
|
|
115
|
+
const result = this.serialize(Array.from(data), segments, meta, maps, blobs);
|
|
116
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.SET, ...segments]);
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
119
|
+
if (data instanceof Map) {
|
|
120
|
+
const result = this.serialize(Array.from(data.entries()), segments, meta, maps, blobs);
|
|
121
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.MAP, ...segments]);
|
|
122
|
+
return result;
|
|
123
|
+
}
|
|
124
|
+
if (Array.isArray(data)) {
|
|
125
|
+
const json = data.map((v, i) => {
|
|
126
|
+
if (v === void 0) {
|
|
127
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.UNDEFINED, ...segments, i]);
|
|
128
|
+
return v;
|
|
129
|
+
}
|
|
130
|
+
return this.serialize(v, [...segments, i], meta, maps, blobs)[0];
|
|
131
|
+
});
|
|
132
|
+
return [json, meta, maps, blobs];
|
|
133
|
+
}
|
|
134
|
+
if (isObject(data)) {
|
|
135
|
+
const json = {};
|
|
136
|
+
for (const k in data) {
|
|
137
|
+
json[k] = this.serialize(data[k], [...segments, k], meta, maps, blobs)[0];
|
|
138
|
+
}
|
|
139
|
+
return [json, meta, maps, blobs];
|
|
140
|
+
}
|
|
141
|
+
return [data, meta, maps, blobs];
|
|
142
|
+
}
|
|
143
|
+
deserialize(json, meta, maps, getBlob) {
|
|
144
|
+
const ref = { data: json };
|
|
145
|
+
if (maps && getBlob) {
|
|
146
|
+
maps.forEach((segments, i) => {
|
|
147
|
+
let currentRef = ref;
|
|
148
|
+
let preSegment = "data";
|
|
149
|
+
segments.forEach((segment) => {
|
|
150
|
+
currentRef = currentRef[preSegment];
|
|
151
|
+
preSegment = segment;
|
|
152
|
+
});
|
|
153
|
+
currentRef[preSegment] = getBlob(i);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
for (const item of meta) {
|
|
157
|
+
const type = item[0];
|
|
158
|
+
let currentRef = ref;
|
|
159
|
+
let preSegment = "data";
|
|
160
|
+
for (let i = 1; i < item.length; i++) {
|
|
161
|
+
currentRef = currentRef[preSegment];
|
|
162
|
+
preSegment = item[i];
|
|
163
|
+
}
|
|
164
|
+
for (const custom of this.customSerializers) {
|
|
165
|
+
if (custom.type === type) {
|
|
166
|
+
currentRef[preSegment] = custom.deserialize(currentRef[preSegment]);
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
switch (type) {
|
|
171
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.BIGINT:
|
|
172
|
+
currentRef[preSegment] = BigInt(currentRef[preSegment]);
|
|
173
|
+
break;
|
|
174
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.DATE:
|
|
175
|
+
currentRef[preSegment] = new Date(currentRef[preSegment] ?? "Invalid Date");
|
|
176
|
+
break;
|
|
177
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.NAN:
|
|
178
|
+
currentRef[preSegment] = Number.NaN;
|
|
179
|
+
break;
|
|
180
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.UNDEFINED:
|
|
181
|
+
currentRef[preSegment] = void 0;
|
|
182
|
+
break;
|
|
183
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.URL:
|
|
184
|
+
currentRef[preSegment] = new URL(currentRef[preSegment]);
|
|
185
|
+
break;
|
|
186
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.REGEXP: {
|
|
187
|
+
const [, pattern, flags] = currentRef[preSegment].match(/^\/(.*)\/([a-z]*)$/);
|
|
188
|
+
currentRef[preSegment] = new RegExp(pattern, flags);
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.SET:
|
|
192
|
+
currentRef[preSegment] = new Set(currentRef[preSegment]);
|
|
193
|
+
break;
|
|
194
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.MAP:
|
|
195
|
+
currentRef[preSegment] = new Map(currentRef[preSegment]);
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return ref.data;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
class StandardRPCLinkCodec {
|
|
204
|
+
constructor(serializer, options) {
|
|
205
|
+
this.serializer = serializer;
|
|
206
|
+
this.baseUrl = options.url;
|
|
207
|
+
this.maxUrlLength = options.maxUrlLength ?? 2083;
|
|
208
|
+
this.fallbackMethod = options.fallbackMethod ?? "POST";
|
|
209
|
+
this.expectedMethod = options.method ?? this.fallbackMethod;
|
|
210
|
+
this.headers = options.headers ?? {};
|
|
211
|
+
}
|
|
212
|
+
baseUrl;
|
|
213
|
+
maxUrlLength;
|
|
214
|
+
fallbackMethod;
|
|
215
|
+
expectedMethod;
|
|
216
|
+
headers;
|
|
217
|
+
async encode(path, input, options) {
|
|
218
|
+
const expectedMethod = await value(this.expectedMethod, options, path, input);
|
|
219
|
+
const headers = await value(this.headers, options, path, input);
|
|
220
|
+
const baseUrl = await value(this.baseUrl, options, path, input);
|
|
221
|
+
const url = new URL(`${trim(baseUrl.toString(), "/")}/${path.map(encodeURIComponent).join("/")}`);
|
|
222
|
+
const serialized = this.serializer.serialize(input);
|
|
223
|
+
if (expectedMethod === "GET" && !(serialized instanceof FormData) && !(serialized instanceof Blob) && !isAsyncIteratorObject(serialized)) {
|
|
224
|
+
const maxUrlLength = await value(this.maxUrlLength, options, path, input);
|
|
225
|
+
const getUrl = new URL(url);
|
|
226
|
+
getUrl.searchParams.append("data", stringifyJSON(serialized) ?? "");
|
|
227
|
+
if (getUrl.toString().length <= maxUrlLength) {
|
|
228
|
+
return {
|
|
229
|
+
body: void 0,
|
|
230
|
+
method: expectedMethod,
|
|
231
|
+
headers,
|
|
232
|
+
url: getUrl,
|
|
233
|
+
signal: options.signal
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return {
|
|
238
|
+
url,
|
|
239
|
+
method: expectedMethod === "GET" ? this.fallbackMethod : expectedMethod,
|
|
240
|
+
headers,
|
|
241
|
+
body: serialized,
|
|
242
|
+
signal: options.signal
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
async decode(response) {
|
|
246
|
+
const isOk = response.status >= 200 && response.status < 300;
|
|
247
|
+
const deserialized = await (async () => {
|
|
248
|
+
let isBodyOk = false;
|
|
249
|
+
try {
|
|
250
|
+
const body = await response.body();
|
|
251
|
+
isBodyOk = true;
|
|
252
|
+
return this.serializer.deserialize(body);
|
|
253
|
+
} catch (error) {
|
|
254
|
+
if (!isBodyOk) {
|
|
255
|
+
throw new Error("Cannot parse response body, please check the response body and content-type.", {
|
|
256
|
+
cause: error
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
throw new Error("Invalid RPC response format.", {
|
|
260
|
+
cause: error
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
})();
|
|
264
|
+
if (!isOk) {
|
|
265
|
+
if (ORPCError.isValidJSON(deserialized)) {
|
|
266
|
+
throw ORPCError.fromJSON(deserialized);
|
|
267
|
+
}
|
|
268
|
+
throw new Error("Invalid RPC error response format.", {
|
|
269
|
+
cause: deserialized
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
return deserialized;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
class StandardRPCSerializer {
|
|
277
|
+
constructor(jsonSerializer) {
|
|
278
|
+
this.jsonSerializer = jsonSerializer;
|
|
279
|
+
}
|
|
280
|
+
serialize(data) {
|
|
281
|
+
if (isAsyncIteratorObject(data)) {
|
|
282
|
+
return mapEventIterator(data, {
|
|
283
|
+
value: async (value) => this.#serialize(value, false),
|
|
284
|
+
error: async (e) => {
|
|
285
|
+
return new ErrorEvent({
|
|
286
|
+
data: this.#serialize(toORPCError(e).toJSON(), false),
|
|
287
|
+
cause: e
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
return this.#serialize(data, true);
|
|
293
|
+
}
|
|
294
|
+
#serialize(data, enableFormData) {
|
|
295
|
+
if (data === void 0 || data instanceof Blob) {
|
|
296
|
+
return data;
|
|
297
|
+
}
|
|
298
|
+
const [json, meta_, maps, blobs] = this.jsonSerializer.serialize(data);
|
|
299
|
+
const meta = meta_.length === 0 ? void 0 : meta_;
|
|
300
|
+
if (!enableFormData || blobs.length === 0) {
|
|
301
|
+
return {
|
|
302
|
+
json,
|
|
303
|
+
meta
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
const form = new FormData();
|
|
307
|
+
form.set("data", stringifyJSON({ json, meta, maps }));
|
|
308
|
+
blobs.forEach((blob, i) => {
|
|
309
|
+
form.set(i.toString(), blob);
|
|
310
|
+
});
|
|
311
|
+
return form;
|
|
312
|
+
}
|
|
313
|
+
deserialize(data) {
|
|
314
|
+
if (isAsyncIteratorObject(data)) {
|
|
315
|
+
return mapEventIterator(data, {
|
|
316
|
+
value: async (value) => this.#deserialize(value),
|
|
317
|
+
error: async (e) => {
|
|
318
|
+
if (!(e instanceof ErrorEvent)) {
|
|
319
|
+
return e;
|
|
320
|
+
}
|
|
321
|
+
const deserialized = this.#deserialize(e.data);
|
|
322
|
+
if (ORPCError.isValidJSON(deserialized)) {
|
|
323
|
+
return ORPCError.fromJSON(deserialized, { cause: e });
|
|
324
|
+
}
|
|
325
|
+
return new ErrorEvent({
|
|
326
|
+
data: deserialized,
|
|
327
|
+
cause: e
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
return this.#deserialize(data);
|
|
333
|
+
}
|
|
334
|
+
#deserialize(data) {
|
|
335
|
+
if (data === void 0 || data instanceof Blob) {
|
|
336
|
+
return data;
|
|
337
|
+
}
|
|
338
|
+
if (!(data instanceof FormData)) {
|
|
339
|
+
return this.jsonSerializer.deserialize(data.json, data.meta ?? []);
|
|
340
|
+
}
|
|
341
|
+
const serialized = JSON.parse(data.get("data"));
|
|
342
|
+
return this.jsonSerializer.deserialize(
|
|
343
|
+
serialized.json,
|
|
344
|
+
serialized.meta ?? [],
|
|
345
|
+
serialized.maps,
|
|
346
|
+
(i) => data.get(i.toString())
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
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 };
|
|
@@ -147,7 +147,8 @@ function updateEventIteratorStatus(state, status) {
|
|
|
147
147
|
state.listeners.forEach((cb) => cb(status));
|
|
148
148
|
}
|
|
149
149
|
}
|
|
150
|
-
function onEventIteratorStatusChange(iterator, callback,
|
|
150
|
+
function onEventIteratorStatusChange(iterator, callback, options = {}) {
|
|
151
|
+
const notifyImmediately = options.notifyImmediately ?? true;
|
|
151
152
|
const state = iteratorStates.get(iterator);
|
|
152
153
|
if (!state) {
|
|
153
154
|
throw new Error("Iterator is not registered.");
|
|
@@ -229,7 +230,7 @@ function createAutoRetryEventIterator(initial, reconnect, initialLastEventId) {
|
|
|
229
230
|
retryTimes += 1;
|
|
230
231
|
if (retryTimes > MAX_ALLOWED_RETRY_TIMES) {
|
|
231
232
|
throw exit(new Error(
|
|
232
|
-
`Exceeded maximum retry attempts (${MAX_ALLOWED_RETRY_TIMES}) for event
|
|
233
|
+
`Exceeded maximum retry attempts (${MAX_ALLOWED_RETRY_TIMES}) for event iterator. Possible infinite retry loop detected. Please review the retry logic.`,
|
|
233
234
|
{ cause: currentError }
|
|
234
235
|
));
|
|
235
236
|
}
|
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.8900489",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -34,12 +34,12 @@
|
|
|
34
34
|
"dist"
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@orpc/
|
|
38
|
-
"@orpc/standard-server": "0.0.0-next.
|
|
39
|
-
"@orpc/
|
|
37
|
+
"@orpc/standard-server": "0.0.0-next.8900489",
|
|
38
|
+
"@orpc/standard-server-fetch": "0.0.0-next.8900489",
|
|
39
|
+
"@orpc/shared": "0.0.0-next.8900489"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"zod": "^3.24.
|
|
42
|
+
"zod": "^3.24.2"
|
|
43
43
|
},
|
|
44
44
|
"scripts": {
|
|
45
45
|
"build": "unbuild",
|