@orpc/client 0.0.0-next.0c0619f → 0.0.0-next.0ca4e14

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 +29 -21
  2. package/dist/adapters/fetch/index.d.mts +33 -0
  3. package/dist/adapters/fetch/index.d.ts +33 -0
  4. package/dist/adapters/fetch/index.mjs +29 -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 +44 -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 +389 -0
  20. package/dist/shared/client.BOYsZIRq.d.mts +29 -0
  21. package/dist/shared/client.BOYsZIRq.d.ts +29 -0
  22. package/dist/shared/client.C4VxIexA.d.mts +46 -0
  23. package/dist/shared/client.CXXEPIbK.d.ts +46 -0
  24. package/dist/shared/client.DHOfWE0c.mjs +172 -0
  25. package/dist/shared/client.DwfV9Oyl.mjs +355 -0
  26. package/dist/shared/client.WCinBImJ.d.ts +91 -0
  27. package/dist/shared/client.aTp4sII-.d.mts +91 -0
  28. package/package.json +30 -24
  29. package/dist/chunk-2UPNYYFF.js +0 -288
  30. package/dist/chunk-OX3K2AN4.js +0 -212
  31. package/dist/fetch.js +0 -128
  32. package/dist/index.js +0 -81
  33. package/dist/openapi.js +0 -329
  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 -84
  45. package/dist/src/openapi/index.d.ts +0 -4
  46. package/dist/src/openapi/json-serializer.d.ts +0 -5
  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 -5
@@ -1,212 +0,0 @@
1
- import {
2
- ORPCError,
3
- mapEventIterator,
4
- toORPCError
5
- } from "./chunk-2UPNYYFF.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/server-standard";
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-OX3K2AN4.js.map
package/dist/fetch.js DELETED
@@ -1,128 +0,0 @@
1
- import {
2
- RPCSerializer
3
- } from "./chunk-OX3K2AN4.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) && !(serialized instanceof Blob) && !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
package/dist/index.js DELETED
@@ -1,81 +0,0 @@
1
- import {
2
- COMMON_ORPC_ERROR_DEFS,
3
- ORPCError,
4
- createAutoRetryEventIterator,
5
- fallbackORPCErrorMessage,
6
- fallbackORPCErrorStatus,
7
- isDefinedError,
8
- mapEventIterator,
9
- onEventIteratorStatusChange,
10
- registerEventIteratorState,
11
- toORPCError,
12
- updateEventIteratorStatus
13
- } from "./chunk-2UPNYYFF.js";
14
-
15
- // src/client.ts
16
- function createORPCClient(link, options) {
17
- const path = options?.path ?? [];
18
- const procedureClient = async (...[input, options2]) => {
19
- const optionsOut = {
20
- ...options2,
21
- context: options2?.context ?? {}
22
- // options.context can be undefined when all field is optional
23
- };
24
- return await link.call(path, input, optionsOut);
25
- };
26
- const recursive = new Proxy(procedureClient, {
27
- get(target, key) {
28
- if (typeof key !== "string") {
29
- return Reflect.get(target, key);
30
- }
31
- return createORPCClient(link, {
32
- ...options,
33
- path: [...path, key]
34
- });
35
- }
36
- });
37
- return recursive;
38
- }
39
-
40
- // src/dynamic-link.ts
41
- var DynamicLink = class {
42
- constructor(linkResolver) {
43
- this.linkResolver = linkResolver;
44
- }
45
- async call(path, input, options) {
46
- const resolvedLink = await this.linkResolver(options, path, input);
47
- const output = await resolvedLink.call(path, input, options);
48
- return output;
49
- }
50
- };
51
-
52
- // src/utils.ts
53
- async function safe(promise) {
54
- try {
55
- const output = await promise;
56
- return [output, void 0, false];
57
- } catch (e) {
58
- const error = e;
59
- if (isDefinedError(error)) {
60
- return [void 0, error, true];
61
- }
62
- return [void 0, error, false];
63
- }
64
- }
65
- export {
66
- COMMON_ORPC_ERROR_DEFS,
67
- DynamicLink,
68
- ORPCError,
69
- createAutoRetryEventIterator,
70
- createORPCClient,
71
- fallbackORPCErrorMessage,
72
- fallbackORPCErrorStatus,
73
- isDefinedError,
74
- mapEventIterator,
75
- onEventIteratorStatusChange,
76
- registerEventIteratorState,
77
- safe,
78
- toORPCError,
79
- updateEventIteratorStatus
80
- };
81
- //# sourceMappingURL=index.js.map