@openid4vc/utils 0.3.0-alpha-20251001121503 → 0.3.0-alpha-20251017092354
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.d.mts +121 -105
- package/dist/index.d.ts +121 -105
- package/dist/index.js +399 -440
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +348 -377
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,511 +1,470 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
-
|
|
62
|
+
return value.toLowerCase().includes(contentType);
|
|
90
63
|
}
|
|
91
64
|
function isResponseContentType(contentType, response) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
-
|
|
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
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
124
|
+
//#endregion
|
|
125
|
+
//#region src/error/JsonParseError.ts
|
|
110
126
|
var JsonParseError = class extends Error {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
127
|
+
constructor(message, jsonString) {
|
|
128
|
+
super(`${message}\n${jsonString}`);
|
|
129
|
+
}
|
|
115
130
|
};
|
|
116
131
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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
|
-
|
|
144
|
+
return str.replace(/"/g, "\\\"");
|
|
127
145
|
}
|
|
128
146
|
function joinPath(path) {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
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
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
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
|
-
|
|
180
|
-
|
|
176
|
+
if (!error) return "";
|
|
177
|
+
return `\t- ${error?.issues.map((issue) => getMessageFromZodIssue(issue)).join(constants.issueSeparator)}`;
|
|
181
178
|
}
|
|
182
179
|
|
|
183
|
-
|
|
180
|
+
//#endregion
|
|
181
|
+
//#region src/error/ValidationError.ts
|
|
184
182
|
var ValidationError = class extends Error {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
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
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
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
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
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
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
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
|
-
|
|
246
|
-
|
|
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
|
-
|
|
260
|
+
//#endregion
|
|
261
|
+
//#region src/object.ts
|
|
250
262
|
function isObject(item) {
|
|
251
|
-
|
|
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
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
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
|
-
|
|
280
|
+
//#endregion
|
|
281
|
+
//#region src/parse.ts
|
|
270
282
|
function stringToJsonWithErrorHandling(string, errorMessage) {
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
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
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
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
|
-
|
|
289
|
-
|
|
290
|
-
|
|
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
|
-
|
|
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
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
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
|
-
|
|
324
|
+
//#endregion
|
|
325
|
+
//#region src/url.ts
|
|
314
326
|
function getQueryParams(url) {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
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
|
-
|
|
325
|
-
|
|
326
|
-
|
|
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
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
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
|
-
|
|
369
|
+
const zIs = (schema, data) => schema.safeParse(data).success;
|
|
407
370
|
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
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
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
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
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
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
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
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
|