@orpc/client 1.3.0 → 1.4.1

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.
@@ -1,5 +1,6 @@
1
1
  import { Value, Promisable } from '@orpc/shared';
2
2
  import { StandardHeaders, StandardRequest } from '@orpc/standard-server';
3
+ import { BatchResponseMode } from '@orpc/standard-server/batch';
3
4
  import { S as StandardLinkClientInterceptorOptions, a as StandardLinkPlugin, b as StandardLinkOptions, c as StandardLinkInterceptorOptions } from '../shared/client.BMoG_EdN.mjs';
4
5
  import { a as ClientContext } from '../shared/client.4TS_0JaO.mjs';
5
6
 
@@ -17,6 +18,12 @@ interface BatchLinkPluginOptions<T extends ClientContext> {
17
18
  * @default 10
18
19
  */
19
20
  maxSize?: Value<Promisable<number>, [readonly [StandardLinkClientInterceptorOptions<T>, ...StandardLinkClientInterceptorOptions<T>[]]]>;
21
+ /**
22
+ * The batch response mode.
23
+ *
24
+ * @default 'streaming'
25
+ */
26
+ mode?: Value<BatchResponseMode, [readonly [StandardLinkClientInterceptorOptions<T>, ...StandardLinkClientInterceptorOptions<T>[]]]>;
20
27
  /**
21
28
  * Defines the URL to use for the batch request.
22
29
  *
@@ -52,10 +59,10 @@ interface BatchLinkPluginOptions<T extends ClientContext> {
52
59
  exclude?: (options: StandardLinkClientInterceptorOptions<T>) => boolean;
53
60
  }
54
61
  /**
55
- * The Batch Request/Response Plugin allows you to combine multiple requests and responses into a single batch,
62
+ * The Batch Requests Plugin allows you to combine multiple requests and responses into a single batch,
56
63
  * reducing the overhead of sending each one separately.
57
64
  *
58
- * @see {@link https://orpc.unnoq.com/docs/plugins/batch-request-response Batch Request/Response Plugin Docs}
65
+ * @see {@link https://orpc.unnoq.com/docs/plugins/batch-requests Batch Requests Plugin Docs}
59
66
  */
60
67
  declare class BatchLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
61
68
  #private;
@@ -66,12 +73,45 @@ declare class BatchLinkPlugin<T extends ClientContext> implements StandardLinkPl
66
73
  private readonly batchHeaders;
67
74
  private readonly mapRequestItem;
68
75
  private readonly exclude;
76
+ private readonly mode;
69
77
  private pending;
70
78
  order: number;
71
79
  constructor(options: NoInfer<BatchLinkPluginOptions<T>>);
72
80
  init(options: StandardLinkOptions<T>): void;
73
81
  }
74
82
 
83
+ interface DedupeRequestsPluginGroup<T extends ClientContext> {
84
+ condition(options: StandardLinkClientInterceptorOptions<T>): boolean;
85
+ /**
86
+ * The context used for the rest of the request lifecycle.
87
+ */
88
+ context: T;
89
+ }
90
+ interface DedupeRequestsPluginOptions<T extends ClientContext> {
91
+ /**
92
+ * To enable deduplication, a request must match at least one defined group.
93
+ * Requests that fall into the same group are considered for deduplication together.
94
+ */
95
+ groups: readonly [DedupeRequestsPluginGroup<T>, ...DedupeRequestsPluginGroup<T>[]];
96
+ /**
97
+ * Filters requests to dedupe
98
+ *
99
+ * @default (({ request }) => request.method === 'GET')
100
+ */
101
+ filter?: (options: StandardLinkClientInterceptorOptions<T>) => boolean;
102
+ }
103
+ /**
104
+ * Prevents duplicate requests by deduplicating similar ones to reduce server load.
105
+ *
106
+ * @see {@link https://orpc.unnoq.com/docs/plugins/dedupe-requests Dedupe Requests Plugin}
107
+ */
108
+ declare class DedupeRequestsPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
109
+ #private;
110
+ order: number;
111
+ constructor(options: NoInfer<DedupeRequestsPluginOptions<T>>);
112
+ init(options: StandardLinkOptions<T>): void;
113
+ }
114
+
75
115
  interface ClientRetryPluginAttemptOptions<T extends ClientContext> extends StandardLinkInterceptorOptions<T> {
76
116
  lastEventRetry: number | undefined;
77
117
  attemptIndex: number;
@@ -158,5 +198,5 @@ declare class SimpleCsrfProtectionLinkPlugin<T extends ClientContext> implements
158
198
  init(options: StandardLinkOptions<T>): void;
159
199
  }
160
200
 
161
- export { BatchLinkPlugin, ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse, SimpleCsrfProtectionLinkPlugin };
162
- export type { BatchLinkPluginGroup, BatchLinkPluginOptions, ClientRetryPluginAttemptOptions, ClientRetryPluginContext, ClientRetryPluginOptions, SimpleCsrfProtectionLinkPluginOptions };
201
+ export { BatchLinkPlugin, ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse, DedupeRequestsPlugin, SimpleCsrfProtectionLinkPlugin };
202
+ export type { BatchLinkPluginGroup, BatchLinkPluginOptions, ClientRetryPluginAttemptOptions, ClientRetryPluginContext, ClientRetryPluginOptions, DedupeRequestsPluginGroup, DedupeRequestsPluginOptions, SimpleCsrfProtectionLinkPluginOptions };
@@ -1,5 +1,6 @@
1
1
  import { Value, Promisable } from '@orpc/shared';
2
2
  import { StandardHeaders, StandardRequest } from '@orpc/standard-server';
3
+ import { BatchResponseMode } from '@orpc/standard-server/batch';
3
4
  import { S as StandardLinkClientInterceptorOptions, a as StandardLinkPlugin, b as StandardLinkOptions, c as StandardLinkInterceptorOptions } from '../shared/client.C0KbSWlC.js';
4
5
  import { a as ClientContext } from '../shared/client.4TS_0JaO.js';
5
6
 
@@ -17,6 +18,12 @@ interface BatchLinkPluginOptions<T extends ClientContext> {
17
18
  * @default 10
18
19
  */
19
20
  maxSize?: Value<Promisable<number>, [readonly [StandardLinkClientInterceptorOptions<T>, ...StandardLinkClientInterceptorOptions<T>[]]]>;
21
+ /**
22
+ * The batch response mode.
23
+ *
24
+ * @default 'streaming'
25
+ */
26
+ mode?: Value<BatchResponseMode, [readonly [StandardLinkClientInterceptorOptions<T>, ...StandardLinkClientInterceptorOptions<T>[]]]>;
20
27
  /**
21
28
  * Defines the URL to use for the batch request.
22
29
  *
@@ -52,10 +59,10 @@ interface BatchLinkPluginOptions<T extends ClientContext> {
52
59
  exclude?: (options: StandardLinkClientInterceptorOptions<T>) => boolean;
53
60
  }
54
61
  /**
55
- * The Batch Request/Response Plugin allows you to combine multiple requests and responses into a single batch,
62
+ * The Batch Requests Plugin allows you to combine multiple requests and responses into a single batch,
56
63
  * reducing the overhead of sending each one separately.
57
64
  *
58
- * @see {@link https://orpc.unnoq.com/docs/plugins/batch-request-response Batch Request/Response Plugin Docs}
65
+ * @see {@link https://orpc.unnoq.com/docs/plugins/batch-requests Batch Requests Plugin Docs}
59
66
  */
60
67
  declare class BatchLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
61
68
  #private;
@@ -66,12 +73,45 @@ declare class BatchLinkPlugin<T extends ClientContext> implements StandardLinkPl
66
73
  private readonly batchHeaders;
67
74
  private readonly mapRequestItem;
68
75
  private readonly exclude;
76
+ private readonly mode;
69
77
  private pending;
70
78
  order: number;
71
79
  constructor(options: NoInfer<BatchLinkPluginOptions<T>>);
72
80
  init(options: StandardLinkOptions<T>): void;
73
81
  }
74
82
 
83
+ interface DedupeRequestsPluginGroup<T extends ClientContext> {
84
+ condition(options: StandardLinkClientInterceptorOptions<T>): boolean;
85
+ /**
86
+ * The context used for the rest of the request lifecycle.
87
+ */
88
+ context: T;
89
+ }
90
+ interface DedupeRequestsPluginOptions<T extends ClientContext> {
91
+ /**
92
+ * To enable deduplication, a request must match at least one defined group.
93
+ * Requests that fall into the same group are considered for deduplication together.
94
+ */
95
+ groups: readonly [DedupeRequestsPluginGroup<T>, ...DedupeRequestsPluginGroup<T>[]];
96
+ /**
97
+ * Filters requests to dedupe
98
+ *
99
+ * @default (({ request }) => request.method === 'GET')
100
+ */
101
+ filter?: (options: StandardLinkClientInterceptorOptions<T>) => boolean;
102
+ }
103
+ /**
104
+ * Prevents duplicate requests by deduplicating similar ones to reduce server load.
105
+ *
106
+ * @see {@link https://orpc.unnoq.com/docs/plugins/dedupe-requests Dedupe Requests Plugin}
107
+ */
108
+ declare class DedupeRequestsPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
109
+ #private;
110
+ order: number;
111
+ constructor(options: NoInfer<DedupeRequestsPluginOptions<T>>);
112
+ init(options: StandardLinkOptions<T>): void;
113
+ }
114
+
75
115
  interface ClientRetryPluginAttemptOptions<T extends ClientContext> extends StandardLinkInterceptorOptions<T> {
76
116
  lastEventRetry: number | undefined;
77
117
  attemptIndex: number;
@@ -158,5 +198,5 @@ declare class SimpleCsrfProtectionLinkPlugin<T extends ClientContext> implements
158
198
  init(options: StandardLinkOptions<T>): void;
159
199
  }
160
200
 
161
- export { BatchLinkPlugin, ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse, SimpleCsrfProtectionLinkPlugin };
162
- export type { BatchLinkPluginGroup, BatchLinkPluginOptions, ClientRetryPluginAttemptOptions, ClientRetryPluginContext, ClientRetryPluginOptions, SimpleCsrfProtectionLinkPluginOptions };
201
+ export { BatchLinkPlugin, ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse, DedupeRequestsPlugin, SimpleCsrfProtectionLinkPlugin };
202
+ export type { BatchLinkPluginGroup, BatchLinkPluginOptions, ClientRetryPluginAttemptOptions, ClientRetryPluginContext, ClientRetryPluginOptions, DedupeRequestsPluginGroup, DedupeRequestsPluginOptions, SimpleCsrfProtectionLinkPluginOptions };
@@ -1,6 +1,6 @@
1
- import { isAsyncIteratorObject, value, splitInHalf, toArray } from '@orpc/shared';
2
- import { toBatchRequest, parseBatchResponse } from '@orpc/standard-server/batch';
3
- import { getEventMeta } from '@orpc/standard-server';
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
4
 
5
5
  class BatchLinkPlugin {
6
6
  groups;
@@ -10,6 +10,7 @@ class BatchLinkPlugin {
10
10
  batchHeaders;
11
11
  mapRequestItem;
12
12
  exclude;
13
+ mode;
13
14
  pending;
14
15
  order = 5e6;
15
16
  constructor(options) {
@@ -17,6 +18,7 @@ class BatchLinkPlugin {
17
18
  this.pending = /* @__PURE__ */ new Map();
18
19
  this.maxSize = options.maxSize ?? 10;
19
20
  this.maxUrlLength = options.maxUrlLength ?? 2083;
21
+ this.mode = options.mode ?? "streaming";
20
22
  this.batchUrl = options.url ?? (([options2]) => `${options2.request.url.origin}${options2.request.url.pathname}/__batch__`);
21
23
  this.batchHeaders = options.headers ?? (([options2, ...rest]) => {
22
24
  const headers = {};
@@ -71,7 +73,7 @@ class BatchLinkPlugin {
71
73
  }
72
74
  return new Promise((resolve, reject) => {
73
75
  this.#enqueueRequest(group, options2, resolve, reject);
74
- setTimeout(() => this.#processPendingBatches());
76
+ defer(() => this.#processPendingBatches());
75
77
  });
76
78
  });
77
79
  }
@@ -127,8 +129,9 @@ class BatchLinkPlugin {
127
129
  this.#executeBatch(method, group, second);
128
130
  return;
129
131
  }
132
+ const mode = value(this.mode, options);
130
133
  const lazyResponse = await options[0].next({
131
- request: { ...batchRequest, headers: { ...batchRequest.headers, "x-orpc-batch": "1" } },
134
+ request: { ...batchRequest, headers: { ...batchRequest.headers, "x-orpc-batch": mode } },
132
135
  signal: batchRequest.signal,
133
136
  context: group.context,
134
137
  input: group.input,
@@ -147,6 +150,101 @@ class BatchLinkPlugin {
147
150
  }
148
151
  }
149
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
+ }
247
+
150
248
  class ClientRetryPluginInvalidEventIteratorRetryResponse extends Error {
151
249
  }
152
250
  class ClientRetryPlugin {
@@ -288,4 +386,4 @@ class SimpleCsrfProtectionLinkPlugin {
288
386
  }
289
387
  }
290
388
 
291
- export { BatchLinkPlugin, ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse, SimpleCsrfProtectionLinkPlugin };
389
+ export { BatchLinkPlugin, ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse, DedupeRequestsPlugin, SimpleCsrfProtectionLinkPlugin };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/client",
3
3
  "type": "module",
4
- "version": "1.3.0",
4
+ "version": "1.4.1",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -49,13 +49,13 @@
49
49
  "dist"
50
50
  ],
51
51
  "dependencies": {
52
- "@orpc/shared": "1.3.0",
53
- "@orpc/standard-server": "1.3.0",
54
- "@orpc/standard-server-fetch": "1.3.0",
55
- "@orpc/standard-server-peer": "1.3.0"
52
+ "@orpc/shared": "1.4.1",
53
+ "@orpc/standard-server": "1.4.1",
54
+ "@orpc/standard-server-fetch": "1.4.1",
55
+ "@orpc/standard-server-peer": "1.4.1"
56
56
  },
57
57
  "devDependencies": {
58
- "zod": "^3.25.11"
58
+ "zod": "^3.25.49"
59
59
  },
60
60
  "scripts": {
61
61
  "build": "unbuild",