@orpc/client 2.0.0-beta.49 → 2.0.0-beta.50

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.
Files changed (75) hide show
  1. package/dist/adapters/fetch/index.d.ts +3 -2
  2. package/dist/adapters/fetch/index.js +2 -2
  3. package/dist/adapters/fetch/rpc-link.d.ts +8 -8
  4. package/dist/adapters/fetch/rpc-link.js +16 -13
  5. package/dist/adapters/fetch/transport.d.ts +29 -31
  6. package/dist/adapters/fetch/transport.js +49 -63
  7. package/dist/adapters/message-port/index.d.ts +4 -3
  8. package/dist/adapters/message-port/index.js +3 -3
  9. package/dist/adapters/message-port/message-port.d.ts +13 -13
  10. package/dist/adapters/message-port/message-port.js +35 -53
  11. package/dist/adapters/message-port/rpc-link.d.ts +8 -8
  12. package/dist/adapters/message-port/rpc-link.js +17 -14
  13. package/dist/adapters/message-port/transport.d.ts +31 -34
  14. package/dist/adapters/message-port/transport.js +38 -52
  15. package/dist/adapters/standard/codec.d.ts +10 -10
  16. package/dist/adapters/standard/index.d.ts +6 -5
  17. package/dist/adapters/standard/index.js +2 -4
  18. package/dist/adapters/standard/link.d.ts +31 -31
  19. package/dist/adapters/standard/link.js +101 -85
  20. package/dist/adapters/standard/plugin.d.ts +25 -31
  21. package/dist/adapters/standard/plugin.js +14 -16
  22. package/dist/adapters/standard/rpc-link-codec.d.ts +53 -60
  23. package/dist/adapters/standard/rpc-link-codec.js +98 -102
  24. package/dist/adapters/standard/transport.d.ts +7 -7
  25. package/dist/adapters/websocket/index.d.ts +3 -2
  26. package/dist/adapters/websocket/index.js +2 -2
  27. package/dist/adapters/websocket/rpc-link.d.ts +8 -8
  28. package/dist/adapters/websocket/rpc-link.js +16 -13
  29. package/dist/adapters/websocket/transport.d.ts +101 -100
  30. package/dist/adapters/websocket/transport.js +102 -130
  31. package/dist/async-iterator-object.d.ts +2 -2
  32. package/dist/async-iterator-object.js +27 -28
  33. package/dist/client-safe.d.ts +4 -6
  34. package/dist/client-safe.js +29 -34
  35. package/dist/client.d.ts +24 -26
  36. package/dist/client.js +35 -37
  37. package/dist/consts.d.ts +1 -1
  38. package/dist/consts.js +18 -41
  39. package/dist/dynamic-link.d.ts +6 -6
  40. package/dist/dynamic-link.js +16 -16
  41. package/dist/error-utils.d.ts +3 -4
  42. package/dist/error-utils.js +47 -51
  43. package/dist/error.d.ts +79 -79
  44. package/dist/error.js +154 -166
  45. package/dist/index.d.ts +14 -53
  46. package/dist/index.js +12 -40
  47. package/dist/plugins/batch.d.ts +122 -155
  48. package/dist/plugins/batch.js +320 -391
  49. package/dist/plugins/cookie-jar.d.ts +64 -61
  50. package/dist/plugins/cookie-jar.js +208 -245
  51. package/dist/plugins/dedupe.d.ts +49 -53
  52. package/dist/plugins/dedupe.js +168 -211
  53. package/dist/plugins/index.d.ts +9 -28
  54. package/dist/plugins/index.js +8 -23
  55. package/dist/plugins/request-compression.d.ts +38 -35
  56. package/dist/plugins/request-compression.js +131 -177
  57. package/dist/plugins/response-compression.d.ts +21 -18
  58. package/dist/plugins/response-compression.js +64 -88
  59. package/dist/plugins/retry-after.d.ts +31 -38
  60. package/dist/plugins/retry-after.js +60 -85
  61. package/dist/plugins/retry.d.ts +55 -55
  62. package/dist/plugins/retry.js +118 -137
  63. package/dist/plugins/timeout.d.ts +19 -17
  64. package/dist/plugins/timeout.js +41 -36
  65. package/dist/rpc-json-serializer.d.ts +84 -84
  66. package/dist/rpc-json-serializer.js +338 -383
  67. package/dist/rpc-serializer.d.ts +20 -21
  68. package/dist/rpc-serializer.js +97 -125
  69. package/dist/types.d.ts +19 -31
  70. package/dist/utils.d.ts +18 -29
  71. package/dist/utils.js +63 -47
  72. package/package.json +5 -5
  73. package/dist/adapters/standard/codec.js +0 -1
  74. package/dist/adapters/standard/transport.js +0 -1
  75. package/dist/types.js +0 -1
@@ -1,20 +1,19 @@
1
- import type { StandardBody } from '@standard-server/core';
2
- import type { RPCJsonSerializerOptions } from './rpc-json-serializer.ts';
3
- import { RPCJsonSerializer } from './rpc-json-serializer.ts';
1
+ import { RPCJsonSerializer, RPCJsonSerializerOptions } from "./rpc-json-serializer.js";
2
+ import { StandardBody } from "@standard-server/core";
4
3
  export interface RPCSerializerSerializeOptions {
5
- /**
6
- * Use FormData for serialization when nested blobs are present.
7
- * Does not apply to root-level Blob values.
8
- *
9
- * @default true
10
- */
11
- useFormDataForBlobFields?: boolean | undefined;
4
+ /**
5
+ * Use FormData for serialization when nested blobs are present.
6
+ * Does not apply to root-level Blob values.
7
+ *
8
+ * @default true
9
+ */
10
+ useFormDataForBlobFields?: boolean | undefined;
12
11
  }
13
12
  export interface RPCSerializerOptions extends RPCJsonSerializerOptions {
14
- /**
15
- * Default options for serialize method
16
- */
17
- serialize?: RPCSerializerSerializeOptions | undefined;
13
+ /**
14
+ * Default options for serialize method
15
+ */
16
+ serialize?: RPCSerializerSerializeOptions | undefined;
18
17
  }
19
18
  /**
20
19
  * Serializes and deserializes data for the RPC protocol,
@@ -23,10 +22,10 @@ export interface RPCSerializerOptions extends RPCJsonSerializerOptions {
23
22
  * @see {@link https://orpc.dev/docs/rpc/serializer | RPC Serializer}
24
23
  */
25
24
  export declare class RPCSerializer extends RPCJsonSerializer {
26
- private readonly defaultSerializeOptions;
27
- constructor(options?: RPCSerializerOptions);
28
- serialize(data: unknown, options?: RPCSerializerSerializeOptions): StandardBody;
29
- private serializePayload;
30
- deserialize(data: StandardBody): unknown;
31
- private deserializePayload;
32
- }
25
+ private readonly defaultSerializeOptions;
26
+ constructor(options?: RPCSerializerOptions);
27
+ serialize(data: unknown, options?: RPCSerializerSerializeOptions): StandardBody;
28
+ private serializePayload;
29
+ deserialize(data: StandardBody): unknown;
30
+ private deserializePayload;
31
+ }
@@ -1,130 +1,102 @@
1
- import { isAsyncIteratorObject, isPlainObject, stringifyJSON } from '@orpc/shared';
2
- import { EventStreamErrorEvent } from '@standard-server/core';
3
- import { wrapAsyncIteratorPreservingEventMeta } from './async-iterator-object.js';
4
- import { createORPCErrorFromJson, isORPCErrorJson, toORPCError } from './error-utils.js';
5
- import { RPCJsonSerializer } from './rpc-json-serializer.js';
1
+ import { wrapAsyncIteratorPreservingEventMeta } from "./async-iterator-object.js";
2
+ import { createORPCErrorFromJson, isORPCErrorJson, toORPCError } from "./error-utils.js";
3
+ import { RPCJsonSerializer } from "./rpc-json-serializer.js";
4
+ import { NON_NEGATIVE_INTEGER_REGEX, isAsyncIteratorObject, isPlainObject, stringifyJSON } from "@orpc/shared";
5
+ import { EventStreamErrorEvent } from "@standard-server/core";
6
+ // src/rpc-serializer.ts
6
7
  /**
7
- * Serializes and deserializes data for the RPC protocol,
8
- * preserving native types like Date, BigInt, Set, and Map that plain JSON cannot represent.
9
- *
10
- * @see {@link https://orpc.dev/docs/rpc/serializer | RPC Serializer}
11
- */
12
- export class RPCSerializer extends RPCJsonSerializer {
13
- defaultSerializeOptions;
14
- constructor(options = {}) {
15
- super(options);
16
- this.defaultSerializeOptions = options.serialize;
17
- }
18
- serialize(data, options = {}) {
19
- // standard body already supports these types without additional serialization.
20
- if (data === undefined || data instanceof ReadableStream || data instanceof Blob) {
21
- return data;
22
- }
23
- if (isAsyncIteratorObject(data)) {
24
- return wrapAsyncIteratorPreservingEventMeta(data, {
25
- mapResult: (result) => {
26
- // standard event stream data already supports these types without additional serialization.
27
- if (result.value === undefined) {
28
- return result;
29
- }
30
- return { done: result.done, value: this.serializePayload(result.value, false) };
31
- },
32
- mapError: (e) => new EventStreamErrorEvent(this.serializePayload(toORPCError(e).toJSON(), false), {
33
- cause: e,
34
- }),
35
- });
36
- }
37
- const useFormDataForBlobs = options.useFormDataForBlobFields ??
38
- this.defaultSerializeOptions?.useFormDataForBlobFields ??
39
- true;
40
- return this.serializePayload(data, useFormDataForBlobs);
41
- }
42
- serializePayload(data, useFormDataForBlobs) {
43
- const { json, meta, maps, blobs } = this.serializeJson(data);
44
- if (!useFormDataForBlobs || !blobs?.length) {
45
- return { json, meta };
46
- }
47
- const form = new FormData();
48
- form.set('data', stringifyJSON({ json, meta, maps }));
49
- blobs.forEach((blob, i) => {
50
- form.set(i.toString(), blob);
51
- });
52
- return form;
53
- }
54
- deserialize(data) {
55
- if (data === undefined || data instanceof ReadableStream || data instanceof Blob) {
56
- return data;
57
- }
58
- if (isAsyncIteratorObject(data)) {
59
- return wrapAsyncIteratorPreservingEventMeta(data, {
60
- mapResult: (result) => {
61
- if (result.value === undefined) {
62
- return result;
63
- }
64
- return { done: result.done, value: this.deserializePayload(result.value) };
65
- },
66
- mapError: (e) => {
67
- if (!(e instanceof EventStreamErrorEvent) || e.data === undefined) {
68
- return e;
69
- }
70
- let deserialized;
71
- try {
72
- deserialized = this.deserializePayload(e.data);
73
- }
74
- catch {
75
- return e;
76
- }
77
- if (isORPCErrorJson(deserialized)) {
78
- return createORPCErrorFromJson(deserialized, { cause: e });
79
- }
80
- return new EventStreamErrorEvent(deserialized, { cause: e });
81
- },
82
- });
83
- }
84
- return this.deserializePayload(data);
85
- }
86
- deserializePayload(data) {
87
- if (!(data instanceof FormData)) {
88
- return this.deserializeJson(assertEnvelope(data, false));
89
- }
90
- let raw;
91
- let hasBlobs = false;
92
- const blobs = [];
93
- for (const [key, value] of data) {
94
- if (key === 'data' && raw === undefined && typeof value === 'string') {
95
- raw = value;
96
- }
97
- else if (value instanceof Blob && BLOB_KEY_REGEX.test(key)) {
98
- blobs[Number(key)] = value;
99
- hasBlobs = true;
100
- }
101
- else {
102
- throw invalidFormat();
103
- }
104
- }
105
- const serialized = raw === undefined ? { json: undefined } : assertEnvelope(JSON.parse(raw), true);
106
- if (hasBlobs && serialized.maps === undefined) {
107
- throw invalidFormat();
108
- }
109
- return this.deserializeJson({ ...serialized, blobs });
110
- }
111
- }
112
- const BLOB_KEY_REGEX = /^(?:0|[1-9]\d*)$/;
8
+ * Serializes and deserializes data for the RPC protocol,
9
+ * preserving native types like Date, BigInt, Set, and Map that plain JSON cannot represent.
10
+ *
11
+ * @see {@link https://orpc.dev/docs/rpc/serializer | RPC Serializer}
12
+ */
13
+ var RPCSerializer = class extends RPCJsonSerializer {
14
+ defaultSerializeOptions;
15
+ constructor(options = {}) {
16
+ super(options);
17
+ this.defaultSerializeOptions = options.serialize;
18
+ }
19
+ serialize(data, options = {}) {
20
+ if (data === void 0 || data instanceof ReadableStream || data instanceof Blob) return data;
21
+ if (isAsyncIteratorObject(data)) return wrapAsyncIteratorPreservingEventMeta(data, {
22
+ mapResult: (result) => {
23
+ if (result.value === void 0) return result;
24
+ return {
25
+ done: result.done,
26
+ value: this.serializePayload(result.value, false)
27
+ };
28
+ },
29
+ mapError: (e) => new EventStreamErrorEvent(this.serializePayload(toORPCError(e).toJSON(), false), { cause: e })
30
+ });
31
+ const useFormDataForBlobs = options.useFormDataForBlobFields ?? this.defaultSerializeOptions?.useFormDataForBlobFields ?? true;
32
+ return this.serializePayload(data, useFormDataForBlobs);
33
+ }
34
+ serializePayload(data, useFormDataForBlobs) {
35
+ const { json, meta, maps, blobs } = this.serializeJson(data);
36
+ if (!useFormDataForBlobs || !blobs?.length) return {
37
+ json,
38
+ meta
39
+ };
40
+ const form = new FormData();
41
+ form.set("data", stringifyJSON({
42
+ json,
43
+ meta,
44
+ maps
45
+ }));
46
+ blobs.forEach((blob, i) => {
47
+ form.set(i.toString(), blob);
48
+ });
49
+ return form;
50
+ }
51
+ deserialize(data) {
52
+ if (data === void 0 || data instanceof ReadableStream || data instanceof Blob) return data;
53
+ if (isAsyncIteratorObject(data)) return wrapAsyncIteratorPreservingEventMeta(data, {
54
+ mapResult: (result) => {
55
+ if (result.value === void 0) return result;
56
+ return {
57
+ done: result.done,
58
+ value: this.deserializePayload(result.value)
59
+ };
60
+ },
61
+ mapError: (e) => {
62
+ if (!(e instanceof EventStreamErrorEvent) || e.data === void 0) return e;
63
+ let deserialized;
64
+ try {
65
+ deserialized = this.deserializePayload(e.data);
66
+ } catch {
67
+ return e;
68
+ }
69
+ if (isORPCErrorJson(deserialized)) return createORPCErrorFromJson(deserialized, { cause: e });
70
+ return new EventStreamErrorEvent(deserialized, { cause: e });
71
+ }
72
+ });
73
+ return this.deserializePayload(data);
74
+ }
75
+ deserializePayload(data) {
76
+ if (!(data instanceof FormData)) return this.deserializeJson(assertEnvelope(data, false));
77
+ let raw;
78
+ let hasBlobs = false;
79
+ const blobs = [];
80
+ for (const [key, value] of data) if (key === "data" && raw === void 0 && typeof value === "string") raw = value;
81
+ else if (value instanceof Blob && NON_NEGATIVE_INTEGER_REGEX.test(key)) {
82
+ blobs[Number(key)] = value;
83
+ hasBlobs = true;
84
+ } else throw invalidFormat();
85
+ const serialized = raw === void 0 ? { json: void 0 } : assertEnvelope(JSON.parse(raw), true);
86
+ if (hasBlobs && serialized.maps === void 0) throw invalidFormat();
87
+ return this.deserializeJson({
88
+ ...serialized,
89
+ blobs
90
+ });
91
+ }
92
+ };
113
93
  function invalidFormat() {
114
- return new TypeError('Invalid RPC serialized data format.');
94
+ return /* @__PURE__ */ new TypeError("Invalid RPC serialized data format.");
115
95
  }
116
96
  function assertEnvelope(value, allowMaps) {
117
- if (!isPlainObject(value)) {
118
- throw invalidFormat();
119
- }
120
- for (const key of Object.keys(value)) {
121
- if (key !== 'json' && key !== 'meta' && (!allowMaps || key !== 'maps')) {
122
- throw invalidFormat();
123
- }
124
- }
125
- if ((value.meta !== undefined && !Array.isArray(value.meta)) ||
126
- (value.maps !== undefined && !Array.isArray(value.maps))) {
127
- throw invalidFormat();
128
- }
129
- return value;
97
+ if (!isPlainObject(value)) throw invalidFormat();
98
+ for (const key of Object.keys(value)) if (key !== "json" && key !== "meta" && (!allowMaps || key !== "maps")) throw invalidFormat();
99
+ if (value.meta !== void 0 && !Array.isArray(value.meta) || value.maps !== void 0 && !Array.isArray(value.maps)) throw invalidFormat();
100
+ return value;
130
101
  }
102
+ export { RPCSerializer };
package/dist/types.d.ts CHANGED
@@ -1,23 +1,23 @@
1
- import type { PromiseWithError } from '@orpc/shared';
1
+ import { PromiseWithError } from "@orpc/shared";
2
2
  export interface ClientContext {
3
- [key: PropertyKey]: any;
3
+ [key: PropertyKey]: any;
4
4
  }
5
5
  export interface ClientOptions<T extends ClientContext> {
6
- signal?: AbortSignal | undefined;
7
- lastEventId?: string | undefined;
8
- context: T;
6
+ signal?: AbortSignal | undefined;
7
+ lastEventId?: string | undefined;
8
+ context: T;
9
9
  }
10
- export type FriendlyClientOptions<T extends ClientContext> = Omit<ClientOptions<T>, 'context'> & (object extends T ? {
11
- context?: T | undefined;
10
+ export type FriendlyClientOptions<T extends ClientContext> = Omit<ClientOptions<T>, "context"> & (object extends T ? {
11
+ context?: T | undefined;
12
12
  } : {
13
- context: T;
13
+ context: T;
14
14
  });
15
15
  export type ClientRest<TClientContext extends ClientContext, TInput> = object extends TClientContext ? undefined extends TInput ? [input?: TInput, options?: FriendlyClientOptions<TClientContext>] : [input: TInput, options?: FriendlyClientOptions<TClientContext>] : [input: TInput, options: FriendlyClientOptions<TClientContext>];
16
16
  export interface Client<TClientContext extends ClientContext, TInput, TOutput, TError> {
17
- (...rest: ClientRest<TClientContext, TInput>): PromiseWithError<TOutput, TError>;
17
+ (...rest: ClientRest<TClientContext, TInput>): PromiseWithError<TOutput, TError>;
18
18
  }
19
19
  export type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
20
- [k: string]: NestedClient<TClientContext>;
20
+ [k: string]: NestedClient<TClientContext>;
21
21
  };
22
22
  export type AnyNestedClient = NestedClient<any>;
23
23
  /**
@@ -27,7 +27,7 @@ export type AnyNestedClient = NestedClient<any>;
27
27
  */
28
28
  export type InferClientContext<T extends AnyNestedClient> = T extends NestedClient<infer U> ? U : never;
29
29
  export interface ClientLink<TClientContext extends ClientContext> {
30
- call: (path: string[], input: unknown, options: ClientOptions<TClientContext>) => Promise<unknown>;
30
+ call: (path: string[], input: unknown, options: ClientOptions<TClientContext>) => Promise<unknown>;
31
31
  }
32
32
  /**
33
33
  * Recursively infers the **input types** from a client.
@@ -36,9 +36,7 @@ export interface ClientLink<TClientContext extends ClientContext> {
36
36
  *
37
37
  * @see {@link https://orpc.dev/docs/client/client-side#infer-client-inputs | Client-Side Clients - Infer Client Inputs}
38
38
  */
39
- export type InferClientInputs<T extends AnyNestedClient> = T extends Client<any, infer U, any, any> ? U : {
40
- [K in keyof T]: T[K] extends AnyNestedClient ? InferClientInputs<T[K]> : never;
41
- };
39
+ export type InferClientInputs<T extends AnyNestedClient> = T extends Client<any, infer U, any, any> ? U : { [K in keyof T]: T[K] extends AnyNestedClient ? InferClientInputs<T[K]> : never; };
42
40
  /**
43
41
  * Recursively infers the **body input types** from a client.
44
42
  *
@@ -48,10 +46,8 @@ export type InferClientInputs<T extends AnyNestedClient> = T extends Client<any,
48
46
  * @see {@link https://orpc.dev/docs/client/client-side#infer-client-body-inputs | Client-Side Clients - Infer Client Body Inputs}
49
47
  */
50
48
  export type InferClientBodyInputs<T extends AnyNestedClient> = T extends Client<any, infer U, any, any> ? U extends {
51
- body: infer UBody;
52
- } ? UBody : U : {
53
- [K in keyof T]: T[K] extends AnyNestedClient ? InferClientBodyInputs<T[K]> : never;
54
- };
49
+ body: infer UBody;
50
+ } ? UBody : U : { [K in keyof T]: T[K] extends AnyNestedClient ? InferClientBodyInputs<T[K]> : never; };
55
51
  /**
56
52
  * Recursively infers the **output types** from a client.
57
53
  *
@@ -59,9 +55,7 @@ export type InferClientBodyInputs<T extends AnyNestedClient> = T extends Client<
59
55
  *
60
56
  * @see {@link https://orpc.dev/docs/client/client-side#infer-client-outputs | Client-Side Clients - Infer Client Outputs}
61
57
  */
62
- export type InferClientOutputs<T extends AnyNestedClient> = T extends Client<any, any, infer U, any> ? U : {
63
- [K in keyof T]: T[K] extends AnyNestedClient ? InferClientOutputs<T[K]> : never;
64
- };
58
+ export type InferClientOutputs<T extends AnyNestedClient> = T extends Client<any, any, infer U, any> ? U : { [K in keyof T]: T[K] extends AnyNestedClient ? InferClientOutputs<T[K]> : never; };
65
59
  /**
66
60
  * Recursively infers the **body output types** from a client.
67
61
  *
@@ -71,10 +65,8 @@ export type InferClientOutputs<T extends AnyNestedClient> = T extends Client<any
71
65
  * @see {@link https://orpc.dev/docs/client/client-side#infer-client-body-outputs | Client-Side Clients - Infer Client Body Outputs}
72
66
  */
73
67
  export type InferClientBodyOutputs<T extends AnyNestedClient> = T extends Client<any, any, infer U, any> ? U extends {
74
- body: infer UBody;
75
- } ? UBody : U : {
76
- [K in keyof T]: T[K] extends AnyNestedClient ? InferClientBodyOutputs<T[K]> : never;
77
- };
68
+ body: infer UBody;
69
+ } ? UBody : U : { [K in keyof T]: T[K] extends AnyNestedClient ? InferClientBodyOutputs<T[K]> : never; };
78
70
  /**
79
71
  * Recursively infers the **error types** from a client when you use [type-safe errors](https://orpc.dev/docs/error-handling#typesafe-errors).
80
72
  *
@@ -82,9 +74,7 @@ export type InferClientBodyOutputs<T extends AnyNestedClient> = T extends Client
82
74
  *
83
75
  * @see {@link https://orpc.dev/docs/client/client-side#infer-client-errors | Client-Side Clients - Infer Client Errors}
84
76
  */
85
- export type InferClientErrors<T extends AnyNestedClient> = T extends Client<any, any, any, infer U> ? U : {
86
- [K in keyof T]: T[K] extends AnyNestedClient ? InferClientErrors<T[K]> : never;
87
- };
77
+ export type InferClientErrors<T extends AnyNestedClient> = T extends Client<any, any, any, infer U> ? U : { [K in keyof T]: T[K] extends AnyNestedClient ? InferClientErrors<T[K]> : never; };
88
78
  /**
89
79
  * Recursively infers a **union of all error types** from a client when you use [type-safe errors](https://orpc.dev/docs/error-handling#typesafe-errors).
90
80
  *
@@ -92,6 +82,4 @@ export type InferClientErrors<T extends AnyNestedClient> = T extends Client<any,
92
82
  *
93
83
  * @see {@link https://orpc.dev/docs/client/client-side#infer-client-error | Client-Side Clients - Infer Client Error}
94
84
  */
95
- export type InferClientError<T extends AnyNestedClient> = T extends Client<any, any, any, infer U> ? U : {
96
- [K in keyof T]: T[K] extends AnyNestedClient ? InferClientError<T[K]> : never;
97
- }[keyof T];
85
+ export type InferClientError<T extends AnyNestedClient> = T extends Client<any, any, any, infer U> ? U : { [K in keyof T]: T[K] extends AnyNestedClient ? InferClientError<T[K]> : never; }[keyof T];
package/dist/utils.d.ts CHANGED
@@ -1,33 +1,22 @@
1
- import type { PromiseWithError, ThrowableError } from '@orpc/shared';
2
- import type { AnyORPCError } from './error.ts';
3
- import type { ClientContext, ClientOptions, ClientRest, FriendlyClientOptions } from './types.ts';
4
- export declare function resolveFriendlyClientOptions<T extends ClientContext>(options: FriendlyClientOptions<T>): ClientOptions<T>;
1
+ import { AnyORPCError } from "./error.js";
2
+ import { ClientContext, ClientOptions, ClientRest } from "./types.js";
3
+ import { PromiseWithError, ThrowableError } from "@orpc/shared";
5
4
  export declare function resolveClientRest<TClientContext extends ClientContext, TInput>(rest: ClientRest<TClientContext, TInput>): [input: TInput, options: ClientOptions<TClientContext>];
6
5
  export type SafeResult<TOutput, TError> = ([error: null, data: TOutput, definedError: null, isSuccess: true] & {
7
- error: null;
8
- data: TOutput;
9
- definedError: null;
10
- isSuccess: true;
11
- }) | ([
12
- error: Exclude<TError, AnyORPCError>,
13
- data: undefined,
14
- definedError: null,
15
- isSuccess: false
16
- ] & {
17
- error: Exclude<TError, AnyORPCError>;
18
- data: undefined;
19
- definedError: null;
20
- isSuccess: false;
21
- }) | ([
22
- error: Extract<TError, AnyORPCError>,
23
- data: undefined,
24
- definedError: Extract<TError, AnyORPCError>,
25
- isSuccess: false
26
- ] & {
27
- error: Extract<TError, AnyORPCError>;
28
- data: undefined;
29
- definedError: Extract<TError, AnyORPCError>;
30
- isSuccess: false;
6
+ error: null;
7
+ data: TOutput;
8
+ definedError: null;
9
+ isSuccess: true;
10
+ }) | ([error: Exclude<TError, AnyORPCError>, data: undefined, definedError: null, isSuccess: false] & {
11
+ error: Exclude<TError, AnyORPCError>;
12
+ data: undefined;
13
+ definedError: null;
14
+ isSuccess: false;
15
+ }) | ([error: Extract<TError, AnyORPCError>, data: undefined, definedError: Extract<TError, AnyORPCError>, isSuccess: false] & {
16
+ error: Extract<TError, AnyORPCError>;
17
+ data: undefined;
18
+ definedError: Extract<TError, AnyORPCError>;
19
+ isSuccess: false;
31
20
  });
32
21
  /**
33
22
  * Works like try/catch, but help you infer the error type if it is a defined ORPCError.
@@ -44,4 +33,4 @@ export type SafeResult<TOutput, TError> = ([error: null, data: TOutput, definedE
44
33
  *
45
34
  * @see {@link https://orpc.dev/docs/client/error-handling#using-safe-and-isdefinederror | Client Error Handling - Using safe and isDefinedError}
46
35
  */
47
- export declare function safe<TOutput, TError = ThrowableError>(promise: PromiseWithError<TOutput, TError>): Promise<SafeResult<TOutput, TError>>;
36
+ export declare function safe<TOutput, TError = ThrowableError>(promise: PromiseWithError<TOutput, TError>): Promise<SafeResult<TOutput, TError>>;
package/dist/utils.js CHANGED
@@ -1,51 +1,67 @@
1
- import { isDefinedError } from './error-utils.js';
2
- export function resolveFriendlyClientOptions(options) {
3
- return {
4
- ...options,
5
- context: options.context ?? {}, // Context only optional if all fields are optional
6
- };
1
+ import { isDefinedError } from "./error-utils.js";
2
+ // src/utils.ts
3
+ function resolveFriendlyClientOptions(options) {
4
+ return {
5
+ ...options,
6
+ context: options.context ?? {}
7
+ };
7
8
  }
8
- export function resolveClientRest(rest) {
9
- return [
10
- rest[0], // rest[0] can be undefined if TInput is optional,
11
- resolveFriendlyClientOptions(rest[1] ?? {}), // rest[1] can be undefined if all fields of FriendlyClientOptions are optional
12
- ];
9
+ function resolveClientRest(rest) {
10
+ return [rest[0], resolveFriendlyClientOptions(rest[1] ?? {})];
13
11
  }
14
12
  /**
15
- * Works like try/catch, but help you infer the error type if it is a defined ORPCError.
16
- *
17
- * @example
18
- * ```ts
19
- * const [error, data, definedError, isSuccess] = await safe(client(...))
20
- * // or const { error, data, definedError, isSuccess } = await safe(client(...))
21
- *
22
- * if (definedError) {
23
- * console.log(definedError) // or error, both are well typed
24
- * }
25
- * ```
26
- *
27
- * @see {@link https://orpc.dev/docs/client/error-handling#using-safe-and-isdefinederror | Client Error Handling - Using safe and isDefinedError}
28
- */
29
- export async function safe(promise) {
30
- try {
31
- const output = await promise;
32
- return Object.assign([null, output, null, true], {
33
- error: null,
34
- data: output,
35
- definedError: null,
36
- isSuccess: true,
37
- });
38
- }
39
- catch (e) {
40
- const error = e;
41
- if (isDefinedError(error)) {
42
- return Object.assign([error, undefined, error, false], { error, data: undefined, definedError: error, isSuccess: false });
43
- }
44
- return Object.assign([error, undefined, null, false], {
45
- error: error,
46
- data: undefined,
47
- definedError: null,
48
- isSuccess: false,
49
- });
50
- }
13
+ * Works like try/catch, but help you infer the error type if it is a defined ORPCError.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * const [error, data, definedError, isSuccess] = await safe(client(...))
18
+ * // or const { error, data, definedError, isSuccess } = await safe(client(...))
19
+ *
20
+ * if (definedError) {
21
+ * console.log(definedError) // or error, both are well typed
22
+ * }
23
+ * ```
24
+ *
25
+ * @see {@link https://orpc.dev/docs/client/error-handling#using-safe-and-isdefinederror | Client Error Handling - Using safe and isDefinedError}
26
+ */
27
+ async function safe(promise) {
28
+ try {
29
+ const output = await promise;
30
+ return Object.assign([
31
+ null,
32
+ output,
33
+ null,
34
+ true
35
+ ], {
36
+ error: null,
37
+ data: output,
38
+ definedError: null,
39
+ isSuccess: true
40
+ });
41
+ } catch (e) {
42
+ const error = e;
43
+ if (isDefinedError(error)) return Object.assign([
44
+ error,
45
+ void 0,
46
+ error,
47
+ false
48
+ ], {
49
+ error,
50
+ data: void 0,
51
+ definedError: error,
52
+ isSuccess: false
53
+ });
54
+ return Object.assign([
55
+ error,
56
+ void 0,
57
+ null,
58
+ false
59
+ ], {
60
+ error,
61
+ data: void 0,
62
+ definedError: null,
63
+ isSuccess: false
64
+ });
65
+ }
51
66
  }
67
+ export { resolveClientRest, resolveFriendlyClientOptions, safe };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orpc/client",
3
- "version": "2.0.0-beta.49",
3
+ "version": "2.0.0-beta.50",
4
4
  "description": "Consume oRPC and OpenAPI APIs with end-to-end type safety over HTTP, WebSocket, or message ports",
5
5
  "keywords": [
6
6
  "api",
@@ -63,10 +63,10 @@
63
63
  }
64
64
  },
65
65
  "dependencies": {
66
- "@orpc/shared": "2.0.0-beta.49",
67
- "@standard-server/core": "~0.15.0",
68
- "@standard-server/fetch": "~0.15.0",
69
- "@standard-server/peer": "~0.15.0"
66
+ "@orpc/shared": "2.0.0-beta.50",
67
+ "@standard-server/core": "~0.16.0",
68
+ "@standard-server/fetch": "~0.16.0",
69
+ "@standard-server/peer": "~0.16.0"
70
70
  },
71
71
  "devDependencies": {
72
72
  "@types/ws": "^8.18.2",
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
package/dist/types.js DELETED
@@ -1 +0,0 @@
1
- export {};