ag-common 0.0.733 → 0.0.736

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.
@@ -22,7 +22,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
22
22
  function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
23
23
  };
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.copyFile = exports.deleteFiles = exports.deleteFile = exports.putS3Object = exports.getS3Object = exports.s3 = exports.setS3 = void 0;
25
+ exports.copyFile = exports.deleteFiles = exports.deleteFile = exports.putS3Object = exports.getS3Object = exports.s3 = exports.setS3 = exports.createStorageClient = void 0;
26
26
  exports.getS3Objects = getS3Objects;
27
27
  exports.listFiles = listFiles;
28
28
  exports.getPresignedPostURL = getPresignedPostURL;
@@ -31,12 +31,46 @@ const s3_presigned_post_1 = require("@aws-sdk/s3-presigned-post");
31
31
  const array_1 = require("../../common/helpers/array");
32
32
  const log_1 = require("../../common/helpers/log");
33
33
  const object_1 = require("../../common/helpers/object");
34
- const setS3 = (region) => {
35
- const raw = new client_s3_1.S3Client({ region });
36
- return raw;
34
+ // Cache for memoization
35
+ const clientCache = new Map();
36
+ const getCacheKey = (config) => {
37
+ return JSON.stringify({
38
+ provider: config.provider,
39
+ region: config.region,
40
+ endpoint: config.endpoint,
41
+ accountId: config.accountId,
42
+ });
37
43
  };
44
+ const createStorageClient = (config) => {
45
+ const cacheKey = getCacheKey(config);
46
+ const cachedClient = clientCache.get(cacheKey);
47
+ if (cachedClient) {
48
+ return cachedClient;
49
+ }
50
+ const baseConfig = {
51
+ region: config.region,
52
+ };
53
+ let client;
54
+ if (config.provider === 'r2') {
55
+ const accountId = config.accountId || process.env.CLOUDFLARE_ACCOUNT_ID;
56
+ if (!accountId) {
57
+ throw new Error('Account ID is required for R2. Set CLOUDFLARE_ACCOUNT_ID env var or provide accountId in config');
58
+ }
59
+ client = new client_s3_1.S3Client(Object.assign(Object.assign({}, baseConfig), { endpoint: `https://${accountId}.r2.cloudflarestorage.com`, forcePathStyle: true }));
60
+ }
61
+ else {
62
+ client = new client_s3_1.S3Client(baseConfig);
63
+ }
64
+ clientCache.set(cacheKey, client);
65
+ return client;
66
+ };
67
+ exports.createStorageClient = createStorageClient;
68
+ const setS3 = (config) => (0, exports.createStorageClient)(config);
38
69
  exports.setS3 = setS3;
39
- exports.s3 = (0, exports.setS3)('ap-southeast-2');
70
+ exports.s3 = (0, exports.setS3)({
71
+ provider: 's3',
72
+ region: 'ap-southeast-2',
73
+ });
40
74
  const getS3Object = (_a) => __awaiter(void 0, [_a], void 0, function* ({ fileurl: { Bucket, Key }, }) {
41
75
  try {
42
76
  const r = yield exports.s3.send(new client_s3_1.GetObjectCommand({ Bucket, Key }));
@@ -15,7 +15,7 @@ const sleep_1 = require("../../common/helpers/sleep");
15
15
  const withRetry = (operation, operationName, opt) => __awaiter(void 0, void 0, void 0, function* () {
16
16
  let retryCount = 0;
17
17
  const baseDelay = 2000;
18
- let { maxRetries = 3 } = opt !== null && opt !== void 0 ? opt : {};
18
+ const { maxRetries = 3 } = opt !== null && opt !== void 0 ? opt : {};
19
19
  // eslint-disable-next-line
20
20
  while (true) {
21
21
  try {
@@ -1,2 +1,2 @@
1
1
  export * from './helpers';
2
- export type * from './types';
2
+ export type * from './types/index';
@@ -1,5 +1,7 @@
1
1
  import type { aws_dynamodb as dynamodb, aws_iam as iam, aws_lambda as lambda } from 'aws-cdk-lib';
2
- import type { Key } from './aws';
2
+ export interface Key {
3
+ [key: string]: string | number;
4
+ }
3
5
  export interface DYNAMOKEYS {
4
6
  type: string;
5
7
  L1: string;
@@ -1,4 +1,16 @@
1
- export declare const arrayToObject: <TIn, Indexer extends string | number, TOut>(arr: TIn[], keyF: (a: TIn) => Indexer, valueF: (a: TIn) => TOut) => { [a in Indexer]: TOut; };
1
+ export declare const arrayToObject: <TIn, Indexer extends string | number, TOut>(
2
+ /**
3
+ * array items
4
+ */
5
+ arr: TIn[],
6
+ /**
7
+ * from an array item, get the indexer
8
+ */
9
+ keyF: (a: TIn) => Indexer,
10
+ /**
11
+ * from an array item, return the new value
12
+ */
13
+ valueF: (a: TIn) => TOut) => { [a in Indexer]: TOut; };
2
14
  export declare const flat: <T>(arr: T[][]) => T[];
3
15
  export declare const take: <T>(array: T[], num: number) => {
4
16
  part: T[];
@@ -1,5 +1,5 @@
1
1
  import { Buffer } from 'buffer';
2
- export declare const toBuffer: (ab: ArrayBuffer) => Buffer<ArrayBufferLike>;
2
+ export declare const toBuffer: (ab: ArrayBuffer) => Buffer<ArrayBuffer>;
3
3
  export declare function bufferToArrayBuffer(buffer: Buffer): ArrayBuffer;
4
4
  export declare function b64ToArrayBuffer(base64: string): ArrayBuffer;
5
5
  /**
@@ -13,4 +13,4 @@ export declare function arrayBufferToBase64(buffer: ArrayBuffer): string;
13
13
  * @param raw
14
14
  * @returns
15
15
  */
16
- export declare const base64ToBinary: (raw: string) => Buffer<ArrayBufferLike>;
16
+ export declare const base64ToBinary: (raw: string) => Buffer<ArrayBuffer>;
@@ -37,7 +37,9 @@ export interface IUtcDateParams {
37
37
  minutes: number;
38
38
  seconds: number;
39
39
  }
40
- export declare const getUtcDateTime: (skipTime?: boolean) => {
40
+ export declare const getUtcDateTime: (
41
+ /** if true will 0 H,M,S */
42
+ skipTime?: boolean) => {
41
43
  ticks: number;
42
44
  };
43
45
  export declare const toMs: ({ day, hours, minutes, month, seconds, year, }: IUtcDateParams) => number;
@@ -57,7 +57,9 @@ export declare const filterObject: <TA extends Record<string | number, TB>, TB>(
57
57
  /** remove key values from an object where the value is null or undefined or other specific passed in values */
58
58
  export declare const removeUndefValuesFromObject: <TA>(orig: Record<string, TA>) => Record<string, TA extends null | undefined ? never : TA>;
59
59
  /** remove key values from an object where the value is null or undefined or other specific passed in values */
60
- export declare const removeUndefValuesFromObjectAdditional: <T>(orig: Record<string, T>, ...additionalRemoves: T[]) => Record<string, T>;
60
+ export declare const removeUndefValuesFromObjectAdditional: <T>(orig: Record<string, T>,
61
+ /** other than null or undefined */
62
+ ...additionalRemoves: T[]) => Record<string, T>;
61
63
  /**
62
64
  * cast Record<string,string[]|string> to Record<string,string>. can be used for querystring params
63
65
  * @param orig
@@ -17,7 +17,7 @@ function getStringFromStream(stream) {
17
17
  const reader = stream.getReader();
18
18
  let result = '';
19
19
  try {
20
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition,no-constant-condition
20
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
21
21
  while (true) {
22
22
  const { done, value } = yield reader.read();
23
23
  if (done)
@@ -15,7 +15,8 @@ const BGcss = (0, react_1.css) `
15
15
  display: flex;
16
16
  border: 0;
17
17
  padding: 3px;
18
- background-image: linear-gradient(white, white),
18
+ background-image:
19
+ linear-gradient(white, white),
19
20
  linear-gradient(to bottom right, var(--left), var(--right));
20
21
  background-origin: border-box;
21
22
  background-clip: content-box, border-box;
@@ -23,7 +23,7 @@ function getCookieRawWrapper({ name, cookieDocument, defaultValue, parse: parseR
23
23
  };
24
24
  let raw = '';
25
25
  let currentCount = 0;
26
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition,no-constant-condition
26
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
27
27
  while (true) {
28
28
  const newv = (0, raw_1.getCookie)({
29
29
  name: name + currentCount,
@@ -48,7 +48,7 @@ function wipeCookies(name) {
48
48
  return;
49
49
  }
50
50
  let currentCount = 0;
51
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition,no-constant-condition
51
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
52
52
  while (true) {
53
53
  if ((0, exports.getCookie)({
54
54
  name: name + currentCount,
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.733",
2
+ "version": "0.0.736",
3
3
  "name": "ag-common",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
@@ -18,56 +18,59 @@
18
18
  "test": "globstar -- node --import tsx --test \"src/**/*.test.ts\""
19
19
  },
20
20
  "dependencies": {
21
- "@aws-sdk/client-dynamodb": "3.744.0",
22
- "@aws-sdk/client-s3": "3.744.0",
23
- "@aws-sdk/client-ses": "3.744.0",
24
- "@aws-sdk/client-sqs": "3.744.0",
25
- "@aws-sdk/client-sts": "3.744.0",
26
- "@aws-sdk/lib-dynamodb": "3.744.0",
27
- "@aws-sdk/s3-presigned-post": "3.744.0",
28
- "@aws-sdk/util-dynamodb": "3.744.0",
29
- "aws-cdk-lib": "2.178.1",
30
- "axios": "1.7.9",
31
- "buffer": "6.0.3",
32
- "constructs": "10.4.2",
33
- "jsonwebtoken": "9.0.2",
34
- "jwks-rsa": "3.1.0",
35
- "node-cache": "5.1.2",
36
- "openapi-request-validator": "12.1.3",
37
- "react": "19.0.0",
38
- "react-dom": "19.0.0",
39
- "typescript": "5.7.3"
21
+ "@aws-sdk/client-dynamodb": "^3.788.0",
22
+ "@aws-sdk/client-s3": "^3.787.0",
23
+ "@aws-sdk/client-ses": "^3.787.0",
24
+ "@aws-sdk/client-sqs": "^3.787.0",
25
+ "@aws-sdk/client-sts": "^3.787.0",
26
+ "@aws-sdk/lib-dynamodb": "^3.789.0",
27
+ "@aws-sdk/s3-presigned-post": "^3.787.0",
28
+ "@aws-sdk/util-dynamodb": "^3.788.0",
29
+ "aws-cdk-lib": "^2.189.1",
30
+ "axios": "^1.8.4",
31
+ "buffer": "^6.0.3",
32
+ "constructs": "^10.4.2",
33
+ "jsonwebtoken": "^9.0.2",
34
+ "jwks-rsa": "^3.2.0",
35
+ "node-cache": "^5.1.2",
36
+ "openapi-request-validator": "^12.1.3",
37
+ "react": "^19.1.0",
38
+ "react-dom": "^19.1.0",
39
+ "typescript": "^5.8.3"
40
40
  },
41
41
  "devDependencies": {
42
- "@babel/core": "7.26.7",
43
- "@babel/preset-typescript": "7.26.0",
44
- "@babel/types": "7.26.7",
45
- "@emotion/react": "11.14.0",
46
- "@emotion/styled": "11.14.0",
47
- "@smithy/types": "4.1.0",
48
- "@storybook/addon-actions": "8.1.6",
49
- "@storybook/addon-docs": "8.1.6",
50
- "@storybook/addon-essentials": "8.1.6",
51
- "@storybook/addon-interactions": "8.1.6",
52
- "@storybook/addon-links": "8.1.6",
53
- "@storybook/addon-webpack5-compiler-swc": "1.0.3",
54
- "@storybook/addons": "7.6.17",
55
- "@storybook/react": "8.1.6",
56
- "@storybook/react-webpack5": "8.1.6",
57
- "@storybook/theming": "8.1.6",
58
- "@types/jsonwebtoken": "9.0.8",
59
- "@types/node": "22.13.1",
60
- "@types/react": "19.0.8",
61
- "@types/react-dom": "19.0.3",
62
- "cross-env": "7.0.3",
63
- "eslint-config-e7npm": "0.1.13",
64
- "globstar": "1.0.0",
65
- "rimraf": "6.0.1",
66
- "storybook": "8.1.6",
67
- "tsx": "4.19.2"
42
+ "@babel/core": "^7.26.10",
43
+ "@babel/preset-typescript": "^7.27.0",
44
+ "@babel/types": "^7.27.0",
45
+ "@emotion/react": "^11.14.0",
46
+ "@emotion/styled": "^11.14.0",
47
+ "@smithy/types": "^4.2.0",
48
+ "@storybook/addon-actions": "^8.6.12",
49
+ "@storybook/addon-docs": "8.6.12",
50
+ "@storybook/addon-essentials": "^8.6.12",
51
+ "@storybook/addon-interactions": "^8.6.12",
52
+ "@storybook/addon-links": "^8.6.12",
53
+ "@storybook/addon-webpack5-compiler-swc": "^3.0.0",
54
+ "@storybook/addons": "^7.6.17",
55
+ "@storybook/react": "^8.6.12",
56
+ "@storybook/react-webpack5": "^8.6.12",
57
+ "@storybook/theming": "^8.6.12",
58
+ "@types/jsonwebtoken": "^9.0.9",
59
+ "@types/node": "^22.14.1",
60
+ "@types/react": "^19.1.2",
61
+ "@types/react-dom": "^19.1.2",
62
+ "@typescript-eslint/eslint-plugin": "^8.30.1",
63
+ "@typescript-eslint/parser": "^8.30.1",
64
+ "cross-env": "^7.0.3",
65
+ "eslint": "^9.24.0",
66
+ "eslint-config-e7npm": "^0.1.23",
67
+ "globstar": "^1.0.0",
68
+ "rimraf": "^6.0.1",
69
+ "storybook": "^8.6.12",
70
+ "tsx": "^4.19.3"
68
71
  },
69
72
  "resolutions": {
70
- "globals": "15.14.0"
73
+ "globals": "15.15.0"
71
74
  },
72
75
  "files": [
73
76
  "dist/**/*",
@@ -1,145 +0,0 @@
1
- import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
2
- import type { AwsCredentialIdentity } from '@smithy/types';
3
- type DynamoDBError = {
4
- error: string;
5
- };
6
- type DynamoDBSuccess<T> = {
7
- data: T;
8
- };
9
- type DynamoDBResult<T> = DynamoDBSuccess<T> | DynamoDBError;
10
- interface Key {
11
- [key: string]: string | number;
12
- }
13
- interface DynamoFilter {
14
- filterExpression: string;
15
- attrNames: Record<string, string>;
16
- attrValues?: Record<string, unknown>;
17
- }
18
- interface ScanOptions {
19
- /** eg
20
- * filter: {
21
- filterExpression: '#feedIcon = :empty',
22
- attrNames: { '#feedIcon': 'feedIcon' },
23
- attrValues: {
24
- ':empty': '',
25
- },
26
- },
27
- */
28
- filter?: DynamoFilter;
29
- requiredAttributeList?: string[];
30
- indexName?: string;
31
- alwaysRetry?: boolean;
32
- }
33
- interface DynamoQueryParams {
34
- tableName: string;
35
- pkName: string;
36
- pkValue: string | number;
37
- pkOperator?: '=' | '<' | '>' | '<=' | '>=';
38
- skName?: string;
39
- skValue?: string | number | [string | number, string | number];
40
- skOperator?: '=' | '<' | '>' | '<=' | '>=' | 'BETWEEN' | 'BEGINS_WITH';
41
- indexName?: string;
42
- limit?: number;
43
- startKey?: Key;
44
- filter?: DynamoFilter;
45
- sortAscending?: boolean;
46
- }
47
- export declare let dynamoDb: DynamoDBDocument;
48
- /**
49
- * Sets up the DynamoDB client with the specified region and credentials.
50
- * @param region - AWS region to connect to
51
- * @param credentials - Optional AWS credentials
52
- * @returns Configured DynamoDBDocument client
53
- */
54
- export declare const setDynamo: (region: string, credentials?: AwsCredentialIdentity) => DynamoDBDocument;
55
- /**
56
- * Puts a single item into a DynamoDB table.
57
- * @param item - The item to put into the table
58
- * @param tableName - Name of the DynamoDB table
59
- * @param opt - Optional parameters including primary key name for conditional put
60
- * @returns Promise resolving to void on success or error message on failure
61
- */
62
- export declare const putDynamo: <T extends Record<string, unknown>>(item: T, tableName: string, opt?: {
63
- pkName?: string;
64
- }) => Promise<DynamoDBResult<void>>;
65
- /**
66
- * Writes multiple items to a DynamoDB table in batches.
67
- * Automatically chunks items into batches of 20 (or specified size) to comply with DynamoDB limits.
68
- * @param tableName - Name of the DynamoDB table
69
- * @param items - Array of items to write
70
- * @param opt - Optional parameters including batch size and retry behavior
71
- * @returns Promise resolving to void on success or error message on failure
72
- */
73
- export declare const batchWrite: <T extends Record<string, unknown>>(tableName: string, items: T[], opt?: {
74
- /** option to always retry on 429 until done */
75
- alwaysRetry?: boolean;
76
- /** default 20 */
77
- batchSize?: number;
78
- }) => Promise<DynamoDBResult<void>>;
79
- /**
80
- * Deletes multiple items from a DynamoDB table in batches.
81
- * Automatically chunks keys into batches of 20 (or specified size) to comply with DynamoDB limits.
82
- * @param params - Parameters including table name, keys to delete, and options
83
- * @returns Promise resolving to void on success or error message on failure
84
- */
85
- export declare const batchDelete: (params: {
86
- tableName: string;
87
- keys: string[];
88
- pkName: string;
89
- opt?: {
90
- /** default 20 */
91
- batchSize?: number;
92
- /** option to always retry on 429 until done. default false */
93
- alwaysRetry?: boolean;
94
- };
95
- }) => Promise<DynamoDBResult<void>>;
96
- /**
97
- * Scans a DynamoDB table and returns all matching items.
98
- * Handles pagination automatically and supports filtering and projection.
99
- * @param tableName - Name of the DynamoDB table
100
- * @param options - Optional parameters for filtering, projection, and index usage
101
- * @returns Promise resolving to array of items on success or error message on failure
102
- */
103
- export declare const scan: <T>(tableName: string, options?: ScanOptions) => Promise<DynamoDBResult<T[]>>;
104
- /**
105
- * Scans a DynamoDB table and yields items in batches.
106
- * Useful for processing large tables without loading all items into memory.
107
- * @param tableName - Name of the DynamoDB table
108
- * @param options - Optional parameters including batch size, filtering, and projection
109
- * @returns AsyncGenerator yielding batches of items
110
- * @throws Error if the scan operation fails
111
- */
112
- export declare function scanWithGenerator<T>(tableName: string, options?: ScanOptions & {
113
- /** how many to return in scan generator. default 100 */
114
- BATCH_SIZE?: number;
115
- }): AsyncGenerator<T[], void, unknown>;
116
- export declare const getItemsDynamo: <T>(params: {
117
- tableName: string;
118
- items: {
119
- pkName: string;
120
- pkValue: string;
121
- }[];
122
- }) => Promise<DynamoDBResult<T[]>>;
123
- export declare const getItemDynamo: <T>(params: {
124
- tableName: string;
125
- pkName: string;
126
- pkValue: string;
127
- }) => Promise<DynamoDBResult<T>>;
128
- export declare const queryDynamo: <T>(params: DynamoQueryParams) => Promise<{
129
- data: T[];
130
- startKey?: Key;
131
- } | {
132
- error: string;
133
- }>;
134
- export declare const getDynamoTtlDays: (days: number) => number;
135
- export declare const getDynamoTtlMinutes: (minutes: number) => number;
136
- export declare const wipeTable: (tableName: string) => Promise<DynamoDBResult<void>>;
137
- export declare const getDynamoUpdates: <T extends Record<string, unknown>>(item: T, options?: {
138
- excludeKeys?: string[];
139
- }) => {
140
- UpdateExpression: string;
141
- ExpressionAttributeNames: Record<string, string>;
142
- ExpressionAttributeValues: Record<string, unknown>;
143
- ReturnValues: "UPDATED_NEW";
144
- };
145
- export {};