@dvsa/appdev-api-common 0.9.3-canary.0 → 0.10.1

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.
@@ -54,13 +54,20 @@ class ClientCredentials {
54
54
  const response = await fetch(this.tokenUrl, {
55
55
  method: "POST",
56
56
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
57
- body: (0, node_querystring_1.stringify)({
58
- grant_type: ClientCredentials.grantType,
59
- client_id: this.clientId,
60
- client_secret: this.clientSecret,
61
- scope: this.scope,
62
- resource: this.resource,
63
- }),
57
+ body: this.resource
58
+ ? (0, node_querystring_1.stringify)({
59
+ grant_type: ClientCredentials.grantType,
60
+ client_id: this.clientId,
61
+ client_secret: this.clientSecret,
62
+ scope: this.scope,
63
+ resource: this.resource,
64
+ })
65
+ : (0, node_querystring_1.stringify)({
66
+ grant_type: ClientCredentials.grantType,
67
+ client_id: this.clientId,
68
+ client_secret: this.clientSecret,
69
+ scope: this.scope,
70
+ }),
64
71
  });
65
72
  if (!response.ok) {
66
73
  console.error("Error fetching client credentials", response);
package/package.json CHANGED
@@ -1,11 +1,7 @@
1
1
  {
2
2
  "name": "@dvsa/appdev-api-common",
3
- "version": "0.9.3-canary.0",
4
- "keywords": [
5
- "dvsa",
6
- "nodejs",
7
- "typescript"
8
- ],
3
+ "version": "0.10.1",
4
+ "keywords": ["dvsa", "nodejs", "typescript"],
9
5
  "author": "DVSA",
10
6
  "description": "Utils library for common API functionality",
11
7
  "publishConfig": {
@@ -1,2 +1,9 @@
1
- import type { NextFunction, Response, Request } from 'express';
1
+ import type { NextFunction, Request, Response } from "express";
2
+ /**
3
+ * Middleware to decode base64+gzip encoded request payloads and turn them into their JSON equivalent
4
+ * @param {Request} request - Express request object
5
+ * @param {Response} response - Express response object
6
+ * @param {NextFunction} next - Next function to pass control to the next middleware
7
+ * @constructor
8
+ */
2
9
  export declare const DecodeBase64GzipPayload: (request: Request, response: Response, next: NextFunction) => Response<any, Record<string, any>> | undefined;
@@ -1,17 +1,28 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DecodeBase64GzipPayload = void 0;
4
- const compression_1 = require("./compression");
5
4
  const http_status_codes_1 = require("../api/http-status-codes");
5
+ const compression_1 = require("./compression");
6
+ /**
7
+ * Middleware to decode base64+gzip encoded request payloads and turn them into their JSON equivalent
8
+ * @param {Request} request - Express request object
9
+ * @param {Response} response - Express response object
10
+ * @param {NextFunction} next - Next function to pass control to the next middleware
11
+ * @constructor
12
+ */
6
13
  const DecodeBase64GzipPayload = (request, response, next) => {
7
- const compressionHeaderValue = 'base64+gzip';
14
+ const compressionHeaderValue = "base64+gzip";
8
15
  try {
9
- if (typeof request.body === 'string' && request.header('X-Payload-Encoding') === compressionHeaderValue) {
16
+ if (typeof request.body === "string" &&
17
+ request.header("X-Payload-Encoding") === compressionHeaderValue) {
10
18
  request.body = compression_1.DataCompression.decompress(request.body);
11
19
  }
12
20
  }
13
21
  catch (err) {
14
- return response.status(http_status_codes_1.HttpStatus.BAD_REQUEST).send({ message: 'Failed to decode payload' });
22
+ return response.status(http_status_codes_1.HttpStatus.BAD_REQUEST).send({
23
+ error: "Bad request",
24
+ message: err instanceof Error ? err.message : JSON.stringify(err),
25
+ });
15
26
  }
16
27
  next();
17
28
  };
@@ -1,6 +1,13 @@
1
- import type { Request, Response } from 'express';
2
- import type { APIGatewayProxyResult } from 'aws-lambda';
3
- import type { Logger } from '@aws-lambda-powertools/logger';
1
+ import type { Logger } from "@aws-lambda-powertools/logger";
2
+ import type { APIGatewayProxyResult } from "aws-lambda";
3
+ import type { Request, Response } from "express";
4
+ /**
5
+ * Interceptor to encode the response payload if requested by the client
6
+ * @param {Request, Response} request, response - Express request & response object
7
+ * @param {Partial<APIGatewayProxyResult>} content - API Gateway Proxy Result content
8
+ * @param {Logger} logger - Logger instance
9
+ * @constructor
10
+ */
4
11
  export declare const EncodePayloadInterceptor: ({ response, request }: {
5
12
  response: Response;
6
13
  request: Request;
@@ -3,8 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.EncodePayloadInterceptor = void 0;
4
4
  const http_status_codes_1 = require("../api/http-status-codes");
5
5
  const compression_1 = require("./compression");
6
+ /**
7
+ * Interceptor to encode the response payload if requested by the client
8
+ * @param {Request, Response} request, response - Express request & response object
9
+ * @param {Partial<APIGatewayProxyResult>} content - API Gateway Proxy Result content
10
+ * @param {Logger} logger - Logger instance
11
+ * @constructor
12
+ */
6
13
  const EncodePayloadInterceptor = ({ response, request }, content, logger) => {
7
- const compressionHeaderValue = 'base64+gzip';
14
+ const compressionHeaderValue = "base64+gzip";
8
15
  for (const [header, value] of Object.entries(content?.headers || {})) {
9
16
  response.setHeader(header, value);
10
17
  }
@@ -15,30 +22,30 @@ const EncodePayloadInterceptor = ({ response, request }, content, logger) => {
15
22
  if (!content.body) {
16
23
  return response;
17
24
  }
18
- const didRequestCompressed = request?.headers?.['x-accept-encoding'] === compressionHeaderValue;
25
+ const didRequestCompressed = request?.headers?.["x-accept-encoding"] === compressionHeaderValue;
19
26
  const shouldCompress = didRequestCompressed && content.statusCode === http_status_codes_1.HttpStatus.OK;
20
27
  // Parse the body if it's a string
21
- const bodyData = typeof content.body === 'string' ? JSON.parse(content.body) : content.body;
28
+ const bodyData = typeof content.body === "string" ? JSON.parse(content.body) : content.body;
22
29
  if (shouldCompress) {
23
- logger.debug('Compressing response data');
30
+ logger.debug("Compressing response data");
24
31
  try {
25
32
  const compressedData = compression_1.DataCompression.compress(bodyData);
26
33
  // Check if compressed data is actually smaller
27
- const originalSize = Buffer.byteLength(JSON.stringify(bodyData), 'utf8');
28
- const compressedSize = Buffer.byteLength(JSON.stringify(compressedData), 'utf8');
34
+ const originalSize = Buffer.byteLength(JSON.stringify(bodyData), "utf8");
35
+ const compressedSize = Buffer.byteLength(JSON.stringify(compressedData), "utf8");
29
36
  if (compressedSize >= originalSize) {
30
- logger.debug('Compression not beneficial, using original data');
37
+ logger.debug("Compression not beneficial, using original data");
31
38
  response.json(bodyData);
32
39
  }
33
40
  else {
34
41
  // Set the content encoding headers
35
- response.setHeader('Access-Control-Expose-Headers', 'content-encoding');
36
- response.setHeader('content-encoding', compressionHeaderValue);
42
+ response.setHeader("Access-Control-Expose-Headers", "content-encoding");
43
+ response.setHeader("content-encoding", compressionHeaderValue);
37
44
  response.json(compressedData);
38
45
  }
39
46
  }
40
47
  catch (err) {
41
- logger.error('Error compressing response data', { err });
48
+ logger.error("Error compressing response data", { err });
42
49
  response.json(bodyData);
43
50
  }
44
51
  }