@orpc/client 0.0.0-next.b5ac9a3 → 0.0.0-next.b5b7502

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,7 +1,6 @@
1
- import { intercept, isObject, value, trim, isAsyncIteratorObject, stringifyJSON } from '@orpc/shared';
2
- import { C as CompositeClientPlugin } from './client.CvnV7_uV.mjs';
3
- import { ErrorEvent } from '@orpc/standard-server';
4
- import { O as ORPCError, m as mapEventIterator, t as toORPCError } from './client.BacCdg3F.mjs';
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, O as ORPCError, m as mapEventIterator, t as toORPCError } from './client.jKEwIsRd.mjs';
5
4
 
6
5
  class InvalidEventIteratorRetryResponse extends Error {
7
6
  }
@@ -9,10 +8,11 @@ class StandardLink {
9
8
  constructor(codec, sender, options = {}) {
10
9
  this.codec = codec;
11
10
  this.sender = sender;
12
- const plugin = new CompositeClientPlugin(options.plugins);
13
- plugin.init(options);
14
- this.interceptors = options.interceptors ?? [];
15
- this.clientInterceptors = options.clientInterceptors ?? [];
11
+ for (const plugin of toArray(options.plugins)) {
12
+ plugin.init?.(options);
13
+ }
14
+ this.interceptors = toArray(options.interceptors);
15
+ this.clientInterceptors = toArray(options.clientInterceptors);
16
16
  }
17
17
  interceptors;
18
18
  clientInterceptors;
@@ -111,6 +111,9 @@ class StandardRPCJsonSerializer {
111
111
  if (isObject(data)) {
112
112
  const json = {};
113
113
  for (const k in data) {
114
+ if (k === "toJSON" && typeof data[k] === "function") {
115
+ continue;
116
+ }
114
117
  json[k] = this.serialize(data[k], [...segments, k], meta, maps, blobs)[0];
115
118
  }
116
119
  return [json, meta, maps, blobs];
@@ -177,6 +180,13 @@ class StandardRPCJsonSerializer {
177
180
  }
178
181
  }
179
182
 
183
+ function toHttpPath(path) {
184
+ return `/${path.map(encodeURIComponent).join("/")}`;
185
+ }
186
+ function getMalformedResponseErrorCode(status) {
187
+ return Object.entries(COMMON_ORPC_ERROR_DEFS).find(([, def]) => def.status === status)?.[0] ?? "MALFORMED_ORPC_ERROR_RESPONSE";
188
+ }
189
+
180
190
  class StandardRPCLinkCodec {
181
191
  constructor(serializer, options) {
182
192
  this.serializer = serializer;
@@ -193,17 +203,11 @@ class StandardRPCLinkCodec {
193
203
  headers;
194
204
  async encode(path, input, options) {
195
205
  const expectedMethod = await value(this.expectedMethod, options, path, input);
196
- const headers = { ...await value(this.headers, options, path, input) };
206
+ let headers = await value(this.headers, options, path, input);
197
207
  const baseUrl = await value(this.baseUrl, options, path, input);
198
- const url = new URL(`${trim(baseUrl.toString(), "/")}/${path.map(encodeURIComponent).join("/")}`);
208
+ const url = new URL(`${baseUrl.toString().replace(/\/$/, "")}${toHttpPath(path)}`);
199
209
  if (options.lastEventId !== void 0) {
200
- if (Array.isArray(headers["last-event-id"])) {
201
- headers["last-event-id"] = [...headers["last-event-id"], options.lastEventId];
202
- } else if (headers["last-event-id"] !== void 0) {
203
- headers["last-event-id"] = [headers["last-event-id"], options.lastEventId];
204
- } else {
205
- headers["last-event-id"] = options.lastEventId;
206
- }
210
+ headers = mergeStandardHeaders(headers, { "last-event-id": options.lastEventId });
207
211
  }
208
212
  const serialized = this.serializer.serialize(input);
209
213
  if (expectedMethod === "GET" && !(serialized instanceof FormData) && !isAsyncIteratorObject(serialized)) {
@@ -229,7 +233,7 @@ class StandardRPCLinkCodec {
229
233
  };
230
234
  }
231
235
  async decode(response) {
232
- const isOk = response.status >= 200 && response.status < 300;
236
+ const isOk = !isORPCErrorStatus(response.status);
233
237
  const deserialized = await (async () => {
234
238
  let isBodyOk = false;
235
239
  try {
@@ -251,8 +255,9 @@ class StandardRPCLinkCodec {
251
255
  if (ORPCError.isValidJSON(deserialized)) {
252
256
  throw ORPCError.fromJSON(deserialized);
253
257
  }
254
- throw new Error("Invalid RPC error response format.", {
255
- cause: deserialized
258
+ throw new ORPCError(getMalformedResponseErrorCode(response.status), {
259
+ status: response.status,
260
+ data: deserialized
256
261
  });
257
262
  }
258
263
  return deserialized;
@@ -328,4 +333,4 @@ class StandardRPCSerializer {
328
333
  }
329
334
  }
330
335
 
331
- export { InvalidEventIteratorRetryResponse as I, StandardLink as S, STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES as a, StandardRPCJsonSerializer as b, StandardRPCLinkCodec as c, StandardRPCSerializer as d };
336
+ export { InvalidEventIteratorRetryResponse as I, StandardLink as S, STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES as a, StandardRPCJsonSerializer as b, StandardRPCLinkCodec as c, StandardRPCSerializer as d, getMalformedResponseErrorCode as g, toHttpPath as t };
@@ -1,27 +1,30 @@
1
- import { Interceptor } from '@orpc/shared';
1
+ import { Interceptor, ThrowableError } from '@orpc/shared';
2
2
  import { StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
3
- import { a as ClientContext, C as ClientOptionsOut, b as ClientLink } from './client.CupM8eRP.mjs';
3
+ import { a as ClientContext, b as ClientOptions, C as ClientLink } from './client.C0lT7w02.js';
4
4
 
5
5
  interface StandardLinkCodec<T extends ClientContext> {
6
- encode(path: readonly string[], input: unknown, options: ClientOptionsOut<any>): Promise<StandardRequest>;
7
- decode(response: StandardLazyResponse, options: ClientOptionsOut<T>, path: readonly string[], input: unknown): Promise<unknown>;
6
+ encode(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<StandardRequest>;
7
+ decode(response: StandardLazyResponse, options: ClientOptions<T>, path: readonly string[], input: unknown): Promise<unknown>;
8
8
  }
9
9
  interface StandardLinkClient<T extends ClientContext> {
10
- call(request: StandardRequest, options: ClientOptionsOut<T>, path: readonly string[], input: unknown): Promise<StandardLazyResponse>;
10
+ call(request: StandardRequest, options: ClientOptions<T>, path: readonly string[], input: unknown): Promise<StandardLazyResponse>;
11
11
  }
12
12
 
13
13
  declare class InvalidEventIteratorRetryResponse extends Error {
14
14
  }
15
+ interface StandardLinkPlugin<T extends ClientContext> {
16
+ init?(options: StandardLinkOptions<T>): void;
17
+ }
15
18
  interface StandardLinkOptions<T extends ClientContext> {
16
19
  interceptors?: Interceptor<{
17
20
  path: readonly string[];
18
21
  input: unknown;
19
- options: ClientOptionsOut<T>;
20
- }, unknown, unknown>[];
22
+ options: ClientOptions<T>;
23
+ }, unknown, ThrowableError>[];
21
24
  clientInterceptors?: Interceptor<{
22
25
  request: StandardRequest;
23
- }, StandardLazyResponse, unknown>[];
24
- plugins?: ClientPlugin<T>[];
26
+ }, StandardLazyResponse, ThrowableError>[];
27
+ plugins?: StandardLinkPlugin<T>[];
25
28
  }
26
29
  declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
27
30
  #private;
@@ -30,16 +33,7 @@ declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
30
33
  private readonly interceptors;
31
34
  private readonly clientInterceptors;
32
35
  constructor(codec: StandardLinkCodec<T>, sender: StandardLinkClient<T>, options?: StandardLinkOptions<T>);
33
- call(path: readonly string[], input: unknown, options: ClientOptionsOut<T>): Promise<unknown>;
34
- }
35
-
36
- interface ClientPlugin<T extends ClientContext> {
37
- init?(options: StandardLinkOptions<T>): void;
38
- }
39
- declare class CompositeClientPlugin<T extends ClientContext> implements ClientPlugin<T> {
40
- private readonly plugins;
41
- constructor(plugins?: ClientPlugin<T>[]);
42
- init(options: StandardLinkOptions<T>): void;
36
+ call(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<unknown>;
43
37
  }
44
38
 
45
- export { type ClientPlugin as C, InvalidEventIteratorRetryResponse as I, type StandardLinkOptions as S, CompositeClientPlugin as a, type StandardLinkClient as b, type StandardLinkCodec as c, StandardLink as d };
39
+ export { InvalidEventIteratorRetryResponse as I, type StandardLinkPlugin as S, type StandardLinkOptions as a, StandardLink as b, type StandardLinkCodec as c, type StandardLinkClient as d };
@@ -91,8 +91,8 @@ class ORPCError extends Error {
91
91
  status;
92
92
  data;
93
93
  constructor(code, ...[options]) {
94
- if (options?.status && (options.status < 400 || options.status >= 600)) {
95
- throw new Error("[ORPCError] The error status code must be in the 400-599 range.");
94
+ if (options?.status && !isORPCErrorStatus(options.status)) {
95
+ throw new Error("[ORPCError] Invalid error status code.");
96
96
  }
97
97
  const message = fallbackORPCErrorMessage(code, options?.message);
98
98
  super(message, options);
@@ -136,6 +136,9 @@ function toORPCError(error) {
136
136
  cause: error
137
137
  });
138
138
  }
139
+ function isORPCErrorStatus(status) {
140
+ return status < 200 || status >= 400;
141
+ }
139
142
 
140
143
  function mapEventIterator(iterator, maps) {
141
144
  return async function* () {
@@ -169,4 +172,4 @@ function mapEventIterator(iterator, maps) {
169
172
  }();
170
173
  }
171
174
 
172
- export { COMMON_ORPC_ERROR_DEFS as C, ORPCError as O, fallbackORPCErrorMessage as a, fallbackORPCErrorStatus as f, isDefinedError as i, mapEventIterator as m, toORPCError as t };
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 };
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.b5ac9a3",
4
+ "version": "0.0.0-next.b5b7502",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -39,9 +39,9 @@
39
39
  "dist"
40
40
  ],
41
41
  "dependencies": {
42
- "@orpc/shared": "0.0.0-next.b5ac9a3",
43
- "@orpc/standard-server": "0.0.0-next.b5ac9a3",
44
- "@orpc/standard-server-fetch": "0.0.0-next.b5ac9a3"
42
+ "@orpc/shared": "0.0.0-next.b5b7502",
43
+ "@orpc/standard-server": "0.0.0-next.b5b7502",
44
+ "@orpc/standard-server-fetch": "0.0.0-next.b5b7502"
45
45
  },
46
46
  "devDependencies": {
47
47
  "zod": "^3.24.2"
@@ -1,30 +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
- export type { ClientOptionsOut as C, InferClientContext as I, NestedClient as N, ClientContext as a, ClientLink as b, ClientPromiseResult as c, ClientOptions as d, ClientRest as e, Client as f };
@@ -1,30 +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
- export type { ClientOptionsOut as C, InferClientContext as I, NestedClient as N, ClientContext as a, ClientLink as b, ClientPromiseResult as c, ClientOptions as d, ClientRest as e, Client as f };
@@ -1,12 +0,0 @@
1
- class CompositeClientPlugin {
2
- constructor(plugins = []) {
3
- this.plugins = plugins;
4
- }
5
- init(options) {
6
- for (const plugin of this.plugins) {
7
- plugin.init?.(options);
8
- }
9
- }
10
- }
11
-
12
- export { CompositeClientPlugin as C };