@adobe-commerce/aio-toolkit 1.0.1 → 1.0.2
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 +44 -0
- package/README.md +66 -10
- package/dist/index.d.mts +6 -5
- package/dist/index.d.ts +6 -5
- package/dist/index.js +56 -17
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +56 -17
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -423,39 +423,44 @@ var _FileRepository = class _FileRepository {
|
|
|
423
423
|
/**
|
|
424
424
|
* Saves a file record to the repository
|
|
425
425
|
* @param payload - The data to save
|
|
426
|
-
* @
|
|
426
|
+
* @param id - Optional ID for the file (sanitized to alphanumeric + underscore, takes precedence over payload.id)
|
|
427
|
+
* @returns Promise<string | null> The filename on success, null on failure
|
|
427
428
|
*/
|
|
428
|
-
async save(payload = {}) {
|
|
429
|
+
async save(payload = {}, id) {
|
|
429
430
|
try {
|
|
430
431
|
const filesLib = await this.getFiles();
|
|
431
|
-
let
|
|
432
|
-
if (
|
|
433
|
-
|
|
432
|
+
let fileId;
|
|
433
|
+
if (id) {
|
|
434
|
+
fileId = this.sanitizeFileId(id);
|
|
435
|
+
} else if ("id" in payload && payload.id !== void 0) {
|
|
436
|
+
fileId = String(payload.id);
|
|
437
|
+
} else {
|
|
438
|
+
fileId = String((/* @__PURE__ */ new Date()).getTime());
|
|
434
439
|
}
|
|
435
|
-
const filepath = `${this.filepath}/${
|
|
440
|
+
const filepath = `${this.filepath}/${fileId}.json`;
|
|
436
441
|
const existingFile = await filesLib.list(filepath);
|
|
437
442
|
if (existingFile.length) {
|
|
438
443
|
const buffer = await filesLib.read(filepath);
|
|
439
444
|
const existingData = JSON.parse(buffer.toString());
|
|
440
445
|
payload = {
|
|
441
446
|
...payload,
|
|
442
|
-
|
|
447
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
443
448
|
};
|
|
444
449
|
payload = { ...existingData, ...payload };
|
|
445
450
|
await filesLib.delete(filepath);
|
|
446
451
|
} else {
|
|
447
452
|
payload = {
|
|
448
453
|
...payload,
|
|
449
|
-
id:
|
|
450
|
-
|
|
451
|
-
|
|
454
|
+
id: fileId,
|
|
455
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
456
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
452
457
|
};
|
|
453
458
|
}
|
|
454
459
|
await filesLib.write(filepath, JSON.stringify(payload));
|
|
455
|
-
return
|
|
460
|
+
return fileId;
|
|
456
461
|
} catch (error) {
|
|
457
462
|
console.error("Error saving file:", error);
|
|
458
|
-
return
|
|
463
|
+
return null;
|
|
459
464
|
}
|
|
460
465
|
}
|
|
461
466
|
/**
|
|
@@ -470,6 +475,21 @@ var _FileRepository = class _FileRepository {
|
|
|
470
475
|
}
|
|
471
476
|
return await this.list();
|
|
472
477
|
}
|
|
478
|
+
/**
|
|
479
|
+
* Sanitizes the file ID to contain only alphanumeric characters and underscores
|
|
480
|
+
* @param id - The ID to sanitize
|
|
481
|
+
* @returns Sanitized ID with invalid characters replaced by underscores
|
|
482
|
+
*/
|
|
483
|
+
sanitizeFileId(id) {
|
|
484
|
+
if (!id || typeof id !== "string") {
|
|
485
|
+
return String((/* @__PURE__ */ new Date()).getTime());
|
|
486
|
+
}
|
|
487
|
+
const sanitized = id.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
488
|
+
if (!sanitized || /^_+$/.test(sanitized)) {
|
|
489
|
+
return String((/* @__PURE__ */ new Date()).getTime());
|
|
490
|
+
}
|
|
491
|
+
return sanitized;
|
|
492
|
+
}
|
|
473
493
|
/**
|
|
474
494
|
* Initializes and returns the Files library instance
|
|
475
495
|
* @returns Promise<any> Initialized Files library instance
|
|
@@ -647,13 +667,32 @@ var _RestClient = class _RestClient {
|
|
|
647
667
|
headers
|
|
648
668
|
};
|
|
649
669
|
if (payload !== null) {
|
|
670
|
+
let body;
|
|
671
|
+
let contentType;
|
|
672
|
+
if (payload instanceof URLSearchParams) {
|
|
673
|
+
body = payload.toString();
|
|
674
|
+
contentType = headers["Content-Type"] || "application/x-www-form-urlencoded";
|
|
675
|
+
} else if (typeof FormData !== "undefined" && payload instanceof FormData) {
|
|
676
|
+
body = payload;
|
|
677
|
+
contentType = headers["Content-Type"];
|
|
678
|
+
} else if (typeof payload === "string") {
|
|
679
|
+
body = payload;
|
|
680
|
+
contentType = headers["Content-Type"] || "text/plain";
|
|
681
|
+
} else if (payload instanceof Buffer || payload instanceof ArrayBuffer || typeof Uint8Array !== "undefined" && payload instanceof Uint8Array) {
|
|
682
|
+
body = payload;
|
|
683
|
+
contentType = headers["Content-Type"] || "application/octet-stream";
|
|
684
|
+
} else {
|
|
685
|
+
body = JSON.stringify(payload);
|
|
686
|
+
contentType = headers["Content-Type"] || "application/json";
|
|
687
|
+
}
|
|
688
|
+
const requestHeaders = { ...headers };
|
|
689
|
+
if (contentType) {
|
|
690
|
+
requestHeaders["Content-Type"] = contentType;
|
|
691
|
+
}
|
|
650
692
|
options = {
|
|
651
693
|
...options,
|
|
652
|
-
body
|
|
653
|
-
headers:
|
|
654
|
-
...headers,
|
|
655
|
-
"Content-Type": "application/json"
|
|
656
|
-
}
|
|
694
|
+
body,
|
|
695
|
+
headers: requestHeaders
|
|
657
696
|
};
|
|
658
697
|
}
|
|
659
698
|
return await fetch(endpoint, options);
|