@openaddresses/batch-schema 2.3.4 → 3.1.0

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/.eslintrc.json CHANGED
@@ -8,7 +8,8 @@
8
8
  "Map": true
9
9
  },
10
10
  "parserOptions": {
11
- "ecmaVersion": 13
11
+ "ecmaVersion": 13,
12
+ "sourceType": "module"
12
13
  },
13
14
  "plugins": [ "node" ],
14
15
  "rules": {
@@ -39,7 +40,6 @@
39
40
  "template-curly-spacing": [ "error", "never" ],
40
41
  "semi-spacing": "error",
41
42
  "strict": "error",
42
- "node/no-unsupported-features": [ "error", { "version": 8 } ],
43
43
  "node/no-missing-require": "error",
44
44
  "valid-jsdoc": ["error", {
45
45
  "requireParamDescription": false,
package/CHANGELOG.md CHANGED
@@ -10,6 +10,18 @@
10
10
 
11
11
  ## Version History
12
12
 
13
+ ### v3.0.1
14
+
15
+ - :rocket: Add `.main` property to package.json
16
+
17
+ ### v3.0.0
18
+
19
+ - :rocket: Update to ES Module System
20
+
21
+ ### v2.4.0
22
+
23
+ - :tada: Add `schema.not_found()`
24
+
13
25
  ### v2.3.4
14
26
 
15
27
  - :arrow_up: Update base deps
package/README.md CHANGED
@@ -43,6 +43,9 @@ async function server() {
43
43
  });
44
44
  });
45
45
 
46
+ // Handle Unmatched Routes
47
+ schema.not_found();
48
+
46
49
  // Handle Validation Errors => JSON Middleware
47
50
  schema.error();
48
51
  }
@@ -72,6 +75,15 @@ Adds a route called `GET /schema` which allows the caller to get a list of endpo
72
75
  as well as full schema details for every route. If your API is public we recommend enabling this feature, however
73
76
  if you do not wish for API routes to be published, this feature is disabled unless called.
74
77
 
78
+ ### schema.not_found
79
+
80
+ Adds a middlware which will catch all routes that have not been defined and return
81
+ a standard error object.
82
+
83
+ ```
84
+ schema.not_found()
85
+ ```
86
+
75
87
  ### schema.error
76
88
 
77
89
  Adds a middlware which will convert validation errors into a standard JSON error format.
package/index.js CHANGED
@@ -1,4 +1,7 @@
1
- 'use strict';
1
+ import Schema from './lib/schema.js';
2
+ import Err from './lib/error.js';
2
3
 
3
- module.exports.Schema = require('./lib/schema');
4
- module.exports.Err = require('./lib/error')
4
+ export {
5
+ Schema,
6
+ Err
7
+ };
package/lib/error.js CHANGED
@@ -1,9 +1,7 @@
1
- 'use strict';
2
-
3
1
  /**
4
2
  * @class
5
3
  */
6
- class PublicError {
4
+ export default class PublicError {
7
5
  constructor(status, err, safe, print = true) {
8
6
  // Wrap postgres errors to ensure stack trace (line nums) are returned
9
7
  if (err && err.severity) err = new Error(err);
@@ -43,5 +41,3 @@ class PublicError {
43
41
  }
44
42
  }
45
43
  }
46
-
47
- module.exports = PublicError;
package/lib/schema.js CHANGED
@@ -1,11 +1,9 @@
1
- 'use strict';
1
+ import { Validator } from 'express-json-validator-middleware';
2
+ import { ValidationError } from 'express-json-validator-middleware';
3
+ import $RefParser from 'json-schema-ref-parser';
2
4
 
3
- const { Validator } = require('express-json-validator-middleware');
4
- const $RefParser = require('json-schema-ref-parser');
5
- const path = require('path');
6
- const Err = require('./error');
7
- const SchemaRoute = require('../routes/schema');
8
- const { ValidationError } = require('express-json-validator-middleware');
5
+ import Err from './error.js';
6
+ import SchemaRoute from '../routes/schema.js';
9
7
 
10
8
  /**
11
9
  * @class
@@ -38,7 +36,7 @@ class Param {
38
36
  /**
39
37
  * @class
40
38
  */
41
- class Schemas {
39
+ export default class Schemas {
42
40
  /**
43
41
  * @constructor
44
42
  *
@@ -50,7 +48,7 @@ class Schemas {
50
48
  if (!router) throw new Error('Router Param Required');
51
49
 
52
50
  if (!opts.schemas) {
53
- this.schemas_path = path.resolve(__dirname, '../schema/');
51
+ this.schemas_path = String(new URL('../schema/', import.meta.url)).replace('file://', '');
54
52
  } else {
55
53
  this.schemas_path = opts.schemas;
56
54
  }
@@ -121,9 +119,9 @@ class Schemas {
121
119
  if (!schemas[type]) continue;
122
120
 
123
121
  try {
124
- schemas[type] = await $RefParser.dereference(path.resolve(path.resolve(__dirname, '../schema/'), schemas[type]));
122
+ schemas[type] = await $RefParser.dereference(`../schema/${schemas[type]}`);
125
123
  } catch (err) {
126
- schemas[type] = await $RefParser.dereference(path.resolve(this.schemas_path, schemas[type]));
124
+ schemas[type] = await $RefParser.dereference(`${this.schemas_path}/${schemas[type]}`);
127
125
  }
128
126
  }
129
127
 
@@ -236,6 +234,19 @@ class Schemas {
236
234
  return schema;
237
235
  }
238
236
 
237
+ /**
238
+ * Catch all for unmatched routes
239
+ */
240
+ not_found() {
241
+ this.router.all('*', (req, res) => {
242
+ return res.status(404).json({
243
+ status: 404,
244
+ message: 'API endpoint does not exist!',
245
+ messages: []
246
+ });
247
+ });
248
+ }
249
+
239
250
  /**
240
251
  * Convert validation errors into standardized JSON Error Messages
241
252
  */
@@ -281,5 +292,3 @@ class Schemas {
281
292
  return lite;
282
293
  }
283
294
  }
284
-
285
- module.exports = Schemas;
package/package.json CHANGED
@@ -1,7 +1,9 @@
1
1
  {
2
2
  "name": "@openaddresses/batch-schema",
3
+ "type": "module",
3
4
  "root": true,
4
- "version": "2.3.4",
5
+ "main": "index.js",
6
+ "version": "3.1.0",
5
7
  "description": "Strongly Validated JSON Schema support for address",
6
8
  "repository": "git@github.com:openaddresses/batch-schema",
7
9
  "author": "ingalls <nick@ingalls.ca>",
package/routes/schema.js CHANGED
@@ -1,7 +1,6 @@
1
- 'use strict';
2
- const Err = require('../lib/error');
1
+ import Err from '../lib/error.js';
3
2
 
4
- async function router(schema) {
3
+ export default async function router(schema) {
5
4
  /**
6
5
  * @api {get} /api/schema List Schemas
7
6
  * @apiVersion 1.0.0
@@ -36,5 +35,3 @@ async function router(schema) {
36
35
  }
37
36
  });
38
37
  }
39
-
40
- module.exports = router;
@@ -1,8 +1,7 @@
1
- 'use strict';
2
- const test = require('tape');
3
- const express = require('express');
4
- const { Schema } = require('../');
5
- const got = require('got');
1
+ import test from 'tape';
2
+ import express from 'express';
3
+ import { Schema } from '../index.js';
4
+ import got from 'got';
6
5
 
7
6
  const app = express();
8
7
  const schema = new Schema(express.Router(), {
@@ -1,17 +1,16 @@
1
- 'use strict';
2
- const fs = require('fs');
3
- const path = require('path');
4
- const test = require('tape');
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import test from 'tape';
5
4
 
6
- const glob = require('glob');
7
- const $RefParser = require('json-schema-ref-parser');
8
- const Ajv = require('ajv');
5
+ import glob from 'glob';
6
+ import $RefParser from 'json-schema-ref-parser';
7
+ import Ajv from 'ajv';
9
8
 
10
9
  const ajv = new Ajv({
11
10
  allErrors: true
12
11
  });
13
12
 
14
- glob.sync(path.resolve(__dirname, '../schema/**/*.json')).forEach((source) => {
13
+ for (const source of glob.sync('../schema/**/*.json')) {
15
14
  test(`schema/${path.parse(source).base}`, async (t) => {
16
15
  try {
17
16
  const file = fs.readFileSync(source);
@@ -31,4 +30,4 @@ glob.sync(path.resolve(__dirname, '../schema/**/*.json')).forEach((source) => {
31
30
  }
32
31
  t.end();
33
32
  });
34
- });
33
+ }