@orpc/client 0.0.0-next.df024bb → 0.0.0-next.e0f01a5
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 -1
- package/dist/adapters/fetch/index.d.mts +11 -15
- package/dist/adapters/fetch/index.d.ts +11 -15
- package/dist/adapters/fetch/index.mjs +8 -13
- package/dist/adapters/standard/index.d.mts +8 -147
- package/dist/adapters/standard/index.d.ts +8 -147
- package/dist/adapters/standard/index.mjs +2 -2
- package/dist/index.d.mts +23 -20
- package/dist/index.d.ts +23 -20
- package/dist/index.mjs +35 -33
- package/dist/plugins/index.d.mts +144 -0
- package/dist/plugins/index.d.ts +144 -0
- package/dist/plugins/index.mjs +291 -0
- package/dist/shared/client.8TjrVhkC.d.ts +91 -0
- package/dist/shared/client.BjJBZryq.d.mts +91 -0
- package/dist/shared/client.C9U9n1f3.d.ts +46 -0
- package/dist/shared/client.CRWEpqLB.mjs +175 -0
- package/dist/shared/client.CipPQkhk.d.mts +29 -0
- package/dist/shared/client.CipPQkhk.d.ts +29 -0
- package/dist/shared/{client.Ch8Gzz9n.mjs → client.DpICn1BD.mjs} +174 -140
- package/dist/shared/client.FXep-a3a.d.mts +46 -0
- package/package.json +10 -5
- package/dist/shared/client.D_CzLDyB.d.mts +0 -42
- package/dist/shared/client.D_CzLDyB.d.ts +0 -42
- package/dist/shared/client.DcaJQZfy.mjs +0 -265
@@ -0,0 +1,29 @@
|
|
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
|
+
interface ClientOptions<T extends ClientContext> {
|
7
|
+
signal?: AbortSignal;
|
8
|
+
lastEventId?: string | undefined;
|
9
|
+
context: T;
|
10
|
+
}
|
11
|
+
type FriendlyClientOptions<T extends ClientContext> = Omit<ClientOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
12
|
+
context?: T;
|
13
|
+
} : {
|
14
|
+
context: T;
|
15
|
+
});
|
16
|
+
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>];
|
17
|
+
type ClientPromiseResult<TOutput, TError> = PromiseWithError<TOutput, TError>;
|
18
|
+
interface Client<TClientContext extends ClientContext, TInput, TOutput, TError> {
|
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
|
+
interface ClientLink<TClientContext extends ClientContext> {
|
26
|
+
call: (path: readonly string[], input: unknown, options: ClientOptions<TClientContext>) => Promise<unknown>;
|
27
|
+
}
|
28
|
+
|
29
|
+
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 };
|
@@ -0,0 +1,29 @@
|
|
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
|
+
interface ClientOptions<T extends ClientContext> {
|
7
|
+
signal?: AbortSignal;
|
8
|
+
lastEventId?: string | undefined;
|
9
|
+
context: T;
|
10
|
+
}
|
11
|
+
type FriendlyClientOptions<T extends ClientContext> = Omit<ClientOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
12
|
+
context?: T;
|
13
|
+
} : {
|
14
|
+
context: T;
|
15
|
+
});
|
16
|
+
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>];
|
17
|
+
type ClientPromiseResult<TOutput, TError> = PromiseWithError<TOutput, TError>;
|
18
|
+
interface Client<TClientContext extends ClientContext, TInput, TOutput, TError> {
|
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
|
+
interface ClientLink<TClientContext extends ClientContext> {
|
26
|
+
call: (path: readonly string[], input: unknown, options: ClientOptions<TClientContext>) => Promise<unknown>;
|
27
|
+
}
|
28
|
+
|
29
|
+
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,106 +1,116 @@
|
|
1
|
-
import { intercept,
|
2
|
-
import {
|
3
|
-
import {
|
1
|
+
import { toArray, intercept, isObject, value, isAsyncIteratorObject, stringifyJSON } from '@orpc/shared';
|
2
|
+
import { mergeStandardHeaders, ErrorEvent } from '@orpc/standard-server';
|
3
|
+
import { C as COMMON_ORPC_ERROR_DEFS, b as isORPCErrorStatus, c as isORPCErrorJson, d as createORPCErrorFromJson, O as ORPCError, m as mapEventIterator, t as toORPCError } from './client.CRWEpqLB.mjs';
|
4
4
|
|
5
|
-
class
|
5
|
+
class CompositeStandardLinkPlugin {
|
6
|
+
plugins;
|
7
|
+
constructor(plugins = []) {
|
8
|
+
this.plugins = [...plugins].sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
9
|
+
}
|
10
|
+
init(options) {
|
11
|
+
for (const plugin of this.plugins) {
|
12
|
+
plugin.init?.(options);
|
13
|
+
}
|
14
|
+
}
|
6
15
|
}
|
16
|
+
|
7
17
|
class StandardLink {
|
8
|
-
constructor(codec, sender, options) {
|
18
|
+
constructor(codec, sender, options = {}) {
|
9
19
|
this.codec = codec;
|
10
20
|
this.sender = sender;
|
11
|
-
|
12
|
-
|
13
|
-
this.
|
14
|
-
this.
|
15
|
-
this.clientInterceptors = options.clientInterceptors ?? [];
|
21
|
+
const plugin = new CompositeStandardLinkPlugin(options.plugins);
|
22
|
+
plugin.init(options);
|
23
|
+
this.interceptors = toArray(options.interceptors);
|
24
|
+
this.clientInterceptors = toArray(options.clientInterceptors);
|
16
25
|
}
|
17
|
-
eventIteratorMaxRetries;
|
18
|
-
eventIteratorRetryDelay;
|
19
|
-
eventIteratorShouldRetry;
|
20
26
|
interceptors;
|
21
27
|
clientInterceptors;
|
22
28
|
call(path, input, options) {
|
23
|
-
return intercept(this.interceptors, { path, input
|
29
|
+
return intercept(this.interceptors, { ...options, path, input }, async ({ path: path2, input: input2, ...options2 }) => {
|
24
30
|
const output = await this.#call(path2, input2, options2);
|
25
|
-
|
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);
|
31
|
+
return output;
|
46
32
|
});
|
47
33
|
}
|
48
34
|
async #call(path, input, options) {
|
49
35
|
const request = await this.codec.encode(path, input, options);
|
50
36
|
const response = await intercept(
|
51
37
|
this.clientInterceptors,
|
52
|
-
{ request },
|
53
|
-
({ request: request2 }) => this.sender.call(request2,
|
38
|
+
{ ...options, input, path, request },
|
39
|
+
({ input: input2, path: path2, request: request2, ...options2 }) => this.sender.call(request2, options2, path2, input2)
|
54
40
|
);
|
55
41
|
const output = await this.codec.decode(response, options, path, input);
|
56
42
|
return output;
|
57
43
|
}
|
58
44
|
}
|
59
45
|
|
60
|
-
|
46
|
+
const STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES = {
|
47
|
+
BIGINT: 0,
|
48
|
+
DATE: 1,
|
49
|
+
NAN: 2,
|
50
|
+
UNDEFINED: 3,
|
51
|
+
URL: 4,
|
52
|
+
REGEXP: 5,
|
53
|
+
SET: 6,
|
54
|
+
MAP: 7
|
55
|
+
};
|
56
|
+
class StandardRPCJsonSerializer {
|
57
|
+
customSerializers;
|
58
|
+
constructor(options = {}) {
|
59
|
+
this.customSerializers = options.customJsonSerializers ?? [];
|
60
|
+
if (this.customSerializers.length !== new Set(this.customSerializers.map((custom) => custom.type)).size) {
|
61
|
+
throw new Error("Custom serializer type must be unique.");
|
62
|
+
}
|
63
|
+
}
|
61
64
|
serialize(data, segments = [], meta = [], maps = [], blobs = []) {
|
65
|
+
for (const custom of this.customSerializers) {
|
66
|
+
if (custom.condition(data)) {
|
67
|
+
const result = this.serialize(custom.serialize(data), segments, meta, maps, blobs);
|
68
|
+
meta.push([custom.type, ...segments]);
|
69
|
+
return result;
|
70
|
+
}
|
71
|
+
}
|
62
72
|
if (data instanceof Blob) {
|
63
73
|
maps.push(segments);
|
64
74
|
blobs.push(data);
|
65
75
|
return [data, meta, maps, blobs];
|
66
76
|
}
|
67
77
|
if (typeof data === "bigint") {
|
68
|
-
meta.push([
|
78
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.BIGINT, ...segments]);
|
69
79
|
return [data.toString(), meta, maps, blobs];
|
70
80
|
}
|
71
81
|
if (data instanceof Date) {
|
72
|
-
meta.push([
|
82
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.DATE, ...segments]);
|
73
83
|
if (Number.isNaN(data.getTime())) {
|
74
84
|
return [null, meta, maps, blobs];
|
75
85
|
}
|
76
86
|
return [data.toISOString(), meta, maps, blobs];
|
77
87
|
}
|
78
88
|
if (Number.isNaN(data)) {
|
79
|
-
meta.push([
|
89
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.NAN, ...segments]);
|
80
90
|
return [null, meta, maps, blobs];
|
81
91
|
}
|
82
92
|
if (data instanceof URL) {
|
83
|
-
meta.push([
|
93
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.URL, ...segments]);
|
84
94
|
return [data.toString(), meta, maps, blobs];
|
85
95
|
}
|
86
96
|
if (data instanceof RegExp) {
|
87
|
-
meta.push([
|
97
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.REGEXP, ...segments]);
|
88
98
|
return [data.toString(), meta, maps, blobs];
|
89
99
|
}
|
90
100
|
if (data instanceof Set) {
|
91
101
|
const result = this.serialize(Array.from(data), segments, meta, maps, blobs);
|
92
|
-
meta.push([
|
102
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.SET, ...segments]);
|
93
103
|
return result;
|
94
104
|
}
|
95
105
|
if (data instanceof Map) {
|
96
106
|
const result = this.serialize(Array.from(data.entries()), segments, meta, maps, blobs);
|
97
|
-
meta.push([
|
107
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.MAP, ...segments]);
|
98
108
|
return result;
|
99
109
|
}
|
100
110
|
if (Array.isArray(data)) {
|
101
111
|
const json = data.map((v, i) => {
|
102
112
|
if (v === void 0) {
|
103
|
-
meta.push([
|
113
|
+
meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.UNDEFINED, ...segments, i]);
|
104
114
|
return v;
|
105
115
|
}
|
106
116
|
return this.serialize(v, [...segments, i], meta, maps, blobs)[0];
|
@@ -110,6 +120,9 @@ class RPCJsonSerializer {
|
|
110
120
|
if (isObject(data)) {
|
111
121
|
const json = {};
|
112
122
|
for (const k in data) {
|
123
|
+
if (k === "toJSON" && typeof data[k] === "function") {
|
124
|
+
continue;
|
125
|
+
}
|
113
126
|
json[k] = this.serialize(data[k], [...segments, k], meta, maps, blobs)[0];
|
114
127
|
}
|
115
128
|
return [json, meta, maps, blobs];
|
@@ -129,38 +142,45 @@ class RPCJsonSerializer {
|
|
129
142
|
currentRef[preSegment] = getBlob(i);
|
130
143
|
});
|
131
144
|
}
|
132
|
-
for (const
|
145
|
+
for (const item of meta) {
|
146
|
+
const type = item[0];
|
133
147
|
let currentRef = ref;
|
134
148
|
let preSegment = "data";
|
135
|
-
|
149
|
+
for (let i = 1; i < item.length; i++) {
|
136
150
|
currentRef = currentRef[preSegment];
|
137
|
-
preSegment =
|
138
|
-
}
|
151
|
+
preSegment = item[i];
|
152
|
+
}
|
153
|
+
for (const custom of this.customSerializers) {
|
154
|
+
if (custom.type === type) {
|
155
|
+
currentRef[preSegment] = custom.deserialize(currentRef[preSegment]);
|
156
|
+
break;
|
157
|
+
}
|
158
|
+
}
|
139
159
|
switch (type) {
|
140
|
-
case
|
160
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.BIGINT:
|
141
161
|
currentRef[preSegment] = BigInt(currentRef[preSegment]);
|
142
162
|
break;
|
143
|
-
case
|
163
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.DATE:
|
144
164
|
currentRef[preSegment] = new Date(currentRef[preSegment] ?? "Invalid Date");
|
145
165
|
break;
|
146
|
-
case
|
166
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.NAN:
|
147
167
|
currentRef[preSegment] = Number.NaN;
|
148
168
|
break;
|
149
|
-
case
|
169
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.UNDEFINED:
|
150
170
|
currentRef[preSegment] = void 0;
|
151
171
|
break;
|
152
|
-
case
|
172
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.URL:
|
153
173
|
currentRef[preSegment] = new URL(currentRef[preSegment]);
|
154
174
|
break;
|
155
|
-
case
|
175
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.REGEXP: {
|
156
176
|
const [, pattern, flags] = currentRef[preSegment].match(/^\/(.*)\/([a-z]*)$/);
|
157
177
|
currentRef[preSegment] = new RegExp(pattern, flags);
|
158
178
|
break;
|
159
179
|
}
|
160
|
-
case
|
180
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.SET:
|
161
181
|
currentRef[preSegment] = new Set(currentRef[preSegment]);
|
162
182
|
break;
|
163
|
-
case
|
183
|
+
case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.MAP:
|
164
184
|
currentRef[preSegment] = new Map(currentRef[preSegment]);
|
165
185
|
break;
|
166
186
|
}
|
@@ -169,8 +189,93 @@ class RPCJsonSerializer {
|
|
169
189
|
}
|
170
190
|
}
|
171
191
|
|
172
|
-
|
173
|
-
|
192
|
+
function toHttpPath(path) {
|
193
|
+
return `/${path.map(encodeURIComponent).join("/")}`;
|
194
|
+
}
|
195
|
+
function getMalformedResponseErrorCode(status) {
|
196
|
+
return Object.entries(COMMON_ORPC_ERROR_DEFS).find(([, def]) => def.status === status)?.[0] ?? "MALFORMED_ORPC_ERROR_RESPONSE";
|
197
|
+
}
|
198
|
+
|
199
|
+
class StandardRPCLinkCodec {
|
200
|
+
constructor(serializer, options) {
|
201
|
+
this.serializer = serializer;
|
202
|
+
this.baseUrl = options.url;
|
203
|
+
this.maxUrlLength = options.maxUrlLength ?? 2083;
|
204
|
+
this.fallbackMethod = options.fallbackMethod ?? "POST";
|
205
|
+
this.expectedMethod = options.method ?? this.fallbackMethod;
|
206
|
+
this.headers = options.headers ?? {};
|
207
|
+
}
|
208
|
+
baseUrl;
|
209
|
+
maxUrlLength;
|
210
|
+
fallbackMethod;
|
211
|
+
expectedMethod;
|
212
|
+
headers;
|
213
|
+
async encode(path, input, options) {
|
214
|
+
const expectedMethod = await value(this.expectedMethod, options, path, input);
|
215
|
+
let headers = await value(this.headers, options, path, input);
|
216
|
+
const baseUrl = await value(this.baseUrl, options, path, input);
|
217
|
+
const url = new URL(baseUrl);
|
218
|
+
url.pathname = `${url.pathname.replace(/\/$/, "")}${toHttpPath(path)}`;
|
219
|
+
if (options.lastEventId !== void 0) {
|
220
|
+
headers = mergeStandardHeaders(headers, { "last-event-id": options.lastEventId });
|
221
|
+
}
|
222
|
+
const serialized = this.serializer.serialize(input);
|
223
|
+
if (expectedMethod === "GET" && !(serialized instanceof FormData) && !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 = !isORPCErrorStatus(response.status);
|
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 (isORPCErrorJson(deserialized)) {
|
266
|
+
throw createORPCErrorFromJson(deserialized);
|
267
|
+
}
|
268
|
+
throw new ORPCError(getMalformedResponseErrorCode(response.status), {
|
269
|
+
status: response.status,
|
270
|
+
data: { ...response, body: deserialized }
|
271
|
+
});
|
272
|
+
}
|
273
|
+
return deserialized;
|
274
|
+
}
|
275
|
+
}
|
276
|
+
|
277
|
+
class StandardRPCSerializer {
|
278
|
+
constructor(jsonSerializer) {
|
174
279
|
this.jsonSerializer = jsonSerializer;
|
175
280
|
}
|
176
281
|
serialize(data) {
|
@@ -188,9 +293,6 @@ class RPCSerializer {
|
|
188
293
|
return this.#serialize(data, true);
|
189
294
|
}
|
190
295
|
#serialize(data, enableFormData) {
|
191
|
-
if (data === void 0 || data instanceof Blob) {
|
192
|
-
return data;
|
193
|
-
}
|
194
296
|
const [json, meta_, maps, blobs] = this.jsonSerializer.serialize(data);
|
195
297
|
const meta = meta_.length === 0 ? void 0 : meta_;
|
196
298
|
if (!enableFormData || blobs.length === 0) {
|
@@ -215,8 +317,8 @@ class RPCSerializer {
|
|
215
317
|
return e;
|
216
318
|
}
|
217
319
|
const deserialized = this.#deserialize(e.data);
|
218
|
-
if (
|
219
|
-
return
|
320
|
+
if (isORPCErrorJson(deserialized)) {
|
321
|
+
return createORPCErrorFromJson(deserialized, { cause: e });
|
220
322
|
}
|
221
323
|
return new ErrorEvent({
|
222
324
|
data: deserialized,
|
@@ -228,9 +330,6 @@ class RPCSerializer {
|
|
228
330
|
return this.#deserialize(data);
|
229
331
|
}
|
230
332
|
#deserialize(data) {
|
231
|
-
if (data === void 0 || data instanceof Blob) {
|
232
|
-
return data;
|
233
|
-
}
|
234
333
|
if (!(data instanceof FormData)) {
|
235
334
|
return this.jsonSerializer.deserialize(data.json, data.meta ?? []);
|
236
335
|
}
|
@@ -244,78 +343,13 @@ class RPCSerializer {
|
|
244
343
|
}
|
245
344
|
}
|
246
345
|
|
247
|
-
class
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
rpcSerializer;
|
254
|
-
constructor(options) {
|
255
|
-
this.baseUrl = options.url;
|
256
|
-
this.maxUrlLength = options.maxUrlLength ?? 2083;
|
257
|
-
this.fallbackMethod = options.fallbackMethod ?? "POST";
|
258
|
-
this.expectedMethod = options.method ?? this.fallbackMethod;
|
259
|
-
this.headers = options.headers ?? {};
|
260
|
-
this.rpcSerializer = options.rpcSerializer ?? new RPCSerializer();
|
261
|
-
}
|
262
|
-
async encode(path, input, options) {
|
263
|
-
const expectedMethod = await value(this.expectedMethod, options, path, input);
|
264
|
-
const headers = await value(this.headers, options, path, input);
|
265
|
-
const baseUrl = await value(this.baseUrl, options, path, input);
|
266
|
-
const url = new URL(`${trim(baseUrl.toString(), "/")}/${path.map(encodeURIComponent).join("/")}`);
|
267
|
-
const serialized = this.rpcSerializer.serialize(input);
|
268
|
-
if (expectedMethod === "GET" && !(serialized instanceof FormData) && !(serialized instanceof Blob) && !isAsyncIteratorObject(serialized)) {
|
269
|
-
const maxUrlLength = await value(this.maxUrlLength, options, path, input);
|
270
|
-
const getUrl = new URL(url);
|
271
|
-
getUrl.searchParams.append("data", stringifyJSON(serialized) ?? "");
|
272
|
-
if (getUrl.toString().length <= maxUrlLength) {
|
273
|
-
return {
|
274
|
-
body: void 0,
|
275
|
-
method: expectedMethod,
|
276
|
-
headers,
|
277
|
-
url: getUrl,
|
278
|
-
signal: options.signal
|
279
|
-
};
|
280
|
-
}
|
281
|
-
}
|
282
|
-
return {
|
283
|
-
url,
|
284
|
-
method: expectedMethod === "GET" ? this.fallbackMethod : expectedMethod,
|
285
|
-
headers,
|
286
|
-
body: serialized,
|
287
|
-
signal: options.signal
|
288
|
-
};
|
289
|
-
}
|
290
|
-
async decode(response) {
|
291
|
-
const isOk = response.status >= 200 && response.status < 300;
|
292
|
-
const deserialized = await (async () => {
|
293
|
-
let isBodyOk = false;
|
294
|
-
try {
|
295
|
-
const body = await response.body();
|
296
|
-
isBodyOk = true;
|
297
|
-
return this.rpcSerializer.deserialize(body);
|
298
|
-
} catch (error) {
|
299
|
-
if (!isBodyOk) {
|
300
|
-
throw new Error("Cannot parse response body, please check the response body and content-type.", {
|
301
|
-
cause: error
|
302
|
-
});
|
303
|
-
}
|
304
|
-
throw new Error("Invalid RPC response format.", {
|
305
|
-
cause: error
|
306
|
-
});
|
307
|
-
}
|
308
|
-
})();
|
309
|
-
if (!isOk) {
|
310
|
-
if (ORPCError.isValidJSON(deserialized)) {
|
311
|
-
throw ORPCError.fromJSON(deserialized);
|
312
|
-
}
|
313
|
-
throw new Error("Invalid RPC error response format.", {
|
314
|
-
cause: deserialized
|
315
|
-
});
|
316
|
-
}
|
317
|
-
return deserialized;
|
346
|
+
class StandardRPCLink extends StandardLink {
|
347
|
+
constructor(linkClient, options) {
|
348
|
+
const jsonSerializer = new StandardRPCJsonSerializer(options);
|
349
|
+
const serializer = new StandardRPCSerializer(jsonSerializer);
|
350
|
+
const linkCodec = new StandardRPCLinkCodec(serializer, options);
|
351
|
+
super(linkCodec, linkClient, options);
|
318
352
|
}
|
319
353
|
}
|
320
354
|
|
321
|
-
export {
|
355
|
+
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, getMalformedResponseErrorCode as g, toHttpPath as t };
|
@@ -0,0 +1,46 @@
|
|
1
|
+
import { Interceptor, ThrowableError } from '@orpc/shared';
|
2
|
+
import { StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
|
3
|
+
import { a as ClientContext, b as ClientOptions, C as ClientLink } from './client.CipPQkhk.mjs';
|
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
|
+
}
|
14
|
+
|
15
|
+
interface StandardLinkCodec<T extends ClientContext> {
|
16
|
+
encode(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<StandardRequest>;
|
17
|
+
decode(response: StandardLazyResponse, options: ClientOptions<T>, path: readonly string[], input: unknown): Promise<unknown>;
|
18
|
+
}
|
19
|
+
interface StandardLinkClient<T extends ClientContext> {
|
20
|
+
call(request: StandardRequest, options: ClientOptions<T>, path: readonly string[], input: unknown): Promise<StandardLazyResponse>;
|
21
|
+
}
|
22
|
+
|
23
|
+
interface StandardLinkInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
|
24
|
+
path: readonly string[];
|
25
|
+
input: unknown;
|
26
|
+
}
|
27
|
+
interface StandardLinkClientInterceptorOptions<T extends ClientContext> extends StandardLinkInterceptorOptions<T> {
|
28
|
+
request: StandardRequest;
|
29
|
+
}
|
30
|
+
interface StandardLinkOptions<T extends ClientContext> {
|
31
|
+
interceptors?: Interceptor<StandardLinkInterceptorOptions<T>, unknown, ThrowableError>[];
|
32
|
+
clientInterceptors?: Interceptor<StandardLinkClientInterceptorOptions<T>, StandardLazyResponse, ThrowableError>[];
|
33
|
+
plugins?: StandardLinkPlugin<T>[];
|
34
|
+
}
|
35
|
+
declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
|
36
|
+
#private;
|
37
|
+
readonly codec: StandardLinkCodec<T>;
|
38
|
+
readonly sender: StandardLinkClient<T>;
|
39
|
+
private readonly interceptors;
|
40
|
+
private readonly clientInterceptors;
|
41
|
+
constructor(codec: StandardLinkCodec<T>, sender: StandardLinkClient<T>, options?: StandardLinkOptions<T>);
|
42
|
+
call(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<unknown>;
|
43
|
+
}
|
44
|
+
|
45
|
+
export { CompositeStandardLinkPlugin as C, StandardLink as d };
|
46
|
+
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.e0f01a5",
|
5
5
|
"license": "MIT",
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
7
7
|
"repository": {
|
@@ -19,6 +19,11 @@
|
|
19
19
|
"import": "./dist/index.mjs",
|
20
20
|
"default": "./dist/index.mjs"
|
21
21
|
},
|
22
|
+
"./plugins": {
|
23
|
+
"types": "./dist/plugins/index.d.mts",
|
24
|
+
"import": "./dist/plugins/index.mjs",
|
25
|
+
"default": "./dist/plugins/index.mjs"
|
26
|
+
},
|
22
27
|
"./standard": {
|
23
28
|
"types": "./dist/adapters/standard/index.d.mts",
|
24
29
|
"import": "./dist/adapters/standard/index.mjs",
|
@@ -34,12 +39,12 @@
|
|
34
39
|
"dist"
|
35
40
|
],
|
36
41
|
"dependencies": {
|
37
|
-
"@orpc/shared": "0.0.0-next.
|
38
|
-
"@orpc/standard-server": "0.0.0-next.
|
39
|
-
"@orpc/standard-server-fetch": "0.0.0-next.
|
42
|
+
"@orpc/shared": "0.0.0-next.e0f01a5",
|
43
|
+
"@orpc/standard-server": "0.0.0-next.e0f01a5",
|
44
|
+
"@orpc/standard-server-fetch": "0.0.0-next.e0f01a5"
|
40
45
|
},
|
41
46
|
"devDependencies": {
|
42
|
-
"zod": "^3.24.
|
47
|
+
"zod": "^3.24.2"
|
43
48
|
},
|
44
49
|
"scripts": {
|
45
50
|
"build": "unbuild",
|
@@ -1,42 +0,0 @@
|
|
1
|
-
type ClientContext = Record<string, any>;
|
2
|
-
type ClientOptions<TClientContext extends ClientContext> = {
|
3
|
-
signal?: AbortSignal;
|
4
|
-
lastEventId?: string | undefined;
|
5
|
-
} & (Record<never, never> extends TClientContext ? {
|
6
|
-
context?: TClientContext;
|
7
|
-
} : {
|
8
|
-
context: TClientContext;
|
9
|
-
});
|
10
|
-
type ClientRest<TClientContext extends ClientContext, TInput> = Record<never, never> extends TClientContext ? undefined extends TInput ? [input?: TInput, options?: ClientOptions<TClientContext>] : [input: TInput, options?: ClientOptions<TClientContext>] : [input: TInput, options: ClientOptions<TClientContext>];
|
11
|
-
type ClientPromiseResult<TOutput, TError extends Error> = Promise<TOutput> & {
|
12
|
-
__error?: {
|
13
|
-
type: TError;
|
14
|
-
};
|
15
|
-
};
|
16
|
-
interface Client<TClientContext extends ClientContext, TInput, TOutput, TError extends Error> {
|
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 ClientOptionsOut<TClientContext extends ClientContext> = ClientOptions<TClientContext> & {
|
24
|
-
context: TClientContext;
|
25
|
-
};
|
26
|
-
interface ClientLink<TClientContext extends ClientContext> {
|
27
|
-
call: (path: readonly string[], input: unknown, options: ClientOptionsOut<TClientContext>) => Promise<unknown>;
|
28
|
-
}
|
29
|
-
|
30
|
-
declare function mapEventIterator<TYield, TReturn, TNext, TMap = TYield | TReturn>(iterator: AsyncIterator<TYield, TReturn, TNext>, maps: {
|
31
|
-
value: (value: NoInfer<TYield | TReturn>, done: boolean | undefined) => Promise<TMap>;
|
32
|
-
error: (error: unknown) => Promise<unknown>;
|
33
|
-
}): AsyncGenerator<TMap, TMap, TNext>;
|
34
|
-
interface EventIteratorReconnectOptions {
|
35
|
-
lastRetry: number | undefined;
|
36
|
-
lastEventId: string | undefined;
|
37
|
-
retryTimes: number;
|
38
|
-
error: unknown;
|
39
|
-
}
|
40
|
-
declare function createAutoRetryEventIterator<TYield, TReturn>(initial: AsyncIterator<TYield, TReturn, void>, reconnect: (options: EventIteratorReconnectOptions) => Promise<AsyncIterator<TYield, TReturn, void> | null>, initialLastEventId: string | undefined): AsyncGenerator<TYield, TReturn, void>;
|
41
|
-
|
42
|
-
export { type ClientContext as C, type EventIteratorReconnectOptions as E, type InferClientContext as I, type NestedClient as N, type ClientOptionsOut as a, type ClientLink as b, type ClientPromiseResult as c, createAutoRetryEventIterator as d, type ClientOptions as e, type ClientRest as f, type Client as g, mapEventIterator as m };
|
@@ -1,42 +0,0 @@
|
|
1
|
-
type ClientContext = Record<string, any>;
|
2
|
-
type ClientOptions<TClientContext extends ClientContext> = {
|
3
|
-
signal?: AbortSignal;
|
4
|
-
lastEventId?: string | undefined;
|
5
|
-
} & (Record<never, never> extends TClientContext ? {
|
6
|
-
context?: TClientContext;
|
7
|
-
} : {
|
8
|
-
context: TClientContext;
|
9
|
-
});
|
10
|
-
type ClientRest<TClientContext extends ClientContext, TInput> = Record<never, never> extends TClientContext ? undefined extends TInput ? [input?: TInput, options?: ClientOptions<TClientContext>] : [input: TInput, options?: ClientOptions<TClientContext>] : [input: TInput, options: ClientOptions<TClientContext>];
|
11
|
-
type ClientPromiseResult<TOutput, TError extends Error> = Promise<TOutput> & {
|
12
|
-
__error?: {
|
13
|
-
type: TError;
|
14
|
-
};
|
15
|
-
};
|
16
|
-
interface Client<TClientContext extends ClientContext, TInput, TOutput, TError extends Error> {
|
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 ClientOptionsOut<TClientContext extends ClientContext> = ClientOptions<TClientContext> & {
|
24
|
-
context: TClientContext;
|
25
|
-
};
|
26
|
-
interface ClientLink<TClientContext extends ClientContext> {
|
27
|
-
call: (path: readonly string[], input: unknown, options: ClientOptionsOut<TClientContext>) => Promise<unknown>;
|
28
|
-
}
|
29
|
-
|
30
|
-
declare function mapEventIterator<TYield, TReturn, TNext, TMap = TYield | TReturn>(iterator: AsyncIterator<TYield, TReturn, TNext>, maps: {
|
31
|
-
value: (value: NoInfer<TYield | TReturn>, done: boolean | undefined) => Promise<TMap>;
|
32
|
-
error: (error: unknown) => Promise<unknown>;
|
33
|
-
}): AsyncGenerator<TMap, TMap, TNext>;
|
34
|
-
interface EventIteratorReconnectOptions {
|
35
|
-
lastRetry: number | undefined;
|
36
|
-
lastEventId: string | undefined;
|
37
|
-
retryTimes: number;
|
38
|
-
error: unknown;
|
39
|
-
}
|
40
|
-
declare function createAutoRetryEventIterator<TYield, TReturn>(initial: AsyncIterator<TYield, TReturn, void>, reconnect: (options: EventIteratorReconnectOptions) => Promise<AsyncIterator<TYield, TReturn, void> | null>, initialLastEventId: string | undefined): AsyncGenerator<TYield, TReturn, void>;
|
41
|
-
|
42
|
-
export { type ClientContext as C, type EventIteratorReconnectOptions as E, type InferClientContext as I, type NestedClient as N, type ClientOptionsOut as a, type ClientLink as b, type ClientPromiseResult as c, createAutoRetryEventIterator as d, type ClientOptions as e, type ClientRest as f, type Client as g, mapEventIterator as m };
|