@orpc/client 0.0.0-next.6b06c96 → 0.0.0-next.6bc474e
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 +144 -34
- package/dist/adapters/fetch/index.d.mts +31 -11
- package/dist/adapters/fetch/index.d.ts +31 -11
- package/dist/adapters/fetch/index.mjs +27 -13
- package/dist/adapters/message-port/index.d.mts +80 -0
- package/dist/adapters/message-port/index.d.ts +80 -0
- package/dist/adapters/message-port/index.mjs +87 -0
- package/dist/adapters/standard/index.d.mts +6 -5
- package/dist/adapters/standard/index.d.ts +6 -5
- package/dist/adapters/standard/index.mjs +4 -2
- package/dist/adapters/websocket/index.d.mts +29 -0
- package/dist/adapters/websocket/index.d.ts +29 -0
- package/dist/adapters/websocket/index.mjs +47 -0
- package/dist/index.d.mts +98 -23
- package/dist/index.d.ts +98 -23
- package/dist/index.mjs +55 -8
- package/dist/plugins/index.d.mts +208 -9
- package/dist/plugins/index.d.ts +208 -9
- package/dist/plugins/index.mjs +399 -25
- package/dist/shared/{client.C486S9aU.d.mts → client.2jUAqzYU.d.ts} +16 -11
- package/dist/shared/client.4qgEyLNQ.mjs +171 -0
- package/dist/shared/{client.DKA6DQSn.d.ts → client.B3pNRBih.d.ts} +13 -9
- package/dist/shared/{client.Bx07L657.d.mts → client.BFAVy68H.d.mts} +13 -9
- package/dist/shared/client.BLtwTQUg.mjs +40 -0
- package/dist/shared/{client.BQuFq0Vi.mjs → client.CEWGvV28.mjs} +98 -32
- package/dist/shared/{client.qdWq4aGG.d.ts → client.CpCa3si8.d.mts} +16 -11
- package/dist/shared/client.i2uoJbEp.d.mts +83 -0
- package/dist/shared/client.i2uoJbEp.d.ts +83 -0
- package/package.json +18 -8
- package/dist/shared/client.CipPQkhk.d.mts +0 -29
- package/dist/shared/client.CipPQkhk.d.ts +0 -29
- package/dist/shared/client.jKEwIsRd.mjs +0 -175
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { resolveMaybeOptionalOptions, getConstructor, isObject } from '@orpc/shared';
|
|
2
|
+
|
|
3
|
+
const ORPC_CLIENT_PACKAGE_NAME = "@orpc/client";
|
|
4
|
+
const ORPC_CLIENT_PACKAGE_VERSION = "0.0.0-next.6bc474e";
|
|
5
|
+
|
|
6
|
+
const COMMON_ORPC_ERROR_DEFS = {
|
|
7
|
+
BAD_REQUEST: {
|
|
8
|
+
status: 400,
|
|
9
|
+
message: "Bad Request"
|
|
10
|
+
},
|
|
11
|
+
UNAUTHORIZED: {
|
|
12
|
+
status: 401,
|
|
13
|
+
message: "Unauthorized"
|
|
14
|
+
},
|
|
15
|
+
FORBIDDEN: {
|
|
16
|
+
status: 403,
|
|
17
|
+
message: "Forbidden"
|
|
18
|
+
},
|
|
19
|
+
NOT_FOUND: {
|
|
20
|
+
status: 404,
|
|
21
|
+
message: "Not Found"
|
|
22
|
+
},
|
|
23
|
+
METHOD_NOT_SUPPORTED: {
|
|
24
|
+
status: 405,
|
|
25
|
+
message: "Method Not Supported"
|
|
26
|
+
},
|
|
27
|
+
NOT_ACCEPTABLE: {
|
|
28
|
+
status: 406,
|
|
29
|
+
message: "Not Acceptable"
|
|
30
|
+
},
|
|
31
|
+
TIMEOUT: {
|
|
32
|
+
status: 408,
|
|
33
|
+
message: "Request Timeout"
|
|
34
|
+
},
|
|
35
|
+
CONFLICT: {
|
|
36
|
+
status: 409,
|
|
37
|
+
message: "Conflict"
|
|
38
|
+
},
|
|
39
|
+
PRECONDITION_FAILED: {
|
|
40
|
+
status: 412,
|
|
41
|
+
message: "Precondition Failed"
|
|
42
|
+
},
|
|
43
|
+
PAYLOAD_TOO_LARGE: {
|
|
44
|
+
status: 413,
|
|
45
|
+
message: "Payload Too Large"
|
|
46
|
+
},
|
|
47
|
+
UNSUPPORTED_MEDIA_TYPE: {
|
|
48
|
+
status: 415,
|
|
49
|
+
message: "Unsupported Media Type"
|
|
50
|
+
},
|
|
51
|
+
UNPROCESSABLE_CONTENT: {
|
|
52
|
+
status: 422,
|
|
53
|
+
message: "Unprocessable Content"
|
|
54
|
+
},
|
|
55
|
+
TOO_MANY_REQUESTS: {
|
|
56
|
+
status: 429,
|
|
57
|
+
message: "Too Many Requests"
|
|
58
|
+
},
|
|
59
|
+
CLIENT_CLOSED_REQUEST: {
|
|
60
|
+
status: 499,
|
|
61
|
+
message: "Client Closed Request"
|
|
62
|
+
},
|
|
63
|
+
INTERNAL_SERVER_ERROR: {
|
|
64
|
+
status: 500,
|
|
65
|
+
message: "Internal Server Error"
|
|
66
|
+
},
|
|
67
|
+
NOT_IMPLEMENTED: {
|
|
68
|
+
status: 501,
|
|
69
|
+
message: "Not Implemented"
|
|
70
|
+
},
|
|
71
|
+
BAD_GATEWAY: {
|
|
72
|
+
status: 502,
|
|
73
|
+
message: "Bad Gateway"
|
|
74
|
+
},
|
|
75
|
+
SERVICE_UNAVAILABLE: {
|
|
76
|
+
status: 503,
|
|
77
|
+
message: "Service Unavailable"
|
|
78
|
+
},
|
|
79
|
+
GATEWAY_TIMEOUT: {
|
|
80
|
+
status: 504,
|
|
81
|
+
message: "Gateway Timeout"
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
function fallbackORPCErrorStatus(code, status) {
|
|
85
|
+
return status ?? COMMON_ORPC_ERROR_DEFS[code]?.status ?? 500;
|
|
86
|
+
}
|
|
87
|
+
function fallbackORPCErrorMessage(code, message) {
|
|
88
|
+
return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
|
|
89
|
+
}
|
|
90
|
+
const GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL = Symbol.for(`__${ORPC_CLIENT_PACKAGE_NAME}@${ORPC_CLIENT_PACKAGE_VERSION}/error/ORPC_ERROR_CONSTRUCTORS__`);
|
|
91
|
+
void (globalThis[GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL] ??= /* @__PURE__ */ new WeakSet());
|
|
92
|
+
const globalORPCErrorConstructors = globalThis[GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL];
|
|
93
|
+
class ORPCError extends Error {
|
|
94
|
+
defined;
|
|
95
|
+
code;
|
|
96
|
+
status;
|
|
97
|
+
data;
|
|
98
|
+
constructor(code, ...rest) {
|
|
99
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
100
|
+
if (options.status !== void 0 && !isORPCErrorStatus(options.status)) {
|
|
101
|
+
throw new Error("[ORPCError] Invalid error status code.");
|
|
102
|
+
}
|
|
103
|
+
const message = fallbackORPCErrorMessage(code, options.message);
|
|
104
|
+
super(message, options);
|
|
105
|
+
this.code = code;
|
|
106
|
+
this.status = fallbackORPCErrorStatus(code, options.status);
|
|
107
|
+
this.defined = options.defined ?? false;
|
|
108
|
+
this.data = options.data;
|
|
109
|
+
}
|
|
110
|
+
toJSON() {
|
|
111
|
+
return {
|
|
112
|
+
defined: this.defined,
|
|
113
|
+
code: this.code,
|
|
114
|
+
status: this.status,
|
|
115
|
+
message: this.message,
|
|
116
|
+
data: this.data
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Workaround for Next.js where different contexts use separate
|
|
121
|
+
* dependency graphs, causing multiple ORPCError constructors existing and breaking
|
|
122
|
+
* `instanceof` checks across contexts.
|
|
123
|
+
*
|
|
124
|
+
* This is particularly problematic with "Optimized SSR", where orpc-client
|
|
125
|
+
* executes in one context but is invoked from another. When an error is thrown
|
|
126
|
+
* in the execution context, `instanceof ORPCError` checks fail in the
|
|
127
|
+
* invocation context due to separate class constructors.
|
|
128
|
+
*
|
|
129
|
+
* @todo Remove this and related code if Next.js resolves the multiple dependency graph issue.
|
|
130
|
+
*/
|
|
131
|
+
static [Symbol.hasInstance](instance) {
|
|
132
|
+
if (globalORPCErrorConstructors.has(this)) {
|
|
133
|
+
const constructor = getConstructor(instance);
|
|
134
|
+
if (constructor && globalORPCErrorConstructors.has(constructor)) {
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return super[Symbol.hasInstance](instance);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
globalORPCErrorConstructors.add(ORPCError);
|
|
142
|
+
function isDefinedError(error) {
|
|
143
|
+
return error instanceof ORPCError && error.defined;
|
|
144
|
+
}
|
|
145
|
+
function toORPCError(error) {
|
|
146
|
+
return error instanceof ORPCError ? error : new ORPCError("INTERNAL_SERVER_ERROR", {
|
|
147
|
+
message: "Internal server error",
|
|
148
|
+
cause: error
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
function isORPCErrorStatus(status) {
|
|
152
|
+
return status < 200 || status >= 400;
|
|
153
|
+
}
|
|
154
|
+
function isORPCErrorJson(json) {
|
|
155
|
+
if (!isObject(json)) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
const validKeys = ["defined", "code", "status", "message", "data"];
|
|
159
|
+
if (Object.keys(json).some((k) => !validKeys.includes(k))) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
return "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "status" in json && typeof json.status === "number" && isORPCErrorStatus(json.status) && "message" in json && typeof json.message === "string";
|
|
163
|
+
}
|
|
164
|
+
function createORPCErrorFromJson(json, options = {}) {
|
|
165
|
+
return new ORPCError(json.code, {
|
|
166
|
+
...options,
|
|
167
|
+
...json
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export { COMMON_ORPC_ERROR_DEFS as C, ORPC_CLIENT_PACKAGE_NAME as O, ORPC_CLIENT_PACKAGE_VERSION as a, fallbackORPCErrorMessage as b, ORPCError as c, isORPCErrorStatus as d, isORPCErrorJson as e, fallbackORPCErrorStatus as f, createORPCErrorFromJson as g, isDefinedError as i, toORPCError as t };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { Segment, Value } from '@orpc/shared';
|
|
1
|
+
import { b as ClientContext, c as ClientOptions, f as HTTPMethod } from './client.i2uoJbEp.js';
|
|
2
|
+
import { e as StandardLinkCodec, b as StandardLinkOptions, d as StandardLink, f as StandardLinkClient } from './client.2jUAqzYU.js';
|
|
3
|
+
import { Segment, Value, Promisable } from '@orpc/shared';
|
|
4
4
|
import { StandardHeaders, StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
|
|
5
5
|
|
|
6
6
|
declare const STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES: {
|
|
@@ -44,30 +44,30 @@ interface StandardRPCLinkCodecOptions<T extends ClientContext> {
|
|
|
44
44
|
/**
|
|
45
45
|
* Base url for all requests.
|
|
46
46
|
*/
|
|
47
|
-
url: Value<string | URL
|
|
47
|
+
url: Value<Promisable<string | URL>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
48
48
|
/**
|
|
49
49
|
* The maximum length of the URL.
|
|
50
50
|
*
|
|
51
51
|
* @default 2083
|
|
52
52
|
*/
|
|
53
|
-
maxUrlLength?: Value<number
|
|
53
|
+
maxUrlLength?: Value<Promisable<number>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
54
54
|
/**
|
|
55
55
|
* The method used to make the request.
|
|
56
56
|
*
|
|
57
57
|
* @default 'POST'
|
|
58
58
|
*/
|
|
59
|
-
method?: Value<HTTPMethod, [
|
|
59
|
+
method?: Value<Promisable<Exclude<HTTPMethod, 'HEAD'>>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
60
60
|
/**
|
|
61
61
|
* The method to use when the payload cannot safely pass to the server with method return from method function.
|
|
62
62
|
* GET is not allowed, it's very dangerous.
|
|
63
63
|
*
|
|
64
64
|
* @default 'POST'
|
|
65
65
|
*/
|
|
66
|
-
fallbackMethod?: Exclude<HTTPMethod, 'GET'>;
|
|
66
|
+
fallbackMethod?: Exclude<HTTPMethod, 'HEAD' | 'GET'>;
|
|
67
67
|
/**
|
|
68
68
|
* Inject headers to the request.
|
|
69
69
|
*/
|
|
70
|
-
headers?: Value<StandardHeaders
|
|
70
|
+
headers?: Value<Promisable<StandardHeaders | Headers>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
71
71
|
}
|
|
72
72
|
declare class StandardRPCLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
|
|
73
73
|
private readonly serializer;
|
|
@@ -83,5 +83,9 @@ declare class StandardRPCLinkCodec<T extends ClientContext> implements StandardL
|
|
|
83
83
|
|
|
84
84
|
interface StandardRPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, StandardRPCLinkCodecOptions<T>, StandardRPCJsonSerializerOptions {
|
|
85
85
|
}
|
|
86
|
+
declare class StandardRPCLink<T extends ClientContext> extends StandardLink<T> {
|
|
87
|
+
constructor(linkClient: StandardLinkClient<T>, options: StandardRPCLinkOptions<T>);
|
|
88
|
+
}
|
|
86
89
|
|
|
87
|
-
export { STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES as S,
|
|
90
|
+
export { STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES as S, StandardRPCJsonSerializer as e, StandardRPCLink as g, StandardRPCLinkCodec as i, StandardRPCSerializer as j };
|
|
91
|
+
export type { StandardRPCJsonSerializedMetaItem as a, StandardRPCJsonSerialized as b, StandardRPCCustomJsonSerializer as c, StandardRPCJsonSerializerOptions as d, StandardRPCLinkOptions as f, StandardRPCLinkCodecOptions as h };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { Segment, Value } from '@orpc/shared';
|
|
1
|
+
import { b as ClientContext, c as ClientOptions, f as HTTPMethod } from './client.i2uoJbEp.mjs';
|
|
2
|
+
import { e as StandardLinkCodec, b as StandardLinkOptions, d as StandardLink, f as StandardLinkClient } from './client.CpCa3si8.mjs';
|
|
3
|
+
import { Segment, Value, Promisable } from '@orpc/shared';
|
|
4
4
|
import { StandardHeaders, StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
|
|
5
5
|
|
|
6
6
|
declare const STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES: {
|
|
@@ -44,30 +44,30 @@ interface StandardRPCLinkCodecOptions<T extends ClientContext> {
|
|
|
44
44
|
/**
|
|
45
45
|
* Base url for all requests.
|
|
46
46
|
*/
|
|
47
|
-
url: Value<string | URL
|
|
47
|
+
url: Value<Promisable<string | URL>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
48
48
|
/**
|
|
49
49
|
* The maximum length of the URL.
|
|
50
50
|
*
|
|
51
51
|
* @default 2083
|
|
52
52
|
*/
|
|
53
|
-
maxUrlLength?: Value<number
|
|
53
|
+
maxUrlLength?: Value<Promisable<number>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
54
54
|
/**
|
|
55
55
|
* The method used to make the request.
|
|
56
56
|
*
|
|
57
57
|
* @default 'POST'
|
|
58
58
|
*/
|
|
59
|
-
method?: Value<HTTPMethod, [
|
|
59
|
+
method?: Value<Promisable<Exclude<HTTPMethod, 'HEAD'>>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
60
60
|
/**
|
|
61
61
|
* The method to use when the payload cannot safely pass to the server with method return from method function.
|
|
62
62
|
* GET is not allowed, it's very dangerous.
|
|
63
63
|
*
|
|
64
64
|
* @default 'POST'
|
|
65
65
|
*/
|
|
66
|
-
fallbackMethod?: Exclude<HTTPMethod, 'GET'>;
|
|
66
|
+
fallbackMethod?: Exclude<HTTPMethod, 'HEAD' | 'GET'>;
|
|
67
67
|
/**
|
|
68
68
|
* Inject headers to the request.
|
|
69
69
|
*/
|
|
70
|
-
headers?: Value<StandardHeaders
|
|
70
|
+
headers?: Value<Promisable<StandardHeaders | Headers>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
71
71
|
}
|
|
72
72
|
declare class StandardRPCLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
|
|
73
73
|
private readonly serializer;
|
|
@@ -83,5 +83,9 @@ declare class StandardRPCLinkCodec<T extends ClientContext> implements StandardL
|
|
|
83
83
|
|
|
84
84
|
interface StandardRPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, StandardRPCLinkCodecOptions<T>, StandardRPCJsonSerializerOptions {
|
|
85
85
|
}
|
|
86
|
+
declare class StandardRPCLink<T extends ClientContext> extends StandardLink<T> {
|
|
87
|
+
constructor(linkClient: StandardLinkClient<T>, options: StandardRPCLinkOptions<T>);
|
|
88
|
+
}
|
|
86
89
|
|
|
87
|
-
export { STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES as S,
|
|
90
|
+
export { STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES as S, StandardRPCJsonSerializer as e, StandardRPCLink as g, StandardRPCLinkCodec as i, StandardRPCSerializer as j };
|
|
91
|
+
export type { StandardRPCJsonSerializedMetaItem as a, StandardRPCJsonSerialized as b, StandardRPCCustomJsonSerializer as c, StandardRPCJsonSerializerOptions as d, StandardRPCLinkOptions as f, StandardRPCLinkCodecOptions as h };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { AsyncIteratorClass, isTypescriptObject } from '@orpc/shared';
|
|
2
|
+
import { getEventMeta, withEventMeta } from '@orpc/standard-server';
|
|
3
|
+
|
|
4
|
+
function mapEventIterator(iterator, maps) {
|
|
5
|
+
const mapError = async (error) => {
|
|
6
|
+
let mappedError = await maps.error(error);
|
|
7
|
+
if (mappedError !== error) {
|
|
8
|
+
const meta = getEventMeta(error);
|
|
9
|
+
if (meta && isTypescriptObject(mappedError)) {
|
|
10
|
+
mappedError = withEventMeta(mappedError, meta);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return mappedError;
|
|
14
|
+
};
|
|
15
|
+
return new AsyncIteratorClass(async () => {
|
|
16
|
+
const { done, value } = await (async () => {
|
|
17
|
+
try {
|
|
18
|
+
return await iterator.next();
|
|
19
|
+
} catch (error) {
|
|
20
|
+
throw await mapError(error);
|
|
21
|
+
}
|
|
22
|
+
})();
|
|
23
|
+
let mappedValue = await maps.value(value, done);
|
|
24
|
+
if (mappedValue !== value) {
|
|
25
|
+
const meta = getEventMeta(value);
|
|
26
|
+
if (meta && isTypescriptObject(mappedValue)) {
|
|
27
|
+
mappedValue = withEventMeta(mappedValue, meta);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return { done, value: mappedValue };
|
|
31
|
+
}, async () => {
|
|
32
|
+
try {
|
|
33
|
+
await iterator.return?.();
|
|
34
|
+
} catch (error) {
|
|
35
|
+
throw await mapError(error);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export { mapEventIterator as m };
|
|
@@ -1,36 +1,79 @@
|
|
|
1
|
-
import { toArray, intercept, isObject, value,
|
|
1
|
+
import { toArray, runWithSpan, ORPC_NAME, isAsyncIteratorObject, asyncIteratorWithSpan, intercept, getGlobalOtelConfig, isObject, value, stringifyJSON } from '@orpc/shared';
|
|
2
2
|
import { mergeStandardHeaders, ErrorEvent } from '@orpc/standard-server';
|
|
3
|
-
import { C as COMMON_ORPC_ERROR_DEFS,
|
|
3
|
+
import { C as COMMON_ORPC_ERROR_DEFS, d as isORPCErrorStatus, e as isORPCErrorJson, g as createORPCErrorFromJson, c as ORPCError, t as toORPCError } from './client.4qgEyLNQ.mjs';
|
|
4
|
+
import { toStandardHeaders as toStandardHeaders$1 } from '@orpc/standard-server-fetch';
|
|
5
|
+
import { m as mapEventIterator } from './client.BLtwTQUg.mjs';
|
|
4
6
|
|
|
5
|
-
class
|
|
7
|
+
class CompositeStandardLinkPlugin {
|
|
8
|
+
plugins;
|
|
9
|
+
constructor(plugins = []) {
|
|
10
|
+
this.plugins = [...plugins].sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
11
|
+
}
|
|
12
|
+
init(options) {
|
|
13
|
+
for (const plugin of this.plugins) {
|
|
14
|
+
plugin.init?.(options);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
6
17
|
}
|
|
18
|
+
|
|
7
19
|
class StandardLink {
|
|
8
20
|
constructor(codec, sender, options = {}) {
|
|
9
21
|
this.codec = codec;
|
|
10
22
|
this.sender = sender;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
23
|
+
const plugin = new CompositeStandardLinkPlugin(options.plugins);
|
|
24
|
+
plugin.init(options);
|
|
14
25
|
this.interceptors = toArray(options.interceptors);
|
|
15
26
|
this.clientInterceptors = toArray(options.clientInterceptors);
|
|
16
27
|
}
|
|
17
28
|
interceptors;
|
|
18
29
|
clientInterceptors;
|
|
19
30
|
call(path, input, options) {
|
|
20
|
-
return
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
return runWithSpan(
|
|
32
|
+
{ name: `${ORPC_NAME}.${path.join("/")}`, signal: options.signal },
|
|
33
|
+
(span) => {
|
|
34
|
+
span?.setAttribute("rpc.system", ORPC_NAME);
|
|
35
|
+
span?.setAttribute("rpc.method", path.join("."));
|
|
36
|
+
if (isAsyncIteratorObject(input)) {
|
|
37
|
+
input = asyncIteratorWithSpan(
|
|
38
|
+
{ name: "consume_event_iterator_input", signal: options.signal },
|
|
39
|
+
input
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
return intercept(this.interceptors, { ...options, path, input }, async ({ path: path2, input: input2, ...options2 }) => {
|
|
43
|
+
const otelConfig = getGlobalOtelConfig();
|
|
44
|
+
let otelContext;
|
|
45
|
+
const currentSpan = otelConfig?.trace.getActiveSpan() ?? span;
|
|
46
|
+
if (currentSpan && otelConfig) {
|
|
47
|
+
otelContext = otelConfig?.trace.setSpan(otelConfig.context.active(), currentSpan);
|
|
48
|
+
}
|
|
49
|
+
const request = await runWithSpan(
|
|
50
|
+
{ name: "encode_request", context: otelContext },
|
|
51
|
+
() => this.codec.encode(path2, input2, options2)
|
|
52
|
+
);
|
|
53
|
+
const response = await intercept(
|
|
54
|
+
this.clientInterceptors,
|
|
55
|
+
{ ...options2, input: input2, path: path2, request },
|
|
56
|
+
({ input: input3, path: path3, request: request2, ...options3 }) => {
|
|
57
|
+
return runWithSpan(
|
|
58
|
+
{ name: "send_request", signal: options3.signal, context: otelContext },
|
|
59
|
+
() => this.sender.call(request2, options3, path3, input3)
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
);
|
|
63
|
+
const output = await runWithSpan(
|
|
64
|
+
{ name: "decode_response", context: otelContext },
|
|
65
|
+
() => this.codec.decode(response, options2, path2, input2)
|
|
66
|
+
);
|
|
67
|
+
if (isAsyncIteratorObject(output)) {
|
|
68
|
+
return asyncIteratorWithSpan(
|
|
69
|
+
{ name: "consume_event_iterator_output", signal: options2.signal },
|
|
70
|
+
output
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
return output;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
31
76
|
);
|
|
32
|
-
const output = await this.codec.decode(response, options, path, input);
|
|
33
|
-
return output;
|
|
34
77
|
}
|
|
35
78
|
}
|
|
36
79
|
|
|
@@ -129,6 +172,9 @@ class StandardRPCJsonSerializer {
|
|
|
129
172
|
segments.forEach((segment) => {
|
|
130
173
|
currentRef = currentRef[preSegment];
|
|
131
174
|
preSegment = segment;
|
|
175
|
+
if (!Object.hasOwn(currentRef, preSegment)) {
|
|
176
|
+
throw new Error(`Security error: accessing non-existent path during deserialization. Path segment: ${preSegment}`);
|
|
177
|
+
}
|
|
132
178
|
});
|
|
133
179
|
currentRef[preSegment] = getBlob(i);
|
|
134
180
|
});
|
|
@@ -140,6 +186,9 @@ class StandardRPCJsonSerializer {
|
|
|
140
186
|
for (let i = 1; i < item.length; i++) {
|
|
141
187
|
currentRef = currentRef[preSegment];
|
|
142
188
|
preSegment = item[i];
|
|
189
|
+
if (!Object.hasOwn(currentRef, preSegment)) {
|
|
190
|
+
throw new Error(`Security error: accessing non-existent path during deserialization. Path segment: ${preSegment}`);
|
|
191
|
+
}
|
|
143
192
|
}
|
|
144
193
|
for (const custom of this.customSerializers) {
|
|
145
194
|
if (custom.type === type) {
|
|
@@ -183,6 +232,12 @@ class StandardRPCJsonSerializer {
|
|
|
183
232
|
function toHttpPath(path) {
|
|
184
233
|
return `/${path.map(encodeURIComponent).join("/")}`;
|
|
185
234
|
}
|
|
235
|
+
function toStandardHeaders(headers) {
|
|
236
|
+
if (typeof headers.forEach === "function") {
|
|
237
|
+
return toStandardHeaders$1(headers);
|
|
238
|
+
}
|
|
239
|
+
return headers;
|
|
240
|
+
}
|
|
186
241
|
function getMalformedResponseErrorCode(status) {
|
|
187
242
|
return Object.entries(COMMON_ORPC_ERROR_DEFS).find(([, def]) => def.status === status)?.[0] ?? "MALFORMED_ORPC_ERROR_RESPONSE";
|
|
188
243
|
}
|
|
@@ -202,18 +257,17 @@ class StandardRPCLinkCodec {
|
|
|
202
257
|
expectedMethod;
|
|
203
258
|
headers;
|
|
204
259
|
async encode(path, input, options) {
|
|
205
|
-
|
|
206
|
-
const expectedMethod = await value(this.expectedMethod, generalOptions);
|
|
207
|
-
let headers = await value(this.headers, generalOptions);
|
|
208
|
-
const baseUrl = await value(this.baseUrl, generalOptions);
|
|
209
|
-
const url = new URL(baseUrl);
|
|
210
|
-
url.pathname = `${url.pathname.replace(/\/$/, "")}${toHttpPath(path)}`;
|
|
260
|
+
let headers = toStandardHeaders(await value(this.headers, options, path, input));
|
|
211
261
|
if (options.lastEventId !== void 0) {
|
|
212
262
|
headers = mergeStandardHeaders(headers, { "last-event-id": options.lastEventId });
|
|
213
263
|
}
|
|
264
|
+
const expectedMethod = await value(this.expectedMethod, options, path, input);
|
|
265
|
+
const baseUrl = await value(this.baseUrl, options, path, input);
|
|
266
|
+
const url = new URL(baseUrl);
|
|
267
|
+
url.pathname = `${url.pathname.replace(/\/$/, "")}${toHttpPath(path)}`;
|
|
214
268
|
const serialized = this.serializer.serialize(input);
|
|
215
269
|
if (expectedMethod === "GET" && !(serialized instanceof FormData) && !isAsyncIteratorObject(serialized)) {
|
|
216
|
-
const maxUrlLength = await value(this.maxUrlLength,
|
|
270
|
+
const maxUrlLength = await value(this.maxUrlLength, options, path, input);
|
|
217
271
|
const getUrl = new URL(url);
|
|
218
272
|
getUrl.searchParams.append("data", stringifyJSON(serialized));
|
|
219
273
|
if (getUrl.toString().length <= maxUrlLength) {
|
|
@@ -254,12 +308,12 @@ class StandardRPCLinkCodec {
|
|
|
254
308
|
}
|
|
255
309
|
})();
|
|
256
310
|
if (!isOk) {
|
|
257
|
-
if (
|
|
258
|
-
throw
|
|
311
|
+
if (isORPCErrorJson(deserialized)) {
|
|
312
|
+
throw createORPCErrorFromJson(deserialized);
|
|
259
313
|
}
|
|
260
314
|
throw new ORPCError(getMalformedResponseErrorCode(response.status), {
|
|
261
315
|
status: response.status,
|
|
262
|
-
data: deserialized
|
|
316
|
+
data: { ...response, body: deserialized }
|
|
263
317
|
});
|
|
264
318
|
}
|
|
265
319
|
return deserialized;
|
|
@@ -309,8 +363,8 @@ class StandardRPCSerializer {
|
|
|
309
363
|
return e;
|
|
310
364
|
}
|
|
311
365
|
const deserialized = this.#deserialize(e.data);
|
|
312
|
-
if (
|
|
313
|
-
return
|
|
366
|
+
if (isORPCErrorJson(deserialized)) {
|
|
367
|
+
return createORPCErrorFromJson(deserialized, { cause: e });
|
|
314
368
|
}
|
|
315
369
|
return new ErrorEvent({
|
|
316
370
|
data: deserialized,
|
|
@@ -322,6 +376,9 @@ class StandardRPCSerializer {
|
|
|
322
376
|
return this.#deserialize(data);
|
|
323
377
|
}
|
|
324
378
|
#deserialize(data) {
|
|
379
|
+
if (data === void 0) {
|
|
380
|
+
return void 0;
|
|
381
|
+
}
|
|
325
382
|
if (!(data instanceof FormData)) {
|
|
326
383
|
return this.jsonSerializer.deserialize(data.json, data.meta ?? []);
|
|
327
384
|
}
|
|
@@ -335,4 +392,13 @@ class StandardRPCSerializer {
|
|
|
335
392
|
}
|
|
336
393
|
}
|
|
337
394
|
|
|
338
|
-
|
|
395
|
+
class StandardRPCLink extends StandardLink {
|
|
396
|
+
constructor(linkClient, options) {
|
|
397
|
+
const jsonSerializer = new StandardRPCJsonSerializer(options);
|
|
398
|
+
const serializer = new StandardRPCSerializer(jsonSerializer);
|
|
399
|
+
const linkCodec = new StandardRPCLinkCodec(serializer, options);
|
|
400
|
+
super(linkCodec, linkClient, options);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
export { CompositeStandardLinkPlugin as C, StandardLink as S, STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES as a, StandardRPCJsonSerializer as b, StandardRPCLink as c, StandardRPCLinkCodec as d, StandardRPCSerializer as e, toStandardHeaders as f, getMalformedResponseErrorCode as g, toHttpPath as t };
|
|
@@ -1,6 +1,16 @@
|
|
|
1
|
-
import { Interceptor
|
|
1
|
+
import { Interceptor } from '@orpc/shared';
|
|
2
2
|
import { StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
|
|
3
|
-
import {
|
|
3
|
+
import { b as ClientContext, c as ClientOptions, C as ClientLink } from './client.i2uoJbEp.mjs';
|
|
4
|
+
|
|
5
|
+
interface StandardLinkPlugin<T extends ClientContext> {
|
|
6
|
+
order?: number;
|
|
7
|
+
init?(options: StandardLinkOptions<T>): void;
|
|
8
|
+
}
|
|
9
|
+
declare class CompositeStandardLinkPlugin<T extends ClientContext, TPlugin extends StandardLinkPlugin<T>> implements StandardLinkPlugin<T> {
|
|
10
|
+
protected readonly plugins: TPlugin[];
|
|
11
|
+
constructor(plugins?: readonly TPlugin[]);
|
|
12
|
+
init(options: StandardLinkOptions<T>): void;
|
|
13
|
+
}
|
|
4
14
|
|
|
5
15
|
interface StandardLinkCodec<T extends ClientContext> {
|
|
6
16
|
encode(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<StandardRequest>;
|
|
@@ -10,11 +20,6 @@ interface StandardLinkClient<T extends ClientContext> {
|
|
|
10
20
|
call(request: StandardRequest, options: ClientOptions<T>, path: readonly string[], input: unknown): Promise<StandardLazyResponse>;
|
|
11
21
|
}
|
|
12
22
|
|
|
13
|
-
declare class InvalidEventIteratorRetryResponse extends Error {
|
|
14
|
-
}
|
|
15
|
-
interface StandardLinkPlugin<T extends ClientContext> {
|
|
16
|
-
init?(options: StandardLinkOptions<T>): void;
|
|
17
|
-
}
|
|
18
23
|
interface StandardLinkInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
|
|
19
24
|
path: readonly string[];
|
|
20
25
|
input: unknown;
|
|
@@ -23,12 +28,11 @@ interface StandardLinkClientInterceptorOptions<T extends ClientContext> extends
|
|
|
23
28
|
request: StandardRequest;
|
|
24
29
|
}
|
|
25
30
|
interface StandardLinkOptions<T extends ClientContext> {
|
|
26
|
-
interceptors?: Interceptor<StandardLinkInterceptorOptions<T>, unknown
|
|
27
|
-
clientInterceptors?: Interceptor<StandardLinkClientInterceptorOptions<T>, StandardLazyResponse
|
|
31
|
+
interceptors?: Interceptor<StandardLinkInterceptorOptions<T>, Promise<unknown>>[];
|
|
32
|
+
clientInterceptors?: Interceptor<StandardLinkClientInterceptorOptions<T>, Promise<StandardLazyResponse>>[];
|
|
28
33
|
plugins?: StandardLinkPlugin<T>[];
|
|
29
34
|
}
|
|
30
35
|
declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
|
|
31
|
-
#private;
|
|
32
36
|
readonly codec: StandardLinkCodec<T>;
|
|
33
37
|
readonly sender: StandardLinkClient<T>;
|
|
34
38
|
private readonly interceptors;
|
|
@@ -37,4 +41,5 @@ declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
|
|
|
37
41
|
call(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<unknown>;
|
|
38
42
|
}
|
|
39
43
|
|
|
40
|
-
export {
|
|
44
|
+
export { CompositeStandardLinkPlugin as C, StandardLink as d };
|
|
45
|
+
export type { StandardLinkClientInterceptorOptions as S, StandardLinkPlugin as a, StandardLinkOptions as b, StandardLinkInterceptorOptions as c, StandardLinkCodec as e, StandardLinkClient as f };
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { PromiseWithError } from '@orpc/shared';
|
|
2
|
+
|
|
3
|
+
type HTTPPath = `/${string}`;
|
|
4
|
+
type HTTPMethod = 'HEAD' | 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
5
|
+
type ClientContext = Record<PropertyKey, any>;
|
|
6
|
+
interface ClientOptions<T extends ClientContext> {
|
|
7
|
+
signal?: AbortSignal;
|
|
8
|
+
lastEventId?: string | undefined;
|
|
9
|
+
context: T;
|
|
10
|
+
}
|
|
11
|
+
type FriendlyClientOptions<T extends ClientContext> = Omit<ClientOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
|
12
|
+
context?: T;
|
|
13
|
+
} : {
|
|
14
|
+
context: T;
|
|
15
|
+
});
|
|
16
|
+
type ClientRest<TClientContext extends ClientContext, TInput> = Record<never, never> extends TClientContext ? undefined extends TInput ? [input?: TInput, options?: FriendlyClientOptions<TClientContext>] : [input: TInput, options?: FriendlyClientOptions<TClientContext>] : [input: TInput, options: FriendlyClientOptions<TClientContext>];
|
|
17
|
+
type ClientPromiseResult<TOutput, TError> = PromiseWithError<TOutput, TError>;
|
|
18
|
+
interface Client<TClientContext extends ClientContext, TInput, TOutput, TError> {
|
|
19
|
+
(...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
|
|
20
|
+
}
|
|
21
|
+
type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
|
|
22
|
+
[k: string]: NestedClient<TClientContext>;
|
|
23
|
+
};
|
|
24
|
+
type InferClientContext<T extends NestedClient<any>> = T extends NestedClient<infer U> ? U : never;
|
|
25
|
+
interface ClientLink<TClientContext extends ClientContext> {
|
|
26
|
+
call: (path: readonly string[], input: unknown, options: ClientOptions<TClientContext>) => Promise<unknown>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Recursively infers the **input types** from a client.
|
|
30
|
+
*
|
|
31
|
+
* Produces a nested map where each endpoint's input type is preserved.
|
|
32
|
+
*/
|
|
33
|
+
type InferClientInputs<T extends NestedClient<any>> = T extends Client<any, infer U, any, any> ? U : {
|
|
34
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? InferClientInputs<T[K]> : never;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Recursively infers the **body input types** from a client.
|
|
38
|
+
*
|
|
39
|
+
* If an endpoint's input includes `{ body: ... }`, only the `body` portion is extracted.
|
|
40
|
+
* Produces a nested map of body input types.
|
|
41
|
+
*/
|
|
42
|
+
type InferClientBodyInputs<T extends NestedClient<any>> = T extends Client<any, infer U, any, any> ? U extends {
|
|
43
|
+
body: infer UBody;
|
|
44
|
+
} ? UBody : U : {
|
|
45
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? InferClientBodyInputs<T[K]> : never;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Recursively infers the **output types** from a client.
|
|
49
|
+
*
|
|
50
|
+
* Produces a nested map where each endpoint's output type is preserved.
|
|
51
|
+
*/
|
|
52
|
+
type InferClientOutputs<T extends NestedClient<any>> = T extends Client<any, any, infer U, any> ? U : {
|
|
53
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? InferClientOutputs<T[K]> : never;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Recursively infers the **body output types** from a client.
|
|
57
|
+
*
|
|
58
|
+
* If an endpoint's output includes `{ body: ... }`, only the `body` portion is extracted.
|
|
59
|
+
* Produces a nested map of body output types.
|
|
60
|
+
*/
|
|
61
|
+
type InferClientBodyOutputs<T extends NestedClient<any>> = T extends Client<any, any, infer U, any> ? U extends {
|
|
62
|
+
body: infer UBody;
|
|
63
|
+
} ? UBody : U : {
|
|
64
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? InferClientBodyOutputs<T[K]> : never;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Recursively infers the **error types** from a client when you use [type-safe errors](https://orpc.dev/docs/error-handling#type‐safe-error-handling).
|
|
68
|
+
*
|
|
69
|
+
* Produces a nested map where each endpoint's error type is preserved.
|
|
70
|
+
*/
|
|
71
|
+
type InferClientErrors<T extends NestedClient<any>> = T extends Client<any, any, any, infer U> ? U : {
|
|
72
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? InferClientErrors<T[K]> : never;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Recursively infers a **union of all error types** from a client when you use [type-safe errors](https://orpc.dev/docs/error-handling#type‐safe-error-handling).
|
|
76
|
+
*
|
|
77
|
+
* Useful when you want to handle all possible errors from any endpoint at once.
|
|
78
|
+
*/
|
|
79
|
+
type InferClientErrorUnion<T extends NestedClient<any>> = T extends Client<any, any, any, infer U> ? U : {
|
|
80
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? InferClientErrorUnion<T[K]> : never;
|
|
81
|
+
}[keyof T];
|
|
82
|
+
|
|
83
|
+
export type { ClientLink as C, FriendlyClientOptions as F, HTTPPath as H, InferClientContext as I, NestedClient as N, ClientPromiseResult as a, ClientContext as b, ClientOptions as c, Client as d, ClientRest as e, HTTPMethod as f, InferClientInputs as g, InferClientBodyInputs as h, InferClientOutputs as i, InferClientBodyOutputs as j, InferClientErrors as k, InferClientErrorUnion as l };
|