@dvsa/appdev-api-common 0.4.5 → 0.5.0

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/README.md CHANGED
@@ -305,3 +305,34 @@ Example error handling:
305
305
  const invalidDate = new DateTime("invalid-date");
306
306
  console.log(invalidDate.toString()); // Returns an invalid date string
307
307
  ```
308
+
309
+
310
+ # Compression
311
+
312
+ ## Overview
313
+ `DataCompression` is a utility class to simplify the compression & decompression using Gzip and Gunzip algorithms.
314
+
315
+ ## Usage
316
+
317
+ ### Importing the `DataCompression` Class
318
+ ```ts
319
+ import { DataCompression } from '@dvsa/appdev-api-common';
320
+ ```
321
+
322
+ ### Compressing Data
323
+ This is the process of taking a plain JSON object and compressing it using Gzip.
324
+
325
+ ```ts
326
+ const data = { key: 'value' };
327
+ const compressedData = DataCompression.compress(data);
328
+ // H4sIAAAAAAAAA6tWyk6tVLJSKkvMKU1VqgUAv5wYPw8AAAA=
329
+ ```
330
+
331
+ ### Decompressing Data
332
+ This is the process of taking a compressed JSON object and decompressing it using Gunzip.
333
+
334
+ ```ts
335
+ const data = "H4sIAAAAAAAAA6tWyk6tVLJSKkvMKU1VqgUAv5wYPw8AAAA=";
336
+ const decompressedData = DataCompression.decompress(data);
337
+ // { key: 'value' }
338
+ ```
package/index.d.ts CHANGED
@@ -3,5 +3,6 @@ export * from "./auth/auth-checker";
3
3
  export * from "./auth/auth-errors";
4
4
  export * from "./auth/client-credentials";
5
5
  export * from "./auth/verify-jwt";
6
+ export * from "./utils/compression";
6
7
  export * from "./utils/date-time";
7
8
  export * from "./validation/request-body";
package/index.js CHANGED
@@ -19,5 +19,6 @@ __exportStar(require("./auth/auth-checker"), exports);
19
19
  __exportStar(require("./auth/auth-errors"), exports);
20
20
  __exportStar(require("./auth/client-credentials"), exports);
21
21
  __exportStar(require("./auth/verify-jwt"), exports);
22
+ __exportStar(require("./utils/compression"), exports);
22
23
  __exportStar(require("./utils/date-time"), exports);
23
24
  __exportStar(require("./validation/request-body"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dvsa/appdev-api-common",
3
- "version": "0.4.5",
3
+ "version": "0.5.0",
4
4
  "keywords": ["dvsa", "nodejs", "typescript"],
5
5
  "author": "DVSA",
6
6
  "description": "Utils library for common API functionality",
@@ -0,0 +1,16 @@
1
+ export declare class DataCompression {
2
+ /**
3
+ * Extracts a compressed (in base64) string using gunzip into a JSON object
4
+ * @template T
5
+ * @param {string} compressedData
6
+ * @returns {T}
7
+ */
8
+ static decompress<T>(compressedData: string): T;
9
+ /**
10
+ * Compresses (with gzip) a JSON object into a base64 string
11
+ * @template T
12
+ * @param {T} data
13
+ * @returns {string}
14
+ */
15
+ static compress<T>(data: T): string;
16
+ }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DataCompression = void 0;
4
+ const node_zlib_1 = require("node:zlib");
5
+ // biome-ignore lint/complexity/noStaticOnlyClass: Valid use case for a static class
6
+ class DataCompression {
7
+ /**
8
+ * Extracts a compressed (in base64) string using gunzip into a JSON object
9
+ * @template T
10
+ * @param {string} compressedData
11
+ * @returns {T}
12
+ */
13
+ static decompress(compressedData) {
14
+ const gzippedBytes = Buffer.from(compressedData, "base64");
15
+ const unzippedJson = (0, node_zlib_1.gunzipSync)(gzippedBytes).toString();
16
+ return JSON.parse(unzippedJson);
17
+ }
18
+ /**
19
+ * Compresses (with gzip) a JSON object into a base64 string
20
+ * @template T
21
+ * @param {T} data
22
+ * @returns {string}
23
+ */
24
+ static compress(data) {
25
+ const jsonString = JSON.stringify(data);
26
+ const gzippedData = (0, node_zlib_1.gzipSync)(Buffer.from(jsonString));
27
+ return gzippedData.toString("base64");
28
+ }
29
+ }
30
+ exports.DataCompression = DataCompression;