fastify-txstate 3.2.12 → 3.2.14

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.
Files changed (2) hide show
  1. package/lib/index.js +60 -16
  2. 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");
@@ -117,27 +118,46 @@ class Server {
117
118
  if (missingResponse) {
118
119
  newSchema.response['200'] = {
119
120
  description: 'Success. Return type has not been specified.',
120
- type: 'null'
121
+ type: 'object'
121
122
  };
122
123
  }
123
124
  route.schema = newSchema;
124
125
  });
125
- /**
126
- * Fastify validates response schema while serializing the response
127
- *
128
- * This is great, but because Ajv treats optional properties as non-nullable, optional
129
- * properties that are set to `null` instead of `undefined` will fail validation (or with `coerceTypes`,
130
- * be converted to empty string or 0 or false). This is silly behavior, so we're adding a hook to
131
- * convert all nulls to undefined before fastify validates.
132
- */
133
- this.app.addHook('preSerialization', async (req, res, payload) => {
134
- return req.routeSchema?.response ? (0, txstate_utils_1.destroyNulls)(payload) : payload;
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].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(',') ?? [])]);
138
158
  this.setValidOriginHosts([...(config.validOriginHosts ?? []), ...(process.env.VALID_ORIGIN_HOSTS?.split(',') ?? [])]);
139
159
  this.setValidOriginSuffixes([...(config.validOriginSuffixes ?? []), ...(process.env.VALID_ORIGIN_SUFFIXES?.split(',') ?? [])]);
140
- this.app.addHook('preHandler', async (req, res) => {
160
+ this.app.addHook('onRequest', async (req, res) => {
141
161
  res.extraLogInfo = {};
142
162
  if (!req.headers.origin)
143
163
  return;
@@ -186,7 +206,7 @@ class Server {
186
206
  PATCH: true,
187
207
  DELETE: true
188
208
  };
189
- this.app.addHook('preHandler', async (req, res) => {
209
+ this.app.addHook('onRequest', async (req, res) => {
190
210
  if (!authenticatedMethods[req.method])
191
211
  return;
192
212
  try {
@@ -230,14 +250,14 @@ class Server {
230
250
  for (const v of err.validation ?? []) {
231
251
  if (v.keyword === 'errorMessage') {
232
252
  for (const ov of v.params.errors) {
233
- if (['type', 'additionalProperties'].includes(ov.keyword))
253
+ if (['type', 'additionalProperties', 'minProperties'].includes(ov.keyword))
234
254
  developerErrors.push({ ...ov, message: v.message });
235
255
  else
236
256
  userErrors.push({ ...ov, message: v.message });
237
257
  }
238
258
  }
239
259
  else {
240
- if (['type', 'additionalProperties'].includes(v.keyword))
260
+ if (['type', 'additionalProperties', 'minProperties'].includes(v.keyword))
241
261
  developerErrors.push(v);
242
262
  else
243
263
  userErrors.push(v);
@@ -340,7 +360,31 @@ this is log into this application and use dev tools to pull your token from the
340
360
  // Apply the security globally to all operations
341
361
  openapi.security = [{ unifiedAuth: [] }];
342
362
  }
343
- await this.app.register(swagger_1.default, { openapi });
363
+ function findRefs(obj, id) {
364
+ if (obj == null)
365
+ return undefined;
366
+ if (obj.$id?.length)
367
+ id = obj.$id;
368
+ if (obj.$ref === '#' && id?.length) {
369
+ obj.type = 'string';
370
+ obj.enum = [id];
371
+ delete obj.$ref;
372
+ }
373
+ else {
374
+ for (const val of Object.values(obj)) {
375
+ if (typeof val === 'object' && !(val instanceof Date))
376
+ findRefs(val, id);
377
+ }
378
+ }
379
+ return obj;
380
+ }
381
+ await this.app.register(swagger_1.default, {
382
+ openapi,
383
+ transform({ schema, url, route, swaggerObject, openapiObject }) {
384
+ const newSchema = findRefs((0, txstate_utils_1.clone)(schema));
385
+ return { schema: newSchema, url, route, swaggerObject, openapiObject };
386
+ }
387
+ });
344
388
  await this.app.register(swagger_ui_1.default, { ...opts?.ui, routePrefix: opts?.path ?? opts?.ui?.routePrefix ?? '/docs' });
345
389
  }
346
390
  async close(softSeconds) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastify-txstate",
3
- "version": "3.2.12",
3
+ "version": "3.2.14",
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.12.2",
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",