@orpc/client 0.0.0-next.ff5907c → 0.0.0-next.ff68fdb

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 (36) hide show
  1. package/README.md +90 -0
  2. package/dist/adapters/fetch/index.d.mts +29 -0
  3. package/dist/adapters/fetch/index.d.ts +29 -0
  4. package/dist/adapters/fetch/index.mjs +35 -0
  5. package/dist/adapters/standard/index.d.mts +148 -0
  6. package/dist/adapters/standard/index.d.ts +148 -0
  7. package/dist/adapters/standard/index.mjs +4 -0
  8. package/dist/index.d.mts +155 -0
  9. package/dist/index.d.ts +155 -0
  10. package/dist/{index.js → index.mjs} +20 -38
  11. package/dist/shared/client.D_CzLDyB.d.mts +42 -0
  12. package/dist/shared/client.D_CzLDyB.d.ts +42 -0
  13. package/dist/shared/client.Df5pd75N.mjs +320 -0
  14. package/dist/{chunk-2UPNYYFF.js → shared/client.XAn8cDTM.mjs} +14 -36
  15. package/package.json +16 -26
  16. package/dist/chunk-TPEMQB7D.js +0 -178
  17. package/dist/fetch.js +0 -128
  18. package/dist/openapi.js +0 -329
  19. package/dist/rpc.js +0 -10
  20. package/dist/src/adapters/fetch/index.d.ts +0 -3
  21. package/dist/src/adapters/fetch/rpc-link.d.ts +0 -98
  22. package/dist/src/adapters/fetch/types.d.ts +0 -5
  23. package/dist/src/client.d.ts +0 -9
  24. package/dist/src/dynamic-link.d.ts +0 -12
  25. package/dist/src/error.d.ts +0 -106
  26. package/dist/src/event-iterator-state.d.ts +0 -9
  27. package/dist/src/event-iterator.d.ts +0 -12
  28. package/dist/src/index.d.ts +0 -9
  29. package/dist/src/openapi/bracket-notation.d.ts +0 -84
  30. package/dist/src/openapi/index.d.ts +0 -4
  31. package/dist/src/openapi/json-serializer.d.ts +0 -5
  32. package/dist/src/openapi/serializer.d.ts +0 -11
  33. package/dist/src/rpc/index.d.ts +0 -2
  34. package/dist/src/rpc/serializer.d.ts +0 -22
  35. package/dist/src/types.d.ts +0 -29
  36. package/dist/src/utils.d.ts +0 -5
@@ -1,12 +1,7 @@
1
- var __defProp = Object.defineProperty;
2
- var __export = (target, all) => {
3
- for (var name in all)
4
- __defProp(target, name, { get: all[name], enumerable: true });
5
- };
1
+ import { isObject, isTypescriptObject, retry } from '@orpc/shared';
2
+ import { getEventMeta, withEventMeta } from '@orpc/standard-server';
6
3
 
7
- // src/error.ts
8
- import { isObject } from "@orpc/shared";
9
- var COMMON_ORPC_ERROR_DEFS = {
4
+ const COMMON_ORPC_ERROR_DEFS = {
10
5
  BAD_REQUEST: {
11
6
  status: 400,
12
7
  message: "Bad Request"
@@ -90,7 +85,7 @@ function fallbackORPCErrorStatus(code, status) {
90
85
  function fallbackORPCErrorMessage(code, message) {
91
86
  return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
92
87
  }
93
- var ORPCError = class _ORPCError extends Error {
88
+ class ORPCError extends Error {
94
89
  defined;
95
90
  code;
96
91
  status;
@@ -116,7 +111,7 @@ var ORPCError = class _ORPCError extends Error {
116
111
  };
117
112
  }
118
113
  static fromJSON(json, options) {
119
- return new _ORPCError(json.code, {
114
+ return new ORPCError(json.code, {
120
115
  ...options,
121
116
  ...json
122
117
  });
@@ -131,7 +126,7 @@ var ORPCError = class _ORPCError extends Error {
131
126
  }
132
127
  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";
133
128
  }
134
- };
129
+ }
135
130
  function isDefinedError(error) {
136
131
  return error instanceof ORPCError && error.defined;
137
132
  }
@@ -142,8 +137,7 @@ function toORPCError(error) {
142
137
  });
143
138
  }
144
139
 
145
- // src/event-iterator-state.ts
146
- var iteratorStates = /* @__PURE__ */ new WeakMap();
140
+ const iteratorStates = /* @__PURE__ */ new WeakMap();
147
141
  function registerEventIteratorState(iterator, state) {
148
142
  iteratorStates.set(iterator, state);
149
143
  }
@@ -153,7 +147,8 @@ function updateEventIteratorStatus(state, status) {
153
147
  state.listeners.forEach((cb) => cb(status));
154
148
  }
155
149
  }
156
- function onEventIteratorStatusChange(iterator, callback, notifyImmediately = true) {
150
+ function onEventIteratorStatusChange(iterator, callback, options = {}) {
151
+ const notifyImmediately = options.notifyImmediately ?? true;
157
152
  const state = iteratorStates.get(iterator);
158
153
  if (!state) {
159
154
  throw new Error("Iterator is not registered.");
@@ -170,9 +165,6 @@ function onEventIteratorStatusChange(iterator, callback, notifyImmediately = tru
170
165
  };
171
166
  }
172
167
 
173
- // src/event-iterator.ts
174
- import { getEventMeta, isEventMetaContainer, withEventMeta } from "@orpc/server-standard";
175
- import { retry } from "@orpc/shared";
176
168
  function mapEventIterator(iterator, maps) {
177
169
  return async function* () {
178
170
  try {
@@ -181,7 +173,7 @@ function mapEventIterator(iterator, maps) {
181
173
  let mappedValue = await maps.value(value, done);
182
174
  if (mappedValue !== value) {
183
175
  const meta = getEventMeta(value);
184
- if (meta && isEventMetaContainer(mappedValue)) {
176
+ if (meta && isTypescriptObject(mappedValue)) {
185
177
  mappedValue = withEventMeta(mappedValue, meta);
186
178
  }
187
179
  }
@@ -194,7 +186,7 @@ function mapEventIterator(iterator, maps) {
194
186
  let mappedError = await maps.error(error);
195
187
  if (mappedError !== error) {
196
188
  const meta = getEventMeta(error);
197
- if (meta && isEventMetaContainer(mappedError)) {
189
+ if (meta && isTypescriptObject(mappedError)) {
198
190
  mappedError = withEventMeta(mappedError, meta);
199
191
  }
200
192
  }
@@ -204,7 +196,7 @@ function mapEventIterator(iterator, maps) {
204
196
  }
205
197
  }();
206
198
  }
207
- var MAX_ALLOWED_RETRY_TIMES = 99;
199
+ const MAX_ALLOWED_RETRY_TIMES = 99;
208
200
  function createAutoRetryEventIterator(initial, reconnect, initialLastEventId) {
209
201
  const state = {
210
202
  status: "connected",
@@ -238,7 +230,7 @@ function createAutoRetryEventIterator(initial, reconnect, initialLastEventId) {
238
230
  retryTimes += 1;
239
231
  if (retryTimes > MAX_ALLOWED_RETRY_TIMES) {
240
232
  throw exit(new Error(
241
- `Exceeded maximum retry attempts (${MAX_ALLOWED_RETRY_TIMES}) for event source. Possible infinite retry loop detected. Please review the retry logic.`,
233
+ `Exceeded maximum retry attempts (${MAX_ALLOWED_RETRY_TIMES}) for event iterator. Possible infinite retry loop detected. Please review the retry logic.`,
242
234
  { cause: currentError }
243
235
  ));
244
236
  }
@@ -271,18 +263,4 @@ function createAutoRetryEventIterator(initial, reconnect, initialLastEventId) {
271
263
  return iterator;
272
264
  }
273
265
 
274
- export {
275
- __export,
276
- COMMON_ORPC_ERROR_DEFS,
277
- fallbackORPCErrorStatus,
278
- fallbackORPCErrorMessage,
279
- ORPCError,
280
- isDefinedError,
281
- toORPCError,
282
- registerEventIteratorState,
283
- updateEventIteratorStatus,
284
- onEventIteratorStatusChange,
285
- mapEventIterator,
286
- createAutoRetryEventIterator
287
- };
288
- //# sourceMappingURL=chunk-2UPNYYFF.js.map
266
+ export { COMMON_ORPC_ERROR_DEFS as C, ORPCError as O, fallbackORPCErrorMessage as a, createAutoRetryEventIterator as c, fallbackORPCErrorStatus as f, isDefinedError as i, mapEventIterator as m, onEventIteratorStatusChange as o, registerEventIteratorState as r, toORPCError as t, updateEventIteratorStatus as u };
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.ff5907c",
4
+ "version": "0.0.0-next.ff68fdb",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -15,44 +15,34 @@
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"
26
- },
27
- "./rpc": {
28
- "types": "./dist/src/rpc/index.d.ts",
29
- "import": "./dist/rpc.js",
30
- "default": "./dist/rpc.js"
22
+ "./standard": {
23
+ "types": "./dist/adapters/standard/index.d.mts",
24
+ "import": "./dist/adapters/standard/index.mjs",
25
+ "default": "./dist/adapters/standard/index.mjs"
31
26
  },
32
27
  "./fetch": {
33
- "types": "./dist/src/adapters/fetch/index.d.ts",
34
- "import": "./dist/fetch.js",
35
- "default": "./dist/fetch.js"
36
- },
37
- "./🔒/*": {
38
- "types": "./dist/src/*.d.ts"
28
+ "types": "./dist/adapters/fetch/index.d.mts",
29
+ "import": "./dist/adapters/fetch/index.mjs",
30
+ "default": "./dist/adapters/fetch/index.mjs"
39
31
  }
40
32
  },
41
33
  "files": [
42
- "!**/*.map",
43
- "!**/*.tsbuildinfo",
44
34
  "dist"
45
35
  ],
46
36
  "dependencies": {
47
- "@orpc/server-standard": "^0.4.0",
48
- "@orpc/server-standard-fetch": "^0.4.0",
49
- "@orpc/shared": "0.0.0-next.ff5907c"
37
+ "@orpc/standard-server": "0.0.0-next.ff68fdb",
38
+ "@orpc/standard-server-fetch": "0.0.0-next.ff68fdb",
39
+ "@orpc/shared": "0.0.0-next.ff68fdb"
50
40
  },
51
41
  "devDependencies": {
52
- "zod": "^3.24.1"
42
+ "zod": "^3.24.2"
53
43
  },
54
44
  "scripts": {
55
- "build": "tsup --onSuccess='tsc -b --noCheck'",
45
+ "build": "unbuild",
56
46
  "build:watch": "pnpm run build --watch",
57
47
  "type:check": "tsc -b"
58
48
  }
@@ -1,178 +0,0 @@
1
- import {
2
- ORPCError,
3
- mapEventIterator,
4
- toORPCError
5
- } from "./chunk-2UPNYYFF.js";
6
-
7
- // src/rpc/serializer.ts
8
- import { ErrorEvent, isAsyncIteratorObject } from "@orpc/server-standard";
9
- import { findDeepMatches, isObject, set } from "@orpc/shared";
10
- var RPCSerializer = class {
11
- serialize(data) {
12
- if (isAsyncIteratorObject(data)) {
13
- return mapEventIterator(data, {
14
- value: async (value) => serializeRPCJson(value),
15
- error: async (e) => {
16
- if (e instanceof ErrorEvent) {
17
- return new ErrorEvent({
18
- data: serializeRPCJson(e.data),
19
- cause: e
20
- });
21
- }
22
- return new ErrorEvent({
23
- data: serializeRPCJson(toORPCError(e).toJSON()),
24
- cause: e
25
- });
26
- }
27
- });
28
- }
29
- const serializedJSON = serializeRPCJson(data);
30
- const { maps, values: blobs } = findDeepMatches((v) => v instanceof Blob, serializedJSON.json);
31
- if (blobs.length === 0) {
32
- return serializedJSON;
33
- }
34
- const form = new FormData();
35
- form.set("data", JSON.stringify(serializedJSON));
36
- form.set("maps", JSON.stringify(maps));
37
- for (const i in blobs) {
38
- form.set(i, blobs[i]);
39
- }
40
- return form;
41
- }
42
- deserialize(serialized) {
43
- if (isAsyncIteratorObject(serialized)) {
44
- return mapEventIterator(serialized, {
45
- value: async (value) => deserializeRPCJson(value),
46
- error: async (e) => {
47
- if (!(e instanceof ErrorEvent)) {
48
- return e;
49
- }
50
- const deserialized = deserializeRPCJson(e.data);
51
- if (ORPCError.isValidJSON(deserialized)) {
52
- return ORPCError.fromJSON(deserialized, { cause: e });
53
- }
54
- return new ErrorEvent({
55
- data: deserialized,
56
- cause: e
57
- });
58
- }
59
- });
60
- }
61
- if (!(serialized instanceof FormData)) {
62
- return deserializeRPCJson(serialized);
63
- }
64
- const data = JSON.parse(serialized.get("data"));
65
- const maps = JSON.parse(serialized.get("maps"));
66
- for (const i in maps) {
67
- data.json = set(data.json, maps[i], serialized.get(i));
68
- }
69
- return deserializeRPCJson(data);
70
- }
71
- };
72
- function serializeRPCJson(value, segments = [], meta = []) {
73
- if (typeof value === "bigint") {
74
- meta.push(["bigint", segments]);
75
- return { json: value.toString(), meta };
76
- }
77
- if (value instanceof Date) {
78
- meta.push(["date", segments]);
79
- const data = Number.isNaN(value.getTime()) ? "Invalid Date" : value.toISOString();
80
- return { json: data, meta };
81
- }
82
- if (Number.isNaN(value)) {
83
- meta.push(["nan", segments]);
84
- return { json: "NaN", meta };
85
- }
86
- if (value instanceof RegExp) {
87
- meta.push(["regexp", segments]);
88
- return { json: value.toString(), meta };
89
- }
90
- if (value instanceof URL) {
91
- meta.push(["url", segments]);
92
- return { json: value.toString(), meta };
93
- }
94
- if (isObject(value)) {
95
- const json = {};
96
- for (const k in value) {
97
- json[k] = serializeRPCJson(value[k], [...segments, k], meta).json;
98
- }
99
- return { json, meta };
100
- }
101
- if (Array.isArray(value)) {
102
- const json = value.map((v, i) => {
103
- if (v === void 0) {
104
- meta.push(["undefined", [...segments, i]]);
105
- return null;
106
- }
107
- return serializeRPCJson(v, [...segments, i], meta).json;
108
- });
109
- return { json, meta };
110
- }
111
- if (value instanceof Set) {
112
- const result = serializeRPCJson(Array.from(value), segments, meta);
113
- meta.push(["set", segments]);
114
- return result;
115
- }
116
- if (value instanceof Map) {
117
- const result = serializeRPCJson(Array.from(value.entries()), segments, meta);
118
- meta.push(["map", segments]);
119
- return result;
120
- }
121
- return { json: value, meta };
122
- }
123
- function deserializeRPCJson({
124
- json,
125
- meta
126
- }) {
127
- if (meta.length === 0) {
128
- return json;
129
- }
130
- const ref = { data: json };
131
- for (const [type, segments] of meta) {
132
- let currentRef = ref;
133
- let preSegment = "data";
134
- for (let i = 0; i < segments.length; i++) {
135
- currentRef = currentRef[preSegment];
136
- preSegment = segments[i];
137
- }
138
- switch (type) {
139
- case "nan":
140
- currentRef[preSegment] = Number.NaN;
141
- break;
142
- case "bigint":
143
- currentRef[preSegment] = BigInt(currentRef[preSegment]);
144
- break;
145
- case "date":
146
- currentRef[preSegment] = new Date(currentRef[preSegment]);
147
- break;
148
- case "regexp": {
149
- const [, pattern, flags] = currentRef[preSegment].match(/^\/(.*)\/([a-z]*)$/);
150
- currentRef[preSegment] = new RegExp(pattern, flags);
151
- break;
152
- }
153
- case "url":
154
- currentRef[preSegment] = new URL(currentRef[preSegment]);
155
- break;
156
- case "undefined":
157
- currentRef[preSegment] = void 0;
158
- break;
159
- case "map":
160
- currentRef[preSegment] = new Map(currentRef[preSegment]);
161
- break;
162
- case "set":
163
- currentRef[preSegment] = new Set(currentRef[preSegment]);
164
- break;
165
- /* v8 ignore next 3 */
166
- default: {
167
- const _expected = type;
168
- }
169
- }
170
- }
171
- return ref.data;
172
- }
173
-
174
- export {
175
- RPCSerializer,
176
- serializeRPCJson
177
- };
178
- //# sourceMappingURL=chunk-TPEMQB7D.js.map
package/dist/fetch.js DELETED
@@ -1,128 +0,0 @@
1
- import {
2
- RPCSerializer
3
- } from "./chunk-TPEMQB7D.js";
4
- import {
5
- ORPCError,
6
- createAutoRetryEventIterator
7
- } from "./chunk-2UPNYYFF.js";
8
-
9
- // src/adapters/fetch/rpc-link.ts
10
- import { isAsyncIteratorObject } from "@orpc/server-standard";
11
- import { toFetchBody, toStandardBody } from "@orpc/server-standard-fetch";
12
- import { trim, value } from "@orpc/shared";
13
- var InvalidEventSourceRetryResponse = class extends Error {
14
- };
15
- var RPCLink = class {
16
- fetch;
17
- rpcSerializer;
18
- maxUrlLength;
19
- fallbackMethod;
20
- method;
21
- headers;
22
- url;
23
- eventSourceMaxNumberOfRetries;
24
- eventSourceRetryDelay;
25
- eventSourceRetry;
26
- constructor(options) {
27
- this.fetch = options.fetch ?? globalThis.fetch.bind(globalThis);
28
- this.rpcSerializer = options.rpcSerializer ?? new RPCSerializer();
29
- this.maxUrlLength = options.maxUrlLength ?? 2083;
30
- this.fallbackMethod = options.fallbackMethod ?? "POST";
31
- this.url = options.url;
32
- this.eventSourceMaxNumberOfRetries = options.eventSourceMaxNumberOfRetries ?? 5;
33
- this.method = options.method ?? this.fallbackMethod;
34
- this.headers = options.headers ?? {};
35
- this.eventSourceRetry = options.eventSourceRetry ?? true;
36
- this.eventSourceRetryDelay = options.eventSourceRetryDelay ?? (({ retryTimes, lastRetry }) => lastRetry ?? 1e3 * 2 ** retryTimes);
37
- }
38
- async call(path, input, options) {
39
- const output = await this.performCall(path, input, options);
40
- if (!isAsyncIteratorObject(output)) {
41
- return output;
42
- }
43
- return createAutoRetryEventIterator(output, async (reconnectOptions) => {
44
- if (options.signal?.aborted || reconnectOptions.retryTimes > this.eventSourceMaxNumberOfRetries) {
45
- return null;
46
- }
47
- if (!await value(this.eventSourceRetry, reconnectOptions, options, path, input)) {
48
- return null;
49
- }
50
- const delay = await value(this.eventSourceRetryDelay, reconnectOptions, options, path, input);
51
- await new Promise((resolve) => setTimeout(resolve, delay));
52
- const updatedOptions = { ...options, lastEventId: reconnectOptions.lastEventId };
53
- const maybeIterator = await this.performCall(path, input, updatedOptions);
54
- if (!isAsyncIteratorObject(maybeIterator)) {
55
- throw new InvalidEventSourceRetryResponse("Invalid EventSource retry response");
56
- }
57
- return maybeIterator;
58
- }, void 0);
59
- }
60
- async performCall(path, input, options) {
61
- const encoded = await this.encodeRequest(path, input, options);
62
- const fetchBody = toFetchBody(encoded.body, encoded.headers);
63
- if (options.lastEventId !== void 0) {
64
- encoded.headers.set("last-event-id", options.lastEventId);
65
- }
66
- const response = await this.fetch(encoded.url, {
67
- method: encoded.method,
68
- headers: encoded.headers,
69
- body: fetchBody,
70
- signal: options.signal
71
- }, options, path, input);
72
- const body = await toStandardBody(response);
73
- const deserialized = (() => {
74
- try {
75
- return this.rpcSerializer.deserialize(body);
76
- } catch (error) {
77
- if (response.ok) {
78
- throw new ORPCError("INTERNAL_SERVER_ERROR", {
79
- message: "Invalid RPC response",
80
- cause: error
81
- });
82
- }
83
- throw new ORPCError(response.status.toString(), {
84
- message: response.statusText
85
- });
86
- }
87
- })();
88
- if (!response.ok) {
89
- if (ORPCError.isValidJSON(deserialized)) {
90
- throw ORPCError.fromJSON(deserialized);
91
- }
92
- throw new ORPCError("INTERNAL_SERVER_ERROR", {
93
- message: "Invalid RPC error response",
94
- cause: deserialized
95
- });
96
- }
97
- return deserialized;
98
- }
99
- async encodeRequest(path, input, options) {
100
- const expectedMethod = await value(this.method, options, path, input);
101
- const headers = new Headers(await value(this.headers, options, path, input));
102
- const url = new URL(`${trim(this.url, "/")}/${path.map(encodeURIComponent).join("/")}`);
103
- const serialized = this.rpcSerializer.serialize(input);
104
- if (expectedMethod === "GET" && !(serialized instanceof FormData) && !isAsyncIteratorObject(serialized)) {
105
- const getUrl = new URL(url);
106
- getUrl.searchParams.append("data", JSON.stringify(serialized));
107
- if (getUrl.toString().length <= this.maxUrlLength) {
108
- return {
109
- body: void 0,
110
- method: expectedMethod,
111
- headers,
112
- url: getUrl
113
- };
114
- }
115
- }
116
- return {
117
- url,
118
- method: expectedMethod === "GET" ? this.fallbackMethod : expectedMethod,
119
- headers,
120
- body: serialized
121
- };
122
- }
123
- };
124
- export {
125
- InvalidEventSourceRetryResponse,
126
- RPCLink
127
- };
128
- //# sourceMappingURL=fetch.js.map