ag-common 0.0.893 → 0.0.895

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.
Files changed (94) hide show
  1. package/dist/api/helpers/acm.js +7 -17
  2. package/dist/api/helpers/api.js +28 -23
  3. package/dist/api/helpers/apigw.js +54 -71
  4. package/dist/api/helpers/cosmos/delete.js +81 -94
  5. package/dist/api/helpers/cosmos/get.js +170 -195
  6. package/dist/api/helpers/cosmos/index.js +23 -53
  7. package/dist/api/helpers/cosmos/write.js +88 -92
  8. package/dist/api/helpers/dynamo/delete.js +22 -52
  9. package/dist/api/helpers/dynamo/get.js +145 -135
  10. package/dist/api/helpers/dynamo/index.d.ts +2 -2
  11. package/dist/api/helpers/dynamo/set.js +22 -29
  12. package/dist/api/helpers/enforceDynamoProvisionCap.js +5 -5
  13. package/dist/api/helpers/google/apikey.js +1 -2
  14. package/dist/api/helpers/google/gemini.js +26 -37
  15. package/dist/api/helpers/retryOnError.js +15 -26
  16. package/dist/api/helpers/s3.d.ts +2 -2
  17. package/dist/api/helpers/s3.js +102 -121
  18. package/dist/api/helpers/ses.js +19 -26
  19. package/dist/api/helpers/sqs.js +5 -15
  20. package/dist/api/helpers/ssmInfra/dynamo.js +9 -5
  21. package/dist/api/helpers/sts.js +26 -37
  22. package/dist/api/helpers/validations.js +7 -17
  23. package/dist/api/helpers/zod.js +1 -2
  24. package/dist/common/helpers/async.js +27 -42
  25. package/dist/common/helpers/csv.js +1 -4
  26. package/dist/common/helpers/date.js +2 -2
  27. package/dist/common/helpers/fetch.js +42 -55
  28. package/dist/common/helpers/generator.js +11 -22
  29. package/dist/common/helpers/i18n.js +3 -5
  30. package/dist/common/helpers/log.js +4 -4
  31. package/dist/common/helpers/math.js +1 -2
  32. package/dist/common/helpers/node-cache.js +10 -2
  33. package/dist/common/helpers/random.js +2 -2
  34. package/dist/common/helpers/secondsInNearest.js +1 -2
  35. package/dist/common/helpers/stream.js +24 -35
  36. package/dist/common/helpers/string/redact.js +1 -2
  37. package/dist/common/helpers/withRetry.js +5 -14
  38. package/dist/common/helpers/xml.js +9 -22
  39. package/dist/node/helpers/fetch.js +22 -33
  40. package/dist/ui/components/DarkMode/Base.js +10 -5
  41. package/dist/ui/components/Markdown/index.js +4 -5
  42. package/dist/ui/components/shadcn/accordion.js +13 -36
  43. package/dist/ui/components/shadcn/alert.d.ts +1 -1
  44. package/dist/ui/components/shadcn/alert.js +3 -23
  45. package/dist/ui/components/shadcn/avatar.js +4 -24
  46. package/dist/ui/components/shadcn/button.d.ts +1 -1
  47. package/dist/ui/components/shadcn/button.js +2 -14
  48. package/dist/ui/components/shadcn/card.js +6 -35
  49. package/dist/ui/components/shadcn/checkbox.js +4 -18
  50. package/dist/ui/components/shadcn/dialog.js +17 -46
  51. package/dist/ui/components/shadcn/dropdown-list.js +2 -2
  52. package/dist/ui/components/shadcn/dropdown-menu.js +22 -58
  53. package/dist/ui/components/shadcn/input-selector.js +4 -5
  54. package/dist/ui/components/shadcn/input.js +2 -14
  55. package/dist/ui/components/shadcn/label.d.ts +1 -1
  56. package/dist/ui/components/shadcn/label.js +2 -16
  57. package/dist/ui/components/shadcn/popover.js +3 -17
  58. package/dist/ui/components/shadcn/radio-group.js +4 -17
  59. package/dist/ui/components/shadcn/scroll-area.js +9 -26
  60. package/dist/ui/components/shadcn/select.js +23 -55
  61. package/dist/ui/components/shadcn/sheet.d.ts +1 -1
  62. package/dist/ui/components/shadcn/sheet.js +13 -42
  63. package/dist/ui/components/shadcn/switch.js +3 -17
  64. package/dist/ui/components/shadcn/textarea.js +2 -14
  65. package/dist/ui/components/shadcn/toast.js +6 -4
  66. package/dist/ui/helpers/cookie/get.js +5 -1
  67. package/dist/ui/helpers/cookie/raw.js +1 -2
  68. package/dist/ui/helpers/cookie/set.js +2 -2
  69. package/dist/ui/helpers/cookie/use.js +19 -9
  70. package/dist/ui/helpers/date.js +3 -4
  71. package/dist/ui/helpers/debounce.js +1 -1
  72. package/dist/ui/helpers/extractAttributes.js +1 -2
  73. package/dist/ui/helpers/openDialog.js +1 -2
  74. package/dist/ui/helpers/routes.js +6 -4
  75. package/dist/ui/helpers/serviceWorker.js +7 -18
  76. package/dist/ui/helpers/useContextMenu.js +2 -3
  77. package/dist/ui/helpers/useElementAttribute.js +1 -1
  78. package/dist/ui/helpers/useGranularHook.js +2 -2
  79. package/dist/ui/helpers/useInterval.js +5 -7
  80. package/dist/ui/helpers/useLocalStorage.js +1 -1
  81. package/dist/ui/helpers/useOnClickOutside.js +2 -3
  82. package/dist/ui/helpers/useOnScroll.js +6 -8
  83. package/dist/ui/helpers/useOverloadPageSearch.js +1 -1
  84. package/dist/ui/helpers/useQueryString.js +2 -2
  85. package/dist/ui/helpers/useResize.js +1 -1
  86. package/dist/ui/helpers/useTimeout.js +1 -1
  87. package/dist/ui/helpers/useTooltip.js +8 -5
  88. package/dist/ui/icons/Checkmark.js +2 -5
  89. package/dist/ui/icons/Circle.js +2 -5
  90. package/dist/ui/icons/Magnify.js +2 -5
  91. package/dist/ui/icons/Sun.js +8 -11
  92. package/dist/ui/icons/Warning.js +7 -10
  93. package/dist/ui/styles/common.js +8 -20
  94. package/package.json +56 -65
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.findCertificateByDomain = exports.acm = exports.setAcm = void 0;
13
4
  /* eslint-disable no-await-in-loop */
@@ -26,24 +17,23 @@ exports.acm = (0, exports.setAcm)('us-east-1');
26
17
  * @returns The certificate ARN
27
18
  * @throws Error if no matching certificate is found
28
19
  */
29
- const findCertificateByDomain = (domain_1, ...args_1) => __awaiter(void 0, [domain_1, ...args_1], void 0, function* (domain, region = 'us-east-1') {
30
- var _a, _b, _c;
31
- const ar = yield exports.acm.config.region();
20
+ const findCertificateByDomain = async (domain, region = 'us-east-1') => {
21
+ const ar = await exports.acm.config.region();
32
22
  if (ar !== region) {
33
23
  (0, exports.setAcm)(region);
34
24
  }
35
25
  const requiredDomains = new Set([domain, `*.${domain}`]);
36
- const listResponse = yield exports.acm.send(new client_acm_1.ListCertificatesCommand({}));
37
- if (!((_a = listResponse.CertificateSummaryList) === null || _a === void 0 ? void 0 : _a.length)) {
26
+ const listResponse = await exports.acm.send(new client_acm_1.ListCertificatesCommand({}));
27
+ if (!listResponse.CertificateSummaryList?.length) {
38
28
  throw new Error(`No ACM certificates found in region ${region}`);
39
29
  }
40
30
  for (const cert of listResponse.CertificateSummaryList) {
41
31
  if (!cert.CertificateArn)
42
32
  continue;
43
- const describeResponse = yield exports.acm.send(new client_acm_1.DescribeCertificateCommand({
33
+ const describeResponse = await exports.acm.send(new client_acm_1.DescribeCertificateCommand({
44
34
  CertificateArn: cert.CertificateArn,
45
35
  }));
46
- const sans = (_c = (_b = describeResponse.Certificate) === null || _b === void 0 ? void 0 : _b.SubjectAlternativeNames) !== null && _c !== void 0 ? _c : [];
36
+ const sans = describeResponse.Certificate?.SubjectAlternativeNames ?? [];
47
37
  const sanSet = new Set(sans);
48
38
  const hasAllDomains = [...requiredDomains].every((d) => sanSet.has(d));
49
39
  if (hasAllDomains) {
@@ -51,5 +41,5 @@ const findCertificateByDomain = (domain_1, ...args_1) => __awaiter(void 0, [doma
51
41
  }
52
42
  }
53
43
  throw new Error(`No ACM certificate found covering both "${domain}" and "*.${domain}" in region ${region}`);
54
- });
44
+ };
55
45
  exports.findCertificateByDomain = findCertificateByDomain;
@@ -1,15 +1,4 @@
1
1
  "use strict";
2
- var __rest = (this && this.__rest) || function (s, e) {
3
- var t = {};
4
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
- t[p] = s[p];
6
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
- t[p[i]] = s[p[i]];
10
- }
11
- return t;
12
- };
13
2
  Object.defineProperty(exports, "__esModule", { value: true });
14
3
  exports.generateDynamoPKS = exports.stripPKs = exports.returnCode = void 0;
15
4
  const array_1 = require("../../common/helpers/array");
@@ -21,7 +10,10 @@ const returnCode = (statusCode, body, extraHeaders, fullSiteUrl) => {
21
10
  'Access-Control-Allow-Credentials': true, // Required for cookies, authorization headers with HTTPS
22
11
  };
23
12
  if (extraHeaders && Object.keys(extraHeaders).length > 0) {
24
- headers = Object.assign(Object.assign({}, headers), extraHeaders);
13
+ headers = {
14
+ ...headers,
15
+ ...extraHeaders,
16
+ };
25
17
  }
26
18
  return {
27
19
  headers,
@@ -44,7 +36,7 @@ const stripPKs = (record, keepPk = true) => {
44
36
  }
45
37
  // @ts-ignore
46
38
  // eslint-disable-next-line
47
- const { PK, PK1, PK2, PK3, PK4, PK5, L1, L2, L3, L4, L5, L6, type } = record, rest = __rest(record, ["PK", "PK1", "PK2", "PK3", "PK4", "PK5", "L1", "L2", "L3", "L4", "L5", "L6", "type"]);
39
+ const { PK, PK1, PK2, PK3, PK4, PK5, L1, L2, L3, L4, L5, L6, type, ...rest } = record;
48
40
  if (keepPk) {
49
41
  //@ts-ignore
50
42
  rest.PK = PK;
@@ -69,15 +61,28 @@ const generateDynamoPKS = ({ type, L1, L2, L3, L4, L5, L6, additionalPKValues, }
69
61
  const additionalPK = additionalPKValues && additionalPKValues.length > 0
70
62
  ? `#${additionalPKValues.join(c)}`
71
63
  : '';
72
- return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ PK: keys.join(c) + additionalPK, type,
73
- L1 }, (L2 && { L2 })), (L3 && { L3 })), (L4 && { L4 })), (L5 && { L5 })), (L6 && { L6 })), { PK1: L1 }), (keys.length >= 2 && {
74
- PK2: [keys[0], keys[1]].join(c),
75
- })), (keys.length >= 3 && {
76
- PK3: [keys[0], keys[1], keys[2]].join(c),
77
- })), (keys.length >= 4 && {
78
- PK4: [keys[0], keys[1], keys[2], keys[3]].join(c),
79
- })), (keys.length >= 5 && {
80
- PK5: [keys[0], keys[1], keys[2], keys[3], keys[4]].join(c),
81
- }));
64
+ return {
65
+ PK: keys.join(c) + additionalPK,
66
+ type,
67
+ L1,
68
+ ...(L2 && { L2 }),
69
+ ...(L3 && { L3 }),
70
+ ...(L4 && { L4 }),
71
+ ...(L5 && { L5 }),
72
+ ...(L6 && { L6 }),
73
+ PK1: L1,
74
+ ...(keys.length >= 2 && {
75
+ PK2: [keys[0], keys[1]].join(c),
76
+ }),
77
+ ...(keys.length >= 3 && {
78
+ PK3: [keys[0], keys[1], keys[2]].join(c),
79
+ }),
80
+ ...(keys.length >= 4 && {
81
+ PK4: [keys[0], keys[1], keys[2], keys[3]].join(c),
82
+ }),
83
+ ...(keys.length >= 5 && {
84
+ PK5: [keys[0], keys[1], keys[2], keys[3], keys[4]].join(c),
85
+ }),
86
+ };
82
87
  };
83
88
  exports.generateDynamoPKS = generateDynamoPKS;
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.sendWebSocketMessage = sendWebSocketMessage;
13
4
  exports.deleteConnection = deleteConnection;
@@ -18,81 +9,73 @@ const common_1 = require("../../common");
18
9
  /**
19
10
  * Sends a message to a specific WebSocket connection
20
11
  */
21
- function sendWebSocketMessage(p) {
22
- return __awaiter(this, void 0, void 0, function* () {
23
- const wsClient = new client_apigatewaymanagementapi_1.ApiGatewayManagementApiClient(p);
24
- try {
25
- const s = yield wsClient.send(new client_apigatewaymanagementapi_1.PostToConnectionCommand({
26
- ConnectionId: p.connectionId,
27
- Data: JSON.stringify(p.message),
28
- }));
29
- if (s.$metadata.httpStatusCode === 200) {
30
- return true;
31
- }
32
- throw new Error('Error:' + JSON.stringify(s));
33
- }
34
- catch (e) {
35
- (0, common_1.warn)('Failed to send WebSocket message:', p, e);
36
- return false;
12
+ async function sendWebSocketMessage(p) {
13
+ const wsClient = new client_apigatewaymanagementapi_1.ApiGatewayManagementApiClient(p);
14
+ try {
15
+ const s = await wsClient.send(new client_apigatewaymanagementapi_1.PostToConnectionCommand({
16
+ ConnectionId: p.connectionId,
17
+ Data: JSON.stringify(p.message),
18
+ }));
19
+ if (s.$metadata.httpStatusCode === 200) {
20
+ return true;
37
21
  }
38
- });
22
+ throw new Error('Error:' + JSON.stringify(s));
23
+ }
24
+ catch (e) {
25
+ (0, common_1.warn)('Failed to send WebSocket message:', p, e);
26
+ return false;
27
+ }
39
28
  }
40
29
  /**
41
30
  * Deletes a WebSocket connection
42
31
  */
43
- function deleteConnection(_a) {
44
- return __awaiter(this, arguments, void 0, function* ({ connectionId, endpoint, }) {
45
- const wsClient = new client_apigatewaymanagementapi_1.ApiGatewayManagementApiClient({
46
- endpoint,
47
- });
48
- try {
49
- const s = yield wsClient.send(new client_apigatewaymanagementapi_1.DeleteConnectionCommand({
50
- ConnectionId: connectionId,
51
- }));
52
- if (s.$metadata.httpStatusCode === 200) {
53
- return true;
54
- }
55
- return false;
56
- }
57
- catch (e) {
58
- (0, common_1.warn)('Failed to delete WebSocket connection:', e.message);
59
- return false;
60
- }
32
+ async function deleteConnection({ connectionId, endpoint, }) {
33
+ const wsClient = new client_apigatewaymanagementapi_1.ApiGatewayManagementApiClient({
34
+ endpoint,
61
35
  });
36
+ try {
37
+ const s = await wsClient.send(new client_apigatewaymanagementapi_1.DeleteConnectionCommand({
38
+ ConnectionId: connectionId,
39
+ }));
40
+ if (s.$metadata.httpStatusCode === 200) {
41
+ return true;
42
+ }
43
+ return false;
44
+ }
45
+ catch (e) {
46
+ (0, common_1.warn)('Failed to delete WebSocket connection:', e.message);
47
+ return false;
48
+ }
62
49
  }
63
50
  /**
64
51
  * Checks if a WebSocket connection is still active
65
52
  */
66
- function isConnectionActive(_a) {
67
- return __awaiter(this, arguments, void 0, function* ({ connectionId, endpoint, }) {
68
- const wsClient = new client_apigatewaymanagementapi_1.ApiGatewayManagementApiClient({
69
- endpoint,
70
- });
71
- try {
72
- const s = yield wsClient.send(new client_apigatewaymanagementapi_1.GetConnectionCommand({
73
- ConnectionId: connectionId,
74
- }));
75
- if (s.$metadata.httpStatusCode === 200) {
76
- return true;
77
- }
78
- return false;
79
- }
80
- catch (e) {
81
- (0, common_1.warn)('Failed to check if WebSocket connection is active:', e.message);
82
- return false;
83
- }
53
+ async function isConnectionActive({ connectionId, endpoint, }) {
54
+ const wsClient = new client_apigatewaymanagementapi_1.ApiGatewayManagementApiClient({
55
+ endpoint,
84
56
  });
57
+ try {
58
+ const s = await wsClient.send(new client_apigatewaymanagementapi_1.GetConnectionCommand({
59
+ ConnectionId: connectionId,
60
+ }));
61
+ if (s.$metadata.httpStatusCode === 200) {
62
+ return true;
63
+ }
64
+ return false;
65
+ }
66
+ catch (e) {
67
+ (0, common_1.warn)('Failed to check if WebSocket connection is active:', e.message);
68
+ return false;
69
+ }
85
70
  }
86
71
  /**
87
72
  * Broadcasts a message to multiple WebSocket connections
88
73
  */
89
- function broadcastMessage(_a) {
90
- return __awaiter(this, arguments, void 0, function* ({ connectionIds, message, endpoint, }) {
91
- const results = yield Promise.all(connectionIds.map((connectionId) => sendWebSocketMessage({
92
- connectionId,
93
- message,
94
- endpoint,
95
- })));
96
- return results.every((r) => r);
97
- });
74
+ async function broadcastMessage({ connectionIds, message, endpoint, }) {
75
+ const results = await Promise.all(connectionIds.map((connectionId) => sendWebSocketMessage({
76
+ connectionId,
77
+ message,
78
+ endpoint,
79
+ })));
80
+ return results.every((r) => r);
98
81
  }
@@ -1,111 +1,98 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.deleteItemCosmos = deleteItemCosmos;
13
4
  exports.deleteItemsByField = deleteItemsByField;
14
5
  const log_1 = require("../../../common/helpers/log");
15
6
  const withRetry_1 = require("../../../common/helpers/withRetry");
16
7
  const utils_1 = require("./utils");
17
- function deleteItemCosmos(container, pk) {
18
- return __awaiter(this, void 0, void 0, function* () {
19
- try {
20
- // First query to find items with matching PK
21
- const querySpec = {
22
- query: 'SELECT * FROM c WHERE c.PK = @pk',
23
- parameters: [{ name: '@pk', value: pk }],
24
- };
25
- const { resources: items } = yield container.items
26
- .query(querySpec)
27
- .fetchAll();
28
- // Delete each found item and collect their IDs
29
- for (const item of items) {
30
- (0, log_1.warn)('deleting items:', item.id, pk);
31
- yield container.item(item.id, pk).delete();
32
- }
33
- }
34
- catch (e) {
35
- const em = e.message;
36
- (0, log_1.error)('Error deleting item:', em);
37
- throw e;
8
+ async function deleteItemCosmos(container, pk) {
9
+ try {
10
+ // First query to find items with matching PK
11
+ const querySpec = {
12
+ query: 'SELECT * FROM c WHERE c.PK = @pk',
13
+ parameters: [{ name: '@pk', value: pk }],
14
+ };
15
+ const { resources: items } = await container.items
16
+ .query(querySpec)
17
+ .fetchAll();
18
+ // Delete each found item and collect their IDs
19
+ for (const item of items) {
20
+ (0, log_1.warn)('deleting items:', item.id, pk);
21
+ await container.item(item.id, pk).delete();
38
22
  }
39
- });
23
+ }
24
+ catch (e) {
25
+ const em = e.message;
26
+ (0, log_1.error)('Error deleting item:', em);
27
+ throw e;
28
+ }
40
29
  }
41
- function deleteItemsByField(container, field, value) {
42
- return __awaiter(this, void 0, void 0, function* () {
43
- try {
44
- const query = `
30
+ async function deleteItemsByField(container, field, value) {
31
+ try {
32
+ const query = `
45
33
  SELECT c.id, c.PK
46
34
  FROM c
47
35
  WHERE c.${field} = @value
48
36
  `;
49
- const { resources } = yield container.items
50
- .query({
51
- query,
52
- parameters: [{ name: '@value', value }],
53
- })
54
- .fetchAll();
55
- if (resources.length === 0) {
56
- return {};
57
- }
58
- (0, log_1.info)(`Deleting ${resources.length} items where ${field} = ${value} from Cosmos`);
59
- //throw if item.id or PK doesn't exist on an item
60
- resources.forEach((item) => {
61
- if (!item.id || !item.PK) {
62
- throw new Error('Item missing required fields: ' + JSON.stringify(item));
63
- }
64
- });
65
- const operations = resources.map((item) => ({
66
- id: item.id,
67
- partitionKey: item.PK,
68
- operationType: 'Delete',
69
- }));
70
- for (let i = 0; i < operations.length; i += utils_1.BULK_CHUNK_SIZE) {
71
- const chunk = operations.slice(i, i + utils_1.BULK_CHUNK_SIZE);
72
- // Execute bulk operation with retry
73
- yield (0, withRetry_1.withRetry)(() => __awaiter(this, void 0, void 0, function* () {
74
- (0, log_1.info)(`Deleting chunk ${i / utils_1.BULK_CHUNK_SIZE + 1} of ${Math.ceil(operations.length / utils_1.BULK_CHUNK_SIZE)}`);
75
- const bulkResponse = yield container.items.bulk(chunk);
76
- //if an item contains a statuscode 429, then throw an error
77
- bulkResponse.forEach((response) => {
78
- if (response.statusCode === 429) {
79
- throw new Error('429');
80
- }
81
- });
82
- // Process results
83
- let successCount = 0;
84
- let failureCount = 0;
85
- bulkResponse.forEach((response, index) => {
86
- if (response.statusCode === 204) {
87
- successCount++;
88
- }
89
- else if (response.statusCode === 404) {
90
- failureCount++;
91
- }
92
- else {
93
- const item = chunk[index];
94
- (0, log_1.error)(`Failed to delete item - ID: ${item.id}, PK: ${item.partitionKey}, Status: ${response.statusCode}`, response.resourceBody);
95
- failureCount++;
96
- }
97
- });
98
- (0, log_1.info)(`Chunk ${i / utils_1.BULK_CHUNK_SIZE + 1}/${Math.ceil(operations.length / utils_1.BULK_CHUNK_SIZE)} results - Success: ${successCount}, Failed: ${failureCount}`);
99
- return bulkResponse;
100
- }), `bulk_delete_chunk_${i / utils_1.BULK_CHUNK_SIZE + 1}`);
101
- }
102
- (0, log_1.info)('All items deleted successfully');
37
+ const { resources } = await container.items
38
+ .query({
39
+ query,
40
+ parameters: [{ name: '@value', value }],
41
+ })
42
+ .fetchAll();
43
+ if (resources.length === 0) {
103
44
  return {};
104
45
  }
105
- catch (e) {
106
- const em = e.message;
107
- (0, log_1.error)('Error deleting items:', em);
108
- throw e;
46
+ (0, log_1.info)(`Deleting ${resources.length} items where ${field} = ${value} from Cosmos`);
47
+ //throw if item.id or PK doesn't exist on an item
48
+ resources.forEach((item) => {
49
+ if (!item.id || !item.PK) {
50
+ throw new Error('Item missing required fields: ' + JSON.stringify(item));
51
+ }
52
+ });
53
+ const operations = resources.map((item) => ({
54
+ id: item.id,
55
+ partitionKey: item.PK,
56
+ operationType: 'Delete',
57
+ }));
58
+ for (let i = 0; i < operations.length; i += utils_1.BULK_CHUNK_SIZE) {
59
+ const chunk = operations.slice(i, i + utils_1.BULK_CHUNK_SIZE);
60
+ // Execute bulk operation with retry
61
+ await (0, withRetry_1.withRetry)(async () => {
62
+ (0, log_1.info)(`Deleting chunk ${i / utils_1.BULK_CHUNK_SIZE + 1} of ${Math.ceil(operations.length / utils_1.BULK_CHUNK_SIZE)}`);
63
+ const bulkResponse = await container.items.bulk(chunk);
64
+ //if an item contains a statuscode 429, then throw an error
65
+ bulkResponse.forEach((response) => {
66
+ if (response.statusCode === 429) {
67
+ throw new Error('429');
68
+ }
69
+ });
70
+ // Process results
71
+ let successCount = 0;
72
+ let failureCount = 0;
73
+ bulkResponse.forEach((response, index) => {
74
+ if (response.statusCode === 204) {
75
+ successCount++;
76
+ }
77
+ else if (response.statusCode === 404) {
78
+ failureCount++;
79
+ }
80
+ else {
81
+ const item = chunk[index];
82
+ (0, log_1.error)(`Failed to delete item - ID: ${item.id}, PK: ${item.partitionKey}, Status: ${response.statusCode}`, response.resourceBody);
83
+ failureCount++;
84
+ }
85
+ });
86
+ (0, log_1.info)(`Chunk ${i / utils_1.BULK_CHUNK_SIZE + 1}/${Math.ceil(operations.length / utils_1.BULK_CHUNK_SIZE)} results - Success: ${successCount}, Failed: ${failureCount}`);
87
+ return bulkResponse;
88
+ }, `bulk_delete_chunk_${i / utils_1.BULK_CHUNK_SIZE + 1}`);
109
89
  }
110
- });
90
+ (0, log_1.info)('All items deleted successfully');
91
+ return {};
92
+ }
93
+ catch (e) {
94
+ const em = e.message;
95
+ (0, log_1.error)('Error deleting items:', em);
96
+ throw e;
97
+ }
111
98
  }