@orpc/client 0.0.0-next.85e5dff → 0.0.0-next.8606faa
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 +22 -22
- 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 +7 -5
- package/dist/adapters/standard/index.d.ts +7 -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 +104 -29
- package/dist/index.d.ts +104 -29
- package/dist/index.mjs +58 -11
- package/dist/plugins/index.d.mts +209 -21
- package/dist/plugins/index.d.ts +209 -21
- package/dist/plugins/index.mjs +412 -53
- package/dist/shared/client.BBB2AT0F.mjs +171 -0
- package/dist/shared/client.BH1AYT_p.d.mts +83 -0
- package/dist/shared/client.BH1AYT_p.d.ts +83 -0
- package/dist/shared/client.BLtwTQUg.mjs +40 -0
- package/dist/shared/{client.D-jrSuDb.d.ts → client.BxV-mzeR.d.ts} +13 -25
- package/dist/shared/{client.grRbC25r.d.ts → client.CPgZaUox.d.mts} +21 -15
- package/dist/shared/{client.D77bw0hs.mjs → client.Cju0ZXxP.mjs} +95 -30
- package/dist/shared/{client.Bt94sjrK.d.mts → client.D8lMmWVC.d.mts} +13 -25
- package/dist/shared/{client.5813Ufvs.d.mts → client.De8SW4Kw.d.ts} +21 -15
- package/package.json +16 -5
- package/dist/shared/client.C0lT7w02.d.mts +0 -30
- package/dist/shared/client.C0lT7w02.d.ts +0 -30
- package/dist/shared/client.jKEwIsRd.mjs +0 -175
|
@@ -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.unnoq.com/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.unnoq.com/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 };
|
|
@@ -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.unnoq.com/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.unnoq.com/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 };
|
|
@@ -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,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.BH1AYT_p.js';
|
|
2
|
+
import { e as StandardLinkCodec, b as StandardLinkOptions, d as StandardLink, f as StandardLinkClient } from './client.De8SW4Kw.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,46 +44,30 @@ interface StandardRPCLinkCodecOptions<T extends ClientContext> {
|
|
|
44
44
|
/**
|
|
45
45
|
* Base url for all requests.
|
|
46
46
|
*/
|
|
47
|
-
url: Value<string | URL,
|
|
48
|
-
options: ClientOptions<T>,
|
|
49
|
-
path: readonly string[],
|
|
50
|
-
input: unknown
|
|
51
|
-
]>;
|
|
47
|
+
url: Value<Promisable<string | URL>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
52
48
|
/**
|
|
53
49
|
* The maximum length of the URL.
|
|
54
50
|
*
|
|
55
51
|
* @default 2083
|
|
56
52
|
*/
|
|
57
|
-
maxUrlLength?: Value<number,
|
|
58
|
-
options: ClientOptions<T>,
|
|
59
|
-
path: readonly string[],
|
|
60
|
-
input: unknown
|
|
61
|
-
]>;
|
|
53
|
+
maxUrlLength?: Value<Promisable<number>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
62
54
|
/**
|
|
63
55
|
* The method used to make the request.
|
|
64
56
|
*
|
|
65
57
|
* @default 'POST'
|
|
66
58
|
*/
|
|
67
|
-
method?: Value<HTTPMethod, [
|
|
68
|
-
options: ClientOptions<T>,
|
|
69
|
-
path: readonly string[],
|
|
70
|
-
input: unknown
|
|
71
|
-
]>;
|
|
59
|
+
method?: Value<Promisable<Exclude<HTTPMethod, 'HEAD'>>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
72
60
|
/**
|
|
73
61
|
* The method to use when the payload cannot safely pass to the server with method return from method function.
|
|
74
62
|
* GET is not allowed, it's very dangerous.
|
|
75
63
|
*
|
|
76
64
|
* @default 'POST'
|
|
77
65
|
*/
|
|
78
|
-
fallbackMethod?: Exclude<HTTPMethod, 'GET'>;
|
|
66
|
+
fallbackMethod?: Exclude<HTTPMethod, 'HEAD' | 'GET'>;
|
|
79
67
|
/**
|
|
80
68
|
* Inject headers to the request.
|
|
81
69
|
*/
|
|
82
|
-
headers?: Value<StandardHeaders,
|
|
83
|
-
options: ClientOptions<T>,
|
|
84
|
-
path: readonly string[],
|
|
85
|
-
input: unknown
|
|
86
|
-
]>;
|
|
70
|
+
headers?: Value<Promisable<StandardHeaders | Headers>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
87
71
|
}
|
|
88
72
|
declare class StandardRPCLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
|
|
89
73
|
private readonly serializer;
|
|
@@ -99,5 +83,9 @@ declare class StandardRPCLinkCodec<T extends ClientContext> implements StandardL
|
|
|
99
83
|
|
|
100
84
|
interface StandardRPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, StandardRPCLinkCodecOptions<T>, StandardRPCJsonSerializerOptions {
|
|
101
85
|
}
|
|
86
|
+
declare class StandardRPCLink<T extends ClientContext> extends StandardLink<T> {
|
|
87
|
+
constructor(linkClient: StandardLinkClient<T>, options: StandardRPCLinkOptions<T>);
|
|
88
|
+
}
|
|
102
89
|
|
|
103
|
-
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,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.BH1AYT_p.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,24 +20,19 @@ 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
|
-
|
|
23
|
+
interface StandardLinkInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
|
|
24
|
+
path: readonly string[];
|
|
25
|
+
input: unknown;
|
|
14
26
|
}
|
|
15
|
-
interface
|
|
16
|
-
|
|
27
|
+
interface StandardLinkClientInterceptorOptions<T extends ClientContext> extends StandardLinkInterceptorOptions<T> {
|
|
28
|
+
request: StandardRequest;
|
|
17
29
|
}
|
|
18
30
|
interface StandardLinkOptions<T extends ClientContext> {
|
|
19
|
-
interceptors?: Interceptor<
|
|
20
|
-
|
|
21
|
-
input: unknown;
|
|
22
|
-
options: ClientOptions<T>;
|
|
23
|
-
}, unknown, ThrowableError>[];
|
|
24
|
-
clientInterceptors?: Interceptor<{
|
|
25
|
-
request: StandardRequest;
|
|
26
|
-
}, StandardLazyResponse, ThrowableError>[];
|
|
31
|
+
interceptors?: Interceptor<StandardLinkInterceptorOptions<T>, Promise<unknown>>[];
|
|
32
|
+
clientInterceptors?: Interceptor<StandardLinkClientInterceptorOptions<T>, Promise<StandardLazyResponse>>[];
|
|
27
33
|
plugins?: StandardLinkPlugin<T>[];
|
|
28
34
|
}
|
|
29
35
|
declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
|
|
30
|
-
#private;
|
|
31
36
|
readonly codec: StandardLinkCodec<T>;
|
|
32
37
|
readonly sender: StandardLinkClient<T>;
|
|
33
38
|
private readonly interceptors;
|
|
@@ -36,4 +41,5 @@ declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
|
|
|
36
41
|
call(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<unknown>;
|
|
37
42
|
}
|
|
38
43
|
|
|
39
|
-
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 };
|
|
@@ -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 {
|
|
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.BBB2AT0F.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
|
|
|
@@ -183,6 +226,15 @@ class StandardRPCJsonSerializer {
|
|
|
183
226
|
function toHttpPath(path) {
|
|
184
227
|
return `/${path.map(encodeURIComponent).join("/")}`;
|
|
185
228
|
}
|
|
229
|
+
function toStandardHeaders(headers) {
|
|
230
|
+
if (typeof headers.forEach === "function") {
|
|
231
|
+
return toStandardHeaders$1(headers);
|
|
232
|
+
}
|
|
233
|
+
return headers;
|
|
234
|
+
}
|
|
235
|
+
function getMalformedResponseErrorCode(status) {
|
|
236
|
+
return Object.entries(COMMON_ORPC_ERROR_DEFS).find(([, def]) => def.status === status)?.[0] ?? "MALFORMED_ORPC_ERROR_RESPONSE";
|
|
237
|
+
}
|
|
186
238
|
|
|
187
239
|
class StandardRPCLinkCodec {
|
|
188
240
|
constructor(serializer, options) {
|
|
@@ -199,13 +251,14 @@ class StandardRPCLinkCodec {
|
|
|
199
251
|
expectedMethod;
|
|
200
252
|
headers;
|
|
201
253
|
async encode(path, input, options) {
|
|
202
|
-
|
|
203
|
-
let headers = await value(this.headers, options, path, input);
|
|
204
|
-
const baseUrl = await value(this.baseUrl, options, path, input);
|
|
205
|
-
const url = new URL(`${baseUrl.toString().replace(/\/$/, "")}${toHttpPath(path)}`);
|
|
254
|
+
let headers = toStandardHeaders(await value(this.headers, options, path, input));
|
|
206
255
|
if (options.lastEventId !== void 0) {
|
|
207
256
|
headers = mergeStandardHeaders(headers, { "last-event-id": options.lastEventId });
|
|
208
257
|
}
|
|
258
|
+
const expectedMethod = await value(this.expectedMethod, options, path, input);
|
|
259
|
+
const baseUrl = await value(this.baseUrl, options, path, input);
|
|
260
|
+
const url = new URL(baseUrl);
|
|
261
|
+
url.pathname = `${url.pathname.replace(/\/$/, "")}${toHttpPath(path)}`;
|
|
209
262
|
const serialized = this.serializer.serialize(input);
|
|
210
263
|
if (expectedMethod === "GET" && !(serialized instanceof FormData) && !isAsyncIteratorObject(serialized)) {
|
|
211
264
|
const maxUrlLength = await value(this.maxUrlLength, options, path, input);
|
|
@@ -249,12 +302,12 @@ class StandardRPCLinkCodec {
|
|
|
249
302
|
}
|
|
250
303
|
})();
|
|
251
304
|
if (!isOk) {
|
|
252
|
-
if (
|
|
253
|
-
throw
|
|
305
|
+
if (isORPCErrorJson(deserialized)) {
|
|
306
|
+
throw createORPCErrorFromJson(deserialized);
|
|
254
307
|
}
|
|
255
|
-
throw new ORPCError(
|
|
308
|
+
throw new ORPCError(getMalformedResponseErrorCode(response.status), {
|
|
256
309
|
status: response.status,
|
|
257
|
-
data: deserialized
|
|
310
|
+
data: { ...response, body: deserialized }
|
|
258
311
|
});
|
|
259
312
|
}
|
|
260
313
|
return deserialized;
|
|
@@ -304,8 +357,8 @@ class StandardRPCSerializer {
|
|
|
304
357
|
return e;
|
|
305
358
|
}
|
|
306
359
|
const deserialized = this.#deserialize(e.data);
|
|
307
|
-
if (
|
|
308
|
-
return
|
|
360
|
+
if (isORPCErrorJson(deserialized)) {
|
|
361
|
+
return createORPCErrorFromJson(deserialized, { cause: e });
|
|
309
362
|
}
|
|
310
363
|
return new ErrorEvent({
|
|
311
364
|
data: deserialized,
|
|
@@ -317,6 +370,9 @@ class StandardRPCSerializer {
|
|
|
317
370
|
return this.#deserialize(data);
|
|
318
371
|
}
|
|
319
372
|
#deserialize(data) {
|
|
373
|
+
if (data === void 0) {
|
|
374
|
+
return void 0;
|
|
375
|
+
}
|
|
320
376
|
if (!(data instanceof FormData)) {
|
|
321
377
|
return this.jsonSerializer.deserialize(data.json, data.meta ?? []);
|
|
322
378
|
}
|
|
@@ -330,4 +386,13 @@ class StandardRPCSerializer {
|
|
|
330
386
|
}
|
|
331
387
|
}
|
|
332
388
|
|
|
333
|
-
|
|
389
|
+
class StandardRPCLink extends StandardLink {
|
|
390
|
+
constructor(linkClient, options) {
|
|
391
|
+
const jsonSerializer = new StandardRPCJsonSerializer(options);
|
|
392
|
+
const serializer = new StandardRPCSerializer(jsonSerializer);
|
|
393
|
+
const linkCodec = new StandardRPCLinkCodec(serializer, options);
|
|
394
|
+
super(linkCodec, linkClient, options);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
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,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.BH1AYT_p.mjs';
|
|
2
|
+
import { e as StandardLinkCodec, b as StandardLinkOptions, d as StandardLink, f as StandardLinkClient } from './client.CPgZaUox.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,46 +44,30 @@ interface StandardRPCLinkCodecOptions<T extends ClientContext> {
|
|
|
44
44
|
/**
|
|
45
45
|
* Base url for all requests.
|
|
46
46
|
*/
|
|
47
|
-
url: Value<string | URL,
|
|
48
|
-
options: ClientOptions<T>,
|
|
49
|
-
path: readonly string[],
|
|
50
|
-
input: unknown
|
|
51
|
-
]>;
|
|
47
|
+
url: Value<Promisable<string | URL>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
52
48
|
/**
|
|
53
49
|
* The maximum length of the URL.
|
|
54
50
|
*
|
|
55
51
|
* @default 2083
|
|
56
52
|
*/
|
|
57
|
-
maxUrlLength?: Value<number,
|
|
58
|
-
options: ClientOptions<T>,
|
|
59
|
-
path: readonly string[],
|
|
60
|
-
input: unknown
|
|
61
|
-
]>;
|
|
53
|
+
maxUrlLength?: Value<Promisable<number>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
62
54
|
/**
|
|
63
55
|
* The method used to make the request.
|
|
64
56
|
*
|
|
65
57
|
* @default 'POST'
|
|
66
58
|
*/
|
|
67
|
-
method?: Value<HTTPMethod, [
|
|
68
|
-
options: ClientOptions<T>,
|
|
69
|
-
path: readonly string[],
|
|
70
|
-
input: unknown
|
|
71
|
-
]>;
|
|
59
|
+
method?: Value<Promisable<Exclude<HTTPMethod, 'HEAD'>>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
72
60
|
/**
|
|
73
61
|
* The method to use when the payload cannot safely pass to the server with method return from method function.
|
|
74
62
|
* GET is not allowed, it's very dangerous.
|
|
75
63
|
*
|
|
76
64
|
* @default 'POST'
|
|
77
65
|
*/
|
|
78
|
-
fallbackMethod?: Exclude<HTTPMethod, 'GET'>;
|
|
66
|
+
fallbackMethod?: Exclude<HTTPMethod, 'HEAD' | 'GET'>;
|
|
79
67
|
/**
|
|
80
68
|
* Inject headers to the request.
|
|
81
69
|
*/
|
|
82
|
-
headers?: Value<StandardHeaders,
|
|
83
|
-
options: ClientOptions<T>,
|
|
84
|
-
path: readonly string[],
|
|
85
|
-
input: unknown
|
|
86
|
-
]>;
|
|
70
|
+
headers?: Value<Promisable<StandardHeaders | Headers>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
87
71
|
}
|
|
88
72
|
declare class StandardRPCLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
|
|
89
73
|
private readonly serializer;
|
|
@@ -99,5 +83,9 @@ declare class StandardRPCLinkCodec<T extends ClientContext> implements StandardL
|
|
|
99
83
|
|
|
100
84
|
interface StandardRPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, StandardRPCLinkCodecOptions<T>, StandardRPCJsonSerializerOptions {
|
|
101
85
|
}
|
|
86
|
+
declare class StandardRPCLink<T extends ClientContext> extends StandardLink<T> {
|
|
87
|
+
constructor(linkClient: StandardLinkClient<T>, options: StandardRPCLinkOptions<T>);
|
|
88
|
+
}
|
|
102
89
|
|
|
103
|
-
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,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.BH1AYT_p.js';
|
|
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,24 +20,19 @@ 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
|
-
|
|
23
|
+
interface StandardLinkInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
|
|
24
|
+
path: readonly string[];
|
|
25
|
+
input: unknown;
|
|
14
26
|
}
|
|
15
|
-
interface
|
|
16
|
-
|
|
27
|
+
interface StandardLinkClientInterceptorOptions<T extends ClientContext> extends StandardLinkInterceptorOptions<T> {
|
|
28
|
+
request: StandardRequest;
|
|
17
29
|
}
|
|
18
30
|
interface StandardLinkOptions<T extends ClientContext> {
|
|
19
|
-
interceptors?: Interceptor<
|
|
20
|
-
|
|
21
|
-
input: unknown;
|
|
22
|
-
options: ClientOptions<T>;
|
|
23
|
-
}, unknown, ThrowableError>[];
|
|
24
|
-
clientInterceptors?: Interceptor<{
|
|
25
|
-
request: StandardRequest;
|
|
26
|
-
}, StandardLazyResponse, ThrowableError>[];
|
|
31
|
+
interceptors?: Interceptor<StandardLinkInterceptorOptions<T>, Promise<unknown>>[];
|
|
32
|
+
clientInterceptors?: Interceptor<StandardLinkClientInterceptorOptions<T>, Promise<StandardLazyResponse>>[];
|
|
27
33
|
plugins?: StandardLinkPlugin<T>[];
|
|
28
34
|
}
|
|
29
35
|
declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
|
|
30
|
-
#private;
|
|
31
36
|
readonly codec: StandardLinkCodec<T>;
|
|
32
37
|
readonly sender: StandardLinkClient<T>;
|
|
33
38
|
private readonly interceptors;
|
|
@@ -36,4 +41,5 @@ declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
|
|
|
36
41
|
call(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<unknown>;
|
|
37
42
|
}
|
|
38
43
|
|
|
39
|
-
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 };
|