@yorun-ai/vrpc 0.9.1 → 0.9.2
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 +103 -3
- package/README.zh-CN.md +103 -3
- package/dist/client.cjs +45 -24
- package/dist/client.d.cts +47 -11
- package/dist/client.d.ts +47 -11
- package/dist/client.es.js +41 -25
- package/dist/http.cjs +7 -3
- package/dist/http.d.cts +35 -9
- package/dist/http.d.ts +35 -9
- package/dist/http.es.js +4 -4
- package/dist/{request-options-disym648.js → request-options-Bjy2LvRE.js} +175 -47
- package/dist/{request-options-2HZzkj4w.cjs → request-options-m8_63jpk.cjs} +210 -46
- package/docs/guides/axios.md +43 -21
- package/docs/guides/usage.md +167 -7
- package/docs/maintainers/architecture.md +26 -4
- package/docs/maintainers/protocol.md +16 -1
- package/package.json +1 -1
package/dist/client.es.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { c as HTTP_TIMEOUT_ERROR_CODE, d as
|
|
1
|
+
import { _ as isVrpcError, c as HTTP_TIMEOUT_ERROR_CODE, d as HttpInvokeError, f as HttpTimeoutError, h as VrpcProtocolError, i as createFetchTransport, l as HttpAbortError, m as VrpcInvokeError, n as splitVrpcRequestOptions, o as buildRequestUrl, p as HttpTransportError, r as createClientCore, s as mergeHeaders, u as HttpClientError } from "./request-options-Bjy2LvRE.js";
|
|
2
2
|
//#region src/protocol/id.ts
|
|
3
3
|
var DEFAULT_CLIENT_INSTANCE_ID_STORAGE_KEY = "vrpc-client-instance-id";
|
|
4
4
|
function getRandomByteArray(length) {
|
|
@@ -223,8 +223,8 @@ function readEnvelopeResult(payload) {
|
|
|
223
223
|
if (!payload || typeof payload !== "object") return;
|
|
224
224
|
return payload.result;
|
|
225
225
|
}
|
|
226
|
-
function requireResponseHeader(
|
|
227
|
-
const value = headers.get(name);
|
|
226
|
+
function requireResponseHeader(response, name) {
|
|
227
|
+
const value = response.headers.get(name);
|
|
228
228
|
if (!value) throw new Error(`Invalid vRPC response: missing ${name} header.`);
|
|
229
229
|
return value;
|
|
230
230
|
}
|
|
@@ -233,28 +233,33 @@ function readDecodedEnvelopeField(payload, field) {
|
|
|
233
233
|
if (payload && typeof payload === "object") return payload[field];
|
|
234
234
|
}
|
|
235
235
|
async function decodeVrpcResponsePayload(response, cborCodec, resultSchema) {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
236
|
+
try {
|
|
237
|
+
requireResponseHeader(response, "vrpc-status");
|
|
238
|
+
assertValidVrpcServerHeader(response.headers.get("vrpc-server"));
|
|
239
|
+
const contentType = mediaTypeOf(requireResponseHeader(response, "content-type"));
|
|
240
|
+
if (contentType === "application/vrpc+json") {
|
|
241
|
+
const text = await response.text();
|
|
242
|
+
if (!text) throw new Error("Invalid vRPC response: empty response body.");
|
|
243
|
+
try {
|
|
244
|
+
return JSON.parse(text);
|
|
245
|
+
} catch {
|
|
246
|
+
throw new Error("Invalid vRPC response: JSON body cannot be parsed.");
|
|
247
|
+
}
|
|
246
248
|
}
|
|
249
|
+
if (contentType !== "application/vrpc+cbor") throw new Error(`Invalid vRPC response content-type: ${contentType || "empty"}.`);
|
|
250
|
+
if (!resultSchema) throw new Error("Invalid vRPC response: unexpected CBOR body for a JSON method.");
|
|
251
|
+
if (!cborCodec) throw new Error("A cborCodec is required to decode a CBOR vRPC response.");
|
|
252
|
+
const decoded = await cborCodec.decode(new Uint8Array(await response.arrayBuffer()));
|
|
253
|
+
const result = readDecodedEnvelopeField(decoded, "result");
|
|
254
|
+
const error = readDecodedEnvelopeField(decoded, "error");
|
|
255
|
+
return {
|
|
256
|
+
result: fromCborWireValue(result, resultSchema),
|
|
257
|
+
error: normalizeCborValue(error)
|
|
258
|
+
};
|
|
259
|
+
} catch (cause) {
|
|
260
|
+
if (cause instanceof VrpcProtocolError) throw cause;
|
|
261
|
+
throw new VrpcProtocolError(response, cause instanceof Error ? cause.message : "Invalid vRPC response.", {}, { cause });
|
|
247
262
|
}
|
|
248
|
-
if (contentType !== "application/vrpc+cbor") throw new Error(`Invalid vRPC response content-type: ${contentType || "empty"}.`);
|
|
249
|
-
if (!resultSchema) throw new Error("Invalid vRPC response: unexpected CBOR body for a JSON method.");
|
|
250
|
-
if (!cborCodec) throw new Error("A cborCodec is required to decode a CBOR vRPC response.");
|
|
251
|
-
const decoded = await cborCodec.decode(new Uint8Array(await response.arrayBuffer()));
|
|
252
|
-
const result = readDecodedEnvelopeField(decoded, "result");
|
|
253
|
-
const error = readDecodedEnvelopeField(decoded, "error");
|
|
254
|
-
return {
|
|
255
|
-
result: fromCborWireValue(result, resultSchema),
|
|
256
|
-
error: normalizeCborValue(error)
|
|
257
|
-
};
|
|
258
263
|
}
|
|
259
264
|
function normalizeResponseHeaders(headers) {
|
|
260
265
|
if (!headers) return new Headers();
|
|
@@ -416,7 +421,18 @@ function createVrpcClient(options) {
|
|
|
416
421
|
}
|
|
417
422
|
};
|
|
418
423
|
},
|
|
419
|
-
parseResponse: (response, context) =>
|
|
424
|
+
parseResponse: async (response, context) => {
|
|
425
|
+
try {
|
|
426
|
+
return await decodeVrpcResponsePayload(response, cborCodec, context.options.wire?.result);
|
|
427
|
+
} catch (cause) {
|
|
428
|
+
const vrpcStatus = response.headers.get("vrpc-status");
|
|
429
|
+
if (vrpcStatus && (!response.ok || vrpcStatus !== "OK")) throw new VrpcInvokeError(response, null, {
|
|
430
|
+
url: context.url,
|
|
431
|
+
method: String(context.init.method || "POST").toUpperCase()
|
|
432
|
+
}, { cause });
|
|
433
|
+
throw cause;
|
|
434
|
+
}
|
|
435
|
+
},
|
|
420
436
|
validateRequest: (_context, requestMeta) => {
|
|
421
437
|
if (requestMeta.method !== "POST") throw new Error(`Unsupported method: ${requestMeta.method}, only POST is allowed.`);
|
|
422
438
|
},
|
|
@@ -439,4 +455,4 @@ function createVrpcClient(options) {
|
|
|
439
455
|
};
|
|
440
456
|
}
|
|
441
457
|
//#endregion
|
|
442
|
-
export { DEFAULT_CLIENT_INSTANCE_ID_STORAGE_KEY, HTTP_TIMEOUT_ERROR_CODE, HttpInvokeError, HttpTimeoutError, VRPC_CBOR_CONTENT_TYPE, VRPC_JSON_CONTENT_TYPE, VrpcInvokeError, buildVrpcHeaders, buildVrpcPath, buildVrpcRequestBody, createFetchTransport, createVrpcClient, decodeVrpcResponsePayload, encodeVrpcRequestBody, generateClientInstanceId, generateVrpcId, generateVrpcSpan, getClientInstanceId, parseVrpcResponse, unwrapVrpcResponse };
|
|
458
|
+
export { DEFAULT_CLIENT_INSTANCE_ID_STORAGE_KEY, HTTP_TIMEOUT_ERROR_CODE, HttpAbortError, HttpClientError, HttpInvokeError, HttpTimeoutError, HttpTransportError, VRPC_CBOR_CONTENT_TYPE, VRPC_JSON_CONTENT_TYPE, VrpcInvokeError, VrpcProtocolError, buildVrpcHeaders, buildVrpcPath, buildVrpcRequestBody, createFetchTransport, createVrpcClient, decodeVrpcResponsePayload, encodeVrpcRequestBody, generateClientInstanceId, generateVrpcId, generateVrpcSpan, getClientInstanceId, isVrpcError, parseVrpcResponse, unwrapVrpcResponse };
|
package/dist/http.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_request_options = require("./request-options-
|
|
2
|
+
const require_request_options = require("./request-options-m8_63jpk.cjs");
|
|
3
3
|
//#region src/core/create-http-client.ts
|
|
4
4
|
function resolveJsonBody(request) {
|
|
5
5
|
if (request.json !== void 0 && request.body !== void 0) throw new Error("Request json and body cannot be used together.");
|
|
@@ -7,7 +7,7 @@ function resolveJsonBody(request) {
|
|
|
7
7
|
return request.body;
|
|
8
8
|
}
|
|
9
9
|
function createHttpClient(options) {
|
|
10
|
-
const { prefixUrl, fetchImpl, transport, interceptors, ...baseOptions } = options;
|
|
10
|
+
const { prefixUrl, allowAbsoluteUrls = false, fetchImpl, transport, interceptors, ...baseOptions } = options;
|
|
11
11
|
const core = require_request_options.createClientCore({
|
|
12
12
|
prefixUrl,
|
|
13
13
|
baseOptions,
|
|
@@ -18,7 +18,7 @@ function createHttpClient(options) {
|
|
|
18
18
|
prepareRequest: (request, helpers) => {
|
|
19
19
|
const requestOptions = helpers.splitRequestOptions(request.options);
|
|
20
20
|
const method = String(request.method || "GET").toUpperCase();
|
|
21
|
-
const url = require_request_options.appendQueryToUrl(require_request_options.buildRequestUrl(helpers.prefixUrl, request.path), request.query);
|
|
21
|
+
const url = require_request_options.appendQueryToUrl(require_request_options.buildRequestUrl(helpers.prefixUrl, request.path, { allowAbsoluteUrls }), request.query);
|
|
22
22
|
const finalHeaders = require_request_options.mergeHeaders(helpers.baseOptions.headers, requestOptions.headers);
|
|
23
23
|
const body = resolveJsonBody(request);
|
|
24
24
|
if (request.json !== void 0 && !finalHeaders.has("content-type")) finalHeaders.set("content-type", "application/json");
|
|
@@ -63,7 +63,11 @@ function createHttpClient(options) {
|
|
|
63
63
|
}
|
|
64
64
|
//#endregion
|
|
65
65
|
exports.HTTP_TIMEOUT_ERROR_CODE = require_request_options.HTTP_TIMEOUT_ERROR_CODE;
|
|
66
|
+
exports.HttpAbortError = require_request_options.HttpAbortError;
|
|
67
|
+
exports.HttpClientError = require_request_options.HttpClientError;
|
|
66
68
|
exports.HttpInvokeError = require_request_options.HttpInvokeError;
|
|
67
69
|
exports.HttpTimeoutError = require_request_options.HttpTimeoutError;
|
|
70
|
+
exports.HttpTransportError = require_request_options.HttpTransportError;
|
|
68
71
|
exports.createFetchTransport = require_request_options.createFetchTransport;
|
|
69
72
|
exports.createHttpClient = createHttpClient;
|
|
73
|
+
exports.isHttpError = require_request_options.isHttpError;
|
package/dist/http.d.cts
CHANGED
|
@@ -19,6 +19,7 @@ type HttpRequestConfig = {
|
|
|
19
19
|
};
|
|
20
20
|
type HttpClientOptions = HttpRequestOptions & {
|
|
21
21
|
prefixUrl: string | URL;
|
|
22
|
+
allowAbsoluteUrls?: boolean;
|
|
22
23
|
fetchImpl?: typeof fetch;
|
|
23
24
|
transport?: HttpTransport;
|
|
24
25
|
interceptors?: HttpInterceptor[];
|
|
@@ -71,13 +72,38 @@ type HttpErrorRequestMeta = {
|
|
|
71
72
|
url?: string;
|
|
72
73
|
method?: string;
|
|
73
74
|
};
|
|
75
|
+
type HttpClientErrorOptions = {
|
|
76
|
+
cause?: unknown;
|
|
77
|
+
};
|
|
78
|
+
type HttpAbortErrorOptions = HttpClientErrorOptions & {
|
|
79
|
+
reason?: unknown;
|
|
80
|
+
};
|
|
81
|
+
type HttpClientErrorKind = "invoke" | "timeout" | "abort" | "transport" | "protocol";
|
|
74
82
|
declare const HTTP_TIMEOUT_ERROR_CODE: "HTTP_TIMEOUT";
|
|
75
|
-
declare
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
83
|
+
declare const HTTP_ABORT_ERROR_CODE: "HTTP_ABORTED";
|
|
84
|
+
declare const HTTP_TRANSPORT_ERROR_CODE: "HTTP_TRANSPORT_ERROR";
|
|
85
|
+
declare class HttpClientError<TKind extends HttpClientErrorKind = HttpClientErrorKind> extends Error {
|
|
86
|
+
readonly isHttpClientError: true;
|
|
87
|
+
readonly kind: TKind;
|
|
88
|
+
readonly url: string;
|
|
89
|
+
readonly method: string;
|
|
90
|
+
constructor(message: string, kind: TKind, requestMeta?: HttpErrorRequestMeta, options?: HttpClientErrorOptions);
|
|
91
|
+
}
|
|
92
|
+
declare class HttpTimeoutError extends HttpClientError<"timeout"> {
|
|
93
|
+
readonly code: typeof HTTP_TIMEOUT_ERROR_CODE;
|
|
94
|
+
readonly timeoutMs: number;
|
|
95
|
+
constructor(timeoutMs: number, requestMeta?: HttpErrorRequestMeta, options?: HttpClientErrorOptions);
|
|
96
|
+
}
|
|
97
|
+
declare class HttpAbortError extends HttpClientError<"abort"> {
|
|
98
|
+
readonly code: typeof HTTP_ABORT_ERROR_CODE;
|
|
99
|
+
readonly reason?: unknown;
|
|
100
|
+
constructor(requestMeta?: HttpErrorRequestMeta, options?: HttpAbortErrorOptions);
|
|
79
101
|
}
|
|
80
|
-
declare class
|
|
102
|
+
declare class HttpTransportError extends HttpClientError<"transport"> {
|
|
103
|
+
readonly code: typeof HTTP_TRANSPORT_ERROR_CODE;
|
|
104
|
+
constructor(requestMeta?: HttpErrorRequestMeta, options?: HttpClientErrorOptions);
|
|
105
|
+
}
|
|
106
|
+
declare class HttpInvokeError extends HttpClientError<"invoke"> {
|
|
81
107
|
type?: string;
|
|
82
108
|
code?: string;
|
|
83
109
|
reason?: string;
|
|
@@ -86,10 +112,10 @@ declare class HttpInvokeError extends Error {
|
|
|
86
112
|
statusText: string;
|
|
87
113
|
responseHeaders: Headers;
|
|
88
114
|
payload: unknown;
|
|
89
|
-
|
|
90
|
-
method: string;
|
|
91
|
-
constructor(response: Response, payload: unknown, requestMeta?: HttpErrorRequestMeta);
|
|
115
|
+
constructor(response: Response, payload: unknown, requestMeta?: HttpErrorRequestMeta, options?: HttpClientErrorOptions);
|
|
92
116
|
}
|
|
117
|
+
type HttpError = HttpAbortError | HttpInvokeError | HttpTimeoutError | HttpTransportError;
|
|
118
|
+
declare function isHttpError(error: unknown): error is HttpError;
|
|
93
119
|
//#endregion
|
|
94
120
|
//#region src/core/create-http-client.d.ts
|
|
95
121
|
declare function createHttpClient(options: HttpClientOptions): HttpClient;
|
|
@@ -97,4 +123,4 @@ declare function createHttpClient(options: HttpClientOptions): HttpClient;
|
|
|
97
123
|
//#region src/transports/fetch-transport.d.ts
|
|
98
124
|
declare function createFetchTransport(fetchImpl?: typeof fetch): HttpTransport;
|
|
99
125
|
//#endregion
|
|
100
|
-
export { HTTP_TIMEOUT_ERROR_CODE, type HttpClient, type HttpClientOptions, type HttpErrorPayload, type HttpInterceptor, type HttpInvokeContext, HttpInvokeError, type HttpQueryParams, type HttpQueryPrimitive, type HttpQueryValue, type HttpRequestConfig, type HttpRequestInit, type HttpRequestOptions, HttpTimeoutError, type HttpTransport, type HttpTransportRequest, type HttpTransportResponse, createFetchTransport, createHttpClient };
|
|
126
|
+
export { HTTP_TIMEOUT_ERROR_CODE, HttpAbortError, type HttpAbortErrorOptions, type HttpClient, HttpClientError, type HttpClientErrorKind, type HttpClientErrorOptions, type HttpClientOptions, type HttpError, type HttpErrorPayload, type HttpErrorRequestMeta, type HttpInterceptor, type HttpInvokeContext, HttpInvokeError, type HttpQueryParams, type HttpQueryPrimitive, type HttpQueryValue, type HttpRequestConfig, type HttpRequestInit, type HttpRequestOptions, HttpTimeoutError, type HttpTransport, HttpTransportError, type HttpTransportRequest, type HttpTransportResponse, createFetchTransport, createHttpClient, isHttpError };
|
package/dist/http.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ type HttpRequestConfig = {
|
|
|
19
19
|
};
|
|
20
20
|
type HttpClientOptions = HttpRequestOptions & {
|
|
21
21
|
prefixUrl: string | URL;
|
|
22
|
+
allowAbsoluteUrls?: boolean;
|
|
22
23
|
fetchImpl?: typeof fetch;
|
|
23
24
|
transport?: HttpTransport;
|
|
24
25
|
interceptors?: HttpInterceptor[];
|
|
@@ -71,13 +72,38 @@ type HttpErrorRequestMeta = {
|
|
|
71
72
|
url?: string;
|
|
72
73
|
method?: string;
|
|
73
74
|
};
|
|
75
|
+
type HttpClientErrorOptions = {
|
|
76
|
+
cause?: unknown;
|
|
77
|
+
};
|
|
78
|
+
type HttpAbortErrorOptions = HttpClientErrorOptions & {
|
|
79
|
+
reason?: unknown;
|
|
80
|
+
};
|
|
81
|
+
type HttpClientErrorKind = "invoke" | "timeout" | "abort" | "transport" | "protocol";
|
|
74
82
|
declare const HTTP_TIMEOUT_ERROR_CODE: "HTTP_TIMEOUT";
|
|
75
|
-
declare
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
83
|
+
declare const HTTP_ABORT_ERROR_CODE: "HTTP_ABORTED";
|
|
84
|
+
declare const HTTP_TRANSPORT_ERROR_CODE: "HTTP_TRANSPORT_ERROR";
|
|
85
|
+
declare class HttpClientError<TKind extends HttpClientErrorKind = HttpClientErrorKind> extends Error {
|
|
86
|
+
readonly isHttpClientError: true;
|
|
87
|
+
readonly kind: TKind;
|
|
88
|
+
readonly url: string;
|
|
89
|
+
readonly method: string;
|
|
90
|
+
constructor(message: string, kind: TKind, requestMeta?: HttpErrorRequestMeta, options?: HttpClientErrorOptions);
|
|
91
|
+
}
|
|
92
|
+
declare class HttpTimeoutError extends HttpClientError<"timeout"> {
|
|
93
|
+
readonly code: typeof HTTP_TIMEOUT_ERROR_CODE;
|
|
94
|
+
readonly timeoutMs: number;
|
|
95
|
+
constructor(timeoutMs: number, requestMeta?: HttpErrorRequestMeta, options?: HttpClientErrorOptions);
|
|
96
|
+
}
|
|
97
|
+
declare class HttpAbortError extends HttpClientError<"abort"> {
|
|
98
|
+
readonly code: typeof HTTP_ABORT_ERROR_CODE;
|
|
99
|
+
readonly reason?: unknown;
|
|
100
|
+
constructor(requestMeta?: HttpErrorRequestMeta, options?: HttpAbortErrorOptions);
|
|
79
101
|
}
|
|
80
|
-
declare class
|
|
102
|
+
declare class HttpTransportError extends HttpClientError<"transport"> {
|
|
103
|
+
readonly code: typeof HTTP_TRANSPORT_ERROR_CODE;
|
|
104
|
+
constructor(requestMeta?: HttpErrorRequestMeta, options?: HttpClientErrorOptions);
|
|
105
|
+
}
|
|
106
|
+
declare class HttpInvokeError extends HttpClientError<"invoke"> {
|
|
81
107
|
type?: string;
|
|
82
108
|
code?: string;
|
|
83
109
|
reason?: string;
|
|
@@ -86,10 +112,10 @@ declare class HttpInvokeError extends Error {
|
|
|
86
112
|
statusText: string;
|
|
87
113
|
responseHeaders: Headers;
|
|
88
114
|
payload: unknown;
|
|
89
|
-
|
|
90
|
-
method: string;
|
|
91
|
-
constructor(response: Response, payload: unknown, requestMeta?: HttpErrorRequestMeta);
|
|
115
|
+
constructor(response: Response, payload: unknown, requestMeta?: HttpErrorRequestMeta, options?: HttpClientErrorOptions);
|
|
92
116
|
}
|
|
117
|
+
type HttpError = HttpAbortError | HttpInvokeError | HttpTimeoutError | HttpTransportError;
|
|
118
|
+
declare function isHttpError(error: unknown): error is HttpError;
|
|
93
119
|
//#endregion
|
|
94
120
|
//#region src/core/create-http-client.d.ts
|
|
95
121
|
declare function createHttpClient(options: HttpClientOptions): HttpClient;
|
|
@@ -97,4 +123,4 @@ declare function createHttpClient(options: HttpClientOptions): HttpClient;
|
|
|
97
123
|
//#region src/transports/fetch-transport.d.ts
|
|
98
124
|
declare function createFetchTransport(fetchImpl?: typeof fetch): HttpTransport;
|
|
99
125
|
//#endregion
|
|
100
|
-
export { HTTP_TIMEOUT_ERROR_CODE, type HttpClient, type HttpClientOptions, type HttpErrorPayload, type HttpInterceptor, type HttpInvokeContext, HttpInvokeError, type HttpQueryParams, type HttpQueryPrimitive, type HttpQueryValue, type HttpRequestConfig, type HttpRequestInit, type HttpRequestOptions, HttpTimeoutError, type HttpTransport, type HttpTransportRequest, type HttpTransportResponse, createFetchTransport, createHttpClient };
|
|
126
|
+
export { HTTP_TIMEOUT_ERROR_CODE, HttpAbortError, type HttpAbortErrorOptions, type HttpClient, HttpClientError, type HttpClientErrorKind, type HttpClientErrorOptions, type HttpClientOptions, type HttpError, type HttpErrorPayload, type HttpErrorRequestMeta, type HttpInterceptor, type HttpInvokeContext, HttpInvokeError, type HttpQueryParams, type HttpQueryPrimitive, type HttpQueryValue, type HttpRequestConfig, type HttpRequestInit, type HttpRequestOptions, HttpTimeoutError, type HttpTransport, HttpTransportError, type HttpTransportRequest, type HttpTransportResponse, createFetchTransport, createHttpClient, isHttpError };
|
package/dist/http.es.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as appendQueryToUrl, c as HTTP_TIMEOUT_ERROR_CODE, i as createFetchTransport, l as
|
|
1
|
+
import { a as appendQueryToUrl, c as HTTP_TIMEOUT_ERROR_CODE, d as HttpInvokeError, f as HttpTimeoutError, g as isHttpError, i as createFetchTransport, l as HttpAbortError, o as buildRequestUrl, p as HttpTransportError, r as createClientCore, s as mergeHeaders, t as splitHttpRequestOptions, u as HttpClientError } from "./request-options-Bjy2LvRE.js";
|
|
2
2
|
//#region src/core/create-http-client.ts
|
|
3
3
|
function resolveJsonBody(request) {
|
|
4
4
|
if (request.json !== void 0 && request.body !== void 0) throw new Error("Request json and body cannot be used together.");
|
|
@@ -6,7 +6,7 @@ function resolveJsonBody(request) {
|
|
|
6
6
|
return request.body;
|
|
7
7
|
}
|
|
8
8
|
function createHttpClient(options) {
|
|
9
|
-
const { prefixUrl, fetchImpl, transport, interceptors, ...baseOptions } = options;
|
|
9
|
+
const { prefixUrl, allowAbsoluteUrls = false, fetchImpl, transport, interceptors, ...baseOptions } = options;
|
|
10
10
|
const core = createClientCore({
|
|
11
11
|
prefixUrl,
|
|
12
12
|
baseOptions,
|
|
@@ -17,7 +17,7 @@ function createHttpClient(options) {
|
|
|
17
17
|
prepareRequest: (request, helpers) => {
|
|
18
18
|
const requestOptions = helpers.splitRequestOptions(request.options);
|
|
19
19
|
const method = String(request.method || "GET").toUpperCase();
|
|
20
|
-
const url = appendQueryToUrl(buildRequestUrl(helpers.prefixUrl, request.path), request.query);
|
|
20
|
+
const url = appendQueryToUrl(buildRequestUrl(helpers.prefixUrl, request.path, { allowAbsoluteUrls }), request.query);
|
|
21
21
|
const finalHeaders = mergeHeaders(helpers.baseOptions.headers, requestOptions.headers);
|
|
22
22
|
const body = resolveJsonBody(request);
|
|
23
23
|
if (request.json !== void 0 && !finalHeaders.has("content-type")) finalHeaders.set("content-type", "application/json");
|
|
@@ -61,4 +61,4 @@ function createHttpClient(options) {
|
|
|
61
61
|
};
|
|
62
62
|
}
|
|
63
63
|
//#endregion
|
|
64
|
-
export { HTTP_TIMEOUT_ERROR_CODE, HttpInvokeError, HttpTimeoutError, createFetchTransport, createHttpClient };
|
|
64
|
+
export { HTTP_TIMEOUT_ERROR_CODE, HttpAbortError, HttpClientError, HttpInvokeError, HttpTimeoutError, HttpTransportError, createFetchTransport, createHttpClient, isHttpError };
|
|
@@ -1,15 +1,68 @@
|
|
|
1
1
|
//#region src/contracts/errors.ts
|
|
2
2
|
var HTTP_TIMEOUT_ERROR_CODE = "HTTP_TIMEOUT";
|
|
3
|
-
var
|
|
3
|
+
var HTTP_ABORT_ERROR_CODE = "HTTP_ABORTED";
|
|
4
|
+
var HTTP_TRANSPORT_ERROR_CODE = "HTTP_TRANSPORT_ERROR";
|
|
5
|
+
var VRPC_PROTOCOL_ERROR_CODE = "VRPC_PROTOCOL_ERROR";
|
|
6
|
+
var HttpClientError = class extends Error {
|
|
7
|
+
isHttpClientError = true;
|
|
8
|
+
kind;
|
|
9
|
+
url;
|
|
10
|
+
method;
|
|
11
|
+
constructor(message, kind, requestMeta = {}, options = {}) {
|
|
12
|
+
super(message, options.cause === void 0 ? void 0 : { cause: options.cause });
|
|
13
|
+
this.name = "HttpClientError";
|
|
14
|
+
this.kind = kind;
|
|
15
|
+
this.url = requestMeta.url || "";
|
|
16
|
+
this.method = requestMeta.method || "GET";
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var HttpTimeoutError = class extends HttpClientError {
|
|
4
20
|
code;
|
|
5
21
|
timeoutMs;
|
|
6
|
-
constructor(timeoutMs) {
|
|
7
|
-
super(`request timeout after ${timeoutMs}ms
|
|
22
|
+
constructor(timeoutMs, requestMeta = {}, options = {}) {
|
|
23
|
+
super(`request timeout after ${timeoutMs}ms`, "timeout", requestMeta, options);
|
|
8
24
|
this.name = "HttpTimeoutError";
|
|
9
25
|
this.code = HTTP_TIMEOUT_ERROR_CODE;
|
|
10
26
|
this.timeoutMs = timeoutMs;
|
|
11
27
|
}
|
|
12
28
|
};
|
|
29
|
+
var HttpAbortError = class extends HttpClientError {
|
|
30
|
+
code;
|
|
31
|
+
reason;
|
|
32
|
+
constructor(requestMeta = {}, options = {}) {
|
|
33
|
+
super("request aborted", "abort", requestMeta, options);
|
|
34
|
+
this.name = "HttpAbortError";
|
|
35
|
+
this.code = HTTP_ABORT_ERROR_CODE;
|
|
36
|
+
this.reason = options.reason;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
var HttpTransportError = class extends HttpClientError {
|
|
40
|
+
code;
|
|
41
|
+
constructor(requestMeta = {}, options = {}) {
|
|
42
|
+
super("network request failed", "transport", requestMeta, options);
|
|
43
|
+
this.name = "HttpTransportError";
|
|
44
|
+
this.code = HTTP_TRANSPORT_ERROR_CODE;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
var VrpcProtocolError = class extends HttpClientError {
|
|
48
|
+
code;
|
|
49
|
+
status;
|
|
50
|
+
statusText;
|
|
51
|
+
responseHeaders;
|
|
52
|
+
vrpcStatus;
|
|
53
|
+
constructor(response, message, requestMeta = {}, options = {}) {
|
|
54
|
+
super(message, "protocol", {
|
|
55
|
+
url: requestMeta.url || response.url || "",
|
|
56
|
+
method: requestMeta.method || "POST"
|
|
57
|
+
}, options);
|
|
58
|
+
this.name = "VrpcProtocolError";
|
|
59
|
+
this.code = VRPC_PROTOCOL_ERROR_CODE;
|
|
60
|
+
this.status = response.status;
|
|
61
|
+
this.statusText = response.statusText;
|
|
62
|
+
this.responseHeaders = response.headers;
|
|
63
|
+
this.vrpcStatus = response.headers.get("vrpc-status");
|
|
64
|
+
}
|
|
65
|
+
};
|
|
13
66
|
function readServerErrorMessage(payload) {
|
|
14
67
|
if (!payload || typeof payload !== "object") return;
|
|
15
68
|
const body = payload;
|
|
@@ -22,7 +75,7 @@ function readServerErrorStringField(payload, field) {
|
|
|
22
75
|
const value = payload[field];
|
|
23
76
|
return typeof value === "string" ? value : void 0;
|
|
24
77
|
}
|
|
25
|
-
var HttpInvokeError = class extends
|
|
78
|
+
var HttpInvokeError = class extends HttpClientError {
|
|
26
79
|
type;
|
|
27
80
|
code;
|
|
28
81
|
reason;
|
|
@@ -31,11 +84,12 @@ var HttpInvokeError = class extends Error {
|
|
|
31
84
|
statusText;
|
|
32
85
|
responseHeaders;
|
|
33
86
|
payload;
|
|
34
|
-
|
|
35
|
-
method;
|
|
36
|
-
constructor(response, payload, requestMeta = {}) {
|
|
87
|
+
constructor(response, payload, requestMeta = {}, options = {}) {
|
|
37
88
|
const message = readServerErrorMessage(payload) || `request failed (status=${response.status})`;
|
|
38
|
-
super(message
|
|
89
|
+
super(message, "invoke", {
|
|
90
|
+
url: requestMeta.url || response.url || "",
|
|
91
|
+
method: requestMeta.method || "GET"
|
|
92
|
+
}, options);
|
|
39
93
|
this.name = "HttpInvokeError";
|
|
40
94
|
this.status = response.status;
|
|
41
95
|
this.statusText = response.statusText;
|
|
@@ -45,19 +99,47 @@ var HttpInvokeError = class extends Error {
|
|
|
45
99
|
this.code = readServerErrorStringField(payload, "code");
|
|
46
100
|
this.reason = readServerErrorStringField(payload, "reason");
|
|
47
101
|
this.detail = readServerErrorStringField(payload, "detail");
|
|
48
|
-
this.url = requestMeta.url || response.url || "";
|
|
49
|
-
this.method = requestMeta.method || "GET";
|
|
50
102
|
}
|
|
51
103
|
};
|
|
52
104
|
var VrpcInvokeError = class extends HttpInvokeError {
|
|
53
105
|
vrpcStatus;
|
|
54
|
-
constructor(response, payload, requestMeta = {}) {
|
|
55
|
-
super(response, payload, requestMeta);
|
|
106
|
+
constructor(response, payload, requestMeta = {}, options = {}) {
|
|
107
|
+
super(response, payload, requestMeta, options);
|
|
56
108
|
this.name = "VrpcInvokeError";
|
|
57
|
-
|
|
58
|
-
this.vrpcStatus = vrpcStatus;
|
|
109
|
+
this.vrpcStatus = response.headers.get("vrpc-status");
|
|
59
110
|
}
|
|
60
111
|
};
|
|
112
|
+
function hasHttpClientErrorBrand(error) {
|
|
113
|
+
return Boolean(error && typeof error === "object" && error.isHttpClientError === true);
|
|
114
|
+
}
|
|
115
|
+
function isHttpError(error) {
|
|
116
|
+
if (!hasHttpClientErrorBrand(error)) return false;
|
|
117
|
+
const candidate = error;
|
|
118
|
+
switch (candidate.kind) {
|
|
119
|
+
case "abort": return candidate.code === HTTP_ABORT_ERROR_CODE;
|
|
120
|
+
case "timeout": return candidate.code === "HTTP_TIMEOUT" && typeof candidate.timeoutMs === "number";
|
|
121
|
+
case "transport": return candidate.code === HTTP_TRANSPORT_ERROR_CODE;
|
|
122
|
+
case "invoke": return typeof candidate.status === "number" && "payload" in candidate;
|
|
123
|
+
default: return false;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function isVrpcError(error) {
|
|
127
|
+
if (!hasHttpClientErrorBrand(error)) return false;
|
|
128
|
+
const candidate = error;
|
|
129
|
+
switch (candidate.kind) {
|
|
130
|
+
case "abort": return candidate.code === HTTP_ABORT_ERROR_CODE;
|
|
131
|
+
case "timeout": return candidate.code === "HTTP_TIMEOUT" && typeof candidate.timeoutMs === "number";
|
|
132
|
+
case "transport": return candidate.code === HTTP_TRANSPORT_ERROR_CODE;
|
|
133
|
+
case "protocol": return candidate.code === "VRPC_PROTOCOL_ERROR" && typeof candidate.status === "number";
|
|
134
|
+
case "invoke": return typeof candidate.status === "number" && "vrpcStatus" in candidate;
|
|
135
|
+
default: return false;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function isAbortErrorLike(error) {
|
|
139
|
+
if (!error || typeof error !== "object" || error instanceof HttpTimeoutError) return false;
|
|
140
|
+
const candidate = error;
|
|
141
|
+
return candidate.kind === "abort" || candidate.name === "HttpAbortError" || candidate.name === "AbortError" || candidate.code === "HTTP_ABORTED" || candidate.code === "ABORT_ERR" || candidate.code === "ERR_CANCELED" || candidate.__CANCEL__ === true;
|
|
142
|
+
}
|
|
61
143
|
//#endregion
|
|
62
144
|
//#region src/core/http-utils.ts
|
|
63
145
|
var NULL_BODY_STATUS_CODES = /* @__PURE__ */ new Set([
|
|
@@ -73,10 +155,18 @@ function isNullBodyStatus(status) {
|
|
|
73
155
|
function shouldTreatResponseAsBodyless(method, status) {
|
|
74
156
|
return method.toUpperCase() === "HEAD" || isNullBodyStatus(status);
|
|
75
157
|
}
|
|
76
|
-
|
|
158
|
+
var ALLOWED_ABSOLUTE_URL_PROTOCOLS = /* @__PURE__ */ new Set(["http:", "https:"]);
|
|
159
|
+
function buildRequestUrl(prefixUrl, path, { allowAbsoluteUrls = false } = {}) {
|
|
160
|
+
if (/^\s*\/\//.test(path)) throw new Error("Protocol-relative request URLs are not supported.");
|
|
161
|
+
let absoluteUrl;
|
|
77
162
|
try {
|
|
78
|
-
|
|
163
|
+
absoluteUrl = new URL(path);
|
|
79
164
|
} catch {}
|
|
165
|
+
if (absoluteUrl) {
|
|
166
|
+
if (!ALLOWED_ABSOLUTE_URL_PROTOCOLS.has(absoluteUrl.protocol)) throw new Error(`Unsupported absolute request URL protocol: ${absoluteUrl.protocol || "unknown"}`);
|
|
167
|
+
if (!allowAbsoluteUrls) throw new Error("Absolute request URLs are disabled. Set allowAbsoluteUrls: true on createHttpClient() to enable them.");
|
|
168
|
+
return absoluteUrl.toString();
|
|
169
|
+
}
|
|
80
170
|
const base = String(prefixUrl).replace(/\/+$/, "");
|
|
81
171
|
const relativePath = path.replace(/^\/+/, "");
|
|
82
172
|
return relativePath ? `${base}/${relativePath}` : `${base}/`;
|
|
@@ -124,43 +214,56 @@ async function parseResponsePayload(response) {
|
|
|
124
214
|
}
|
|
125
215
|
//#endregion
|
|
126
216
|
//#region src/transports/fetch-transport.ts
|
|
127
|
-
function
|
|
128
|
-
return new HttpTimeoutError(timeoutMs);
|
|
129
|
-
}
|
|
130
|
-
function withTimeoutSignal(timeoutMs, signal) {
|
|
217
|
+
function withTimeoutSignal(timeoutMs, callerSignal, requestMeta) {
|
|
131
218
|
if (!timeoutMs || timeoutMs <= 0) return {
|
|
132
|
-
signal:
|
|
133
|
-
cleanup: () => {}
|
|
219
|
+
signal: callerSignal || void 0,
|
|
220
|
+
cleanup: () => {},
|
|
221
|
+
getAbortSource: () => callerSignal?.aborted ? "caller" : void 0,
|
|
222
|
+
getTimeoutError: () => void 0
|
|
134
223
|
};
|
|
135
224
|
const controller = new AbortController();
|
|
136
|
-
|
|
225
|
+
let abortSource;
|
|
226
|
+
let timeoutError;
|
|
227
|
+
const timer = setTimeout(() => {
|
|
228
|
+
if (controller.signal.aborted) return;
|
|
229
|
+
abortSource = "timeout";
|
|
230
|
+
timeoutError = new HttpTimeoutError(timeoutMs, requestMeta);
|
|
231
|
+
controller.abort(timeoutError);
|
|
232
|
+
}, timeoutMs);
|
|
137
233
|
const clear = () => clearTimeout(timer);
|
|
138
|
-
if (!
|
|
234
|
+
if (!callerSignal) return {
|
|
139
235
|
signal: controller.signal,
|
|
140
|
-
cleanup: clear
|
|
236
|
+
cleanup: clear,
|
|
237
|
+
getAbortSource: () => abortSource,
|
|
238
|
+
getTimeoutError: () => timeoutError
|
|
141
239
|
};
|
|
142
|
-
|
|
143
|
-
controller.
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
149
|
-
const relayAbort = () => controller.abort(signal.reason);
|
|
150
|
-
signal.addEventListener("abort", relayAbort, { once: true });
|
|
240
|
+
const relayAbort = () => {
|
|
241
|
+
if (controller.signal.aborted) return;
|
|
242
|
+
abortSource = "caller";
|
|
243
|
+
controller.abort(callerSignal.reason);
|
|
244
|
+
};
|
|
245
|
+
if (callerSignal.aborted) relayAbort();
|
|
246
|
+
else callerSignal.addEventListener("abort", relayAbort, { once: true });
|
|
151
247
|
return {
|
|
152
248
|
signal: controller.signal,
|
|
153
249
|
cleanup: () => {
|
|
154
250
|
clear();
|
|
155
|
-
|
|
156
|
-
}
|
|
251
|
+
callerSignal.removeEventListener("abort", relayAbort);
|
|
252
|
+
},
|
|
253
|
+
getAbortSource: () => abortSource,
|
|
254
|
+
getTimeoutError: () => timeoutError
|
|
157
255
|
};
|
|
158
256
|
}
|
|
159
257
|
function createFetchTransport(fetchImpl) {
|
|
160
258
|
const request = fetchImpl || globalThis.fetch;
|
|
161
259
|
if (!request) throw new Error("fetch is not available in current runtime.");
|
|
162
260
|
return { request: async (input) => {
|
|
163
|
-
const
|
|
261
|
+
const requestMeta = {
|
|
262
|
+
url: input.url,
|
|
263
|
+
method: input.method
|
|
264
|
+
};
|
|
265
|
+
const callerSignal = input.init.signal || input.signal;
|
|
266
|
+
const { signal, cleanup, getAbortSource, getTimeoutError } = withTimeoutSignal(input.timeoutMs, callerSignal, requestMeta);
|
|
164
267
|
try {
|
|
165
268
|
const response = await request(input.url, {
|
|
166
269
|
...input.init,
|
|
@@ -178,6 +281,20 @@ function createFetchTransport(fetchImpl) {
|
|
|
178
281
|
body,
|
|
179
282
|
url: response.url || input.url
|
|
180
283
|
};
|
|
284
|
+
} catch (cause) {
|
|
285
|
+
const abortSource = getAbortSource();
|
|
286
|
+
if (abortSource === "timeout") {
|
|
287
|
+
const timeoutError = getTimeoutError();
|
|
288
|
+
if (cause === timeoutError && timeoutError) throw timeoutError;
|
|
289
|
+
throw new HttpTimeoutError(input.timeoutMs, requestMeta, { cause });
|
|
290
|
+
}
|
|
291
|
+
if (abortSource === "caller") throw new HttpAbortError(requestMeta, {
|
|
292
|
+
cause,
|
|
293
|
+
reason: callerSignal?.reason
|
|
294
|
+
});
|
|
295
|
+
if (isHttpError(cause)) throw cause;
|
|
296
|
+
if (isAbortErrorLike(cause)) throw new HttpAbortError(requestMeta, { cause });
|
|
297
|
+
throw new HttpTransportError(requestMeta, { cause });
|
|
181
298
|
} finally {
|
|
182
299
|
cleanup();
|
|
183
300
|
}
|
|
@@ -238,15 +355,26 @@ function createClientCore(options) {
|
|
|
238
355
|
url: context.url
|
|
239
356
|
};
|
|
240
357
|
await validateRequest?.(context, requestMeta);
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
358
|
+
let result;
|
|
359
|
+
try {
|
|
360
|
+
result = await httpTransport.request({
|
|
361
|
+
url: context.url,
|
|
362
|
+
init: context.init,
|
|
363
|
+
method,
|
|
364
|
+
headers,
|
|
365
|
+
body: resolveTransportBody(context.init, prepared.fallbackBody),
|
|
366
|
+
timeoutMs: prepared.timeoutMs,
|
|
367
|
+
signal: context.init.signal
|
|
368
|
+
});
|
|
369
|
+
} catch (cause) {
|
|
370
|
+
if (isHttpError(cause) || isVrpcError(cause)) throw cause;
|
|
371
|
+
if (context.init.signal?.aborted || isAbortErrorLike(cause)) throw new HttpAbortError(requestMeta, {
|
|
372
|
+
cause,
|
|
373
|
+
reason: context.init.signal?.reason
|
|
374
|
+
});
|
|
375
|
+
throw new HttpTransportError(requestMeta, { cause });
|
|
376
|
+
}
|
|
377
|
+
const response = buildResponseFromTransportResult(result);
|
|
250
378
|
const payload = await parseResponse(response, context);
|
|
251
379
|
await runAfterResponseInterceptors(interceptors, response, context);
|
|
252
380
|
await assertResponse(response, payload, context, requestMeta);
|
|
@@ -285,4 +413,4 @@ function splitVrpcRequestOptions(options) {
|
|
|
285
413
|
};
|
|
286
414
|
}
|
|
287
415
|
//#endregion
|
|
288
|
-
export { appendQueryToUrl as a, HTTP_TIMEOUT_ERROR_CODE as c,
|
|
416
|
+
export { isVrpcError as _, appendQueryToUrl as a, HTTP_TIMEOUT_ERROR_CODE as c, HttpInvokeError as d, HttpTimeoutError as f, isHttpError as g, VrpcProtocolError as h, createFetchTransport as i, HttpAbortError as l, VrpcInvokeError as m, splitVrpcRequestOptions as n, buildRequestUrl as o, HttpTransportError as p, createClientCore as r, mergeHeaders as s, splitHttpRequestOptions as t, HttpClientError as u };
|