@orpc/client 2.0.0-beta.47 → 2.0.0-beta.48
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 +2 -3
- package/dist/adapters/fetch/index.d.mts +2 -48
- package/dist/adapters/fetch/index.mjs +2 -77
- package/dist/adapters/fetch/rpc-link.d.mts +13 -0
- package/dist/adapters/fetch/rpc-link.mjs +10 -0
- package/dist/adapters/fetch/transport.d.mts +31 -0
- package/dist/adapters/fetch/transport.mjs +65 -0
- package/dist/adapters/message-port/index.d.mts +3 -80
- package/dist/adapters/message-port/index.mjs +3 -101
- package/dist/adapters/message-port/message-port.d.mts +29 -0
- package/dist/adapters/message-port/message-port.mjs +43 -0
- package/dist/adapters/message-port/rpc-link.d.mts +14 -0
- package/dist/adapters/message-port/rpc-link.mjs +10 -0
- package/dist/adapters/message-port/transport.d.mts +35 -0
- package/dist/adapters/message-port/transport.mjs +47 -0
- package/dist/adapters/standard/codec.d.mts +14 -0
- package/dist/adapters/standard/codec.mjs +1 -0
- package/dist/adapters/standard/index.d.mts +5 -60
- package/dist/adapters/standard/index.mjs +4 -6
- package/dist/adapters/standard/link.d.mts +40 -0
- package/dist/adapters/standard/link.mjs +79 -0
- package/dist/adapters/standard/plugin.d.mts +32 -0
- package/dist/adapters/standard/plugin.mjs +16 -0
- package/dist/adapters/standard/rpc-link-codec.d.mts +55 -0
- package/dist/adapters/standard/rpc-link-codec.mjs +104 -0
- package/dist/adapters/standard/transport.d.mts +14 -0
- package/dist/adapters/standard/transport.mjs +1 -0
- package/dist/adapters/websocket/index.d.mts +2 -136
- package/dist/adapters/websocket/index.mjs +2 -131
- package/dist/adapters/websocket/rpc-link.d.mts +13 -0
- package/dist/adapters/websocket/rpc-link.mjs +10 -0
- package/dist/adapters/websocket/transport.d.mts +119 -0
- package/dist/adapters/websocket/transport.mjs +123 -0
- package/dist/async-iterator-object.d.mts +7 -0
- package/dist/async-iterator-object.mjs +30 -0
- package/dist/client-safe.d.mts +16 -0
- package/dist/client-safe.mjs +23 -0
- package/dist/client.d.mts +51 -0
- package/dist/client.mjs +32 -0
- package/dist/consts.d.mts +10 -0
- package/dist/consts.mjs +10 -0
- package/dist/dynamic-link.d.mts +13 -0
- package/dist/dynamic-link.mjs +12 -0
- package/dist/error-utils.d.mts +24 -0
- package/dist/error-utils.mjs +44 -0
- package/dist/error.d.mts +138 -0
- package/dist/error.mjs +105 -0
- package/dist/index.d.mts +42 -137
- package/dist/index.mjs +18 -142
- package/dist/plugins/batch.d.mts +150 -0
- package/dist/plugins/batch.mjs +339 -0
- package/dist/plugins/cookie-jar.d.mts +82 -0
- package/dist/plugins/cookie-jar.mjs +229 -0
- package/dist/plugins/dedupe.d.mts +58 -0
- package/dist/plugins/dedupe.mjs +199 -0
- package/dist/plugins/index.d.mts +28 -420
- package/dist/plugins/index.mjs +17 -971
- package/dist/plugins/request-compression.d.mts +44 -0
- package/dist/plugins/request-compression.mjs +150 -0
- package/dist/plugins/response-compression.d.mts +28 -0
- package/dist/plugins/response-compression.mjs +78 -0
- package/dist/plugins/retry-after.d.mts +41 -0
- package/dist/plugins/retry-after.mjs +71 -0
- package/dist/plugins/retry.d.mts +82 -0
- package/dist/plugins/retry.mjs +127 -0
- package/dist/plugins/timeout.d.mts +25 -0
- package/dist/plugins/timeout.mjs +36 -0
- package/dist/rpc-json-serializer.d.mts +99 -0
- package/dist/rpc-json-serializer.mjs +365 -0
- package/dist/rpc-serializer.d.mts +32 -0
- package/dist/rpc-serializer.mjs +100 -0
- package/dist/types.d.mts +93 -0
- package/dist/types.mjs +1 -0
- package/dist/utils.d.mts +46 -0
- package/dist/utils.mjs +53 -0
- package/package.json +5 -5
- package/dist/adapters/fetch/index.d.ts +0 -48
- package/dist/adapters/message-port/index.d.ts +0 -80
- package/dist/adapters/standard/index.d.ts +0 -60
- package/dist/adapters/websocket/index.d.ts +0 -136
- package/dist/index.d.ts +0 -144
- package/dist/plugins/index.d.ts +0 -420
- 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.mts +0 -131
- 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
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
import { PromiseWithError, Registry, MaybeOptionalOptions } from '@orpc/shared';
|
|
2
|
-
import { StandardResponse } from '@standard-server/core';
|
|
3
|
-
|
|
4
|
-
interface ClientContext {
|
|
5
|
-
[key: PropertyKey]: any;
|
|
6
|
-
}
|
|
7
|
-
interface ClientOptions<T extends ClientContext> {
|
|
8
|
-
signal?: AbortSignal | undefined;
|
|
9
|
-
lastEventId?: string | undefined;
|
|
10
|
-
context: T;
|
|
11
|
-
}
|
|
12
|
-
type FriendlyClientOptions<T extends ClientContext> = Omit<ClientOptions<T>, 'context'> & (object extends T ? {
|
|
13
|
-
context?: T | undefined;
|
|
14
|
-
} : {
|
|
15
|
-
context: T;
|
|
16
|
-
});
|
|
17
|
-
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>];
|
|
18
|
-
interface Client<TClientContext extends ClientContext, TInput, TOutput, TError> {
|
|
19
|
-
(...rest: ClientRest<TClientContext, TInput>): PromiseWithError<TOutput, TError>;
|
|
20
|
-
}
|
|
21
|
-
type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
|
|
22
|
-
[k: string]: NestedClient<TClientContext>;
|
|
23
|
-
};
|
|
24
|
-
type AnyNestedClient = NestedClient<any>;
|
|
25
|
-
/**
|
|
26
|
-
* Infers the **client context type** required by a client.
|
|
27
|
-
*
|
|
28
|
-
* @see {@link https://orpc.dev/docs/client/client-side#infer-client-context | Client-Side Clients - Infer Client Context}
|
|
29
|
-
*/
|
|
30
|
-
type InferClientContext<T extends AnyNestedClient> = T extends NestedClient<infer U> ? U : never;
|
|
31
|
-
interface ClientLink<TClientContext extends ClientContext> {
|
|
32
|
-
call: (path: string[], input: unknown, options: ClientOptions<TClientContext>) => Promise<unknown>;
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Recursively infers the **input types** from a client.
|
|
36
|
-
*
|
|
37
|
-
* Produces a nested map where each endpoint's input type is preserved.
|
|
38
|
-
*
|
|
39
|
-
* @see {@link https://orpc.dev/docs/client/client-side#infer-client-inputs | Client-Side Clients - Infer Client Inputs}
|
|
40
|
-
*/
|
|
41
|
-
type InferClientInputs<T extends AnyNestedClient> = T extends Client<any, infer U, any, any> ? U : {
|
|
42
|
-
[K in keyof T]: T[K] extends AnyNestedClient ? InferClientInputs<T[K]> : never;
|
|
43
|
-
};
|
|
44
|
-
/**
|
|
45
|
-
* Recursively infers the **body input types** from a client.
|
|
46
|
-
*
|
|
47
|
-
* If an endpoint's input includes `{ body: ... }`, only the `body` portion is extracted.
|
|
48
|
-
* Produces a nested map of body input types.
|
|
49
|
-
*
|
|
50
|
-
* @see {@link https://orpc.dev/docs/client/client-side#infer-client-body-inputs | Client-Side Clients - Infer Client Body Inputs}
|
|
51
|
-
*/
|
|
52
|
-
type InferClientBodyInputs<T extends AnyNestedClient> = T extends Client<any, infer U, any, any> ? U extends {
|
|
53
|
-
body: infer UBody;
|
|
54
|
-
} ? UBody : U : {
|
|
55
|
-
[K in keyof T]: T[K] extends AnyNestedClient ? InferClientBodyInputs<T[K]> : never;
|
|
56
|
-
};
|
|
57
|
-
/**
|
|
58
|
-
* Recursively infers the **output types** from a client.
|
|
59
|
-
*
|
|
60
|
-
* Produces a nested map where each endpoint's output type is preserved.
|
|
61
|
-
*
|
|
62
|
-
* @see {@link https://orpc.dev/docs/client/client-side#infer-client-outputs | Client-Side Clients - Infer Client Outputs}
|
|
63
|
-
*/
|
|
64
|
-
type InferClientOutputs<T extends AnyNestedClient> = T extends Client<any, any, infer U, any> ? U : {
|
|
65
|
-
[K in keyof T]: T[K] extends AnyNestedClient ? InferClientOutputs<T[K]> : never;
|
|
66
|
-
};
|
|
67
|
-
/**
|
|
68
|
-
* Recursively infers the **body output types** from a client.
|
|
69
|
-
*
|
|
70
|
-
* If an endpoint's output includes `{ body: ... }`, only the `body` portion is extracted.
|
|
71
|
-
* Produces a nested map of body output types.
|
|
72
|
-
*
|
|
73
|
-
* @see {@link https://orpc.dev/docs/client/client-side#infer-client-body-outputs | Client-Side Clients - Infer Client Body Outputs}
|
|
74
|
-
*/
|
|
75
|
-
type InferClientBodyOutputs<T extends AnyNestedClient> = T extends Client<any, any, infer U, any> ? U extends {
|
|
76
|
-
body: infer UBody;
|
|
77
|
-
} ? UBody : U : {
|
|
78
|
-
[K in keyof T]: T[K] extends AnyNestedClient ? InferClientBodyOutputs<T[K]> : never;
|
|
79
|
-
};
|
|
80
|
-
/**
|
|
81
|
-
* Recursively infers the **error types** from a client when you use [type-safe errors](https://orpc.dev/docs/error-handling#typesafe-errors).
|
|
82
|
-
*
|
|
83
|
-
* Produces a nested map where each endpoint's error type is preserved.
|
|
84
|
-
*
|
|
85
|
-
* @see {@link https://orpc.dev/docs/client/client-side#infer-client-errors | Client-Side Clients - Infer Client Errors}
|
|
86
|
-
*/
|
|
87
|
-
type InferClientErrors<T extends AnyNestedClient> = T extends Client<any, any, any, infer U> ? U : {
|
|
88
|
-
[K in keyof T]: T[K] extends AnyNestedClient ? InferClientErrors<T[K]> : never;
|
|
89
|
-
};
|
|
90
|
-
/**
|
|
91
|
-
* 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).
|
|
92
|
-
*
|
|
93
|
-
* Useful when you want to handle all possible errors from any endpoint at once.
|
|
94
|
-
*
|
|
95
|
-
* @see {@link https://orpc.dev/docs/client/client-side#infer-client-error | Client-Side Clients - Infer Client Error}
|
|
96
|
-
*/
|
|
97
|
-
type InferClientError<T extends AnyNestedClient> = T extends Client<any, any, any, infer U> ? U : {
|
|
98
|
-
[K in keyof T]: T[K] extends AnyNestedClient ? InferClientError<T[K]> : never;
|
|
99
|
-
}[keyof T];
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Default mapping between common oRPC error codes and HTTP status codes.
|
|
103
|
-
* Handlers use it to determine response status codes; spread it to build a custom `errorStatusMap`.
|
|
104
|
-
*
|
|
105
|
-
* @see {@link https://orpc.dev/docs/rpc/handler#custom-error-response | RPC Handler - Custom Error Response}
|
|
106
|
-
* @see {@link https://orpc.dev/docs/openapi/handler#custom-error-response | OpenAPI Handler - Custom Error Response}
|
|
107
|
-
*/
|
|
108
|
-
declare const COMMON_ERROR_STATUS_MAP: {
|
|
109
|
-
BAD_REQUEST: number;
|
|
110
|
-
UNAUTHORIZED: number;
|
|
111
|
-
PAYMENT_REQUIRED: number;
|
|
112
|
-
FORBIDDEN: number;
|
|
113
|
-
NOT_FOUND: number;
|
|
114
|
-
METHOD_NOT_SUPPORTED: number;
|
|
115
|
-
NOT_ACCEPTABLE: number;
|
|
116
|
-
TIMEOUT: number;
|
|
117
|
-
CONFLICT: number;
|
|
118
|
-
GONE: number;
|
|
119
|
-
LENGTH_REQUIRED: number;
|
|
120
|
-
PRECONDITION_FAILED: number;
|
|
121
|
-
PAYLOAD_TOO_LARGE: number;
|
|
122
|
-
UNSUPPORTED_MEDIA_TYPE: number;
|
|
123
|
-
UNPROCESSABLE_CONTENT: number;
|
|
124
|
-
PRECONDITION_REQUIRED: number;
|
|
125
|
-
TOO_MANY_REQUESTS: number;
|
|
126
|
-
CLIENT_CLOSED_REQUEST: number;
|
|
127
|
-
INTERNAL_SERVER_ERROR: number;
|
|
128
|
-
NOT_IMPLEMENTED: number;
|
|
129
|
-
BAD_GATEWAY: number;
|
|
130
|
-
SERVICE_UNAVAILABLE: number;
|
|
131
|
-
GATEWAY_TIMEOUT: number;
|
|
132
|
-
};
|
|
133
|
-
type ORPCErrorCode = Registry extends {
|
|
134
|
-
ORPCErrorCode: infer T extends string;
|
|
135
|
-
} ? T : (keyof typeof COMMON_ERROR_STATUS_MAP) | (string & {});
|
|
136
|
-
type ORPCErrorOptions<TData> = ErrorOptions & {
|
|
137
|
-
message?: string | undefined;
|
|
138
|
-
} & (undefined extends TData ? {
|
|
139
|
-
data?: TData;
|
|
140
|
-
} : {
|
|
141
|
-
data: TData;
|
|
142
|
-
});
|
|
143
|
-
/**
|
|
144
|
-
* Typed error carrying a `code`, a `message`, and optional `data`.
|
|
145
|
-
* Throw it from handlers or middleware to produce typed error responses on the client.
|
|
146
|
-
*
|
|
147
|
-
* @see {@link https://orpc.dev/docs/error-handling#orpcerror-class | Error Handling - ORPCError Class}
|
|
148
|
-
*/
|
|
149
|
-
declare class ORPCError<TCode extends ORPCErrorCode, TData> extends Error {
|
|
150
|
-
/**
|
|
151
|
-
* @remarks
|
|
152
|
-
* **Note**: The `__branch` property is used for type branding, helping TypeScript distinguish
|
|
153
|
-
* an `ORPCError` instance from plain objects with a similar structure.
|
|
154
|
-
*/
|
|
155
|
-
readonly name: "ORPCError" & {
|
|
156
|
-
__branch: "ORPCError";
|
|
157
|
-
};
|
|
158
|
-
/**
|
|
159
|
-
* Indicates whether the error matches a definition in the procedure's `.errors` map,
|
|
160
|
-
* which makes its type inferable on the client.
|
|
161
|
-
*/
|
|
162
|
-
readonly defined: boolean;
|
|
163
|
-
code: TCode;
|
|
164
|
-
data: TData;
|
|
165
|
-
constructor(code: TCode, ...rest: MaybeOptionalOptions<ORPCErrorOptions<TData>>);
|
|
166
|
-
toJSON(): ORPCErrorJSON<TCode, TData>;
|
|
167
|
-
/**
|
|
168
|
-
* Workaround for Next.js where different contexts use separate
|
|
169
|
-
* dependency graphs, causing multiple ORPCError constructors existing and breaking
|
|
170
|
-
* `instanceof` checks across contexts.
|
|
171
|
-
*
|
|
172
|
-
* This is particularly problematic with "Optimized SSR", where orpc-client
|
|
173
|
-
* executes in one context but is invoked from another. When an error is thrown
|
|
174
|
-
* in the execution context, `instanceof ORPCError` checks fail in the
|
|
175
|
-
* invocation context due to separate class constructors.
|
|
176
|
-
*
|
|
177
|
-
* @todo Remove this and related code if Next.js resolves the multiple dependency graph issue.
|
|
178
|
-
*/
|
|
179
|
-
static [Symbol.hasInstance](instance: unknown): boolean;
|
|
180
|
-
}
|
|
181
|
-
interface ORPCErrorJSON<TCode extends string, TData> extends Pick<ORPCError<TCode, TData>, 'code' | 'message' | 'data'> {
|
|
182
|
-
/**
|
|
183
|
-
* remove readonly
|
|
184
|
-
*/
|
|
185
|
-
defined: boolean;
|
|
186
|
-
}
|
|
187
|
-
type AnyORPCError = ORPCError<any, any>;
|
|
188
|
-
type AnyORPCErrorJSON = ORPCErrorJSON<any, any>;
|
|
189
|
-
interface ResponseErrorOptions extends ErrorOptions {
|
|
190
|
-
/**
|
|
191
|
-
* @default inferred from the response status
|
|
192
|
-
*/
|
|
193
|
-
message?: string | undefined;
|
|
194
|
-
response: StandardResponse;
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Error carrying the resolved response a link received. Links set it as the `cause`
|
|
198
|
-
* of an `ORPCError` decoded from an error response, so details that are not part
|
|
199
|
-
* of the error itself, such as its status or headers, stay available.
|
|
200
|
-
*
|
|
201
|
-
* @see {@link https://orpc.dev/docs/rpc/link#error-responses | RPC Link - Error Responses}
|
|
202
|
-
* @see {@link https://orpc.dev/docs/openapi/link#error-responses | OpenAPI Link - Error Responses}
|
|
203
|
-
*/
|
|
204
|
-
declare class ResponseError extends Error {
|
|
205
|
-
readonly name: string;
|
|
206
|
-
response: StandardResponse;
|
|
207
|
-
constructor(options: ResponseErrorOptions);
|
|
208
|
-
}
|
|
209
|
-
interface MalformedResponseErrorOptions extends ResponseErrorOptions {
|
|
210
|
-
/**
|
|
211
|
-
* @default inferred from the response body or status
|
|
212
|
-
*/
|
|
213
|
-
message?: string | undefined;
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Error thrown by links when a response does not follow the expected oRPC format,
|
|
217
|
-
* for example when a proxy or gateway answers instead of your handler. Extends
|
|
218
|
-
* `ResponseError`, so it carries the resolved response. Unless overridden via
|
|
219
|
-
* `options.message`, the message is inferred from the response body or status.
|
|
220
|
-
*
|
|
221
|
-
* Unlike `ORPCError`, it reaches a handler's callers as a generic `INTERNAL_SERVER_ERROR`
|
|
222
|
-
* when thrown from the handler, so an upstream's response is not sent to them.
|
|
223
|
-
*
|
|
224
|
-
* @see {@link https://orpc.dev/docs/rpc/link#malformed-responses | RPC Link - Malformed Responses}
|
|
225
|
-
* @see {@link https://orpc.dev/docs/openapi/link#malformed-responses | OpenAPI Link - Malformed Responses}
|
|
226
|
-
*/
|
|
227
|
-
declare class MalformedResponseError extends ResponseError {
|
|
228
|
-
readonly name = "MalformedResponseError";
|
|
229
|
-
constructor(options: MalformedResponseErrorOptions);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
export { MalformedResponseError as M, ResponseError as R, ORPCError as h, COMMON_ERROR_STATUS_MAP as j };
|
|
233
|
-
export type { AnyORPCError as A, ClientContext as C, FriendlyClientOptions as F, InferClientContext as I, NestedClient as N, ORPCErrorCode as O, ClientOptions as a, ClientLink as b, AnyNestedClient as c, InferClientError as d, Client as e, ClientRest as f, ORPCErrorJSON as g, AnyORPCErrorJSON as i, InferClientBodyInputs as k, InferClientBodyOutputs as l, InferClientErrors as m, InferClientInputs as n, InferClientOutputs as o, MalformedResponseErrorOptions as p, ORPCErrorOptions as q, ResponseErrorOptions as r };
|
|
@@ -1,172 +0,0 @@
|
|
|
1
|
-
import { sortPlugins, runWithSpan, ORPC_NAME, getTracer, isAsyncIteratorObject, override, traceAsyncIterator, traceReadableStream, intercept, value, pathToHttpPath, stringifyJSON } from '@orpc/shared';
|
|
2
|
-
import { mergeStandardHeaders, parseStandardUrl } from '@standard-server/core';
|
|
3
|
-
import { toStandardHeaders } from '@standard-server/fetch';
|
|
4
|
-
import { M as MalformedResponseError, R as ResponseError } from './client.LF04eOvL.mjs';
|
|
5
|
-
import { R as RPCSerializer, i as isORPCErrorJson, c as createORPCErrorFromJson } from './client._wG5INmC.mjs';
|
|
6
|
-
|
|
7
|
-
class CompositeStandardLinkPlugin {
|
|
8
|
-
name = "~composite";
|
|
9
|
-
plugins;
|
|
10
|
-
constructor(plugins = []) {
|
|
11
|
-
this.plugins = sortPlugins(plugins);
|
|
12
|
-
}
|
|
13
|
-
init(options) {
|
|
14
|
-
for (const plugin of this.plugins) {
|
|
15
|
-
if (plugin.init) {
|
|
16
|
-
options = plugin.init(options);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
return options;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
class StandardLink {
|
|
24
|
-
constructor(codec, transport, options = {}) {
|
|
25
|
-
this.codec = codec;
|
|
26
|
-
this.transport = transport;
|
|
27
|
-
options = new CompositeStandardLinkPlugin(options.plugins).init(options);
|
|
28
|
-
this.interceptors = options.interceptors;
|
|
29
|
-
this.transportInterceptors = options.transportInterceptors;
|
|
30
|
-
}
|
|
31
|
-
interceptors;
|
|
32
|
-
transportInterceptors;
|
|
33
|
-
/**
|
|
34
|
-
* @throws ORPCError, MalformedResponseError, transport-level errors (network failures, timeouts, etc.)
|
|
35
|
-
*/
|
|
36
|
-
call(path, input, options) {
|
|
37
|
-
return runWithSpan(`${ORPC_NAME}.${path.join("/")}`, (span) => {
|
|
38
|
-
span?.setAttribute("rpc.system", ORPC_NAME);
|
|
39
|
-
span?.setAttribute("rpc.method", path.join("."));
|
|
40
|
-
if (getTracer() && isAsyncIteratorObject(input)) {
|
|
41
|
-
input = override(input, traceAsyncIterator("consume_async_iterator_object_input", input));
|
|
42
|
-
} else if (getTracer() && input instanceof ReadableStream) {
|
|
43
|
-
input = override(input, traceReadableStream("consume_octet_stream_input", input));
|
|
44
|
-
}
|
|
45
|
-
return intercept(this.interceptors, { ...options, path, input }, async ({ path: path2, input: input2, ...options2 }) => {
|
|
46
|
-
const tracer = getTracer();
|
|
47
|
-
const activeSpan = tracer?.getActiveSpan() ?? span;
|
|
48
|
-
let request = await runWithSpan(
|
|
49
|
-
{ name: "encode_input", parent: activeSpan },
|
|
50
|
-
() => this.codec.encodeInput(input2, path2, options2)
|
|
51
|
-
);
|
|
52
|
-
if (activeSpan && tracer?.inject) {
|
|
53
|
-
const headers = { ...request.headers };
|
|
54
|
-
tracer.inject(activeSpan, headers);
|
|
55
|
-
request = { ...request, headers };
|
|
56
|
-
}
|
|
57
|
-
const response = await intercept(
|
|
58
|
-
this.transportInterceptors,
|
|
59
|
-
{ ...options2, path: path2, request },
|
|
60
|
-
({ path: path3, request: request2, ...options3 }) => {
|
|
61
|
-
const activeTransportSpan = tracer?.getActiveSpan() ?? activeSpan;
|
|
62
|
-
return runWithSpan(
|
|
63
|
-
{ name: "send_request", parent: activeTransportSpan },
|
|
64
|
-
() => this.transport.send(request2, path3, options3)
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
);
|
|
68
|
-
const decodedResult = await runWithSpan(
|
|
69
|
-
{ name: "decode_response", parent: activeSpan },
|
|
70
|
-
() => this.codec.decodeResponse(response, path2, options2)
|
|
71
|
-
);
|
|
72
|
-
if (decodedResult.kind === "error") {
|
|
73
|
-
throw decodedResult.error;
|
|
74
|
-
}
|
|
75
|
-
const output = decodedResult.output;
|
|
76
|
-
if (getTracer() && isAsyncIteratorObject(output)) {
|
|
77
|
-
return override(output, traceAsyncIterator("consume_async_iterator_object_output", output));
|
|
78
|
-
} else if (getTracer() && output instanceof ReadableStream) {
|
|
79
|
-
return override(output, traceReadableStream("consume_octet_stream_output", output));
|
|
80
|
-
}
|
|
81
|
-
return output;
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const END_SLASH_REGEX = /\/$/;
|
|
88
|
-
class RPCLinkCodec {
|
|
89
|
-
baseUrl;
|
|
90
|
-
maxUrlLength;
|
|
91
|
-
fallbackMethod;
|
|
92
|
-
expectedMethod;
|
|
93
|
-
headers;
|
|
94
|
-
serializer;
|
|
95
|
-
constructor(options) {
|
|
96
|
-
this.baseUrl = options.url ?? "/";
|
|
97
|
-
this.maxUrlLength = options.maxUrlLength ?? 2083;
|
|
98
|
-
this.fallbackMethod = options.fallbackMethod ?? "POST";
|
|
99
|
-
this.expectedMethod = options.method ?? this.fallbackMethod;
|
|
100
|
-
this.headers = options.headers ?? {};
|
|
101
|
-
this.serializer = options.serializer ?? new RPCSerializer();
|
|
102
|
-
}
|
|
103
|
-
async encodeInput(input, path, options) {
|
|
104
|
-
let headers = toResolvedStandardHeaders(await value(this.headers, options, path, input));
|
|
105
|
-
if (options.lastEventId !== void 0) {
|
|
106
|
-
headers = mergeStandardHeaders(headers, { "last-event-id": options.lastEventId });
|
|
107
|
-
}
|
|
108
|
-
const expectedMethod = await value(this.expectedMethod, options, path, input);
|
|
109
|
-
const baseUrl = await value(this.baseUrl, options, path, input);
|
|
110
|
-
const [pathname, search, hash] = parseStandardUrl(baseUrl);
|
|
111
|
-
const newPathname = `${pathname.replace(END_SLASH_REGEX, "")}${pathToHttpPath(path)}`;
|
|
112
|
-
const serialized = this.serializer.serialize(input);
|
|
113
|
-
if (expectedMethod === "GET" && !(serialized instanceof Blob) && !(serialized instanceof ReadableStream) && !(serialized instanceof FormData) && !isAsyncIteratorObject(serialized)) {
|
|
114
|
-
const maxUrlLength = await value(this.maxUrlLength, options, path, input);
|
|
115
|
-
const mergedSearch = new URLSearchParams(search);
|
|
116
|
-
mergedSearch.append("data", stringifyJSON(serialized) ?? "");
|
|
117
|
-
const url2 = `${newPathname}?${mergedSearch}${hash ?? ""}`;
|
|
118
|
-
if (url2.length <= maxUrlLength) {
|
|
119
|
-
return {
|
|
120
|
-
body: void 0,
|
|
121
|
-
method: expectedMethod,
|
|
122
|
-
headers,
|
|
123
|
-
url: url2,
|
|
124
|
-
signal: options.signal
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
const url = `${newPathname}${search ?? ""}${hash ?? ""}`;
|
|
129
|
-
return {
|
|
130
|
-
url,
|
|
131
|
-
method: expectedMethod === "GET" ? this.fallbackMethod : expectedMethod,
|
|
132
|
-
headers,
|
|
133
|
-
body: serialized,
|
|
134
|
-
signal: options.signal
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
async decodeResponse(response) {
|
|
138
|
-
const isOk = response.status < 400;
|
|
139
|
-
const body = await response.resolveBody();
|
|
140
|
-
const deserialized = await (async () => {
|
|
141
|
-
try {
|
|
142
|
-
return this.serializer.deserialize(body);
|
|
143
|
-
} catch (cause) {
|
|
144
|
-
throw new MalformedResponseError({
|
|
145
|
-
message: "Invalid RPC response format.",
|
|
146
|
-
response: { status: response.status, headers: response.headers, body },
|
|
147
|
-
cause
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
})();
|
|
151
|
-
if (!isOk) {
|
|
152
|
-
if (isORPCErrorJson(deserialized)) {
|
|
153
|
-
return {
|
|
154
|
-
kind: "error",
|
|
155
|
-
error: createORPCErrorFromJson(deserialized, {
|
|
156
|
-
cause: new ResponseError({ response: { status: response.status, headers: response.headers, body } })
|
|
157
|
-
})
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
throw new MalformedResponseError({ response: { headers: response.headers, status: response.status, body } });
|
|
161
|
-
}
|
|
162
|
-
return { kind: "output", output: deserialized };
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
function toResolvedStandardHeaders(headers) {
|
|
166
|
-
if (typeof headers.forEach === "function") {
|
|
167
|
-
return toStandardHeaders(headers);
|
|
168
|
-
}
|
|
169
|
-
return headers;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
export { RPCLinkCodec as R, StandardLink as S };
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import { StandardBody } from '@standard-server/core';
|
|
2
|
-
import { Segment } from '@orpc/shared';
|
|
3
|
-
|
|
4
|
-
type RPCJsonSerializationMeta = [type: string, ...path: Segment[]];
|
|
5
|
-
type RPCJsonSerialization = {
|
|
6
|
-
json: unknown;
|
|
7
|
-
meta?: RPCJsonSerializationMeta[] | undefined;
|
|
8
|
-
maps?: undefined;
|
|
9
|
-
blobs?: undefined;
|
|
10
|
-
} | {
|
|
11
|
-
json: unknown;
|
|
12
|
-
meta?: RPCJsonSerializationMeta[] | undefined;
|
|
13
|
-
maps: Segment[][];
|
|
14
|
-
blobs: Blob[];
|
|
15
|
-
};
|
|
16
|
-
interface RPCJsonSerializerHandler {
|
|
17
|
-
condition(value: unknown): boolean;
|
|
18
|
-
serialize(value: any): unknown;
|
|
19
|
-
/**
|
|
20
|
-
* `serialized` comes from the wire, so validate its type and throw on mismatch.
|
|
21
|
-
*/
|
|
22
|
-
deserialize(serialized: unknown): unknown;
|
|
23
|
-
/**
|
|
24
|
-
* If false, the result of this serializer will not be further processed by other serializers,
|
|
25
|
-
* even if it matches their conditions and treat it as final serialized value.
|
|
26
|
-
* This can be useful for serializers that return primitive values, which should not be further processed.
|
|
27
|
-
* to improve performance and avoid potential issues with other serializers.
|
|
28
|
-
*
|
|
29
|
-
* @default false
|
|
30
|
-
*/
|
|
31
|
-
isTerminal?: boolean | undefined;
|
|
32
|
-
}
|
|
33
|
-
interface RPCJsonSerializerOptions {
|
|
34
|
-
/**
|
|
35
|
-
* Extend or override the built-in type handlers used during serialization and deserialization.
|
|
36
|
-
*
|
|
37
|
-
* Each key is a unique type identifier (e.g. `"date"`, `"bigint"`) and maps to a handler
|
|
38
|
-
* that defines how to detect, serialize, and deserialize values of that type.
|
|
39
|
-
*
|
|
40
|
-
* **Extending:** Add new keys to support custom types:
|
|
41
|
-
* ```ts
|
|
42
|
-
* handlers: {
|
|
43
|
-
* buffer: {
|
|
44
|
-
* condition: (v) => v instanceof Buffer,
|
|
45
|
-
* serialize: (v: Buffer) => v.toString('base64'),
|
|
46
|
-
* deserialize: (s: string) => Buffer.from(s, 'base64'),
|
|
47
|
-
* isTerminal: true,
|
|
48
|
-
* }
|
|
49
|
-
* }
|
|
50
|
-
* ```
|
|
51
|
-
*
|
|
52
|
-
* **Overriding:** Use an existing key to replace a built-in handler:
|
|
53
|
-
* ```ts
|
|
54
|
-
* handlers: {
|
|
55
|
-
* date: {
|
|
56
|
-
* condition: (v) => v instanceof Date,
|
|
57
|
-
* serialize: (v: Date) => v.getTime(),
|
|
58
|
-
* deserialize: (n: number) => new Date(n),
|
|
59
|
-
* isTerminal: true,
|
|
60
|
-
* }
|
|
61
|
-
* }
|
|
62
|
-
* ```
|
|
63
|
-
*
|
|
64
|
-
* **Disabling:** Set a key to `undefined` to remove a built-in handler:
|
|
65
|
-
* ```ts
|
|
66
|
-
* handlers: { url: undefined }
|
|
67
|
-
* ```
|
|
68
|
-
*
|
|
69
|
-
* Built-in type keys: `undefined`, `bigint`, `date`, `nan`, `infinity`, `url`, `set`, `map`.
|
|
70
|
-
*/
|
|
71
|
-
handlers?: Record<string, undefined | RPCJsonSerializerHandler> | undefined;
|
|
72
|
-
/**
|
|
73
|
-
* If true, properties with undefined values will be omitted during serialization.
|
|
74
|
-
*
|
|
75
|
-
* @default true
|
|
76
|
-
*/
|
|
77
|
-
omitUndefinedProperties?: boolean | undefined;
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Serializes and deserializes native types like Date, BigInt, Set, and Map
|
|
81
|
-
* into a JSON value plus separate metadata describing how to restore them.
|
|
82
|
-
*
|
|
83
|
-
* @see {@link https://orpc.dev/docs/rpc/serializer#rpc-json-serializer | RPC Serializer - RPC JSON Serializer}
|
|
84
|
-
*/
|
|
85
|
-
declare class RPCJsonSerializer {
|
|
86
|
-
private readonly handlers;
|
|
87
|
-
private readonly inlineBuiltInHandlers;
|
|
88
|
-
private readonly handlerEntries;
|
|
89
|
-
private readonly omitUndefinedProperties;
|
|
90
|
-
constructor(options?: RPCJsonSerializerOptions);
|
|
91
|
-
serializeJson(data: unknown): RPCJsonSerialization;
|
|
92
|
-
/**
|
|
93
|
-
* `segments` is a shared mutable stack (push/pop while walking),
|
|
94
|
-
* so it must be copied before being stored in `meta` or `maps`.
|
|
95
|
-
*/
|
|
96
|
-
private serializeValue;
|
|
97
|
-
deserializeJson(serialized: RPCJsonSerialization): unknown;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
interface RPCSerializerSerializeOptions {
|
|
101
|
-
/**
|
|
102
|
-
* Use FormData for serialization when nested blobs are present.
|
|
103
|
-
* Does not apply to root-level Blob values.
|
|
104
|
-
*
|
|
105
|
-
* @default true
|
|
106
|
-
*/
|
|
107
|
-
useFormDataForBlobFields?: boolean | undefined;
|
|
108
|
-
}
|
|
109
|
-
interface RPCSerializerOptions extends RPCJsonSerializerOptions {
|
|
110
|
-
/**
|
|
111
|
-
* Default options for serialize method
|
|
112
|
-
*/
|
|
113
|
-
serialize?: RPCSerializerSerializeOptions | undefined;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Serializes and deserializes data for the RPC protocol,
|
|
117
|
-
* preserving native types like Date, BigInt, Set, and Map that plain JSON cannot represent.
|
|
118
|
-
*
|
|
119
|
-
* @see {@link https://orpc.dev/docs/rpc/serializer | RPC Serializer}
|
|
120
|
-
*/
|
|
121
|
-
declare class RPCSerializer extends RPCJsonSerializer {
|
|
122
|
-
private readonly defaultSerializeOptions;
|
|
123
|
-
constructor(options?: RPCSerializerOptions);
|
|
124
|
-
serialize(data: unknown, options?: RPCSerializerSerializeOptions): StandardBody;
|
|
125
|
-
private serializePayload;
|
|
126
|
-
deserialize(data: StandardBody): unknown;
|
|
127
|
-
private deserializePayload;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export { RPCJsonSerializer as b, RPCSerializer as e };
|
|
131
|
-
export type { RPCJsonSerialization as R, RPCJsonSerializationMeta as a, RPCJsonSerializerHandler as c, RPCJsonSerializerOptions as d, RPCSerializerOptions as f, RPCSerializerSerializeOptions as g };
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import { StandardBody } from '@standard-server/core';
|
|
2
|
-
import { Segment } from '@orpc/shared';
|
|
3
|
-
|
|
4
|
-
type RPCJsonSerializationMeta = [type: string, ...path: Segment[]];
|
|
5
|
-
type RPCJsonSerialization = {
|
|
6
|
-
json: unknown;
|
|
7
|
-
meta?: RPCJsonSerializationMeta[] | undefined;
|
|
8
|
-
maps?: undefined;
|
|
9
|
-
blobs?: undefined;
|
|
10
|
-
} | {
|
|
11
|
-
json: unknown;
|
|
12
|
-
meta?: RPCJsonSerializationMeta[] | undefined;
|
|
13
|
-
maps: Segment[][];
|
|
14
|
-
blobs: Blob[];
|
|
15
|
-
};
|
|
16
|
-
interface RPCJsonSerializerHandler {
|
|
17
|
-
condition(value: unknown): boolean;
|
|
18
|
-
serialize(value: any): unknown;
|
|
19
|
-
/**
|
|
20
|
-
* `serialized` comes from the wire, so validate its type and throw on mismatch.
|
|
21
|
-
*/
|
|
22
|
-
deserialize(serialized: unknown): unknown;
|
|
23
|
-
/**
|
|
24
|
-
* If false, the result of this serializer will not be further processed by other serializers,
|
|
25
|
-
* even if it matches their conditions and treat it as final serialized value.
|
|
26
|
-
* This can be useful for serializers that return primitive values, which should not be further processed.
|
|
27
|
-
* to improve performance and avoid potential issues with other serializers.
|
|
28
|
-
*
|
|
29
|
-
* @default false
|
|
30
|
-
*/
|
|
31
|
-
isTerminal?: boolean | undefined;
|
|
32
|
-
}
|
|
33
|
-
interface RPCJsonSerializerOptions {
|
|
34
|
-
/**
|
|
35
|
-
* Extend or override the built-in type handlers used during serialization and deserialization.
|
|
36
|
-
*
|
|
37
|
-
* Each key is a unique type identifier (e.g. `"date"`, `"bigint"`) and maps to a handler
|
|
38
|
-
* that defines how to detect, serialize, and deserialize values of that type.
|
|
39
|
-
*
|
|
40
|
-
* **Extending:** Add new keys to support custom types:
|
|
41
|
-
* ```ts
|
|
42
|
-
* handlers: {
|
|
43
|
-
* buffer: {
|
|
44
|
-
* condition: (v) => v instanceof Buffer,
|
|
45
|
-
* serialize: (v: Buffer) => v.toString('base64'),
|
|
46
|
-
* deserialize: (s: string) => Buffer.from(s, 'base64'),
|
|
47
|
-
* isTerminal: true,
|
|
48
|
-
* }
|
|
49
|
-
* }
|
|
50
|
-
* ```
|
|
51
|
-
*
|
|
52
|
-
* **Overriding:** Use an existing key to replace a built-in handler:
|
|
53
|
-
* ```ts
|
|
54
|
-
* handlers: {
|
|
55
|
-
* date: {
|
|
56
|
-
* condition: (v) => v instanceof Date,
|
|
57
|
-
* serialize: (v: Date) => v.getTime(),
|
|
58
|
-
* deserialize: (n: number) => new Date(n),
|
|
59
|
-
* isTerminal: true,
|
|
60
|
-
* }
|
|
61
|
-
* }
|
|
62
|
-
* ```
|
|
63
|
-
*
|
|
64
|
-
* **Disabling:** Set a key to `undefined` to remove a built-in handler:
|
|
65
|
-
* ```ts
|
|
66
|
-
* handlers: { url: undefined }
|
|
67
|
-
* ```
|
|
68
|
-
*
|
|
69
|
-
* Built-in type keys: `undefined`, `bigint`, `date`, `nan`, `infinity`, `url`, `set`, `map`.
|
|
70
|
-
*/
|
|
71
|
-
handlers?: Record<string, undefined | RPCJsonSerializerHandler> | undefined;
|
|
72
|
-
/**
|
|
73
|
-
* If true, properties with undefined values will be omitted during serialization.
|
|
74
|
-
*
|
|
75
|
-
* @default true
|
|
76
|
-
*/
|
|
77
|
-
omitUndefinedProperties?: boolean | undefined;
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Serializes and deserializes native types like Date, BigInt, Set, and Map
|
|
81
|
-
* into a JSON value plus separate metadata describing how to restore them.
|
|
82
|
-
*
|
|
83
|
-
* @see {@link https://orpc.dev/docs/rpc/serializer#rpc-json-serializer | RPC Serializer - RPC JSON Serializer}
|
|
84
|
-
*/
|
|
85
|
-
declare class RPCJsonSerializer {
|
|
86
|
-
private readonly handlers;
|
|
87
|
-
private readonly inlineBuiltInHandlers;
|
|
88
|
-
private readonly handlerEntries;
|
|
89
|
-
private readonly omitUndefinedProperties;
|
|
90
|
-
constructor(options?: RPCJsonSerializerOptions);
|
|
91
|
-
serializeJson(data: unknown): RPCJsonSerialization;
|
|
92
|
-
/**
|
|
93
|
-
* `segments` is a shared mutable stack (push/pop while walking),
|
|
94
|
-
* so it must be copied before being stored in `meta` or `maps`.
|
|
95
|
-
*/
|
|
96
|
-
private serializeValue;
|
|
97
|
-
deserializeJson(serialized: RPCJsonSerialization): unknown;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
interface RPCSerializerSerializeOptions {
|
|
101
|
-
/**
|
|
102
|
-
* Use FormData for serialization when nested blobs are present.
|
|
103
|
-
* Does not apply to root-level Blob values.
|
|
104
|
-
*
|
|
105
|
-
* @default true
|
|
106
|
-
*/
|
|
107
|
-
useFormDataForBlobFields?: boolean | undefined;
|
|
108
|
-
}
|
|
109
|
-
interface RPCSerializerOptions extends RPCJsonSerializerOptions {
|
|
110
|
-
/**
|
|
111
|
-
* Default options for serialize method
|
|
112
|
-
*/
|
|
113
|
-
serialize?: RPCSerializerSerializeOptions | undefined;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Serializes and deserializes data for the RPC protocol,
|
|
117
|
-
* preserving native types like Date, BigInt, Set, and Map that plain JSON cannot represent.
|
|
118
|
-
*
|
|
119
|
-
* @see {@link https://orpc.dev/docs/rpc/serializer | RPC Serializer}
|
|
120
|
-
*/
|
|
121
|
-
declare class RPCSerializer extends RPCJsonSerializer {
|
|
122
|
-
private readonly defaultSerializeOptions;
|
|
123
|
-
constructor(options?: RPCSerializerOptions);
|
|
124
|
-
serialize(data: unknown, options?: RPCSerializerSerializeOptions): StandardBody;
|
|
125
|
-
private serializePayload;
|
|
126
|
-
deserialize(data: StandardBody): unknown;
|
|
127
|
-
private deserializePayload;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export { RPCJsonSerializer as b, RPCSerializer as e };
|
|
131
|
-
export type { RPCJsonSerialization as R, RPCJsonSerializationMeta as a, RPCJsonSerializerHandler as c, RPCJsonSerializerOptions as d, RPCSerializerOptions as f, RPCSerializerSerializeOptions as g };
|