@orpc/client 0.0.0-next.904b0c2 → 0.0.0-next.90a7c1e
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 +18 -22
- package/dist/adapters/fetch/index.d.mts +12 -5
- package/dist/adapters/fetch/index.d.ts +12 -5
- package/dist/adapters/fetch/index.mjs +4 -7
- package/dist/adapters/message-port/index.d.mts +59 -0
- package/dist/adapters/message-port/index.d.ts +59 -0
- package/dist/adapters/message-port/index.mjs +71 -0
- package/dist/adapters/standard/index.d.mts +5 -4
- package/dist/adapters/standard/index.d.ts +5 -4
- package/dist/adapters/standard/index.mjs +2 -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 +44 -0
- package/dist/index.d.mts +60 -27
- package/dist/index.d.ts +60 -27
- package/dist/index.mjs +30 -13
- package/dist/plugins/index.d.mts +162 -21
- package/dist/plugins/index.d.ts +162 -21
- package/dist/plugins/index.mjs +306 -43
- package/dist/shared/{client.87WXDX8t.d.mts → client.BOYsZIRq.d.mts} +13 -16
- package/dist/shared/{client.87WXDX8t.d.ts → client.BOYsZIRq.d.ts} +13 -16
- package/dist/shared/{client.jKEwIsRd.mjs → client.BngOL3Ai.mjs} +46 -48
- package/dist/shared/{client.D77bw0hs.mjs → client.BvXDSBF5.mjs} +38 -16
- package/dist/shared/{client.BaocqKnn.d.ts → client.C4VxIexA.d.mts} +20 -13
- package/dist/shared/{client.D9lmRwGB.d.mts → client.CXXEPIbK.d.ts} +20 -13
- package/dist/shared/{client.CZFIVTQj.d.ts → client.WCinBImJ.d.ts} +13 -25
- package/dist/shared/{client.BC0T26HA.d.mts → client.aTp4sII-.d.mts} +13 -25
- package/package.json +16 -5
package/dist/plugins/index.mjs
CHANGED
|
@@ -1,5 +1,249 @@
|
|
|
1
|
-
import { isAsyncIteratorObject, value } from '@orpc/shared';
|
|
2
|
-
import {
|
|
1
|
+
import { isAsyncIteratorObject, defer, value, splitInHalf, toArray, stringifyJSON } from '@orpc/shared';
|
|
2
|
+
import { toBatchRequest, parseBatchResponse, toBatchAbortSignal } from '@orpc/standard-server/batch';
|
|
3
|
+
import { replicateStandardLazyResponse, getEventMeta } from '@orpc/standard-server';
|
|
4
|
+
|
|
5
|
+
class BatchLinkPlugin {
|
|
6
|
+
groups;
|
|
7
|
+
maxSize;
|
|
8
|
+
batchUrl;
|
|
9
|
+
maxUrlLength;
|
|
10
|
+
batchHeaders;
|
|
11
|
+
mapRequestItem;
|
|
12
|
+
exclude;
|
|
13
|
+
mode;
|
|
14
|
+
pending;
|
|
15
|
+
order = 5e6;
|
|
16
|
+
constructor(options) {
|
|
17
|
+
this.groups = options.groups;
|
|
18
|
+
this.pending = /* @__PURE__ */ new Map();
|
|
19
|
+
this.maxSize = options.maxSize ?? 10;
|
|
20
|
+
this.maxUrlLength = options.maxUrlLength ?? 2083;
|
|
21
|
+
this.mode = options.mode ?? "streaming";
|
|
22
|
+
this.batchUrl = options.url ?? (([options2]) => `${options2.request.url.origin}${options2.request.url.pathname}/__batch__`);
|
|
23
|
+
this.batchHeaders = options.headers ?? (([options2, ...rest]) => {
|
|
24
|
+
const headers = {};
|
|
25
|
+
for (const [key, value2] of Object.entries(options2.request.headers)) {
|
|
26
|
+
if (rest.every((item) => item.request.headers[key] === value2)) {
|
|
27
|
+
headers[key] = value2;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return headers;
|
|
31
|
+
});
|
|
32
|
+
this.mapRequestItem = options.mapRequestItem ?? (({ request, batchHeaders }) => {
|
|
33
|
+
const headers = {};
|
|
34
|
+
for (const [key, value2] of Object.entries(request.headers)) {
|
|
35
|
+
if (batchHeaders[key] !== value2) {
|
|
36
|
+
headers[key] = value2;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
method: request.method,
|
|
41
|
+
url: request.url,
|
|
42
|
+
headers,
|
|
43
|
+
body: request.body,
|
|
44
|
+
signal: request.signal
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
this.exclude = options.exclude ?? (() => false);
|
|
48
|
+
}
|
|
49
|
+
init(options) {
|
|
50
|
+
options.clientInterceptors ??= [];
|
|
51
|
+
options.clientInterceptors.push((options2) => {
|
|
52
|
+
if (options2.request.headers["x-orpc-batch"] !== "1") {
|
|
53
|
+
return options2.next();
|
|
54
|
+
}
|
|
55
|
+
return options2.next({
|
|
56
|
+
...options2,
|
|
57
|
+
request: {
|
|
58
|
+
...options2.request,
|
|
59
|
+
headers: {
|
|
60
|
+
...options2.request.headers,
|
|
61
|
+
"x-orpc-batch": void 0
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
options.clientInterceptors.push((options2) => {
|
|
67
|
+
if (this.exclude(options2) || options2.request.body instanceof Blob || options2.request.body instanceof FormData || isAsyncIteratorObject(options2.request.body)) {
|
|
68
|
+
return options2.next();
|
|
69
|
+
}
|
|
70
|
+
const group = this.groups.find((group2) => group2.condition(options2));
|
|
71
|
+
if (!group) {
|
|
72
|
+
return options2.next();
|
|
73
|
+
}
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
this.#enqueueRequest(group, options2, resolve, reject);
|
|
76
|
+
defer(() => this.#processPendingBatches());
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
#enqueueRequest(group, options, resolve, reject) {
|
|
81
|
+
const items = this.pending.get(group);
|
|
82
|
+
if (items) {
|
|
83
|
+
items.push([options, resolve, reject]);
|
|
84
|
+
} else {
|
|
85
|
+
this.pending.set(group, [[options, resolve, reject]]);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async #processPendingBatches() {
|
|
89
|
+
const pending = this.pending;
|
|
90
|
+
this.pending = /* @__PURE__ */ new Map();
|
|
91
|
+
for (const [group, items] of pending) {
|
|
92
|
+
const getItems = items.filter(([options]) => options.request.method === "GET");
|
|
93
|
+
const restItems = items.filter(([options]) => options.request.method !== "GET");
|
|
94
|
+
this.#executeBatch("GET", group, getItems);
|
|
95
|
+
this.#executeBatch("POST", group, restItems);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async #executeBatch(method, group, groupItems) {
|
|
99
|
+
if (!groupItems.length) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const batchItems = groupItems;
|
|
103
|
+
if (batchItems.length === 1) {
|
|
104
|
+
batchItems[0][0].next().then(batchItems[0][1]).catch(batchItems[0][2]);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
try {
|
|
108
|
+
const options = batchItems.map(([options2]) => options2);
|
|
109
|
+
const maxSize = await value(this.maxSize, options);
|
|
110
|
+
if (batchItems.length > maxSize) {
|
|
111
|
+
const [first, second] = splitInHalf(batchItems);
|
|
112
|
+
this.#executeBatch(method, group, first);
|
|
113
|
+
this.#executeBatch(method, group, second);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const batchUrl = new URL(await value(this.batchUrl, options));
|
|
117
|
+
const batchHeaders = await value(this.batchHeaders, options);
|
|
118
|
+
const mappedItems = batchItems.map(([options2]) => this.mapRequestItem({ ...options2, batchUrl, batchHeaders }));
|
|
119
|
+
const batchRequest = toBatchRequest({
|
|
120
|
+
method,
|
|
121
|
+
url: batchUrl,
|
|
122
|
+
headers: batchHeaders,
|
|
123
|
+
requests: mappedItems
|
|
124
|
+
});
|
|
125
|
+
const maxUrlLength = await value(this.maxUrlLength, options);
|
|
126
|
+
if (batchRequest.url.toString().length > maxUrlLength) {
|
|
127
|
+
const [first, second] = splitInHalf(batchItems);
|
|
128
|
+
this.#executeBatch(method, group, first);
|
|
129
|
+
this.#executeBatch(method, group, second);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const mode = value(this.mode, options);
|
|
133
|
+
const lazyResponse = await options[0].next({
|
|
134
|
+
request: { ...batchRequest, headers: { ...batchRequest.headers, "x-orpc-batch": mode } },
|
|
135
|
+
signal: batchRequest.signal,
|
|
136
|
+
context: group.context,
|
|
137
|
+
input: group.input,
|
|
138
|
+
path: toArray(group.path)
|
|
139
|
+
});
|
|
140
|
+
const parsed = parseBatchResponse({ ...lazyResponse, body: await lazyResponse.body() });
|
|
141
|
+
for await (const item of parsed) {
|
|
142
|
+
batchItems[item.index]?.[1]({ ...item, body: () => Promise.resolve(item.body) });
|
|
143
|
+
}
|
|
144
|
+
throw new Error("Something went wrong make batch response not contains enough responses. This can be a bug please report it.");
|
|
145
|
+
} catch (error) {
|
|
146
|
+
for (const [, , reject] of batchItems) {
|
|
147
|
+
reject(error);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
class DedupeRequestsPlugin {
|
|
154
|
+
#groups;
|
|
155
|
+
#filter;
|
|
156
|
+
order = 4e6;
|
|
157
|
+
// make sure execute before batch plugin
|
|
158
|
+
#queue = /* @__PURE__ */ new Map();
|
|
159
|
+
constructor(options) {
|
|
160
|
+
this.#groups = options.groups;
|
|
161
|
+
this.#filter = options.filter ?? (({ request }) => request.method === "GET");
|
|
162
|
+
}
|
|
163
|
+
init(options) {
|
|
164
|
+
options.clientInterceptors ??= [];
|
|
165
|
+
options.clientInterceptors.push((options2) => {
|
|
166
|
+
if (options2.request.body instanceof Blob || options2.request.body instanceof FormData || options2.request.body instanceof URLSearchParams || isAsyncIteratorObject(options2.request.body) || !this.#filter(options2)) {
|
|
167
|
+
return options2.next();
|
|
168
|
+
}
|
|
169
|
+
const group = this.#groups.find((group2) => group2.condition(options2));
|
|
170
|
+
if (!group) {
|
|
171
|
+
return options2.next();
|
|
172
|
+
}
|
|
173
|
+
return new Promise((resolve, reject) => {
|
|
174
|
+
this.#enqueue(group, options2, resolve, reject);
|
|
175
|
+
defer(() => this.#dequeue());
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
#enqueue(group, options, resolve, reject) {
|
|
180
|
+
let queue = this.#queue.get(group);
|
|
181
|
+
if (!queue) {
|
|
182
|
+
this.#queue.set(group, queue = []);
|
|
183
|
+
}
|
|
184
|
+
const matched = queue.find((item) => {
|
|
185
|
+
const requestString1 = stringifyJSON({
|
|
186
|
+
body: item.options.request.body,
|
|
187
|
+
headers: item.options.request.headers,
|
|
188
|
+
method: item.options.request.method,
|
|
189
|
+
url: item.options.request.url
|
|
190
|
+
});
|
|
191
|
+
const requestString2 = stringifyJSON({
|
|
192
|
+
body: options.request.body,
|
|
193
|
+
headers: options.request.headers,
|
|
194
|
+
method: options.request.method,
|
|
195
|
+
url: options.request.url
|
|
196
|
+
});
|
|
197
|
+
return requestString1 === requestString2;
|
|
198
|
+
});
|
|
199
|
+
if (matched) {
|
|
200
|
+
matched.signals.push(options.request.signal);
|
|
201
|
+
matched.resolves.push(resolve);
|
|
202
|
+
matched.rejects.push(reject);
|
|
203
|
+
} else {
|
|
204
|
+
queue.push({
|
|
205
|
+
options,
|
|
206
|
+
signals: [options.request.signal],
|
|
207
|
+
resolves: [resolve],
|
|
208
|
+
rejects: [reject]
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
async #dequeue() {
|
|
213
|
+
const promises = [];
|
|
214
|
+
for (const [group, items] of this.#queue) {
|
|
215
|
+
for (const { options, signals, resolves, rejects } of items) {
|
|
216
|
+
promises.push(
|
|
217
|
+
this.#execute(group, options, signals, resolves, rejects)
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
this.#queue.clear();
|
|
222
|
+
await Promise.all(promises);
|
|
223
|
+
}
|
|
224
|
+
async #execute(group, options, signals, resolves, rejects) {
|
|
225
|
+
try {
|
|
226
|
+
const dedupedRequest = {
|
|
227
|
+
...options.request,
|
|
228
|
+
signal: toBatchAbortSignal(signals)
|
|
229
|
+
};
|
|
230
|
+
const response = await options.next({
|
|
231
|
+
...options,
|
|
232
|
+
request: dedupedRequest,
|
|
233
|
+
signal: dedupedRequest.signal,
|
|
234
|
+
context: group.context
|
|
235
|
+
});
|
|
236
|
+
const replicatedResponses = replicateStandardLazyResponse(response, resolves.length);
|
|
237
|
+
for (const resolve of resolves) {
|
|
238
|
+
resolve(replicatedResponses.shift());
|
|
239
|
+
}
|
|
240
|
+
} catch (error) {
|
|
241
|
+
for (const reject of rejects) {
|
|
242
|
+
reject(error);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
3
247
|
|
|
4
248
|
class ClientRetryPluginInvalidEventIteratorRetryResponse extends Error {
|
|
5
249
|
}
|
|
@@ -17,71 +261,57 @@ class ClientRetryPlugin {
|
|
|
17
261
|
init(options) {
|
|
18
262
|
options.interceptors ??= [];
|
|
19
263
|
options.interceptors.push(async (interceptorOptions) => {
|
|
20
|
-
const maxAttempts =
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
264
|
+
const maxAttempts = await value(
|
|
265
|
+
interceptorOptions.context.retry ?? this.defaultRetry,
|
|
266
|
+
interceptorOptions
|
|
267
|
+
);
|
|
268
|
+
const retryDelay = interceptorOptions.context.retryDelay ?? this.defaultRetryDelay;
|
|
269
|
+
const shouldRetry = interceptorOptions.context.shouldRetry ?? this.defaultShouldRetry;
|
|
270
|
+
const onRetry = interceptorOptions.context.onRetry ?? this.defaultOnRetry;
|
|
24
271
|
if (maxAttempts <= 0) {
|
|
25
272
|
return interceptorOptions.next();
|
|
26
273
|
}
|
|
27
|
-
let lastEventId = interceptorOptions.
|
|
274
|
+
let lastEventId = interceptorOptions.lastEventId;
|
|
28
275
|
let lastEventRetry;
|
|
29
|
-
let
|
|
276
|
+
let callback;
|
|
30
277
|
let attemptIndex = 0;
|
|
31
|
-
const next = async (
|
|
32
|
-
let
|
|
278
|
+
const next = async (initialError) => {
|
|
279
|
+
let currentError = initialError;
|
|
33
280
|
while (true) {
|
|
34
|
-
const
|
|
35
|
-
if (
|
|
281
|
+
const updatedInterceptorOptions = { ...interceptorOptions, lastEventId };
|
|
282
|
+
if (currentError) {
|
|
36
283
|
if (attemptIndex >= maxAttempts) {
|
|
37
|
-
throw
|
|
284
|
+
throw currentError.error;
|
|
38
285
|
}
|
|
39
286
|
const attemptOptions = {
|
|
287
|
+
...updatedInterceptorOptions,
|
|
40
288
|
attemptIndex,
|
|
41
|
-
error:
|
|
42
|
-
lastEventId,
|
|
289
|
+
error: currentError.error,
|
|
43
290
|
lastEventRetry
|
|
44
291
|
};
|
|
45
292
|
const shouldRetryBool = await value(
|
|
46
293
|
shouldRetry,
|
|
47
|
-
attemptOptions
|
|
48
|
-
newClientOptions,
|
|
49
|
-
interceptorOptions.path,
|
|
50
|
-
interceptorOptions.input
|
|
294
|
+
attemptOptions
|
|
51
295
|
);
|
|
52
296
|
if (!shouldRetryBool) {
|
|
53
|
-
throw
|
|
297
|
+
throw currentError.error;
|
|
54
298
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
newClientOptions,
|
|
58
|
-
interceptorOptions.path,
|
|
59
|
-
interceptorOptions.input
|
|
60
|
-
);
|
|
61
|
-
const retryDelayMs = await value(
|
|
62
|
-
retryDelay,
|
|
63
|
-
attemptOptions,
|
|
64
|
-
newClientOptions,
|
|
65
|
-
interceptorOptions.path,
|
|
66
|
-
interceptorOptions.input
|
|
67
|
-
);
|
|
299
|
+
callback = onRetry?.(attemptOptions);
|
|
300
|
+
const retryDelayMs = await value(retryDelay, attemptOptions);
|
|
68
301
|
await new Promise((resolve) => setTimeout(resolve, retryDelayMs));
|
|
69
302
|
attemptIndex++;
|
|
70
303
|
}
|
|
71
304
|
try {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
options: newClientOptions
|
|
75
|
-
});
|
|
76
|
-
return output2;
|
|
305
|
+
currentError = void 0;
|
|
306
|
+
return await interceptorOptions.next(updatedInterceptorOptions);
|
|
77
307
|
} catch (error) {
|
|
78
|
-
|
|
308
|
+
currentError = { error };
|
|
309
|
+
if (updatedInterceptorOptions.signal?.aborted === true) {
|
|
79
310
|
throw error;
|
|
80
311
|
}
|
|
81
|
-
current = { error };
|
|
82
312
|
} finally {
|
|
83
|
-
|
|
84
|
-
|
|
313
|
+
callback?.(!currentError);
|
|
314
|
+
callback = void 0;
|
|
85
315
|
}
|
|
86
316
|
}
|
|
87
317
|
};
|
|
@@ -123,4 +353,37 @@ class ClientRetryPlugin {
|
|
|
123
353
|
}
|
|
124
354
|
}
|
|
125
355
|
|
|
126
|
-
|
|
356
|
+
class SimpleCsrfProtectionLinkPlugin {
|
|
357
|
+
headerName;
|
|
358
|
+
headerValue;
|
|
359
|
+
exclude;
|
|
360
|
+
constructor(options = {}) {
|
|
361
|
+
this.headerName = options.headerName ?? "x-csrf-token";
|
|
362
|
+
this.headerValue = options.headerValue ?? "orpc";
|
|
363
|
+
this.exclude = options.exclude ?? false;
|
|
364
|
+
}
|
|
365
|
+
order = 8e6;
|
|
366
|
+
init(options) {
|
|
367
|
+
options.clientInterceptors ??= [];
|
|
368
|
+
options.clientInterceptors.push(async (options2) => {
|
|
369
|
+
const excluded = await value(this.exclude, options2);
|
|
370
|
+
if (excluded) {
|
|
371
|
+
return options2.next();
|
|
372
|
+
}
|
|
373
|
+
const headerName = await value(this.headerName, options2);
|
|
374
|
+
const headerValue = await value(this.headerValue, options2);
|
|
375
|
+
return options2.next({
|
|
376
|
+
...options2,
|
|
377
|
+
request: {
|
|
378
|
+
...options2.request,
|
|
379
|
+
headers: {
|
|
380
|
+
...options2.request.headers,
|
|
381
|
+
[headerName]: headerValue
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
export { BatchLinkPlugin, ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse, DedupeRequestsPlugin, SimpleCsrfProtectionLinkPlugin };
|
|
@@ -1,32 +1,29 @@
|
|
|
1
|
+
import { PromiseWithError } from '@orpc/shared';
|
|
2
|
+
|
|
1
3
|
type HTTPPath = `/${string}`;
|
|
2
|
-
type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
3
|
-
type ClientContext = Record<
|
|
4
|
-
|
|
4
|
+
type HTTPMethod = 'HEAD' | 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
5
|
+
type ClientContext = Record<PropertyKey, any>;
|
|
6
|
+
interface ClientOptions<T extends ClientContext> {
|
|
5
7
|
signal?: AbortSignal;
|
|
6
8
|
lastEventId?: string | undefined;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
context: T;
|
|
10
|
+
}
|
|
11
|
+
type FriendlyClientOptions<T extends ClientContext> = Omit<ClientOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
|
12
|
+
context?: T;
|
|
9
13
|
} : {
|
|
10
|
-
context:
|
|
14
|
+
context: T;
|
|
11
15
|
});
|
|
12
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>];
|
|
13
|
-
type ClientPromiseResult<TOutput, TError
|
|
14
|
-
|
|
15
|
-
type: TError;
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
|
-
interface Client<TClientContext extends ClientContext, TInput, TOutput, TError extends Error> {
|
|
17
|
+
type ClientPromiseResult<TOutput, TError> = PromiseWithError<TOutput, TError>;
|
|
18
|
+
interface Client<TClientContext extends ClientContext, TInput, TOutput, TError> {
|
|
19
19
|
(...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
|
|
20
20
|
}
|
|
21
21
|
type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
|
|
22
22
|
[k: string]: NestedClient<TClientContext>;
|
|
23
23
|
};
|
|
24
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
25
|
interface ClientLink<TClientContext extends ClientContext> {
|
|
29
26
|
call: (path: readonly string[], input: unknown, options: ClientOptions<TClientContext>) => Promise<unknown>;
|
|
30
27
|
}
|
|
31
28
|
|
|
32
|
-
export type { ClientLink as C, FriendlyClientOptions as F, HTTPPath as H, InferClientContext as I, NestedClient as N,
|
|
29
|
+
export type { ClientLink as C, FriendlyClientOptions as F, HTTPPath as H, InferClientContext as I, NestedClient as N, ClientPromiseResult as a, ClientContext as b, ClientOptions as c, Client as d, ClientRest as e, HTTPMethod as f };
|
|
@@ -1,32 +1,29 @@
|
|
|
1
|
+
import { PromiseWithError } from '@orpc/shared';
|
|
2
|
+
|
|
1
3
|
type HTTPPath = `/${string}`;
|
|
2
|
-
type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
3
|
-
type ClientContext = Record<
|
|
4
|
-
|
|
4
|
+
type HTTPMethod = 'HEAD' | 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
5
|
+
type ClientContext = Record<PropertyKey, any>;
|
|
6
|
+
interface ClientOptions<T extends ClientContext> {
|
|
5
7
|
signal?: AbortSignal;
|
|
6
8
|
lastEventId?: string | undefined;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
context: T;
|
|
10
|
+
}
|
|
11
|
+
type FriendlyClientOptions<T extends ClientContext> = Omit<ClientOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
|
12
|
+
context?: T;
|
|
9
13
|
} : {
|
|
10
|
-
context:
|
|
14
|
+
context: T;
|
|
11
15
|
});
|
|
12
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>];
|
|
13
|
-
type ClientPromiseResult<TOutput, TError
|
|
14
|
-
|
|
15
|
-
type: TError;
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
|
-
interface Client<TClientContext extends ClientContext, TInput, TOutput, TError extends Error> {
|
|
17
|
+
type ClientPromiseResult<TOutput, TError> = PromiseWithError<TOutput, TError>;
|
|
18
|
+
interface Client<TClientContext extends ClientContext, TInput, TOutput, TError> {
|
|
19
19
|
(...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
|
|
20
20
|
}
|
|
21
21
|
type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
|
|
22
22
|
[k: string]: NestedClient<TClientContext>;
|
|
23
23
|
};
|
|
24
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
25
|
interface ClientLink<TClientContext extends ClientContext> {
|
|
29
26
|
call: (path: readonly string[], input: unknown, options: ClientOptions<TClientContext>) => Promise<unknown>;
|
|
30
27
|
}
|
|
31
28
|
|
|
32
|
-
export type { ClientLink as C, FriendlyClientOptions as F, HTTPPath as H, InferClientContext as I, NestedClient as N,
|
|
29
|
+
export type { ClientLink as C, FriendlyClientOptions as F, HTTPPath as H, InferClientContext as I, NestedClient as N, ClientPromiseResult as a, ClientContext as b, ClientOptions as c, Client as d, ClientRest as e, HTTPMethod as f };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isObject, isTypescriptObject } from '@orpc/shared';
|
|
1
|
+
import { resolveMaybeOptionalOptions, isObject, AsyncIteratorClass, isTypescriptObject } from '@orpc/shared';
|
|
2
2
|
import { getEventMeta, withEventMeta } from '@orpc/standard-server';
|
|
3
3
|
|
|
4
4
|
const COMMON_ORPC_ERROR_DEFS = {
|
|
@@ -90,16 +90,17 @@ class ORPCError extends Error {
|
|
|
90
90
|
code;
|
|
91
91
|
status;
|
|
92
92
|
data;
|
|
93
|
-
constructor(code, ...
|
|
94
|
-
|
|
93
|
+
constructor(code, ...rest) {
|
|
94
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
95
|
+
if (options.status !== void 0 && !isORPCErrorStatus(options.status)) {
|
|
95
96
|
throw new Error("[ORPCError] Invalid error status code.");
|
|
96
97
|
}
|
|
97
|
-
const message = fallbackORPCErrorMessage(code, options
|
|
98
|
+
const message = fallbackORPCErrorMessage(code, options.message);
|
|
98
99
|
super(message, options);
|
|
99
100
|
this.code = code;
|
|
100
|
-
this.status = fallbackORPCErrorStatus(code, options
|
|
101
|
-
this.defined = options
|
|
102
|
-
this.data = options
|
|
101
|
+
this.status = fallbackORPCErrorStatus(code, options.status);
|
|
102
|
+
this.defined = options.defined ?? false;
|
|
103
|
+
this.data = options.data;
|
|
103
104
|
}
|
|
104
105
|
toJSON() {
|
|
105
106
|
return {
|
|
@@ -110,22 +111,6 @@ class ORPCError extends Error {
|
|
|
110
111
|
data: this.data
|
|
111
112
|
};
|
|
112
113
|
}
|
|
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
114
|
}
|
|
130
115
|
function isDefinedError(error) {
|
|
131
116
|
return error instanceof ORPCError && error.defined;
|
|
@@ -139,37 +124,50 @@ function toORPCError(error) {
|
|
|
139
124
|
function isORPCErrorStatus(status) {
|
|
140
125
|
return status < 200 || status >= 400;
|
|
141
126
|
}
|
|
127
|
+
function isORPCErrorJson(json) {
|
|
128
|
+
if (!isObject(json)) {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
const validKeys = ["defined", "code", "status", "message", "data"];
|
|
132
|
+
if (Object.keys(json).some((k) => !validKeys.includes(k))) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
return "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "status" in json && typeof json.status === "number" && isORPCErrorStatus(json.status) && "message" in json && typeof json.message === "string";
|
|
136
|
+
}
|
|
137
|
+
function createORPCErrorFromJson(json, options = {}) {
|
|
138
|
+
return new ORPCError(json.code, {
|
|
139
|
+
...options,
|
|
140
|
+
...json
|
|
141
|
+
});
|
|
142
|
+
}
|
|
142
143
|
|
|
143
144
|
function mapEventIterator(iterator, maps) {
|
|
144
|
-
return async
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
145
|
+
return new AsyncIteratorClass(async () => {
|
|
146
|
+
const { done, value } = await (async () => {
|
|
147
|
+
try {
|
|
148
|
+
return await iterator.next();
|
|
149
|
+
} catch (error) {
|
|
150
|
+
let mappedError = await maps.error(error);
|
|
151
|
+
if (mappedError !== error) {
|
|
152
|
+
const meta = getEventMeta(error);
|
|
153
|
+
if (meta && isTypescriptObject(mappedError)) {
|
|
154
|
+
mappedError = withEventMeta(mappedError, meta);
|
|
153
155
|
}
|
|
154
156
|
}
|
|
155
|
-
|
|
156
|
-
return mappedValue;
|
|
157
|
-
}
|
|
158
|
-
yield mappedValue;
|
|
157
|
+
throw mappedError;
|
|
159
158
|
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
}
|
|
159
|
+
})();
|
|
160
|
+
let mappedValue = await maps.value(value, done);
|
|
161
|
+
if (mappedValue !== value) {
|
|
162
|
+
const meta = getEventMeta(value);
|
|
163
|
+
if (meta && isTypescriptObject(mappedValue)) {
|
|
164
|
+
mappedValue = withEventMeta(mappedValue, meta);
|
|
167
165
|
}
|
|
168
|
-
throw mappedError;
|
|
169
|
-
} finally {
|
|
170
|
-
await iterator.return?.();
|
|
171
166
|
}
|
|
172
|
-
|
|
167
|
+
return { done, value: mappedValue };
|
|
168
|
+
}, async () => {
|
|
169
|
+
await iterator.return?.();
|
|
170
|
+
});
|
|
173
171
|
}
|
|
174
172
|
|
|
175
|
-
export { COMMON_ORPC_ERROR_DEFS as C, ORPCError as O, fallbackORPCErrorMessage as a, isORPCErrorStatus as b, fallbackORPCErrorStatus as f, isDefinedError as i, mapEventIterator as m, toORPCError as t };
|
|
173
|
+
export { COMMON_ORPC_ERROR_DEFS as C, ORPCError as O, fallbackORPCErrorMessage as a, isORPCErrorStatus as b, isORPCErrorJson as c, createORPCErrorFromJson as d, fallbackORPCErrorStatus as f, isDefinedError as i, mapEventIterator as m, toORPCError as t };
|