@openaddresses/batch-schema 7.1.0 → 7.3.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/CHANGELOG.md +8 -0
- package/lib/docs.js +36 -0
- package/lib/middleware.js +6 -0
- package/lib/schema.js +14 -1
- package/package.json +4 -1
package/CHANGELOG.md
CHANGED
|
@@ -10,6 +10,14 @@
|
|
|
10
10
|
|
|
11
11
|
## Version History
|
|
12
12
|
|
|
13
|
+
### v7.3.0
|
|
14
|
+
|
|
15
|
+
- :arrow_up: `res.json` will automatically serialize Generic objects
|
|
16
|
+
|
|
17
|
+
### v7.2.0
|
|
18
|
+
|
|
19
|
+
- :tada: Preliminary support for automatically generated an APIDoc definitions file
|
|
20
|
+
|
|
13
21
|
### v7.1.0
|
|
14
22
|
|
|
15
23
|
- :bug: Only validate response on 200 status
|
package/lib/docs.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @class Docs
|
|
3
|
+
*/
|
|
4
|
+
export default class Docs extends Array {
|
|
5
|
+
push(parsed, schemas) {
|
|
6
|
+
const apiParams = [];
|
|
7
|
+
for (const key in schemas) {
|
|
8
|
+
if (key[0] === ':') {
|
|
9
|
+
apiParams.push(`* @apiParam {${schemas[key]}} ${key.slice(1)} param`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
super.push(
|
|
14
|
+
`
|
|
15
|
+
/**
|
|
16
|
+
* @api {${parsed[0].toLowerCase()}} ${parsed[1]} ${schemas.name || parsed.join(' ')}
|
|
17
|
+
* @apiVersion ${schemas.version || '1.0.0'}
|
|
18
|
+
* @apiName ${parsed.join('-')}
|
|
19
|
+
* @apiGroup ${schemas.group || 'Default'}
|
|
20
|
+
* @apiPermission ${schemas.auth || 'Unknown'}
|
|
21
|
+
*
|
|
22
|
+
* @apidescription
|
|
23
|
+
* ${schemas.description || 'No Description'}
|
|
24
|
+
*
|
|
25
|
+
${apiParams.join('\n')}
|
|
26
|
+
*
|
|
27
|
+
* ${schemas.query_url ? `@apiSchema (Query) {jsonschema=${schemas.query_url}} apiParam` : ''}
|
|
28
|
+
* ${schemas.body_url ? `@apiSchema (Body) {jsonschema=${schemas.body_url}} apiParam` : ''}
|
|
29
|
+
* ${schemas.res_url ? `@apiSchema {jsonschema=${schemas.res_url}} apiSuccess` : ''}
|
|
30
|
+
*/
|
|
31
|
+
`.split('\n')
|
|
32
|
+
.map((l) => l.trim())
|
|
33
|
+
.join('\n')
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
}
|
package/lib/middleware.js
CHANGED
|
@@ -3,6 +3,7 @@ import Param from './param.js';
|
|
|
3
3
|
import Ajv from 'ajv';
|
|
4
4
|
import addFormats from 'ajv-formats';
|
|
5
5
|
import { ValidationError } from 'express-json-validator-middleware';
|
|
6
|
+
import Generic from '@openaddresses/batch-generic';
|
|
6
7
|
|
|
7
8
|
const ajv = addFormats(new Ajv({
|
|
8
9
|
allErrors: true,
|
|
@@ -99,6 +100,7 @@ export default class Middleware {
|
|
|
99
100
|
|
|
100
101
|
/**
|
|
101
102
|
* Express middleware to validate the response body
|
|
103
|
+
* and optionally serialize Generic object to JSON
|
|
102
104
|
*
|
|
103
105
|
* @param {Object} schema JSON Schema
|
|
104
106
|
*
|
|
@@ -110,6 +112,10 @@ export default class Middleware {
|
|
|
110
112
|
return function (req, res, next) {
|
|
111
113
|
const json = res.json;
|
|
112
114
|
res.json = function(obj) {
|
|
115
|
+
if (obj instanceof Generic && typeof obj.serialize === 'function') {
|
|
116
|
+
obj = obj.serialize();
|
|
117
|
+
}
|
|
118
|
+
|
|
113
119
|
if (res.statusCode === null || res.statusCode === 200) {
|
|
114
120
|
validate(obj);
|
|
115
121
|
}
|
package/lib/schema.js
CHANGED
|
@@ -7,6 +7,7 @@ import bodyparser from 'body-parser';
|
|
|
7
7
|
|
|
8
8
|
import SchemaRoute from '../routes/schema.js';
|
|
9
9
|
import Param from './param.js';
|
|
10
|
+
import Docs from './docs.js';
|
|
10
11
|
import Middleware from './middleware.js';
|
|
11
12
|
|
|
12
13
|
/**
|
|
@@ -17,6 +18,8 @@ import Middleware from './middleware.js';
|
|
|
17
18
|
* @param {String} opts.schemas Schemas Path
|
|
18
19
|
* @param {boolean|string} [opts.logging=true] disable logging with false
|
|
19
20
|
* @param {number} [opts.limit=50] body size limit in mb
|
|
21
|
+
*
|
|
22
|
+
* @param {URL} [opts.apidoc] apidoc file location
|
|
20
23
|
*/
|
|
21
24
|
export default class Schemas {
|
|
22
25
|
constructor(router, opts = {}) {
|
|
@@ -45,6 +48,8 @@ export default class Schemas {
|
|
|
45
48
|
|
|
46
49
|
this.schemas = new Map();
|
|
47
50
|
this.validate = this.validator.validate;
|
|
51
|
+
|
|
52
|
+
this.docs = new Docs();
|
|
48
53
|
}
|
|
49
54
|
|
|
50
55
|
async api() {
|
|
@@ -121,6 +126,8 @@ export default class Schemas {
|
|
|
121
126
|
for (const type of ['body', 'query', 'res']) {
|
|
122
127
|
if (!schemas[type]) continue;
|
|
123
128
|
|
|
129
|
+
// TODO: Write schemas to a tmp directory so blueprints & main schemas are in the same place
|
|
130
|
+
schemas[`${type}_url`] = '../schema/' + schemas[type];
|
|
124
131
|
try {
|
|
125
132
|
schemas[type] = await $RefParser.dereference((new URL(`../schema/${schemas[type]}`, import.meta.url)).pathname);
|
|
126
133
|
} catch (err) {
|
|
@@ -128,6 +135,8 @@ export default class Schemas {
|
|
|
128
135
|
}
|
|
129
136
|
}
|
|
130
137
|
|
|
138
|
+
this.docs.push(parsed, schemas);
|
|
139
|
+
|
|
131
140
|
this.schemas.set(parsed.join(' '), schemas);
|
|
132
141
|
|
|
133
142
|
const opts = {};
|
|
@@ -178,7 +187,11 @@ export default class Schemas {
|
|
|
178
187
|
if (!schema.body) schema.body = null;
|
|
179
188
|
if (!schema.res) schema.res = null;
|
|
180
189
|
|
|
181
|
-
return
|
|
190
|
+
return {
|
|
191
|
+
query: schema.query,
|
|
192
|
+
body: schema.body,
|
|
193
|
+
res: schema.res
|
|
194
|
+
};
|
|
182
195
|
}
|
|
183
196
|
|
|
184
197
|
/**
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"type": "module",
|
|
4
4
|
"root": true,
|
|
5
5
|
"main": "index.js",
|
|
6
|
-
"version": "7.
|
|
6
|
+
"version": "7.3.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>",
|
|
@@ -29,6 +29,9 @@
|
|
|
29
29
|
"morgan": "^1.10.0",
|
|
30
30
|
"tape": "^5.3.1"
|
|
31
31
|
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"@openaddresses/batch-generic": "x.x.x"
|
|
34
|
+
},
|
|
32
35
|
"devDependencies": {
|
|
33
36
|
"documentation": "^13.2.5",
|
|
34
37
|
"eslint": "^8.0.1",
|