@orpc/client 0.0.0-next.999d654 → 0.0.0-next.9a53484

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.
Files changed (33) hide show
  1. package/README.md +36 -25
  2. package/dist/adapters/fetch/index.d.mts +31 -14
  3. package/dist/adapters/fetch/index.d.ts +31 -14
  4. package/dist/adapters/fetch/index.mjs +27 -16
  5. package/dist/adapters/message-port/index.d.mts +80 -0
  6. package/dist/adapters/message-port/index.d.ts +80 -0
  7. package/dist/adapters/message-port/index.mjs +87 -0
  8. package/dist/adapters/standard/index.d.mts +9 -146
  9. package/dist/adapters/standard/index.d.ts +9 -146
  10. package/dist/adapters/standard/index.mjs +4 -2
  11. package/dist/adapters/websocket/index.d.mts +29 -0
  12. package/dist/adapters/websocket/index.d.ts +29 -0
  13. package/dist/adapters/websocket/index.mjs +47 -0
  14. package/dist/index.d.mts +107 -32
  15. package/dist/index.d.ts +107 -32
  16. package/dist/index.mjs +85 -36
  17. package/dist/plugins/index.d.mts +249 -0
  18. package/dist/plugins/index.d.ts +249 -0
  19. package/dist/plugins/index.mjs +485 -0
  20. package/dist/shared/client.2jUAqzYU.d.ts +45 -0
  21. package/dist/shared/client.B3pNRBih.d.ts +91 -0
  22. package/dist/shared/client.BFAVy68H.d.mts +91 -0
  23. package/dist/shared/client.BLtwTQUg.mjs +40 -0
  24. package/dist/shared/client.CFpGQEdV.mjs +171 -0
  25. package/dist/shared/client.CifbFNOC.mjs +398 -0
  26. package/dist/shared/client.CpCa3si8.d.mts +45 -0
  27. package/dist/shared/client.i2uoJbEp.d.mts +83 -0
  28. package/dist/shared/client.i2uoJbEp.d.ts +83 -0
  29. package/package.json +22 -6
  30. package/dist/shared/client.D_CzLDyB.d.mts +0 -42
  31. package/dist/shared/client.D_CzLDyB.d.ts +0 -42
  32. package/dist/shared/client.Df5pd75N.mjs +0 -320
  33. package/dist/shared/client.XAn8cDTM.mjs +0 -266
@@ -0,0 +1,45 @@
1
+ import { Interceptor } from '@orpc/shared';
2
+ import { StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
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
+ }
14
+
15
+ interface StandardLinkCodec<T extends ClientContext> {
16
+ encode(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<StandardRequest>;
17
+ decode(response: StandardLazyResponse, options: ClientOptions<T>, path: readonly string[], input: unknown): Promise<unknown>;
18
+ }
19
+ interface StandardLinkClient<T extends ClientContext> {
20
+ call(request: StandardRequest, options: ClientOptions<T>, path: readonly string[], input: unknown): Promise<StandardLazyResponse>;
21
+ }
22
+
23
+ interface StandardLinkInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
24
+ path: readonly string[];
25
+ input: unknown;
26
+ }
27
+ interface StandardLinkClientInterceptorOptions<T extends ClientContext> extends StandardLinkInterceptorOptions<T> {
28
+ request: StandardRequest;
29
+ }
30
+ interface StandardLinkOptions<T extends ClientContext> {
31
+ interceptors?: Interceptor<StandardLinkInterceptorOptions<T>, Promise<unknown>>[];
32
+ clientInterceptors?: Interceptor<StandardLinkClientInterceptorOptions<T>, Promise<StandardLazyResponse>>[];
33
+ plugins?: StandardLinkPlugin<T>[];
34
+ }
35
+ declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
36
+ readonly codec: StandardLinkCodec<T>;
37
+ readonly sender: StandardLinkClient<T>;
38
+ private readonly interceptors;
39
+ private readonly clientInterceptors;
40
+ constructor(codec: StandardLinkCodec<T>, sender: StandardLinkClient<T>, options?: StandardLinkOptions<T>);
41
+ call(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<unknown>;
42
+ }
43
+
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 };
@@ -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 };
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@orpc/client",
3
3
  "type": "module",
4
- "version": "0.0.0-next.999d654",
4
+ "version": "0.0.0-next.9a53484",
5
5
  "license": "MIT",
6
- "homepage": "https://orpc.unnoq.com",
6
+ "homepage": "https://orpc.dev",
7
7
  "repository": {
8
8
  "type": "git",
9
9
  "url": "git+https://github.com/unnoq/orpc.git",
@@ -19,6 +19,11 @@
19
19
  "import": "./dist/index.mjs",
20
20
  "default": "./dist/index.mjs"
21
21
  },
22
+ "./plugins": {
23
+ "types": "./dist/plugins/index.d.mts",
24
+ "import": "./dist/plugins/index.mjs",
25
+ "default": "./dist/plugins/index.mjs"
26
+ },
22
27
  "./standard": {
23
28
  "types": "./dist/adapters/standard/index.d.mts",
24
29
  "import": "./dist/adapters/standard/index.mjs",
@@ -28,18 +33,29 @@
28
33
  "types": "./dist/adapters/fetch/index.d.mts",
29
34
  "import": "./dist/adapters/fetch/index.mjs",
30
35
  "default": "./dist/adapters/fetch/index.mjs"
36
+ },
37
+ "./websocket": {
38
+ "types": "./dist/adapters/websocket/index.d.mts",
39
+ "import": "./dist/adapters/websocket/index.mjs",
40
+ "default": "./dist/adapters/websocket/index.mjs"
41
+ },
42
+ "./message-port": {
43
+ "types": "./dist/adapters/message-port/index.d.mts",
44
+ "import": "./dist/adapters/message-port/index.mjs",
45
+ "default": "./dist/adapters/message-port/index.mjs"
31
46
  }
32
47
  },
33
48
  "files": [
34
49
  "dist"
35
50
  ],
36
51
  "dependencies": {
37
- "@orpc/standard-server": "0.0.0-next.999d654",
38
- "@orpc/standard-server-fetch": "0.0.0-next.999d654",
39
- "@orpc/shared": "0.0.0-next.999d654"
52
+ "@orpc/shared": "0.0.0-next.9a53484",
53
+ "@orpc/standard-server": "0.0.0-next.9a53484",
54
+ "@orpc/standard-server-fetch": "0.0.0-next.9a53484",
55
+ "@orpc/standard-server-peer": "0.0.0-next.9a53484"
40
56
  },
41
57
  "devDependencies": {
42
- "zod": "^3.24.2"
58
+ "zod": "^4.1.12"
43
59
  },
44
60
  "scripts": {
45
61
  "build": "unbuild",
@@ -1,42 +0,0 @@
1
- type ClientContext = Record<string, any>;
2
- type ClientOptions<TClientContext extends ClientContext> = {
3
- signal?: AbortSignal;
4
- lastEventId?: string | undefined;
5
- } & (Record<never, never> extends TClientContext ? {
6
- context?: TClientContext;
7
- } : {
8
- context: TClientContext;
9
- });
10
- type ClientRest<TClientContext extends ClientContext, TInput> = Record<never, never> extends TClientContext ? undefined extends TInput ? [input?: TInput, options?: ClientOptions<TClientContext>] : [input: TInput, options?: ClientOptions<TClientContext>] : [input: TInput, options: ClientOptions<TClientContext>];
11
- type ClientPromiseResult<TOutput, TError extends Error> = Promise<TOutput> & {
12
- __error?: {
13
- type: TError;
14
- };
15
- };
16
- interface Client<TClientContext extends ClientContext, TInput, TOutput, TError extends Error> {
17
- (...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
18
- }
19
- type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
20
- [k: string]: NestedClient<TClientContext>;
21
- };
22
- type InferClientContext<T extends NestedClient<any>> = T extends NestedClient<infer U> ? U : never;
23
- type ClientOptionsOut<TClientContext extends ClientContext> = ClientOptions<TClientContext> & {
24
- context: TClientContext;
25
- };
26
- interface ClientLink<TClientContext extends ClientContext> {
27
- call: (path: readonly string[], input: unknown, options: ClientOptionsOut<TClientContext>) => Promise<unknown>;
28
- }
29
-
30
- declare function mapEventIterator<TYield, TReturn, TNext, TMap = TYield | TReturn>(iterator: AsyncIterator<TYield, TReturn, TNext>, maps: {
31
- value: (value: NoInfer<TYield | TReturn>, done: boolean | undefined) => Promise<TMap>;
32
- error: (error: unknown) => Promise<unknown>;
33
- }): AsyncGenerator<TMap, TMap, TNext>;
34
- interface EventIteratorReconnectOptions {
35
- lastRetry: number | undefined;
36
- lastEventId: string | undefined;
37
- retryTimes: number;
38
- error: unknown;
39
- }
40
- declare function createAutoRetryEventIterator<TYield, TReturn>(initial: AsyncIterator<TYield, TReturn, void>, reconnect: (options: EventIteratorReconnectOptions) => Promise<AsyncIterator<TYield, TReturn, void> | null>, initialLastEventId: string | undefined): AsyncGenerator<TYield, TReturn, void>;
41
-
42
- export { type ClientContext as C, type EventIteratorReconnectOptions as E, type InferClientContext as I, type NestedClient as N, type ClientOptionsOut as a, type ClientLink as b, type ClientPromiseResult as c, createAutoRetryEventIterator as d, type ClientOptions as e, type ClientRest as f, type Client as g, mapEventIterator as m };
@@ -1,42 +0,0 @@
1
- type ClientContext = Record<string, any>;
2
- type ClientOptions<TClientContext extends ClientContext> = {
3
- signal?: AbortSignal;
4
- lastEventId?: string | undefined;
5
- } & (Record<never, never> extends TClientContext ? {
6
- context?: TClientContext;
7
- } : {
8
- context: TClientContext;
9
- });
10
- type ClientRest<TClientContext extends ClientContext, TInput> = Record<never, never> extends TClientContext ? undefined extends TInput ? [input?: TInput, options?: ClientOptions<TClientContext>] : [input: TInput, options?: ClientOptions<TClientContext>] : [input: TInput, options: ClientOptions<TClientContext>];
11
- type ClientPromiseResult<TOutput, TError extends Error> = Promise<TOutput> & {
12
- __error?: {
13
- type: TError;
14
- };
15
- };
16
- interface Client<TClientContext extends ClientContext, TInput, TOutput, TError extends Error> {
17
- (...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
18
- }
19
- type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
20
- [k: string]: NestedClient<TClientContext>;
21
- };
22
- type InferClientContext<T extends NestedClient<any>> = T extends NestedClient<infer U> ? U : never;
23
- type ClientOptionsOut<TClientContext extends ClientContext> = ClientOptions<TClientContext> & {
24
- context: TClientContext;
25
- };
26
- interface ClientLink<TClientContext extends ClientContext> {
27
- call: (path: readonly string[], input: unknown, options: ClientOptionsOut<TClientContext>) => Promise<unknown>;
28
- }
29
-
30
- declare function mapEventIterator<TYield, TReturn, TNext, TMap = TYield | TReturn>(iterator: AsyncIterator<TYield, TReturn, TNext>, maps: {
31
- value: (value: NoInfer<TYield | TReturn>, done: boolean | undefined) => Promise<TMap>;
32
- error: (error: unknown) => Promise<unknown>;
33
- }): AsyncGenerator<TMap, TMap, TNext>;
34
- interface EventIteratorReconnectOptions {
35
- lastRetry: number | undefined;
36
- lastEventId: string | undefined;
37
- retryTimes: number;
38
- error: unknown;
39
- }
40
- declare function createAutoRetryEventIterator<TYield, TReturn>(initial: AsyncIterator<TYield, TReturn, void>, reconnect: (options: EventIteratorReconnectOptions) => Promise<AsyncIterator<TYield, TReturn, void> | null>, initialLastEventId: string | undefined): AsyncGenerator<TYield, TReturn, void>;
41
-
42
- export { type ClientContext as C, type EventIteratorReconnectOptions as E, type InferClientContext as I, type NestedClient as N, type ClientOptionsOut as a, type ClientLink as b, type ClientPromiseResult as c, createAutoRetryEventIterator as d, type ClientOptions as e, type ClientRest as f, type Client as g, mapEventIterator as m };
@@ -1,320 +0,0 @@
1
- import { intercept, isAsyncIteratorObject, value, isObject, trim, stringifyJSON } from '@orpc/shared';
2
- import { c as createAutoRetryEventIterator, O as ORPCError, m as mapEventIterator, t as toORPCError } from './client.XAn8cDTM.mjs';
3
- import { ErrorEvent } from '@orpc/standard-server';
4
-
5
- class InvalidEventIteratorRetryResponse extends Error {
6
- }
7
- class StandardLink {
8
- constructor(codec, sender, options) {
9
- this.codec = codec;
10
- this.sender = sender;
11
- this.eventIteratorMaxRetries = options.eventIteratorMaxRetries ?? 5;
12
- this.eventIteratorRetryDelay = options.eventIteratorRetryDelay ?? ((o) => o.lastRetry ?? 1e3 * 2 ** o.retryTimes);
13
- this.eventIteratorShouldRetry = options.eventIteratorShouldRetry ?? true;
14
- this.interceptors = options.interceptors ?? [];
15
- this.clientInterceptors = options.clientInterceptors ?? [];
16
- }
17
- eventIteratorMaxRetries;
18
- eventIteratorRetryDelay;
19
- eventIteratorShouldRetry;
20
- interceptors;
21
- clientInterceptors;
22
- call(path, input, options) {
23
- return intercept(this.interceptors, { path, input, options }, async ({ path: path2, input: input2, options: options2 }) => {
24
- const output = await this.#call(path2, input2, options2);
25
- if (!isAsyncIteratorObject(output)) {
26
- return output;
27
- }
28
- return createAutoRetryEventIterator(output, async (reconnectOptions) => {
29
- const maxRetries = await value(this.eventIteratorMaxRetries, reconnectOptions, options2, path2, input2);
30
- if (options2.signal?.aborted || reconnectOptions.retryTimes > maxRetries) {
31
- return null;
32
- }
33
- const shouldRetry = await value(this.eventIteratorShouldRetry, reconnectOptions, options2, path2, input2);
34
- if (!shouldRetry) {
35
- return null;
36
- }
37
- const retryDelay = await value(this.eventIteratorRetryDelay, reconnectOptions, options2, path2, input2);
38
- await new Promise((resolve) => setTimeout(resolve, retryDelay));
39
- const updatedOptions = { ...options2, lastEventId: reconnectOptions.lastEventId };
40
- const maybeIterator = await this.#call(path2, input2, updatedOptions);
41
- if (!isAsyncIteratorObject(maybeIterator)) {
42
- throw new InvalidEventIteratorRetryResponse("Invalid Event Iterator retry response");
43
- }
44
- return maybeIterator;
45
- }, options2.lastEventId);
46
- });
47
- }
48
- async #call(path, input, options) {
49
- const request = await this.codec.encode(path, input, options);
50
- const response = await intercept(
51
- this.clientInterceptors,
52
- { request },
53
- ({ request: request2 }) => this.sender.call(request2, options, path, input)
54
- );
55
- const output = await this.codec.decode(response, options, path, input);
56
- return output;
57
- }
58
- }
59
-
60
- class RPCJsonSerializer {
61
- serialize(data, segments = [], meta = [], maps = [], blobs = []) {
62
- if (data instanceof Blob) {
63
- maps.push(segments);
64
- blobs.push(data);
65
- return [data, meta, maps, blobs];
66
- }
67
- if (typeof data === "bigint") {
68
- meta.push([0, segments]);
69
- return [data.toString(), meta, maps, blobs];
70
- }
71
- if (data instanceof Date) {
72
- meta.push([1, segments]);
73
- if (Number.isNaN(data.getTime())) {
74
- return [null, meta, maps, blobs];
75
- }
76
- return [data.toISOString(), meta, maps, blobs];
77
- }
78
- if (Number.isNaN(data)) {
79
- meta.push([2, segments]);
80
- return [null, meta, maps, blobs];
81
- }
82
- if (data instanceof URL) {
83
- meta.push([4, segments]);
84
- return [data.toString(), meta, maps, blobs];
85
- }
86
- if (data instanceof RegExp) {
87
- meta.push([5, segments]);
88
- return [data.toString(), meta, maps, blobs];
89
- }
90
- if (data instanceof Set) {
91
- const result = this.serialize(Array.from(data), segments, meta, maps, blobs);
92
- meta.push([6, segments]);
93
- return result;
94
- }
95
- if (data instanceof Map) {
96
- const result = this.serialize(Array.from(data.entries()), segments, meta, maps, blobs);
97
- meta.push([7, segments]);
98
- return result;
99
- }
100
- if (Array.isArray(data)) {
101
- const json = data.map((v, i) => {
102
- if (v === void 0) {
103
- meta.push([3, [...segments, i]]);
104
- return v;
105
- }
106
- return this.serialize(v, [...segments, i], meta, maps, blobs)[0];
107
- });
108
- return [json, meta, maps, blobs];
109
- }
110
- if (isObject(data)) {
111
- const json = {};
112
- for (const k in data) {
113
- json[k] = this.serialize(data[k], [...segments, k], meta, maps, blobs)[0];
114
- }
115
- return [json, meta, maps, blobs];
116
- }
117
- return [data, meta, maps, blobs];
118
- }
119
- deserialize(json, meta, maps, getBlob) {
120
- const ref = { data: json };
121
- if (maps && getBlob) {
122
- maps.forEach((segments, i) => {
123
- let currentRef = ref;
124
- let preSegment = "data";
125
- segments.forEach((segment) => {
126
- currentRef = currentRef[preSegment];
127
- preSegment = segment;
128
- });
129
- currentRef[preSegment] = getBlob(i);
130
- });
131
- }
132
- for (const [type, segments] of meta) {
133
- let currentRef = ref;
134
- let preSegment = "data";
135
- segments.forEach((segment) => {
136
- currentRef = currentRef[preSegment];
137
- preSegment = segment;
138
- });
139
- switch (type) {
140
- case 0:
141
- currentRef[preSegment] = BigInt(currentRef[preSegment]);
142
- break;
143
- case 1:
144
- currentRef[preSegment] = new Date(currentRef[preSegment] ?? "Invalid Date");
145
- break;
146
- case 2:
147
- currentRef[preSegment] = Number.NaN;
148
- break;
149
- case 3:
150
- currentRef[preSegment] = void 0;
151
- break;
152
- case 4:
153
- currentRef[preSegment] = new URL(currentRef[preSegment]);
154
- break;
155
- case 5: {
156
- const [, pattern, flags] = currentRef[preSegment].match(/^\/(.*)\/([a-z]*)$/);
157
- currentRef[preSegment] = new RegExp(pattern, flags);
158
- break;
159
- }
160
- case 6:
161
- currentRef[preSegment] = new Set(currentRef[preSegment]);
162
- break;
163
- case 7:
164
- currentRef[preSegment] = new Map(currentRef[preSegment]);
165
- break;
166
- }
167
- }
168
- return ref.data;
169
- }
170
- }
171
-
172
- class StandardRPCLinkCodec {
173
- constructor(serializer, options) {
174
- this.serializer = serializer;
175
- this.baseUrl = options.url;
176
- this.maxUrlLength = options.maxUrlLength ?? 2083;
177
- this.fallbackMethod = options.fallbackMethod ?? "POST";
178
- this.expectedMethod = options.method ?? this.fallbackMethod;
179
- this.headers = options.headers ?? {};
180
- }
181
- baseUrl;
182
- maxUrlLength;
183
- fallbackMethod;
184
- expectedMethod;
185
- headers;
186
- async encode(path, input, options) {
187
- const expectedMethod = await value(this.expectedMethod, options, path, input);
188
- const headers = await value(this.headers, options, path, input);
189
- const baseUrl = await value(this.baseUrl, options, path, input);
190
- const url = new URL(`${trim(baseUrl.toString(), "/")}/${path.map(encodeURIComponent).join("/")}`);
191
- const serialized = this.serializer.serialize(input);
192
- if (expectedMethod === "GET" && !(serialized instanceof FormData) && !(serialized instanceof Blob) && !isAsyncIteratorObject(serialized)) {
193
- const maxUrlLength = await value(this.maxUrlLength, options, path, input);
194
- const getUrl = new URL(url);
195
- getUrl.searchParams.append("data", stringifyJSON(serialized) ?? "");
196
- if (getUrl.toString().length <= maxUrlLength) {
197
- return {
198
- body: void 0,
199
- method: expectedMethod,
200
- headers,
201
- url: getUrl,
202
- signal: options.signal
203
- };
204
- }
205
- }
206
- return {
207
- url,
208
- method: expectedMethod === "GET" ? this.fallbackMethod : expectedMethod,
209
- headers,
210
- body: serialized,
211
- signal: options.signal
212
- };
213
- }
214
- async decode(response) {
215
- const isOk = response.status >= 200 && response.status < 300;
216
- const deserialized = await (async () => {
217
- let isBodyOk = false;
218
- try {
219
- const body = await response.body();
220
- isBodyOk = true;
221
- return this.serializer.deserialize(body);
222
- } catch (error) {
223
- if (!isBodyOk) {
224
- throw new Error("Cannot parse response body, please check the response body and content-type.", {
225
- cause: error
226
- });
227
- }
228
- throw new Error("Invalid RPC response format.", {
229
- cause: error
230
- });
231
- }
232
- })();
233
- if (!isOk) {
234
- if (ORPCError.isValidJSON(deserialized)) {
235
- throw ORPCError.fromJSON(deserialized);
236
- }
237
- throw new Error("Invalid RPC error response format.", {
238
- cause: deserialized
239
- });
240
- }
241
- return deserialized;
242
- }
243
- }
244
-
245
- class RPCSerializer {
246
- constructor(jsonSerializer = new RPCJsonSerializer()) {
247
- this.jsonSerializer = jsonSerializer;
248
- }
249
- serialize(data) {
250
- if (isAsyncIteratorObject(data)) {
251
- return mapEventIterator(data, {
252
- value: async (value) => this.#serialize(value, false),
253
- error: async (e) => {
254
- return new ErrorEvent({
255
- data: this.#serialize(toORPCError(e).toJSON(), false),
256
- cause: e
257
- });
258
- }
259
- });
260
- }
261
- return this.#serialize(data, true);
262
- }
263
- #serialize(data, enableFormData) {
264
- if (data === void 0 || data instanceof Blob) {
265
- return data;
266
- }
267
- const [json, meta_, maps, blobs] = this.jsonSerializer.serialize(data);
268
- const meta = meta_.length === 0 ? void 0 : meta_;
269
- if (!enableFormData || blobs.length === 0) {
270
- return {
271
- json,
272
- meta
273
- };
274
- }
275
- const form = new FormData();
276
- form.set("data", stringifyJSON({ json, meta, maps }));
277
- blobs.forEach((blob, i) => {
278
- form.set(i.toString(), blob);
279
- });
280
- return form;
281
- }
282
- deserialize(data) {
283
- if (isAsyncIteratorObject(data)) {
284
- return mapEventIterator(data, {
285
- value: async (value) => this.#deserialize(value),
286
- error: async (e) => {
287
- if (!(e instanceof ErrorEvent)) {
288
- return e;
289
- }
290
- const deserialized = this.#deserialize(e.data);
291
- if (ORPCError.isValidJSON(deserialized)) {
292
- return ORPCError.fromJSON(deserialized, { cause: e });
293
- }
294
- return new ErrorEvent({
295
- data: deserialized,
296
- cause: e
297
- });
298
- }
299
- });
300
- }
301
- return this.#deserialize(data);
302
- }
303
- #deserialize(data) {
304
- if (data === void 0 || data instanceof Blob) {
305
- return data;
306
- }
307
- if (!(data instanceof FormData)) {
308
- return this.jsonSerializer.deserialize(data.json, data.meta ?? []);
309
- }
310
- const serialized = JSON.parse(data.get("data"));
311
- return this.jsonSerializer.deserialize(
312
- serialized.json,
313
- serialized.meta ?? [],
314
- serialized.maps,
315
- (i) => data.get(i.toString())
316
- );
317
- }
318
- }
319
-
320
- export { InvalidEventIteratorRetryResponse as I, RPCJsonSerializer as R, StandardLink as S, StandardRPCLinkCodec as a, RPCSerializer as b };