@openid4vc/utils 0.3.0-alpha-20251001121503 → 0.3.0-alpha-20251017082202

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/index.js CHANGED
@@ -1,511 +1,470 @@
1
- "use strict";
1
+ //#region rolldown:runtime
2
2
  var __create = Object.create;
3
3
  var __defProp = Object.defineProperty;
4
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
8
  var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
29
22
 
30
- // src/index.ts
31
- var index_exports = {};
32
- __export(index_exports, {
33
- ContentType: () => ContentType,
34
- Headers: () => _Headers,
35
- InvalidFetchResponseError: () => InvalidFetchResponseError,
36
- JsonParseError: () => JsonParseError,
37
- URL: () => _URL,
38
- URLSearchParams: () => _URLSearchParams,
39
- ValidationError: () => ValidationError,
40
- addSecondsToDate: () => addSecondsToDate,
41
- arrayEqualsIgnoreOrder: () => arrayEqualsIgnoreOrder,
42
- createFetcher: () => createFetcher,
43
- createZodFetcher: () => createZodFetcher,
44
- dateToSeconds: () => dateToSeconds,
45
- decodeBase64: () => decodeBase64,
46
- decodeUtf8String: () => decodeUtf8String,
47
- encodeToBase64: () => encodeToBase64,
48
- encodeToBase64Url: () => encodeToBase64Url,
49
- encodeToUtf8String: () => encodeToUtf8String,
50
- encodeWwwAuthenticateHeader: () => encodeWwwAuthenticateHeader,
51
- formatZodError: () => formatZodError,
52
- getGlobalConfig: () => getGlobalConfig,
53
- getQueryParams: () => getQueryParams,
54
- isContentType: () => isContentType,
55
- isObject: () => isObject,
56
- isResponseContentType: () => isResponseContentType,
57
- joinUriParts: () => joinUriParts,
58
- mergeDeep: () => mergeDeep,
59
- objectToQueryParams: () => objectToQueryParams,
60
- parseIfJson: () => parseIfJson,
61
- parseWithErrorHandling: () => parseWithErrorHandling,
62
- parseWwwAuthenticateHeader: () => parseWwwAuthenticateHeader,
63
- setGlobalConfig: () => setGlobalConfig,
64
- stringToJsonWithErrorHandling: () => stringToJsonWithErrorHandling,
65
- zHttpMethod: () => zHttpMethod,
66
- zHttpsUrl: () => zHttpsUrl,
67
- zInteger: () => zInteger,
68
- zIs: () => zIs,
69
- zStringToJson: () => zStringToJson
70
- });
71
- module.exports = __toCommonJS(index_exports);
23
+ //#endregion
24
+ let buffer = require("buffer");
25
+ buffer = __toESM(buffer);
26
+ let zod = require("zod");
27
+ zod = __toESM(zod);
28
+
29
+ //#region src/array.ts
30
+ /**
31
+ * Only primitive types allowed
32
+ * Must have not duplicate entries (will always return false in this case)
33
+ */
34
+ function arrayEqualsIgnoreOrder(a, b) {
35
+ if (new Set(a).size !== new Set(b).size) return false;
36
+ if (a.length !== b.length) return false;
37
+ return a.every((k) => b.includes(k));
38
+ }
72
39
 
73
- // src/globals.ts
74
- var _URL = URL;
75
- var _URLSearchParams = URLSearchParams;
76
- var _Headers = Headers;
40
+ //#endregion
41
+ //#region src/config.ts
42
+ let GLOBAL_CONFIG = { allowInsecureUrls: false };
43
+ function setGlobalConfig(config) {
44
+ GLOBAL_CONFIG = config;
45
+ }
46
+ function getGlobalConfig() {
47
+ return GLOBAL_CONFIG;
48
+ }
77
49
 
78
- // src/content-type.ts
79
- var ContentType = /* @__PURE__ */ ((ContentType2) => {
80
- ContentType2["XWwwFormUrlencoded"] = "application/x-www-form-urlencoded";
81
- ContentType2["Json"] = "application/json";
82
- ContentType2["JwkSet"] = "application/jwk-set+json";
83
- ContentType2["OAuthAuthorizationRequestJwt"] = "application/oauth-authz-req+jwt";
84
- ContentType2["Jwt"] = "application/jwt";
85
- ContentType2["Html"] = "text/html";
86
- return ContentType2;
87
- })(ContentType || {});
50
+ //#endregion
51
+ //#region src/content-type.ts
52
+ let ContentType = /* @__PURE__ */ function(ContentType$1) {
53
+ ContentType$1["XWwwFormUrlencoded"] = "application/x-www-form-urlencoded";
54
+ ContentType$1["Json"] = "application/json";
55
+ ContentType$1["JwkSet"] = "application/jwk-set+json";
56
+ ContentType$1["OAuthAuthorizationRequestJwt"] = "application/oauth-authz-req+jwt";
57
+ ContentType$1["Jwt"] = "application/jwt";
58
+ ContentType$1["Html"] = "text/html";
59
+ return ContentType$1;
60
+ }({});
88
61
  function isContentType(contentType, value) {
89
- return value.toLowerCase().includes(contentType);
62
+ return value.toLowerCase().includes(contentType);
90
63
  }
91
64
  function isResponseContentType(contentType, response) {
92
- const contentTypeArray = Array.isArray(contentType) ? contentType : [contentType];
93
- const header = response.headers.get("Content-Type");
94
- if (!header) return false;
95
- return contentTypeArray.some((contentTypeEntry) => isContentType(contentTypeEntry, header));
65
+ const contentTypeArray = Array.isArray(contentType) ? contentType : [contentType];
66
+ const header = response.headers.get("Content-Type");
67
+ if (!header) return false;
68
+ return contentTypeArray.some((contentTypeEntry) => isContentType(contentTypeEntry, header));
69
+ }
70
+
71
+ //#endregion
72
+ //#region src/date.ts
73
+ /**
74
+ * Get the time in seconds since epoch for a date.
75
+ * If date is not provided the current time will be used.
76
+ */
77
+ function dateToSeconds(date) {
78
+ const milliseconds = date?.getTime() ?? Date.now();
79
+ return Math.floor(milliseconds / 1e3);
80
+ }
81
+ function addSecondsToDate(date, seconds) {
82
+ return new Date(date.getTime() + seconds * 1e3);
96
83
  }
97
84
 
98
- // src/error/InvalidFetchResponseError.ts
85
+ //#endregion
86
+ //#region src/encoding.ts
87
+ function decodeUtf8String(string) {
88
+ return new Uint8Array(buffer.Buffer.from(string, "utf-8"));
89
+ }
90
+ function encodeToUtf8String(data) {
91
+ return buffer.Buffer.from(data).toString("utf-8");
92
+ }
93
+ /**
94
+ * Also supports base64 url
95
+ */
96
+ function decodeBase64(base64) {
97
+ return new Uint8Array(buffer.Buffer.from(base64, "base64"));
98
+ }
99
+ function encodeToBase64(data) {
100
+ if (typeof data === "string") return buffer.Buffer.from(data).toString("base64");
101
+ return buffer.Buffer.from(data).toString("base64");
102
+ }
103
+ function encodeToBase64Url(data) {
104
+ return base64ToBase64Url(encodeToBase64(data));
105
+ }
106
+ /**
107
+ * The 'buffer' npm library does not support base64url.
108
+ */
109
+ function base64ToBase64Url(base64) {
110
+ return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
111
+ }
112
+
113
+ //#endregion
114
+ //#region src/error/InvalidFetchResponseError.ts
99
115
  var InvalidFetchResponseError = class extends Error {
100
- constructor(message, textResponse, response) {
101
- const textResponseMessage = isResponseContentType("text/html" /* Html */, response) ? void 0 : textResponse.substring(0, 1e3);
102
- super(textResponseMessage ? `${message}
103
- ${textResponseMessage}` : message);
104
- this.textResponse = textResponse;
105
- this.response = response.clone();
106
- }
116
+ constructor(message, textResponse, response) {
117
+ const textResponseMessage = isResponseContentType(ContentType.Html, response) ? void 0 : textResponse.substring(0, 1e3);
118
+ super(textResponseMessage ? `${message}\n${textResponseMessage}` : message);
119
+ this.textResponse = textResponse;
120
+ this.response = response.clone();
121
+ }
107
122
  };
108
123
 
109
- // src/error/JsonParseError.ts
124
+ //#endregion
125
+ //#region src/error/JsonParseError.ts
110
126
  var JsonParseError = class extends Error {
111
- constructor(message, jsonString) {
112
- super(`${message}
113
- ${jsonString}`);
114
- }
127
+ constructor(message, jsonString) {
128
+ super(`${message}\n${jsonString}`);
129
+ }
115
130
  };
116
131
 
117
- // src/zod-error.ts
118
- var import_zod = require("zod");
119
- var constants = {
120
- // biome-ignore lint/suspicious/noMisleadingCharacterClass: expected
121
- identifierRegex: /[$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*/u,
122
- unionSeparator: ", or ",
123
- issueSeparator: "\n - "
132
+ //#endregion
133
+ //#region src/zod-error.ts
134
+ /**
135
+ * Some code comes from `zod-validation-error` package (MIT License) and
136
+ * was slightly simplified to fit our needs.
137
+ */
138
+ const constants = {
139
+ identifierRegex: /[$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*/u,
140
+ unionSeparator: ", or ",
141
+ issueSeparator: "\n - "
124
142
  };
125
143
  function escapeQuotes(str) {
126
- return str.replace(/"/g, '\\"');
144
+ return str.replace(/"/g, "\\\"");
127
145
  }
128
146
  function joinPath(path) {
129
- if (path.length === 1) {
130
- return path[0].toString();
131
- }
132
- return path.reduce((acc, item) => {
133
- if (typeof item === "number") {
134
- return `${acc}[${item.toString()}]`;
135
- }
136
- if (item.includes('"')) {
137
- return `${acc}["${escapeQuotes(item)}"]`;
138
- }
139
- if (!constants.identifierRegex.test(item)) {
140
- return `${acc}["${item}"]`;
141
- }
142
- const separator = acc.length === 0 ? "" : ".";
143
- return acc + separator + item;
144
- }, "");
147
+ if (path.length === 1) return path[0].toString();
148
+ return path.reduce((acc, item) => {
149
+ if (typeof item === "number") return `${acc}[${item.toString()}]`;
150
+ if (item.includes("\"")) return `${acc}["${escapeQuotes(item)}"]`;
151
+ if (!constants.identifierRegex.test(item)) return `${acc}["${item}"]`;
152
+ return acc + (acc.length === 0 ? "" : ".") + item;
153
+ }, "");
145
154
  }
146
155
  function getMessageFromZodIssue(issue) {
147
- if (issue.code === import_zod.ZodIssueCode.invalid_union) {
148
- return getMessageFromUnionErrors(issue.unionErrors);
149
- }
150
- if (issue.code === import_zod.ZodIssueCode.invalid_arguments) {
151
- return [issue.message, ...issue.argumentsError.issues.map((issue2) => getMessageFromZodIssue(issue2))].join(
152
- constants.issueSeparator
153
- );
154
- }
155
- if (issue.code === import_zod.ZodIssueCode.invalid_return_type) {
156
- return [issue.message, ...issue.returnTypeError.issues.map((issue2) => getMessageFromZodIssue(issue2))].join(
157
- constants.issueSeparator
158
- );
159
- }
160
- if (issue.path.length !== 0) {
161
- if (issue.path.length === 1) {
162
- const identifier = issue.path[0];
163
- if (typeof identifier === "number") {
164
- return `${issue.message} at index ${identifier}`;
165
- }
166
- }
167
- return `${issue.message} at "${joinPath(issue.path)}"`;
168
- }
169
- return issue.message;
156
+ if (issue.code === zod.ZodIssueCode.invalid_union) return getMessageFromUnionErrors(issue.unionErrors);
157
+ if (issue.code === zod.ZodIssueCode.invalid_arguments) return [issue.message, ...issue.argumentsError.issues.map((issue$1) => getMessageFromZodIssue(issue$1))].join(constants.issueSeparator);
158
+ if (issue.code === zod.ZodIssueCode.invalid_return_type) return [issue.message, ...issue.returnTypeError.issues.map((issue$1) => getMessageFromZodIssue(issue$1))].join(constants.issueSeparator);
159
+ if (issue.path.length !== 0) {
160
+ if (issue.path.length === 1) {
161
+ const identifier = issue.path[0];
162
+ if (typeof identifier === "number") return `${issue.message} at index ${identifier}`;
163
+ }
164
+ return `${issue.message} at "${joinPath(issue.path)}"`;
165
+ }
166
+ return issue.message;
170
167
  }
171
168
  function getMessageFromUnionErrors(unionErrors) {
172
- return unionErrors.reduce((acc, zodError) => {
173
- const newIssues = zodError.issues.map((issue) => getMessageFromZodIssue(issue)).join(constants.issueSeparator);
174
- if (!acc.includes(newIssues)) acc.push(newIssues);
175
- return acc;
176
- }, []).join(constants.unionSeparator);
169
+ return unionErrors.reduce((acc, zodError) => {
170
+ const newIssues = zodError.issues.map((issue) => getMessageFromZodIssue(issue)).join(constants.issueSeparator);
171
+ if (!acc.includes(newIssues)) acc.push(newIssues);
172
+ return acc;
173
+ }, []).join(constants.unionSeparator);
177
174
  }
178
175
  function formatZodError(error) {
179
- if (!error) return "";
180
- return ` - ${error?.issues.map((issue) => getMessageFromZodIssue(issue)).join(constants.issueSeparator)}`;
176
+ if (!error) return "";
177
+ return `\t- ${error?.issues.map((issue) => getMessageFromZodIssue(issue)).join(constants.issueSeparator)}`;
181
178
  }
182
179
 
183
- // src/error/ValidationError.ts
180
+ //#endregion
181
+ //#region src/error/ValidationError.ts
184
182
  var ValidationError = class extends Error {
185
- constructor(message, zodError) {
186
- super(message);
187
- const formattedError = formatZodError(zodError);
188
- this.message = `${message}
189
- ${formattedError}`;
190
- Object.defineProperty(this, "zodError", {
191
- value: zodError,
192
- writable: false,
193
- enumerable: false
194
- });
195
- }
183
+ constructor(message, zodError) {
184
+ super(message);
185
+ this.message = `${message}\n${formatZodError(zodError)}`;
186
+ Object.defineProperty(this, "zodError", {
187
+ value: zodError,
188
+ writable: false,
189
+ enumerable: false
190
+ });
191
+ }
196
192
  };
197
193
 
198
- // src/array.ts
199
- function arrayEqualsIgnoreOrder(a, b) {
200
- if (new Set(a).size !== new Set(b).size) return false;
201
- if (a.length !== b.length) return false;
202
- return a.every((k) => b.includes(k));
203
- }
204
-
205
- // src/config.ts
206
- var GLOBAL_CONFIG = {
207
- allowInsecureUrls: false
194
+ //#endregion
195
+ //#region src/error/FetchError.ts
196
+ var FetchError = class extends Error {
197
+ constructor(message, { cause } = {}) {
198
+ super(`${message}\nCause: ${cause?.message}`);
199
+ this.cause = cause;
200
+ }
208
201
  };
209
- function setGlobalConfig(config) {
210
- GLOBAL_CONFIG = config;
211
- }
212
- function getGlobalConfig() {
213
- return GLOBAL_CONFIG;
214
- }
215
202
 
216
- // src/date.ts
217
- function dateToSeconds(date) {
218
- const milliseconds = date?.getTime() ?? Date.now();
219
- return Math.floor(milliseconds / 1e3);
220
- }
221
- function addSecondsToDate(date, seconds) {
222
- return new Date(date.getTime() + seconds * 1e3);
223
- }
203
+ //#endregion
204
+ //#region src/globals.ts
205
+ const _URL = URL;
206
+ const _URLSearchParams = URLSearchParams;
207
+ const _Headers = Headers;
224
208
 
225
- // src/encoding.ts
226
- var import_buffer = require("buffer");
227
- function decodeUtf8String(string) {
228
- return new Uint8Array(import_buffer.Buffer.from(string, "utf-8"));
229
- }
230
- function encodeToUtf8String(data) {
231
- return import_buffer.Buffer.from(data).toString("utf-8");
232
- }
233
- function decodeBase64(base64) {
234
- return new Uint8Array(import_buffer.Buffer.from(base64, "base64"));
235
- }
236
- function encodeToBase64(data) {
237
- if (typeof data === "string") {
238
- return import_buffer.Buffer.from(data).toString("base64");
239
- }
240
- return import_buffer.Buffer.from(data).toString("base64");
241
- }
242
- function encodeToBase64Url(data) {
243
- return base64ToBase64Url(encodeToBase64(data));
209
+ //#endregion
210
+ //#region src/fetcher.ts
211
+ /**
212
+ * The default fetcher used by createZodFetcher when no
213
+ * fetcher is provided.
214
+ */
215
+ const defaultFetcher = fetch;
216
+ function createFetcher(fetcher = defaultFetcher) {
217
+ return (input, init, ...args) => {
218
+ return fetcher(input, init ? {
219
+ ...init,
220
+ body: init.body instanceof _URLSearchParams ? init.body.toString() : init.body
221
+ } : void 0, ...args).catch((error) => {
222
+ throw new FetchError(`Unknown error occurred during fetch to '${input}'`, { cause: error });
223
+ });
224
+ };
244
225
  }
245
- function base64ToBase64Url(base64) {
246
- return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
226
+ /**
227
+ * Creates a `fetchWithZod` function that takes in a schema of
228
+ * the expected response, and the arguments to the fetcher
229
+ * you provided.
230
+ *
231
+ * @example
232
+ *
233
+ * const fetchWithZod = createZodFetcher((url) => {
234
+ * return fetch(url).then((res) => res.json());
235
+ * });
236
+ *
237
+ * const response = await fetchWithZod(
238
+ * z.object({
239
+ * hello: z.string(),
240
+ * }),
241
+ * "https://example.com",
242
+ * );
243
+ */
244
+ function createZodFetcher(fetcher) {
245
+ return async (schema, expectedContentType, ...args) => {
246
+ const response = await createFetcher(fetcher)(...args);
247
+ const expectedContentTypeArray = Array.isArray(expectedContentType) ? expectedContentType : [expectedContentType];
248
+ if (response.ok && !isResponseContentType(expectedContentTypeArray, response)) throw new InvalidFetchResponseError(`Expected response to match content type ${expectedContentTypeArray.join(" | ")}, but received '${response.headers.get("Content-Type")}'`, await response.clone().text(), response);
249
+ if (expectedContentTypeArray.includes(ContentType.OAuthAuthorizationRequestJwt)) return {
250
+ response,
251
+ result: response.ok ? schema.safeParse(await response.text()) : void 0
252
+ };
253
+ return {
254
+ response,
255
+ result: response.ok ? schema.safeParse(await response.json()) : void 0
256
+ };
257
+ };
247
258
  }
248
259
 
249
- // src/object.ts
260
+ //#endregion
261
+ //#region src/object.ts
250
262
  function isObject(item) {
251
- return item != null && typeof item === "object" && !Array.isArray(item);
263
+ return item != null && typeof item === "object" && !Array.isArray(item);
252
264
  }
265
+ /**
266
+ * Deep merge two objects.
267
+ * @param target
268
+ * @param ...sources
269
+ */
253
270
  function mergeDeep(target, ...sources) {
254
- if (!sources.length) return target;
255
- const source = sources.shift();
256
- if (isObject(target) && isObject(source)) {
257
- for (const key in source) {
258
- if (isObject(source[key])) {
259
- if (!target[key]) Object.assign(target, { [key]: {} });
260
- mergeDeep(target[key], source[key]);
261
- } else {
262
- Object.assign(target, { [key]: source[key] });
263
- }
264
- }
265
- }
266
- return mergeDeep(target, ...sources);
271
+ if (!sources.length) return target;
272
+ const source = sources.shift();
273
+ if (isObject(target) && isObject(source)) for (const key in source) if (isObject(source[key])) {
274
+ if (!target[key]) Object.assign(target, { [key]: {} });
275
+ mergeDeep(target[key], source[key]);
276
+ } else Object.assign(target, { [key]: source[key] });
277
+ return mergeDeep(target, ...sources);
267
278
  }
268
279
 
269
- // src/parse.ts
280
+ //#endregion
281
+ //#region src/parse.ts
270
282
  function stringToJsonWithErrorHandling(string, errorMessage) {
271
- try {
272
- return JSON.parse(string);
273
- } catch (error) {
274
- throw new JsonParseError(errorMessage ?? "Unable to parse string to JSON.", string);
275
- }
283
+ try {
284
+ return JSON.parse(string);
285
+ } catch (_error) {
286
+ throw new JsonParseError(errorMessage ?? "Unable to parse string to JSON.", string);
287
+ }
276
288
  }
277
289
  function parseIfJson(data) {
278
- if (typeof data !== "string") {
279
- return data;
280
- }
281
- try {
282
- return JSON.parse(data);
283
- } catch (error) {
284
- }
285
- return data;
290
+ if (typeof data !== "string") return data;
291
+ try {
292
+ return JSON.parse(data);
293
+ } catch (_error) {}
294
+ return data;
286
295
  }
287
296
  function parseWithErrorHandling(schema, data, customErrorMessage) {
288
- const parseResult = schema.safeParse(data);
289
- if (!parseResult.success) {
290
- throw new ValidationError(
291
- customErrorMessage ?? `Error validating schema with data ${JSON.stringify(data)}`,
292
- parseResult.error
293
- );
294
- }
295
- return parseResult.data;
297
+ const parseResult = schema.safeParse(data);
298
+ if (!parseResult.success) throw new ValidationError(customErrorMessage ?? `Error validating schema with data ${JSON.stringify(data)}`, parseResult.error);
299
+ return parseResult.data;
296
300
  }
297
301
 
298
- // src/path.ts
302
+ //#endregion
303
+ //#region src/path.ts
304
+ /**
305
+ * Combine multiple uri parts into a single uri taking into account slashes.
306
+ *
307
+ * @param parts the parts to combine
308
+ * @returns the combined url
309
+ */
299
310
  function joinUriParts(base, parts) {
300
- if (parts.length === 0) return base;
301
- let combined = base.trim();
302
- combined = base.endsWith("/") ? base.slice(0, base.length - 1) : base;
303
- for (const part of parts) {
304
- let strippedPart = part.trim();
305
- strippedPart = strippedPart.startsWith("/") ? strippedPart.slice(1) : strippedPart;
306
- strippedPart = strippedPart.endsWith("/") ? strippedPart.slice(0, strippedPart.length - 1) : strippedPart;
307
- if (strippedPart === "") continue;
308
- combined += `/${strippedPart}`;
309
- }
310
- return combined;
311
+ if (parts.length === 0) return base;
312
+ let combined = base.trim();
313
+ combined = base.endsWith("/") ? base.slice(0, base.length - 1) : base;
314
+ for (const part of parts) {
315
+ let strippedPart = part.trim();
316
+ strippedPart = strippedPart.startsWith("/") ? strippedPart.slice(1) : strippedPart;
317
+ strippedPart = strippedPart.endsWith("/") ? strippedPart.slice(0, strippedPart.length - 1) : strippedPart;
318
+ if (strippedPart === "") continue;
319
+ combined += `/${strippedPart}`;
320
+ }
321
+ return combined;
311
322
  }
312
323
 
313
- // src/url.ts
324
+ //#endregion
325
+ //#region src/url.ts
314
326
  function getQueryParams(url) {
315
- const parsedUrl = new _URL(url);
316
- const searchParams = new _URLSearchParams(parsedUrl.search);
317
- const params = {};
318
- searchParams.forEach((value, key) => {
319
- params[key] = value;
320
- });
321
- return params;
327
+ const searchParams = new _URLSearchParams(new _URL(url).search);
328
+ const params = {};
329
+ searchParams.forEach((value, key) => {
330
+ params[key] = value;
331
+ });
332
+ return params;
322
333
  }
323
334
  function objectToQueryParams(object) {
324
- const params = new _URLSearchParams();
325
- for (const [key, value] of Object.entries(object)) {
326
- if (value != null) {
327
- params.append(key, typeof value === "object" ? JSON.stringify(value) : String(value));
328
- }
329
- }
330
- return params;
335
+ const params = new _URLSearchParams();
336
+ for (const [key, value] of Object.entries(object)) if (value != null) params.append(key, typeof value === "object" ? JSON.stringify(value) : String(value));
337
+ return params;
331
338
  }
332
339
 
333
- // src/error/FetchError.ts
334
- var FetchError = class extends Error {
335
- constructor(message, { cause } = {}) {
336
- super(`${message}
337
- Cause: ${cause?.message}`);
338
- this.cause = cause;
339
- }
340
- };
341
-
342
- // src/fetcher.ts
343
- var defaultFetcher = fetch;
344
- function createFetcher(fetcher = defaultFetcher) {
345
- return (input, init, ...args) => {
346
- return fetcher(
347
- input,
348
- init ? {
349
- ...init,
350
- // React Native does not seem to handle the toString(). This is hard to catch when running
351
- // tests in Node.JS where this does work correctly. so we handle it here.
352
- body: init.body instanceof _URLSearchParams ? init.body.toString() : init.body
353
- } : void 0,
354
- ...args
355
- ).catch((error) => {
356
- throw new FetchError(`Unknown error occurred during fetch to '${input}'`, { cause: error });
357
- });
358
- };
359
- }
360
- function createZodFetcher(fetcher) {
361
- return async (schema, expectedContentType, ...args) => {
362
- const response = await createFetcher(fetcher)(...args);
363
- const expectedContentTypeArray = Array.isArray(expectedContentType) ? expectedContentType : [expectedContentType];
364
- if (response.ok && !isResponseContentType(expectedContentTypeArray, response)) {
365
- throw new InvalidFetchResponseError(
366
- `Expected response to match content type ${expectedContentTypeArray.join(" | ")}, but received '${response.headers.get("Content-Type")}'`,
367
- await response.clone().text(),
368
- response
369
- );
370
- }
371
- if (expectedContentTypeArray.includes("application/oauth-authz-req+jwt" /* OAuthAuthorizationRequestJwt */)) {
372
- return {
373
- response,
374
- result: response.ok ? schema.safeParse(await response.text()) : void 0
375
- };
376
- }
377
- return {
378
- response,
379
- result: response.ok ? schema.safeParse(await response.json()) : void 0
380
- };
381
- };
382
- }
383
-
384
- // src/validation.ts
385
- var import_zod2 = __toESM(require("zod"));
386
- var zHttpsUrl = import_zod2.default.string().url().refine(
387
- (url) => {
388
- const { allowInsecureUrls } = getGlobalConfig();
389
- return allowInsecureUrls ? url.startsWith("http://") || url.startsWith("https://") : url.startsWith("https://");
390
- },
391
- { message: "url must be an https:// url" }
392
- );
393
- var zInteger = import_zod2.default.number().int();
394
- var zHttpMethod = import_zod2.default.enum(["GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "TRACE", "CONNECT", "PATCH"]);
395
- var zStringToJson = import_zod2.default.string().transform((string, ctx) => {
396
- try {
397
- return JSON.parse(string);
398
- } catch (error) {
399
- ctx.addIssue({
400
- code: "custom",
401
- message: "Expected a JSON string, but could not parse the string to JSON"
402
- });
403
- return import_zod2.default.NEVER;
404
- }
340
+ //#endregion
341
+ //#region src/validation.ts
342
+ const zHttpsUrl = zod.default.string().url().refine((url) => {
343
+ const { allowInsecureUrls } = getGlobalConfig();
344
+ return allowInsecureUrls ? url.startsWith("http://") || url.startsWith("https://") : url.startsWith("https://");
345
+ }, { message: "url must be an https:// url" });
346
+ const zInteger = zod.default.number().int();
347
+ const zHttpMethod = zod.default.enum([
348
+ "GET",
349
+ "POST",
350
+ "PUT",
351
+ "DELETE",
352
+ "HEAD",
353
+ "OPTIONS",
354
+ "TRACE",
355
+ "CONNECT",
356
+ "PATCH"
357
+ ]);
358
+ const zStringToJson = zod.default.string().transform((string, ctx) => {
359
+ try {
360
+ return JSON.parse(string);
361
+ } catch (_error) {
362
+ ctx.addIssue({
363
+ code: "custom",
364
+ message: "Expected a JSON string, but could not parse the string to JSON"
365
+ });
366
+ return zod.default.NEVER;
367
+ }
405
368
  });
406
- var zIs = (schema, data) => schema.safeParse(data).success;
369
+ const zIs = (schema, data) => schema.safeParse(data).success;
407
370
 
408
- // src/www-authenticate.ts
409
- var unquote = (value) => value.substring(1, value.length - 1).replace(/\\"/g, '"');
410
- var sanitize = (value) => value.charAt(0) === '"' ? unquote(value) : value.trim();
411
- var body = (
412
- // biome-ignore lint/suspicious/noControlCharactersInRegex: <explanation>
413
- /((?:[a-zA-Z0-9._~+\/-]+=*(?:\s+|$))|[^\u0000-\u001F\u007F()<>@,;:\\"/?={}\[\]\u0020\u0009]+)(?:=([^\\"=\s,]+|"(?:[^"\\]|\\.)*"))?/g
414
- );
415
- var parsePayload = (scheme, string) => {
416
- const payload = {};
417
- while (true) {
418
- const res = body.exec(string);
419
- if (!res) break;
420
- const [, key, newValue] = res;
421
- const payloadValue = payload[key];
422
- if (newValue) {
423
- const sanitizedValue = sanitize(newValue);
424
- payload[key] = payloadValue ? Array.isArray(payloadValue) ? [...payloadValue, sanitizedValue] : [payloadValue, sanitizedValue] : sanitizedValue;
425
- } else if (!payloadValue) {
426
- payload[key] = null;
427
- }
428
- }
429
- return { scheme, payload };
371
+ //#endregion
372
+ //#region src/www-authenticate.ts
373
+ const unquote = (value) => value.substring(1, value.length - 1).replace(/\\"/g, "\"");
374
+ const sanitize = (value) => value.charAt(0) === "\"" ? unquote(value) : value.trim();
375
+ const body = /((?:[a-zA-Z0-9._~+/-]+=*(?:\s+|$))|[^\u0000-\u001F\u007F()<>@,;:\\"/?={}[\]\u0020\u0009]+)(?:=([^\\"=\s,]+|"(?:[^"\\]|\\.)*"))?/g;
376
+ const parsePayload = (scheme, string) => {
377
+ const payload = {};
378
+ while (true) {
379
+ const res = body.exec(string);
380
+ if (!res) break;
381
+ const [, key, newValue] = res;
382
+ const payloadValue = payload[key];
383
+ if (newValue) {
384
+ const sanitizedValue = sanitize(newValue);
385
+ payload[key] = payloadValue ? Array.isArray(payloadValue) ? [...payloadValue, sanitizedValue] : [payloadValue, sanitizedValue] : sanitizedValue;
386
+ } else if (!payloadValue) payload[key] = null;
387
+ }
388
+ return {
389
+ scheme,
390
+ payload
391
+ };
430
392
  };
431
393
  function parseWwwAuthenticateHeader(str) {
432
- const start = str.indexOf(" ");
433
- let scheme = str.substring(0, start);
434
- let value = str.substring(start);
435
- const challenges = [];
436
- const endsWithSchemeRegex = /, ?(Bearer|DPoP|Basic)$/;
437
- const endsWithSchemeTest = endsWithSchemeRegex.exec(value);
438
- let endsWithScheme = void 0;
439
- if (endsWithSchemeTest) {
440
- value = value.substring(0, value.length - endsWithSchemeTest[0].length);
441
- endsWithScheme = endsWithSchemeTest[1];
442
- }
443
- const additionalSchemesRegex = /(.*?)(, ?)(Bearer|DPoP|Basic)[, ]/;
444
- let match = additionalSchemesRegex.exec(value);
445
- while (match) {
446
- challenges.push(parsePayload(scheme, match[1]));
447
- value = value.substring(match[0].length - 1);
448
- scheme = match[3];
449
- match = additionalSchemesRegex.exec(value);
450
- }
451
- challenges.push(parsePayload(scheme, value));
452
- if (endsWithScheme) {
453
- challenges.push({ scheme: endsWithScheme, payload: {} });
454
- }
455
- return challenges;
394
+ const start = str.indexOf(" ");
395
+ let scheme = str.substring(0, start);
396
+ let value = str.substring(start);
397
+ const challenges = [];
398
+ const endsWithSchemeTest = /, ?(Bearer|DPoP|Basic)$/.exec(value);
399
+ let endsWithScheme;
400
+ if (endsWithSchemeTest) {
401
+ value = value.substring(0, value.length - endsWithSchemeTest[0].length);
402
+ endsWithScheme = endsWithSchemeTest[1];
403
+ }
404
+ const additionalSchemesRegex = /(.*?)(, ?)(Bearer|DPoP|Basic)[, ]/;
405
+ let match = additionalSchemesRegex.exec(value);
406
+ while (match) {
407
+ challenges.push(parsePayload(scheme, match[1]));
408
+ value = value.substring(match[0].length - 1);
409
+ scheme = match[3];
410
+ match = additionalSchemesRegex.exec(value);
411
+ }
412
+ challenges.push(parsePayload(scheme, value));
413
+ if (endsWithScheme) challenges.push({
414
+ scheme: endsWithScheme,
415
+ payload: {}
416
+ });
417
+ return challenges;
456
418
  }
457
419
  function encodeWwwAuthenticateHeader(challenges) {
458
- const entries = [];
459
- for (const challenge of challenges) {
460
- const encodedParams = Object.entries(challenge.payload).flatMap(([key, value]) => {
461
- const encode = (s) => s.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
462
- if (Array.isArray(value)) {
463
- return value.map((v) => `${key}="${encode(v)}"`);
464
- }
465
- return value ? `${key}="${encode(value)}"` : key;
466
- });
467
- entries.push(encodedParams.length === 0 ? challenge.scheme : `${challenge.scheme} ${encodedParams.join(", ")}`);
468
- }
469
- return entries.join(", ");
420
+ const entries = [];
421
+ for (const challenge of challenges) {
422
+ const encodedParams = Object.entries(challenge.payload).flatMap(([key, value]) => {
423
+ const encode = (s) => s.replace(/\\/g, "\\\\").replace(/"/g, "\\\"");
424
+ if (Array.isArray(value)) return value.map((v) => `${key}="${encode(v)}"`);
425
+ return value ? `${key}="${encode(value)}"` : key;
426
+ });
427
+ entries.push(encodedParams.length === 0 ? challenge.scheme : `${challenge.scheme} ${encodedParams.join(", ")}`);
428
+ }
429
+ return entries.join(", ");
470
430
  }
471
- // Annotate the CommonJS export names for ESM import in node:
472
- 0 && (module.exports = {
473
- ContentType,
474
- Headers,
475
- InvalidFetchResponseError,
476
- JsonParseError,
477
- URL,
478
- URLSearchParams,
479
- ValidationError,
480
- addSecondsToDate,
481
- arrayEqualsIgnoreOrder,
482
- createFetcher,
483
- createZodFetcher,
484
- dateToSeconds,
485
- decodeBase64,
486
- decodeUtf8String,
487
- encodeToBase64,
488
- encodeToBase64Url,
489
- encodeToUtf8String,
490
- encodeWwwAuthenticateHeader,
491
- formatZodError,
492
- getGlobalConfig,
493
- getQueryParams,
494
- isContentType,
495
- isObject,
496
- isResponseContentType,
497
- joinUriParts,
498
- mergeDeep,
499
- objectToQueryParams,
500
- parseIfJson,
501
- parseWithErrorHandling,
502
- parseWwwAuthenticateHeader,
503
- setGlobalConfig,
504
- stringToJsonWithErrorHandling,
505
- zHttpMethod,
506
- zHttpsUrl,
507
- zInteger,
508
- zIs,
509
- zStringToJson
510
- });
431
+
432
+ //#endregion
433
+ exports.ContentType = ContentType;
434
+ exports.Headers = _Headers;
435
+ exports.InvalidFetchResponseError = InvalidFetchResponseError;
436
+ exports.JsonParseError = JsonParseError;
437
+ exports.URL = _URL;
438
+ exports.URLSearchParams = _URLSearchParams;
439
+ exports.ValidationError = ValidationError;
440
+ exports.addSecondsToDate = addSecondsToDate;
441
+ exports.arrayEqualsIgnoreOrder = arrayEqualsIgnoreOrder;
442
+ exports.createFetcher = createFetcher;
443
+ exports.createZodFetcher = createZodFetcher;
444
+ exports.dateToSeconds = dateToSeconds;
445
+ exports.decodeBase64 = decodeBase64;
446
+ exports.decodeUtf8String = decodeUtf8String;
447
+ exports.encodeToBase64 = encodeToBase64;
448
+ exports.encodeToBase64Url = encodeToBase64Url;
449
+ exports.encodeToUtf8String = encodeToUtf8String;
450
+ exports.encodeWwwAuthenticateHeader = encodeWwwAuthenticateHeader;
451
+ exports.formatZodError = formatZodError;
452
+ exports.getGlobalConfig = getGlobalConfig;
453
+ exports.getQueryParams = getQueryParams;
454
+ exports.isContentType = isContentType;
455
+ exports.isObject = isObject;
456
+ exports.isResponseContentType = isResponseContentType;
457
+ exports.joinUriParts = joinUriParts;
458
+ exports.mergeDeep = mergeDeep;
459
+ exports.objectToQueryParams = objectToQueryParams;
460
+ exports.parseIfJson = parseIfJson;
461
+ exports.parseWithErrorHandling = parseWithErrorHandling;
462
+ exports.parseWwwAuthenticateHeader = parseWwwAuthenticateHeader;
463
+ exports.setGlobalConfig = setGlobalConfig;
464
+ exports.stringToJsonWithErrorHandling = stringToJsonWithErrorHandling;
465
+ exports.zHttpMethod = zHttpMethod;
466
+ exports.zHttpsUrl = zHttpsUrl;
467
+ exports.zInteger = zInteger;
468
+ exports.zIs = zIs;
469
+ exports.zStringToJson = zStringToJson;
511
470
  //# sourceMappingURL=index.js.map