@dvsa/appdev-api-common 0.8.0 → 0.8.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.
@@ -1,7 +1,42 @@
1
1
  import type { Logger } from "@aws-lambda-powertools/logger";
2
- export declare function LogDurationWithAccessor<T extends {
2
+ /**
3
+ * Decorator to log the duration of a method execution by passing in an accessor function to retrieve the logger.
4
+ * @param getLogger - A function that takes the class instance and returns the logger instance.
5
+ * @param {string} label - Optional label for the log message.
6
+ *
7
+ * Note: If you are already using a logger as a class property, you can use the `Timed` decorator instead.
8
+ *
9
+ * @constructor
10
+ *
11
+ * Example usage:
12
+ * class MyClass {
13
+ * @TimedWithAccessor(() => Container.get(LOGGER))
14
+ * async findAll() {} // The log name here will be [findAll]
15
+ * }
16
+ */
17
+ export declare function TimedWithAccessor<T extends {
3
18
  logger: Logger;
4
19
  }>(getLogger: (self: T) => Logger, label?: string): (_target: unknown, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
20
+ /**
21
+ * Decorator to log the duration of a method execution.
22
+ * @param {string} label - Optional label for the log message.
23
+ *
24
+ * Note: This decorator can be used on any class method that has a `logger` property.
25
+ * This method will throw an error if the `logger` property is not found on the class instance.
26
+ * If you are not using logger as a class property, you can use `LogDurationWithAccessor` instead.
27
+ * @constructor
28
+ *
29
+ * Example usage:
30
+ * class MyClass {
31
+ * private readonly logger = Container.get(Logger);
32
+ *
33
+ * @Timed()
34
+ * myMethod() {} // The log name here will be [myMethod]
35
+ *
36
+ * @Timed('MyClass.myMethod')
37
+ * myMethodWithOptName() {} // The log name here will be [MyClass.myMethod]
38
+ * }
39
+ */
5
40
  export declare function Timed<T extends {
6
41
  logger: Logger;
7
42
  }>(label?: string): (_target: unknown, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
@@ -1,9 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LogDurationWithAccessor = LogDurationWithAccessor;
3
+ exports.TimedWithAccessor = TimedWithAccessor;
4
4
  exports.Timed = Timed;
5
5
  const node_perf_hooks_1 = require("node:perf_hooks");
6
- function LogDurationWithAccessor(getLogger, label) {
6
+ /**
7
+ * Decorator to log the duration of a method execution by passing in an accessor function to retrieve the logger.
8
+ * @param getLogger - A function that takes the class instance and returns the logger instance.
9
+ * @param {string} label - Optional label for the log message.
10
+ *
11
+ * Note: If you are already using a logger as a class property, you can use the `Timed` decorator instead.
12
+ *
13
+ * @constructor
14
+ *
15
+ * Example usage:
16
+ * class MyClass {
17
+ * @TimedWithAccessor(() => Container.get(LOGGER))
18
+ * async findAll() {} // The log name here will be [findAll]
19
+ * }
20
+ */
21
+ function TimedWithAccessor(getLogger, label) {
7
22
  return (_target, propertyKey, descriptor) => {
8
23
  const originalMethod = descriptor.value;
9
24
  // biome-ignore lint/suspicious/noExplicitAny: "any" is correct for this context
@@ -41,8 +56,28 @@ function LogDurationWithAccessor(getLogger, label) {
41
56
  return descriptor;
42
57
  };
43
58
  }
59
+ /**
60
+ * Decorator to log the duration of a method execution.
61
+ * @param {string} label - Optional label for the log message.
62
+ *
63
+ * Note: This decorator can be used on any class method that has a `logger` property.
64
+ * This method will throw an error if the `logger` property is not found on the class instance.
65
+ * If you are not using logger as a class property, you can use `LogDurationWithAccessor` instead.
66
+ * @constructor
67
+ *
68
+ * Example usage:
69
+ * class MyClass {
70
+ * private readonly logger = Container.get(Logger);
71
+ *
72
+ * @Timed()
73
+ * myMethod() {} // The log name here will be [myMethod]
74
+ *
75
+ * @Timed('MyClass.myMethod')
76
+ * myMethodWithOptName() {} // The log name here will be [MyClass.myMethod]
77
+ * }
78
+ */
44
79
  function Timed(label) {
45
- return LogDurationWithAccessor((cls) => {
80
+ return TimedWithAccessor((cls) => {
46
81
  if (!cls.logger) {
47
82
  throw new Error(`[${label}] Logger not found on decorated class.`);
48
83
  }
package/http/index.d.ts CHANGED
@@ -3,8 +3,34 @@ export declare class HTTPError extends Error {
3
3
  constructor(message: string, response: Response);
4
4
  }
5
5
  export declare class HTTP {
6
+ /**
7
+ * Performs an HTTP GET request.
8
+ * Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
9
+ * @param url
10
+ * @param options
11
+ */
6
12
  static get(url: string, options?: RequestInit): Promise<Response>;
13
+ /**
14
+ * Performs an HTTP POST request.
15
+ * Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
16
+ * @param url
17
+ * @param body
18
+ * @param options
19
+ */
7
20
  static post<T>(url: string, body: T, options?: RequestInit): Promise<Response>;
21
+ /**
22
+ * Performs an HTTP PUT request.
23
+ * Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
24
+ * @param url
25
+ * @param body
26
+ * @param options
27
+ */
8
28
  static put<T>(url: string, body: T, options?: RequestInit): Promise<Response>;
29
+ /**
30
+ * Performs an HTTP DELETE request.
31
+ * Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
32
+ * @param url
33
+ * @param options
34
+ */
9
35
  static delete(url: string, options?: RequestInit): Promise<Response>;
10
36
  }
package/http/index.js CHANGED
@@ -13,6 +13,12 @@ class HTTPError extends Error {
13
13
  exports.HTTPError = HTTPError;
14
14
  // biome-ignore lint/complexity/noStaticOnlyClass: makes sense for an HTTP utility to encompass all methods
15
15
  class HTTP {
16
+ /**
17
+ * Performs an HTTP GET request.
18
+ * Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
19
+ * @param url
20
+ * @param options
21
+ */
16
22
  static async get(url, options) {
17
23
  const response = await fetch(url, { method: "GET", ...options });
18
24
  if (!response.ok) {
@@ -20,6 +26,13 @@ class HTTP {
20
26
  }
21
27
  return response;
22
28
  }
29
+ /**
30
+ * Performs an HTTP POST request.
31
+ * Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
32
+ * @param url
33
+ * @param body
34
+ * @param options
35
+ */
23
36
  static async post(url, body, options) {
24
37
  const response = await fetch(url, {
25
38
  method: "POST",
@@ -32,6 +45,13 @@ class HTTP {
32
45
  }
33
46
  return response;
34
47
  }
48
+ /**
49
+ * Performs an HTTP PUT request.
50
+ * Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
51
+ * @param url
52
+ * @param body
53
+ * @param options
54
+ */
35
55
  static async put(url, body, options) {
36
56
  const response = await fetch(url, {
37
57
  method: "PUT",
@@ -44,6 +64,12 @@ class HTTP {
44
64
  }
45
65
  return response;
46
66
  }
67
+ /**
68
+ * Performs an HTTP DELETE request.
69
+ * Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
70
+ * @param url
71
+ * @param options
72
+ */
47
73
  static async delete(url, options) {
48
74
  const response = await fetch(url, { method: "DELETE", ...options });
49
75
  if (!response.ok) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dvsa/appdev-api-common",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "keywords": ["dvsa", "nodejs", "typescript"],
5
5
  "author": "DVSA",
6
6
  "description": "Utils library for common API functionality",