@01.software/sdk 0.67.0 → 0.68.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 +13 -0
- package/README.md +122 -6
- package/dist/client.cjs +419 -319
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +11 -6
- package/dist/client.d.ts +11 -6
- package/dist/client.js +419 -319
- package/dist/client.js.map +1 -1
- package/dist/{collection-client-D4HEOoO_.d.ts → collection-client-IOaWsTMI.d.ts} +4 -4
- package/dist/{collection-client-jrNFGbrO.d.cts → collection-client-MbIJ5KzW.d.cts} +4 -4
- package/dist/{const-BU79hCFB.d.ts → const-Do27Udj6.d.ts} +2 -2
- package/dist/{const-CCQ3MdLE.d.cts → const-Dw5ElJs_.d.cts} +2 -2
- package/dist/embedded-admin/server.cjs +16 -1
- package/dist/embedded-admin/server.cjs.map +1 -1
- package/dist/embedded-admin/server.d.cts +4 -2
- package/dist/embedded-admin/server.d.ts +4 -2
- package/dist/embedded-admin/server.js +16 -1
- package/dist/embedded-admin/server.js.map +1 -1
- package/dist/embedded-admin.cjs +19 -0
- package/dist/embedded-admin.cjs.map +1 -1
- package/dist/embedded-admin.d.cts +7 -1
- package/dist/embedded-admin.d.ts +7 -1
- package/dist/embedded-admin.js +19 -0
- package/dist/embedded-admin.js.map +1 -1
- package/dist/errors.d.cts +2 -2
- package/dist/errors.d.ts +2 -2
- package/dist/{index-CwKxWHIa.d.ts → index-DcYsLOja.d.ts} +3 -3
- package/dist/{index-DiyhsVHd.d.cts → index-gnVxTKXu.d.cts} +3 -3
- package/dist/index.cjs +424 -323
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -7
- package/dist/index.d.ts +7 -7
- package/dist/index.js +424 -323
- package/dist/index.js.map +1 -1
- package/dist/{payload-types-jdsUXunB.d.cts → payload-types-BY1uZFRf.d.cts} +34 -1
- package/dist/{payload-types-jdsUXunB.d.ts → payload-types-BY1uZFRf.d.ts} +34 -1
- package/dist/query.cjs +8 -0
- package/dist/query.cjs.map +1 -1
- package/dist/query.d.cts +5 -5
- package/dist/query.d.ts +5 -5
- package/dist/query.js +8 -0
- package/dist/query.js.map +1 -1
- package/dist/realtime.d.cts +2 -2
- package/dist/realtime.d.ts +2 -2
- package/dist/server.cjs +1183 -1071
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.cts +9 -8
- package/dist/server.d.ts +9 -8
- package/dist/server.js +1183 -1071
- package/dist/server.js.map +1 -1
- package/dist/{types-BusejG-v.d.ts → types-AWSVpuPE.d.ts} +2 -2
- package/dist/{types-I2k6Laad.d.ts → types-CLRw0aDa.d.ts} +147 -40
- package/dist/{types-BPwXAFZ7.d.cts → types-DnIuxgu2.d.cts} +2 -2
- package/dist/{types-CvdBV3ri.d.cts → types-csjaig4G.d.cts} +147 -40
- package/dist/ui/form.d.cts +1 -1
- package/dist/ui/form.d.ts +1 -1
- package/dist/ui/video.d.cts +1 -1
- package/dist/ui/video.d.ts +1 -1
- package/dist/webhook.d.cts +5 -5
- package/dist/webhook.d.ts +5 -5
- package/package.json +2 -2
package/dist/client.cjs
CHANGED
|
@@ -41,9 +41,6 @@ __export(client_exports, {
|
|
|
41
41
|
});
|
|
42
42
|
module.exports = __toCommonJS(client_exports);
|
|
43
43
|
|
|
44
|
-
// src/core/collection/http-client.ts
|
|
45
|
-
var import_qs_esm = require("qs-esm");
|
|
46
|
-
|
|
47
44
|
// src/core/errors.ts
|
|
48
45
|
var SDK_ERROR_BRAND = /* @__PURE__ */ Symbol.for("@01.software/sdk/SDKError");
|
|
49
46
|
var SDK_ERROR_TYPE_BRAND = /* @__PURE__ */ Symbol.for("@01.software/sdk/SDKError.type");
|
|
@@ -1047,281 +1044,6 @@ function stockSnapshotSearchParams(params) {
|
|
|
1047
1044
|
return `variantIds=${params.variantIds.map(encodeURIComponent).join(",")}`;
|
|
1048
1045
|
}
|
|
1049
1046
|
|
|
1050
|
-
// src/core/collection/http-client.ts
|
|
1051
|
-
function validateWhere(where) {
|
|
1052
|
-
if (where === void 0) return false;
|
|
1053
|
-
let hasNull = false;
|
|
1054
|
-
function visit(value, depth, key) {
|
|
1055
|
-
if (depth > 10) {
|
|
1056
|
-
throw createValidationError(
|
|
1057
|
-
"Collection where must not exceed nesting depth 10."
|
|
1058
|
-
);
|
|
1059
|
-
}
|
|
1060
|
-
if (value === null && (key === "equals" || key === "not_equals")) {
|
|
1061
|
-
hasNull = true;
|
|
1062
|
-
return;
|
|
1063
|
-
}
|
|
1064
|
-
if (value == null) {
|
|
1065
|
-
throw createValidationError(
|
|
1066
|
-
"Collection where must not contain null or undefined values."
|
|
1067
|
-
);
|
|
1068
|
-
}
|
|
1069
|
-
if (Array.isArray(value)) {
|
|
1070
|
-
if (value.length === 0) {
|
|
1071
|
-
throw createValidationError(
|
|
1072
|
-
"Collection where must not contain empty arrays, including and/or."
|
|
1073
|
-
);
|
|
1074
|
-
}
|
|
1075
|
-
if (value.length > 1e3) {
|
|
1076
|
-
throw createValidationError(
|
|
1077
|
-
"Collection where arrays must not contain more than 1000 elements."
|
|
1078
|
-
);
|
|
1079
|
-
}
|
|
1080
|
-
for (const item of value) visit(item, depth + 1);
|
|
1081
|
-
} else if (typeof value === "object" && !(value instanceof Date)) {
|
|
1082
|
-
const values = Object.entries(value);
|
|
1083
|
-
if (values.length === 0) {
|
|
1084
|
-
throw createValidationError(
|
|
1085
|
-
"Collection where must not contain empty condition objects."
|
|
1086
|
-
);
|
|
1087
|
-
}
|
|
1088
|
-
for (const [key2, item] of values) visit(item, depth + 1, key2);
|
|
1089
|
-
}
|
|
1090
|
-
}
|
|
1091
|
-
visit(where, 0);
|
|
1092
|
-
return hasNull;
|
|
1093
|
-
}
|
|
1094
|
-
var HttpClient = class {
|
|
1095
|
-
constructor(publishableKey, secretKey, getCustomerToken, onUnauthorized, onRequestId, apiUrl) {
|
|
1096
|
-
this.publishableKey = requirePublishableKeyForSecret(
|
|
1097
|
-
"CollectionClient",
|
|
1098
|
-
publishableKey,
|
|
1099
|
-
secretKey
|
|
1100
|
-
);
|
|
1101
|
-
this.secretKey = secretKey;
|
|
1102
|
-
this.getCustomerToken = getCustomerToken;
|
|
1103
|
-
this.onUnauthorized = onUnauthorized;
|
|
1104
|
-
this.onRequestId = onRequestId;
|
|
1105
|
-
this.apiUrl = apiUrl;
|
|
1106
|
-
}
|
|
1107
|
-
get defaultOptions() {
|
|
1108
|
-
const opts = {
|
|
1109
|
-
apiUrl: this.apiUrl,
|
|
1110
|
-
publishableKey: this.publishableKey,
|
|
1111
|
-
secretKey: this.secretKey
|
|
1112
|
-
};
|
|
1113
|
-
const token = this.getCustomerToken?.();
|
|
1114
|
-
if (token) {
|
|
1115
|
-
opts.customerToken = token;
|
|
1116
|
-
if (this.onUnauthorized) {
|
|
1117
|
-
opts.onUnauthorized = this.onUnauthorized;
|
|
1118
|
-
}
|
|
1119
|
-
}
|
|
1120
|
-
return opts;
|
|
1121
|
-
}
|
|
1122
|
-
async fetchWithTracking(url, opts) {
|
|
1123
|
-
try {
|
|
1124
|
-
const response = await httpFetch(url, opts);
|
|
1125
|
-
this.onRequestId?.(response.headers.get("x-request-id") ?? null);
|
|
1126
|
-
return response;
|
|
1127
|
-
} catch (err) {
|
|
1128
|
-
const id = err instanceof SDKError ? err.requestId ?? null : null;
|
|
1129
|
-
this.onRequestId?.(id);
|
|
1130
|
-
throw err;
|
|
1131
|
-
}
|
|
1132
|
-
}
|
|
1133
|
-
buildUrl(endpoint, options) {
|
|
1134
|
-
if (!options) return endpoint;
|
|
1135
|
-
const { fallbackLocale, ...rest } = options;
|
|
1136
|
-
const queryOptions = fallbackLocale === void 0 ? rest : {
|
|
1137
|
-
...rest,
|
|
1138
|
-
"fallback-locale": fallbackLocale === false ? "none" : fallbackLocale
|
|
1139
|
-
};
|
|
1140
|
-
const encodedOptions = validateWhere(queryOptions.where) ? { ...queryOptions, where: JSON.stringify(queryOptions.where) } : queryOptions;
|
|
1141
|
-
const queryString = (0, import_qs_esm.stringify)(encodedOptions, { addQueryPrefix: true });
|
|
1142
|
-
return queryString ? `${endpoint}${queryString}` : endpoint;
|
|
1143
|
-
}
|
|
1144
|
-
assertJsonResponse(response) {
|
|
1145
|
-
const contentType = response.headers.get("content-type");
|
|
1146
|
-
if (!contentType?.includes("application/json")) {
|
|
1147
|
-
throw createApiError("Response is not in JSON format.", response.status, {
|
|
1148
|
-
contentType
|
|
1149
|
-
});
|
|
1150
|
-
}
|
|
1151
|
-
}
|
|
1152
|
-
/**
|
|
1153
|
-
* Parse Payload CMS find response (list query)
|
|
1154
|
-
* Returns native Payload response structure
|
|
1155
|
-
*/
|
|
1156
|
-
async parseFindResponse(response) {
|
|
1157
|
-
const contentType = response.headers.get("content-type");
|
|
1158
|
-
try {
|
|
1159
|
-
this.assertJsonResponse(response);
|
|
1160
|
-
const jsonData = await response.json();
|
|
1161
|
-
if (jsonData.docs === void 0) {
|
|
1162
|
-
throw createApiError("Invalid find response.", response.status, {
|
|
1163
|
-
jsonData
|
|
1164
|
-
});
|
|
1165
|
-
}
|
|
1166
|
-
return {
|
|
1167
|
-
docs: jsonData.docs,
|
|
1168
|
-
totalDocs: jsonData.totalDocs ?? 0,
|
|
1169
|
-
limit: jsonData.limit || 20,
|
|
1170
|
-
totalPages: jsonData.totalPages ?? 0,
|
|
1171
|
-
page: jsonData.page || 1,
|
|
1172
|
-
pagingCounter: jsonData.pagingCounter || 1,
|
|
1173
|
-
hasPrevPage: jsonData.hasPrevPage ?? false,
|
|
1174
|
-
hasNextPage: jsonData.hasNextPage ?? false,
|
|
1175
|
-
prevPage: jsonData.prevPage ?? null,
|
|
1176
|
-
nextPage: jsonData.nextPage ?? null
|
|
1177
|
-
};
|
|
1178
|
-
} catch (error) {
|
|
1179
|
-
if (error instanceof SDKError) throw error;
|
|
1180
|
-
throw createApiError("Failed to parse response.", response.status, {
|
|
1181
|
-
contentType,
|
|
1182
|
-
error: error instanceof Error ? error.message : error
|
|
1183
|
-
});
|
|
1184
|
-
}
|
|
1185
|
-
}
|
|
1186
|
-
/**
|
|
1187
|
-
* Parse Payload CMS mutation response (create/update)
|
|
1188
|
-
* Returns native Payload response structure
|
|
1189
|
-
*/
|
|
1190
|
-
async parseMutationResponse(response) {
|
|
1191
|
-
const contentType = response.headers.get("content-type");
|
|
1192
|
-
try {
|
|
1193
|
-
this.assertJsonResponse(response);
|
|
1194
|
-
const jsonData = await response.json();
|
|
1195
|
-
if (jsonData.doc === void 0) {
|
|
1196
|
-
throw createApiError("Invalid mutation response.", response.status, {
|
|
1197
|
-
jsonData
|
|
1198
|
-
});
|
|
1199
|
-
}
|
|
1200
|
-
return {
|
|
1201
|
-
message: jsonData.message || "",
|
|
1202
|
-
doc: jsonData.doc,
|
|
1203
|
-
errors: jsonData.errors
|
|
1204
|
-
};
|
|
1205
|
-
} catch (error) {
|
|
1206
|
-
if (error instanceof SDKError) throw error;
|
|
1207
|
-
throw createApiError("Failed to parse response.", response.status, {
|
|
1208
|
-
contentType,
|
|
1209
|
-
error: error instanceof Error ? error.message : error
|
|
1210
|
-
});
|
|
1211
|
-
}
|
|
1212
|
-
}
|
|
1213
|
-
/**
|
|
1214
|
-
* Parse Payload CMS document response (findById/delete)
|
|
1215
|
-
* Returns document directly without wrapper
|
|
1216
|
-
*/
|
|
1217
|
-
async parseDocumentResponse(response) {
|
|
1218
|
-
const contentType = response.headers.get("content-type");
|
|
1219
|
-
try {
|
|
1220
|
-
this.assertJsonResponse(response);
|
|
1221
|
-
const jsonData = await response.json();
|
|
1222
|
-
return jsonData;
|
|
1223
|
-
} catch (error) {
|
|
1224
|
-
if (error instanceof SDKError) throw error;
|
|
1225
|
-
throw createApiError("Failed to parse response.", response.status, {
|
|
1226
|
-
contentType,
|
|
1227
|
-
error: error instanceof Error ? error.message : error
|
|
1228
|
-
});
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
|
-
};
|
|
1232
|
-
|
|
1233
|
-
// src/core/collection/query-builder.ts
|
|
1234
|
-
var PUBLIC_READ_DEPTH_ERROR = "Publishable collection reads require depth: 0. Use a shaped commerce endpoint or server client for populated data.";
|
|
1235
|
-
var PUBLIC_READ_JOINS_ERROR = "Publishable collection reads require joins: false. Use a shaped commerce endpoint or server client for joined data.";
|
|
1236
|
-
var PUBLIC_READ_POPULATE_ERROR = "Publishable collection reads do not support populate. Use a shaped commerce endpoint or server client for populated data.";
|
|
1237
|
-
function withDefaultPublicReadOptions(options) {
|
|
1238
|
-
const rawOptions = options;
|
|
1239
|
-
if (rawOptions?.depth !== void 0 && rawOptions.depth !== 0) {
|
|
1240
|
-
throw createValidationError(PUBLIC_READ_DEPTH_ERROR);
|
|
1241
|
-
}
|
|
1242
|
-
if (rawOptions?.joins !== void 0 && rawOptions.joins !== false) {
|
|
1243
|
-
throw createValidationError(PUBLIC_READ_JOINS_ERROR);
|
|
1244
|
-
}
|
|
1245
|
-
if (rawOptions?.populate !== void 0) {
|
|
1246
|
-
throw createValidationError(PUBLIC_READ_POPULATE_ERROR);
|
|
1247
|
-
}
|
|
1248
|
-
return {
|
|
1249
|
-
...options,
|
|
1250
|
-
depth: options?.depth ?? 0,
|
|
1251
|
-
joins: options?.joins ?? false
|
|
1252
|
-
};
|
|
1253
|
-
}
|
|
1254
|
-
var ReadOnlyCollectionQueryBuilder = class {
|
|
1255
|
-
constructor(api, collection) {
|
|
1256
|
-
this.api = api;
|
|
1257
|
-
this.collection = collection;
|
|
1258
|
-
}
|
|
1259
|
-
async find(options) {
|
|
1260
|
-
return this.api.requestFind(
|
|
1261
|
-
`/api/${String(this.collection)}`,
|
|
1262
|
-
withDefaultPublicReadOptions(options)
|
|
1263
|
-
);
|
|
1264
|
-
}
|
|
1265
|
-
async findById(id, options) {
|
|
1266
|
-
return this.api.requestFindById(
|
|
1267
|
-
`/api/${String(this.collection)}/${String(id)}`,
|
|
1268
|
-
withDefaultPublicReadOptions(options)
|
|
1269
|
-
);
|
|
1270
|
-
}
|
|
1271
|
-
async count(options) {
|
|
1272
|
-
return this.api.requestCount(
|
|
1273
|
-
`/api/${String(this.collection)}/count`,
|
|
1274
|
-
withDefaultPublicReadOptions(options)
|
|
1275
|
-
);
|
|
1276
|
-
}
|
|
1277
|
-
};
|
|
1278
|
-
|
|
1279
|
-
// src/core/collection/collection-client.ts
|
|
1280
|
-
var ReadOnlyCollectionClient = class extends HttpClient {
|
|
1281
|
-
from(collection) {
|
|
1282
|
-
return new ReadOnlyCollectionQueryBuilder(this, collection);
|
|
1283
|
-
}
|
|
1284
|
-
async requestFind(endpoint, options) {
|
|
1285
|
-
const url = this.buildUrl(endpoint, options);
|
|
1286
|
-
const response = await this.fetchWithTracking(url, {
|
|
1287
|
-
...this.defaultOptions,
|
|
1288
|
-
method: "GET"
|
|
1289
|
-
});
|
|
1290
|
-
return this.parseFindResponse(response);
|
|
1291
|
-
}
|
|
1292
|
-
async requestFindEndpoint(endpoint, data) {
|
|
1293
|
-
const response = await this.fetchWithTracking(endpoint, {
|
|
1294
|
-
...this.defaultOptions,
|
|
1295
|
-
method: "POST",
|
|
1296
|
-
body: data ? JSON.stringify(data) : void 0
|
|
1297
|
-
});
|
|
1298
|
-
return this.parseFindResponse(response);
|
|
1299
|
-
}
|
|
1300
|
-
async requestFindEndpointGet(endpoint) {
|
|
1301
|
-
const response = await this.fetchWithTracking(endpoint, {
|
|
1302
|
-
...this.defaultOptions,
|
|
1303
|
-
method: "GET"
|
|
1304
|
-
});
|
|
1305
|
-
return this.parseFindResponse(response);
|
|
1306
|
-
}
|
|
1307
|
-
async requestFindById(endpoint, options) {
|
|
1308
|
-
const url = this.buildUrl(endpoint, options);
|
|
1309
|
-
const response = await this.fetchWithTracking(url, {
|
|
1310
|
-
...this.defaultOptions,
|
|
1311
|
-
method: "GET"
|
|
1312
|
-
});
|
|
1313
|
-
return this.parseDocumentResponse(response);
|
|
1314
|
-
}
|
|
1315
|
-
async requestCount(endpoint, options) {
|
|
1316
|
-
const url = this.buildUrl(endpoint, options);
|
|
1317
|
-
const response = await this.fetchWithTracking(url, {
|
|
1318
|
-
...this.defaultOptions,
|
|
1319
|
-
method: "GET"
|
|
1320
|
-
});
|
|
1321
|
-
return this.parseDocumentResponse(response);
|
|
1322
|
-
}
|
|
1323
|
-
};
|
|
1324
|
-
|
|
1325
1047
|
// src/core/api/parse-response.ts
|
|
1326
1048
|
async function parseApiResponse(response, endpoint, options = {}) {
|
|
1327
1049
|
if (response.status === 204 || response.headers.get("content-length") === "0") {
|
|
@@ -1583,6 +1305,14 @@ var EXTERNAL_CUSTOMER_LINK_ENDPOINT_OPERATIONS = {
|
|
|
1583
1305
|
"/external-links/clear-previous-state"
|
|
1584
1306
|
)
|
|
1585
1307
|
};
|
|
1308
|
+
var BENEFIT_VOUCHER_ENDPOINT_OPERATIONS = {
|
|
1309
|
+
issue: operation("customers", "post", "/benefit-vouchers/issue"),
|
|
1310
|
+
get: operation("customers", "post", "/benefit-vouchers/get"),
|
|
1311
|
+
list: operation("customers", "post", "/benefit-vouchers/list"),
|
|
1312
|
+
redeem: operation("customers", "post", "/benefit-vouchers/redeem"),
|
|
1313
|
+
void: operation("customers", "post", "/benefit-vouchers/void"),
|
|
1314
|
+
listMine: operation("customers", "get", "/me/benefit-vouchers")
|
|
1315
|
+
};
|
|
1586
1316
|
var CUSTOMER_GROUP_ENDPOINT_OPERATIONS = {
|
|
1587
1317
|
members: operation("customer-groups", "get", "/:id/members"),
|
|
1588
1318
|
grant: operation("customer-groups", "post", "/:id/members/grant"),
|
|
@@ -1820,62 +1550,422 @@ var CustomerScopedTransport = class {
|
|
|
1820
1550
|
);
|
|
1821
1551
|
}
|
|
1822
1552
|
this.publishableKey = requirePublishableKeyForSecret(
|
|
1823
|
-
apiName,
|
|
1824
|
-
options.publishableKey,
|
|
1825
|
-
options.secretKey
|
|
1553
|
+
apiName,
|
|
1554
|
+
options.publishableKey,
|
|
1555
|
+
options.secretKey
|
|
1556
|
+
);
|
|
1557
|
+
this.secretKey = options.secretKey;
|
|
1558
|
+
this.apiUrl = options.apiUrl;
|
|
1559
|
+
this.customerToken = options.customerToken;
|
|
1560
|
+
this.onUnauthorized = options.onUnauthorized;
|
|
1561
|
+
this.onRequestId = options.onRequestId;
|
|
1562
|
+
}
|
|
1563
|
+
async request(endpoint, options) {
|
|
1564
|
+
const token = typeof this.customerToken === "function" ? this.customerToken() : this.customerToken;
|
|
1565
|
+
try {
|
|
1566
|
+
const response = await httpFetch(endpoint, {
|
|
1567
|
+
method: options.method,
|
|
1568
|
+
apiUrl: this.apiUrl,
|
|
1569
|
+
publishableKey: this.publishableKey,
|
|
1570
|
+
secretKey: this.secretKey,
|
|
1571
|
+
customerToken: token ?? void 0,
|
|
1572
|
+
...token && this.onUnauthorized && { onUnauthorized: this.onUnauthorized },
|
|
1573
|
+
...options.body !== void 0 && {
|
|
1574
|
+
body: JSON.stringify(options.body)
|
|
1575
|
+
},
|
|
1576
|
+
...options.headers && { headers: options.headers },
|
|
1577
|
+
...options.errorFamily && { errorFamily: options.errorFamily }
|
|
1578
|
+
});
|
|
1579
|
+
this.onRequestId?.(response.headers.get("x-request-id") ?? null);
|
|
1580
|
+
return parseApiResponse(response, endpoint, {
|
|
1581
|
+
errorFamily: options.errorFamily
|
|
1582
|
+
});
|
|
1583
|
+
} catch (err) {
|
|
1584
|
+
const id = err instanceof SDKError ? err.requestId ?? null : null;
|
|
1585
|
+
this.onRequestId?.(id);
|
|
1586
|
+
throw err;
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
};
|
|
1590
|
+
var CustomerScopedApi = class {
|
|
1591
|
+
constructor(apiName, options) {
|
|
1592
|
+
this.transport = new CustomerScopedTransport(apiName, options);
|
|
1593
|
+
}
|
|
1594
|
+
requestEndpoint(endpoint, body, options) {
|
|
1595
|
+
return this.transport.request(resolveOperationPath(endpoint, options), {
|
|
1596
|
+
method: resolveOperationMethod(endpoint),
|
|
1597
|
+
body,
|
|
1598
|
+
headers: options?.headers,
|
|
1599
|
+
errorFamily: resolveOperationErrorFamily(endpoint)
|
|
1600
|
+
});
|
|
1601
|
+
}
|
|
1602
|
+
};
|
|
1603
|
+
var RawCustomerScopedApi = class {
|
|
1604
|
+
constructor(apiName, options) {
|
|
1605
|
+
this.transport = new CustomerScopedTransport(apiName, options);
|
|
1606
|
+
}
|
|
1607
|
+
request(endpoint, options) {
|
|
1608
|
+
return this.transport.request(endpoint, options);
|
|
1609
|
+
}
|
|
1610
|
+
};
|
|
1611
|
+
|
|
1612
|
+
// src/core/api/benefit-voucher-types.ts
|
|
1613
|
+
var BENEFIT_VOUCHER_VOID_REASONS = [
|
|
1614
|
+
"issued_in_error",
|
|
1615
|
+
"benefit_revoked",
|
|
1616
|
+
"customer_request",
|
|
1617
|
+
"other"
|
|
1618
|
+
];
|
|
1619
|
+
function invalidBenefitVoucherResponse() {
|
|
1620
|
+
throw createApiError("Invalid benefit voucher response", 200);
|
|
1621
|
+
}
|
|
1622
|
+
function object(value, keys) {
|
|
1623
|
+
if (!value || typeof value !== "object" || Array.isArray(value) || Object.keys(value).length !== keys.length || keys.some((key) => !(key in value)))
|
|
1624
|
+
invalidBenefitVoucherResponse();
|
|
1625
|
+
return value;
|
|
1626
|
+
}
|
|
1627
|
+
var uuid = (v) => typeof v === "string" && (/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(
|
|
1628
|
+
v
|
|
1629
|
+
) || v === "00000000-0000-0000-0000-000000000000" || v.toLowerCase() === "ffffffff-ffff-ffff-ffff-ffffffffffff");
|
|
1630
|
+
var time = (v) => typeof v === "string" && /^\d{4}-\d{2}-\d{2}T/.test(v) && Number.isFinite(Date.parse(v)) && new Date(Date.parse(v)).toISOString() === v;
|
|
1631
|
+
var actor = (v) => typeof v === "string" && /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/.test(v);
|
|
1632
|
+
function parseBenefitVoucher(value, mine = false) {
|
|
1633
|
+
const v = object(value, [
|
|
1634
|
+
"id",
|
|
1635
|
+
"name",
|
|
1636
|
+
"description",
|
|
1637
|
+
"state",
|
|
1638
|
+
"issuedAt",
|
|
1639
|
+
"expiresAt",
|
|
1640
|
+
...mine ? ["closedAt"] : ["customerId", "definitionId", "source", "closed"]
|
|
1641
|
+
]);
|
|
1642
|
+
if (!uuid(v.id) || typeof v.name !== "string" || !v.name.length || v.name.length > 120 || v.description !== null && (typeof v.description !== "string" || v.description.length > 1e3) || !["issued", "redeemed", "expired", "voided"].includes(v.state) || !time(v.issuedAt) || !time(v.expiresAt))
|
|
1643
|
+
invalidBenefitVoucherResponse();
|
|
1644
|
+
if (mine) {
|
|
1645
|
+
if (v.closedAt !== null && !time(v.closedAt))
|
|
1646
|
+
invalidBenefitVoucherResponse();
|
|
1647
|
+
} else {
|
|
1648
|
+
if (!uuid(v.customerId) || !uuid(v.definitionId))
|
|
1649
|
+
invalidBenefitVoucherResponse();
|
|
1650
|
+
const source = object(v.source, ["namespace", "ref"]);
|
|
1651
|
+
if (typeof source.namespace !== "string" || !/^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/.test(source.namespace) || typeof source.ref !== "string" || !source.ref.length || source.ref.length > 256)
|
|
1652
|
+
invalidBenefitVoucherResponse();
|
|
1653
|
+
if (v.closed !== null) {
|
|
1654
|
+
const closed = object(v.closed, [
|
|
1655
|
+
"at",
|
|
1656
|
+
"channel",
|
|
1657
|
+
"actor",
|
|
1658
|
+
"actorRef",
|
|
1659
|
+
"voidReason"
|
|
1660
|
+
]);
|
|
1661
|
+
const principal = object(closed.actor, ["type", "id"]);
|
|
1662
|
+
if (!time(closed.at) || !uuid(principal.id) || !["server", "console"].includes(closed.channel) || !["server_credential", "operator"].includes(principal.type) || closed.actorRef !== null && !actor(closed.actorRef) || closed.voidReason !== null && !BENEFIT_VOUCHER_VOID_REASONS.includes(closed.voidReason))
|
|
1663
|
+
invalidBenefitVoucherResponse();
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
return value;
|
|
1667
|
+
}
|
|
1668
|
+
function parseBenefitVoucherList(value, mine = false) {
|
|
1669
|
+
const v = object(value, ["vouchers", "nextCursor", "evaluatedAt"]);
|
|
1670
|
+
if (!Array.isArray(v.vouchers) || v.vouchers.length > 100 || !time(v.evaluatedAt) || v.nextCursor !== null && (typeof v.nextCursor !== "string" || !v.nextCursor.length || v.nextCursor.length > 2048))
|
|
1671
|
+
invalidBenefitVoucherResponse();
|
|
1672
|
+
for (const row of v.vouchers) {
|
|
1673
|
+
if (mine) parseBenefitVoucher(row, true);
|
|
1674
|
+
else parseBenefitVoucher(row);
|
|
1675
|
+
}
|
|
1676
|
+
return value;
|
|
1677
|
+
}
|
|
1678
|
+
|
|
1679
|
+
// src/core/api/customer-benefit-vouchers-api.ts
|
|
1680
|
+
var CustomerBenefitVouchersApi = class extends CustomerScopedApi {
|
|
1681
|
+
async listMine(params = {}) {
|
|
1682
|
+
const query = new URLSearchParams();
|
|
1683
|
+
for (const [key, value] of Object.entries(params))
|
|
1684
|
+
if (value !== void 0) query.set(key, String(value));
|
|
1685
|
+
return parseBenefitVoucherList(
|
|
1686
|
+
await this.requestEndpoint(
|
|
1687
|
+
BENEFIT_VOUCHER_ENDPOINT_OPERATIONS.listMine,
|
|
1688
|
+
void 0,
|
|
1689
|
+
{ query }
|
|
1690
|
+
),
|
|
1691
|
+
true
|
|
1692
|
+
);
|
|
1693
|
+
}
|
|
1694
|
+
};
|
|
1695
|
+
|
|
1696
|
+
// src/core/collection/http-client.ts
|
|
1697
|
+
var import_qs_esm = require("qs-esm");
|
|
1698
|
+
function validateWhere(where) {
|
|
1699
|
+
if (where === void 0) return false;
|
|
1700
|
+
let hasNull = false;
|
|
1701
|
+
function visit(value, depth, key) {
|
|
1702
|
+
if (depth > 10) {
|
|
1703
|
+
throw createValidationError(
|
|
1704
|
+
"Collection where must not exceed nesting depth 10."
|
|
1705
|
+
);
|
|
1706
|
+
}
|
|
1707
|
+
if (value === null && (key === "equals" || key === "not_equals")) {
|
|
1708
|
+
hasNull = true;
|
|
1709
|
+
return;
|
|
1710
|
+
}
|
|
1711
|
+
if (value == null) {
|
|
1712
|
+
throw createValidationError(
|
|
1713
|
+
"Collection where must not contain null or undefined values."
|
|
1714
|
+
);
|
|
1715
|
+
}
|
|
1716
|
+
if (Array.isArray(value)) {
|
|
1717
|
+
if (value.length === 0) {
|
|
1718
|
+
throw createValidationError(
|
|
1719
|
+
"Collection where must not contain empty arrays, including and/or."
|
|
1720
|
+
);
|
|
1721
|
+
}
|
|
1722
|
+
if (value.length > 1e3) {
|
|
1723
|
+
throw createValidationError(
|
|
1724
|
+
"Collection where arrays must not contain more than 1000 elements."
|
|
1725
|
+
);
|
|
1726
|
+
}
|
|
1727
|
+
for (const item of value) visit(item, depth + 1);
|
|
1728
|
+
} else if (typeof value === "object" && !(value instanceof Date)) {
|
|
1729
|
+
const values = Object.entries(value);
|
|
1730
|
+
if (values.length === 0) {
|
|
1731
|
+
throw createValidationError(
|
|
1732
|
+
"Collection where must not contain empty condition objects."
|
|
1733
|
+
);
|
|
1734
|
+
}
|
|
1735
|
+
for (const [key2, item] of values) visit(item, depth + 1, key2);
|
|
1736
|
+
}
|
|
1737
|
+
}
|
|
1738
|
+
visit(where, 0);
|
|
1739
|
+
return hasNull;
|
|
1740
|
+
}
|
|
1741
|
+
var HttpClient = class {
|
|
1742
|
+
constructor(publishableKey, secretKey, getCustomerToken, onUnauthorized, onRequestId, apiUrl) {
|
|
1743
|
+
this.publishableKey = requirePublishableKeyForSecret(
|
|
1744
|
+
"CollectionClient",
|
|
1745
|
+
publishableKey,
|
|
1746
|
+
secretKey
|
|
1826
1747
|
);
|
|
1827
|
-
this.secretKey =
|
|
1828
|
-
this.
|
|
1829
|
-
this.
|
|
1830
|
-
this.
|
|
1831
|
-
this.
|
|
1748
|
+
this.secretKey = secretKey;
|
|
1749
|
+
this.getCustomerToken = getCustomerToken;
|
|
1750
|
+
this.onUnauthorized = onUnauthorized;
|
|
1751
|
+
this.onRequestId = onRequestId;
|
|
1752
|
+
this.apiUrl = apiUrl;
|
|
1832
1753
|
}
|
|
1833
|
-
|
|
1834
|
-
const
|
|
1754
|
+
get defaultOptions() {
|
|
1755
|
+
const opts = {
|
|
1756
|
+
apiUrl: this.apiUrl,
|
|
1757
|
+
publishableKey: this.publishableKey,
|
|
1758
|
+
secretKey: this.secretKey
|
|
1759
|
+
};
|
|
1760
|
+
const token = this.getCustomerToken?.();
|
|
1761
|
+
if (token) {
|
|
1762
|
+
opts.customerToken = token;
|
|
1763
|
+
if (this.onUnauthorized) {
|
|
1764
|
+
opts.onUnauthorized = this.onUnauthorized;
|
|
1765
|
+
}
|
|
1766
|
+
}
|
|
1767
|
+
return opts;
|
|
1768
|
+
}
|
|
1769
|
+
async fetchWithTracking(url, opts) {
|
|
1835
1770
|
try {
|
|
1836
|
-
const response = await httpFetch(
|
|
1837
|
-
method: options.method,
|
|
1838
|
-
apiUrl: this.apiUrl,
|
|
1839
|
-
publishableKey: this.publishableKey,
|
|
1840
|
-
secretKey: this.secretKey,
|
|
1841
|
-
customerToken: token ?? void 0,
|
|
1842
|
-
...token && this.onUnauthorized && { onUnauthorized: this.onUnauthorized },
|
|
1843
|
-
...options.body !== void 0 && {
|
|
1844
|
-
body: JSON.stringify(options.body)
|
|
1845
|
-
},
|
|
1846
|
-
...options.headers && { headers: options.headers },
|
|
1847
|
-
...options.errorFamily && { errorFamily: options.errorFamily }
|
|
1848
|
-
});
|
|
1771
|
+
const response = await httpFetch(url, opts);
|
|
1849
1772
|
this.onRequestId?.(response.headers.get("x-request-id") ?? null);
|
|
1850
|
-
return
|
|
1851
|
-
errorFamily: options.errorFamily
|
|
1852
|
-
});
|
|
1773
|
+
return response;
|
|
1853
1774
|
} catch (err) {
|
|
1854
1775
|
const id = err instanceof SDKError ? err.requestId ?? null : null;
|
|
1855
1776
|
this.onRequestId?.(id);
|
|
1856
1777
|
throw err;
|
|
1857
1778
|
}
|
|
1858
1779
|
}
|
|
1780
|
+
buildUrl(endpoint, options) {
|
|
1781
|
+
if (!options) return endpoint;
|
|
1782
|
+
const { fallbackLocale, ...rest } = options;
|
|
1783
|
+
const queryOptions = fallbackLocale === void 0 ? rest : {
|
|
1784
|
+
...rest,
|
|
1785
|
+
"fallback-locale": fallbackLocale === false ? "none" : fallbackLocale
|
|
1786
|
+
};
|
|
1787
|
+
const encodedOptions = validateWhere(queryOptions.where) ? { ...queryOptions, where: JSON.stringify(queryOptions.where) } : queryOptions;
|
|
1788
|
+
const queryString = (0, import_qs_esm.stringify)(encodedOptions, { addQueryPrefix: true });
|
|
1789
|
+
return queryString ? `${endpoint}${queryString}` : endpoint;
|
|
1790
|
+
}
|
|
1791
|
+
assertJsonResponse(response) {
|
|
1792
|
+
const contentType = response.headers.get("content-type");
|
|
1793
|
+
if (!contentType?.includes("application/json")) {
|
|
1794
|
+
throw createApiError("Response is not in JSON format.", response.status, {
|
|
1795
|
+
contentType
|
|
1796
|
+
});
|
|
1797
|
+
}
|
|
1798
|
+
}
|
|
1799
|
+
/**
|
|
1800
|
+
* Parse Payload CMS find response (list query)
|
|
1801
|
+
* Returns native Payload response structure
|
|
1802
|
+
*/
|
|
1803
|
+
async parseFindResponse(response) {
|
|
1804
|
+
const contentType = response.headers.get("content-type");
|
|
1805
|
+
try {
|
|
1806
|
+
this.assertJsonResponse(response);
|
|
1807
|
+
const jsonData = await response.json();
|
|
1808
|
+
if (jsonData.docs === void 0) {
|
|
1809
|
+
throw createApiError("Invalid find response.", response.status, {
|
|
1810
|
+
jsonData
|
|
1811
|
+
});
|
|
1812
|
+
}
|
|
1813
|
+
return {
|
|
1814
|
+
docs: jsonData.docs,
|
|
1815
|
+
totalDocs: jsonData.totalDocs ?? 0,
|
|
1816
|
+
limit: jsonData.limit || 20,
|
|
1817
|
+
totalPages: jsonData.totalPages ?? 0,
|
|
1818
|
+
page: jsonData.page || 1,
|
|
1819
|
+
pagingCounter: jsonData.pagingCounter || 1,
|
|
1820
|
+
hasPrevPage: jsonData.hasPrevPage ?? false,
|
|
1821
|
+
hasNextPage: jsonData.hasNextPage ?? false,
|
|
1822
|
+
prevPage: jsonData.prevPage ?? null,
|
|
1823
|
+
nextPage: jsonData.nextPage ?? null
|
|
1824
|
+
};
|
|
1825
|
+
} catch (error) {
|
|
1826
|
+
if (error instanceof SDKError) throw error;
|
|
1827
|
+
throw createApiError("Failed to parse response.", response.status, {
|
|
1828
|
+
contentType,
|
|
1829
|
+
error: error instanceof Error ? error.message : error
|
|
1830
|
+
});
|
|
1831
|
+
}
|
|
1832
|
+
}
|
|
1833
|
+
/**
|
|
1834
|
+
* Parse Payload CMS mutation response (create/update)
|
|
1835
|
+
* Returns native Payload response structure
|
|
1836
|
+
*/
|
|
1837
|
+
async parseMutationResponse(response) {
|
|
1838
|
+
const contentType = response.headers.get("content-type");
|
|
1839
|
+
try {
|
|
1840
|
+
this.assertJsonResponse(response);
|
|
1841
|
+
const jsonData = await response.json();
|
|
1842
|
+
if (jsonData.doc === void 0) {
|
|
1843
|
+
throw createApiError("Invalid mutation response.", response.status, {
|
|
1844
|
+
jsonData
|
|
1845
|
+
});
|
|
1846
|
+
}
|
|
1847
|
+
return {
|
|
1848
|
+
message: jsonData.message || "",
|
|
1849
|
+
doc: jsonData.doc,
|
|
1850
|
+
errors: jsonData.errors
|
|
1851
|
+
};
|
|
1852
|
+
} catch (error) {
|
|
1853
|
+
if (error instanceof SDKError) throw error;
|
|
1854
|
+
throw createApiError("Failed to parse response.", response.status, {
|
|
1855
|
+
contentType,
|
|
1856
|
+
error: error instanceof Error ? error.message : error
|
|
1857
|
+
});
|
|
1858
|
+
}
|
|
1859
|
+
}
|
|
1860
|
+
/**
|
|
1861
|
+
* Parse Payload CMS document response (findById/delete)
|
|
1862
|
+
* Returns document directly without wrapper
|
|
1863
|
+
*/
|
|
1864
|
+
async parseDocumentResponse(response) {
|
|
1865
|
+
const contentType = response.headers.get("content-type");
|
|
1866
|
+
try {
|
|
1867
|
+
this.assertJsonResponse(response);
|
|
1868
|
+
const jsonData = await response.json();
|
|
1869
|
+
return jsonData;
|
|
1870
|
+
} catch (error) {
|
|
1871
|
+
if (error instanceof SDKError) throw error;
|
|
1872
|
+
throw createApiError("Failed to parse response.", response.status, {
|
|
1873
|
+
contentType,
|
|
1874
|
+
error: error instanceof Error ? error.message : error
|
|
1875
|
+
});
|
|
1876
|
+
}
|
|
1877
|
+
}
|
|
1859
1878
|
};
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1879
|
+
|
|
1880
|
+
// src/core/collection/query-builder.ts
|
|
1881
|
+
var PUBLIC_READ_DEPTH_ERROR = "Publishable collection reads require depth: 0. Use a shaped commerce endpoint or server client for populated data.";
|
|
1882
|
+
var PUBLIC_READ_JOINS_ERROR = "Publishable collection reads require joins: false. Use a shaped commerce endpoint or server client for joined data.";
|
|
1883
|
+
var PUBLIC_READ_POPULATE_ERROR = "Publishable collection reads do not support populate. Use a shaped commerce endpoint or server client for populated data.";
|
|
1884
|
+
function withDefaultPublicReadOptions(options) {
|
|
1885
|
+
const rawOptions = options;
|
|
1886
|
+
if (rawOptions?.depth !== void 0 && rawOptions.depth !== 0) {
|
|
1887
|
+
throw createValidationError(PUBLIC_READ_DEPTH_ERROR);
|
|
1863
1888
|
}
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1889
|
+
if (rawOptions?.joins !== void 0 && rawOptions.joins !== false) {
|
|
1890
|
+
throw createValidationError(PUBLIC_READ_JOINS_ERROR);
|
|
1891
|
+
}
|
|
1892
|
+
if (rawOptions?.populate !== void 0) {
|
|
1893
|
+
throw createValidationError(PUBLIC_READ_POPULATE_ERROR);
|
|
1894
|
+
}
|
|
1895
|
+
return {
|
|
1896
|
+
...options,
|
|
1897
|
+
depth: options?.depth ?? 0,
|
|
1898
|
+
joins: options?.joins ?? false
|
|
1899
|
+
};
|
|
1900
|
+
}
|
|
1901
|
+
var ReadOnlyCollectionQueryBuilder = class {
|
|
1902
|
+
constructor(api, collection) {
|
|
1903
|
+
this.api = api;
|
|
1904
|
+
this.collection = collection;
|
|
1905
|
+
}
|
|
1906
|
+
async find(options) {
|
|
1907
|
+
return this.api.requestFind(
|
|
1908
|
+
`/api/${String(this.collection)}`,
|
|
1909
|
+
withDefaultPublicReadOptions(options)
|
|
1910
|
+
);
|
|
1911
|
+
}
|
|
1912
|
+
async findById(id, options) {
|
|
1913
|
+
return this.api.requestFindById(
|
|
1914
|
+
`/api/${String(this.collection)}/${String(id)}`,
|
|
1915
|
+
withDefaultPublicReadOptions(options)
|
|
1916
|
+
);
|
|
1917
|
+
}
|
|
1918
|
+
async count(options) {
|
|
1919
|
+
return this.api.requestCount(
|
|
1920
|
+
`/api/${String(this.collection)}/count`,
|
|
1921
|
+
withDefaultPublicReadOptions(options)
|
|
1922
|
+
);
|
|
1871
1923
|
}
|
|
1872
1924
|
};
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1925
|
+
|
|
1926
|
+
// src/core/collection/collection-client.ts
|
|
1927
|
+
var ReadOnlyCollectionClient = class extends HttpClient {
|
|
1928
|
+
from(collection) {
|
|
1929
|
+
return new ReadOnlyCollectionQueryBuilder(this, collection);
|
|
1876
1930
|
}
|
|
1877
|
-
|
|
1878
|
-
|
|
1931
|
+
async requestFind(endpoint, options) {
|
|
1932
|
+
const url = this.buildUrl(endpoint, options);
|
|
1933
|
+
const response = await this.fetchWithTracking(url, {
|
|
1934
|
+
...this.defaultOptions,
|
|
1935
|
+
method: "GET"
|
|
1936
|
+
});
|
|
1937
|
+
return this.parseFindResponse(response);
|
|
1938
|
+
}
|
|
1939
|
+
async requestFindEndpoint(endpoint, data) {
|
|
1940
|
+
const response = await this.fetchWithTracking(endpoint, {
|
|
1941
|
+
...this.defaultOptions,
|
|
1942
|
+
method: "POST",
|
|
1943
|
+
body: data ? JSON.stringify(data) : void 0
|
|
1944
|
+
});
|
|
1945
|
+
return this.parseFindResponse(response);
|
|
1946
|
+
}
|
|
1947
|
+
async requestFindEndpointGet(endpoint) {
|
|
1948
|
+
const response = await this.fetchWithTracking(endpoint, {
|
|
1949
|
+
...this.defaultOptions,
|
|
1950
|
+
method: "GET"
|
|
1951
|
+
});
|
|
1952
|
+
return this.parseFindResponse(response);
|
|
1953
|
+
}
|
|
1954
|
+
async requestFindById(endpoint, options) {
|
|
1955
|
+
const url = this.buildUrl(endpoint, options);
|
|
1956
|
+
const response = await this.fetchWithTracking(url, {
|
|
1957
|
+
...this.defaultOptions,
|
|
1958
|
+
method: "GET"
|
|
1959
|
+
});
|
|
1960
|
+
return this.parseDocumentResponse(response);
|
|
1961
|
+
}
|
|
1962
|
+
async requestCount(endpoint, options) {
|
|
1963
|
+
const url = this.buildUrl(endpoint, options);
|
|
1964
|
+
const response = await this.fetchWithTracking(url, {
|
|
1965
|
+
...this.defaultOptions,
|
|
1966
|
+
method: "GET"
|
|
1967
|
+
});
|
|
1968
|
+
return this.parseDocumentResponse(response);
|
|
1879
1969
|
}
|
|
1880
1970
|
};
|
|
1881
1971
|
|
|
@@ -6486,6 +6576,16 @@ var Client = class _Client {
|
|
|
6486
6576
|
this.markets = new MarketDiscovery(
|
|
6487
6577
|
(options2) => this.collections.from("markets").find(options2)
|
|
6488
6578
|
);
|
|
6579
|
+
this.benefitVouchers = new CustomerBenefitVouchersApi(
|
|
6580
|
+
"CustomerBenefitVouchersApi",
|
|
6581
|
+
{
|
|
6582
|
+
publishableKey: this.config.publishableKey,
|
|
6583
|
+
apiUrl: this.config.apiUrl,
|
|
6584
|
+
customerToken: () => this.customer.auth.getToken(),
|
|
6585
|
+
onUnauthorized,
|
|
6586
|
+
onRequestId
|
|
6587
|
+
}
|
|
6588
|
+
);
|
|
6489
6589
|
const membershipApi = new BrowserMembershipApi("MembershipApi", {
|
|
6490
6590
|
publishableKey: this.config.publishableKey,
|
|
6491
6591
|
apiUrl: this.config.apiUrl,
|