@orpc/client 0.0.0-next.fea68c1 → 0.0.0-next.ff2168f

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 (53) hide show
  1. package/README.md +41 -28
  2. package/dist/adapters/fetch/index.d.mts +46 -0
  3. package/dist/adapters/fetch/index.d.ts +46 -0
  4. package/dist/adapters/fetch/index.mjs +46 -0
  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 +11 -0
  9. package/dist/adapters/standard/index.d.ts +11 -0
  10. package/dist/adapters/standard/index.mjs +6 -0
  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 +230 -0
  15. package/dist/index.d.ts +230 -0
  16. package/dist/index.mjs +112 -0
  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.BhBMHCI4.mjs +398 -0
  25. package/dist/shared/client.C6q5291t.mjs +171 -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 +32 -27
  30. package/dist/chunk-7F3XVLRJ.js +0 -281
  31. package/dist/chunk-FF5VXXNP.js +0 -213
  32. package/dist/fetch.js +0 -127
  33. package/dist/index.js +0 -90
  34. package/dist/openapi.js +0 -232
  35. package/dist/rpc.js +0 -10
  36. package/dist/src/adapters/fetch/index.d.ts +0 -3
  37. package/dist/src/adapters/fetch/rpc-link.d.ts +0 -98
  38. package/dist/src/adapters/fetch/types.d.ts +0 -5
  39. package/dist/src/client.d.ts +0 -9
  40. package/dist/src/dynamic-link.d.ts +0 -12
  41. package/dist/src/error.d.ts +0 -106
  42. package/dist/src/event-iterator-state.d.ts +0 -9
  43. package/dist/src/event-iterator.d.ts +0 -12
  44. package/dist/src/index.d.ts +0 -9
  45. package/dist/src/openapi/bracket-notation.d.ts +0 -9
  46. package/dist/src/openapi/index.d.ts +0 -4
  47. package/dist/src/openapi/json-serializer.d.ts +0 -7
  48. package/dist/src/openapi/serializer.d.ts +0 -11
  49. package/dist/src/rpc/index.d.ts +0 -3
  50. package/dist/src/rpc/json-serializer.d.ts +0 -12
  51. package/dist/src/rpc/serializer.d.ts +0 -9
  52. package/dist/src/types.d.ts +0 -29
  53. package/dist/src/utils.d.ts +0 -17
@@ -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,58 +1,63 @@
1
1
  {
2
2
  "name": "@orpc/client",
3
3
  "type": "module",
4
- "version": "0.0.0-next.fea68c1",
4
+ "version": "0.0.0-next.ff2168f",
5
5
  "license": "MIT",
6
- "homepage": "https://orpc.unnoq.com",
6
+ "homepage": "https://orpc.dev",
7
7
  "repository": {
8
8
  "type": "git",
9
- "url": "git+https://github.com/unnoq/orpc.git",
9
+ "url": "git+https://github.com/middleapi/orpc.git",
10
10
  "directory": "packages/client"
11
11
  },
12
12
  "keywords": [
13
- "unnoq",
14
13
  "orpc"
15
14
  ],
16
15
  "exports": {
17
16
  ".": {
18
- "types": "./dist/src/index.d.ts",
19
- "import": "./dist/index.js",
20
- "default": "./dist/index.js"
17
+ "types": "./dist/index.d.mts",
18
+ "import": "./dist/index.mjs",
19
+ "default": "./dist/index.mjs"
21
20
  },
22
- "./openapi": {
23
- "types": "./dist/src/openapi/index.d.ts",
24
- "import": "./dist/openapi.js",
25
- "default": "./dist/openapi.js"
21
+ "./plugins": {
22
+ "types": "./dist/plugins/index.d.mts",
23
+ "import": "./dist/plugins/index.mjs",
24
+ "default": "./dist/plugins/index.mjs"
26
25
  },
27
- "./rpc": {
28
- "types": "./dist/src/rpc/index.d.ts",
29
- "import": "./dist/rpc.js",
30
- "default": "./dist/rpc.js"
26
+ "./standard": {
27
+ "types": "./dist/adapters/standard/index.d.mts",
28
+ "import": "./dist/adapters/standard/index.mjs",
29
+ "default": "./dist/adapters/standard/index.mjs"
31
30
  },
32
31
  "./fetch": {
33
- "types": "./dist/src/adapters/fetch/index.d.ts",
34
- "import": "./dist/fetch.js",
35
- "default": "./dist/fetch.js"
32
+ "types": "./dist/adapters/fetch/index.d.mts",
33
+ "import": "./dist/adapters/fetch/index.mjs",
34
+ "default": "./dist/adapters/fetch/index.mjs"
36
35
  },
37
- "./🔒/*": {
38
- "types": "./dist/src/*.d.ts"
36
+ "./websocket": {
37
+ "types": "./dist/adapters/websocket/index.d.mts",
38
+ "import": "./dist/adapters/websocket/index.mjs",
39
+ "default": "./dist/adapters/websocket/index.mjs"
40
+ },
41
+ "./message-port": {
42
+ "types": "./dist/adapters/message-port/index.d.mts",
43
+ "import": "./dist/adapters/message-port/index.mjs",
44
+ "default": "./dist/adapters/message-port/index.mjs"
39
45
  }
40
46
  },
41
47
  "files": [
42
- "!**/*.map",
43
- "!**/*.tsbuildinfo",
44
48
  "dist"
45
49
  ],
46
50
  "dependencies": {
47
- "@orpc/shared": "0.0.0-next.fea68c1",
48
- "@orpc/standard-server": "0.0.0-next.fea68c1",
49
- "@orpc/standard-server-fetch": "0.0.0-next.fea68c1"
51
+ "@orpc/standard-server": "0.0.0-next.ff2168f",
52
+ "@orpc/standard-server-fetch": "0.0.0-next.ff2168f",
53
+ "@orpc/shared": "0.0.0-next.ff2168f",
54
+ "@orpc/standard-server-peer": "0.0.0-next.ff2168f"
50
55
  },
51
56
  "devDependencies": {
52
- "zod": "^3.24.1"
57
+ "zod": "^4.3.2"
53
58
  },
54
59
  "scripts": {
55
- "build": "tsup --onSuccess='tsc -b --noCheck'",
60
+ "build": "unbuild",
56
61
  "build:watch": "pnpm run build --watch",
57
62
  "type:check": "tsc -b"
58
63
  }
@@ -1,281 +0,0 @@
1
- // src/error.ts
2
- import { isObject } from "@orpc/shared";
3
- var COMMON_ORPC_ERROR_DEFS = {
4
- BAD_REQUEST: {
5
- status: 400,
6
- message: "Bad Request"
7
- },
8
- UNAUTHORIZED: {
9
- status: 401,
10
- message: "Unauthorized"
11
- },
12
- FORBIDDEN: {
13
- status: 403,
14
- message: "Forbidden"
15
- },
16
- NOT_FOUND: {
17
- status: 404,
18
- message: "Not Found"
19
- },
20
- METHOD_NOT_SUPPORTED: {
21
- status: 405,
22
- message: "Method Not Supported"
23
- },
24
- NOT_ACCEPTABLE: {
25
- status: 406,
26
- message: "Not Acceptable"
27
- },
28
- TIMEOUT: {
29
- status: 408,
30
- message: "Request Timeout"
31
- },
32
- CONFLICT: {
33
- status: 409,
34
- message: "Conflict"
35
- },
36
- PRECONDITION_FAILED: {
37
- status: 412,
38
- message: "Precondition Failed"
39
- },
40
- PAYLOAD_TOO_LARGE: {
41
- status: 413,
42
- message: "Payload Too Large"
43
- },
44
- UNSUPPORTED_MEDIA_TYPE: {
45
- status: 415,
46
- message: "Unsupported Media Type"
47
- },
48
- UNPROCESSABLE_CONTENT: {
49
- status: 422,
50
- message: "Unprocessable Content"
51
- },
52
- TOO_MANY_REQUESTS: {
53
- status: 429,
54
- message: "Too Many Requests"
55
- },
56
- CLIENT_CLOSED_REQUEST: {
57
- status: 499,
58
- message: "Client Closed Request"
59
- },
60
- INTERNAL_SERVER_ERROR: {
61
- status: 500,
62
- message: "Internal Server Error"
63
- },
64
- NOT_IMPLEMENTED: {
65
- status: 501,
66
- message: "Not Implemented"
67
- },
68
- BAD_GATEWAY: {
69
- status: 502,
70
- message: "Bad Gateway"
71
- },
72
- SERVICE_UNAVAILABLE: {
73
- status: 503,
74
- message: "Service Unavailable"
75
- },
76
- GATEWAY_TIMEOUT: {
77
- status: 504,
78
- message: "Gateway Timeout"
79
- }
80
- };
81
- function fallbackORPCErrorStatus(code, status) {
82
- return status ?? COMMON_ORPC_ERROR_DEFS[code]?.status ?? 500;
83
- }
84
- function fallbackORPCErrorMessage(code, message) {
85
- return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
86
- }
87
- var ORPCError = class _ORPCError extends Error {
88
- defined;
89
- code;
90
- status;
91
- data;
92
- constructor(code, ...[options]) {
93
- if (options?.status && (options.status < 400 || options.status >= 600)) {
94
- throw new Error("[ORPCError] The error status code must be in the 400-599 range.");
95
- }
96
- const message = fallbackORPCErrorMessage(code, options?.message);
97
- super(message, options);
98
- this.code = code;
99
- this.status = fallbackORPCErrorStatus(code, options?.status);
100
- this.defined = options?.defined ?? false;
101
- this.data = options?.data;
102
- }
103
- toJSON() {
104
- return {
105
- defined: this.defined,
106
- code: this.code,
107
- status: this.status,
108
- message: this.message,
109
- data: this.data
110
- };
111
- }
112
- static fromJSON(json, options) {
113
- return new _ORPCError(json.code, {
114
- ...options,
115
- ...json
116
- });
117
- }
118
- static isValidJSON(json) {
119
- if (!isObject(json)) {
120
- return false;
121
- }
122
- const validKeys = ["defined", "code", "status", "message", "data"];
123
- if (Object.keys(json).some((k) => !validKeys.includes(k))) {
124
- return false;
125
- }
126
- return "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "status" in json && typeof json.status === "number" && "message" in json && typeof json.message === "string";
127
- }
128
- };
129
- function isDefinedError(error) {
130
- return error instanceof ORPCError && error.defined;
131
- }
132
- function toORPCError(error) {
133
- return error instanceof ORPCError ? error : new ORPCError("INTERNAL_SERVER_ERROR", {
134
- message: "Internal server error",
135
- cause: error
136
- });
137
- }
138
-
139
- // src/event-iterator-state.ts
140
- var iteratorStates = /* @__PURE__ */ new WeakMap();
141
- function registerEventIteratorState(iterator, state) {
142
- iteratorStates.set(iterator, state);
143
- }
144
- function updateEventIteratorStatus(state, status) {
145
- if (state.status !== status) {
146
- state.status = status;
147
- state.listeners.forEach((cb) => cb(status));
148
- }
149
- }
150
- function onEventIteratorStatusChange(iterator, callback, notifyImmediately = true) {
151
- const state = iteratorStates.get(iterator);
152
- if (!state) {
153
- throw new Error("Iterator is not registered.");
154
- }
155
- if (notifyImmediately) {
156
- callback(state.status);
157
- }
158
- state.listeners.push(callback);
159
- return () => {
160
- const index = state.listeners.indexOf(callback);
161
- if (index !== -1) {
162
- state.listeners.splice(index, 1);
163
- }
164
- };
165
- }
166
-
167
- // src/event-iterator.ts
168
- import { isTypescriptObject, retry } from "@orpc/shared";
169
- import { getEventMeta, withEventMeta } from "@orpc/standard-server";
170
- function mapEventIterator(iterator, maps) {
171
- return async function* () {
172
- try {
173
- while (true) {
174
- const { done, value } = await iterator.next();
175
- let mappedValue = await maps.value(value, done);
176
- if (mappedValue !== value) {
177
- const meta = getEventMeta(value);
178
- if (meta && isTypescriptObject(mappedValue)) {
179
- mappedValue = withEventMeta(mappedValue, meta);
180
- }
181
- }
182
- if (done) {
183
- return mappedValue;
184
- }
185
- yield mappedValue;
186
- }
187
- } catch (error) {
188
- let mappedError = await maps.error(error);
189
- if (mappedError !== error) {
190
- const meta = getEventMeta(error);
191
- if (meta && isTypescriptObject(mappedError)) {
192
- mappedError = withEventMeta(mappedError, meta);
193
- }
194
- }
195
- throw mappedError;
196
- } finally {
197
- await iterator.return?.();
198
- }
199
- }();
200
- }
201
- var MAX_ALLOWED_RETRY_TIMES = 99;
202
- function createAutoRetryEventIterator(initial, reconnect, initialLastEventId) {
203
- const state = {
204
- status: "connected",
205
- listeners: []
206
- };
207
- const iterator = async function* () {
208
- let current = initial;
209
- let lastEventId = initialLastEventId;
210
- let lastRetry;
211
- let retryTimes = 0;
212
- try {
213
- while (true) {
214
- try {
215
- updateEventIteratorStatus(state, "connected");
216
- const { done, value } = await current.next();
217
- const meta = getEventMeta(value);
218
- lastEventId = meta?.id ?? lastEventId;
219
- lastRetry = meta?.retry ?? lastRetry;
220
- retryTimes = 0;
221
- if (done) {
222
- return value;
223
- }
224
- yield value;
225
- } catch (e) {
226
- updateEventIteratorStatus(state, "reconnecting");
227
- const meta = getEventMeta(e);
228
- lastEventId = meta?.id ?? lastEventId;
229
- lastRetry = meta?.retry ?? lastRetry;
230
- let currentError = e;
231
- current = await retry({ times: MAX_ALLOWED_RETRY_TIMES }, async (exit) => {
232
- retryTimes += 1;
233
- if (retryTimes > MAX_ALLOWED_RETRY_TIMES) {
234
- throw exit(new Error(
235
- `Exceeded maximum retry attempts (${MAX_ALLOWED_RETRY_TIMES}) for event source. Possible infinite retry loop detected. Please review the retry logic.`,
236
- { cause: currentError }
237
- ));
238
- }
239
- const reconnected = await (async () => {
240
- try {
241
- return await reconnect({
242
- lastRetry,
243
- lastEventId,
244
- retryTimes,
245
- error: currentError
246
- });
247
- } catch (e2) {
248
- currentError = e2;
249
- throw e2;
250
- }
251
- })();
252
- if (!reconnected) {
253
- throw exit(currentError);
254
- }
255
- return reconnected;
256
- });
257
- }
258
- }
259
- } finally {
260
- updateEventIteratorStatus(state, "closed");
261
- await current.return?.();
262
- }
263
- }();
264
- registerEventIteratorState(iterator, state);
265
- return iterator;
266
- }
267
-
268
- export {
269
- COMMON_ORPC_ERROR_DEFS,
270
- fallbackORPCErrorStatus,
271
- fallbackORPCErrorMessage,
272
- ORPCError,
273
- isDefinedError,
274
- toORPCError,
275
- registerEventIteratorState,
276
- updateEventIteratorStatus,
277
- onEventIteratorStatusChange,
278
- mapEventIterator,
279
- createAutoRetryEventIterator
280
- };
281
- //# sourceMappingURL=chunk-7F3XVLRJ.js.map