@8ms/helpers 2.0.16 → 2.0.18

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.
Binary file
@@ -40,14 +40,19 @@ export declare class AwsS3Namespace extends BaseNamespace {
40
40
  */
41
41
  listFolders: (bucket: string, folder: string) => Promise<string[]>;
42
42
  move: (fromBucket: string, fromKey: string, toBucket: string, toKey: string) => Promise<void>;
43
- readBrotli: (bucket: string, key: string, isJson?: boolean) => Promise<ReadFileResponse>;
43
+ readBrotli: (bucket: string, key: string, brotliOptions?: object, awsS3Options?: Options) => Promise<ReadFileResponse>;
44
+ readJsonBrotli: (bucket: string, key: string, brotliOptions?: object, awsS3Options?: Options) => Promise<ReadFileResponse>;
44
45
  readBuffer: (bucket: string, key: string) => Promise<ReadBufferResponse>;
45
46
  fileExists: (bucket: string, key: string) => Promise<boolean>;
46
- readFile: (bucket: string, key: string, isJson?: boolean) => Promise<ReadFileResponse>;
47
- readGzip: (bucket: string, key: string, isJson?: boolean) => Promise<ReadFileResponse>;
48
- writeBrotli: (bucket: string, key: string, data: any, isJson?: boolean, options?: Options) => Promise<import("@aws-sdk/client-s3").PutObjectCommandOutput>;
47
+ readFile: (bucket: string, key: string, options?: Options) => Promise<ReadFileResponse>;
48
+ readJson: (bucket: string, key: string) => Promise<ReadFileResponse>;
49
+ readJsonGzip: (bucket: string, key: string, gzipOptions?: object, awsS3Options?: Options) => Promise<ReadFileResponse>;
50
+ readGzip: (bucket: string, key: string, gzipOptions?: object, awsS3Options?: Options) => Promise<ReadFileResponse>;
51
+ writeBrotli: (bucket: string, key: string, data: any, brotliOptions?: object, awsS3Options?: Options) => Promise<import("@aws-sdk/client-s3").PutObjectCommandOutput>;
49
52
  writeFile: (bucket: string, key: string, data: any, options?: Options) => Promise<import("@aws-sdk/client-s3").PutObjectCommandOutput>;
50
- writeGzip: (bucket: string, key: string, data: any, isJson?: boolean, options?: Options) => Promise<import("@aws-sdk/client-s3").PutObjectCommandOutput>;
53
+ writeJson: (bucket: string, key: string, data: any, options?: Options) => Promise<import("@aws-sdk/client-s3").PutObjectCommandOutput>;
54
+ writeJsonGzip: (bucket: string, key: string, data: any, gzipOptions?: object, awsS3Options?: Options) => Promise<import("@aws-sdk/client-s3").PutObjectCommandOutput>;
55
+ writeGzip: (bucket: string, key: string, data: any, gzipOptions?: object, awsS3Options?: Options) => Promise<import("@aws-sdk/client-s3").PutObjectCommandOutput>;
51
56
  /**
52
57
  * Get a presigned post URL so we can upload directly to S3 via frontend.
53
58
  */
@@ -206,17 +206,23 @@ class AwsS3Namespace extends _class_1.BaseNamespace {
206
206
  Key: fromKey,
207
207
  }));
208
208
  };
209
- this.readBrotli = async (bucket, key, isJson = true) => {
209
+ this.readBrotli = async (bucket, key, brotliOptions = {}, awsS3Options = {}) => {
210
210
  await this.ensureInit();
211
- let response = await this.readFile(bucket, key, isJson);
211
+ const apiResponse = await this.readFile(bucket, key, awsS3Options);
212
212
  // Cache exists and is still within our caching timeframe
213
- if (undefined !== response.body) {
214
- response.body = await (0, server_2.getBrotliDecompressed)(response.body);
215
- if (false !== isJson) {
216
- response.body = JSON.parse(response.body);
217
- }
213
+ if (undefined !== apiResponse.body) {
214
+ apiResponse.body = await (0, server_2.getBrotliDecompressed)(apiResponse.body, brotliOptions);
218
215
  }
219
- return response;
216
+ return apiResponse;
217
+ };
218
+ this.readJsonBrotli = async (bucket, key, brotliOptions = {}, awsS3Options = {}) => {
219
+ await this.ensureInit();
220
+ const apiResponse = await this.readFile(bucket, key, awsS3Options);
221
+ if (undefined !== apiResponse.body) {
222
+ apiResponse.body = await (0, server_2.getBrotliDecompressed)(apiResponse.body, brotliOptions);
223
+ apiResponse.body = JSON.parse(apiResponse.body) || null;
224
+ }
225
+ return apiResponse;
220
226
  };
221
227
  this.readBuffer = async (bucket, key) => {
222
228
  await this.ensureInit();
@@ -264,7 +270,7 @@ class AwsS3Namespace extends _class_1.BaseNamespace {
264
270
  }
265
271
  return response;
266
272
  };
267
- this.readFile = async (bucket, key, isJson = true) => {
273
+ this.readFile = async (bucket, key, options = {}) => {
268
274
  await this.ensureInit();
269
275
  let response = (0, cloneDeep_1.default)(server_3.readFileDefault);
270
276
  const today = (0, date_1.getToday)();
@@ -276,6 +282,7 @@ class AwsS3Namespace extends _class_1.BaseNamespace {
276
282
  const apiResponse = await this.client.send(new GetObjectCommand({
277
283
  Bucket: bucket,
278
284
  Key: key,
285
+ ...options,
279
286
  }));
280
287
  //console.log('apiResponse', apiResponse);
281
288
  if ((0, server_1.isResponse200)(apiResponse)) {
@@ -283,19 +290,15 @@ class AwsS3Namespace extends _class_1.BaseNamespace {
283
290
  // Convert the date to unix
284
291
  response.modified.unix = modified.toUnixInteger();
285
292
  // Compare now to unix
286
- response.modified.vs_now.months = today.diff(modified, 'months').months;
287
- response.modified.vs_now.days = today.diff(modified, 'days').days;
288
- response.modified.vs_now.hours = today.diff(modified, 'hours').hours;
289
- response.modified.vs_now.minutes = today.diff(modified, 'minutes').minutes;
293
+ response.modified.vs_now.months = today.diff(modified, "months").months;
294
+ response.modified.vs_now.days = today.diff(modified, "days").days;
295
+ response.modified.vs_now.hours = today.diff(modified, "hours").hours;
296
+ response.modified.vs_now.minutes = today.diff(modified, "minutes").minutes;
290
297
  // Compare now to midnight
291
- response.modified.vs_midnight.hours = today.diff(midnight, 'hours').hours;
292
- response.modified.vs_midnight.minutes = today.diff(midnight, 'minutes').hours;
298
+ response.modified.vs_midnight.hours = today.diff(midnight, "hours").hours;
299
+ response.modified.vs_midnight.minutes = today.diff(midnight, "minutes").hours;
293
300
  // Convert stream into a string
294
301
  response.body = await (0, string_1.getStringFromStream)(apiResponse.Body);
295
- // If we need to return JSON, parse it. On error return undefined
296
- if (false !== isJson) {
297
- response.body = JSON.parse(response.body) || null;
298
- }
299
302
  }
300
303
  }
301
304
  catch (error) {
@@ -304,29 +307,34 @@ class AwsS3Namespace extends _class_1.BaseNamespace {
304
307
  }
305
308
  return response;
306
309
  };
307
- this.readGzip = async (bucket, key, isJson = true) => {
310
+ this.readJson = async (bucket, key) => {
308
311
  await this.ensureInit();
309
- let response = await this.readFile(bucket, key, isJson);
310
- // Cache exists and is still within our caching timeframe
311
- if (undefined !== response.body) {
312
- response.body = await (0, server_2.getGzipDecompressed)(response.body);
313
- if (false !== isJson) {
314
- response.body = JSON.parse(response.body);
315
- }
312
+ const apiResponse = await this.readFile(bucket, key);
313
+ apiResponse.body = JSON.parse(apiResponse.body) || null;
314
+ return apiResponse;
315
+ };
316
+ this.readJsonGzip = async (bucket, key, gzipOptions = {}, awsS3Options = {}) => {
317
+ await this.ensureInit();
318
+ const apiResponse = await this.readFile(bucket, key, awsS3Options);
319
+ if (undefined !== apiResponse.body) {
320
+ apiResponse.body = await (0, server_2.getGzipDecompressed)(apiResponse.body, gzipOptions);
321
+ apiResponse.body = JSON.parse(apiResponse.body) || null;
316
322
  }
317
- return response;
323
+ return apiResponse;
318
324
  };
319
- this.writeBrotli = async (bucket, key, data, isJson = true, options = {}) => {
325
+ this.readGzip = async (bucket, key, gzipOptions = {}, awsS3Options = {}) => {
320
326
  await this.ensureInit();
321
- let finalData = data;
322
- if (false !== isJson) {
323
- finalData = JSON.stringify(data);
327
+ const apiResponse = await this.readFile(bucket, key, awsS3Options);
328
+ if (undefined !== apiResponse.body) {
329
+ apiResponse.body = await (0, server_2.getGzipDecompressed)(apiResponse.body, gzipOptions);
324
330
  }
325
- // Compress the data
326
- const brotliData = await (0, server_2.getBrotliCompressed)({
327
- input: finalData,
328
- });
329
- const apiResponse = await this.writeFile(bucket, key, brotliData, options);
331
+ return apiResponse;
332
+ };
333
+ this.writeBrotli = async (bucket, key, data, brotliOptions = {}, awsS3Options = {}) => {
334
+ await this.ensureInit();
335
+ let finalData = data;
336
+ finalData = await (0, server_2.getBrotliCompressed)(finalData, brotliOptions);
337
+ const apiResponse = await this.writeFile(bucket, key, finalData, awsS3Options);
330
338
  return apiResponse;
331
339
  };
332
340
  this.writeFile = async (bucket, key, data, options = {}) => {
@@ -340,15 +348,23 @@ class AwsS3Namespace extends _class_1.BaseNamespace {
340
348
  }));
341
349
  return apiResponse;
342
350
  };
343
- this.writeGzip = async (bucket, key, data, isJson = true, options = {}) => {
351
+ this.writeJson = async (bucket, key, data, options = {}) => {
352
+ await this.ensureInit();
353
+ const apiResponse = await this.writeFile(bucket, key, JSON.stringify(data), options);
354
+ return apiResponse;
355
+ };
356
+ this.writeJsonGzip = async (bucket, key, data, gzipOptions = {}, awsS3Options = {}) => {
357
+ await this.ensureInit();
358
+ let finalData = JSON.stringify(data);
359
+ finalData = await (0, server_2.getGzipCompressed)(finalData, gzipOptions);
360
+ const apiResponse = await this.writeFile(bucket, key, finalData, awsS3Options);
361
+ return apiResponse;
362
+ };
363
+ this.writeGzip = async (bucket, key, data, gzipOptions = {}, awsS3Options = {}) => {
344
364
  await this.ensureInit();
345
365
  let finalData = data;
346
- if (false !== isJson) {
347
- finalData = JSON.stringify(data);
348
- }
349
- // Compress the data
350
- const gzipData = await (0, server_2.getGzipCompressed)(finalData);
351
- const apiResponse = await this.writeFile(bucket, key, gzipData, options);
366
+ finalData = await (0, server_2.getGzipCompressed)(finalData, gzipOptions);
367
+ const apiResponse = await this.writeFile(bucket, key, finalData, awsS3Options);
352
368
  return apiResponse;
353
369
  };
354
370
  /**
@@ -1,18 +1,9 @@
1
1
  import { BaseNamespace } from "../../_class";
2
2
  import { AwsConfig } from "../server";
3
- import type { SESClient } from "@aws-sdk/client-ses";
4
- type SendProps = {
5
- bcc?: string[];
6
- cc?: string[];
7
- from?: string;
8
- html?: string;
9
- subject?: string;
10
- to?: string | string[];
11
- };
3
+ import type { SendEmailCommandInput, SESClient } from "@aws-sdk/client-ses";
12
4
  export declare class AwsSesNamespace extends BaseNamespace {
13
5
  client: SESClient | null;
14
6
  config: AwsConfig | null;
15
7
  ensureInit: () => Promise<void>;
16
- send: (props: SendProps) => Promise<any>;
8
+ send: (input: SendEmailCommandInput) => Promise<any>;
17
9
  }
18
- export {};
@@ -49,7 +49,7 @@ class AwsSesNamespace extends _class_1.BaseNamespace {
49
49
  }
50
50
  }
51
51
  };
52
- this.send = async (props) => {
52
+ this.send = async (input) => {
53
53
  await this.ensureInit();
54
54
  const { SendEmailCommand } = await Promise.resolve().then(() => __importStar(require("@aws-sdk/client-ses")));
55
55
  let response;
@@ -27,7 +27,7 @@ export declare class SimpleEmail extends BaseClass {
27
27
  setBcc: (recipient: string | string[]) => this;
28
28
  setCc: (recipient: string | string[]) => this;
29
29
  setTo: (recipient: string | string[]) => this;
30
- getEmailParams: () => {
30
+ getSendParam: () => {
31
31
  Destination: {
32
32
  BccAddresses: string[];
33
33
  CcAddresses: string[];
@@ -42,7 +42,7 @@ class SimpleEmail extends _class_1.BaseClass {
42
42
  this.setTo = (recipient) => {
43
43
  return this._setArray("to", recipient);
44
44
  };
45
- this.getEmailParams = () => {
45
+ this.getSendParam = () => {
46
46
  // https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/ses-examples-sending-email.html
47
47
  const params = {
48
48
  Destination: {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@8ms/helpers",
3
3
  "license": "UNLICENSED",
4
- "version": "2.0.16",
4
+ "version": "2.0.18",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/8millionstories-organisation/8ms-helpers-ts.git"
@@ -2,4 +2,4 @@ import { type ZlibOptions } from "zlib";
2
2
  /**
3
3
  * Compress a given input using Gzip.
4
4
  */
5
- export declare const getGzipCompressed: (input: any, options?: ZlibOptions) => Promise<unknown>;
5
+ export declare const getGzipCompressed: (input: any, options?: ZlibOptions) => Promise<string>;