@adobe/aio-commerce-lib-core 0.6.1 → 1.1.0
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/CHANGELOG.md +16 -0
- package/README.md +1 -4
- package/dist/cjs/error/index.cjs +19 -1
- package/dist/cjs/error/index.d.cts +14 -0
- package/dist/cjs/error-QC7QKZdI.cjs +228 -0
- package/dist/cjs/headers/index.cjs +462 -1
- package/dist/cjs/headers/index.d.cts +15 -2
- package/dist/cjs/{index-2lCdMqhT.d.cts → index-Ddzq9z7b.d.cts} +18 -9
- package/dist/cjs/params/index.cjs +41 -1
- package/dist/cjs/params/index.d.cts +15 -1
- package/dist/cjs/responses/index.cjs +209 -1
- package/dist/cjs/responses/index.d.cts +14 -0
- package/dist/es/error/index.d.mts +14 -0
- package/dist/es/error/index.mjs +17 -1
- package/dist/es/error-dCuAZfGs.mjs +183 -0
- package/dist/es/headers/index.d.mts +15 -2
- package/dist/es/headers/index.mjs +449 -1
- package/dist/es/{index-CPYhPt8X.d.mts → index-2lCs6JZP.d.mts} +18 -9
- package/dist/es/params/index.d.mts +15 -1
- package/dist/es/params/index.mjs +38 -1
- package/dist/es/responses/index.d.mts +14 -0
- package/dist/es/responses/index.mjs +182 -1
- package/package.json +9 -6
- package/dist/cjs/error-LUQPfUfP.cjs +0 -2
- package/dist/es/error-LySQb5Bh.mjs +0 -2
|
@@ -1 +1,462 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2026 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
16
|
+
const require_error = require('../error-QC7QKZdI.cjs');
|
|
17
|
+
let valibot = require("valibot");
|
|
18
|
+
let camelcase = require("camelcase");
|
|
19
|
+
camelcase = require_error.__toESM(camelcase, 1);
|
|
20
|
+
|
|
21
|
+
//#region source/headers/helpers.ts
|
|
22
|
+
/**
|
|
23
|
+
* Extracts a header value from an object with case-insensitive lookup.
|
|
24
|
+
* This is useful for handling HTTP headers which can be case-insensitive per RFC 7230.
|
|
25
|
+
*
|
|
26
|
+
* When enabled via options, comma-separated header values (per RFC 9110) can be
|
|
27
|
+
* automatically split into an array. This is useful when multiple header values
|
|
28
|
+
* are combined into a single comma-separated string.
|
|
29
|
+
*
|
|
30
|
+
* @param headers The headers object to search in.
|
|
31
|
+
* @param name The header name to look for (case-insensitive).
|
|
32
|
+
* @param options Optional settings to control header value processing.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* const headers = { authorization: "Bearer token123" };
|
|
37
|
+
* const auth = getHeader(headers, "Authorization"); // "Bearer token123"
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* // Comma-separated string can be split when enabled (per RFC 9110)
|
|
43
|
+
* const headers = { "Example-Field": "Foo, Bar" };
|
|
44
|
+
* const value = getHeader(headers, "Example-Field", { splitCommaSeparated: true }); // ["Foo", "Bar"]
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7230 - HTTP/1.1 Message Syntax and Routing
|
|
48
|
+
* @see https://datatracker.ietf.org/doc/html/rfc9110#name-field-lines-and-combined-fi - HTTP Semantics: Field Lines and Combined Field Values
|
|
49
|
+
*/
|
|
50
|
+
function getHeader(headers, name, options) {
|
|
51
|
+
const { splitCommaSeparated = false } = options ?? {};
|
|
52
|
+
let value;
|
|
53
|
+
if (Object.hasOwn(headers, name)) value = headers[name];
|
|
54
|
+
else {
|
|
55
|
+
const lowerName = name.toLowerCase();
|
|
56
|
+
for (const key in headers) if (Object.hasOwn(headers, key) && key.toLowerCase() === lowerName) {
|
|
57
|
+
value = headers[key];
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (splitCommaSeparated && typeof value === "string" && value.includes(",")) value = value.split(",").map((v) => v.trim());
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Extracts the `__ow_headers` object from App Builder runtime action parameters.
|
|
66
|
+
*
|
|
67
|
+
* @param params The action parameters from App Builder runtime.
|
|
68
|
+
* @throws {CommerceSdkValidationError} If `__ow_headers` is missing or not an object.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* export async function main(params) {
|
|
73
|
+
* const headers = getHeadersFromParams(params);
|
|
74
|
+
* const apiKey = headers["x-api-key"];
|
|
75
|
+
* }
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
function getHeadersFromParams(params) {
|
|
79
|
+
const parsed = (0, valibot.safeParse)((0, valibot.object)({ __ow_headers: (0, valibot.record)((0, valibot.string)(), (0, valibot.union)([(0, valibot.string)(), (0, valibot.undefined)()])) }), params);
|
|
80
|
+
if (!parsed.success) throw new require_error.CommerceSdkValidationError("Missing __ow_headers in action params", { issues: parsed.issues });
|
|
81
|
+
return parsed.output.__ow_headers;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region source/headers/validation.ts
|
|
86
|
+
/**
|
|
87
|
+
* Returns an array of missing or empty headers using Valibot validation.
|
|
88
|
+
* Case-insensitive by default.
|
|
89
|
+
*
|
|
90
|
+
* Unlike {@link assertRequiredHeaders}, this function does not throw an error.
|
|
91
|
+
* Instead, it returns an array of header names that are missing or empty.
|
|
92
|
+
* This is useful when you need to check headers without immediately failing.
|
|
93
|
+
*
|
|
94
|
+
* @param headers The headers object to validate.
|
|
95
|
+
* @param requiredHeaders Array of required header names.
|
|
96
|
+
* @returns Array of header names that are missing or empty. Empty array if all headers are present and non-empty.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* const headers = { "x-api-key": "key123" };
|
|
101
|
+
* const missing = getMissingOrEmptyHeaders(headers, ["x-api-key", "Authorization"]);
|
|
102
|
+
* // Returns: ["Authorization"]
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* const headers = { "x-api-key": "", "Authorization": "Bearer token" };
|
|
108
|
+
* const missing = getMissingOrEmptyHeaders(headers, ["x-api-key", "Authorization"]);
|
|
109
|
+
* // Returns: ["x-api-key"] (empty string is considered missing)
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
function getMissingOrEmptyHeaders(headers, requiredHeaders) {
|
|
113
|
+
const normalized = {};
|
|
114
|
+
if (headers && typeof headers === "object" && !Array.isArray(headers)) for (const key of Object.keys(headers)) {
|
|
115
|
+
const value = headers[key];
|
|
116
|
+
if (value !== void 0 && value !== null) normalized[key.toLowerCase()] = value;
|
|
117
|
+
}
|
|
118
|
+
const parsed = (0, valibot.safeParse)(buildHeadersSchema(requiredHeaders.map((h) => (0, valibot.literal)(h))), normalized);
|
|
119
|
+
if (parsed.success) return [];
|
|
120
|
+
const lookup = new Map(requiredHeaders.map((h) => [h.toLowerCase(), h]));
|
|
121
|
+
return parsed.issues.flatMap((issue) => {
|
|
122
|
+
const key = issue.path?.[0]?.key;
|
|
123
|
+
const original = typeof key === "string" ? lookup.get(key.toLowerCase()) : null;
|
|
124
|
+
return original ? [original] : [];
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Builds a Valibot schema that checks:
|
|
129
|
+
* - Required headers exist (case-insensitive)
|
|
130
|
+
* - Each required header has a non-empty string value
|
|
131
|
+
*/
|
|
132
|
+
function buildHeadersSchema(requiredHeaders) {
|
|
133
|
+
const buildSchema = () => (0, valibot.pipe)((0, valibot.string)("Expected a string value for the header"), (0, valibot.rawCheck)(({ dataset, addIssue }) => {
|
|
134
|
+
if (!dataset.typed) return;
|
|
135
|
+
const value = dataset.value;
|
|
136
|
+
if (value.trim() === "") {
|
|
137
|
+
addIssue({
|
|
138
|
+
expected: "a non-empty string value",
|
|
139
|
+
received: `"${value}"`,
|
|
140
|
+
message: "Header value cannot be empty or contain only whitespace"
|
|
141
|
+
});
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (value.includes(",")) {
|
|
145
|
+
const parts = value.split(",").map((part) => part.trim());
|
|
146
|
+
if (!parts.some((part) => part !== "")) addIssue({
|
|
147
|
+
expected: "at least one non-empty value in comma-separated list",
|
|
148
|
+
received: `[${parts.join(", ")}]`,
|
|
149
|
+
message: "Header value contains only empty or whitespace values when split by comma"
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}));
|
|
153
|
+
const shape = {};
|
|
154
|
+
for (const schema of requiredHeaders) shape[schema.literal.toLowerCase()] = buildSchema();
|
|
155
|
+
return (0, valibot.object)(shape);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Validates that required headers are present in the headers object.
|
|
159
|
+
* Case-insensitive by default (HTTP standard).
|
|
160
|
+
*
|
|
161
|
+
* This is an advanced validation function that accepts Valibot `LiteralSchema` objects
|
|
162
|
+
* instead of plain strings. Use {@link assertRequiredHeaders} for the simpler string-based API.
|
|
163
|
+
*
|
|
164
|
+
* @param headers The headers object to validate.
|
|
165
|
+
* @param requiredHeaders Array of required header names as Valibot `LiteralSchema` objects.
|
|
166
|
+
* @throws {CommerceSdkValidationError} If validation fails.
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```typescript
|
|
170
|
+
* import { literal } from "valibot";
|
|
171
|
+
* import { assertRequiredHeadersSchema } from "@adobe/aio-commerce-lib-core/headers";
|
|
172
|
+
*
|
|
173
|
+
* const headers = getHeadersFromParams(params);
|
|
174
|
+
* assertRequiredHeadersSchema(headers, [
|
|
175
|
+
* literal("x-api-key"),
|
|
176
|
+
* literal("Authorization"),
|
|
177
|
+
* ]);
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
function assertRequiredHeadersSchema(headers, requiredHeaders) {
|
|
181
|
+
const normalized = {};
|
|
182
|
+
if (headers && typeof headers === "object" && !Array.isArray(headers)) for (const key of Object.keys(headers)) {
|
|
183
|
+
const value = headers[key];
|
|
184
|
+
if (value !== void 0 && value !== null) normalized[key.toLowerCase()] = value;
|
|
185
|
+
}
|
|
186
|
+
const parsed = (0, valibot.safeParse)(buildHeadersSchema(requiredHeaders), normalized);
|
|
187
|
+
if (!parsed.success) throw new require_error.CommerceSdkValidationError("The headers object does not match the required headers (case-insensitive)", { issues: parsed.issues });
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Validates that required headers are present and non-empty in the headers object.
|
|
191
|
+
* Performs case-insensitive validation with {@link getHeader}.
|
|
192
|
+
*
|
|
193
|
+
* @param headers The headers object to validate.
|
|
194
|
+
* @param requiredHeaders Array of required header names.
|
|
195
|
+
* @throws {CommerceSdkValidationError} If any required headers are missing or empty.
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```typescript
|
|
199
|
+
* const headers = { "x-api-key": "key123", "Authorization": "Bearer token" };
|
|
200
|
+
* assertRequiredHeaders(headers, ["x-api-key", "Authorization"]);
|
|
201
|
+
* // Headers are validated, use getHeader() for safe access
|
|
202
|
+
* const apiKey = getHeader(headers, "x-api-key")!;
|
|
203
|
+
* ```
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```typescript
|
|
207
|
+
* try {
|
|
208
|
+
* assertRequiredHeaders({}, ["x-api-key", "Authorization"]);
|
|
209
|
+
* } catch (error) {
|
|
210
|
+
* console.error(error.display());
|
|
211
|
+
* // "The headers object does not match the required headers (case-insensitive)"
|
|
212
|
+
* // ├── Schema validation error at x-api-key → Expected a non-empty string value
|
|
213
|
+
* // └── Schema validation error at Authorization → Expected a non-empty string value
|
|
214
|
+
* }
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
217
|
+
function assertRequiredHeaders(headers, requiredHeaders) {
|
|
218
|
+
assertRequiredHeadersSchema(headers, requiredHeaders.map((header) => (0, valibot.literal)(header)));
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
//#endregion
|
|
222
|
+
//#region source/headers/accessor.ts
|
|
223
|
+
const LEADING_UNDERSCORES = /^_+/;
|
|
224
|
+
/**
|
|
225
|
+
* Converts a header name to a camelCase key aligned with `type-fest`'s `CamelCase` type.
|
|
226
|
+
*
|
|
227
|
+
* `camelcase` v9+ preserves leading underscores (e.g. `_foo_bar` → `_fooBar`), but
|
|
228
|
+
* `type-fest`'s `CamelCase` — used by `HttpHeaderAccessorMap` — drops them
|
|
229
|
+
* (e.g. `_foo_bar` → `fooBar`). Without stripping them first, runtime keys would diverge
|
|
230
|
+
* from the inferred TypeScript type.
|
|
231
|
+
*
|
|
232
|
+
* @remarks This function must be removed and replaced with a bare `camelcase(header)` call
|
|
233
|
+
* once the two helpers agree on leading-underscore handling. That cleanup is a breaking
|
|
234
|
+
* change and must land in a major release.
|
|
235
|
+
*/
|
|
236
|
+
function toHeaderCamelKey(header) {
|
|
237
|
+
return (0, camelcase.default)(header.replace(LEADING_UNDERSCORES, ""));
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Creates a type-safe header accessor object with validated required headers.
|
|
241
|
+
* Header names are normalized to camelCase for consistent access.
|
|
242
|
+
*
|
|
243
|
+
* @param headers The headers object from runtime params.
|
|
244
|
+
* @param requiredHeaders Array of required header names.
|
|
245
|
+
* @param options Optional settings to control header value processing (e.g., `splitCommaSeparated`).
|
|
246
|
+
* @throws {CommerceSdkValidationError} If any required headers are missing or empty.
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* ```typescript
|
|
250
|
+
* const headers = getHeadersFromParams(params);
|
|
251
|
+
* const accessor = createHeaderAccessor(headers, ["x-api-key", "Authorization"]);
|
|
252
|
+
*
|
|
253
|
+
* // TypeScript knows these are non-empty strings (normalized to camelCase)
|
|
254
|
+
* const apiKey = accessor.xApiKey; // string
|
|
255
|
+
* const auth = accessor.authorization; // string
|
|
256
|
+
* ```
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
259
|
+
* ```typescript
|
|
260
|
+
* // With options to enable comma-separated value splitting
|
|
261
|
+
* const accessor = createHeaderAccessor(
|
|
262
|
+
* headers,
|
|
263
|
+
* ["cache-control"],
|
|
264
|
+
* { splitCommaSeparated: true }
|
|
265
|
+
* );
|
|
266
|
+
* // accessor.cacheControl may be string | string[] depending on comma presence
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
function createHeaderAccessor(headers, requiredHeaders, options) {
|
|
270
|
+
assertRequiredHeaders(headers, requiredHeaders);
|
|
271
|
+
const accessor = {};
|
|
272
|
+
for (const header of requiredHeaders) {
|
|
273
|
+
const camelKey = toHeaderCamelKey(header);
|
|
274
|
+
accessor[camelKey] = getHeader(headers, header, options);
|
|
275
|
+
}
|
|
276
|
+
return accessor;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
//#endregion
|
|
280
|
+
//#region source/headers/auth.ts
|
|
281
|
+
/**
|
|
282
|
+
* Type guard to check if authorization is Bearer type.
|
|
283
|
+
*/
|
|
284
|
+
function isBearerAuth(auth) {
|
|
285
|
+
return auth.scheme === "Bearer";
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Type guard to check if authorization is Basic type.
|
|
289
|
+
*/
|
|
290
|
+
function isBasicAuth(auth) {
|
|
291
|
+
return auth.scheme === "Basic";
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Type guard to check if authorization is OAuth type.
|
|
295
|
+
*/
|
|
296
|
+
function isOAuth(auth) {
|
|
297
|
+
return auth.scheme === "OAuth";
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Parses key="value" or key=value parameters from a string.
|
|
301
|
+
* Used for OAuth and other schemes with comma-separated key-value pairs.
|
|
302
|
+
*
|
|
303
|
+
* @param parametersString The parameters string.
|
|
304
|
+
*/
|
|
305
|
+
function parseKeyValueParameters(parametersString) {
|
|
306
|
+
const params = {};
|
|
307
|
+
const paramPattern = /(\w+)=(?:"([^"]*)"|([^,\s]+))/g;
|
|
308
|
+
let match = paramPattern.exec(parametersString);
|
|
309
|
+
while (match !== null) {
|
|
310
|
+
const [, key, quotedValue, unquotedValue] = match;
|
|
311
|
+
params[key] = quotedValue === void 0 ? unquotedValue : quotedValue;
|
|
312
|
+
match = paramPattern.exec(parametersString);
|
|
313
|
+
}
|
|
314
|
+
return params;
|
|
315
|
+
}
|
|
316
|
+
const authorizationSchema = (0, valibot.pipe)((0, valibot.string)("Expected a string value"), (0, valibot.trim)(), (0, valibot.nonEmpty)("Expected a non-empty string value"), (0, valibot.rawTransform)(({ dataset: { value: authorization }, addIssue, NEVER }) => {
|
|
317
|
+
const spaceIndex = authorization.indexOf(" ");
|
|
318
|
+
if (spaceIndex === -1) {
|
|
319
|
+
addIssue({ message: "Invalid Authorization header format" });
|
|
320
|
+
return NEVER;
|
|
321
|
+
}
|
|
322
|
+
return {
|
|
323
|
+
scheme: authorization.slice(0, spaceIndex),
|
|
324
|
+
parameters: authorization.slice(spaceIndex + 1).trim()
|
|
325
|
+
};
|
|
326
|
+
}));
|
|
327
|
+
/**
|
|
328
|
+
* Parses an Authorization header value into its scheme and parameters.
|
|
329
|
+
*
|
|
330
|
+
* @param authorization The Authorization header value.
|
|
331
|
+
* @throws {CommerceSdkValidationError} If the authorization header is malformed.
|
|
332
|
+
*
|
|
333
|
+
* @example
|
|
334
|
+
* ```typescript
|
|
335
|
+
* const auth = parseAuthorization("Bearer token123");
|
|
336
|
+
* // { scheme: "Bearer", token: "token123" }
|
|
337
|
+
* ```
|
|
338
|
+
*
|
|
339
|
+
* @example
|
|
340
|
+
* ```typescript
|
|
341
|
+
* const auth = parseAuthorization("Basic dXNlcjpwYXNz");
|
|
342
|
+
* // { scheme: "Basic", credentials: "dXNlcjpwYXNz" }
|
|
343
|
+
* ```
|
|
344
|
+
*
|
|
345
|
+
* @example
|
|
346
|
+
* ```typescript
|
|
347
|
+
* const auth = parseAuthorization('OAuth oauth_consumer_key="key", oauth_token="token"');
|
|
348
|
+
* // { scheme: "OAuth", parameters: { oauth_consumer_key: "key", oauth_token: "token" } }
|
|
349
|
+
* ```
|
|
350
|
+
*
|
|
351
|
+
* @example
|
|
352
|
+
* ```typescript
|
|
353
|
+
* const auth = parseAuthorization('Digest realm="Example", nonce="abc"');
|
|
354
|
+
* // { scheme: "Digest", rawParameters: 'realm="Example", nonce="abc"' }
|
|
355
|
+
*
|
|
356
|
+
* const parameters = auth.parseParameters();
|
|
357
|
+
* // { realm: "Example", nonce: "abc" }
|
|
358
|
+
* ```
|
|
359
|
+
*/
|
|
360
|
+
function parseAuthorization(authorization) {
|
|
361
|
+
const parsed = (0, valibot.safeParse)(authorizationSchema, authorization);
|
|
362
|
+
if (!parsed.success) throw new require_error.CommerceSdkValidationError("Invalid Authorization header format", { issues: parsed.issues });
|
|
363
|
+
const { scheme, parameters } = parsed.output;
|
|
364
|
+
if (scheme === "Bearer") return {
|
|
365
|
+
scheme: "Bearer",
|
|
366
|
+
token: parameters
|
|
367
|
+
};
|
|
368
|
+
if (scheme === "Basic") return {
|
|
369
|
+
scheme: "Basic",
|
|
370
|
+
credentials: parameters
|
|
371
|
+
};
|
|
372
|
+
if (scheme === "OAuth") return {
|
|
373
|
+
scheme: "OAuth",
|
|
374
|
+
parameters: parseKeyValueParameters(parameters)
|
|
375
|
+
};
|
|
376
|
+
return {
|
|
377
|
+
scheme,
|
|
378
|
+
rawParameters: parameters,
|
|
379
|
+
parseParameters: () => parseKeyValueParameters(parameters)
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
const bearerSchema = (0, valibot.object)({ scheme: (0, valibot.literal)("Bearer") });
|
|
383
|
+
/**
|
|
384
|
+
* Parses and validates a Bearer authorization header.
|
|
385
|
+
*
|
|
386
|
+
* @param authorization The Authorization header value.
|
|
387
|
+
* @throws {CommerceSdkValidationError} If the authorization header is not a Bearer token or if the token is empty.
|
|
388
|
+
*
|
|
389
|
+
* @example
|
|
390
|
+
* ```typescript
|
|
391
|
+
* const auth = parseBearerToken("Bearer eyJhbGci...");
|
|
392
|
+
* console.log(auth.token); // "eyJhbGci..."
|
|
393
|
+
* ```
|
|
394
|
+
*
|
|
395
|
+
* @example
|
|
396
|
+
* ```typescript
|
|
397
|
+
* try {
|
|
398
|
+
* parseBearerToken("Basic dXNlcjpwYXNz");
|
|
399
|
+
* } catch (error) {
|
|
400
|
+
* console.error(error.message); // "Authorization header must be a Bearer token"
|
|
401
|
+
* }
|
|
402
|
+
* ```
|
|
403
|
+
*/
|
|
404
|
+
function parseBearerToken(authorization) {
|
|
405
|
+
const auth = parseAuthorization(authorization);
|
|
406
|
+
const parsed = (0, valibot.safeParse)(bearerSchema, auth);
|
|
407
|
+
if (!parsed.success) throw new require_error.CommerceSdkValidationError("Authorization header must be a Bearer token", { issues: parsed.issues });
|
|
408
|
+
return auth;
|
|
409
|
+
}
|
|
410
|
+
const basicSchema = (0, valibot.object)({ scheme: (0, valibot.literal)("Basic") });
|
|
411
|
+
/**
|
|
412
|
+
* Parses and validates a Basic authorization header.
|
|
413
|
+
*
|
|
414
|
+
* @param authorization The Authorization header value.
|
|
415
|
+
* @throws {CommerceSdkValidationError} If the authorization header is not Basic auth.
|
|
416
|
+
*
|
|
417
|
+
* @example
|
|
418
|
+
* ```typescript
|
|
419
|
+
* const auth = parseBasicToken("Basic dXNlcjpwYXNz");
|
|
420
|
+
* console.log(auth.credentials); // "dXNlcjpwYXNz"
|
|
421
|
+
* ```
|
|
422
|
+
*/
|
|
423
|
+
function parseBasicToken(authorization) {
|
|
424
|
+
const auth = parseAuthorization(authorization);
|
|
425
|
+
const parsed = (0, valibot.safeParse)(basicSchema, auth);
|
|
426
|
+
if (!parsed.success) throw new require_error.CommerceSdkValidationError("Authorization header must be Basic authentication", { issues: parsed.issues });
|
|
427
|
+
return auth;
|
|
428
|
+
}
|
|
429
|
+
const oauthSchema = (0, valibot.object)({ scheme: (0, valibot.literal)("OAuth") });
|
|
430
|
+
/**
|
|
431
|
+
* Parses and validates an OAuth 1.0 authorization header.
|
|
432
|
+
*
|
|
433
|
+
* @param authorization The Authorization header value.
|
|
434
|
+
* @throws {CommerceSdkValidationError} If the authorization header is not OAuth.
|
|
435
|
+
*
|
|
436
|
+
* @example
|
|
437
|
+
* ```typescript
|
|
438
|
+
* const auth = parseOAuthToken('OAuth oauth_consumer_key="key", oauth_token="token"');
|
|
439
|
+
* console.log(auth.parameters.oauth_consumer_key); // "key"
|
|
440
|
+
* console.log(auth.parameters.oauth_token); // "token"
|
|
441
|
+
* ```
|
|
442
|
+
*/
|
|
443
|
+
function parseOAuthToken(authorization) {
|
|
444
|
+
const auth = parseAuthorization(authorization);
|
|
445
|
+
const parsed = (0, valibot.safeParse)(oauthSchema, auth);
|
|
446
|
+
if (!parsed.success) throw new require_error.CommerceSdkValidationError("Authorization header must be OAuth", { issues: parsed.issues });
|
|
447
|
+
return auth;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
//#endregion
|
|
451
|
+
exports.assertRequiredHeaders = assertRequiredHeaders;
|
|
452
|
+
exports.createHeaderAccessor = createHeaderAccessor;
|
|
453
|
+
exports.getHeader = getHeader;
|
|
454
|
+
exports.getHeadersFromParams = getHeadersFromParams;
|
|
455
|
+
exports.getMissingOrEmptyHeaders = getMissingOrEmptyHeaders;
|
|
456
|
+
exports.isBasicAuth = isBasicAuth;
|
|
457
|
+
exports.isBearerAuth = isBearerAuth;
|
|
458
|
+
exports.isOAuth = isOAuth;
|
|
459
|
+
exports.parseAuthorization = parseAuthorization;
|
|
460
|
+
exports.parseBasicToken = parseBasicToken;
|
|
461
|
+
exports.parseBearerToken = parseBearerToken;
|
|
462
|
+
exports.parseOAuthToken = parseOAuthToken;
|
|
@@ -1,8 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2026 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { r as RuntimeActionParams } from "../index-Ddzq9z7b.cjs";
|
|
2
16
|
import { CamelCase } from "type-fest";
|
|
3
17
|
|
|
4
18
|
//#region source/headers/types.d.ts
|
|
5
|
-
|
|
6
19
|
/** The type of an HTTP header value. */
|
|
7
20
|
type HttpHeaderValue = string | string[] | undefined;
|
|
8
21
|
/** The type of an HTTP headers record. */
|
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2026 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
|
|
1
15
|
//#region source/params/helpers.d.ts
|
|
2
16
|
/**
|
|
3
17
|
* Checks if the given runtime action input value is non-empty.
|
|
@@ -27,15 +41,10 @@ type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTION
|
|
|
27
41
|
type HttpMethodLowercase = Lowercase<HttpMethod>;
|
|
28
42
|
/** The type of the runtime action parameters. */
|
|
29
43
|
type RuntimeActionParams = {
|
|
30
|
-
/** If the runtime action is invoked via HTTP, this will be the headers of the request. */
|
|
31
|
-
|
|
32
|
-
/** If the runtime action is invoked via HTTP, this will be the
|
|
33
|
-
|
|
34
|
-
/** If the runtime action is invoked via HTTP, this will be the unmatched path of the request (matching stops after consuming the action extension) */
|
|
35
|
-
__ow_path?: string;
|
|
36
|
-
/** If the runtime action is invoked via HTTP, this will be the request body entity, as a base64-encoded string when its content is binary or JSON object/array, or as a plain string otherwise */
|
|
37
|
-
__ow_body?: string;
|
|
38
|
-
/** If the runtime action is invoked via HTTP, this will be the query parameters of the request, as an unparsed string. */
|
|
44
|
+
/** If the runtime action is invoked via HTTP, this will be the headers of the request. */__ow_headers?: Record<string, string | undefined>; /** If the runtime action is invoked via HTTP, this will be the HTTP method of the request. */
|
|
45
|
+
__ow_method?: HttpMethodLowercase; /** If the runtime action is invoked via HTTP, this will be the unmatched path of the request (matching stops after consuming the action extension) */
|
|
46
|
+
__ow_path?: string; /** If the runtime action is invoked via HTTP, this will be the request body entity, as a base64-encoded string when its content is binary or JSON object/array, or as a plain string otherwise */
|
|
47
|
+
__ow_body?: string; /** If the runtime action is invoked via HTTP, this will be the query parameters of the request, as an unparsed string. */
|
|
39
48
|
__ow_query?: string;
|
|
40
49
|
[key: string]: unknown;
|
|
41
50
|
};
|
|
@@ -1 +1,41 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2026 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
16
|
+
|
|
17
|
+
//#region source/params/helpers.ts
|
|
18
|
+
/**
|
|
19
|
+
* Checks if the given runtime action input value is non-empty.
|
|
20
|
+
*
|
|
21
|
+
* @param name - The name of the parameter. Required because of `aio app dev` compatibility: inputs mapped to undefined env vars come as $<input_name> in dev mode, but as '' in prod mode.
|
|
22
|
+
* @param value - The value of the parameter.
|
|
23
|
+
*/
|
|
24
|
+
function nonEmpty(name, value) {
|
|
25
|
+
if (value === void 0 || value === null) return false;
|
|
26
|
+
const v = String(value)?.trim();
|
|
27
|
+
return !!v && v !== `$${name}`;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Checks if all required parameters are non-empty.
|
|
31
|
+
*
|
|
32
|
+
* @param params - The action input parameters.
|
|
33
|
+
* @param required - The list of required parameter names.
|
|
34
|
+
*/
|
|
35
|
+
function allNonEmpty(params, required) {
|
|
36
|
+
return required.every((name) => nonEmpty(name, params[name]));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
//#endregion
|
|
40
|
+
exports.allNonEmpty = allNonEmpty;
|
|
41
|
+
exports.nonEmpty = nonEmpty;
|
|
@@ -1,2 +1,16 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2026 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { a as nonEmpty, i as allNonEmpty, n as HttpMethodLowercase, r as RuntimeActionParams, t as HttpMethod } from "../index-Ddzq9z7b.cjs";
|
|
2
16
|
export { HttpMethod, HttpMethodLowercase, RuntimeActionParams, allNonEmpty, nonEmpty };
|