@orpc/client 0.0.0-next.b45a533 → 0.0.0-next.b47b94e
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.
- package/README.md +25 -17
- package/dist/adapters/fetch/index.d.mts +17 -13
- package/dist/adapters/fetch/index.d.ts +17 -13
- package/dist/adapters/fetch/index.mjs +5 -11
- package/dist/adapters/standard/index.d.mts +8 -146
- package/dist/adapters/standard/index.d.ts +8 -146
- package/dist/adapters/standard/index.mjs +2 -2
- package/dist/adapters/websocket/index.d.mts +29 -0
- package/dist/adapters/websocket/index.d.ts +29 -0
- package/dist/adapters/websocket/index.mjs +31 -0
- package/dist/index.d.mts +36 -22
- package/dist/index.d.ts +36 -22
- package/dist/index.mjs +35 -33
- package/dist/plugins/index.d.mts +162 -0
- package/dist/plugins/index.d.ts +162 -0
- package/dist/plugins/index.mjs +291 -0
- package/dist/shared/client.4TS_0JaO.d.mts +29 -0
- package/dist/shared/client.4TS_0JaO.d.ts +29 -0
- package/dist/shared/client.7ZYxJok_.d.ts +46 -0
- package/dist/shared/client.B2432-Lu.d.ts +91 -0
- package/dist/shared/client.CRWEpqLB.mjs +175 -0
- package/dist/shared/client.ClwIM_ku.d.mts +91 -0
- package/dist/shared/{client.Df5pd75N.mjs → client.DpICn1BD.mjs} +113 -78
- package/dist/shared/client.ds1abV85.d.mts +46 -0
- package/package.json +15 -4
- package/dist/shared/client.D_CzLDyB.d.mts +0 -42
- package/dist/shared/client.D_CzLDyB.d.ts +0 -42
- package/dist/shared/client.XAn8cDTM.mjs +0 -266
@@ -1,266 +0,0 @@
|
|
1
|
-
import { isObject, isTypescriptObject, retry } 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, ...[options]) {
|
94
|
-
if (options?.status && (options.status < 400 || options.status >= 600)) {
|
95
|
-
throw new Error("[ORPCError] The error status code must be in the 400-599 range.");
|
96
|
-
}
|
97
|
-
const message = fallbackORPCErrorMessage(code, options?.message);
|
98
|
-
super(message, options);
|
99
|
-
this.code = code;
|
100
|
-
this.status = fallbackORPCErrorStatus(code, options?.status);
|
101
|
-
this.defined = options?.defined ?? false;
|
102
|
-
this.data = options?.data;
|
103
|
-
}
|
104
|
-
toJSON() {
|
105
|
-
return {
|
106
|
-
defined: this.defined,
|
107
|
-
code: this.code,
|
108
|
-
status: this.status,
|
109
|
-
message: this.message,
|
110
|
-
data: this.data
|
111
|
-
};
|
112
|
-
}
|
113
|
-
static fromJSON(json, options) {
|
114
|
-
return new ORPCError(json.code, {
|
115
|
-
...options,
|
116
|
-
...json
|
117
|
-
});
|
118
|
-
}
|
119
|
-
static isValidJSON(json) {
|
120
|
-
if (!isObject(json)) {
|
121
|
-
return false;
|
122
|
-
}
|
123
|
-
const validKeys = ["defined", "code", "status", "message", "data"];
|
124
|
-
if (Object.keys(json).some((k) => !validKeys.includes(k))) {
|
125
|
-
return false;
|
126
|
-
}
|
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";
|
128
|
-
}
|
129
|
-
}
|
130
|
-
function isDefinedError(error) {
|
131
|
-
return error instanceof ORPCError && error.defined;
|
132
|
-
}
|
133
|
-
function toORPCError(error) {
|
134
|
-
return error instanceof ORPCError ? error : new ORPCError("INTERNAL_SERVER_ERROR", {
|
135
|
-
message: "Internal server error",
|
136
|
-
cause: error
|
137
|
-
});
|
138
|
-
}
|
139
|
-
|
140
|
-
const 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, options = {}) {
|
151
|
-
const notifyImmediately = options.notifyImmediately ?? true;
|
152
|
-
const state = iteratorStates.get(iterator);
|
153
|
-
if (!state) {
|
154
|
-
throw new Error("Iterator is not registered.");
|
155
|
-
}
|
156
|
-
if (notifyImmediately) {
|
157
|
-
callback(state.status);
|
158
|
-
}
|
159
|
-
state.listeners.push(callback);
|
160
|
-
return () => {
|
161
|
-
const index = state.listeners.indexOf(callback);
|
162
|
-
if (index !== -1) {
|
163
|
-
state.listeners.splice(index, 1);
|
164
|
-
}
|
165
|
-
};
|
166
|
-
}
|
167
|
-
|
168
|
-
function mapEventIterator(iterator, maps) {
|
169
|
-
return async function* () {
|
170
|
-
try {
|
171
|
-
while (true) {
|
172
|
-
const { done, value } = await iterator.next();
|
173
|
-
let mappedValue = await maps.value(value, done);
|
174
|
-
if (mappedValue !== value) {
|
175
|
-
const meta = getEventMeta(value);
|
176
|
-
if (meta && isTypescriptObject(mappedValue)) {
|
177
|
-
mappedValue = withEventMeta(mappedValue, meta);
|
178
|
-
}
|
179
|
-
}
|
180
|
-
if (done) {
|
181
|
-
return mappedValue;
|
182
|
-
}
|
183
|
-
yield mappedValue;
|
184
|
-
}
|
185
|
-
} catch (error) {
|
186
|
-
let mappedError = await maps.error(error);
|
187
|
-
if (mappedError !== error) {
|
188
|
-
const meta = getEventMeta(error);
|
189
|
-
if (meta && isTypescriptObject(mappedError)) {
|
190
|
-
mappedError = withEventMeta(mappedError, meta);
|
191
|
-
}
|
192
|
-
}
|
193
|
-
throw mappedError;
|
194
|
-
} finally {
|
195
|
-
await iterator.return?.();
|
196
|
-
}
|
197
|
-
}();
|
198
|
-
}
|
199
|
-
const MAX_ALLOWED_RETRY_TIMES = 99;
|
200
|
-
function createAutoRetryEventIterator(initial, reconnect, initialLastEventId) {
|
201
|
-
const state = {
|
202
|
-
status: "connected",
|
203
|
-
listeners: []
|
204
|
-
};
|
205
|
-
const iterator = async function* () {
|
206
|
-
let current = initial;
|
207
|
-
let lastEventId = initialLastEventId;
|
208
|
-
let lastRetry;
|
209
|
-
let retryTimes = 0;
|
210
|
-
try {
|
211
|
-
while (true) {
|
212
|
-
try {
|
213
|
-
updateEventIteratorStatus(state, "connected");
|
214
|
-
const { done, value } = await current.next();
|
215
|
-
const meta = getEventMeta(value);
|
216
|
-
lastEventId = meta?.id ?? lastEventId;
|
217
|
-
lastRetry = meta?.retry ?? lastRetry;
|
218
|
-
retryTimes = 0;
|
219
|
-
if (done) {
|
220
|
-
return value;
|
221
|
-
}
|
222
|
-
yield value;
|
223
|
-
} catch (e) {
|
224
|
-
updateEventIteratorStatus(state, "reconnecting");
|
225
|
-
const meta = getEventMeta(e);
|
226
|
-
lastEventId = meta?.id ?? lastEventId;
|
227
|
-
lastRetry = meta?.retry ?? lastRetry;
|
228
|
-
let currentError = e;
|
229
|
-
current = await retry({ times: MAX_ALLOWED_RETRY_TIMES }, async (exit) => {
|
230
|
-
retryTimes += 1;
|
231
|
-
if (retryTimes > MAX_ALLOWED_RETRY_TIMES) {
|
232
|
-
throw exit(new Error(
|
233
|
-
`Exceeded maximum retry attempts (${MAX_ALLOWED_RETRY_TIMES}) for event iterator. Possible infinite retry loop detected. Please review the retry logic.`,
|
234
|
-
{ cause: currentError }
|
235
|
-
));
|
236
|
-
}
|
237
|
-
const reconnected = await (async () => {
|
238
|
-
try {
|
239
|
-
return await reconnect({
|
240
|
-
lastRetry,
|
241
|
-
lastEventId,
|
242
|
-
retryTimes,
|
243
|
-
error: currentError
|
244
|
-
});
|
245
|
-
} catch (e2) {
|
246
|
-
currentError = e2;
|
247
|
-
throw e2;
|
248
|
-
}
|
249
|
-
})();
|
250
|
-
if (!reconnected) {
|
251
|
-
throw exit(currentError);
|
252
|
-
}
|
253
|
-
return reconnected;
|
254
|
-
});
|
255
|
-
}
|
256
|
-
}
|
257
|
-
} finally {
|
258
|
-
updateEventIteratorStatus(state, "closed");
|
259
|
-
await current.return?.();
|
260
|
-
}
|
261
|
-
}();
|
262
|
-
registerEventIteratorState(iterator, state);
|
263
|
-
return iterator;
|
264
|
-
}
|
265
|
-
|
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 };
|