@odatnurd/cf-requests 0.1.10 → 0.1.11
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 +11 -6
- package/lib/handlers.js +6 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -233,11 +233,12 @@ status code is used to construct the JSON as well as the response object:
|
|
|
233
233
|
`status` is option and defaults to `200` if not provided.
|
|
234
234
|
|
|
235
235
|
If the `verify()` function was used to set a schema, then this function will
|
|
236
|
-
validate that the `result
|
|
237
|
-
optionally mask it, if `verify()` was given
|
|
236
|
+
validate that the **entire returned body, including `result`** you provide
|
|
237
|
+
matches the schema, and will also optionally mask it, if `verify()` was given
|
|
238
|
+
a mask function.
|
|
238
239
|
|
|
239
|
-
When using `verify()`, if the
|
|
240
|
-
|
|
240
|
+
When using `verify()`, if the result body does not conform to the schema, a
|
|
241
|
+
`SchemaError` exception will be thrown. This is automatically handled by
|
|
241
242
|
`body()`, and will result in a `fail()` response instead of a `success()`.
|
|
242
243
|
|
|
243
244
|
---
|
|
@@ -300,8 +301,12 @@ export function verify({ validate, mask? }) {}
|
|
|
300
301
|
```
|
|
301
302
|
|
|
302
303
|
This function registers the provided validation/masking pair for the current
|
|
303
|
-
route. This causes `success` to validate (and optionally mask) the
|
|
304
|
-
|
|
304
|
+
route. This causes `success` to validate (and optionally mask) the resulting
|
|
305
|
+
message body before finalizing the request and sending the data out.
|
|
306
|
+
|
|
307
|
+
> ℹ️ The schema is run over the **entire returned body, not just the`result`**,
|
|
308
|
+
> so in practice the schema needs to match what the `success()` function uses
|
|
309
|
+
> as the final result.
|
|
305
310
|
|
|
306
311
|
The parameter should be an object that contains a `validate` and an (optional)
|
|
307
312
|
`mask` member:
|
package/lib/handlers.js
CHANGED
|
@@ -64,13 +64,16 @@ export const success = async (ctx, message, result, status) => {
|
|
|
64
64
|
status ??= 200;
|
|
65
65
|
result ??= [];
|
|
66
66
|
|
|
67
|
+
// Construct the body that we will be returning back.
|
|
68
|
+
let body = { success: true, status, message, data: result };
|
|
69
|
+
|
|
67
70
|
// See if there is a validator attached to this; if so, we have more work to
|
|
68
71
|
// do; if not, we can skip.
|
|
69
72
|
const validator = ctx.get('__cf_requests_response_validator');
|
|
70
73
|
if (validator !== undefined) {
|
|
71
74
|
// Try to validate; if this does not return true, then the data is not valid
|
|
72
75
|
// and we should throw an error.
|
|
73
|
-
const valid = validator.validate(
|
|
76
|
+
const valid = validator.validate(body);
|
|
74
77
|
if (valid !== true) {
|
|
75
78
|
throw new SchemaError('response data failed schema validation', 500, valid);
|
|
76
79
|
}
|
|
@@ -78,12 +81,12 @@ export const success = async (ctx, message, result, status) => {
|
|
|
78
81
|
// If there is a mask function, then use it to set up the value of the
|
|
79
82
|
// result.
|
|
80
83
|
if (typeof validator.mask === 'function') {
|
|
81
|
-
|
|
84
|
+
body = validator.mask(body);
|
|
82
85
|
}
|
|
83
86
|
}
|
|
84
87
|
|
|
85
88
|
ctx.status(status);
|
|
86
|
-
return ctx.json(
|
|
89
|
+
return ctx.json(body);
|
|
87
90
|
}
|
|
88
91
|
|
|
89
92
|
|
package/package.json
CHANGED