@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
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Promisable } from "@orpc/shared";
|
|
2
|
+
import type { StandardLazyResponse, StandardRequest } from "@standard-server/core";
|
|
3
|
+
import type { ClientContext, ClientOptions } from "../../types.mjs";
|
|
4
|
+
export type StandardLinkCodecDecodedResponse = {
|
|
5
|
+
kind: "output";
|
|
6
|
+
output: unknown;
|
|
7
|
+
} | {
|
|
8
|
+
kind: "error";
|
|
9
|
+
error: Error;
|
|
10
|
+
};
|
|
11
|
+
export interface StandardLinkCodec<T extends ClientContext> {
|
|
12
|
+
encodeInput(input: unknown, path: string[], options: ClientOptions<T>): Promisable<StandardRequest>;
|
|
13
|
+
decodeResponse(response: StandardLazyResponse, path: string[], options: ClientOptions<T>): Promisable<StandardLinkCodecDecodedResponse>;
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,60 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import { RPCSerializer } from '../../index.mjs';
|
|
7
|
-
|
|
8
|
-
interface RPCLinkCodecOptions<T extends ClientContext> {
|
|
9
|
-
/**
|
|
10
|
-
* Base url for all requests (without origin). Should match with handler's prefix.
|
|
11
|
-
*
|
|
12
|
-
* @example '/rpc?base=1'
|
|
13
|
-
*
|
|
14
|
-
* @default '/'
|
|
15
|
-
*/
|
|
16
|
-
url?: Value<Promisable<StandardUrl>, [options: ClientOptions<T>, path: string[], input: unknown]> | undefined;
|
|
17
|
-
/**
|
|
18
|
-
* The maximum length of the URL.
|
|
19
|
-
*
|
|
20
|
-
* If the URL exceeds this length, the codec should use the `fallbackMethod` to send the request with the payload in the body instead of the URL.
|
|
21
|
-
*
|
|
22
|
-
* @default 2083
|
|
23
|
-
*/
|
|
24
|
-
maxUrlLength?: Value<Promisable<number>, [options: ClientOptions<T>, path: string[], input: unknown]> | undefined;
|
|
25
|
-
/**
|
|
26
|
-
* The method used to make the request.
|
|
27
|
-
*
|
|
28
|
-
* @default 'POST'
|
|
29
|
-
*/
|
|
30
|
-
method?: Value<Promisable<'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'QUERY'>, [options: ClientOptions<T>, path: string[], input: unknown]> | undefined;
|
|
31
|
-
/**
|
|
32
|
-
* The method to use when the payload cannot safely pass to the server with method return from method function.
|
|
33
|
-
* GET is not allowed, it's very dangerous.
|
|
34
|
-
*
|
|
35
|
-
* @default 'POST'
|
|
36
|
-
*/
|
|
37
|
-
fallbackMethod?: 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'QUERY' | undefined;
|
|
38
|
-
/**
|
|
39
|
-
* Inject headers to the request.
|
|
40
|
-
*/
|
|
41
|
-
headers?: Value<Promisable<StandardHeaders | Headers>, [options: ClientOptions<T>, path: string[], input: unknown]> | undefined;
|
|
42
|
-
/**
|
|
43
|
-
* Override the default RPC serializer.
|
|
44
|
-
*/
|
|
45
|
-
serializer?: Public<RPCSerializer> | undefined;
|
|
46
|
-
}
|
|
47
|
-
declare class RPCLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
|
|
48
|
-
private readonly baseUrl;
|
|
49
|
-
private readonly maxUrlLength;
|
|
50
|
-
private readonly fallbackMethod;
|
|
51
|
-
private readonly expectedMethod;
|
|
52
|
-
private readonly headers;
|
|
53
|
-
private readonly serializer;
|
|
54
|
-
constructor(options: RPCLinkCodecOptions<T>);
|
|
55
|
-
encodeInput(input: unknown, path: string[], options: ClientOptions<T>): Promise<StandardRequest>;
|
|
56
|
-
decodeResponse(response: StandardLazyResponse): Promise<StandardLinkCodecDecodedResponse>;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export { RPCLinkCodec, StandardLinkCodec, StandardLinkCodecDecodedResponse };
|
|
60
|
-
export type { RPCLinkCodecOptions };
|
|
1
|
+
export * from "./codec.mjs";
|
|
2
|
+
export * from "./link.mjs";
|
|
3
|
+
export type { StandardLinkPlugin } from "./plugin.mjs";
|
|
4
|
+
export * from "./rpc-link-codec.mjs";
|
|
5
|
+
export * from "./transport.mjs";
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import '../../shared/client.LF04eOvL.mjs';
|
|
6
|
-
import '../../shared/client._wG5INmC.mjs';
|
|
1
|
+
export * from "./codec.mjs";
|
|
2
|
+
export * from "./link.mjs";
|
|
3
|
+
export * from "./rpc-link-codec.mjs";
|
|
4
|
+
export * from "./transport.mjs";
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Interceptor } from "@orpc/shared";
|
|
2
|
+
import type { StandardLazyResponse, StandardRequest } from "@standard-server/core";
|
|
3
|
+
import type { ClientContext, ClientLink, ClientOptions } from "../../types.mjs";
|
|
4
|
+
import type { StandardLinkCodec } from "./codec.mjs";
|
|
5
|
+
import type { StandardLinkPlugin } from "./plugin.mjs";
|
|
6
|
+
import type { StandardLinkTransport } from "./transport.mjs";
|
|
7
|
+
export interface StandardLinkInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
|
|
8
|
+
path: string[];
|
|
9
|
+
input: unknown;
|
|
10
|
+
}
|
|
11
|
+
export type StandardLinkInterceptor<T extends ClientContext> = Interceptor<StandardLinkInterceptorOptions<T>, Promise<unknown>>;
|
|
12
|
+
export interface StandardLinkTransportInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
|
|
13
|
+
path: string[];
|
|
14
|
+
request: StandardRequest;
|
|
15
|
+
}
|
|
16
|
+
export type StandardLinkTransportInterceptor<T extends ClientContext> = Interceptor<StandardLinkTransportInterceptorOptions<T>, Promise<StandardLazyResponse>>;
|
|
17
|
+
export interface StandardLinkOptions<T extends ClientContext> {
|
|
18
|
+
/**
|
|
19
|
+
* Interceptors that execute around the entire call, including transport and codec.
|
|
20
|
+
* Useful for error handling, logging, metrics, ...
|
|
21
|
+
*/
|
|
22
|
+
interceptors?: StandardLinkInterceptor<T>[] | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Interceptors that execute around the transport layer, after encoding and before decoding.
|
|
25
|
+
* Useful for modifying the request or response, adding transport-level logging, ...
|
|
26
|
+
*/
|
|
27
|
+
transportInterceptors?: StandardLinkTransportInterceptor<T>[] | undefined;
|
|
28
|
+
plugins?: StandardLinkPlugin<T>[] | undefined;
|
|
29
|
+
}
|
|
30
|
+
export declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
|
|
31
|
+
private readonly codec;
|
|
32
|
+
private readonly transport;
|
|
33
|
+
private readonly interceptors;
|
|
34
|
+
private readonly transportInterceptors;
|
|
35
|
+
constructor(codec: StandardLinkCodec<T>, transport: StandardLinkTransport<T>, options?: StandardLinkOptions<T>);
|
|
36
|
+
/**
|
|
37
|
+
* @throws ORPCError, MalformedResponseError, transport-level errors (network failures, timeouts, etc.)
|
|
38
|
+
*/
|
|
39
|
+
call(path: string[], input: unknown, options: ClientOptions<T>): Promise<unknown>;
|
|
40
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { getTracer, intercept, isAsyncIteratorObject, ORPC_NAME, override, runWithSpan, traceAsyncIterator, traceReadableStream } from "@orpc/shared";
|
|
2
|
+
import { CompositeStandardLinkPlugin } from "./plugin.mjs";
|
|
3
|
+
export class StandardLink {
|
|
4
|
+
codec;
|
|
5
|
+
transport;
|
|
6
|
+
interceptors;
|
|
7
|
+
transportInterceptors;
|
|
8
|
+
constructor(codec, transport, options = {}) {
|
|
9
|
+
this.codec = codec;
|
|
10
|
+
this.transport = transport;
|
|
11
|
+
options = new CompositeStandardLinkPlugin(options.plugins).init(options);
|
|
12
|
+
this.interceptors = options.interceptors;
|
|
13
|
+
this.transportInterceptors = options.transportInterceptors;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
call(path, input, options) {
|
|
17
|
+
return runWithSpan(`${ORPC_NAME}.${path.join("/")}`, (span) => {
|
|
18
|
+
|
|
19
|
+
span?.setAttribute("rpc.system", ORPC_NAME);
|
|
20
|
+
span?.setAttribute("rpc.method", path.join("."));
|
|
21
|
+
if (getTracer() && isAsyncIteratorObject(input)) {
|
|
22
|
+
|
|
23
|
+
input = override(input, traceAsyncIterator("consume_async_iterator_object_input", input));
|
|
24
|
+
} else if (getTracer() && input instanceof ReadableStream) {
|
|
25
|
+
|
|
26
|
+
input = override(input, traceReadableStream("consume_octet_stream_input", input));
|
|
27
|
+
}
|
|
28
|
+
return intercept(this.interceptors, {
|
|
29
|
+
...options,
|
|
30
|
+
path,
|
|
31
|
+
input
|
|
32
|
+
}, async ({ path, input, ...options }) => {
|
|
33
|
+
|
|
34
|
+
const tracer = getTracer();
|
|
35
|
+
const activeSpan = tracer?.getActiveSpan() ?? span;
|
|
36
|
+
let request = await runWithSpan({
|
|
37
|
+
name: "encode_input",
|
|
38
|
+
parent: activeSpan
|
|
39
|
+
}, () => this.codec.encodeInput(input, path, options));
|
|
40
|
+
if (activeSpan && tracer?.inject) {
|
|
41
|
+
const headers = { ...request.headers };
|
|
42
|
+
tracer.inject(activeSpan, headers);
|
|
43
|
+
request = {
|
|
44
|
+
...request,
|
|
45
|
+
headers
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
const response = await intercept(this.transportInterceptors, {
|
|
49
|
+
...options,
|
|
50
|
+
path,
|
|
51
|
+
request
|
|
52
|
+
}, ({ path, request, ...options }) => {
|
|
53
|
+
|
|
54
|
+
const activeTransportSpan = tracer?.getActiveSpan() ?? activeSpan;
|
|
55
|
+
return runWithSpan({
|
|
56
|
+
name: "send_request",
|
|
57
|
+
parent: activeTransportSpan
|
|
58
|
+
}, () => this.transport.send(request, path, options));
|
|
59
|
+
});
|
|
60
|
+
const decodedResult = await runWithSpan({
|
|
61
|
+
name: "decode_response",
|
|
62
|
+
parent: activeSpan
|
|
63
|
+
}, () => this.codec.decodeResponse(response, path, options));
|
|
64
|
+
if (decodedResult.kind === "error") {
|
|
65
|
+
throw decodedResult.error;
|
|
66
|
+
}
|
|
67
|
+
const output = decodedResult.output;
|
|
68
|
+
if (getTracer() && isAsyncIteratorObject(output)) {
|
|
69
|
+
|
|
70
|
+
return override(output, traceAsyncIterator("consume_async_iterator_object_output", output));
|
|
71
|
+
} else if (getTracer() && output instanceof ReadableStream) {
|
|
72
|
+
|
|
73
|
+
return override(output, traceReadableStream("consume_octet_stream_output", output));
|
|
74
|
+
}
|
|
75
|
+
return output;
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { OrderablePlugin } from "@orpc/shared";
|
|
2
|
+
import type { ClientContext } from "../../types.mjs";
|
|
3
|
+
import type { StandardLinkOptions } from "./link.mjs";
|
|
4
|
+
export interface StandardLinkPlugin<T extends ClientContext> extends OrderablePlugin {
|
|
5
|
+
/**
|
|
6
|
+
* Initializes the plugin and returns new link options.
|
|
7
|
+
* Called once per plugin instance during composition.
|
|
8
|
+
*
|
|
9
|
+
* This method allows plugins to wrap, extend, or transform link options
|
|
10
|
+
* such as interceptors, or configuration.
|
|
11
|
+
*
|
|
12
|
+
* @param options - The current link options from previous plugins or base configuration
|
|
13
|
+
* @returns Transformed link options with plugin's modifications applied
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* init(options) {
|
|
18
|
+
* return {
|
|
19
|
+
* ...options,
|
|
20
|
+
* interceptors: [...(options.interceptors || []), myInterceptor]
|
|
21
|
+
* }
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
init?(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
|
|
26
|
+
}
|
|
27
|
+
export declare class CompositeStandardLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
28
|
+
name: string;
|
|
29
|
+
protected readonly plugins: StandardLinkPlugin<T>[];
|
|
30
|
+
constructor(plugins?: StandardLinkPlugin<T>[]);
|
|
31
|
+
init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { sortPlugins } from "@orpc/shared";
|
|
2
|
+
export class CompositeStandardLinkPlugin {
|
|
3
|
+
name = "~composite";
|
|
4
|
+
plugins;
|
|
5
|
+
constructor(plugins = []) {
|
|
6
|
+
this.plugins = sortPlugins(plugins);
|
|
7
|
+
}
|
|
8
|
+
init(options) {
|
|
9
|
+
for (const plugin of this.plugins) {
|
|
10
|
+
if (plugin.init) {
|
|
11
|
+
options = plugin.init(options);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return options;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { Promisable, Public, Value } from "@orpc/shared";
|
|
2
|
+
import type { StandardHeaders, StandardLazyResponse, StandardRequest, StandardUrl } from "@standard-server/core";
|
|
3
|
+
import type { ClientContext, ClientOptions } from "../../types.mjs";
|
|
4
|
+
import type { StandardLinkCodec, StandardLinkCodecDecodedResponse } from "./index.mjs";
|
|
5
|
+
import { RPCSerializer } from "../../rpc-serializer.mjs";
|
|
6
|
+
export interface RPCLinkCodecOptions<T extends ClientContext> {
|
|
7
|
+
/**
|
|
8
|
+
* Base url for all requests (without origin). Should match with handler's prefix.
|
|
9
|
+
*
|
|
10
|
+
* @example '/rpc?base=1'
|
|
11
|
+
*
|
|
12
|
+
* @default '/'
|
|
13
|
+
*/
|
|
14
|
+
url?: Value<Promisable<StandardUrl>, [options: ClientOptions<T>, path: string[], input: unknown]> | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* The maximum length of the URL.
|
|
17
|
+
*
|
|
18
|
+
* If the URL exceeds this length, the codec should use the `fallbackMethod` to send the request with the payload in the body instead of the URL.
|
|
19
|
+
*
|
|
20
|
+
* @default 2083
|
|
21
|
+
*/
|
|
22
|
+
maxUrlLength?: Value<Promisable<number>, [options: ClientOptions<T>, path: string[], input: unknown]> | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* The method used to make the request.
|
|
25
|
+
*
|
|
26
|
+
* @default 'POST'
|
|
27
|
+
*/
|
|
28
|
+
method?: Value<Promisable<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "QUERY">, [options: ClientOptions<T>, path: string[], input: unknown]> | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* The method to use when the payload cannot safely pass to the server with method return from method function.
|
|
31
|
+
* GET is not allowed, it's very dangerous.
|
|
32
|
+
*
|
|
33
|
+
* @default 'POST'
|
|
34
|
+
*/
|
|
35
|
+
fallbackMethod?: "POST" | "PUT" | "PATCH" | "DELETE" | "QUERY" | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Inject headers to the request.
|
|
38
|
+
*/
|
|
39
|
+
headers?: Value<Promisable<StandardHeaders | Headers>, [options: ClientOptions<T>, path: string[], input: unknown]> | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* Override the default RPC serializer.
|
|
42
|
+
*/
|
|
43
|
+
serializer?: Public<RPCSerializer> | undefined;
|
|
44
|
+
}
|
|
45
|
+
export declare class RPCLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
|
|
46
|
+
private readonly baseUrl;
|
|
47
|
+
private readonly maxUrlLength;
|
|
48
|
+
private readonly fallbackMethod;
|
|
49
|
+
private readonly expectedMethod;
|
|
50
|
+
private readonly headers;
|
|
51
|
+
private readonly serializer;
|
|
52
|
+
constructor(options: RPCLinkCodecOptions<T>);
|
|
53
|
+
encodeInput(input: unknown, path: string[], options: ClientOptions<T>): Promise<StandardRequest>;
|
|
54
|
+
decodeResponse(response: StandardLazyResponse): Promise<StandardLinkCodecDecodedResponse>;
|
|
55
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { isAsyncIteratorObject, pathToHttpPath, stringifyJSON, value } from "@orpc/shared";
|
|
2
|
+
import { mergeStandardHeaders, parseStandardUrl } from "@standard-server/core";
|
|
3
|
+
import { toStandardHeaders } from "@standard-server/fetch";
|
|
4
|
+
import { MalformedResponseError, ResponseError } from "../../error.mjs";
|
|
5
|
+
import { createORPCErrorFromJson, isORPCErrorJson } from "../../error-utils.mjs";
|
|
6
|
+
import { RPCSerializer } from "../../rpc-serializer.mjs";
|
|
7
|
+
const END_SLASH_REGEX = /\/$/;
|
|
8
|
+
export class RPCLinkCodec {
|
|
9
|
+
baseUrl;
|
|
10
|
+
maxUrlLength;
|
|
11
|
+
fallbackMethod;
|
|
12
|
+
expectedMethod;
|
|
13
|
+
headers;
|
|
14
|
+
serializer;
|
|
15
|
+
constructor(options) {
|
|
16
|
+
this.baseUrl = options.url ?? "/";
|
|
17
|
+
this.maxUrlLength = options.maxUrlLength ?? 2083;
|
|
18
|
+
this.fallbackMethod = options.fallbackMethod ?? "POST";
|
|
19
|
+
this.expectedMethod = options.method ?? this.fallbackMethod;
|
|
20
|
+
this.headers = options.headers ?? {};
|
|
21
|
+
this.serializer = options.serializer ?? new RPCSerializer();
|
|
22
|
+
}
|
|
23
|
+
async encodeInput(input, path, options) {
|
|
24
|
+
let headers = toResolvedStandardHeaders(await value(this.headers, options, path, input));
|
|
25
|
+
if (options.lastEventId !== undefined) {
|
|
26
|
+
headers = mergeStandardHeaders(headers, { "last-event-id": options.lastEventId });
|
|
27
|
+
}
|
|
28
|
+
const expectedMethod = await value(this.expectedMethod, options, path, input);
|
|
29
|
+
const baseUrl = await value(this.baseUrl, options, path, input);
|
|
30
|
+
const [pathname, search, hash] = parseStandardUrl(baseUrl);
|
|
31
|
+
const newPathname = `${pathname.replace(END_SLASH_REGEX, "")}${pathToHttpPath(path)}`;
|
|
32
|
+
const serialized = this.serializer.serialize(input);
|
|
33
|
+
if (expectedMethod === "GET" && !(serialized instanceof Blob) && !(serialized instanceof ReadableStream) && !(serialized instanceof FormData) && !isAsyncIteratorObject(serialized)) {
|
|
34
|
+
const maxUrlLength = await value(this.maxUrlLength, options, path, input);
|
|
35
|
+
const mergedSearch = new URLSearchParams(search);
|
|
36
|
+
mergedSearch.append("data", stringifyJSON(serialized) ?? "");
|
|
37
|
+
const url = `${newPathname}?${mergedSearch}${hash ?? ""}`;
|
|
38
|
+
if (url.length <= maxUrlLength) {
|
|
39
|
+
return {
|
|
40
|
+
body: undefined,
|
|
41
|
+
method: expectedMethod,
|
|
42
|
+
headers,
|
|
43
|
+
url,
|
|
44
|
+
signal: options.signal
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const url = `${newPathname}${search ?? ""}${hash ?? ""}`;
|
|
49
|
+
return {
|
|
50
|
+
url,
|
|
51
|
+
method: expectedMethod === "GET" ? this.fallbackMethod : expectedMethod,
|
|
52
|
+
headers,
|
|
53
|
+
body: serialized,
|
|
54
|
+
signal: options.signal
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
async decodeResponse(response) {
|
|
58
|
+
const isOk = response.status < 400;
|
|
59
|
+
const body = await response.resolveBody();
|
|
60
|
+
const deserialized = await (async () => {
|
|
61
|
+
try {
|
|
62
|
+
return this.serializer.deserialize(body);
|
|
63
|
+
} catch (cause) {
|
|
64
|
+
throw new MalformedResponseError({
|
|
65
|
+
message: "Invalid RPC response format.",
|
|
66
|
+
response: {
|
|
67
|
+
status: response.status,
|
|
68
|
+
headers: response.headers,
|
|
69
|
+
body
|
|
70
|
+
},
|
|
71
|
+
cause
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
})();
|
|
75
|
+
if (!isOk) {
|
|
76
|
+
if (isORPCErrorJson(deserialized)) {
|
|
77
|
+
return {
|
|
78
|
+
kind: "error",
|
|
79
|
+
error: createORPCErrorFromJson(deserialized, { cause: new ResponseError({ response: {
|
|
80
|
+
status: response.status,
|
|
81
|
+
headers: response.headers,
|
|
82
|
+
body
|
|
83
|
+
} }) })
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
throw new MalformedResponseError({ response: {
|
|
87
|
+
headers: response.headers,
|
|
88
|
+
status: response.status,
|
|
89
|
+
body
|
|
90
|
+
} });
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
kind: "output",
|
|
94
|
+
output: deserialized
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function toResolvedStandardHeaders(headers) {
|
|
99
|
+
|
|
100
|
+
if (typeof headers.forEach === "function") {
|
|
101
|
+
return toStandardHeaders(headers);
|
|
102
|
+
}
|
|
103
|
+
return headers;
|
|
104
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { StandardLazyResponse, StandardRequest } from "@standard-server/core";
|
|
2
|
+
import type { ClientContext, ClientOptions } from "../../types.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Handles the transport layer for sending requests and receiving responses.
|
|
5
|
+
*
|
|
6
|
+
* Implementations are responsible for the actual network communication,
|
|
7
|
+
* such as HTTP fetch, WebSocket, or other transport mechanisms.
|
|
8
|
+
*/
|
|
9
|
+
export interface StandardLinkTransport<T extends ClientContext> {
|
|
10
|
+
/**
|
|
11
|
+
* @throws Transport-level errors (network failures, timeouts, etc.)
|
|
12
|
+
*/
|
|
13
|
+
send(request: StandardRequest, path: string[], options: ClientOptions<T>): Promise<StandardLazyResponse>;
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,136 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { StandardRequest, StandardLazyResponse } from '@standard-server/core';
|
|
4
|
-
import { 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
|
-
type WebSocketLike = Pick<WebSocket, 'addEventListener' | 'removeEventListener' | 'send' | 'readyState'>;
|
|
10
|
-
interface WebSocketLinkTransportAttemptInfo {
|
|
11
|
-
/**
|
|
12
|
-
* Total number of connection attempts for this transport's lifetime.
|
|
13
|
-
* Starts at 1 on the first attempt, increments on every subsequent
|
|
14
|
-
* attempt, and never resets.
|
|
15
|
-
*/
|
|
16
|
-
totalAttempt: number;
|
|
17
|
-
/**
|
|
18
|
-
* Attempt number within the current (re)connect cycle.
|
|
19
|
-
* Starts at 1, increments on each consecutive failure, and resets to 1
|
|
20
|
-
* once a connection succeeds or the cycle gives up. Use this for backoff calculations.
|
|
21
|
-
*/
|
|
22
|
-
attempt: number;
|
|
23
|
-
}
|
|
24
|
-
interface WebSocketLinkTransportReconnectOptions {
|
|
25
|
-
/**
|
|
26
|
-
* Whether to automatically reconnect when the connection is lost.
|
|
27
|
-
*
|
|
28
|
-
* @default false
|
|
29
|
-
*/
|
|
30
|
-
enabled: boolean;
|
|
31
|
-
/**
|
|
32
|
-
* Delay before a (re)connect attempt, in milliseconds.
|
|
33
|
-
*
|
|
34
|
-
* @default info => info.attempt === 1 ? 0 : 2_000
|
|
35
|
-
*/
|
|
36
|
-
delay?: ((info: WebSocketLinkTransportAttemptInfo) => number) | undefined;
|
|
37
|
-
/**
|
|
38
|
-
* Maximum number of consecutive failed attempts before giving up.
|
|
39
|
-
* When reached, calls waiting for the connection fail, and the next
|
|
40
|
-
* call starts a new reconnect cycle.
|
|
41
|
-
* Should greater than 1
|
|
42
|
-
*
|
|
43
|
-
* @default Infinity
|
|
44
|
-
*/
|
|
45
|
-
maxAttempt?: number | undefined;
|
|
46
|
-
/**
|
|
47
|
-
* Maximum number of connection attempts over the transport's lifetime,
|
|
48
|
-
* successful ones included (see `totalAttempt`).
|
|
49
|
-
* When reached, calls that need a new connection fail instead of connecting.
|
|
50
|
-
*
|
|
51
|
-
* @default Infinity
|
|
52
|
-
*/
|
|
53
|
-
maxTotalAttempt?: number | undefined;
|
|
54
|
-
/**
|
|
55
|
-
* Whether to proactively reconnect right after the socket closes,
|
|
56
|
-
* rather than waiting for the next call to trigger reconnection.
|
|
57
|
-
* Reduces latency for the next request.
|
|
58
|
-
*
|
|
59
|
-
* @default { enabled: false }
|
|
60
|
-
*/
|
|
61
|
-
onClose?: {
|
|
62
|
-
/**
|
|
63
|
-
* Whether to proactively reconnect right after the socket closes,
|
|
64
|
-
* rather than waiting for the next call to trigger reconnection.
|
|
65
|
-
* Reduces latency for the next request.
|
|
66
|
-
*
|
|
67
|
-
* @default false
|
|
68
|
-
*/
|
|
69
|
-
enabled: boolean;
|
|
70
|
-
/**
|
|
71
|
-
* Delay before reconnecting after the socket closes, in milliseconds.
|
|
72
|
-
*
|
|
73
|
-
* @default 0
|
|
74
|
-
*/
|
|
75
|
-
delay?: number | undefined;
|
|
76
|
-
} | undefined;
|
|
77
|
-
}
|
|
78
|
-
interface WebSocketLinkTransportOptions<_T extends ClientContext> {
|
|
79
|
-
/**
|
|
80
|
-
* Returns a WebSocket instance for peer communication.
|
|
81
|
-
* Can be async for lazy resolution.
|
|
82
|
-
*/
|
|
83
|
-
connect: (info: WebSocketLinkTransportAttemptInfo) => Promisable<WebSocketLike>;
|
|
84
|
-
/**
|
|
85
|
-
* Whether to connect immediately on initialization, instead of waiting
|
|
86
|
-
* for the first call. Reduces latency for the first request.
|
|
87
|
-
*
|
|
88
|
-
* @default false
|
|
89
|
-
*/
|
|
90
|
-
connectOnInit?: boolean | undefined;
|
|
91
|
-
/**
|
|
92
|
-
* Reconnection behavior when the connection is lost.
|
|
93
|
-
*
|
|
94
|
-
* @default { enabled: false }
|
|
95
|
-
*/
|
|
96
|
-
reconnect?: WebSocketLinkTransportReconnectOptions | undefined;
|
|
97
|
-
/**
|
|
98
|
-
* Options for encoding peer messages. such as `prefix` for distinguishing messages on the same channel..
|
|
99
|
-
*/
|
|
100
|
-
encodePeerMessage?: EncodePeerMessageOptions | undefined;
|
|
101
|
-
/**
|
|
102
|
-
* Options for decoding peer messages. such as `prefix` for distinguishing messages on the same channel..
|
|
103
|
-
*/
|
|
104
|
-
decodePeerMessage?: DecodePeerMessageOptions | undefined;
|
|
105
|
-
}
|
|
106
|
-
declare class WebSocketLinkTransport<T extends ClientContext> implements StandardLinkTransport<T> {
|
|
107
|
-
private readonly connect;
|
|
108
|
-
private readonly reconnectEnabled;
|
|
109
|
-
private readonly reconnectDelay;
|
|
110
|
-
private readonly reconnectMaxAttempt;
|
|
111
|
-
private readonly reconnectMaxTotalAttempt;
|
|
112
|
-
private readonly reconnectOnCloseEnabled;
|
|
113
|
-
private readonly reconnectOnCloseDelay;
|
|
114
|
-
private readonly encodePeerMessageOptions;
|
|
115
|
-
private readonly decodePeerMessageOptions;
|
|
116
|
-
constructor(options: WebSocketLinkTransportOptions<T>);
|
|
117
|
-
send(standardRequest: StandardRequest, _path: string[], _options: ClientOptions<T>): Promise<StandardLazyResponse>;
|
|
118
|
-
private totalAttempt;
|
|
119
|
-
private attempt;
|
|
120
|
-
private current;
|
|
121
|
-
private getConnectedPeer;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
interface RPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, WebSocketLinkTransportOptions<T>, RPCLinkCodecOptions<T> {
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Client link that communicates with an RPC Handler over a WebSocket connection.
|
|
128
|
-
*
|
|
129
|
-
* @see {@link https://orpc.dev/docs/adapters/websocket | WebSocket Adapters}
|
|
130
|
-
*/
|
|
131
|
-
declare class RPCLink<T extends ClientContext> extends StandardLink<T> {
|
|
132
|
-
constructor(options: RPCLinkOptions<T>);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
export { RPCLink, WebSocketLinkTransport };
|
|
136
|
-
export type { RPCLinkOptions, WebSocketLike, WebSocketLinkTransportAttemptInfo, WebSocketLinkTransportOptions, WebSocketLinkTransportReconnectOptions };
|
|
1
|
+
export * from "./rpc-link.mjs";
|
|
2
|
+
export * from "./transport.mjs";
|