fastify-txstate 3.2.13 → 3.2.15
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/lib/index.js +32 -12
- package/package.json +2 -2
package/lib/index.js
CHANGED
|
@@ -18,6 +18,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
18
18
|
};
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
20
|
exports.prodLogger = exports.devLogger = void 0;
|
|
21
|
+
const ajv_1 = __importDefault(require("ajv"));
|
|
21
22
|
const swagger_1 = __importDefault(require("@fastify/swagger"));
|
|
22
23
|
const swagger_ui_1 = __importDefault(require("@fastify/swagger-ui"));
|
|
23
24
|
const fastify_shared_1 = require("@txstate-mws/fastify-shared");
|
|
@@ -103,7 +104,7 @@ class Server {
|
|
|
103
104
|
else
|
|
104
105
|
config.trustProxy = process.env.TRUST_PROXY;
|
|
105
106
|
}
|
|
106
|
-
config.ajv = { ...config.ajv, plugins: [...(config.ajv?.plugins ?? []), ajv_errors_1.default, [ajv_formats_1.default, { mode: 'fast' }]], customOptions: { ...config.ajv?.customOptions, allErrors: true, strictSchema: false } };
|
|
107
|
+
config.ajv = { ...config.ajv, plugins: [...(config.ajv?.plugins ?? []), ajv_errors_1.default, [ajv_formats_1.default, { mode: 'fast' }]], customOptions: { ...config.ajv?.customOptions, allErrors: true, strictSchema: false, coerceTypes: true } };
|
|
107
108
|
this.healthCallback = config.checkHealth;
|
|
108
109
|
this.app = (0, fastify_1.fastify)(config);
|
|
109
110
|
this.app.addHook('onRoute', route => {
|
|
@@ -117,21 +118,40 @@ class Server {
|
|
|
117
118
|
if (missingResponse) {
|
|
118
119
|
newSchema.response['200'] = {
|
|
119
120
|
description: 'Success. Return type has not been specified.',
|
|
120
|
-
type: '
|
|
121
|
+
type: 'object'
|
|
121
122
|
};
|
|
122
123
|
}
|
|
123
124
|
route.schema = newSchema;
|
|
124
125
|
});
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
126
|
+
this.app.addHook('preValidation', (req, res, done) => {
|
|
127
|
+
if (req.body != null && req.routeOptions.schema?.body)
|
|
128
|
+
(0, txstate_utils_1.destroyNulls)(req.body);
|
|
129
|
+
done();
|
|
130
|
+
});
|
|
131
|
+
// use Ajv to validate responses instead of @fastify/json-fast-stringify since ajv does
|
|
132
|
+
// a better job with recursive types and we don't want to have different behavior between
|
|
133
|
+
// input and output validation
|
|
134
|
+
const ajv = new ajv_1.default(config.ajv.customOptions);
|
|
135
|
+
for (const pluginConfig of config.ajv.plugins ?? []) {
|
|
136
|
+
const [plugin, opts] = (0, txstate_utils_1.toArray)(pluginConfig);
|
|
137
|
+
plugin(ajv, opts);
|
|
138
|
+
}
|
|
139
|
+
this.app.setSerializerCompiler((route) => {
|
|
140
|
+
const schema = route.schema;
|
|
141
|
+
const validate = schema == null ? ajv.compile({ type: 'object' }) : ajv.compile(schema);
|
|
142
|
+
return data => {
|
|
143
|
+
/**
|
|
144
|
+
* Ajv unfortunately treats optional properties as non-nullable, so they're allowed to
|
|
145
|
+
* be undefined but not allowed to be null. Worse, with `coerceTypes`, null will be converted
|
|
146
|
+
* to empty string or 0 or false. This is silly behavior, so we're converting all nulls to
|
|
147
|
+
* undefined before we validate.
|
|
148
|
+
*/
|
|
149
|
+
if (schema != null)
|
|
150
|
+
(0, txstate_utils_1.destroyNulls)(data);
|
|
151
|
+
if (!validate(data))
|
|
152
|
+
throw new Error('Output validation failed. ' + validate.errors?.[0].instancePath + ': ' + validate.errors?.[0].message);
|
|
153
|
+
return JSON.stringify(data);
|
|
154
|
+
};
|
|
135
155
|
});
|
|
136
156
|
if (!config.skipOriginCheck && !process.env.SKIP_ORIGIN_CHECK) {
|
|
137
157
|
this.setValidOrigins([...(config.validOrigins ?? []), ...(process.env.VALID_ORIGINS?.split(',') ?? [])]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fastify-txstate",
|
|
3
|
-
"version": "3.2.
|
|
3
|
+
"version": "3.2.15",
|
|
4
4
|
"description": "A small wrapper for fastify providing a set of common conventions & utility functions we use.",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"testserver": "node -r ts-node/register --no-warnings testserver/index.ts"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@elastic/elasticsearch": "^8.
|
|
21
|
+
"@elastic/elasticsearch": "^8.13.0",
|
|
22
22
|
"@fastify/swagger": "^8.14.0",
|
|
23
23
|
"@fastify/swagger-ui": "^3.0.0",
|
|
24
24
|
"@fastify/type-provider-json-schema-to-ts": "^3.0.0",
|