@orpc/client 0.0.0-next.ce8559f → 0.0.0-next.cedbd4c

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/dist/index.d.ts CHANGED
@@ -1,14 +1,14 @@
1
- import { N as NestedClient, C as ClientLink, I as InferClientContext, a as ClientContext, b as ClientOptions, c as ClientPromiseResult, F as FriendlyClientOptions } from './shared/client.4TS_0JaO.js';
2
- export { f as Client, e as ClientRest, d as HTTPMethod, H as HTTPPath } from './shared/client.4TS_0JaO.js';
3
- import { Promisable, MaybeOptionalOptions, AsyncIteratorClass, ThrowableError } from '@orpc/shared';
4
- export { EventPublisher, EventPublisherOptions, EventPublisherSubscribeIteratorOptions, Registry, ThrowableError, onError, onFinish, onStart, onSuccess } from '@orpc/shared';
1
+ import { N as NestedClient, C as ClientLink, I as InferClientContext, a as ClientPromiseResult, b as ClientContext, F as FriendlyClientOptions, c as ClientOptions, d as Client, e as ClientRest } from './shared/client.BOYsZIRq.js';
2
+ export { f as HTTPMethod, H as HTTPPath } from './shared/client.BOYsZIRq.js';
3
+ import { MaybeOptionalOptions, ThrowableError, Promisable, AsyncIteratorClass } from '@orpc/shared';
4
+ export { AsyncIteratorClass, EventPublisher, EventPublisherOptions, EventPublisherSubscribeIteratorOptions, Registry, ThrowableError, asyncIteratorToStream as eventIteratorToStream, onError, onFinish, onStart, onSuccess, streamToAsyncIteratorClass as streamToEventIterator } from '@orpc/shared';
5
5
  export { ErrorEvent } from '@orpc/standard-server';
6
6
 
7
7
  interface createORPCClientOptions {
8
8
  /**
9
9
  * Use as base path for all procedure, useful when you only want to call a subset of the procedure.
10
10
  */
11
- path?: string[];
11
+ path?: readonly string[];
12
12
  }
13
13
  /**
14
14
  * Create a oRPC client-side client from a link.
@@ -17,18 +17,6 @@ interface createORPCClientOptions {
17
17
  */
18
18
  declare function createORPCClient<T extends NestedClient<any>>(link: ClientLink<InferClientContext<T>>, options?: createORPCClientOptions): T;
19
19
 
20
- /**
21
- * DynamicLink provides a way to dynamically resolve and delegate calls to other ClientLinks
22
- * based on the request path, input, and context.
23
- *
24
- * @see {@link https://orpc.unnoq.com/docs/client/dynamic-link Dynamic Link Docs}
25
- */
26
- declare class DynamicLink<TClientContext extends ClientContext> implements ClientLink<TClientContext> {
27
- private readonly linkResolver;
28
- constructor(linkResolver: (options: ClientOptions<TClientContext>, path: readonly string[], input: unknown) => Promisable<ClientLink<TClientContext>>);
29
- call(path: readonly string[], input: unknown, options: ClientOptions<TClientContext>): Promise<unknown>;
30
- }
31
-
32
20
  declare const COMMON_ORPC_ERROR_DEFS: {
33
21
  readonly BAD_REQUEST: {
34
22
  readonly status: 400;
@@ -125,8 +113,21 @@ declare class ORPCError<TCode extends ORPCErrorCode, TData> extends Error {
125
113
  readonly code: TCode;
126
114
  readonly status: number;
127
115
  readonly data: TData;
128
- constructor(code: TCode, ...[options]: MaybeOptionalOptions<ORPCErrorOptions<TData>>);
116
+ constructor(code: TCode, ...rest: MaybeOptionalOptions<ORPCErrorOptions<TData>>);
129
117
  toJSON(): ORPCErrorJSON<TCode, TData>;
118
+ /**
119
+ * Workaround for Next.js where different contexts use separate
120
+ * dependency graphs, causing multiple ORPCError constructors existing and breaking
121
+ * `instanceof` checks across contexts.
122
+ *
123
+ * This is particularly problematic with "Optimized SSR", where orpc-client
124
+ * executes in one context but is invoked from another. When an error is thrown
125
+ * in the execution context, `instanceof ORPCError` checks fail in the
126
+ * invocation context due to separate class constructors.
127
+ *
128
+ * @todo Remove this and related code if Next.js resolves the multiple dependency graph issue.
129
+ */
130
+ static [Symbol.hasInstance](instance: unknown): boolean;
130
131
  }
131
132
  type ORPCErrorJSON<TCode extends string, TData> = Pick<ORPCError<TCode, TData>, 'defined' | 'code' | 'status' | 'message' | 'data'>;
132
133
  declare function isDefinedError<T>(error: T): error is Extract<T, ORPCError<any, any>>;
@@ -135,11 +136,6 @@ declare function isORPCErrorStatus(status: number): boolean;
135
136
  declare function isORPCErrorJson(json: unknown): json is ORPCErrorJSON<ORPCErrorCode, unknown>;
136
137
  declare function createORPCErrorFromJson<TCode extends ORPCErrorCode, TData>(json: ORPCErrorJSON<TCode, TData>, options?: ErrorOptions): ORPCError<TCode, TData>;
137
138
 
138
- declare function mapEventIterator<TYield, TReturn, TNext, TMap = TYield | TReturn>(iterator: AsyncIterator<TYield, TReturn, TNext>, maps: {
139
- value: (value: NoInfer<TYield | TReturn>, done: boolean | undefined) => Promise<TMap>;
140
- error: (error: unknown) => Promise<unknown>;
141
- }): AsyncIteratorClass<TMap, TMap, TNext>;
142
-
143
139
  type SafeResult<TOutput, TError> = [error: null, data: TOutput, isDefined: false, isSuccess: true] & {
144
140
  error: null;
145
141
  data: TOutput;
@@ -165,5 +161,41 @@ type SafeResult<TOutput, TError> = [error: null, data: TOutput, isDefined: false
165
161
  declare function safe<TOutput, TError = ThrowableError>(promise: ClientPromiseResult<TOutput, TError>): Promise<SafeResult<TOutput, TError>>;
166
162
  declare function resolveFriendlyClientOptions<T extends ClientContext>(options: FriendlyClientOptions<T>): ClientOptions<T>;
167
163
 
168
- export { COMMON_ORPC_ERROR_DEFS, ClientContext, ClientLink, ClientOptions, ClientPromiseResult, DynamicLink, FriendlyClientOptions, InferClientContext, NestedClient, ORPCError, createORPCClient, createORPCErrorFromJson, fallbackORPCErrorMessage, fallbackORPCErrorStatus, isDefinedError, isORPCErrorJson, isORPCErrorStatus, mapEventIterator, resolveFriendlyClientOptions, safe, toORPCError };
169
- export type { CommonORPCErrorCode, ORPCErrorCode, ORPCErrorJSON, ORPCErrorOptions, SafeResult, createORPCClientOptions };
164
+ type SafeClient<T extends NestedClient<any>> = T extends Client<infer UContext, infer UInput, infer UOutput, infer UError> ? (...rest: ClientRest<UContext, UInput>) => Promise<SafeResult<UOutput, UError>> : {
165
+ [K in keyof T]: T[K] extends NestedClient<any> ? SafeClient<T[K]> : never;
166
+ };
167
+ /**
168
+ * Create a safe client that automatically wraps all procedure calls with the `safe` util.
169
+ *
170
+ * @example
171
+ * ```ts
172
+ * const safeClient = createSafeClient(client)
173
+ * const { error, data, isDefined } = await safeClient.doSomething({ id: '123' })
174
+ * ```
175
+ *
176
+ * @see {@link https://orpc.unnoq.com/docs/client/error-handling#using-createsafeclient Safe Client Docs}
177
+ */
178
+ declare function createSafeClient<T extends NestedClient<any>>(client: T): SafeClient<T>;
179
+
180
+ declare const ORPC_CLIENT_PACKAGE_NAME = "@orpc/client";
181
+ declare const ORPC_CLIENT_PACKAGE_VERSION = "0.0.0-next.cedbd4c";
182
+
183
+ /**
184
+ * DynamicLink provides a way to dynamically resolve and delegate calls to other ClientLinks
185
+ * based on the request path, input, and context.
186
+ *
187
+ * @see {@link https://orpc.unnoq.com/docs/client/dynamic-link Dynamic Link Docs}
188
+ */
189
+ declare class DynamicLink<TClientContext extends ClientContext> implements ClientLink<TClientContext> {
190
+ private readonly linkResolver;
191
+ constructor(linkResolver: (options: ClientOptions<TClientContext>, path: readonly string[], input: unknown) => Promisable<ClientLink<TClientContext>>);
192
+ call(path: readonly string[], input: unknown, options: ClientOptions<TClientContext>): Promise<unknown>;
193
+ }
194
+
195
+ declare function mapEventIterator<TYield, TReturn, TNext, TMap = TYield | TReturn>(iterator: AsyncIterator<TYield, TReturn, TNext>, maps: {
196
+ value: (value: NoInfer<TYield | TReturn>, done: boolean | undefined) => Promise<TMap>;
197
+ error: (error: unknown) => Promise<unknown>;
198
+ }): AsyncIteratorClass<TMap, TMap, TNext>;
199
+
200
+ export { COMMON_ORPC_ERROR_DEFS, Client, ClientContext, ClientLink, ClientOptions, ClientPromiseResult, ClientRest, DynamicLink, FriendlyClientOptions, InferClientContext, NestedClient, ORPCError, ORPC_CLIENT_PACKAGE_NAME, ORPC_CLIENT_PACKAGE_VERSION, createORPCClient, createORPCErrorFromJson, createSafeClient, fallbackORPCErrorMessage, fallbackORPCErrorStatus, isDefinedError, isORPCErrorJson, isORPCErrorStatus, mapEventIterator, resolveFriendlyClientOptions, safe, toORPCError };
201
+ export type { CommonORPCErrorCode, ORPCErrorCode, ORPCErrorJSON, ORPCErrorOptions, SafeClient, SafeResult, createORPCClientOptions };
package/dist/index.mjs CHANGED
@@ -1,6 +1,7 @@
1
- import { i as isDefinedError } from './shared/client.BX0_8bnM.mjs';
2
- export { C as COMMON_ORPC_ERROR_DEFS, O as ORPCError, d as createORPCErrorFromJson, a as fallbackORPCErrorMessage, f as fallbackORPCErrorStatus, c as isORPCErrorJson, b as isORPCErrorStatus, m as mapEventIterator, t as toORPCError } from './shared/client.BX0_8bnM.mjs';
3
- export { EventPublisher, onError, onFinish, onStart, onSuccess } from '@orpc/shared';
1
+ import { preventNativeAwait, isTypescriptObject } from '@orpc/shared';
2
+ export { AsyncIteratorClass, EventPublisher, asyncIteratorToStream as eventIteratorToStream, onError, onFinish, onStart, onSuccess, streamToAsyncIteratorClass as streamToEventIterator } from '@orpc/shared';
3
+ import { i as isDefinedError } from './shared/client.BuaNv0j7.mjs';
4
+ export { C as COMMON_ORPC_ERROR_DEFS, c as ORPCError, O as ORPC_CLIENT_PACKAGE_NAME, a as ORPC_CLIENT_PACKAGE_VERSION, g as createORPCErrorFromJson, b as fallbackORPCErrorMessage, f as fallbackORPCErrorStatus, e as isORPCErrorJson, d as isORPCErrorStatus, m as mapEventIterator, t as toORPCError } from './shared/client.BuaNv0j7.mjs';
4
5
  export { ErrorEvent } from '@orpc/standard-server';
5
6
 
6
7
  async function safe(promise) {
@@ -32,8 +33,8 @@ function resolveFriendlyClientOptions(options) {
32
33
  };
33
34
  }
34
35
 
35
- function createORPCClient(link, options) {
36
- const path = options?.path ?? [];
36
+ function createORPCClient(link, options = {}) {
37
+ const path = options.path ?? [];
37
38
  const procedureClient = async (...[input, options2 = {}]) => {
38
39
  return await link.call(path, input, resolveFriendlyClientOptions(options2));
39
40
  };
@@ -48,7 +49,23 @@ function createORPCClient(link, options) {
48
49
  });
49
50
  }
50
51
  });
51
- return recursive;
52
+ return preventNativeAwait(recursive);
53
+ }
54
+
55
+ function createSafeClient(client) {
56
+ const proxy = new Proxy((...args) => safe(client(...args)), {
57
+ get(_, prop, receiver) {
58
+ const value = Reflect.get(client, prop, receiver);
59
+ if (typeof prop !== "string") {
60
+ return value;
61
+ }
62
+ if (!isTypescriptObject(value)) {
63
+ return value;
64
+ }
65
+ return createSafeClient(value);
66
+ }
67
+ });
68
+ return proxy;
52
69
  }
53
70
 
54
71
  class DynamicLink {
@@ -62,4 +79,4 @@ class DynamicLink {
62
79
  }
63
80
  }
64
81
 
65
- export { DynamicLink, createORPCClient, isDefinedError, resolveFriendlyClientOptions, safe };
82
+ export { DynamicLink, createORPCClient, createSafeClient, isDefinedError, resolveFriendlyClientOptions, safe };
@@ -1,8 +1,8 @@
1
1
  import { Value, Promisable } from '@orpc/shared';
2
2
  import { StandardHeaders, StandardRequest } from '@orpc/standard-server';
3
3
  import { BatchResponseMode } from '@orpc/standard-server/batch';
4
- import { S as StandardLinkClientInterceptorOptions, a as StandardLinkPlugin, b as StandardLinkOptions, c as StandardLinkInterceptorOptions } from '../shared/client.BMoG_EdN.mjs';
5
- import { a as ClientContext } from '../shared/client.4TS_0JaO.mjs';
4
+ import { S as StandardLinkClientInterceptorOptions, a as StandardLinkPlugin, b as StandardLinkOptions, c as StandardLinkInterceptorOptions } from '../shared/client.Bwgm6dgk.mjs';
5
+ import { b as ClientContext } from '../shared/client.BOYsZIRq.mjs';
6
6
 
7
7
  interface BatchLinkPluginGroup<T extends ClientContext> {
8
8
  condition(options: StandardLinkClientInterceptorOptions<T>): boolean;
@@ -1,8 +1,8 @@
1
1
  import { Value, Promisable } from '@orpc/shared';
2
2
  import { StandardHeaders, StandardRequest } from '@orpc/standard-server';
3
3
  import { BatchResponseMode } from '@orpc/standard-server/batch';
4
- import { S as StandardLinkClientInterceptorOptions, a as StandardLinkPlugin, b as StandardLinkOptions, c as StandardLinkInterceptorOptions } from '../shared/client.C0KbSWlC.js';
5
- import { a as ClientContext } from '../shared/client.4TS_0JaO.js';
4
+ import { S as StandardLinkClientInterceptorOptions, a as StandardLinkPlugin, b as StandardLinkOptions, c as StandardLinkInterceptorOptions } from '../shared/client.BG98rYdO.js';
5
+ import { b as ClientContext } from '../shared/client.BOYsZIRq.js';
6
6
 
7
7
  interface BatchLinkPluginGroup<T extends ClientContext> {
8
8
  condition(options: StandardLinkClientInterceptorOptions<T>): boolean;
@@ -1,4 +1,4 @@
1
- import { isAsyncIteratorObject, defer, value, splitInHalf, toArray, stringifyJSON } from '@orpc/shared';
1
+ import { isAsyncIteratorObject, defer, value, splitInHalf, toArray, stringifyJSON, AsyncIteratorClass } from '@orpc/shared';
2
2
  import { toBatchRequest, parseBatchResponse, toBatchAbortSignal } from '@orpc/standard-server/batch';
3
3
  import { replicateStandardLazyResponse, getEventMeta } from '@orpc/standard-server';
4
4
 
@@ -64,7 +64,7 @@ class BatchLinkPlugin {
64
64
  });
65
65
  });
66
66
  options.clientInterceptors.push((options2) => {
67
- if (this.exclude(options2) || options2.request.body instanceof Blob || options2.request.body instanceof FormData || isAsyncIteratorObject(options2.request.body)) {
67
+ if (this.exclude(options2) || options2.request.body instanceof Blob || options2.request.body instanceof FormData || isAsyncIteratorObject(options2.request.body) || options2.request.signal?.aborted) {
68
68
  return options2.next();
69
69
  }
70
70
  const group = this.groups.find((group2) => group2.condition(options2));
@@ -130,16 +130,27 @@ class BatchLinkPlugin {
130
130
  return;
131
131
  }
132
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) });
133
+ try {
134
+ const lazyResponse = await options[0].next({
135
+ request: { ...batchRequest, headers: { ...batchRequest.headers, "x-orpc-batch": mode } },
136
+ signal: batchRequest.signal,
137
+ context: group.context,
138
+ input: group.input,
139
+ path: toArray(group.path)
140
+ });
141
+ const parsed = parseBatchResponse({ ...lazyResponse, body: await lazyResponse.body() });
142
+ for await (const item of parsed) {
143
+ batchItems[item.index]?.[1]({ ...item, body: () => Promise.resolve(item.body) });
144
+ }
145
+ } catch (err) {
146
+ if (batchRequest.signal?.aborted && batchRequest.signal.reason === err) {
147
+ for (const [{ signal }, , reject] of batchItems) {
148
+ if (signal?.aborted) {
149
+ reject(signal.reason);
150
+ }
151
+ }
152
+ }
153
+ throw err;
143
154
  }
144
155
  throw new Error("Something went wrong make batch response not contains enough responses. This can be a bug please report it.");
145
156
  } catch (error) {
@@ -306,7 +317,7 @@ class ClientRetryPlugin {
306
317
  return await interceptorOptions.next(updatedInterceptorOptions);
307
318
  } catch (error) {
308
319
  currentError = { error };
309
- if (updatedInterceptorOptions.signal?.aborted === true) {
320
+ if (updatedInterceptorOptions.signal?.aborted) {
310
321
  throw error;
311
322
  }
312
323
  } finally {
@@ -319,19 +330,17 @@ class ClientRetryPlugin {
319
330
  if (!isAsyncIteratorObject(output)) {
320
331
  return output;
321
332
  }
322
- return async function* () {
323
- let current = output;
324
- try {
333
+ let current = output;
334
+ let isIteratorAborted = false;
335
+ return new AsyncIteratorClass(
336
+ async () => {
325
337
  while (true) {
326
338
  try {
327
339
  const item = await current.next();
328
340
  const meta = getEventMeta(item.value);
329
341
  lastEventId = meta?.id ?? lastEventId;
330
342
  lastEventRetry = meta?.retry ?? lastEventRetry;
331
- if (item.done) {
332
- return item.value;
333
- }
334
- yield item.value;
343
+ return item;
335
344
  } catch (error) {
336
345
  const meta = getEventMeta(error);
337
346
  lastEventId = meta?.id ?? lastEventId;
@@ -343,12 +352,20 @@ class ClientRetryPlugin {
343
352
  );
344
353
  }
345
354
  current = maybeEventIterator;
355
+ if (isIteratorAborted) {
356
+ await current.return?.();
357
+ throw error;
358
+ }
346
359
  }
347
360
  }
348
- } finally {
349
- await current.return?.();
361
+ },
362
+ async (reason) => {
363
+ isIteratorAborted = true;
364
+ if (reason !== "next") {
365
+ await current.return?.();
366
+ }
350
367
  }
351
- }();
368
+ );
352
369
  });
353
370
  }
354
371
  }
@@ -1,6 +1,6 @@
1
1
  import { Interceptor } from '@orpc/shared';
2
2
  import { StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
3
- import { a as ClientContext, b as ClientOptions, C as ClientLink } from './client.4TS_0JaO.js';
3
+ import { b as ClientContext, c as ClientOptions, C as ClientLink } from './client.BOYsZIRq.js';
4
4
 
5
5
  interface StandardLinkPlugin<T extends ClientContext> {
6
6
  order?: number;
@@ -33,7 +33,6 @@ interface StandardLinkOptions<T extends ClientContext> {
33
33
  plugins?: StandardLinkPlugin<T>[];
34
34
  }
35
35
  declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
36
- #private;
37
36
  readonly codec: StandardLinkCodec<T>;
38
37
  readonly sender: StandardLinkClient<T>;
39
38
  private readonly interceptors;
@@ -26,4 +26,4 @@ interface ClientLink<TClientContext extends ClientContext> {
26
26
  call: (path: readonly string[], input: unknown, options: ClientOptions<TClientContext>) => Promise<unknown>;
27
27
  }
28
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 };
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 };
@@ -26,4 +26,4 @@ interface ClientLink<TClientContext extends ClientContext> {
26
26
  call: (path: readonly string[], input: unknown, options: ClientOptions<TClientContext>) => Promise<unknown>;
27
27
  }
28
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 };
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,6 +1,9 @@
1
- import { isObject, AsyncIteratorClass, isTypescriptObject } from '@orpc/shared';
1
+ import { resolveMaybeOptionalOptions, getConstructor, isObject, AsyncIteratorClass, isTypescriptObject } from '@orpc/shared';
2
2
  import { getEventMeta, withEventMeta } from '@orpc/standard-server';
3
3
 
4
+ const ORPC_CLIENT_PACKAGE_NAME = "@orpc/client";
5
+ const ORPC_CLIENT_PACKAGE_VERSION = "0.0.0-next.cedbd4c";
6
+
4
7
  const COMMON_ORPC_ERROR_DEFS = {
5
8
  BAD_REQUEST: {
6
9
  status: 400,
@@ -85,21 +88,25 @@ function fallbackORPCErrorStatus(code, status) {
85
88
  function fallbackORPCErrorMessage(code, message) {
86
89
  return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
87
90
  }
91
+ const GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL = Symbol.for(`__${ORPC_CLIENT_PACKAGE_NAME}@${ORPC_CLIENT_PACKAGE_VERSION}/error/ORPC_ERROR_CONSTRUCTORS__`);
92
+ void (globalThis[GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL] ??= /* @__PURE__ */ new WeakSet());
93
+ const globalORPCErrorConstructors = globalThis[GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL];
88
94
  class ORPCError extends Error {
89
95
  defined;
90
96
  code;
91
97
  status;
92
98
  data;
93
- constructor(code, ...[options]) {
94
- if (options?.status && !isORPCErrorStatus(options.status)) {
99
+ constructor(code, ...rest) {
100
+ const options = resolveMaybeOptionalOptions(rest);
101
+ if (options.status !== void 0 && !isORPCErrorStatus(options.status)) {
95
102
  throw new Error("[ORPCError] Invalid error status code.");
96
103
  }
97
- const message = fallbackORPCErrorMessage(code, options?.message);
104
+ const message = fallbackORPCErrorMessage(code, options.message);
98
105
  super(message, options);
99
106
  this.code = code;
100
- this.status = fallbackORPCErrorStatus(code, options?.status);
101
- this.defined = options?.defined ?? false;
102
- this.data = options?.data;
107
+ this.status = fallbackORPCErrorStatus(code, options.status);
108
+ this.defined = options.defined ?? false;
109
+ this.data = options.data;
103
110
  }
104
111
  toJSON() {
105
112
  return {
@@ -110,7 +117,29 @@ class ORPCError extends Error {
110
117
  data: this.data
111
118
  };
112
119
  }
120
+ /**
121
+ * Workaround for Next.js where different contexts use separate
122
+ * dependency graphs, causing multiple ORPCError constructors existing and breaking
123
+ * `instanceof` checks across contexts.
124
+ *
125
+ * This is particularly problematic with "Optimized SSR", where orpc-client
126
+ * executes in one context but is invoked from another. When an error is thrown
127
+ * in the execution context, `instanceof ORPCError` checks fail in the
128
+ * invocation context due to separate class constructors.
129
+ *
130
+ * @todo Remove this and related code if Next.js resolves the multiple dependency graph issue.
131
+ */
132
+ static [Symbol.hasInstance](instance) {
133
+ if (globalORPCErrorConstructors.has(this)) {
134
+ const constructor = getConstructor(instance);
135
+ if (constructor && globalORPCErrorConstructors.has(constructor)) {
136
+ return true;
137
+ }
138
+ }
139
+ return super[Symbol.hasInstance](instance);
140
+ }
113
141
  }
142
+ globalORPCErrorConstructors.add(ORPCError);
114
143
  function isDefinedError(error) {
115
144
  return error instanceof ORPCError && error.defined;
116
145
  }
@@ -141,32 +170,39 @@ function createORPCErrorFromJson(json, options = {}) {
141
170
  }
142
171
 
143
172
  function mapEventIterator(iterator, maps) {
173
+ const mapError = async (error) => {
174
+ let mappedError = await maps.error(error);
175
+ if (mappedError !== error) {
176
+ const meta = getEventMeta(error);
177
+ if (meta && isTypescriptObject(mappedError)) {
178
+ mappedError = withEventMeta(mappedError, meta);
179
+ }
180
+ }
181
+ return mappedError;
182
+ };
144
183
  return new AsyncIteratorClass(async () => {
145
- try {
146
- const { done, value } = await iterator.next();
147
- let mappedValue = await maps.value(value, done);
148
- if (mappedValue !== value) {
149
- const meta = getEventMeta(value);
150
- if (meta && isTypescriptObject(mappedValue)) {
151
- mappedValue = withEventMeta(mappedValue, meta);
152
- }
184
+ const { done, value } = await (async () => {
185
+ try {
186
+ return await iterator.next();
187
+ } catch (error) {
188
+ throw await mapError(error);
153
189
  }
154
- return { done, value: mappedValue };
155
- } catch (error) {
156
- let mappedError = await maps.error(error);
157
- if (mappedError !== error) {
158
- const meta = getEventMeta(error);
159
- if (meta && isTypescriptObject(mappedError)) {
160
- mappedError = withEventMeta(mappedError, meta);
161
- }
190
+ })();
191
+ let mappedValue = await maps.value(value, done);
192
+ if (mappedValue !== value) {
193
+ const meta = getEventMeta(value);
194
+ if (meta && isTypescriptObject(mappedValue)) {
195
+ mappedValue = withEventMeta(mappedValue, meta);
162
196
  }
163
- throw mappedError;
164
197
  }
165
- }, async (reason) => {
166
- if (reason !== "next") {
198
+ return { done, value: mappedValue };
199
+ }, async () => {
200
+ try {
167
201
  await iterator.return?.();
202
+ } catch (error) {
203
+ throw await mapError(error);
168
204
  }
169
205
  });
170
206
  }
171
207
 
172
- 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 };
208
+ export { COMMON_ORPC_ERROR_DEFS as C, ORPC_CLIENT_PACKAGE_NAME as O, ORPC_CLIENT_PACKAGE_VERSION as a, fallbackORPCErrorMessage as b, ORPCError as c, isORPCErrorStatus as d, isORPCErrorJson as e, fallbackORPCErrorStatus as f, createORPCErrorFromJson as g, isDefinedError as i, mapEventIterator as m, toORPCError as t };
@@ -1,6 +1,6 @@
1
1
  import { Interceptor } from '@orpc/shared';
2
2
  import { StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
3
- import { a as ClientContext, b as ClientOptions, C as ClientLink } from './client.4TS_0JaO.mjs';
3
+ import { b as ClientContext, c as ClientOptions, C as ClientLink } from './client.BOYsZIRq.mjs';
4
4
 
5
5
  interface StandardLinkPlugin<T extends ClientContext> {
6
6
  order?: number;
@@ -33,7 +33,6 @@ interface StandardLinkOptions<T extends ClientContext> {
33
33
  plugins?: StandardLinkPlugin<T>[];
34
34
  }
35
35
  declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
36
- #private;
37
36
  readonly codec: StandardLinkCodec<T>;
38
37
  readonly sender: StandardLinkClient<T>;
39
38
  private readonly interceptors;
@@ -1,6 +1,7 @@
1
- import { toArray, intercept, isObject, value, isAsyncIteratorObject, stringifyJSON } from '@orpc/shared';
1
+ import { toArray, runWithSpan, ORPC_NAME, isAsyncIteratorObject, asyncIteratorWithSpan, intercept, getGlobalOtelConfig, isObject, value, stringifyJSON } from '@orpc/shared';
2
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.BX0_8bnM.mjs';
3
+ import { C as COMMON_ORPC_ERROR_DEFS, d as isORPCErrorStatus, e as isORPCErrorJson, g as createORPCErrorFromJson, c as ORPCError, m as mapEventIterator, t as toORPCError } from './client.BuaNv0j7.mjs';
4
+ import { toStandardHeaders as toStandardHeaders$1 } from '@orpc/standard-server-fetch';
4
5
 
5
6
  class CompositeStandardLinkPlugin {
6
7
  plugins;
@@ -26,20 +27,52 @@ class StandardLink {
26
27
  interceptors;
27
28
  clientInterceptors;
28
29
  call(path, input, options) {
29
- return intercept(this.interceptors, { ...options, path, input }, async ({ path: path2, input: input2, ...options2 }) => {
30
- const output = await this.#call(path2, input2, options2);
31
- return output;
32
- });
33
- }
34
- async #call(path, input, options) {
35
- const request = await this.codec.encode(path, input, options);
36
- const response = await intercept(
37
- this.clientInterceptors,
38
- { ...options, input, path, request },
39
- ({ input: input2, path: path2, request: request2, ...options2 }) => this.sender.call(request2, options2, path2, input2)
30
+ return runWithSpan(
31
+ { name: `${ORPC_NAME}.${path.join("/")}`, signal: options.signal },
32
+ (span) => {
33
+ span?.setAttribute("rpc.system", ORPC_NAME);
34
+ span?.setAttribute("rpc.method", path.join("."));
35
+ if (isAsyncIteratorObject(input)) {
36
+ input = asyncIteratorWithSpan(
37
+ { name: "consume_event_iterator_input", signal: options.signal },
38
+ input
39
+ );
40
+ }
41
+ return intercept(this.interceptors, { ...options, path, input }, async ({ path: path2, input: input2, ...options2 }) => {
42
+ const otelConfig = getGlobalOtelConfig();
43
+ let otelContext;
44
+ const currentSpan = otelConfig?.trace.getActiveSpan() ?? span;
45
+ if (currentSpan && otelConfig) {
46
+ otelContext = otelConfig?.trace.setSpan(otelConfig.context.active(), currentSpan);
47
+ }
48
+ const request = await runWithSpan(
49
+ { name: "encode_request", context: otelContext },
50
+ () => this.codec.encode(path2, input2, options2)
51
+ );
52
+ const response = await intercept(
53
+ this.clientInterceptors,
54
+ { ...options2, input: input2, path: path2, request },
55
+ ({ input: input3, path: path3, request: request2, ...options3 }) => {
56
+ return runWithSpan(
57
+ { name: "send_request", signal: options3.signal, context: otelContext },
58
+ () => this.sender.call(request2, options3, path3, input3)
59
+ );
60
+ }
61
+ );
62
+ const output = await runWithSpan(
63
+ { name: "decode_response", context: otelContext },
64
+ () => this.codec.decode(response, options2, path2, input2)
65
+ );
66
+ if (isAsyncIteratorObject(output)) {
67
+ return asyncIteratorWithSpan(
68
+ { name: "consume_event_iterator_output", signal: options2.signal },
69
+ output
70
+ );
71
+ }
72
+ return output;
73
+ });
74
+ }
40
75
  );
41
- const output = await this.codec.decode(response, options, path, input);
42
- return output;
43
76
  }
44
77
  }
45
78
 
@@ -192,6 +225,12 @@ class StandardRPCJsonSerializer {
192
225
  function toHttpPath(path) {
193
226
  return `/${path.map(encodeURIComponent).join("/")}`;
194
227
  }
228
+ function toStandardHeaders(headers) {
229
+ if (typeof headers.forEach === "function") {
230
+ return toStandardHeaders$1(headers);
231
+ }
232
+ return headers;
233
+ }
195
234
  function getMalformedResponseErrorCode(status) {
196
235
  return Object.entries(COMMON_ORPC_ERROR_DEFS).find(([, def]) => def.status === status)?.[0] ?? "MALFORMED_ORPC_ERROR_RESPONSE";
197
236
  }
@@ -211,14 +250,14 @@ class StandardRPCLinkCodec {
211
250
  expectedMethod;
212
251
  headers;
213
252
  async encode(path, input, options) {
253
+ let headers = toStandardHeaders(await value(this.headers, options, path, input));
254
+ if (options.lastEventId !== void 0) {
255
+ headers = mergeStandardHeaders(headers, { "last-event-id": options.lastEventId });
256
+ }
214
257
  const expectedMethod = await value(this.expectedMethod, options, path, input);
215
- let headers = await value(this.headers, options, path, input);
216
258
  const baseUrl = await value(this.baseUrl, options, path, input);
217
259
  const url = new URL(baseUrl);
218
260
  url.pathname = `${url.pathname.replace(/\/$/, "")}${toHttpPath(path)}`;
219
- if (options.lastEventId !== void 0) {
220
- headers = mergeStandardHeaders(headers, { "last-event-id": options.lastEventId });
221
- }
222
261
  const serialized = this.serializer.serialize(input);
223
262
  if (expectedMethod === "GET" && !(serialized instanceof FormData) && !isAsyncIteratorObject(serialized)) {
224
263
  const maxUrlLength = await value(this.maxUrlLength, options, path, input);
@@ -330,6 +369,9 @@ class StandardRPCSerializer {
330
369
  return this.#deserialize(data);
331
370
  }
332
371
  #deserialize(data) {
372
+ if (data === void 0) {
373
+ return void 0;
374
+ }
333
375
  if (!(data instanceof FormData)) {
334
376
  return this.jsonSerializer.deserialize(data.json, data.meta ?? []);
335
377
  }
@@ -352,4 +394,4 @@ class StandardRPCLink extends StandardLink {
352
394
  }
353
395
  }
354
396
 
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 };
397
+ 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, toStandardHeaders as f, getMalformedResponseErrorCode as g, toHttpPath as t };
@@ -1,5 +1,5 @@
1
- import { a as ClientContext, b as ClientOptions, d as HTTPMethod } from './client.4TS_0JaO.js';
2
- import { e as StandardLinkCodec, b as StandardLinkOptions, d as StandardLink, f as StandardLinkClient } from './client.C0KbSWlC.js';
1
+ import { b as ClientContext, c as ClientOptions, f as HTTPMethod } from './client.BOYsZIRq.js';
2
+ import { e as StandardLinkCodec, b as StandardLinkOptions, d as StandardLink, f as StandardLinkClient } from './client.BG98rYdO.js';
3
3
  import { Segment, Value, Promisable } from '@orpc/shared';
4
4
  import { StandardHeaders, StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
5
5
 
@@ -67,7 +67,7 @@ interface StandardRPCLinkCodecOptions<T extends ClientContext> {
67
67
  /**
68
68
  * Inject headers to the request.
69
69
  */
70
- headers?: Value<Promisable<StandardHeaders>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
70
+ headers?: Value<Promisable<StandardHeaders | Headers>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
71
71
  }
72
72
  declare class StandardRPCLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
73
73
  private readonly serializer;
@@ -1,5 +1,5 @@
1
- import { a as ClientContext, b as ClientOptions, d as HTTPMethod } from './client.4TS_0JaO.mjs';
2
- import { e as StandardLinkCodec, b as StandardLinkOptions, d as StandardLink, f as StandardLinkClient } from './client.BMoG_EdN.mjs';
1
+ import { b as ClientContext, c as ClientOptions, f as HTTPMethod } from './client.BOYsZIRq.mjs';
2
+ import { e as StandardLinkCodec, b as StandardLinkOptions, d as StandardLink, f as StandardLinkClient } from './client.Bwgm6dgk.mjs';
3
3
  import { Segment, Value, Promisable } from '@orpc/shared';
4
4
  import { StandardHeaders, StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
5
5
 
@@ -67,7 +67,7 @@ interface StandardRPCLinkCodecOptions<T extends ClientContext> {
67
67
  /**
68
68
  * Inject headers to the request.
69
69
  */
70
- headers?: Value<Promisable<StandardHeaders>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
70
+ headers?: Value<Promisable<StandardHeaders | Headers>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
71
71
  }
72
72
  declare class StandardRPCLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
73
73
  private readonly serializer;