@dvsa/appdev-api-common 0.8.0 → 0.9.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/http/decorators.d.ts +36 -1
- package/http/decorators.js +38 -3
- package/http/index.d.ts +32 -2
- package/http/index.js +42 -4
- package/package.json +1 -1
package/http/decorators.d.ts
CHANGED
|
@@ -1,7 +1,42 @@
|
|
|
1
1
|
import type { Logger } from "@aws-lambda-powertools/logger";
|
|
2
|
-
|
|
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;
|
package/http/decorators.js
CHANGED
|
@@ -1,9 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.TimedWithAccessor = TimedWithAccessor;
|
|
4
4
|
exports.Timed = Timed;
|
|
5
5
|
const node_perf_hooks_1 = require("node:perf_hooks");
|
|
6
|
-
|
|
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
|
|
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
|
@@ -1,10 +1,40 @@
|
|
|
1
|
+
type HTTPErrorResponse = Partial<Omit<Response, "body"> & {
|
|
2
|
+
body: unknown;
|
|
3
|
+
}>;
|
|
1
4
|
export declare class HTTPError extends Error {
|
|
2
|
-
response:
|
|
3
|
-
constructor(message: string, response:
|
|
5
|
+
response: HTTPErrorResponse;
|
|
6
|
+
constructor(message: string, response: HTTPErrorResponse);
|
|
4
7
|
}
|
|
5
8
|
export declare class HTTP {
|
|
9
|
+
/**
|
|
10
|
+
* Performs an HTTP GET request.
|
|
11
|
+
* Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
|
|
12
|
+
* @param url
|
|
13
|
+
* @param options
|
|
14
|
+
*/
|
|
6
15
|
static get(url: string, options?: RequestInit): Promise<Response>;
|
|
16
|
+
/**
|
|
17
|
+
* Performs an HTTP POST 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 body
|
|
21
|
+
* @param options
|
|
22
|
+
*/
|
|
7
23
|
static post<T>(url: string, body: T, options?: RequestInit): Promise<Response>;
|
|
24
|
+
/**
|
|
25
|
+
* Performs an HTTP PUT request.
|
|
26
|
+
* Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
|
|
27
|
+
* @param url
|
|
28
|
+
* @param body
|
|
29
|
+
* @param options
|
|
30
|
+
*/
|
|
8
31
|
static put<T>(url: string, body: T, options?: RequestInit): Promise<Response>;
|
|
32
|
+
/**
|
|
33
|
+
* Performs an HTTP DELETE request.
|
|
34
|
+
* Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
|
|
35
|
+
* @param url
|
|
36
|
+
* @param options
|
|
37
|
+
*/
|
|
9
38
|
static delete(url: string, options?: RequestInit): Promise<Response>;
|
|
10
39
|
}
|
|
40
|
+
export {};
|
package/http/index.js
CHANGED
|
@@ -13,13 +13,29 @@ 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) {
|
|
19
|
-
throw new HTTPError(`HTTP GET request failed with status ${response.status}`,
|
|
25
|
+
throw new HTTPError(`HTTP GET request failed with status ${response.status}`, {
|
|
26
|
+
...response,
|
|
27
|
+
body: await response.json(),
|
|
28
|
+
});
|
|
20
29
|
}
|
|
21
30
|
return response;
|
|
22
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Performs an HTTP POST request.
|
|
34
|
+
* Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
|
|
35
|
+
* @param url
|
|
36
|
+
* @param body
|
|
37
|
+
* @param options
|
|
38
|
+
*/
|
|
23
39
|
static async post(url, body, options) {
|
|
24
40
|
const response = await fetch(url, {
|
|
25
41
|
method: "POST",
|
|
@@ -28,10 +44,20 @@ class HTTP {
|
|
|
28
44
|
...options,
|
|
29
45
|
});
|
|
30
46
|
if (!response.ok) {
|
|
31
|
-
throw new HTTPError(`HTTP POST request failed with status ${response.status}`,
|
|
47
|
+
throw new HTTPError(`HTTP POST request failed with status ${response.status}`, {
|
|
48
|
+
...response,
|
|
49
|
+
body: await response.json(),
|
|
50
|
+
});
|
|
32
51
|
}
|
|
33
52
|
return response;
|
|
34
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Performs an HTTP PUT request.
|
|
56
|
+
* Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
|
|
57
|
+
* @param url
|
|
58
|
+
* @param body
|
|
59
|
+
* @param options
|
|
60
|
+
*/
|
|
35
61
|
static async put(url, body, options) {
|
|
36
62
|
const response = await fetch(url, {
|
|
37
63
|
method: "PUT",
|
|
@@ -40,14 +66,26 @@ class HTTP {
|
|
|
40
66
|
...options,
|
|
41
67
|
});
|
|
42
68
|
if (!response.ok) {
|
|
43
|
-
throw new HTTPError(`HTTP PUT request failed with status ${response.status}`,
|
|
69
|
+
throw new HTTPError(`HTTP PUT request failed with status ${response.status}`, {
|
|
70
|
+
...response,
|
|
71
|
+
body: await response.json(),
|
|
72
|
+
});
|
|
44
73
|
}
|
|
45
74
|
return response;
|
|
46
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Performs an HTTP DELETE request.
|
|
78
|
+
* Note: This method will throw an HTTPError if the response is not ok (status code 200-299) to emulate Axios behaviour.
|
|
79
|
+
* @param url
|
|
80
|
+
* @param options
|
|
81
|
+
*/
|
|
47
82
|
static async delete(url, options) {
|
|
48
83
|
const response = await fetch(url, { method: "DELETE", ...options });
|
|
49
84
|
if (!response.ok) {
|
|
50
|
-
throw new HTTPError(`HTTP DELETE request failed with status ${response.status}`,
|
|
85
|
+
throw new HTTPError(`HTTP DELETE request failed with status ${response.status}`, {
|
|
86
|
+
...response,
|
|
87
|
+
body: await response.json(),
|
|
88
|
+
});
|
|
51
89
|
}
|
|
52
90
|
return response;
|
|
53
91
|
}
|