@openaddresses/batch-schema 3.1.3 → 4.2.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.
Files changed (30) hide show
  1. package/.github/workflows/test.yml +1 -1
  2. package/CHANGELOG.md +15 -0
  3. package/docs/assets/anchor.js +350 -0
  4. package/docs/assets/bass-addons.css +12 -0
  5. package/docs/assets/bass.css +544 -0
  6. package/docs/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
  7. package/docs/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
  8. package/docs/assets/fonts/LICENSE.txt +93 -0
  9. package/docs/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
  10. package/docs/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
  11. package/docs/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
  12. package/docs/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
  13. package/docs/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
  14. package/docs/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
  15. package/docs/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
  16. package/docs/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
  17. package/docs/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
  18. package/docs/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
  19. package/docs/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
  20. package/docs/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
  21. package/docs/assets/fonts/source-code-pro.css +23 -0
  22. package/docs/assets/github.css +123 -0
  23. package/docs/assets/site.js +168 -0
  24. package/docs/assets/split.css +15 -0
  25. package/docs/assets/split.js +782 -0
  26. package/docs/assets/style.css +147 -0
  27. package/docs/index.html +792 -0
  28. package/lib/schema.js +29 -11
  29. package/package.json +9 -6
  30. package/test/schema.srv.test.js +25 -22
package/lib/schema.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import { Validator } from 'express-json-validator-middleware';
2
2
  import { ValidationError } from 'express-json-validator-middleware';
3
3
  import $RefParser from 'json-schema-ref-parser';
4
+ import fs from 'fs';
5
+ import path from 'path';
4
6
 
5
7
  import Err from './error.js';
6
8
  import SchemaRoute from '../routes/schema.js';
@@ -35,15 +37,12 @@ class Param {
35
37
 
36
38
  /**
37
39
  * @class
40
+ *
41
+ * @param {Object} router Express Router Object
42
+ * @param {Object} opts Options Object
43
+ * @param {String} opts.schemas Schemas Path
38
44
  */
39
45
  export default class Schemas {
40
- /**
41
- * @constructor
42
- *
43
- * @param {Object} router Express Router Object
44
- * @param {Object} opts Options Object
45
- * @param {String} opts.schemas Schemas Path
46
- */
47
46
  constructor(router, opts = {}) {
48
47
  if (!router) throw new Error('Router Param Required');
49
48
 
@@ -68,6 +67,23 @@ export default class Schemas {
68
67
  await SchemaRoute(this);
69
68
  }
70
69
 
70
+ /**
71
+ * Load routes directory. All .js files in the given directory will be loaded (ES Module)
72
+ * and the default method run
73
+ *
74
+ * @param {String} dirpath Directory to load
75
+ * @param {object} config Config Object
76
+ * @param {Object} opts Options Object
77
+ * @param {boolean} opts.silent Squelch StdOut
78
+ */
79
+ async load(dirpath, config, opts = {}) {
80
+ // Load dynamic routes directory
81
+ for (const r of fs.readdirSync(dirpath)) {
82
+ if (!opts.silent) console.error(`ok - loaded routes/${r}`);
83
+ await (await import(path.resolve(dirpath, r))).default(this, config);
84
+ }
85
+ }
86
+
71
87
  check(url, schemas, fns) {
72
88
  if (typeof url !== 'string') throw new Error('URL should be string');
73
89
 
@@ -180,11 +196,13 @@ export default class Schemas {
180
196
  */
181
197
  static query(schema) {
182
198
  return function (req, res, next) {
183
- for (const key of Object.keys(req.query)) {
199
+ for (const key in req.query) {
184
200
  if (!schema.properties[key] || !schema.properties[key].type) continue;
185
201
 
186
202
  // For easier processing use consistent array format IE: `type: ["integer", "boolean"]` vs type: "integer"
187
- if (!Array.isArray(schema.properties[key].type)) schema.properties[key].type = [schema.properties[key].type];
203
+ if (!Array.isArray(schema.properties[key].type)) {
204
+ schema.properties[key].type = [schema.properties[key].type];
205
+ }
188
206
 
189
207
  for (const type of schema.properties[key].type) {
190
208
  if (type === 'integer' && !isNaN(parseInt(req.query[key]))) {
@@ -203,8 +221,8 @@ export default class Schemas {
203
221
  }
204
222
  }
205
223
 
206
- for (const key of Object.keys(schema.properties)) {
207
- if (req.query[key] === undefined && schema.properties[key].default) {
224
+ for (const key in schema.properties) {
225
+ if (req.query[key] === undefined && schema.properties[key].default !== undefined) {
208
226
  req.query[key] = schema.properties[key].default;
209
227
  }
210
228
  }
package/package.json CHANGED
@@ -3,27 +3,30 @@
3
3
  "type": "module",
4
4
  "root": true,
5
5
  "main": "index.js",
6
- "version": "3.1.3",
6
+ "version": "4.2.0",
7
7
  "description": "Strongly Validated JSON Schema support for address",
8
8
  "repository": "git@github.com:openaddresses/batch-schema",
9
9
  "author": "ingalls <nick@ingalls.ca>",
10
10
  "license": "MIT",
11
11
  "private": false,
12
+ "engines": {
13
+ "node": ">= 18"
14
+ },
12
15
  "scripts": {
13
16
  "test": "tape test/**.test.js",
14
- "lint": "eslint index.js lib/**.js routes/**.js test/**.js"
17
+ "lint": "eslint index.js lib/**.js routes/**.js test/**.js",
18
+ "doc": "documentation build index.js -f html -o docs && documentation lint index.js"
15
19
  },
16
20
  "dependencies": {
17
21
  "ajv": "^8.6.3",
18
22
  "express": "^4.17.1",
19
- "express-json-validator-middleware": "^2.2.1",
20
- "glob": "^7.2.0",
21
- "got": "^11.8.2",
23
+ "express-json-validator-middleware": "^3.0.0",
24
+ "glob": "^8.0.0",
22
25
  "json-schema-ref-parser": "^9.0.9",
23
- "request": "^2.88.2",
24
26
  "tape": "^5.3.1"
25
27
  },
26
28
  "devDependencies": {
29
+ "documentation": "^13.2.5",
27
30
  "eslint": "^8.0.1",
28
31
  "eslint-plugin-node": "^11.0.0"
29
32
  }
@@ -1,7 +1,6 @@
1
1
  import test from 'tape';
2
2
  import express from 'express';
3
3
  import { Schema } from '../index.js';
4
- import got from 'got';
5
4
 
6
5
  const app = express();
7
6
  const schema = new Schema(express.Router(), {
@@ -24,11 +23,10 @@ test('start', (t) => {
24
23
 
25
24
  test('GET: api/schema', async (t) => {
26
25
  try {
27
- const res = await got('http://localhost:2000/api/schema', {
28
- validateStatus: false
29
- }).json();
26
+ const res = await fetch('http://localhost:2000/api/schema');
27
+ const body = await res.json();
30
28
 
31
- t.deepEquals(res, {
29
+ t.deepEquals(body, {
32
30
  'GET /schema':{
33
31
  body: false,
34
32
  query: true,
@@ -44,22 +42,25 @@ test('GET: api/schema', async (t) => {
44
42
 
45
43
  test('GET: api/schema?method=FAKE', async (t) => {
46
44
  try {
47
- await got('http://localhost:2000/api/schema?method=fake');
48
- t.fail('4xx status code should throw');
49
- } catch (err) {
50
- t.deepEquals(JSON.parse(err.response.body), {
45
+ const res = await fetch('http://localhost:2000/api/schema?method=fake');
46
+
47
+ t.notOk(res.ok);
48
+
49
+ t.deepEquals(await res.json(), {
51
50
  status: 400,
52
51
  message: 'validation error',
53
52
  messages: [{
54
53
  keyword: 'enum',
55
- dataPath: '.method',
54
+ instancePath: '/method',
56
55
  schemaPath: '#/properties/method/enum',
57
56
  params: {
58
57
  allowedValues: ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH']
59
58
  },
60
- message: 'should be equal to one of the allowed values'
59
+ message: 'must be equal to one of the allowed values'
61
60
  }]
62
61
  });
62
+ } catch (err) {
63
+ t.error(err);
63
64
  }
64
65
 
65
66
  t.end();
@@ -67,28 +68,30 @@ test('GET: api/schema?method=FAKE', async (t) => {
67
68
 
68
69
  test('GET: api/schema?method=GET', async (t) => {
69
70
  try {
70
- await got('http://localhost:2000/api/schema?method=GET');
71
- t.fail('4xx status code should throw');
72
- } catch (err) {
73
- t.deepEquals(JSON.parse(err.response.body), {
71
+ const res = await fetch('http://localhost:2000/api/schema?method=GET');
72
+ t.notOk(res.ok);
73
+ t.deepEquals(await res.json(), {
74
74
  status: 400,
75
75
  message: 'url & method params must be used together',
76
76
  messages: []
77
77
  });
78
+ } catch (err) {
79
+ t.error(err);
78
80
  }
79
81
 
80
82
  t.end();
81
83
  });
82
84
  test('GET: api/schema?url=123', async (t) => {
83
85
  try {
84
- await got('http://localhost:2000/api/schema?url=123');
85
- t.fail('4xx status code should throw');
86
- } catch (err) {
87
- t.deepEquals(JSON.parse(err.response.body), {
86
+ const res = await fetch('http://localhost:2000/api/schema?url=123');
87
+ t.notOk(res.ok);
88
+ t.deepEquals(await res.json(), {
88
89
  status: 400,
89
90
  message: 'url & method params must be used together',
90
91
  messages: []
91
92
  });
93
+ } catch (err) {
94
+ t.error(err);
92
95
  }
93
96
 
94
97
  t.end();
@@ -96,10 +99,10 @@ test('GET: api/schema?url=123', async (t) => {
96
99
 
97
100
  test('GET: api/schema?method=GET&url=/schema', async (t) => {
98
101
  try {
99
- const res = await got('http://localhost:2000/api/schema?method=GET&url=/schema');
102
+ const res = await fetch('http://localhost:2000/api/schema?method=GET&url=/schema');
100
103
 
101
- t.equals(res.statusCode, 200, 'http: 200');
102
- t.deepEquals(Object.keys(JSON.parse(res.body)).sort(), ['query', 'body', 'res'].sort());
104
+ t.equals(res.status, 200, 'http: 200');
105
+ t.deepEquals(Object.keys(await res.json()).sort(), ['query', 'body', 'res'].sort());
103
106
 
104
107
  } catch (err) {
105
108
  t.error(err, 'no error');