@dvsa/appdev-api-common 0.3.1 → 0.3.3
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 +12 -0
- package/auth/auth-checker.js +5 -3
- package/package.json +6 -2
- package/validation/request-body.d.ts +9 -6
- package/validation/request-body.js +7 -6
package/README.md
CHANGED
|
@@ -35,3 +35,15 @@ There are two ways in which this package can/should be published:
|
|
|
35
35
|
###### Requires manual version bump via the PR
|
|
36
36
|
|
|
37
37
|
- Upon merge into `main` branch, the package will be published via a GHA workflow.
|
|
38
|
+
|
|
39
|
+
### Developing locally
|
|
40
|
+
To test our your changes before publishing to `npm`, you can use the following command:
|
|
41
|
+
|
|
42
|
+
`npm run localLink`
|
|
43
|
+
|
|
44
|
+
Then in the project you wish to use this package, run:
|
|
45
|
+
|
|
46
|
+
`npm link @dvsa/appdev-api-common`
|
|
47
|
+
|
|
48
|
+
Once you've completed your local testing and/or to start again from scratch, you can run:
|
|
49
|
+
`npm unlink @dvsa/appdev-api-common`
|
package/auth/auth-checker.js
CHANGED
|
@@ -13,11 +13,13 @@ class JWTAuthChecker {
|
|
|
13
13
|
*/
|
|
14
14
|
static async execute({ request }, roles = []) {
|
|
15
15
|
// if running locally, skip the token auth and role check
|
|
16
|
-
if (process.env.IS_OFFLINE === "true"
|
|
16
|
+
if (process.env.IS_OFFLINE === "true" &&
|
|
17
|
+
process.env.FORCE_LOCAL_AUTH !== "true")
|
|
17
18
|
return true;
|
|
18
19
|
// extract the token from the request headers
|
|
19
|
-
const
|
|
20
|
-
.headers
|
|
20
|
+
const headers = request?.apiGateway.event
|
|
21
|
+
.headers;
|
|
22
|
+
const token = headers?.Authorization || headers?.authorization;
|
|
21
23
|
// if no token is found, then deny access to resource
|
|
22
24
|
if (!token || token.trim()?.length === 0) {
|
|
23
25
|
throw new auth_errors_1.AuthError(http_status_codes_1.HttpStatus.UNAUTHORIZED, "Missing Authorization header");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dvsa/appdev-api-common",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"dvsa",
|
|
6
6
|
"nodejs",
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"access": "public"
|
|
14
14
|
},
|
|
15
15
|
"scripts": {
|
|
16
|
+
"localLink": "npm run clean && npm version patch && npm run build && cp package.json dist && cd dist && npm link",
|
|
16
17
|
"clean": "rimraf coverage dist",
|
|
17
18
|
"clean:temp": "rimraf auth api",
|
|
18
19
|
"lint": "biome check src",
|
|
@@ -38,9 +39,12 @@
|
|
|
38
39
|
"jest": "^29.7.0",
|
|
39
40
|
"lint-staged": "^15.2.10",
|
|
40
41
|
"rimraf": "^6.0.1",
|
|
41
|
-
"routing-controllers": "^0.
|
|
42
|
+
"routing-controllers": "^0.11.2",
|
|
42
43
|
"ts-jest": "^29.2.5",
|
|
43
44
|
"ts-node": "^10.9.2",
|
|
44
45
|
"typescript": "^5.5.2"
|
|
46
|
+
},
|
|
47
|
+
"lint-staged": {
|
|
48
|
+
"*.{js,ts,mjs,css,md,ts,json}": "npm run lint:fix -- --no-errors-on-unmatched"
|
|
45
49
|
}
|
|
46
50
|
}
|
|
@@ -1,10 +1,13 @@
|
|
|
1
|
+
interface ValidateRequestBodyOptions {
|
|
2
|
+
isArray?: boolean;
|
|
3
|
+
errorDetails?: boolean;
|
|
4
|
+
}
|
|
1
5
|
/**
|
|
2
6
|
* Decorator tp validate an express request body against a specified schema
|
|
3
7
|
* @param {object} schema - the json schema you wish to use as the validator
|
|
4
|
-
* @param
|
|
5
|
-
*
|
|
8
|
+
* @param {ValidateRequestBodyOptions} opts
|
|
9
|
+
* - isArray: whether the body is expected to be an array
|
|
10
|
+
* - errorDetails: whether to return detailed error messages (Note: errors are logged regardless of this setting)
|
|
6
11
|
*/
|
|
7
|
-
export declare function ValidateRequestBody<T>(schema: object,
|
|
8
|
-
|
|
9
|
-
errorDetails: boolean;
|
|
10
|
-
}): (_target: T, _propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
12
|
+
export declare function ValidateRequestBody<T>(schema: object, opts?: ValidateRequestBodyOptions): (_target: T, _propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
13
|
+
export {};
|
|
@@ -11,10 +11,11 @@ ajv.addKeyword("tsEnumNames");
|
|
|
11
11
|
/**
|
|
12
12
|
* Decorator tp validate an express request body against a specified schema
|
|
13
13
|
* @param {object} schema - the json schema you wish to use as the validator
|
|
14
|
-
* @param
|
|
15
|
-
*
|
|
14
|
+
* @param {ValidateRequestBodyOptions} opts
|
|
15
|
+
* - isArray: whether the body is expected to be an array
|
|
16
|
+
* - errorDetails: whether to return detailed error messages (Note: errors are logged regardless of this setting)
|
|
16
17
|
*/
|
|
17
|
-
function ValidateRequestBody(schema,
|
|
18
|
+
function ValidateRequestBody(schema, opts = { isArray: false, errorDetails: false }) {
|
|
18
19
|
return (_target, _propertyKey, descriptor) => {
|
|
19
20
|
const originalMethod = descriptor.value;
|
|
20
21
|
descriptor.value = async function (body, res, next) {
|
|
@@ -27,8 +28,8 @@ function ValidateRequestBody(schema, { isArray, errorDetails } = { isArray: fals
|
|
|
27
28
|
const payload = Buffer.isBuffer(body)
|
|
28
29
|
? JSON.parse(body.toString("utf-8"))
|
|
29
30
|
: body;
|
|
30
|
-
// Create the appropriate schema based on whether we're validating an array
|
|
31
|
-
const schemaToValidate = isArray
|
|
31
|
+
// Create the appropriate schema based on whether we're validating an array or a single object
|
|
32
|
+
const schemaToValidate = opts?.isArray
|
|
32
33
|
? { type: "array", items: schema }
|
|
33
34
|
: schema;
|
|
34
35
|
const validateFunction = ajv.compile(schemaToValidate);
|
|
@@ -40,7 +41,7 @@ function ValidateRequestBody(schema, { isArray, errorDetails } = { isArray: fals
|
|
|
40
41
|
const response = {
|
|
41
42
|
message: "Validation error",
|
|
42
43
|
};
|
|
43
|
-
if (errorDetails) {
|
|
44
|
+
if (opts?.errorDetails) {
|
|
44
45
|
Object.assign(response, { errors: validateFunction.errors });
|
|
45
46
|
}
|
|
46
47
|
return res.status(http_status_codes_1.HttpStatus.BAD_REQUEST).json(response);
|