@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.
- package/dist/adapters/fetch/index.d.ts +3 -2
- package/dist/adapters/fetch/index.js +2 -2
- package/dist/adapters/fetch/rpc-link.d.ts +8 -8
- package/dist/adapters/fetch/rpc-link.js +16 -13
- package/dist/adapters/fetch/transport.d.ts +29 -31
- package/dist/adapters/fetch/transport.js +49 -63
- package/dist/adapters/message-port/index.d.ts +4 -3
- package/dist/adapters/message-port/index.js +3 -3
- package/dist/adapters/message-port/message-port.d.ts +13 -13
- package/dist/adapters/message-port/message-port.js +35 -53
- package/dist/adapters/message-port/rpc-link.d.ts +8 -8
- package/dist/adapters/message-port/rpc-link.js +17 -14
- package/dist/adapters/message-port/transport.d.ts +31 -34
- package/dist/adapters/message-port/transport.js +38 -52
- package/dist/adapters/standard/codec.d.ts +10 -10
- package/dist/adapters/standard/index.d.ts +6 -5
- package/dist/adapters/standard/index.js +2 -4
- package/dist/adapters/standard/link.d.ts +31 -31
- package/dist/adapters/standard/link.js +101 -85
- package/dist/adapters/standard/plugin.d.ts +25 -31
- package/dist/adapters/standard/plugin.js +14 -16
- package/dist/adapters/standard/rpc-link-codec.d.ts +53 -60
- package/dist/adapters/standard/rpc-link-codec.js +98 -102
- package/dist/adapters/standard/transport.d.ts +7 -7
- package/dist/adapters/websocket/index.d.ts +3 -2
- package/dist/adapters/websocket/index.js +2 -2
- package/dist/adapters/websocket/rpc-link.d.ts +8 -8
- package/dist/adapters/websocket/rpc-link.js +16 -13
- package/dist/adapters/websocket/transport.d.ts +101 -100
- package/dist/adapters/websocket/transport.js +102 -130
- package/dist/async-iterator-object.d.ts +2 -2
- package/dist/async-iterator-object.js +27 -28
- package/dist/client-safe.d.ts +4 -6
- package/dist/client-safe.js +29 -34
- package/dist/client.d.ts +24 -26
- package/dist/client.js +35 -37
- package/dist/consts.d.ts +1 -1
- package/dist/consts.js +18 -41
- package/dist/dynamic-link.d.ts +6 -6
- package/dist/dynamic-link.js +16 -16
- package/dist/error-utils.d.ts +3 -4
- package/dist/error-utils.js +47 -51
- package/dist/error.d.ts +79 -79
- package/dist/error.js +154 -166
- package/dist/index.d.ts +14 -53
- package/dist/index.js +12 -40
- package/dist/plugins/batch.d.ts +122 -155
- package/dist/plugins/batch.js +320 -391
- package/dist/plugins/cookie-jar.d.ts +64 -61
- package/dist/plugins/cookie-jar.js +208 -245
- package/dist/plugins/dedupe.d.ts +49 -53
- package/dist/plugins/dedupe.js +168 -211
- package/dist/plugins/index.d.ts +9 -28
- package/dist/plugins/index.js +8 -23
- package/dist/plugins/request-compression.d.ts +38 -35
- package/dist/plugins/request-compression.js +131 -177
- package/dist/plugins/response-compression.d.ts +21 -18
- package/dist/plugins/response-compression.js +64 -88
- package/dist/plugins/retry-after.d.ts +31 -38
- package/dist/plugins/retry-after.js +60 -85
- package/dist/plugins/retry.d.ts +55 -55
- package/dist/plugins/retry.js +118 -137
- package/dist/plugins/timeout.d.ts +19 -17
- package/dist/plugins/timeout.js +41 -36
- package/dist/rpc-json-serializer.d.ts +84 -84
- package/dist/rpc-json-serializer.js +338 -383
- package/dist/rpc-serializer.d.ts +20 -21
- package/dist/rpc-serializer.js +97 -125
- package/dist/types.d.ts +19 -31
- package/dist/utils.d.ts +18 -29
- package/dist/utils.js +63 -47
- package/package.json +5 -5
- package/dist/adapters/standard/codec.js +0 -1
- package/dist/adapters/standard/transport.js +0 -1
- package/dist/types.js +0 -1
package/dist/rpc-serializer.d.ts
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
+
}
|
package/dist/rpc-serializer.js
CHANGED
|
@@ -1,130 +1,102 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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
|
-
|
|
94
|
+
return /* @__PURE__ */ new TypeError("Invalid RPC serialized data format.");
|
|
115
95
|
}
|
|
116
96
|
function assertEnvelope(value, allowMaps) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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
|
|
1
|
+
import { PromiseWithError } from "@orpc/shared";
|
|
2
2
|
export interface ClientContext {
|
|
3
|
-
|
|
3
|
+
[key: PropertyKey]: any;
|
|
4
4
|
}
|
|
5
5
|
export interface ClientOptions<T extends ClientContext> {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
signal?: AbortSignal | undefined;
|
|
7
|
+
lastEventId?: string | undefined;
|
|
8
|
+
context: T;
|
|
9
9
|
}
|
|
10
|
-
export type FriendlyClientOptions<T extends ClientContext> = Omit<ClientOptions<T>,
|
|
11
|
-
|
|
10
|
+
export type FriendlyClientOptions<T extends ClientContext> = Omit<ClientOptions<T>, "context"> & (object extends T ? {
|
|
11
|
+
context?: T | undefined;
|
|
12
12
|
} : {
|
|
13
|
-
|
|
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
|
-
|
|
17
|
+
(...rest: ClientRest<TClientContext, TInput>): PromiseWithError<TOutput, TError>;
|
|
18
18
|
}
|
|
19
19
|
export type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
|
|
20
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
2
|
-
import
|
|
3
|
-
import
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}) | ([
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
] & {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
9
|
-
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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.
|
|
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.
|
|
67
|
-
"@standard-server/core": "~0.
|
|
68
|
-
"@standard-server/fetch": "~0.
|
|
69
|
-
"@standard-server/peer": "~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 {};
|