@8ms/helpers 2.0.15 → 2.0.17

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
  /**
@@ -11,6 +11,13 @@ export declare const getUnix: (input?: InputDate) => number;
11
11
  * If it's not UTC, it needs to be converted to UTC before being used.
12
12
  */
13
13
  export declare const getDate: (input?: InputDate, setMidnight?: boolean) => DateTime<boolean>;
14
+ export declare const isMonday: (input?: InputDate) => boolean;
15
+ export declare const isTuesday: (input?: InputDate) => boolean;
16
+ export declare const isWednesday: (input?: InputDate) => boolean;
17
+ export declare const isThursday: (input?: InputDate) => boolean;
18
+ export declare const isFriday: (input?: InputDate) => boolean;
19
+ export declare const isSaturday: (input?: InputDate) => boolean;
20
+ export declare const isSunday: (input?: InputDate) => boolean;
14
21
  /**
15
22
  * Determines if a given date falls on a weekend.
16
23
  *
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isLastWeek = exports.isThisWeek = exports.getYesterday = exports.getWeeksAgo = exports.getToday = exports.getTwoWeeksAgo = exports.getThisWeek = exports.getLastWeek = exports.getSunday = exports.getMonday = exports.getMin = exports.getMax = exports.getDatesBetween = exports.differenceInBusinessDays = exports.differenceInMinutes = exports.isWeekend = exports.getDate = exports.getUnix = exports.getUtcDate = void 0;
3
+ exports.isLastWeek = exports.isThisWeek = exports.getYesterday = exports.getWeeksAgo = exports.getToday = exports.getTwoWeeksAgo = exports.getThisWeek = exports.getLastWeek = exports.getSunday = exports.getMonday = exports.getMin = exports.getMax = exports.getDatesBetween = exports.differenceInBusinessDays = exports.differenceInMinutes = exports.isWeekend = exports.isSunday = exports.isSaturday = exports.isFriday = exports.isThursday = exports.isWednesday = exports.isTuesday = exports.isMonday = exports.getDate = exports.getUnix = exports.getUtcDate = void 0;
4
4
  const luxon_1 = require("luxon");
5
5
  /**
6
6
  * Convert an client's input (any time-zone) ready to be used by the server (UTC).
@@ -63,6 +63,41 @@ const getDate = (input, setMidnight) => {
63
63
  return instance;
64
64
  };
65
65
  exports.getDate = getDate;
66
+ const isMonday = (input) => {
67
+ const date = (0, exports.getDate)(input);
68
+ return date.weekday === 1;
69
+ };
70
+ exports.isMonday = isMonday;
71
+ const isTuesday = (input) => {
72
+ const date = (0, exports.getDate)(input);
73
+ return date.weekday === 2;
74
+ };
75
+ exports.isTuesday = isTuesday;
76
+ const isWednesday = (input) => {
77
+ const date = (0, exports.getDate)(input);
78
+ return date.weekday === 3;
79
+ };
80
+ exports.isWednesday = isWednesday;
81
+ const isThursday = (input) => {
82
+ const date = (0, exports.getDate)(input);
83
+ return date.weekday === 4;
84
+ };
85
+ exports.isThursday = isThursday;
86
+ const isFriday = (input) => {
87
+ const date = (0, exports.getDate)(input);
88
+ return date.weekday === 6;
89
+ };
90
+ exports.isFriday = isFriday;
91
+ const isSaturday = (input) => {
92
+ const date = (0, exports.getDate)(input);
93
+ return date.weekday === 6;
94
+ };
95
+ exports.isSaturday = isSaturday;
96
+ const isSunday = (input) => {
97
+ const date = (0, exports.getDate)(input);
98
+ return date.weekday === 7;
99
+ };
100
+ exports.isSunday = isSunday;
66
101
  /**
67
102
  * Determines if a given date falls on a weekend.
68
103
  *
@@ -71,7 +106,7 @@ exports.getDate = getDate;
71
106
  */
72
107
  const isWeekend = (input) => {
73
108
  const date = (0, exports.getDate)(input);
74
- return date.weekday >= 6;
109
+ return 6 === date.weekday || 7 === date.weekday;
75
110
  };
76
111
  exports.isWeekend = isWeekend;
77
112
  /**
@@ -80,7 +115,7 @@ exports.isWeekend = isWeekend;
80
115
  const differenceInMinutes = (start, end) => {
81
116
  const startDate = (0, exports.getDate)(start);
82
117
  const endDate = (0, exports.getDate)(end);
83
- return endDate.diff(startDate, 'minutes').minutes;
118
+ return endDate.diff(startDate, "minutes").minutes;
84
119
  };
85
120
  exports.differenceInMinutes = differenceInMinutes;
86
121
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@8ms/helpers",
3
3
  "license": "UNLICENSED",
4
- "version": "2.0.15",
4
+ "version": "2.0.17",
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>;