@yorun-ai/vrpc 0.9.1 → 0.9.3

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/dist/client.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[];
@@ -139,7 +140,7 @@ interface VrpcRequestOptions extends HttpRequestOptions {
139
140
  trace?: VrpcTrace;
140
141
  wire?: VrpcMethodWireSpec;
141
142
  }
142
- type VrpcClientOptions = Omit<HttpClientOptions, "interceptors" | "transport"> & {
143
+ type VrpcClientOptions = Omit<HttpClientOptions, "allowAbsoluteUrls" | "interceptors" | "transport"> & {
143
144
  clientInfo: VrpcClientInfo;
144
145
  traceMode?: VrpcTraceMode;
145
146
  cborCodec?: VrpcCborCodec;
@@ -184,13 +185,47 @@ type HttpErrorRequestMeta = {
184
185
  url?: string;
185
186
  method?: string;
186
187
  };
188
+ type HttpClientErrorOptions = {
189
+ cause?: unknown;
190
+ };
191
+ type HttpAbortErrorOptions = HttpClientErrorOptions & {
192
+ reason?: unknown;
193
+ };
194
+ type HttpClientErrorKind = "invoke" | "timeout" | "abort" | "transport" | "protocol";
187
195
  declare const HTTP_TIMEOUT_ERROR_CODE: "HTTP_TIMEOUT";
188
- declare class HttpTimeoutError extends Error {
189
- code: typeof HTTP_TIMEOUT_ERROR_CODE;
190
- timeoutMs: number;
191
- constructor(timeoutMs: number);
196
+ declare const HTTP_ABORT_ERROR_CODE: "HTTP_ABORTED";
197
+ declare const HTTP_TRANSPORT_ERROR_CODE: "HTTP_TRANSPORT_ERROR";
198
+ declare const VRPC_PROTOCOL_ERROR_CODE: "VRPC_PROTOCOL_ERROR";
199
+ declare class HttpClientError<TKind extends HttpClientErrorKind = HttpClientErrorKind> extends Error {
200
+ readonly isHttpClientError: true;
201
+ readonly kind: TKind;
202
+ readonly url: string;
203
+ readonly method: string;
204
+ constructor(message: string, kind: TKind, requestMeta?: HttpErrorRequestMeta, options?: HttpClientErrorOptions);
205
+ }
206
+ declare class HttpTimeoutError extends HttpClientError<"timeout"> {
207
+ readonly code: typeof HTTP_TIMEOUT_ERROR_CODE;
208
+ readonly timeoutMs: number;
209
+ constructor(timeoutMs: number, requestMeta?: HttpErrorRequestMeta, options?: HttpClientErrorOptions);
210
+ }
211
+ declare class HttpAbortError extends HttpClientError<"abort"> {
212
+ readonly code: typeof HTTP_ABORT_ERROR_CODE;
213
+ readonly reason?: unknown;
214
+ constructor(requestMeta?: HttpErrorRequestMeta, options?: HttpAbortErrorOptions);
192
215
  }
193
- declare class HttpInvokeError extends Error {
216
+ declare class HttpTransportError extends HttpClientError<"transport"> {
217
+ readonly code: typeof HTTP_TRANSPORT_ERROR_CODE;
218
+ constructor(requestMeta?: HttpErrorRequestMeta, options?: HttpClientErrorOptions);
219
+ }
220
+ declare class VrpcProtocolError extends HttpClientError<"protocol"> {
221
+ readonly code: typeof VRPC_PROTOCOL_ERROR_CODE;
222
+ readonly status: number;
223
+ readonly statusText: string;
224
+ readonly responseHeaders: Headers;
225
+ readonly vrpcStatus: string | null;
226
+ constructor(response: Response, message: string, requestMeta?: HttpErrorRequestMeta, options?: HttpClientErrorOptions);
227
+ }
228
+ declare class HttpInvokeError extends HttpClientError<"invoke"> {
194
229
  type?: string;
195
230
  code?: string;
196
231
  reason?: string;
@@ -199,15 +234,16 @@ declare class HttpInvokeError extends Error {
199
234
  statusText: string;
200
235
  responseHeaders: Headers;
201
236
  payload: unknown;
202
- url: string;
203
- method: string;
204
- constructor(response: Response, payload: unknown, requestMeta?: HttpErrorRequestMeta);
237
+ constructor(response: Response, payload: unknown, requestMeta?: HttpErrorRequestMeta, options?: HttpClientErrorOptions);
205
238
  }
206
239
  type VrpcErrorPayload = HttpErrorPayload;
207
240
  declare class VrpcInvokeError extends HttpInvokeError {
208
241
  vrpcStatus: string | null;
209
- constructor(response: Response, payload: unknown, requestMeta?: HttpErrorRequestMeta);
242
+ constructor(response: Response, payload: unknown, requestMeta?: HttpErrorRequestMeta, options?: HttpClientErrorOptions);
210
243
  }
244
+ type HttpError = HttpAbortError | HttpInvokeError | HttpTimeoutError | HttpTransportError;
245
+ type VrpcError = HttpAbortError | HttpTimeoutError | HttpTransportError | VrpcInvokeError | VrpcProtocolError;
246
+ declare function isVrpcError(error: unknown): error is VrpcError;
211
247
  //#endregion
212
248
  //#region src/protocol/id.d.ts
213
249
  declare const DEFAULT_CLIENT_INSTANCE_ID_STORAGE_KEY = "vrpc-client-instance-id";
@@ -244,4 +280,4 @@ declare function createVrpcClient(options: VrpcClientOptions): VrpcClient;
244
280
  //#region src/transports/fetch-transport.d.ts
245
281
  declare function createFetchTransport(fetchImpl?: typeof fetch): HttpTransport;
246
282
  //#endregion
247
- export { type BuildVrpcHeadersOptions, DEFAULT_CLIENT_INSTANCE_ID_STORAGE_KEY, type GetClientInstanceIdOptions, 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, type ParsedVrpcErrorResponse, type ParsedVrpcResponse, type ParsedVrpcSuccessResponse, VRPC_CBOR_CONTENT_TYPE, VRPC_JSON_CONTENT_TYPE, type VrpcCborCodec, type VrpcClient, type VrpcClientInfo, type VrpcClientOptions, type VrpcErrorPayload, type VrpcInterceptor, type VrpcInterceptors, type VrpcInvokeContext, VrpcInvokeError, type VrpcInvokeRequest, type VrpcMethodWireSpec, type VrpcRequestInit, type VrpcRequestOptions, type VrpcResponseLike, type VrpcTrace, type VrpcTraceMode, type VrpcTransport, type VrpcTransportRequest, type VrpcTransportResponse, type VrpcWireSchema, buildVrpcHeaders, buildVrpcPath, buildVrpcRequestBody, createFetchTransport, createVrpcClient, decodeVrpcResponsePayload, encodeVrpcRequestBody, generateClientInstanceId, generateVrpcId, generateVrpcSpan, getClientInstanceId, parseVrpcResponse, unwrapVrpcResponse };
283
+ export { type BuildVrpcHeadersOptions, DEFAULT_CLIENT_INSTANCE_ID_STORAGE_KEY, type GetClientInstanceIdOptions, 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, type ParsedVrpcErrorResponse, type ParsedVrpcResponse, type ParsedVrpcSuccessResponse, VRPC_CBOR_CONTENT_TYPE, VRPC_JSON_CONTENT_TYPE, type VrpcCborCodec, type VrpcClient, type VrpcClientInfo, type VrpcClientOptions, type VrpcError, type VrpcErrorPayload, type VrpcInterceptor, type VrpcInterceptors, type VrpcInvokeContext, VrpcInvokeError, type VrpcInvokeRequest, type VrpcMethodWireSpec, VrpcProtocolError, type VrpcRequestInit, type VrpcRequestOptions, type VrpcResponseLike, type VrpcTrace, type VrpcTraceMode, type VrpcTransport, type VrpcTransportRequest, type VrpcTransportResponse, type VrpcWireSchema, buildVrpcHeaders, buildVrpcPath, buildVrpcRequestBody, createFetchTransport, createVrpcClient, decodeVrpcResponsePayload, encodeVrpcRequestBody, generateClientInstanceId, generateVrpcId, generateVrpcSpan, getClientInstanceId, isVrpcError, parseVrpcResponse, unwrapVrpcResponse };
package/dist/client.es.js CHANGED
@@ -1,4 +1,4 @@
1
- import { c as HTTP_TIMEOUT_ERROR_CODE, d as VrpcInvokeError, i as createFetchTransport, l as HttpInvokeError, n as splitVrpcRequestOptions, o as buildRequestUrl, r as createClientCore, s as mergeHeaders, u as HttpTimeoutError } from "./request-options-disym648.js";
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(headers, name) {
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,34 @@ 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
- requireResponseHeader(response.headers, "vrpc-status");
237
- assertValidVrpcServerHeader(response.headers.get("vrpc-server"));
238
- const contentType = mediaTypeOf(requireResponseHeader(response.headers, "content-type"));
239
- if (contentType === "application/vrpc+json") {
240
- const text = await response.text();
241
- if (!text) throw new Error("Invalid vRPC response: empty response body.");
242
- try {
243
- return JSON.parse(text);
244
- } catch {
245
- throw new Error("Invalid vRPC response: JSON body cannot be parsed.");
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
+ const message = cause instanceof Error ? cause.message : "Invalid vRPC response.";
262
+ throw new VrpcProtocolError(response, message, {}, { cause });
247
263
  }
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
264
  }
259
265
  function normalizeResponseHeaders(headers) {
260
266
  if (!headers) return new Headers();
@@ -416,7 +422,18 @@ function createVrpcClient(options) {
416
422
  }
417
423
  };
418
424
  },
419
- parseResponse: (response, context) => decodeVrpcResponsePayload(response, cborCodec, context.options.wire?.result),
425
+ parseResponse: async (response, context) => {
426
+ try {
427
+ return await decodeVrpcResponsePayload(response, cborCodec, context.options.wire?.result);
428
+ } catch (cause) {
429
+ const vrpcStatus = response.headers.get("vrpc-status");
430
+ if (vrpcStatus && (!response.ok || vrpcStatus !== "OK")) throw new VrpcInvokeError(response, null, {
431
+ url: context.url,
432
+ method: String(context.init.method || "POST").toUpperCase()
433
+ }, { cause });
434
+ throw cause;
435
+ }
436
+ },
420
437
  validateRequest: (_context, requestMeta) => {
421
438
  if (requestMeta.method !== "POST") throw new Error(`Unsupported method: ${requestMeta.method}, only POST is allowed.`);
422
439
  },
@@ -439,4 +456,4 @@ function createVrpcClient(options) {
439
456
  };
440
457
  }
441
458
  //#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 };
459
+ 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-2HZzkj4w.cjs");
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 class HttpTimeoutError extends Error {
76
- code: typeof HTTP_TIMEOUT_ERROR_CODE;
77
- timeoutMs: number;
78
- constructor(timeoutMs: number);
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 HttpInvokeError extends Error {
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
- url: string;
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 class HttpTimeoutError extends Error {
76
- code: typeof HTTP_TIMEOUT_ERROR_CODE;
77
- timeoutMs: number;
78
- constructor(timeoutMs: number);
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 HttpInvokeError extends Error {
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
- url: string;
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 HttpInvokeError, o as buildRequestUrl, r as createClientCore, s as mergeHeaders, t as splitHttpRequestOptions, u as HttpTimeoutError } from "./request-options-disym648.js";
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 };