@orpc/client 2.0.0-beta.47 → 2.0.0-beta.49
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/README.md +6 -7
- package/dist/adapters/fetch/index.d.ts +2 -48
- package/dist/adapters/fetch/index.js +2 -0
- package/dist/adapters/fetch/rpc-link.d.ts +14 -0
- package/dist/adapters/fetch/rpc-link.js +14 -0
- package/dist/adapters/fetch/transport.d.ts +34 -0
- package/dist/adapters/fetch/transport.js +65 -0
- package/dist/adapters/message-port/index.d.ts +3 -80
- package/dist/adapters/message-port/index.js +3 -0
- package/dist/adapters/message-port/message-port.d.ts +29 -0
- package/dist/adapters/message-port/message-port.js +56 -0
- package/dist/adapters/message-port/rpc-link.d.ts +15 -0
- package/dist/adapters/message-port/rpc-link.js +15 -0
- package/dist/adapters/message-port/transport.d.ts +38 -0
- package/dist/adapters/message-port/transport.js +52 -0
- package/dist/adapters/standard/codec.d.ts +14 -0
- package/dist/adapters/standard/codec.js +1 -0
- package/dist/adapters/standard/index.d.ts +5 -60
- package/dist/adapters/standard/index.js +4 -0
- package/dist/adapters/standard/link.d.ts +40 -0
- package/dist/adapters/standard/link.js +85 -0
- package/dist/adapters/standard/plugin.d.ts +32 -0
- package/dist/adapters/standard/plugin.js +16 -0
- package/dist/adapters/standard/{index.d.mts → rpc-link-codec.d.ts} +17 -14
- package/dist/adapters/standard/rpc-link-codec.js +104 -0
- package/dist/adapters/standard/transport.d.ts +14 -0
- package/dist/adapters/standard/transport.js +1 -0
- package/dist/adapters/websocket/index.d.ts +2 -136
- package/dist/adapters/websocket/index.js +2 -0
- package/dist/adapters/websocket/rpc-link.d.ts +14 -0
- package/dist/adapters/websocket/rpc-link.js +14 -0
- package/dist/adapters/websocket/{index.d.mts → transport.d.ts} +32 -53
- package/dist/adapters/websocket/transport.js +133 -0
- package/dist/async-iterator-object.d.ts +2 -0
- package/dist/async-iterator-object.js +29 -0
- package/dist/client-safe.d.ts +18 -0
- package/dist/client-safe.js +36 -0
- package/dist/client.d.ts +40 -0
- package/dist/client.js +39 -0
- package/dist/consts.d.ts +10 -0
- package/dist/consts.js +43 -0
- package/dist/dynamic-link.d.ts +13 -0
- package/dist/dynamic-link.js +17 -0
- package/dist/error-utils.d.ts +21 -0
- package/dist/error-utils.js +58 -0
- package/dist/error.d.ts +132 -0
- package/dist/error.js +178 -0
- package/dist/index.d.ts +38 -129
- package/dist/index.js +40 -0
- package/dist/plugins/batch.d.ts +185 -0
- package/dist/plugins/batch.js +401 -0
- package/dist/plugins/cookie-jar.d.ts +82 -0
- package/dist/plugins/cookie-jar.js +268 -0
- package/dist/plugins/dedupe.d.ts +64 -0
- package/dist/plugins/dedupe.js +226 -0
- package/dist/plugins/index.d.ts +20 -412
- package/dist/plugins/index.js +23 -0
- package/dist/plugins/request-compression.d.ts +44 -0
- package/dist/plugins/request-compression.js +178 -0
- package/dist/plugins/response-compression.d.ts +28 -0
- package/dist/plugins/response-compression.js +91 -0
- package/dist/plugins/retry-after.d.ts +50 -0
- package/dist/plugins/retry-after.js +88 -0
- package/dist/plugins/retry.d.ts +84 -0
- package/dist/plugins/retry.js +140 -0
- package/dist/plugins/timeout.d.ts +25 -0
- package/dist/plugins/timeout.js +37 -0
- package/dist/{shared/client.DqIWeNhQ.d.mts → rpc-json-serializer.d.ts} +9 -41
- package/dist/rpc-json-serializer.js +392 -0
- package/dist/rpc-serializer.d.ts +32 -0
- package/dist/rpc-serializer.js +130 -0
- package/dist/types.d.ts +97 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +47 -0
- package/dist/utils.js +51 -0
- package/package.json +43 -45
- package/dist/adapters/fetch/index.d.mts +0 -48
- package/dist/adapters/fetch/index.mjs +0 -77
- package/dist/adapters/message-port/index.d.mts +0 -80
- package/dist/adapters/message-port/index.mjs +0 -101
- package/dist/adapters/standard/index.mjs +0 -6
- package/dist/adapters/websocket/index.mjs +0 -131
- package/dist/index.d.mts +0 -144
- package/dist/index.mjs +0 -147
- package/dist/plugins/index.d.mts +0 -420
- package/dist/plugins/index.mjs +0 -971
- package/dist/shared/client.BoHJeq15.d.mts +0 -233
- package/dist/shared/client.BoHJeq15.d.ts +0 -233
- package/dist/shared/client.CKQeJJ2U.mjs +0 -172
- package/dist/shared/client.DqIWeNhQ.d.ts +0 -131
- package/dist/shared/client.JBJwBhR8.d.ts +0 -90
- package/dist/shared/client.LF04eOvL.mjs +0 -115
- package/dist/shared/client._wG5INmC.mjs +0 -505
- package/dist/shared/client.euYjjcFz.d.mts +0 -90
|
@@ -0,0 +1,130 @@
|
|
|
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';
|
|
6
|
+
/**
|
|
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*)$/;
|
|
113
|
+
function invalidFormat() {
|
|
114
|
+
return new TypeError('Invalid RPC serialized data format.');
|
|
115
|
+
}
|
|
116
|
+
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;
|
|
130
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { PromiseWithError } from '@orpc/shared';
|
|
2
|
+
export interface ClientContext {
|
|
3
|
+
[key: PropertyKey]: any;
|
|
4
|
+
}
|
|
5
|
+
export interface ClientOptions<T extends ClientContext> {
|
|
6
|
+
signal?: AbortSignal | undefined;
|
|
7
|
+
lastEventId?: string | undefined;
|
|
8
|
+
context: T;
|
|
9
|
+
}
|
|
10
|
+
export type FriendlyClientOptions<T extends ClientContext> = Omit<ClientOptions<T>, 'context'> & (object extends T ? {
|
|
11
|
+
context?: T | undefined;
|
|
12
|
+
} : {
|
|
13
|
+
context: T;
|
|
14
|
+
});
|
|
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
|
+
export interface Client<TClientContext extends ClientContext, TInput, TOutput, TError> {
|
|
17
|
+
(...rest: ClientRest<TClientContext, TInput>): PromiseWithError<TOutput, TError>;
|
|
18
|
+
}
|
|
19
|
+
export type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
|
|
20
|
+
[k: string]: NestedClient<TClientContext>;
|
|
21
|
+
};
|
|
22
|
+
export type AnyNestedClient = NestedClient<any>;
|
|
23
|
+
/**
|
|
24
|
+
* Infers the **client context type** required by a client.
|
|
25
|
+
*
|
|
26
|
+
* @see {@link https://orpc.dev/docs/client/client-side#infer-client-context | Client-Side Clients - Infer Client Context}
|
|
27
|
+
*/
|
|
28
|
+
export type InferClientContext<T extends AnyNestedClient> = T extends NestedClient<infer U> ? U : never;
|
|
29
|
+
export interface ClientLink<TClientContext extends ClientContext> {
|
|
30
|
+
call: (path: string[], input: unknown, options: ClientOptions<TClientContext>) => Promise<unknown>;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Recursively infers the **input types** from a client.
|
|
34
|
+
*
|
|
35
|
+
* Produces a nested map where each endpoint's input type is preserved.
|
|
36
|
+
*
|
|
37
|
+
* @see {@link https://orpc.dev/docs/client/client-side#infer-client-inputs | Client-Side Clients - Infer Client Inputs}
|
|
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
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Recursively infers the **body input types** from a client.
|
|
44
|
+
*
|
|
45
|
+
* If an endpoint's input includes `{ body: ... }`, only the `body` portion is extracted.
|
|
46
|
+
* Produces a nested map of body input types.
|
|
47
|
+
*
|
|
48
|
+
* @see {@link https://orpc.dev/docs/client/client-side#infer-client-body-inputs | Client-Side Clients - Infer Client Body Inputs}
|
|
49
|
+
*/
|
|
50
|
+
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
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Recursively infers the **output types** from a client.
|
|
57
|
+
*
|
|
58
|
+
* Produces a nested map where each endpoint's output type is preserved.
|
|
59
|
+
*
|
|
60
|
+
* @see {@link https://orpc.dev/docs/client/client-side#infer-client-outputs | Client-Side Clients - Infer Client Outputs}
|
|
61
|
+
*/
|
|
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
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Recursively infers the **body output types** from a client.
|
|
67
|
+
*
|
|
68
|
+
* If an endpoint's output includes `{ body: ... }`, only the `body` portion is extracted.
|
|
69
|
+
* Produces a nested map of body output types.
|
|
70
|
+
*
|
|
71
|
+
* @see {@link https://orpc.dev/docs/client/client-side#infer-client-body-outputs | Client-Side Clients - Infer Client Body Outputs}
|
|
72
|
+
*/
|
|
73
|
+
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
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Recursively infers the **error types** from a client when you use [type-safe errors](https://orpc.dev/docs/error-handling#typesafe-errors).
|
|
80
|
+
*
|
|
81
|
+
* Produces a nested map where each endpoint's error type is preserved.
|
|
82
|
+
*
|
|
83
|
+
* @see {@link https://orpc.dev/docs/client/client-side#infer-client-errors | Client-Side Clients - Infer Client Errors}
|
|
84
|
+
*/
|
|
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
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* 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
|
+
*
|
|
91
|
+
* Useful when you want to handle all possible errors from any endpoint at once.
|
|
92
|
+
*
|
|
93
|
+
* @see {@link https://orpc.dev/docs/client/client-side#infer-client-error | Client-Side Clients - Infer Client Error}
|
|
94
|
+
*/
|
|
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];
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
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>;
|
|
5
|
+
export declare function resolveClientRest<TClientContext extends ClientContext, TInput>(rest: ClientRest<TClientContext, TInput>): [input: TInput, options: ClientOptions<TClientContext>];
|
|
6
|
+
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;
|
|
31
|
+
});
|
|
32
|
+
/**
|
|
33
|
+
* Works like try/catch, but help you infer the error type if it is a defined ORPCError.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* const [error, data, definedError, isSuccess] = await safe(client(...))
|
|
38
|
+
* // or const { error, data, definedError, isSuccess } = await safe(client(...))
|
|
39
|
+
*
|
|
40
|
+
* if (definedError) {
|
|
41
|
+
* console.log(definedError) // or error, both are well typed
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @see {@link https://orpc.dev/docs/client/error-handling#using-safe-and-isdefinederror | Client Error Handling - Using safe and isDefinedError}
|
|
46
|
+
*/
|
|
47
|
+
export declare function safe<TOutput, TError = ThrowableError>(promise: PromiseWithError<TOutput, TError>): Promise<SafeResult<TOutput, TError>>;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
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
|
+
};
|
|
7
|
+
}
|
|
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
|
+
];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
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
|
+
}
|
|
51
|
+
}
|
package/package.json
CHANGED
|
@@ -1,79 +1,77 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/client",
|
|
3
|
-
"
|
|
4
|
-
"version": "2.0.0-beta.47",
|
|
3
|
+
"version": "2.0.0-beta.49",
|
|
5
4
|
"description": "Consume oRPC and OpenAPI APIs with end-to-end type safety over HTTP, WebSocket, or message ports",
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"api",
|
|
7
|
+
"client",
|
|
8
|
+
"fetch",
|
|
9
|
+
"openapi",
|
|
10
|
+
"orpc",
|
|
11
|
+
"rpc",
|
|
12
|
+
"typesafe",
|
|
13
|
+
"typescript",
|
|
14
|
+
"websocket"
|
|
10
15
|
],
|
|
11
16
|
"homepage": "https://orpc.dev",
|
|
17
|
+
"license": "MIT",
|
|
12
18
|
"repository": {
|
|
13
19
|
"type": "git",
|
|
14
20
|
"url": "git+https://github.com/middleapi/orpc.git",
|
|
15
21
|
"directory": "packages/client"
|
|
16
22
|
},
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"
|
|
23
|
-
"client",
|
|
24
|
-
"fetch",
|
|
25
|
-
"websocket",
|
|
26
|
-
"openapi"
|
|
23
|
+
"funding": [
|
|
24
|
+
"https://github.com/sponsors/dinwwwh",
|
|
25
|
+
"https://opencollective.com/middleapi"
|
|
26
|
+
],
|
|
27
|
+
"files": [
|
|
28
|
+
"dist"
|
|
27
29
|
],
|
|
30
|
+
"type": "module",
|
|
28
31
|
"sideEffects": false,
|
|
29
32
|
"exports": {
|
|
30
33
|
"./package.json": "./package.json",
|
|
31
34
|
".": {
|
|
32
|
-
"types": "./dist/index.d.
|
|
33
|
-
"import": "./dist/index.
|
|
34
|
-
"default": "./dist/index.
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
36
|
+
"import": "./dist/index.js",
|
|
37
|
+
"default": "./dist/index.js"
|
|
35
38
|
},
|
|
36
39
|
"./plugins": {
|
|
37
|
-
"types": "./dist/plugins/index.d.
|
|
38
|
-
"import": "./dist/plugins/index.
|
|
39
|
-
"default": "./dist/plugins/index.
|
|
40
|
+
"types": "./dist/plugins/index.d.ts",
|
|
41
|
+
"import": "./dist/plugins/index.js",
|
|
42
|
+
"default": "./dist/plugins/index.js"
|
|
40
43
|
},
|
|
41
44
|
"./standard": {
|
|
42
|
-
"types": "./dist/adapters/standard/index.d.
|
|
43
|
-
"import": "./dist/adapters/standard/index.
|
|
44
|
-
"default": "./dist/adapters/standard/index.
|
|
45
|
+
"types": "./dist/adapters/standard/index.d.ts",
|
|
46
|
+
"import": "./dist/adapters/standard/index.js",
|
|
47
|
+
"default": "./dist/adapters/standard/index.js"
|
|
45
48
|
},
|
|
46
49
|
"./fetch": {
|
|
47
|
-
"types": "./dist/adapters/fetch/index.d.
|
|
48
|
-
"import": "./dist/adapters/fetch/index.
|
|
49
|
-
"default": "./dist/adapters/fetch/index.
|
|
50
|
+
"types": "./dist/adapters/fetch/index.d.ts",
|
|
51
|
+
"import": "./dist/adapters/fetch/index.js",
|
|
52
|
+
"default": "./dist/adapters/fetch/index.js"
|
|
50
53
|
},
|
|
51
54
|
"./websocket": {
|
|
52
|
-
"types": "./dist/adapters/websocket/index.d.
|
|
53
|
-
"import": "./dist/adapters/websocket/index.
|
|
54
|
-
"default": "./dist/adapters/websocket/index.
|
|
55
|
+
"types": "./dist/adapters/websocket/index.d.ts",
|
|
56
|
+
"import": "./dist/adapters/websocket/index.js",
|
|
57
|
+
"default": "./dist/adapters/websocket/index.js"
|
|
55
58
|
},
|
|
56
59
|
"./message-port": {
|
|
57
|
-
"types": "./dist/adapters/message-port/index.d.
|
|
58
|
-
"import": "./dist/adapters/message-port/index.
|
|
59
|
-
"default": "./dist/adapters/message-port/index.
|
|
60
|
+
"types": "./dist/adapters/message-port/index.d.ts",
|
|
61
|
+
"import": "./dist/adapters/message-port/index.js",
|
|
62
|
+
"default": "./dist/adapters/message-port/index.js"
|
|
60
63
|
}
|
|
61
64
|
},
|
|
62
|
-
"files": [
|
|
63
|
-
"dist"
|
|
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.49",
|
|
67
|
+
"@standard-server/core": "~0.15.0",
|
|
68
|
+
"@standard-server/fetch": "~0.15.0",
|
|
69
|
+
"@standard-server/peer": "~0.15.0"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
72
|
"@types/ws": "^8.18.2",
|
|
73
|
-
"ws": "^8.22.
|
|
73
|
+
"ws": "^8.22.1",
|
|
74
74
|
"zod": "^4.6.7"
|
|
75
75
|
},
|
|
76
|
-
"scripts": {
|
|
77
|
-
"type:check": "tsc -b"
|
|
78
|
-
}
|
|
76
|
+
"scripts": {}
|
|
79
77
|
}
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { ClientContext, ClientOptions } from '../../index.mjs';
|
|
2
|
-
import { Value, Promisable } from '@orpc/shared';
|
|
3
|
-
import { StandardRequest, StandardLazyResponse } from '@standard-server/core';
|
|
4
|
-
import { ToFetchBodyOptions } from '@standard-server/fetch';
|
|
5
|
-
import { StandardLinkTransport, StandardLink, StandardLinkOptions } from '../standard/index.mjs';
|
|
6
|
-
import { RPCLinkCodecOptions } from '../standard/index.mjs';
|
|
7
|
-
import '../../shared/client.DqIWeNhQ.mjs';
|
|
8
|
-
|
|
9
|
-
interface FetchLinkTransportOptions<T extends ClientContext> {
|
|
10
|
-
/**
|
|
11
|
-
* The origin to prepend to all request URLs, useful for CORS requests.
|
|
12
|
-
*
|
|
13
|
-
* @example 'https://api.example.com'
|
|
14
|
-
* @example 'http://localhost:3000'
|
|
15
|
-
*/
|
|
16
|
-
origin?: Value<Promisable<`https://${string}` | `http://${string}` | ({} & string) | undefined>, [options: ClientOptions<T>, path: string[]]> | undefined;
|
|
17
|
-
/**
|
|
18
|
-
* Options for how to convert the Standard Request to a Fetch Request, like event stream options, etc.
|
|
19
|
-
*/
|
|
20
|
-
toFetchRequest?: ToFetchBodyOptions | undefined;
|
|
21
|
-
/**
|
|
22
|
-
* Override the default fetch implementation.
|
|
23
|
-
*
|
|
24
|
-
* @default (url, init) => globalThis.fetch(url, init)
|
|
25
|
-
*/
|
|
26
|
-
fetch?(url: string, init: RequestInit, options: ClientOptions<T>, path: string[]): Promise<Response>;
|
|
27
|
-
}
|
|
28
|
-
declare class FetchLinkTransport<T extends ClientContext> implements StandardLinkTransport<T> {
|
|
29
|
-
private readonly origin;
|
|
30
|
-
private readonly fetch;
|
|
31
|
-
private readonly toFetchRequestOptions;
|
|
32
|
-
constructor(options: FetchLinkTransportOptions<T>);
|
|
33
|
-
send(standardRequest: StandardRequest, path: string[], options: ClientOptions<T>): Promise<StandardLazyResponse>;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
interface RPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, FetchLinkTransportOptions<T>, RPCLinkCodecOptions<T> {
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Client link that communicates with an RPC Handler over the Fetch API (HTTP).
|
|
40
|
-
*
|
|
41
|
-
* @see {@link https://orpc.dev/docs/adapters/fetch-api | Fetch API Adapter}
|
|
42
|
-
*/
|
|
43
|
-
declare class RPCLink<T extends ClientContext> extends StandardLink<T> {
|
|
44
|
-
constructor(options: RPCLinkOptions<T>);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export { FetchLinkTransport, RPCLink };
|
|
48
|
-
export type { FetchLinkTransportOptions, RPCLinkOptions };
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { value, once } from '@orpc/shared';
|
|
2
|
-
import { toFetchBody, toFetchHeaders, toStandardLazyResponse } from '@standard-server/fetch';
|
|
3
|
-
import { S as StandardLink, R as RPCLinkCodec } from '../../shared/client.CKQeJJ2U.mjs';
|
|
4
|
-
import '@standard-server/core';
|
|
5
|
-
import '../../shared/client.LF04eOvL.mjs';
|
|
6
|
-
import '../../shared/client._wG5INmC.mjs';
|
|
7
|
-
|
|
8
|
-
const GET_SUPPORTED_DUPLEX_MODE = once(() => {
|
|
9
|
-
try {
|
|
10
|
-
let duplex;
|
|
11
|
-
void new Request(
|
|
12
|
-
"https://example.com",
|
|
13
|
-
{
|
|
14
|
-
method: "POST",
|
|
15
|
-
body: new ReadableStream(),
|
|
16
|
-
get duplex() {
|
|
17
|
-
duplex = "half";
|
|
18
|
-
return "half";
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
);
|
|
22
|
-
return duplex;
|
|
23
|
-
} catch {
|
|
24
|
-
return void 0;
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
class FetchLinkTransport {
|
|
28
|
-
origin;
|
|
29
|
-
fetch;
|
|
30
|
-
toFetchRequestOptions;
|
|
31
|
-
constructor(options) {
|
|
32
|
-
this.origin = options.origin;
|
|
33
|
-
this.fetch = options.fetch ?? ((url, init) => globalThis.fetch(url, init));
|
|
34
|
-
this.toFetchRequestOptions = options.toFetchRequest;
|
|
35
|
-
}
|
|
36
|
-
async send(standardRequest, path, options) {
|
|
37
|
-
let origin = await value(this.origin, options, path);
|
|
38
|
-
if (origin?.endsWith("/")) {
|
|
39
|
-
origin = origin.slice(0, -1);
|
|
40
|
-
}
|
|
41
|
-
const url = `${origin ?? ""}${standardRequest.url}`;
|
|
42
|
-
const [body, standardHeaders] = toFetchBody(standardRequest.body, standardRequest.headers, this.toFetchRequestOptions);
|
|
43
|
-
const init = {
|
|
44
|
-
body,
|
|
45
|
-
headers: toFetchHeaders(standardHeaders),
|
|
46
|
-
method: standardRequest.method,
|
|
47
|
-
signal: options.signal,
|
|
48
|
-
redirect: "manual"
|
|
49
|
-
};
|
|
50
|
-
if (body instanceof ReadableStream) {
|
|
51
|
-
const duplex = GET_SUPPORTED_DUPLEX_MODE();
|
|
52
|
-
if (duplex !== void 0) {
|
|
53
|
-
init.duplex = duplex;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
const fetch = this.fetch;
|
|
57
|
-
const response = await fetch(url, init, options, path);
|
|
58
|
-
if (response.type === "opaqueredirect") {
|
|
59
|
-
throw new TypeError("Unexpected redirect response: redirects are not followed.");
|
|
60
|
-
}
|
|
61
|
-
if (response.type === "opaque") {
|
|
62
|
-
throw new TypeError("Unexpected opaque response: the response is unreadable, likely due to a no-cors request.");
|
|
63
|
-
}
|
|
64
|
-
const standardResponse = toStandardLazyResponse(response);
|
|
65
|
-
return standardResponse;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
class RPCLink extends StandardLink {
|
|
70
|
-
constructor(options) {
|
|
71
|
-
const codec = new RPCLinkCodec(options);
|
|
72
|
-
const transport = new FetchLinkTransport(options);
|
|
73
|
-
super(codec, transport, options);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export { FetchLinkTransport, RPCLink };
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { ClientContext, ClientOptions } from '../../index.mjs';
|
|
2
|
-
import { Value, Promisable } from '@orpc/shared';
|
|
3
|
-
import { StandardRequest, StandardLazyResponse } from '@standard-server/core';
|
|
4
|
-
import { ClientPeer, EncodePeerMessageOptions, DecodePeerMessageOptions } from '@standard-server/peer';
|
|
5
|
-
import { StandardLinkTransport, StandardLink, StandardLinkOptions } from '../standard/index.mjs';
|
|
6
|
-
import { RPCLinkCodecOptions } from '../standard/index.mjs';
|
|
7
|
-
import '../../shared/client.DqIWeNhQ.mjs';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* The message port used by electron in main process
|
|
11
|
-
*/
|
|
12
|
-
interface MessagePortMainLike {
|
|
13
|
-
on: <T extends string>(event: T, callback: (event?: {
|
|
14
|
-
data: any;
|
|
15
|
-
}) => void) => void;
|
|
16
|
-
postMessage: (data: any, transfer?: any[]) => void;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* The message port used by browser extension
|
|
20
|
-
*/
|
|
21
|
-
interface BrowserPortLike {
|
|
22
|
-
onMessage: {
|
|
23
|
-
addListener: (callback: (data: any) => void) => void;
|
|
24
|
-
};
|
|
25
|
-
onDisconnect: {
|
|
26
|
-
addListener: (callback: () => void) => void;
|
|
27
|
-
};
|
|
28
|
-
postMessage: (data: any) => void;
|
|
29
|
-
}
|
|
30
|
-
type SupportedMessagePort = Pick<MessagePort, 'addEventListener' | 'postMessage'> | MessagePortMainLike | BrowserPortLike;
|
|
31
|
-
/**
|
|
32
|
-
* Message port can support [The structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)
|
|
33
|
-
*/
|
|
34
|
-
type SupportedMessagePortData = any;
|
|
35
|
-
declare function postMessagePortMessage(port: SupportedMessagePort, data: SupportedMessagePortData, transfer?: any[]): void;
|
|
36
|
-
declare function onMessagePortMessage(port: SupportedMessagePort, callback: (data: SupportedMessagePortData) => void): void;
|
|
37
|
-
declare function onMessagePortClose(port: SupportedMessagePort, callback: () => void): void;
|
|
38
|
-
|
|
39
|
-
type DecodedRequestMessage = ConstructorParameters<typeof ClientPeer>[0] extends (message: infer TMessage) => unknown ? TMessage : never;
|
|
40
|
-
interface MessagePortLinkTransportOptions<_T extends ClientContext> {
|
|
41
|
-
port: SupportedMessagePort;
|
|
42
|
-
/**
|
|
43
|
-
* By default, oRPC serializes request/response messages to string/binary data before sending over message port.
|
|
44
|
-
* If needed, define this option to utilize full power of [MessagePort: postMessage() method](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort/postMessage),
|
|
45
|
-
* such as transferring ownership of objects to the other side or support unserializable objects like `OffscreenCanvas`.
|
|
46
|
-
*
|
|
47
|
-
* @remarks
|
|
48
|
-
* **Note**: Returning `null` or `undefined` disables this feature.
|
|
49
|
-
* **Warning**: Make sure your message port supports `transfer` before using this feature.
|
|
50
|
-
*/
|
|
51
|
-
experimental_transfer?: Value<Promisable<object[] | null | undefined>, [message: DecodedRequestMessage, port: SupportedMessagePort]> | undefined;
|
|
52
|
-
/**
|
|
53
|
-
* Options for encoding peer messages. such as `prefix` for distinguishing messages on the same channel..
|
|
54
|
-
*/
|
|
55
|
-
encodePeerMessage?: EncodePeerMessageOptions | undefined;
|
|
56
|
-
/**
|
|
57
|
-
* Options for decoding peer messages, such as `prefix` for distinguishing messages on the same channel.
|
|
58
|
-
*/
|
|
59
|
-
decodePeerMessage?: DecodePeerMessageOptions | undefined;
|
|
60
|
-
}
|
|
61
|
-
declare class MessagePortLinkTransport<T extends ClientContext> implements StandardLinkTransport<T> {
|
|
62
|
-
private readonly peer;
|
|
63
|
-
constructor({ port, experimental_transfer, encodePeerMessage: encodePeerMessageOptions, decodePeerMessage: decodePeerMessageOptions }: MessagePortLinkTransportOptions<T>);
|
|
64
|
-
send(standardRequest: StandardRequest, _path: string[], _options: ClientOptions<T>): Promise<StandardLazyResponse>;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
interface RPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, MessagePortLinkTransportOptions<T>, RPCLinkCodecOptions<T> {
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Client link that communicates with an RPC Handler over a Message Port
|
|
71
|
-
* (e.g. workers, iframes, browser extensions, Electron).
|
|
72
|
-
*
|
|
73
|
-
* @see {@link https://orpc.dev/docs/adapters/message-port | Message Port Adapter}
|
|
74
|
-
*/
|
|
75
|
-
declare class RPCLink<T extends ClientContext> extends StandardLink<T> {
|
|
76
|
-
constructor(options: RPCLinkOptions<T>);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export { MessagePortLinkTransport, RPCLink, onMessagePortClose, onMessagePortMessage, postMessagePortMessage };
|
|
80
|
-
export type { BrowserPortLike, MessagePortLinkTransportOptions, MessagePortMainLike, RPCLinkOptions, SupportedMessagePort, SupportedMessagePortData };
|