@orpc/client 0.0.0-next.b2e67f7 → 0.0.0-next.b38be35

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 (52) hide show
  1. package/README.md +31 -21
  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 +45 -0
  5. package/dist/adapters/message-port/index.d.mts +59 -0
  6. package/dist/adapters/message-port/index.d.ts +59 -0
  7. package/dist/adapters/message-port/index.mjs +71 -0
  8. package/dist/adapters/standard/index.d.mts +10 -0
  9. package/dist/adapters/standard/index.d.ts +10 -0
  10. package/dist/adapters/standard/index.mjs +4 -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 +45 -0
  14. package/dist/index.d.mts +185 -0
  15. package/dist/index.d.ts +185 -0
  16. package/dist/index.mjs +82 -0
  17. package/dist/plugins/index.d.mts +202 -0
  18. package/dist/plugins/index.d.ts +202 -0
  19. package/dist/plugins/index.mjs +400 -0
  20. package/dist/shared/client.BG98rYdO.d.ts +45 -0
  21. package/dist/shared/client.BOYsZIRq.d.mts +29 -0
  22. package/dist/shared/client.BOYsZIRq.d.ts +29 -0
  23. package/dist/shared/client.Bwgm6dgk.d.mts +45 -0
  24. package/dist/shared/client.C176log5.d.ts +91 -0
  25. package/dist/shared/client.DKmRtVO2.mjs +390 -0
  26. package/dist/shared/client.Ycwr4Tuo.d.mts +91 -0
  27. package/dist/shared/client.txdq_i5V.mjs +180 -0
  28. package/package.json +30 -24
  29. package/dist/chunk-4HZVK3GJ.js +0 -212
  30. package/dist/chunk-X34KXUAJ.js +0 -281
  31. package/dist/fetch.js +0 -128
  32. package/dist/index.js +0 -90
  33. package/dist/openapi.js +0 -231
  34. package/dist/rpc.js +0 -10
  35. package/dist/src/adapters/fetch/index.d.ts +0 -3
  36. package/dist/src/adapters/fetch/rpc-link.d.ts +0 -98
  37. package/dist/src/adapters/fetch/types.d.ts +0 -5
  38. package/dist/src/client.d.ts +0 -9
  39. package/dist/src/dynamic-link.d.ts +0 -12
  40. package/dist/src/error.d.ts +0 -106
  41. package/dist/src/event-iterator-state.d.ts +0 -9
  42. package/dist/src/event-iterator.d.ts +0 -12
  43. package/dist/src/index.d.ts +0 -9
  44. package/dist/src/openapi/bracket-notation.d.ts +0 -9
  45. package/dist/src/openapi/index.d.ts +0 -4
  46. package/dist/src/openapi/json-serializer.d.ts +0 -7
  47. package/dist/src/openapi/serializer.d.ts +0 -11
  48. package/dist/src/rpc/index.d.ts +0 -3
  49. package/dist/src/rpc/json-serializer.d.ts +0 -12
  50. package/dist/src/rpc/serializer.d.ts +0 -9
  51. package/dist/src/types.d.ts +0 -29
  52. package/dist/src/utils.d.ts +0 -17
@@ -0,0 +1,180 @@
1
+ import { resolveMaybeOptionalOptions, isObject, AsyncIteratorClass, isTypescriptObject } from '@orpc/shared';
2
+ import { getEventMeta, withEventMeta } from '@orpc/standard-server';
3
+
4
+ const COMMON_ORPC_ERROR_DEFS = {
5
+ BAD_REQUEST: {
6
+ status: 400,
7
+ message: "Bad Request"
8
+ },
9
+ UNAUTHORIZED: {
10
+ status: 401,
11
+ message: "Unauthorized"
12
+ },
13
+ FORBIDDEN: {
14
+ status: 403,
15
+ message: "Forbidden"
16
+ },
17
+ NOT_FOUND: {
18
+ status: 404,
19
+ message: "Not Found"
20
+ },
21
+ METHOD_NOT_SUPPORTED: {
22
+ status: 405,
23
+ message: "Method Not Supported"
24
+ },
25
+ NOT_ACCEPTABLE: {
26
+ status: 406,
27
+ message: "Not Acceptable"
28
+ },
29
+ TIMEOUT: {
30
+ status: 408,
31
+ message: "Request Timeout"
32
+ },
33
+ CONFLICT: {
34
+ status: 409,
35
+ message: "Conflict"
36
+ },
37
+ PRECONDITION_FAILED: {
38
+ status: 412,
39
+ message: "Precondition Failed"
40
+ },
41
+ PAYLOAD_TOO_LARGE: {
42
+ status: 413,
43
+ message: "Payload Too Large"
44
+ },
45
+ UNSUPPORTED_MEDIA_TYPE: {
46
+ status: 415,
47
+ message: "Unsupported Media Type"
48
+ },
49
+ UNPROCESSABLE_CONTENT: {
50
+ status: 422,
51
+ message: "Unprocessable Content"
52
+ },
53
+ TOO_MANY_REQUESTS: {
54
+ status: 429,
55
+ message: "Too Many Requests"
56
+ },
57
+ CLIENT_CLOSED_REQUEST: {
58
+ status: 499,
59
+ message: "Client Closed Request"
60
+ },
61
+ INTERNAL_SERVER_ERROR: {
62
+ status: 500,
63
+ message: "Internal Server Error"
64
+ },
65
+ NOT_IMPLEMENTED: {
66
+ status: 501,
67
+ message: "Not Implemented"
68
+ },
69
+ BAD_GATEWAY: {
70
+ status: 502,
71
+ message: "Bad Gateway"
72
+ },
73
+ SERVICE_UNAVAILABLE: {
74
+ status: 503,
75
+ message: "Service Unavailable"
76
+ },
77
+ GATEWAY_TIMEOUT: {
78
+ status: 504,
79
+ message: "Gateway Timeout"
80
+ }
81
+ };
82
+ function fallbackORPCErrorStatus(code, status) {
83
+ return status ?? COMMON_ORPC_ERROR_DEFS[code]?.status ?? 500;
84
+ }
85
+ function fallbackORPCErrorMessage(code, message) {
86
+ return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
87
+ }
88
+ class ORPCError extends Error {
89
+ defined;
90
+ code;
91
+ status;
92
+ data;
93
+ constructor(code, ...rest) {
94
+ const options = resolveMaybeOptionalOptions(rest);
95
+ if (options.status !== void 0 && !isORPCErrorStatus(options.status)) {
96
+ throw new Error("[ORPCError] Invalid error status code.");
97
+ }
98
+ const message = fallbackORPCErrorMessage(code, options.message);
99
+ super(message, options);
100
+ this.code = code;
101
+ this.status = fallbackORPCErrorStatus(code, options.status);
102
+ this.defined = options.defined ?? false;
103
+ this.data = options.data;
104
+ }
105
+ toJSON() {
106
+ return {
107
+ defined: this.defined,
108
+ code: this.code,
109
+ status: this.status,
110
+ message: this.message,
111
+ data: this.data
112
+ };
113
+ }
114
+ }
115
+ function isDefinedError(error) {
116
+ return error instanceof ORPCError && error.defined;
117
+ }
118
+ function toORPCError(error) {
119
+ return error instanceof ORPCError ? error : new ORPCError("INTERNAL_SERVER_ERROR", {
120
+ message: "Internal server error",
121
+ cause: error
122
+ });
123
+ }
124
+ function isORPCErrorStatus(status) {
125
+ return status < 200 || status >= 400;
126
+ }
127
+ function isORPCErrorJson(json) {
128
+ if (!isObject(json)) {
129
+ return false;
130
+ }
131
+ const validKeys = ["defined", "code", "status", "message", "data"];
132
+ if (Object.keys(json).some((k) => !validKeys.includes(k))) {
133
+ return false;
134
+ }
135
+ return "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "status" in json && typeof json.status === "number" && isORPCErrorStatus(json.status) && "message" in json && typeof json.message === "string";
136
+ }
137
+ function createORPCErrorFromJson(json, options = {}) {
138
+ return new ORPCError(json.code, {
139
+ ...options,
140
+ ...json
141
+ });
142
+ }
143
+
144
+ function mapEventIterator(iterator, maps) {
145
+ const mapError = async (error) => {
146
+ let mappedError = await maps.error(error);
147
+ if (mappedError !== error) {
148
+ const meta = getEventMeta(error);
149
+ if (meta && isTypescriptObject(mappedError)) {
150
+ mappedError = withEventMeta(mappedError, meta);
151
+ }
152
+ }
153
+ return mappedError;
154
+ };
155
+ return new AsyncIteratorClass(async () => {
156
+ const { done, value } = await (async () => {
157
+ try {
158
+ return await iterator.next();
159
+ } catch (error) {
160
+ throw await mapError(error);
161
+ }
162
+ })();
163
+ let mappedValue = await maps.value(value, done);
164
+ if (mappedValue !== value) {
165
+ const meta = getEventMeta(value);
166
+ if (meta && isTypescriptObject(mappedValue)) {
167
+ mappedValue = withEventMeta(mappedValue, meta);
168
+ }
169
+ }
170
+ return { done, value: mappedValue };
171
+ }, async () => {
172
+ try {
173
+ await iterator.return?.();
174
+ } catch (error) {
175
+ throw await mapError(error);
176
+ }
177
+ });
178
+ }
179
+
180
+ export { COMMON_ORPC_ERROR_DEFS as C, ORPCError as O, fallbackORPCErrorMessage as a, isORPCErrorStatus as b, isORPCErrorJson as c, createORPCErrorFromJson as d, fallbackORPCErrorStatus as f, isDefinedError as i, mapEventIterator as m, toORPCError as t };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/client",
3
3
  "type": "module",
4
- "version": "0.0.0-next.b2e67f7",
4
+ "version": "0.0.0-next.b38be35",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -15,44 +15,50 @@
15
15
  ],
16
16
  "exports": {
17
17
  ".": {
18
- "types": "./dist/src/index.d.ts",
19
- "import": "./dist/index.js",
20
- "default": "./dist/index.js"
18
+ "types": "./dist/index.d.mts",
19
+ "import": "./dist/index.mjs",
20
+ "default": "./dist/index.mjs"
21
21
  },
22
- "./openapi": {
23
- "types": "./dist/src/openapi/index.d.ts",
24
- "import": "./dist/openapi.js",
25
- "default": "./dist/openapi.js"
22
+ "./plugins": {
23
+ "types": "./dist/plugins/index.d.mts",
24
+ "import": "./dist/plugins/index.mjs",
25
+ "default": "./dist/plugins/index.mjs"
26
26
  },
27
- "./rpc": {
28
- "types": "./dist/src/rpc/index.d.ts",
29
- "import": "./dist/rpc.js",
30
- "default": "./dist/rpc.js"
27
+ "./standard": {
28
+ "types": "./dist/adapters/standard/index.d.mts",
29
+ "import": "./dist/adapters/standard/index.mjs",
30
+ "default": "./dist/adapters/standard/index.mjs"
31
31
  },
32
32
  "./fetch": {
33
- "types": "./dist/src/adapters/fetch/index.d.ts",
34
- "import": "./dist/fetch.js",
35
- "default": "./dist/fetch.js"
33
+ "types": "./dist/adapters/fetch/index.d.mts",
34
+ "import": "./dist/adapters/fetch/index.mjs",
35
+ "default": "./dist/adapters/fetch/index.mjs"
36
36
  },
37
- "./🔒/*": {
38
- "types": "./dist/src/*.d.ts"
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"
39
46
  }
40
47
  },
41
48
  "files": [
42
- "!**/*.map",
43
- "!**/*.tsbuildinfo",
44
49
  "dist"
45
50
  ],
46
51
  "dependencies": {
47
- "@orpc/standard-server-fetch": "0.0.0-next.b2e67f7",
48
- "@orpc/shared": "0.0.0-next.b2e67f7",
49
- "@orpc/standard-server": "0.0.0-next.b2e67f7"
52
+ "@orpc/shared": "0.0.0-next.b38be35",
53
+ "@orpc/standard-server": "0.0.0-next.b38be35",
54
+ "@orpc/standard-server-peer": "0.0.0-next.b38be35",
55
+ "@orpc/standard-server-fetch": "0.0.0-next.b38be35"
50
56
  },
51
57
  "devDependencies": {
52
- "zod": "^3.24.1"
58
+ "zod": "^4.0.17"
53
59
  },
54
60
  "scripts": {
55
- "build": "tsup --onSuccess='tsc -b --noCheck'",
61
+ "build": "unbuild",
56
62
  "build:watch": "pnpm run build --watch",
57
63
  "type:check": "tsc -b"
58
64
  }
@@ -1,212 +0,0 @@
1
- import {
2
- ORPCError,
3
- mapEventIterator,
4
- toORPCError
5
- } from "./chunk-X34KXUAJ.js";
6
-
7
- // src/rpc/json-serializer.ts
8
- import { isObject } from "@orpc/shared";
9
- var RPCJsonSerializer = class {
10
- serialize(data, segments = [], meta = [], maps = [], blobs = []) {
11
- if (data instanceof Blob) {
12
- maps.push(segments);
13
- blobs.push(data);
14
- return [data, meta, maps, blobs];
15
- }
16
- if (typeof data === "bigint") {
17
- meta.push([0, segments]);
18
- return [data.toString(), meta, maps, blobs];
19
- }
20
- if (data instanceof Date) {
21
- meta.push([1, segments]);
22
- if (Number.isNaN(data.getTime())) {
23
- return [null, meta, maps, blobs];
24
- }
25
- return [data.toISOString(), meta, maps, blobs];
26
- }
27
- if (Number.isNaN(data)) {
28
- meta.push([2, segments]);
29
- return [null, meta, maps, blobs];
30
- }
31
- if (data instanceof URL) {
32
- meta.push([4, segments]);
33
- return [data.toString(), meta, maps, blobs];
34
- }
35
- if (data instanceof RegExp) {
36
- meta.push([5, segments]);
37
- return [data.toString(), meta, maps, blobs];
38
- }
39
- if (data instanceof Set) {
40
- const result = this.serialize(Array.from(data), segments, meta, maps, blobs);
41
- meta.push([6, segments]);
42
- return result;
43
- }
44
- if (data instanceof Map) {
45
- const result = this.serialize(Array.from(data.entries()), segments, meta, maps, blobs);
46
- meta.push([7, segments]);
47
- return result;
48
- }
49
- if (Array.isArray(data)) {
50
- const json = data.map((v, i) => {
51
- if (v === void 0) {
52
- meta.push([3, [...segments, i]]);
53
- return v;
54
- }
55
- return this.serialize(v, [...segments, i], meta, maps, blobs)[0];
56
- });
57
- return [json, meta, maps, blobs];
58
- }
59
- if (isObject(data)) {
60
- const json = {};
61
- for (const k in data) {
62
- json[k] = this.serialize(data[k], [...segments, k], meta, maps, blobs)[0];
63
- }
64
- return [json, meta, maps, blobs];
65
- }
66
- return [data, meta, maps, blobs];
67
- }
68
- deserialize(json, meta, maps, getBlob) {
69
- const ref = { data: json };
70
- if (maps && getBlob) {
71
- maps.forEach((segments, i) => {
72
- let currentRef = ref;
73
- let preSegment = "data";
74
- segments.forEach((segment) => {
75
- currentRef = currentRef[preSegment];
76
- preSegment = segment;
77
- });
78
- currentRef[preSegment] = getBlob(i);
79
- });
80
- }
81
- for (const [type, segments] of meta) {
82
- let currentRef = ref;
83
- let preSegment = "data";
84
- segments.forEach((segment) => {
85
- currentRef = currentRef[preSegment];
86
- preSegment = segment;
87
- });
88
- switch (type) {
89
- case 0:
90
- currentRef[preSegment] = BigInt(currentRef[preSegment]);
91
- break;
92
- case 1:
93
- currentRef[preSegment] = new Date(currentRef[preSegment] ?? "Invalid Date");
94
- break;
95
- case 2:
96
- currentRef[preSegment] = Number.NaN;
97
- break;
98
- case 3:
99
- currentRef[preSegment] = void 0;
100
- break;
101
- case 4:
102
- currentRef[preSegment] = new URL(currentRef[preSegment]);
103
- break;
104
- case 5: {
105
- const [, pattern, flags] = currentRef[preSegment].match(/^\/(.*)\/([a-z]*)$/);
106
- currentRef[preSegment] = new RegExp(pattern, flags);
107
- break;
108
- }
109
- case 6:
110
- currentRef[preSegment] = new Set(currentRef[preSegment]);
111
- break;
112
- case 7:
113
- currentRef[preSegment] = new Map(currentRef[preSegment]);
114
- break;
115
- /* v8 ignore next 3 */
116
- default: {
117
- const _expected = type;
118
- }
119
- }
120
- }
121
- return ref.data;
122
- }
123
- };
124
-
125
- // src/rpc/serializer.ts
126
- import { ErrorEvent, isAsyncIteratorObject } from "@orpc/standard-server";
127
- var RPCSerializer = class {
128
- constructor(jsonSerializer = new RPCJsonSerializer()) {
129
- this.jsonSerializer = jsonSerializer;
130
- }
131
- serialize(data) {
132
- if (isAsyncIteratorObject(data)) {
133
- return mapEventIterator(data, {
134
- value: async (value) => this.#serialize(value, false),
135
- error: async (e) => {
136
- if (e instanceof ErrorEvent) {
137
- return new ErrorEvent({
138
- data: this.#serialize(e.data, false),
139
- cause: e
140
- });
141
- }
142
- return new ErrorEvent({
143
- data: this.#serialize(toORPCError(e).toJSON(), false),
144
- cause: e
145
- });
146
- }
147
- });
148
- }
149
- return this.#serialize(data, true);
150
- }
151
- #serialize(data, enableFormData) {
152
- if (data === void 0 || data instanceof Blob) {
153
- return data;
154
- }
155
- const [json, meta_, maps, blobs] = this.jsonSerializer.serialize(data);
156
- const meta = meta_.length === 0 ? void 0 : meta_;
157
- if (!enableFormData || blobs.length === 0) {
158
- return {
159
- json,
160
- meta
161
- };
162
- }
163
- const form = new FormData();
164
- form.set("data", JSON.stringify({ json, meta, maps }));
165
- blobs.forEach((blob, i) => {
166
- form.set(i.toString(), blob);
167
- });
168
- return form;
169
- }
170
- deserialize(data) {
171
- if (isAsyncIteratorObject(data)) {
172
- return mapEventIterator(data, {
173
- value: async (value) => this.#deserialize(value),
174
- error: async (e) => {
175
- if (!(e instanceof ErrorEvent)) {
176
- return e;
177
- }
178
- const deserialized = this.#deserialize(e.data);
179
- if (ORPCError.isValidJSON(deserialized)) {
180
- return ORPCError.fromJSON(deserialized, { cause: e });
181
- }
182
- return new ErrorEvent({
183
- data: deserialized,
184
- cause: e
185
- });
186
- }
187
- });
188
- }
189
- return this.#deserialize(data);
190
- }
191
- #deserialize(data) {
192
- if (data === void 0 || data instanceof Blob) {
193
- return data;
194
- }
195
- if (!(data instanceof FormData)) {
196
- return this.jsonSerializer.deserialize(data.json, data.meta ?? []);
197
- }
198
- const serialized = JSON.parse(data.get("data"));
199
- return this.jsonSerializer.deserialize(
200
- serialized.json,
201
- serialized.meta ?? [],
202
- serialized.maps,
203
- (i) => data.get(i.toString())
204
- );
205
- }
206
- };
207
-
208
- export {
209
- RPCJsonSerializer,
210
- RPCSerializer
211
- };
212
- //# sourceMappingURL=chunk-4HZVK3GJ.js.map