@zimic/fetch 0.1.0-canary.2 → 0.1.0-canary.21
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/LICENSE.md +1 -1
- package/README.md +110 -180
- package/dist/index.d.ts +871 -44
- package/dist/index.js +77 -42
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +78 -43
- package/dist/index.mjs.map +1 -1
- package/package.json +14 -13
- package/src/client/FetchClient.ts +113 -66
- package/src/client/errors/FetchResponseError.ts +47 -6
- package/src/client/factory.ts +56 -5
- package/src/client/types/json.ts +7 -0
- package/src/client/types/public.ts +587 -51
- package/src/client/types/requests.ts +257 -49
- package/src/index.ts +5 -9
- package/src/types/requests.ts +1 -2
- package/src/utils/environment.ts +3 -0
- package/src/utils/files.ts +4 -19
- package/src/types/strings.d.ts +0 -9
- package/src/types/utils.ts +0 -11
- package/src/utils/imports.ts +0 -14
- package/src/utils/urls.ts +0 -43
package/dist/index.js
CHANGED
|
@@ -22,26 +22,34 @@ var FetchResponseError = class extends Error {
|
|
|
22
22
|
};
|
|
23
23
|
var FetchResponseError_default = FetchResponseError;
|
|
24
24
|
|
|
25
|
-
//
|
|
26
|
-
|
|
25
|
+
// ../zimic-utils/dist/chunk-PAWJFY3S.mjs
|
|
26
|
+
var __defProp2 = Object.defineProperty;
|
|
27
|
+
var __name2 = /* @__PURE__ */ __name((target, value) => __defProp2(target, "name", { value, configurable: true }), "__name");
|
|
28
|
+
|
|
29
|
+
// ../zimic-utils/dist/chunk-RIVHLEFF.mjs
|
|
30
|
+
var URL_PATH_PARAM_REGEX = /\/:([^/]+)/g;
|
|
31
|
+
function createRegExpFromURL(url) {
|
|
32
|
+
URL_PATH_PARAM_REGEX.lastIndex = 0;
|
|
33
|
+
const urlWithReplacedPathParams = encodeURI(url).replace(/([.()*?+$\\])/g, "\\$1").replace(URL_PATH_PARAM_REGEX, "/(?<$1>[^/]+)").replace(/^(\/)|(\/)$/g, "");
|
|
34
|
+
return new RegExp(`^(?:/)?${urlWithReplacedPathParams}(?:/)?$`);
|
|
35
|
+
}
|
|
36
|
+
__name(createRegExpFromURL, "createRegExpFromURL");
|
|
37
|
+
__name2(createRegExpFromURL, "createRegExpFromURL");
|
|
38
|
+
var createRegExpFromURL_default = createRegExpFromURL;
|
|
39
|
+
|
|
40
|
+
// ../zimic-utils/dist/url/excludeURLParams.mjs
|
|
41
|
+
function excludeURLParams(url) {
|
|
27
42
|
url.hash = "";
|
|
28
43
|
url.search = "";
|
|
29
44
|
url.username = "";
|
|
30
45
|
url.password = "";
|
|
31
46
|
return url;
|
|
32
47
|
}
|
|
33
|
-
__name(
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
__name(prepareURLForRegex, "prepareURLForRegex");
|
|
39
|
-
var URL_PATH_PARAM_REGEX = /\/:([^/]+)/g;
|
|
40
|
-
function createRegexFromURL(url) {
|
|
41
|
-
const urlWithReplacedPathParams = prepareURLForRegex(url).replace(URL_PATH_PARAM_REGEX, "/(?<$1>[^/]+)").replace(/(\/+)$/, "(?:/+)?");
|
|
42
|
-
return new RegExp(`^${urlWithReplacedPathParams}$`);
|
|
43
|
-
}
|
|
44
|
-
__name(createRegexFromURL, "createRegexFromURL");
|
|
48
|
+
__name(excludeURLParams, "excludeURLParams");
|
|
49
|
+
__name2(excludeURLParams, "excludeURLParams");
|
|
50
|
+
var excludeURLParams_default = excludeURLParams;
|
|
51
|
+
|
|
52
|
+
// ../zimic-utils/dist/url/joinURL.mjs
|
|
45
53
|
function joinURL(...parts) {
|
|
46
54
|
return parts.map((part, index) => {
|
|
47
55
|
const isFirstPart = index === 0;
|
|
@@ -57,6 +65,8 @@ function joinURL(...parts) {
|
|
|
57
65
|
}).filter((part) => part.length > 0).join("/");
|
|
58
66
|
}
|
|
59
67
|
__name(joinURL, "joinURL");
|
|
68
|
+
__name2(joinURL, "joinURL");
|
|
69
|
+
var joinURL_default = joinURL;
|
|
60
70
|
|
|
61
71
|
// src/client/FetchClient.ts
|
|
62
72
|
var FetchClient = class {
|
|
@@ -66,8 +76,13 @@ var FetchClient = class {
|
|
|
66
76
|
fetch;
|
|
67
77
|
constructor({ onRequest, onResponse, ...defaults }) {
|
|
68
78
|
this.fetch = this.createFetchFunction();
|
|
69
|
-
this.fetch.defaults =
|
|
70
|
-
|
|
79
|
+
this.fetch.defaults = {
|
|
80
|
+
...defaults,
|
|
81
|
+
headers: defaults.headers ?? {},
|
|
82
|
+
searchParams: defaults.searchParams ?? {}
|
|
83
|
+
};
|
|
84
|
+
this.fetch.loose = this.fetch;
|
|
85
|
+
this.fetch.Request = this.createRequestClass(this.fetch.defaults);
|
|
71
86
|
this.fetch.onRequest = onRequest;
|
|
72
87
|
this.fetch.onResponse = onResponse;
|
|
73
88
|
}
|
|
@@ -90,8 +105,7 @@ var FetchClient = class {
|
|
|
90
105
|
if (this.fetch.onRequest) {
|
|
91
106
|
const requestAfterInterceptor = await this.fetch.onRequest(
|
|
92
107
|
// Optimize type checking by narrowing the type of request
|
|
93
|
-
request
|
|
94
|
-
this.fetch
|
|
108
|
+
request
|
|
95
109
|
);
|
|
96
110
|
if (requestAfterInterceptor !== request) {
|
|
97
111
|
const isFetchRequest = requestAfterInterceptor instanceof this.fetch.Request;
|
|
@@ -105,8 +119,7 @@ var FetchClient = class {
|
|
|
105
119
|
if (this.fetch.onResponse) {
|
|
106
120
|
const responseAfterInterceptor = await this.fetch.onResponse(
|
|
107
121
|
// Optimize type checking by narrowing the type of response
|
|
108
|
-
response
|
|
109
|
-
this.fetch
|
|
122
|
+
response
|
|
110
123
|
);
|
|
111
124
|
const isFetchResponse = responseAfterInterceptor instanceof Response && "request" in responseAfterInterceptor && responseAfterInterceptor.request instanceof this.fetch.Request;
|
|
112
125
|
response = isFetchResponse ? responseAfterInterceptor : this.defineFetchResponseProperties(fetchRequest, responseAfterInterceptor);
|
|
@@ -121,10 +134,17 @@ var FetchClient = class {
|
|
|
121
134
|
enumerable: true,
|
|
122
135
|
configurable: false
|
|
123
136
|
});
|
|
124
|
-
|
|
137
|
+
let responseError;
|
|
125
138
|
Object.defineProperty(fetchResponse, "error", {
|
|
126
|
-
|
|
127
|
-
|
|
139
|
+
get() {
|
|
140
|
+
if (responseError === void 0) {
|
|
141
|
+
responseError = fetchResponse.ok ? null : new FetchResponseError_default(
|
|
142
|
+
fetchRequest,
|
|
143
|
+
fetchResponse
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
return responseError;
|
|
147
|
+
},
|
|
128
148
|
enumerable: true,
|
|
129
149
|
configurable: false
|
|
130
150
|
});
|
|
@@ -136,24 +156,39 @@ var FetchClient = class {
|
|
|
136
156
|
__name(this, "Request");
|
|
137
157
|
}
|
|
138
158
|
path;
|
|
139
|
-
constructor(input,
|
|
140
|
-
const
|
|
159
|
+
constructor(input, init) {
|
|
160
|
+
const initWithDefaults = { ...defaults, ...init };
|
|
161
|
+
const headersFromDefaults = new http.HttpHeaders(defaults.headers);
|
|
162
|
+
const headersFromInit = new http.HttpHeaders(init.headers);
|
|
141
163
|
let url;
|
|
164
|
+
const baseURL = new URL(initWithDefaults.baseURL);
|
|
142
165
|
if (input instanceof globalThis.Request) {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
166
|
+
const request = input;
|
|
167
|
+
const headersFromRequest = new http.HttpHeaders(input.headers);
|
|
168
|
+
initWithDefaults.headers = {
|
|
169
|
+
...headersFromDefaults.toObject(),
|
|
170
|
+
...headersFromRequest.toObject(),
|
|
171
|
+
...headersFromInit.toObject()
|
|
172
|
+
};
|
|
173
|
+
super(request, initWithDefaults);
|
|
148
174
|
url = new URL(input.url);
|
|
149
175
|
} else {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
|
|
176
|
+
initWithDefaults.headers = {
|
|
177
|
+
...headersFromDefaults.toObject(),
|
|
178
|
+
...headersFromInit.toObject()
|
|
179
|
+
};
|
|
180
|
+
url = input instanceof URL ? new URL(input) : new URL(joinURL_default(baseURL, input));
|
|
181
|
+
const searchParamsFromDefaults = new http.HttpSearchParams(defaults.searchParams);
|
|
182
|
+
const searchParamsFromInit = new http.HttpSearchParams(initWithDefaults.searchParams);
|
|
183
|
+
initWithDefaults.searchParams = {
|
|
184
|
+
...searchParamsFromDefaults.toObject(),
|
|
185
|
+
...searchParamsFromInit.toObject()
|
|
186
|
+
};
|
|
187
|
+
url.search = new http.HttpSearchParams(initWithDefaults.searchParams).toString();
|
|
188
|
+
super(url, initWithDefaults);
|
|
155
189
|
}
|
|
156
|
-
|
|
190
|
+
const baseURLWithoutTrailingSlash = baseURL.toString().replace(/\/$/, "");
|
|
191
|
+
this.path = excludeURLParams_default(url).toString().replace(baseURLWithoutTrailingSlash, "");
|
|
157
192
|
}
|
|
158
193
|
clone() {
|
|
159
194
|
const rawClone = super.clone();
|
|
@@ -165,14 +200,14 @@ var FetchClient = class {
|
|
|
165
200
|
}
|
|
166
201
|
return Request2;
|
|
167
202
|
}
|
|
168
|
-
isRequest(request,
|
|
169
|
-
return request instanceof Request && request.method === method && "path" in request && typeof request.path === "string" &&
|
|
203
|
+
isRequest(request, method, path) {
|
|
204
|
+
return request instanceof Request && request.method === method && "path" in request && typeof request.path === "string" && createRegExpFromURL_default(path).test(request.path);
|
|
170
205
|
}
|
|
171
|
-
isResponse(response,
|
|
172
|
-
return response instanceof Response && "request" in response && "error" in response &&
|
|
206
|
+
isResponse(response, method, path) {
|
|
207
|
+
return response instanceof Response && "request" in response && this.isRequest(response.request, method, path) && "error" in response && (response.error === null || response.error instanceof FetchResponseError_default);
|
|
173
208
|
}
|
|
174
|
-
isResponseError(error,
|
|
175
|
-
return error instanceof FetchResponseError_default && error.request
|
|
209
|
+
isResponseError(error, method, path) {
|
|
210
|
+
return error instanceof FetchResponseError_default && this.isRequest(error.request, method, path) && this.isResponse(error.response, method, path);
|
|
176
211
|
}
|
|
177
212
|
};
|
|
178
213
|
var FetchClient_default = FetchClient;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/client/errors/FetchResponseError.ts","../src/utils/urls.ts","../src/client/FetchClient.ts","../src/client/factory.ts"],"names":["Request","HttpSearchParams"],"mappings":";;;;;;;;AAIA,IAAM,kBAAA,GAAN,cAIU,KAAM,CAAA;AAAA,EACd,WAAA,CACS,SACA,QACP,EAAA;AACA,IAAA,KAAA,CAAM,CAAG,EAAA,OAAA,CAAQ,MAAM,CAAA,CAAA,EAAI,OAAQ,CAAA,GAAG,CAAuB,oBAAA,EAAA,QAAA,CAAS,MAAM,CAAA,EAAA,EAAK,QAAS,CAAA,UAAU,CAAE,CAAA,CAAA;AAH/F,IAAA,IAAA,CAAA,OAAA,GAAA,OAAA;AACA,IAAA,IAAA,CAAA,QAAA,GAAA,QAAA;AAGP,IAAA,IAAA,CAAK,IAAO,GAAA,oBAAA;AAAA;AACd,EAfF;AAQgB,IAAA,MAAA,CAAA,IAAA,EAAA,oBAAA,CAAA;AAAA;AAAA,EASd,IAAI,KAAQ,GAAA;AACV,IAAA,OAAO,IAAK,CAAA,QAAA;AAAA;AAEhB,CAAA;AAKA,IAAO,0BAAQ,GAAA;;;ACzBR,SAAS,qBAAqB,GAAU,EAAA;AAC7C,EAAA,GAAA,CAAI,IAAO,GAAA,EAAA;AACX,EAAA,GAAA,CAAI,MAAS,GAAA,EAAA;AACb,EAAA,GAAA,CAAI,QAAW,GAAA,EAAA;AACf,EAAA,GAAA,CAAI,QAAW,GAAA,EAAA;AACf,EAAO,OAAA,GAAA;AACT;AANgB,MAAA,CAAA,oBAAA,EAAA,sBAAA,CAAA;AAQhB,SAAS,mBAAmB,GAAa,EAAA;AACvC,EAAM,MAAA,UAAA,GAAa,UAAU,GAAG,CAAA;AAChC,EAAO,OAAA,UAAA,CAAW,OAAQ,CAAA,gBAAA,EAAkB,MAAM,CAAA;AACpD;AAHS,MAAA,CAAA,kBAAA,EAAA,oBAAA,C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;AAAA,UACA;AAAA,SAKF;AAAA;AACF;AAGF,IAAOA,OAAAA,QAAAA;AAAA;AACT,EAEA,SAAA,CACE,OACA,EAAA,IAAA,EACA,MACsE,EAAA;AACtE,IAAA,OACE,mBAAmB,OACnB,IAAA,OAAA,CAAQ,MAAW,KAAA,MAAA,IACnB,UAAU,OACV,IAAA,OAAO,OAAQ,CAAA,IAAA,KAAS,YACxB,kBAAmB,CAAA,IAAI,CAAE,CAAA,IAAA,CAAK,QAAQ,IAAI,CAAA;AAAA;AAE9C,EAEA,UAAA,CACE,QACA,EAAA,IAAA,EACA,MACwE,EAAA;AACxE,IACE,OAAA,QAAA,YAAoB,QACpB,IAAA,SAAA,IAAa,QACb,IAAA,OAAA,IAAW,QACX,IAAA,IAAA,CAAK,SAAU,CAAA,QAAA,CAAS,OAAS,EAAA,IAAA,EAAM,MAAM,CAAA;AAAA;AAEjD,EAEA,eAAA,CACE,KACA,EAAA,IAAA,EACA,MAC0E,EAAA;AAC1E,IAAA,OACE,iBAAiB,0BACjB,IAAA,KAAA,CAAM,OAAQ,CAAA,MAAA,KAAW,UACzB,OAAO,KAAA,CAAM,OAAQ,CAAA,IAAA,KAAS,YAC9B,kBAAmB,CAAA,IAAI,EAAE,IAAK,CAAA,KAAA,CAAM,QAAQ,IAAI,CAAA;AAAA;AAGtD,CAAA;AAEA,IAAO,mBAAQ,GAAA,WAAA;;;AClOf,SAAS,YACP,OACiC,EAAA;AACjC,EAAA,MAAM,EAAE,KAAA,EAAU,GAAA,IAAI,oBAAgC,OAAO,CAAA;AAC7D,EAAO,OAAA,KAAA;AACT;AALS,MAAA,CAAA,WAAA,EAAA,aAAA,CAAA;AAOT,IAAO,eAAQ,GAAA","file":"index.js","sourcesContent":["import { HttpMethod, HttpMethodSchema } from '@zimic/http';\n\nimport { FetchRequest, FetchResponse } from '../types/requests';\n\nclass FetchResponseError<\n Path extends string = string,\n Method extends HttpMethod = HttpMethod,\n MethodSchema extends HttpMethodSchema = HttpMethodSchema,\n> extends Error {\n constructor(\n public request: FetchRequest<Path, Method, MethodSchema>,\n public response: FetchResponse<Path, Method, MethodSchema, true>,\n ) {\n super(`${request.method} ${request.url} failed with status ${response.status}: ${response.statusText}`);\n this.name = 'FetchResponseError';\n }\n\n get cause() {\n return this.response;\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AnyFetchRequestError = FetchResponseError<any, any, any>;\n\nexport default FetchResponseError;\n","export function excludeNonPathParams(url: URL) {\n url.hash = '';\n url.search = '';\n url.username = '';\n url.password = '';\n return url;\n}\n\nfunction prepareURLForRegex(url: string) {\n const encodedURL = encodeURI(url);\n return encodedURL.replace(/([.()*?+$\\\\])/g, '\\\\$1');\n}\n\nconst URL_PATH_PARAM_REGEX = /\\/:([^/]+)/g;\n\nexport function createRegexFromURL(url: string) {\n const urlWithReplacedPathParams = prepareURLForRegex(url)\n .replace(URL_PATH_PARAM_REGEX, '/(?<$1>[^/]+)')\n .replace(/(\\/+)$/, '(?:/+)?');\n\n return new RegExp(`^${urlWithReplacedPathParams}$`);\n}\n\nexport function joinURL(...parts: (string | URL)[]) {\n return parts\n .map((part, index) => {\n const isFirstPart = index === 0;\n const isLastPart = index === parts.length - 1;\n\n let partAsString = part.toString();\n\n if (!isFirstPart) {\n partAsString = partAsString.replace(/^\\//, '');\n }\n if (!isLastPart) {\n partAsString = partAsString.replace(/\\/$/, '');\n }\n\n return partAsString;\n })\n .filter((part) => part.length > 0)\n .join('/');\n}\n","import {\n HttpSchemaPath,\n HttpSchemaMethod,\n HttpSearchParams,\n LiteralHttpSchemaPathFromNonLiteral,\n HttpSchema,\n} from '@zimic/http';\n\nimport { Default } from '@/types/utils';\nimport { createRegexFromURL, excludeNonPathParams, joinURL } from '@/utils/urls';\n\nimport FetchResponseError from './errors/FetchResponseError';\nimport { FetchInput, FetchOptions, Fetch } from './types/public';\nimport { FetchRequestConstructor, FetchRequestInit, FetchRequest, FetchResponse } from './types/requests';\n\nclass FetchClient<Schema extends HttpSchema> {\n fetch: Fetch<Schema>;\n\n constructor({ onRequest, onResponse, ...defaults }: FetchOptions<Schema>) {\n this.fetch = this.createFetchFunction();\n this.fetch.defaults = defaults;\n this.fetch.Request = this.createRequestClass(defaults);\n this.fetch.onRequest = onRequest;\n this.fetch.onResponse = onResponse;\n }\n\n private createFetchFunction() {\n const fetch = async <\n Path extends HttpSchemaPath.NonLiteral<Schema, Method>,\n Method extends HttpSchemaMethod<Schema>,\n >(\n input: FetchInput<Schema, Path, Method>,\n init: FetchRequestInit<Schema, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>, Method>,\n ) => {\n const request = await this.createFetchRequest<Path, Method>(input, init);\n const requestClone = request.clone();\n\n const rawResponse = await globalThis.fetch(\n // Optimize type checking by narrowing the type of request\n requestClone as Request,\n );\n const response = await this.createFetchResponse<\n LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>,\n Method\n >(request, rawResponse);\n\n return response;\n };\n\n Object.setPrototypeOf(fetch, this);\n\n return fetch as Fetch<Schema>;\n }\n\n private async createFetchRequest<\n Path extends HttpSchemaPath.NonLiteral<Schema, Method>,\n Method extends HttpSchemaMethod<Schema>,\n >(\n input: FetchInput<Schema, Path, Method>,\n init: FetchRequestInit<Schema, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>, Method>,\n ) {\n let request = input instanceof Request ? input : new this.fetch.Request(input, init);\n\n if (this.fetch.onRequest) {\n const requestAfterInterceptor = await this.fetch.onRequest(\n // Optimize type checking by narrowing the type of request\n request as FetchRequest.Loose,\n this.fetch,\n );\n\n if (requestAfterInterceptor !== request) {\n const isFetchRequest = requestAfterInterceptor instanceof this.fetch.Request;\n\n request = isFetchRequest\n ? (requestAfterInterceptor as Request as typeof request)\n : new this.fetch.Request(requestAfterInterceptor as FetchInput<Schema, Path, Method>, init);\n }\n }\n\n return request;\n }\n\n private async createFetchResponse<\n Path extends HttpSchemaPath<Schema, Method>,\n Method extends HttpSchemaMethod<Schema>,\n >(fetchRequest: FetchRequest<Path, Method, Default<Schema[Path][Method]>>, rawResponse: Response) {\n let response = this.defineFetchResponseProperties<Path, Method>(fetchRequest, rawResponse);\n\n if (this.fetch.onResponse) {\n const responseAfterInterceptor = await this.fetch.onResponse(\n // Optimize type checking by narrowing the type of response\n response as FetchResponse.Loose,\n this.fetch,\n );\n\n const isFetchResponse =\n responseAfterInterceptor instanceof Response &&\n 'request' in responseAfterInterceptor &&\n responseAfterInterceptor.request instanceof this.fetch.Request;\n\n response = isFetchResponse\n ? (responseAfterInterceptor as typeof response)\n : this.defineFetchResponseProperties<Path, Method>(fetchRequest, responseAfterInterceptor);\n }\n\n return response;\n }\n\n private defineFetchResponseProperties<\n Path extends HttpSchemaPath<Schema, Method>,\n Method extends HttpSchemaMethod<Schema>,\n >(fetchRequest: FetchRequest<Path, Method, Default<Schema[Path][Method]>>, response: Response) {\n const fetchResponse = response as FetchResponse<Path, Method, Default<Schema[Path][Method]>>;\n\n Object.defineProperty(fetchResponse, 'request', {\n value: fetchRequest satisfies FetchResponse.Loose['request'],\n writable: false,\n enumerable: true,\n configurable: false,\n });\n\n const responseError = (\n fetchResponse.ok ? null : new FetchResponseError(fetchRequest, fetchResponse)\n ) satisfies FetchResponse.Loose['error'];\n\n Object.defineProperty(fetchResponse, 'error', {\n value: responseError,\n writable: false,\n enumerable: true,\n configurable: false,\n });\n\n return fetchResponse;\n }\n\n private createRequestClass(defaults: FetchRequestInit.Defaults) {\n class Request<\n Path extends HttpSchemaPath.NonLiteral<Schema, Method>,\n Method extends HttpSchemaMethod<Schema>,\n > extends globalThis.Request {\n path: LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>;\n\n constructor(\n input: FetchInput<Schema, Path, Method>,\n rawInit: FetchRequestInit<Schema, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>, Method>,\n ) {\n const init = { ...defaults, ...rawInit };\n\n let url: URL;\n\n if (input instanceof globalThis.Request) {\n super(\n // Optimize type checking by narrowing the type of input\n input as globalThis.Request,\n init,\n );\n\n url = new URL(input.url);\n } else {\n url = input instanceof URL ? new URL(input) : new URL(joinURL(init.baseURL, input));\n\n if (init.searchParams) {\n url.search = new HttpSearchParams(init.searchParams).toString();\n }\n\n super(url, init);\n }\n\n this.path = excludeNonPathParams(url)\n .toString()\n .replace(init.baseURL, '') as LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>;\n }\n\n clone(): Request<Path, Method> {\n const rawClone = super.clone();\n\n return new Request<Path, Method>(\n rawClone as unknown as FetchInput<Schema, Path, Method>,\n rawClone as unknown as FetchRequestInit<\n Schema,\n LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>,\n Method\n >,\n );\n }\n }\n\n return Request as FetchRequestConstructor<Schema>;\n }\n\n isRequest<Path extends HttpSchemaPath<Schema, Method>, Method extends HttpSchemaMethod<Schema>>(\n request: unknown,\n path: Path,\n method: Method,\n ): request is FetchRequest<Path, Method, Default<Schema[Path][Method]>> {\n return (\n request instanceof Request &&\n request.method === method &&\n 'path' in request &&\n typeof request.path === 'string' &&\n createRegexFromURL(path).test(request.path)\n );\n }\n\n isResponse<Path extends HttpSchemaPath<Schema, Method>, Method extends HttpSchemaMethod<Schema>>(\n response: unknown,\n path: Path,\n method: Method,\n ): response is FetchResponse<Path, Method, Default<Schema[Path][Method]>> {\n return (\n response instanceof Response &&\n 'request' in response &&\n 'error' in response &&\n this.isRequest(response.request, path, method)\n );\n }\n\n isResponseError<Path extends HttpSchemaPath<Schema, Method>, Method extends HttpSchemaMethod<Schema>>(\n error: unknown,\n path: Path,\n method: Method,\n ): error is FetchResponseError<Path, Method, Default<Schema[Path][Method]>> {\n return (\n error instanceof FetchResponseError &&\n error.request.method === method &&\n typeof error.request.path === 'string' &&\n createRegexFromURL(path).test(error.request.path)\n );\n }\n}\n\nexport default FetchClient;\n","import { HttpSchema } from '@zimic/http';\n\nimport FetchClient from './FetchClient';\nimport { FetchOptions, Fetch as PublicFetch } from './types/public';\n\nfunction createFetch<Schema extends HttpSchema>(\n options: FetchOptions<HttpSchema<Schema>>,\n): PublicFetch<HttpSchema<Schema>> {\n const { fetch } = new FetchClient<HttpSchema<Schema>>(options);\n return fetch;\n}\n\nexport default createFetch;\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/client/errors/FetchResponseError.ts","../../zimic-utils/dist/chunk-PAWJFY3S.mjs","../../zimic-utils/src/url/createRegExpFromURL.ts","../../zimic-utils/src/url/excludeURLParams.ts","../../zimic-utils/src/url/joinURL.ts","../src/client/FetchClient.ts","../src/client/factory.ts"],"names":["__defProp","__name","Request","HttpHeaders","HttpSearchParams"],"mappings":";;;;;;;;AA6CA,IAAM,kBAAA,GAAN,cAIU,KAAM,CAAA;AAAA,EACd,WAAA,CACS,SACA,QACP,EAAA;AACA,IAAA,KAAA,CAAM,CAAG,EAAA,OAAA,CAAQ,MAAM,CAAA,CAAA,EAAI,OAAQ,CAAA,GAAG,CAAuB,oBAAA,EAAA,QAAA,CAAS,MAAM,CAAA,EAAA,EAAK,QAAS,CAAA,UAAU,CAAE,CAAA,CAAA;AAH/F,IAAA,IAAA,CAAA,OAAA,GAAA,OAAA;AACA,IAAA,IAAA,CAAA,QAAA,GAAA,QAAA;AAGP,IAAA,IAAA,CAAK,IAAO,GAAA,oBAAA;AAAA;AACd,EAxDF;AAiDgB,IAAA,MAAA,CAAA,IAAA,EAAA,oBAAA,CAAA;AAAA;AAAA,EASd,IAAI,KAAQ,GAAA;AACV,IAAA,OAAO,IAAK,CAAA,QAAA;AAAA;AAEhB,CAAA;AAKA,IAAO,0BAAQ,GAAA;;;AClEf,IAAIA,aAAY,MAAO,CAAA,cAAA;AACvB,IAAIC,OAAS,mBAAA,MAAA,CAAA,CAAC,MAAQ,EAAA,KAAA,KAAUD,UAAU,CAAA,MAAA,EAAQ,MAAQ,EAAA,EAAE,KAAO,EAAA,YAAA,EAAc,IAAK,EAAC,CAA1E,EAAA,QAAA,CAAA;;;ACDN,IAAM,oBAAuB,GAAA,aAAA;AAEpC,SAAS,o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A,CAAW,KAAM,CAAA,QAAA,EAAU,QAAQ,IAAI,CAAA;AAAA;AAGlD,CAAA;AAEA,IAAO,mBAAQ,GAAA,WAAA;;;AC5Nf,SAAS,YAAuC,OAA8C,EAAA;AAC5F,EAAA,MAAM,EAAE,KAAA,EAAU,GAAA,IAAI,oBAAoB,OAAO,CAAA;AACjD,EAAO,OAAA,KAAA;AACT;AAHS,MAAA,CAAA,WAAA,EAAA,aAAA,CAAA;AAKT,IAAO,eAAQ,GAAA","file":"index.js","sourcesContent":["import { HttpSchema, HttpSchemaMethod, HttpSchemaPath } from '@zimic/http';\n\nimport { FetchRequest, FetchResponse } from '../types/requests';\n\n/**\n * An error representing a response with a failure status code (4XX or 5XX).\n *\n * @example\n * import { type HttpSchema } from '@zimic/http';\n * import { createFetch } from '@zimic/fetch';\n *\n * interface User {\n * id: string;\n * username: string;\n * }\n *\n * type Schema = HttpSchema<{\n * '/users/:userId': {\n * GET: {\n * response: {\n * 200: { body: User };\n * 404: { body: { message: string } };\n * };\n * };\n * };\n * }>;\n *\n * const fetch = createFetch<Schema>({\n * baseURL: 'http://localhost:3000',\n * });\n *\n * const response = await fetch(`/users/${userId}`, {\n * method: 'GET',\n * });\n *\n * if (!response.ok) {\n * console.log(response.status); // 404\n *\n * console.log(response.error); // FetchResponseError<Schema, 'GET', '/users'>\n * console.log(response.error.request); // FetchRequest<Schema, 'GET', '/users'>\n * console.log(response.error.response); // FetchResponse<Schema, 'GET', '/users'>\n * }\n *\n * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchresponseerror `FetchResponseError` API reference}\n */\nclass FetchResponseError<\n Schema extends HttpSchema,\n Method extends HttpSchemaMethod<Schema>,\n Path extends HttpSchemaPath.Literal<Schema, Method>,\n> extends Error {\n constructor(\n public request: FetchRequest<Schema, Method, Path>,\n public response: FetchResponse<Schema, Method, Path, true, 'manual'>,\n ) {\n super(`${request.method} ${request.url} failed with status ${response.status}: ${response.statusText}`);\n this.name = 'FetchResponseError';\n }\n\n get cause() {\n return this.response;\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AnyFetchRequestError = FetchResponseError<any, any, any>;\n\nexport default FetchResponseError;\n","var __defProp = Object.defineProperty;\nvar __name = (target, value) => __defProp(target, \"name\", { value, configurable: true });\n\nexport { __name };\n//# sourceMappingURL=chunk-PAWJFY3S.mjs.map\n//# sourceMappingURL=chunk-PAWJFY3S.mjs.map","export const URL_PATH_PARAM_REGEX = /\\/:([^/]+)/g;\n\nfunction createRegExpFromURL(url: string) {\n URL_PATH_PARAM_REGEX.lastIndex = 0;\n\n const urlWithReplacedPathParams = encodeURI(url)\n .replace(/([.()*?+$\\\\])/g, '\\\\$1')\n .replace(URL_PATH_PARAM_REGEX, '/(?<$1>[^/]+)')\n .replace(/^(\\/)|(\\/)$/g, '');\n\n return new RegExp(`^(?:/)?${urlWithReplacedPathParams}(?:/)?$`);\n}\n\nexport default createRegExpFromURL;\n","function excludeURLParams(url: URL) {\n url.hash = '';\n url.search = '';\n url.username = '';\n url.password = '';\n return url;\n}\n\nexport default excludeURLParams;\n","function joinURL(...parts: (URL | string)[]) {\n return parts\n .map((part, index) => {\n const isFirstPart = index === 0;\n const isLastPart = index === parts.length - 1;\n\n let partAsString = part.toString();\n\n if (!isFirstPart) {\n partAsString = partAsString.replace(/^\\//, '');\n }\n if (!isLastPart) {\n partAsString = partAsString.replace(/\\/$/, '');\n }\n\n return partAsString;\n })\n .filter((part) => part.length > 0)\n .join('/');\n}\n\nexport default joinURL;\n","import {\n HttpSchemaPath,\n HttpSchemaMethod,\n HttpSearchParams,\n LiteralHttpSchemaPathFromNonLiteral,\n HttpSchema,\n HttpHeaders,\n} from '@zimic/http';\nimport createRegexFromURL from '@zimic/utils/url/createRegExpFromURL';\nimport excludeURLParams from '@zimic/utils/url/excludeURLParams';\nimport joinURL from '@zimic/utils/url/joinURL';\n\nimport FetchResponseError from './errors/FetchResponseError';\nimport {\n FetchInput,\n FetchOptions,\n Fetch,\n FetchClient as PublicFetchClient,\n FetchDefaults,\n FetchFunction,\n} from './types/public';\nimport { FetchRequestConstructor, FetchRequestInit, FetchRequest, FetchResponse } from './types/requests';\n\nclass FetchClient<Schema extends HttpSchema>\n implements Omit<PublicFetchClient<Schema>, 'defaults' | 'loose' | 'Request'>\n{\n fetch: Fetch<Schema>;\n\n constructor({ onRequest, onResponse, ...defaults }: FetchOptions<Schema>) {\n this.fetch = this.createFetchFunction();\n\n this.fetch.defaults = {\n ...defaults,\n headers: defaults.headers ?? {},\n searchParams: defaults.searchParams ?? {},\n };\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n this.fetch.loose = this.fetch as Fetch<any> as FetchFunction.Loose;\n\n this.fetch.Request = this.createRequestClass(this.fetch.defaults);\n this.fetch.onRequest = onRequest;\n this.fetch.onResponse = onResponse;\n }\n\n private createFetchFunction() {\n const fetch = async <\n Method extends HttpSchemaMethod<Schema>,\n Path extends HttpSchemaPath.NonLiteral<Schema, Method>,\n >(\n input: FetchInput<Schema, Method, Path>,\n init: FetchRequestInit<Schema, Method, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>>,\n ) => {\n const request = await this.createFetchRequest<Method, Path>(input, init);\n const requestClone = request.clone();\n\n const rawResponse = await globalThis.fetch(\n // Optimize type checking by narrowing the type of request\n requestClone as Request,\n );\n const response = await this.createFetchResponse<\n Method,\n LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>\n >(request, rawResponse);\n\n return response;\n };\n\n Object.setPrototypeOf(fetch, this);\n\n return fetch as Fetch<Schema>;\n }\n\n private async createFetchRequest<\n Method extends HttpSchemaMethod<Schema>,\n Path extends HttpSchemaPath.NonLiteral<Schema, Method>,\n >(\n input: FetchInput<Schema, Method, Path>,\n init: FetchRequestInit<Schema, Method, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>>,\n ) {\n let request = input instanceof Request ? input : new this.fetch.Request(input, init);\n\n if (this.fetch.onRequest) {\n const requestAfterInterceptor = await this.fetch.onRequest(\n // Optimize type checking by narrowing the type of request\n request as FetchRequest.Loose,\n );\n\n if (requestAfterInterceptor !== request) {\n const isFetchRequest = requestAfterInterceptor instanceof this.fetch.Request;\n\n request = isFetchRequest\n ? (requestAfterInterceptor as Request as typeof request)\n : new this.fetch.Request(requestAfterInterceptor as FetchInput<Schema, Method, Path>, init);\n }\n }\n\n return request;\n }\n\n private async createFetchResponse<\n Method extends HttpSchemaMethod<Schema>,\n Path extends HttpSchemaPath.Literal<Schema, Method>,\n >(fetchRequest: FetchRequest<Schema, Method, Path>, rawResponse: Response) {\n let response = this.defineFetchResponseProperties<Method, Path>(fetchRequest, rawResponse);\n\n if (this.fetch.onResponse) {\n const responseAfterInterceptor = await this.fetch.onResponse(\n // Optimize type checking by narrowing the type of response\n response as FetchResponse.Loose,\n );\n\n const isFetchResponse =\n responseAfterInterceptor instanceof Response &&\n 'request' in responseAfterInterceptor &&\n responseAfterInterceptor.request instanceof this.fetch.Request;\n\n response = isFetchResponse\n ? (responseAfterInterceptor as typeof response)\n : this.defineFetchResponseProperties<Method, Path>(fetchRequest, responseAfterInterceptor);\n }\n\n return response;\n }\n\n private defineFetchResponseProperties<\n Method extends HttpSchemaMethod<Schema>,\n Path extends HttpSchemaPath.Literal<Schema, Method>,\n >(fetchRequest: FetchRequest<Schema, Method, Path>, response: Response) {\n const fetchResponse = response as FetchResponse<Schema, Method, Path>;\n\n Object.defineProperty(fetchResponse, 'request', {\n value: fetchRequest satisfies FetchResponse.Loose['request'],\n writable: false,\n enumerable: true,\n configurable: false,\n });\n\n let responseError: FetchResponse.Loose['error'] | undefined;\n\n Object.defineProperty(fetchResponse, 'error', {\n get() {\n if (responseError === undefined) {\n responseError = fetchResponse.ok\n ? null\n : new FetchResponseError(\n fetchRequest,\n fetchResponse as FetchResponse<Schema, Method, Path, true, 'manual'>,\n );\n }\n return responseError;\n },\n enumerable: true,\n configurable: false,\n });\n\n return fetchResponse;\n }\n\n private createRequestClass(defaults: FetchDefaults) {\n class Request<\n Method extends HttpSchemaMethod<Schema>,\n Path extends HttpSchemaPath.NonLiteral<Schema, Method>,\n > extends globalThis.Request {\n path: LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>;\n\n constructor(\n input: FetchInput<Schema, Method, Path>,\n init: FetchRequestInit<Schema, Method, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>>,\n ) {\n const initWithDefaults = { ...defaults, ...init };\n\n const headersFromDefaults = new HttpHeaders(defaults.headers);\n const headersFromInit = new HttpHeaders((init satisfies RequestInit as RequestInit).headers);\n\n let url: URL;\n const baseURL = new URL(initWithDefaults.baseURL);\n\n if (input instanceof globalThis.Request) {\n // Optimize type checking by narrowing the type of input\n const request = input as globalThis.Request;\n const headersFromRequest = new HttpHeaders(input.headers);\n\n initWithDefaults.headers = {\n ...headersFromDefaults.toObject(),\n ...headersFromRequest.toObject(),\n ...headersFromInit.toObject(),\n };\n\n super(request, initWithDefaults);\n\n url = new URL(input.url);\n } else {\n initWithDefaults.headers = {\n ...headersFromDefaults.toObject(),\n ...headersFromInit.toObject(),\n };\n\n url = input instanceof URL ? new URL(input) : new URL(joinURL(baseURL, input));\n\n const searchParamsFromDefaults = new HttpSearchParams(defaults.searchParams);\n const searchParamsFromInit = new HttpSearchParams(initWithDefaults.searchParams);\n\n initWithDefaults.searchParams = {\n ...searchParamsFromDefaults.toObject(),\n ...searchParamsFromInit.toObject(),\n };\n\n url.search = new HttpSearchParams(initWithDefaults.searchParams).toString();\n\n super(url, initWithDefaults);\n }\n\n const baseURLWithoutTrailingSlash = baseURL.toString().replace(/\\/$/, '');\n\n this.path = excludeURLParams(url)\n .toString()\n .replace(baseURLWithoutTrailingSlash, '') as LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>;\n }\n\n clone(): Request<Method, Path> {\n const rawClone = super.clone();\n\n return new Request<Method, Path>(\n rawClone as unknown as FetchInput<Schema, Method, Path>,\n rawClone as unknown as FetchRequestInit<\n Schema,\n Method,\n LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>\n >,\n );\n }\n }\n\n return Request as FetchRequestConstructor<Schema>;\n }\n\n isRequest<Path extends HttpSchemaPath.Literal<Schema, Method>, Method extends HttpSchemaMethod<Schema>>(\n request: unknown,\n method: Method,\n path: Path,\n ): request is FetchRequest<Schema, Method, Path> {\n return (\n request instanceof Request &&\n request.method === method &&\n 'path' in request &&\n typeof request.path === 'string' &&\n createRegexFromURL(path).test(request.path)\n );\n }\n\n isResponse<Path extends HttpSchemaPath.Literal<Schema, Method>, Method extends HttpSchemaMethod<Schema>>(\n response: unknown,\n method: Method,\n path: Path,\n ): response is FetchResponse<Schema, Method, Path> {\n return (\n response instanceof Response &&\n 'request' in response &&\n this.isRequest(response.request, method, path) &&\n 'error' in response &&\n (response.error === null || response.error instanceof FetchResponseError)\n );\n }\n\n isResponseError<Path extends HttpSchemaPath.Literal<Schema, Method>, Method extends HttpSchemaMethod<Schema>>(\n error: unknown,\n method: Method,\n path: Path,\n ): error is FetchResponseError<Schema, Method, Path> {\n return (\n error instanceof FetchResponseError &&\n this.isRequest(error.request, method, path) &&\n this.isResponse(error.response, method, path)\n );\n }\n}\n\nexport default FetchClient;\n","import { HttpSchema } from '@zimic/http';\n\nimport FetchClient from './FetchClient';\nimport { FetchOptions, Fetch } from './types/public';\n\n/**\n * Creates a {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetch fetch instance} typed with an HTTP\n * schema, closely compatible with the {@link https://developer.mozilla.org/docs/Web/API/Fetch_API native Fetch API}. All\n * requests and responses are typed by default with the schema, including methods, paths, status codes, parameters, and\n * bodies.\n *\n * Requests sent by the fetch instance have their URL automatically prefixed with the base URL of the instance.\n * {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetch.defaults Default options} are also applied to the\n * requests, if provided.\n *\n * @example\n * import { type HttpSchema } from '@zimic/http';\n * import { createFetch } from '@zimic/fetch';\n *\n * interface User {\n * id: string;\n * username: string;\n * }\n *\n * type Schema = HttpSchema<{\n * '/users': {\n * POST: {\n * request: {\n * headers: { 'content-type': 'application/json' };\n * body: { username: string };\n * };\n * response: {\n * 201: { body: User };\n * };\n * };\n *\n * GET: {\n * request: {\n * searchParams: {\n * query?: string;\n * page?: number;\n * limit?: number;\n * };\n * };\n * response: {\n * 200: { body: User[] };\n * };\n * };\n * };\n * }>;\n *\n * const fetch = createFetch<Schema>({\n * baseURL: 'http://localhost:3000',\n * });\n *\n * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#createfetch `createFetch(options)` API reference}\n * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetch `fetch` API reference}\n */\nfunction createFetch<Schema extends HttpSchema>(options: FetchOptions<Schema>): Fetch<Schema> {\n const { fetch } = new FetchClient<Schema>(options);\n return fetch;\n}\n\nexport default createFetch;\n"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { HttpSearchParams } from '@zimic/http';
|
|
1
|
+
import { HttpHeaders, HttpSearchParams } from '@zimic/http';
|
|
2
2
|
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
@@ -20,26 +20,34 @@ var FetchResponseError = class extends Error {
|
|
|
20
20
|
};
|
|
21
21
|
var FetchResponseError_default = FetchResponseError;
|
|
22
22
|
|
|
23
|
-
//
|
|
24
|
-
|
|
23
|
+
// ../zimic-utils/dist/chunk-PAWJFY3S.mjs
|
|
24
|
+
var __defProp2 = Object.defineProperty;
|
|
25
|
+
var __name2 = /* @__PURE__ */ __name((target, value) => __defProp2(target, "name", { value, configurable: true }), "__name");
|
|
26
|
+
|
|
27
|
+
// ../zimic-utils/dist/chunk-RIVHLEFF.mjs
|
|
28
|
+
var URL_PATH_PARAM_REGEX = /\/:([^/]+)/g;
|
|
29
|
+
function createRegExpFromURL(url) {
|
|
30
|
+
URL_PATH_PARAM_REGEX.lastIndex = 0;
|
|
31
|
+
const urlWithReplacedPathParams = encodeURI(url).replace(/([.()*?+$\\])/g, "\\$1").replace(URL_PATH_PARAM_REGEX, "/(?<$1>[^/]+)").replace(/^(\/)|(\/)$/g, "");
|
|
32
|
+
return new RegExp(`^(?:/)?${urlWithReplacedPathParams}(?:/)?$`);
|
|
33
|
+
}
|
|
34
|
+
__name(createRegExpFromURL, "createRegExpFromURL");
|
|
35
|
+
__name2(createRegExpFromURL, "createRegExpFromURL");
|
|
36
|
+
var createRegExpFromURL_default = createRegExpFromURL;
|
|
37
|
+
|
|
38
|
+
// ../zimic-utils/dist/url/excludeURLParams.mjs
|
|
39
|
+
function excludeURLParams(url) {
|
|
25
40
|
url.hash = "";
|
|
26
41
|
url.search = "";
|
|
27
42
|
url.username = "";
|
|
28
43
|
url.password = "";
|
|
29
44
|
return url;
|
|
30
45
|
}
|
|
31
|
-
__name(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
__name(prepareURLForRegex, "prepareURLForRegex");
|
|
37
|
-
var URL_PATH_PARAM_REGEX = /\/:([^/]+)/g;
|
|
38
|
-
function createRegexFromURL(url) {
|
|
39
|
-
const urlWithReplacedPathParams = prepareURLForRegex(url).replace(URL_PATH_PARAM_REGEX, "/(?<$1>[^/]+)").replace(/(\/+)$/, "(?:/+)?");
|
|
40
|
-
return new RegExp(`^${urlWithReplacedPathParams}$`);
|
|
41
|
-
}
|
|
42
|
-
__name(createRegexFromURL, "createRegexFromURL");
|
|
46
|
+
__name(excludeURLParams, "excludeURLParams");
|
|
47
|
+
__name2(excludeURLParams, "excludeURLParams");
|
|
48
|
+
var excludeURLParams_default = excludeURLParams;
|
|
49
|
+
|
|
50
|
+
// ../zimic-utils/dist/url/joinURL.mjs
|
|
43
51
|
function joinURL(...parts) {
|
|
44
52
|
return parts.map((part, index) => {
|
|
45
53
|
const isFirstPart = index === 0;
|
|
@@ -55,6 +63,8 @@ function joinURL(...parts) {
|
|
|
55
63
|
}).filter((part) => part.length > 0).join("/");
|
|
56
64
|
}
|
|
57
65
|
__name(joinURL, "joinURL");
|
|
66
|
+
__name2(joinURL, "joinURL");
|
|
67
|
+
var joinURL_default = joinURL;
|
|
58
68
|
|
|
59
69
|
// src/client/FetchClient.ts
|
|
60
70
|
var FetchClient = class {
|
|
@@ -64,8 +74,13 @@ var FetchClient = class {
|
|
|
64
74
|
fetch;
|
|
65
75
|
constructor({ onRequest, onResponse, ...defaults }) {
|
|
66
76
|
this.fetch = this.createFetchFunction();
|
|
67
|
-
this.fetch.defaults =
|
|
68
|
-
|
|
77
|
+
this.fetch.defaults = {
|
|
78
|
+
...defaults,
|
|
79
|
+
headers: defaults.headers ?? {},
|
|
80
|
+
searchParams: defaults.searchParams ?? {}
|
|
81
|
+
};
|
|
82
|
+
this.fetch.loose = this.fetch;
|
|
83
|
+
this.fetch.Request = this.createRequestClass(this.fetch.defaults);
|
|
69
84
|
this.fetch.onRequest = onRequest;
|
|
70
85
|
this.fetch.onResponse = onResponse;
|
|
71
86
|
}
|
|
@@ -88,8 +103,7 @@ var FetchClient = class {
|
|
|
88
103
|
if (this.fetch.onRequest) {
|
|
89
104
|
const requestAfterInterceptor = await this.fetch.onRequest(
|
|
90
105
|
// Optimize type checking by narrowing the type of request
|
|
91
|
-
request
|
|
92
|
-
this.fetch
|
|
106
|
+
request
|
|
93
107
|
);
|
|
94
108
|
if (requestAfterInterceptor !== request) {
|
|
95
109
|
const isFetchRequest = requestAfterInterceptor instanceof this.fetch.Request;
|
|
@@ -103,8 +117,7 @@ var FetchClient = class {
|
|
|
103
117
|
if (this.fetch.onResponse) {
|
|
104
118
|
const responseAfterInterceptor = await this.fetch.onResponse(
|
|
105
119
|
// Optimize type checking by narrowing the type of response
|
|
106
|
-
response
|
|
107
|
-
this.fetch
|
|
120
|
+
response
|
|
108
121
|
);
|
|
109
122
|
const isFetchResponse = responseAfterInterceptor instanceof Response && "request" in responseAfterInterceptor && responseAfterInterceptor.request instanceof this.fetch.Request;
|
|
110
123
|
response = isFetchResponse ? responseAfterInterceptor : this.defineFetchResponseProperties(fetchRequest, responseAfterInterceptor);
|
|
@@ -119,10 +132,17 @@ var FetchClient = class {
|
|
|
119
132
|
enumerable: true,
|
|
120
133
|
configurable: false
|
|
121
134
|
});
|
|
122
|
-
|
|
135
|
+
let responseError;
|
|
123
136
|
Object.defineProperty(fetchResponse, "error", {
|
|
124
|
-
|
|
125
|
-
|
|
137
|
+
get() {
|
|
138
|
+
if (responseError === void 0) {
|
|
139
|
+
responseError = fetchResponse.ok ? null : new FetchResponseError_default(
|
|
140
|
+
fetchRequest,
|
|
141
|
+
fetchResponse
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
return responseError;
|
|
145
|
+
},
|
|
126
146
|
enumerable: true,
|
|
127
147
|
configurable: false
|
|
128
148
|
});
|
|
@@ -134,24 +154,39 @@ var FetchClient = class {
|
|
|
134
154
|
__name(this, "Request");
|
|
135
155
|
}
|
|
136
156
|
path;
|
|
137
|
-
constructor(input,
|
|
138
|
-
const
|
|
157
|
+
constructor(input, init) {
|
|
158
|
+
const initWithDefaults = { ...defaults, ...init };
|
|
159
|
+
const headersFromDefaults = new HttpHeaders(defaults.headers);
|
|
160
|
+
const headersFromInit = new HttpHeaders(init.headers);
|
|
139
161
|
let url;
|
|
162
|
+
const baseURL = new URL(initWithDefaults.baseURL);
|
|
140
163
|
if (input instanceof globalThis.Request) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
164
|
+
const request = input;
|
|
165
|
+
const headersFromRequest = new HttpHeaders(input.headers);
|
|
166
|
+
initWithDefaults.headers = {
|
|
167
|
+
...headersFromDefaults.toObject(),
|
|
168
|
+
...headersFromRequest.toObject(),
|
|
169
|
+
...headersFromInit.toObject()
|
|
170
|
+
};
|
|
171
|
+
super(request, initWithDefaults);
|
|
146
172
|
url = new URL(input.url);
|
|
147
173
|
} else {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
|
|
174
|
+
initWithDefaults.headers = {
|
|
175
|
+
...headersFromDefaults.toObject(),
|
|
176
|
+
...headersFromInit.toObject()
|
|
177
|
+
};
|
|
178
|
+
url = input instanceof URL ? new URL(input) : new URL(joinURL_default(baseURL, input));
|
|
179
|
+
const searchParamsFromDefaults = new HttpSearchParams(defaults.searchParams);
|
|
180
|
+
const searchParamsFromInit = new HttpSearchParams(initWithDefaults.searchParams);
|
|
181
|
+
initWithDefaults.searchParams = {
|
|
182
|
+
...searchParamsFromDefaults.toObject(),
|
|
183
|
+
...searchParamsFromInit.toObject()
|
|
184
|
+
};
|
|
185
|
+
url.search = new HttpSearchParams(initWithDefaults.searchParams).toString();
|
|
186
|
+
super(url, initWithDefaults);
|
|
153
187
|
}
|
|
154
|
-
|
|
188
|
+
const baseURLWithoutTrailingSlash = baseURL.toString().replace(/\/$/, "");
|
|
189
|
+
this.path = excludeURLParams_default(url).toString().replace(baseURLWithoutTrailingSlash, "");
|
|
155
190
|
}
|
|
156
191
|
clone() {
|
|
157
192
|
const rawClone = super.clone();
|
|
@@ -163,14 +198,14 @@ var FetchClient = class {
|
|
|
163
198
|
}
|
|
164
199
|
return Request2;
|
|
165
200
|
}
|
|
166
|
-
isRequest(request,
|
|
167
|
-
return request instanceof Request && request.method === method && "path" in request && typeof request.path === "string" &&
|
|
201
|
+
isRequest(request, method, path) {
|
|
202
|
+
return request instanceof Request && request.method === method && "path" in request && typeof request.path === "string" && createRegExpFromURL_default(path).test(request.path);
|
|
168
203
|
}
|
|
169
|
-
isResponse(response,
|
|
170
|
-
return response instanceof Response && "request" in response && "error" in response &&
|
|
204
|
+
isResponse(response, method, path) {
|
|
205
|
+
return response instanceof Response && "request" in response && this.isRequest(response.request, method, path) && "error" in response && (response.error === null || response.error instanceof FetchResponseError_default);
|
|
171
206
|
}
|
|
172
|
-
isResponseError(error,
|
|
173
|
-
return error instanceof FetchResponseError_default && error.request
|
|
207
|
+
isResponseError(error, method, path) {
|
|
208
|
+
return error instanceof FetchResponseError_default && this.isRequest(error.request, method, path) && this.isResponse(error.response, method, path);
|
|
174
209
|
}
|
|
175
210
|
};
|
|
176
211
|
var FetchClient_default = FetchClient;
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/client/errors/FetchResponseError.ts","../src/utils/urls.ts","../src/client/FetchClient.ts","../src/client/factory.ts"],"names":["Request"],"mappings":";;;;;;AAIA,IAAM,kBAAA,GAAN,cAIU,KAAM,CAAA;AAAA,EACd,WAAA,CACS,SACA,QACP,EAAA;AACA,IAAA,KAAA,CAAM,CAAG,EAAA,OAAA,CAAQ,MAAM,CAAA,CAAA,EAAI,OAAQ,CAAA,GAAG,CAAuB,oBAAA,EAAA,QAAA,CAAS,MAAM,CAAA,EAAA,EAAK,QAAS,CAAA,UAAU,CAAE,CAAA,CAAA;AAH/F,IAAA,IAAA,CAAA,OAAA,GAAA,OAAA;AACA,IAAA,IAAA,CAAA,QAAA,GAAA,QAAA;AAGP,IAAA,IAAA,CAAK,IAAO,GAAA,oBAAA;AAAA;AACd,EAfF;AAQgB,IAAA,MAAA,CAAA,IAAA,EAAA,oBAAA,CAAA;AAAA;AAAA,EASd,IAAI,KAAQ,GAAA;AACV,IAAA,OAAO,IAAK,CAAA,QAAA;AAAA;AAEhB,CAAA;AAKA,IAAO,0BAAQ,GAAA;;;ACzBR,SAAS,qBAAqB,GAAU,EAAA;AAC7C,EAAA,GAAA,CAAI,IAAO,GAAA,EAAA;AACX,EAAA,GAAA,CAAI,MAAS,GAAA,EAAA;AACb,EAAA,GAAA,CAAI,QAAW,GAAA,EAAA;AACf,EAAA,GAAA,CAAI,QAAW,GAAA,EAAA;AACf,EAAO,OAAA,GAAA;AACT;AANgB,MAAA,CAAA,oBAAA,EAAA,sBAAA,CAAA;AAQhB,SAAS,mBAAmB,GAAa,EAAA;AACvC,EAAM,MAAA,UAAA,GAAa,UAAU,GAAG,CAAA;A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{ FetchRequest, FetchResponse } from '../types/requests';\n\nclass FetchResponseError<\n Path extends string = string,\n Method extends HttpMethod = HttpMethod,\n MethodSchema extends HttpMethodSchema = HttpMethodSchema,\n> extends Error {\n constructor(\n public request: FetchRequest<Path, Method, MethodSchema>,\n public response: FetchResponse<Path, Method, MethodSchema, true>,\n ) {\n super(`${request.method} ${request.url} failed with status ${response.status}: ${response.statusText}`);\n this.name = 'FetchResponseError';\n }\n\n get cause() {\n return this.response;\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AnyFetchRequestError = FetchResponseError<any, any, any>;\n\nexport default FetchResponseError;\n","export function excludeNonPathParams(url: URL) {\n url.hash = '';\n url.search = '';\n url.username = '';\n url.password = '';\n return url;\n}\n\nfunction prepareURLForRegex(url: string) {\n const encodedURL = encodeURI(url);\n return encodedURL.replace(/([.()*?+$\\\\])/g, '\\\\$1');\n}\n\nconst URL_PATH_PARAM_REGEX = /\\/:([^/]+)/g;\n\nexport function createRegexFromURL(url: string) {\n const urlWithReplacedPathParams = prepareURLForRegex(url)\n .replace(URL_PATH_PARAM_REGEX, '/(?<$1>[^/]+)')\n .replace(/(\\/+)$/, '(?:/+)?');\n\n return new RegExp(`^${urlWithReplacedPathParams}$`);\n}\n\nexport function joinURL(...parts: (string | URL)[]) {\n return parts\n .map((part, index) => {\n const isFirstPart = index === 0;\n const isLastPart = index === parts.length - 1;\n\n let partAsString = part.toString();\n\n if (!isFirstPart) {\n partAsString = partAsString.replace(/^\\//, '');\n }\n if (!isLastPart) {\n partAsString = partAsString.replace(/\\/$/, '');\n }\n\n return partAsString;\n })\n .filter((part) => part.length > 0)\n .join('/');\n}\n","import {\n HttpSchemaPath,\n HttpSchemaMethod,\n HttpSearchParams,\n LiteralHttpSchemaPathFromNonLiteral,\n HttpSchema,\n} from '@zimic/http';\n\nimport { Default } from '@/types/utils';\nimport { createRegexFromURL, excludeNonPathParams, joinURL } from '@/utils/urls';\n\nimport FetchResponseError from './errors/FetchResponseError';\nimport { FetchInput, FetchOptions, Fetch } from './types/public';\nimport { FetchRequestConstructor, FetchRequestInit, FetchRequest, FetchResponse } from './types/requests';\n\nclass FetchClient<Schema extends HttpSchema> {\n fetch: Fetch<Schema>;\n\n constructor({ onRequest, onResponse, ...defaults }: FetchOptions<Schema>) {\n this.fetch = this.createFetchFunction();\n this.fetch.defaults = defaults;\n this.fetch.Request = this.createRequestClass(defaults);\n this.fetch.onRequest = onRequest;\n this.fetch.onResponse = onResponse;\n }\n\n private createFetchFunction() {\n const fetch = async <\n Path extends HttpSchemaPath.NonLiteral<Schema, Method>,\n Method extends HttpSchemaMethod<Schema>,\n >(\n input: FetchInput<Schema, Path, Method>,\n init: FetchRequestInit<Schema, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>, Method>,\n ) => {\n const request = await this.createFetchRequest<Path, Method>(input, init);\n const requestClone = request.clone();\n\n const rawResponse = await globalThis.fetch(\n // Optimize type checking by narrowing the type of request\n requestClone as Request,\n );\n const response = await this.createFetchResponse<\n LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>,\n Method\n >(request, rawResponse);\n\n return response;\n };\n\n Object.setPrototypeOf(fetch, this);\n\n return fetch as Fetch<Schema>;\n }\n\n private async createFetchRequest<\n Path extends HttpSchemaPath.NonLiteral<Schema, Method>,\n Method extends HttpSchemaMethod<Schema>,\n >(\n input: FetchInput<Schema, Path, Method>,\n init: FetchRequestInit<Schema, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>, Method>,\n ) {\n let request = input instanceof Request ? input : new this.fetch.Request(input, init);\n\n if (this.fetch.onRequest) {\n const requestAfterInterceptor = await this.fetch.onRequest(\n // Optimize type checking by narrowing the type of request\n request as FetchRequest.Loose,\n this.fetch,\n );\n\n if (requestAfterInterceptor !== request) {\n const isFetchRequest = requestAfterInterceptor instanceof this.fetch.Request;\n\n request = isFetchRequest\n ? (requestAfterInterceptor as Request as typeof request)\n : new this.fetch.Request(requestAfterInterceptor as FetchInput<Schema, Path, Method>, init);\n }\n }\n\n return request;\n }\n\n private async createFetchResponse<\n Path extends HttpSchemaPath<Schema, Method>,\n Method extends HttpSchemaMethod<Schema>,\n >(fetchRequest: FetchRequest<Path, Method, Default<Schema[Path][Method]>>, rawResponse: Response) {\n let response = this.defineFetchResponseProperties<Path, Method>(fetchRequest, rawResponse);\n\n if (this.fetch.onResponse) {\n const responseAfterInterceptor = await this.fetch.onResponse(\n // Optimize type checking by narrowing the type of response\n response as FetchResponse.Loose,\n this.fetch,\n );\n\n const isFetchResponse =\n responseAfterInterceptor instanceof Response &&\n 'request' in responseAfterInterceptor &&\n responseAfterInterceptor.request instanceof this.fetch.Request;\n\n response = isFetchResponse\n ? (responseAfterInterceptor as typeof response)\n : this.defineFetchResponseProperties<Path, Method>(fetchRequest, responseAfterInterceptor);\n }\n\n return response;\n }\n\n private defineFetchResponseProperties<\n Path extends HttpSchemaPath<Schema, Method>,\n Method extends HttpSchemaMethod<Schema>,\n >(fetchRequest: FetchRequest<Path, Method, Default<Schema[Path][Method]>>, response: Response) {\n const fetchResponse = response as FetchResponse<Path, Method, Default<Schema[Path][Method]>>;\n\n Object.defineProperty(fetchResponse, 'request', {\n value: fetchRequest satisfies FetchResponse.Loose['request'],\n writable: false,\n enumerable: true,\n configurable: false,\n });\n\n const responseError = (\n fetchResponse.ok ? null : new FetchResponseError(fetchRequest, fetchResponse)\n ) satisfies FetchResponse.Loose['error'];\n\n Object.defineProperty(fetchResponse, 'error', {\n value: responseError,\n writable: false,\n enumerable: true,\n configurable: false,\n });\n\n return fetchResponse;\n }\n\n private createRequestClass(defaults: FetchRequestInit.Defaults) {\n class Request<\n Path extends HttpSchemaPath.NonLiteral<Schema, Method>,\n Method extends HttpSchemaMethod<Schema>,\n > extends globalThis.Request {\n path: LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>;\n\n constructor(\n input: FetchInput<Schema, Path, Method>,\n rawInit: FetchRequestInit<Schema, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>, Method>,\n ) {\n const init = { ...defaults, ...rawInit };\n\n let url: URL;\n\n if (input instanceof globalThis.Request) {\n super(\n // Optimize type checking by narrowing the type of input\n input as globalThis.Request,\n init,\n );\n\n url = new URL(input.url);\n } else {\n url = input instanceof URL ? new URL(input) : new URL(joinURL(init.baseURL, input));\n\n if (init.searchParams) {\n url.search = new HttpSearchParams(init.searchParams).toString();\n }\n\n super(url, init);\n }\n\n this.path = excludeNonPathParams(url)\n .toString()\n .replace(init.baseURL, '') as LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>;\n }\n\n clone(): Request<Path, Method> {\n const rawClone = super.clone();\n\n return new Request<Path, Method>(\n rawClone as unknown as FetchInput<Schema, Path, Method>,\n rawClone as unknown as FetchRequestInit<\n Schema,\n LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>,\n Method\n >,\n );\n }\n }\n\n return Request as FetchRequestConstructor<Schema>;\n }\n\n isRequest<Path extends HttpSchemaPath<Schema, Method>, Method extends HttpSchemaMethod<Schema>>(\n request: unknown,\n path: Path,\n method: Method,\n ): request is FetchRequest<Path, Method, Default<Schema[Path][Method]>> {\n return (\n request instanceof Request &&\n request.method === method &&\n 'path' in request &&\n typeof request.path === 'string' &&\n createRegexFromURL(path).test(request.path)\n );\n }\n\n isResponse<Path extends HttpSchemaPath<Schema, Method>, Method extends HttpSchemaMethod<Schema>>(\n response: unknown,\n path: Path,\n method: Method,\n ): response is FetchResponse<Path, Method, Default<Schema[Path][Method]>> {\n return (\n response instanceof Response &&\n 'request' in response &&\n 'error' in response &&\n this.isRequest(response.request, path, method)\n );\n }\n\n isResponseError<Path extends HttpSchemaPath<Schema, Method>, Method extends HttpSchemaMethod<Schema>>(\n error: unknown,\n path: Path,\n method: Method,\n ): error is FetchResponseError<Path, Method, Default<Schema[Path][Method]>> {\n return (\n error instanceof FetchResponseError &&\n error.request.method === method &&\n typeof error.request.path === 'string' &&\n createRegexFromURL(path).test(error.request.path)\n );\n }\n}\n\nexport default FetchClient;\n","import { HttpSchema } from '@zimic/http';\n\nimport FetchClient from './FetchClient';\nimport { FetchOptions, Fetch as PublicFetch } from './types/public';\n\nfunction createFetch<Schema extends HttpSchema>(\n options: FetchOptions<HttpSchema<Schema>>,\n): PublicFetch<HttpSchema<Schema>> {\n const { fetch } = new FetchClient<HttpSchema<Schema>>(options);\n return fetch;\n}\n\nexport default createFetch;\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/client/errors/FetchResponseError.ts","../../zimic-utils/dist/chunk-PAWJFY3S.mjs","../../zimic-utils/src/url/createRegExpFromURL.ts","../../zimic-utils/src/url/excludeURLParams.ts","../../zimic-utils/src/url/joinURL.ts","../src/client/FetchClient.ts","../src/client/factory.ts"],"names":["__defProp","__name","Request"],"mappings":";;;;;;AA6CA,IAAM,kBAAA,GAAN,cAIU,KAAM,CAAA;AAAA,EACd,WAAA,CACS,SACA,QACP,EAAA;AACA,IAAA,KAAA,CAAM,CAAG,EAAA,OAAA,CAAQ,MAAM,CAAA,CAAA,EAAI,OAAQ,CAAA,GAAG,CAAuB,oBAAA,EAAA,QAAA,CAAS,MAAM,CAAA,EAAA,EAAK,QAAS,CAAA,UAAU,CAAE,CAAA,CAAA;AAH/F,IAAA,IAAA,CAAA,OAAA,GAAA,OAAA;AACA,IAAA,IAAA,CAAA,QAAA,GAAA,QAAA;AAGP,IAAA,IAAA,CAAK,IAAO,GAAA,oBAAA;AAAA;AACd,EAxDF;AAiDgB,IAAA,MAAA,CAAA,IAAA,EAAA,oBAAA,CAAA;AAAA;AAAA,EASd,IAAI,KAAQ,GAAA;AACV,IAAA,OAAO,IAAK,CAAA,QAAA;AAAA;AAEhB,CAAA;AAKA,IAAO,0BAAQ,GAAA;;;AClEf,IAAIA,aAAY,MAAO,CAAA,cAAA;AACvB,IAAIC,OAAS,mBAAA,MAAA,CAAA,CAAC,MAAQ,EAAA,KAAA,KAAUD,UAAU,CAAA,MAAA,EAAQ,MAAQ,EAAA,EAAE,KAAO,EAAA,YAAA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AAA,SAAA,CAAU,MAAM,OAAS,EAAA,MAAA,EAAQ,IAAI,CAAA,IAC1C,IAAK,CAAA,UAAA,CAAW,KAAM,CAAA,QAAA,EAAU,QAAQ,IAAI,CAAA;AAAA;AAGlD,CAAA;AAEA,IAAO,mBAAQ,GAAA,WAAA;;;AC5Nf,SAAS,YAAuC,OAA8C,EAAA;AAC5F,EAAA,MAAM,EAAE,KAAA,EAAU,GAAA,IAAI,oBAAoB,OAAO,CAAA;AACjD,EAAO,OAAA,KAAA;AACT;AAHS,MAAA,CAAA,WAAA,EAAA,aAAA,CAAA;AAKT,IAAO,eAAQ,GAAA","file":"index.mjs","sourcesContent":["import { HttpSchema, HttpSchemaMethod, HttpSchemaPath } from '@zimic/http';\n\nimport { FetchRequest, FetchResponse } from '../types/requests';\n\n/**\n * An error representing a response with a failure status code (4XX or 5XX).\n *\n * @example\n * import { type HttpSchema } from '@zimic/http';\n * import { createFetch } from '@zimic/fetch';\n *\n * interface User {\n * id: string;\n * username: string;\n * }\n *\n * type Schema = HttpSchema<{\n * '/users/:userId': {\n * GET: {\n * response: {\n * 200: { body: User };\n * 404: { body: { message: string } };\n * };\n * };\n * };\n * }>;\n *\n * const fetch = createFetch<Schema>({\n * baseURL: 'http://localhost:3000',\n * });\n *\n * const response = await fetch(`/users/${userId}`, {\n * method: 'GET',\n * });\n *\n * if (!response.ok) {\n * console.log(response.status); // 404\n *\n * console.log(response.error); // FetchResponseError<Schema, 'GET', '/users'>\n * console.log(response.error.request); // FetchRequest<Schema, 'GET', '/users'>\n * console.log(response.error.response); // FetchResponse<Schema, 'GET', '/users'>\n * }\n *\n * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchresponseerror `FetchResponseError` API reference}\n */\nclass FetchResponseError<\n Schema extends HttpSchema,\n Method extends HttpSchemaMethod<Schema>,\n Path extends HttpSchemaPath.Literal<Schema, Method>,\n> extends Error {\n constructor(\n public request: FetchRequest<Schema, Method, Path>,\n public response: FetchResponse<Schema, Method, Path, true, 'manual'>,\n ) {\n super(`${request.method} ${request.url} failed with status ${response.status}: ${response.statusText}`);\n this.name = 'FetchResponseError';\n }\n\n get cause() {\n return this.response;\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AnyFetchRequestError = FetchResponseError<any, any, any>;\n\nexport default FetchResponseError;\n","var __defProp = Object.defineProperty;\nvar __name = (target, value) => __defProp(target, \"name\", { value, configurable: true });\n\nexport { __name };\n//# sourceMappingURL=chunk-PAWJFY3S.mjs.map\n//# sourceMappingURL=chunk-PAWJFY3S.mjs.map","export const URL_PATH_PARAM_REGEX = /\\/:([^/]+)/g;\n\nfunction createRegExpFromURL(url: string) {\n URL_PATH_PARAM_REGEX.lastIndex = 0;\n\n const urlWithReplacedPathParams = encodeURI(url)\n .replace(/([.()*?+$\\\\])/g, '\\\\$1')\n .replace(URL_PATH_PARAM_REGEX, '/(?<$1>[^/]+)')\n .replace(/^(\\/)|(\\/)$/g, '');\n\n return new RegExp(`^(?:/)?${urlWithReplacedPathParams}(?:/)?$`);\n}\n\nexport default createRegExpFromURL;\n","function excludeURLParams(url: URL) {\n url.hash = '';\n url.search = '';\n url.username = '';\n url.password = '';\n return url;\n}\n\nexport default excludeURLParams;\n","function joinURL(...parts: (URL | string)[]) {\n return parts\n .map((part, index) => {\n const isFirstPart = index === 0;\n const isLastPart = index === parts.length - 1;\n\n let partAsString = part.toString();\n\n if (!isFirstPart) {\n partAsString = partAsString.replace(/^\\//, '');\n }\n if (!isLastPart) {\n partAsString = partAsString.replace(/\\/$/, '');\n }\n\n return partAsString;\n })\n .filter((part) => part.length > 0)\n .join('/');\n}\n\nexport default joinURL;\n","import {\n HttpSchemaPath,\n HttpSchemaMethod,\n HttpSearchParams,\n LiteralHttpSchemaPathFromNonLiteral,\n HttpSchema,\n HttpHeaders,\n} from '@zimic/http';\nimport createRegexFromURL from '@zimic/utils/url/createRegExpFromURL';\nimport excludeURLParams from '@zimic/utils/url/excludeURLParams';\nimport joinURL from '@zimic/utils/url/joinURL';\n\nimport FetchResponseError from './errors/FetchResponseError';\nimport {\n FetchInput,\n FetchOptions,\n Fetch,\n FetchClient as PublicFetchClient,\n FetchDefaults,\n FetchFunction,\n} from './types/public';\nimport { FetchRequestConstructor, FetchRequestInit, FetchRequest, FetchResponse } from './types/requests';\n\nclass FetchClient<Schema extends HttpSchema>\n implements Omit<PublicFetchClient<Schema>, 'defaults' | 'loose' | 'Request'>\n{\n fetch: Fetch<Schema>;\n\n constructor({ onRequest, onResponse, ...defaults }: FetchOptions<Schema>) {\n this.fetch = this.createFetchFunction();\n\n this.fetch.defaults = {\n ...defaults,\n headers: defaults.headers ?? {},\n searchParams: defaults.searchParams ?? {},\n };\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n this.fetch.loose = this.fetch as Fetch<any> as FetchFunction.Loose;\n\n this.fetch.Request = this.createRequestClass(this.fetch.defaults);\n this.fetch.onRequest = onRequest;\n this.fetch.onResponse = onResponse;\n }\n\n private createFetchFunction() {\n const fetch = async <\n Method extends HttpSchemaMethod<Schema>,\n Path extends HttpSchemaPath.NonLiteral<Schema, Method>,\n >(\n input: FetchInput<Schema, Method, Path>,\n init: FetchRequestInit<Schema, Method, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>>,\n ) => {\n const request = await this.createFetchRequest<Method, Path>(input, init);\n const requestClone = request.clone();\n\n const rawResponse = await globalThis.fetch(\n // Optimize type checking by narrowing the type of request\n requestClone as Request,\n );\n const response = await this.createFetchResponse<\n Method,\n LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>\n >(request, rawResponse);\n\n return response;\n };\n\n Object.setPrototypeOf(fetch, this);\n\n return fetch as Fetch<Schema>;\n }\n\n private async createFetchRequest<\n Method extends HttpSchemaMethod<Schema>,\n Path extends HttpSchemaPath.NonLiteral<Schema, Method>,\n >(\n input: FetchInput<Schema, Method, Path>,\n init: FetchRequestInit<Schema, Method, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>>,\n ) {\n let request = input instanceof Request ? input : new this.fetch.Request(input, init);\n\n if (this.fetch.onRequest) {\n const requestAfterInterceptor = await this.fetch.onRequest(\n // Optimize type checking by narrowing the type of request\n request as FetchRequest.Loose,\n );\n\n if (requestAfterInterceptor !== request) {\n const isFetchRequest = requestAfterInterceptor instanceof this.fetch.Request;\n\n request = isFetchRequest\n ? (requestAfterInterceptor as Request as typeof request)\n : new this.fetch.Request(requestAfterInterceptor as FetchInput<Schema, Method, Path>, init);\n }\n }\n\n return request;\n }\n\n private async createFetchResponse<\n Method extends HttpSchemaMethod<Schema>,\n Path extends HttpSchemaPath.Literal<Schema, Method>,\n >(fetchRequest: FetchRequest<Schema, Method, Path>, rawResponse: Response) {\n let response = this.defineFetchResponseProperties<Method, Path>(fetchRequest, rawResponse);\n\n if (this.fetch.onResponse) {\n const responseAfterInterceptor = await this.fetch.onResponse(\n // Optimize type checking by narrowing the type of response\n response as FetchResponse.Loose,\n );\n\n const isFetchResponse =\n responseAfterInterceptor instanceof Response &&\n 'request' in responseAfterInterceptor &&\n responseAfterInterceptor.request instanceof this.fetch.Request;\n\n response = isFetchResponse\n ? (responseAfterInterceptor as typeof response)\n : this.defineFetchResponseProperties<Method, Path>(fetchRequest, responseAfterInterceptor);\n }\n\n return response;\n }\n\n private defineFetchResponseProperties<\n Method extends HttpSchemaMethod<Schema>,\n Path extends HttpSchemaPath.Literal<Schema, Method>,\n >(fetchRequest: FetchRequest<Schema, Method, Path>, response: Response) {\n const fetchResponse = response as FetchResponse<Schema, Method, Path>;\n\n Object.defineProperty(fetchResponse, 'request', {\n value: fetchRequest satisfies FetchResponse.Loose['request'],\n writable: false,\n enumerable: true,\n configurable: false,\n });\n\n let responseError: FetchResponse.Loose['error'] | undefined;\n\n Object.defineProperty(fetchResponse, 'error', {\n get() {\n if (responseError === undefined) {\n responseError = fetchResponse.ok\n ? null\n : new FetchResponseError(\n fetchRequest,\n fetchResponse as FetchResponse<Schema, Method, Path, true, 'manual'>,\n );\n }\n return responseError;\n },\n enumerable: true,\n configurable: false,\n });\n\n return fetchResponse;\n }\n\n private createRequestClass(defaults: FetchDefaults) {\n class Request<\n Method extends HttpSchemaMethod<Schema>,\n Path extends HttpSchemaPath.NonLiteral<Schema, Method>,\n > extends globalThis.Request {\n path: LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>;\n\n constructor(\n input: FetchInput<Schema, Method, Path>,\n init: FetchRequestInit<Schema, Method, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>>,\n ) {\n const initWithDefaults = { ...defaults, ...init };\n\n const headersFromDefaults = new HttpHeaders(defaults.headers);\n const headersFromInit = new HttpHeaders((init satisfies RequestInit as RequestInit).headers);\n\n let url: URL;\n const baseURL = new URL(initWithDefaults.baseURL);\n\n if (input instanceof globalThis.Request) {\n // Optimize type checking by narrowing the type of input\n const request = input as globalThis.Request;\n const headersFromRequest = new HttpHeaders(input.headers);\n\n initWithDefaults.headers = {\n ...headersFromDefaults.toObject(),\n ...headersFromRequest.toObject(),\n ...headersFromInit.toObject(),\n };\n\n super(request, initWithDefaults);\n\n url = new URL(input.url);\n } else {\n initWithDefaults.headers = {\n ...headersFromDefaults.toObject(),\n ...headersFromInit.toObject(),\n };\n\n url = input instanceof URL ? new URL(input) : new URL(joinURL(baseURL, input));\n\n const searchParamsFromDefaults = new HttpSearchParams(defaults.searchParams);\n const searchParamsFromInit = new HttpSearchParams(initWithDefaults.searchParams);\n\n initWithDefaults.searchParams = {\n ...searchParamsFromDefaults.toObject(),\n ...searchParamsFromInit.toObject(),\n };\n\n url.search = new HttpSearchParams(initWithDefaults.searchParams).toString();\n\n super(url, initWithDefaults);\n }\n\n const baseURLWithoutTrailingSlash = baseURL.toString().replace(/\\/$/, '');\n\n this.path = excludeURLParams(url)\n .toString()\n .replace(baseURLWithoutTrailingSlash, '') as LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>;\n }\n\n clone(): Request<Method, Path> {\n const rawClone = super.clone();\n\n return new Request<Method, Path>(\n rawClone as unknown as FetchInput<Schema, Method, Path>,\n rawClone as unknown as FetchRequestInit<\n Schema,\n Method,\n LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>\n >,\n );\n }\n }\n\n return Request as FetchRequestConstructor<Schema>;\n }\n\n isRequest<Path extends HttpSchemaPath.Literal<Schema, Method>, Method extends HttpSchemaMethod<Schema>>(\n request: unknown,\n method: Method,\n path: Path,\n ): request is FetchRequest<Schema, Method, Path> {\n return (\n request instanceof Request &&\n request.method === method &&\n 'path' in request &&\n typeof request.path === 'string' &&\n createRegexFromURL(path).test(request.path)\n );\n }\n\n isResponse<Path extends HttpSchemaPath.Literal<Schema, Method>, Method extends HttpSchemaMethod<Schema>>(\n response: unknown,\n method: Method,\n path: Path,\n ): response is FetchResponse<Schema, Method, Path> {\n return (\n response instanceof Response &&\n 'request' in response &&\n this.isRequest(response.request, method, path) &&\n 'error' in response &&\n (response.error === null || response.error instanceof FetchResponseError)\n );\n }\n\n isResponseError<Path extends HttpSchemaPath.Literal<Schema, Method>, Method extends HttpSchemaMethod<Schema>>(\n error: unknown,\n method: Method,\n path: Path,\n ): error is FetchResponseError<Schema, Method, Path> {\n return (\n error instanceof FetchResponseError &&\n this.isRequest(error.request, method, path) &&\n this.isResponse(error.response, method, path)\n );\n }\n}\n\nexport default FetchClient;\n","import { HttpSchema } from '@zimic/http';\n\nimport FetchClient from './FetchClient';\nimport { FetchOptions, Fetch } from './types/public';\n\n/**\n * Creates a {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetch fetch instance} typed with an HTTP\n * schema, closely compatible with the {@link https://developer.mozilla.org/docs/Web/API/Fetch_API native Fetch API}. All\n * requests and responses are typed by default with the schema, including methods, paths, status codes, parameters, and\n * bodies.\n *\n * Requests sent by the fetch instance have their URL automatically prefixed with the base URL of the instance.\n * {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetch.defaults Default options} are also applied to the\n * requests, if provided.\n *\n * @example\n * import { type HttpSchema } from '@zimic/http';\n * import { createFetch } from '@zimic/fetch';\n *\n * interface User {\n * id: string;\n * username: string;\n * }\n *\n * type Schema = HttpSchema<{\n * '/users': {\n * POST: {\n * request: {\n * headers: { 'content-type': 'application/json' };\n * body: { username: string };\n * };\n * response: {\n * 201: { body: User };\n * };\n * };\n *\n * GET: {\n * request: {\n * searchParams: {\n * query?: string;\n * page?: number;\n * limit?: number;\n * };\n * };\n * response: {\n * 200: { body: User[] };\n * };\n * };\n * };\n * }>;\n *\n * const fetch = createFetch<Schema>({\n * baseURL: 'http://localhost:3000',\n * });\n *\n * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#createfetch `createFetch(options)` API reference}\n * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetch `fetch` API reference}\n */\nfunction createFetch<Schema extends HttpSchema>(options: FetchOptions<Schema>): Fetch<Schema> {\n const { fetch } = new FetchClient<Schema>(options);\n return fetch;\n}\n\nexport default createFetch;\n"]}
|