fastify-txstate 3.2.11 → 3.2.13

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 +33 -8
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -28,7 +28,6 @@ const node_fs_1 = __importDefault(require("node:fs"));
28
28
  const node_http_1 = __importDefault(require("node:http"));
29
29
  const txstate_utils_1 = require("txstate-utils");
30
30
  const error_1 = require("./error");
31
- const unified_auth_1 = require("./unified-auth");
32
31
  exports.devLogger = {
33
32
  level: 'info',
34
33
  info: (msg) => { console.info(msg.req ? `${msg.req.method} ${msg.req.url}` : msg.res ? `${msg.res.statusCode} - ${msg.responseTime}` : msg); },
@@ -132,13 +131,13 @@ class Server {
132
131
  * convert all nulls to undefined before fastify validates.
133
132
  */
134
133
  this.app.addHook('preSerialization', async (req, res, payload) => {
135
- return req.routeSchema?.response ? (0, txstate_utils_1.destroyNulls)(payload) : payload;
134
+ return req.routeOptions.schema?.response ? (0, txstate_utils_1.destroyNulls)(payload) : payload;
136
135
  });
137
136
  if (!config.skipOriginCheck && !process.env.SKIP_ORIGIN_CHECK) {
138
137
  this.setValidOrigins([...(config.validOrigins ?? []), ...(process.env.VALID_ORIGINS?.split(',') ?? [])]);
139
138
  this.setValidOriginHosts([...(config.validOriginHosts ?? []), ...(process.env.VALID_ORIGIN_HOSTS?.split(',') ?? [])]);
140
139
  this.setValidOriginSuffixes([...(config.validOriginSuffixes ?? []), ...(process.env.VALID_ORIGIN_SUFFIXES?.split(',') ?? [])]);
141
- this.app.addHook('preHandler', async (req, res) => {
140
+ this.app.addHook('onRequest', async (req, res) => {
142
141
  res.extraLogInfo = {};
143
142
  if (!req.headers.origin)
144
143
  return;
@@ -169,6 +168,8 @@ class Server {
169
168
  else {
170
169
  void res.header('Access-Control-Allow-Origin', req.headers.origin);
171
170
  void res.header('Access-Control-Max-Age', '600'); // ask browser to skip pre-flights for 10 minutes after a yes
171
+ if (req.headers['access-control-request-method'])
172
+ void res.header('access-control-allow-methods', req.headers['access-control-request-method']);
172
173
  if (req.headers['access-control-request-headers'])
173
174
  void res.header('Access-Control-Allow-Headers', req.headers['access-control-request-headers']);
174
175
  }
@@ -185,7 +186,7 @@ class Server {
185
186
  PATCH: true,
186
187
  DELETE: true
187
188
  };
188
- this.app.addHook('preHandler', async (req, res) => {
189
+ this.app.addHook('onRequest', async (req, res) => {
189
190
  if (!authenticatedMethods[req.method])
190
191
  return;
191
192
  try {
@@ -229,14 +230,14 @@ class Server {
229
230
  for (const v of err.validation ?? []) {
230
231
  if (v.keyword === 'errorMessage') {
231
232
  for (const ov of v.params.errors) {
232
- if (['type', 'additionalProperties'].includes(ov.keyword))
233
+ if (['type', 'additionalProperties', 'minProperties'].includes(ov.keyword))
233
234
  developerErrors.push({ ...ov, message: v.message });
234
235
  else
235
236
  userErrors.push({ ...ov, message: v.message });
236
237
  }
237
238
  }
238
239
  else {
239
- if (['type', 'additionalProperties'].includes(v.keyword))
240
+ if (['type', 'additionalProperties', 'minProperties'].includes(v.keyword))
240
241
  developerErrors.push(v);
241
242
  else
242
243
  userErrors.push(v);
@@ -326,7 +327,7 @@ class Server {
326
327
  }
327
328
  async swagger(opts) {
328
329
  let openapi = opts?.openapi ?? {};
329
- if (this.config.authenticate === unified_auth_1.unifiedAuthenticate) {
330
+ if (this.config.authenticate != null) {
330
331
  openapi = (0, txstate_utils_1.set)(openapi, 'components.securitySchemes', {
331
332
  unifiedAuth: {
332
333
  type: 'http',
@@ -339,7 +340,31 @@ this is log into this application and use dev tools to pull your token from the
339
340
  // Apply the security globally to all operations
340
341
  openapi.security = [{ unifiedAuth: [] }];
341
342
  }
342
- await this.app.register(swagger_1.default, { openapi });
343
+ function findRefs(obj, id) {
344
+ if (obj == null)
345
+ return undefined;
346
+ if (obj.$id?.length)
347
+ id = obj.$id;
348
+ if (obj.$ref === '#' && id?.length) {
349
+ obj.type = 'string';
350
+ obj.enum = [id];
351
+ delete obj.$ref;
352
+ }
353
+ else {
354
+ for (const val of Object.values(obj)) {
355
+ if (typeof val === 'object' && !(val instanceof Date))
356
+ findRefs(val, id);
357
+ }
358
+ }
359
+ return obj;
360
+ }
361
+ await this.app.register(swagger_1.default, {
362
+ openapi,
363
+ transform({ schema, url, route, swaggerObject, openapiObject }) {
364
+ const newSchema = findRefs((0, txstate_utils_1.clone)(schema));
365
+ return { schema: newSchema, url, route, swaggerObject, openapiObject };
366
+ }
367
+ });
343
368
  await this.app.register(swagger_ui_1.default, { ...opts?.ui, routePrefix: opts?.path ?? opts?.ui?.routePrefix ?? '/docs' });
344
369
  }
345
370
  async close(softSeconds) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastify-txstate",
3
- "version": "3.2.11",
3
+ "version": "3.2.13",
4
4
  "description": "A small wrapper for fastify providing a set of common conventions & utility functions we use.",
5
5
  "exports": {
6
6
  ".": {