@openaddresses/batch-schema 6.0.0 → 7.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.
- package/CHANGELOG.md +13 -0
- package/index.js +1 -5
- package/lib/docs.js +36 -0
- package/lib/middleware.js +4 -2
- package/lib/param.js +1 -1
- package/lib/schema.js +14 -2
- package/package.json +2 -1
- package/routes/schema.js +1 -1
- package/test/schema.srv.test.js +1 -1
- package/lib/error.js +0 -43
package/CHANGELOG.md
CHANGED
|
@@ -10,6 +10,19 @@
|
|
|
10
10
|
|
|
11
11
|
## Version History
|
|
12
12
|
|
|
13
|
+
### v7.2.0
|
|
14
|
+
|
|
15
|
+
- :tada: Preliminary support for automatically generated an APIDoc definitions file
|
|
16
|
+
|
|
17
|
+
### v7.1.0
|
|
18
|
+
|
|
19
|
+
- :bug: Only validate response on 200 status
|
|
20
|
+
|
|
21
|
+
### v7.0.0
|
|
22
|
+
|
|
23
|
+
- :rocket: Remove PublicError class to it's own package
|
|
24
|
+
- :rocket: `Schema` is now exposed as the default
|
|
25
|
+
|
|
13
26
|
### v6.0.0
|
|
14
27
|
|
|
15
28
|
- :rocket: Remove additional properties if they are not present in `res` and `additionalProperties: false` is set
|
package/index.js
CHANGED
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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import Err from '
|
|
1
|
+
import Err from '@openaddresses/batch-error';
|
|
2
2
|
import Param from './param.js';
|
|
3
3
|
import Ajv from 'ajv';
|
|
4
4
|
import addFormats from 'ajv-formats';
|
|
@@ -110,7 +110,9 @@ export default class Middleware {
|
|
|
110
110
|
return function (req, res, next) {
|
|
111
111
|
const json = res.json;
|
|
112
112
|
res.json = function(obj) {
|
|
113
|
-
|
|
113
|
+
if (res.statusCode === null || res.statusCode === 200) {
|
|
114
|
+
validate(obj);
|
|
115
|
+
}
|
|
114
116
|
|
|
115
117
|
json.call(this, obj);
|
|
116
118
|
};
|
package/lib/param.js
CHANGED
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 = {};
|
|
@@ -148,7 +157,6 @@ export default class Schemas {
|
|
|
148
157
|
|
|
149
158
|
flow[1].push(this.validate(opts));
|
|
150
159
|
|
|
151
|
-
console.error(schemas);
|
|
152
160
|
if (schemas.res) flow[1].push(Middleware.res(schemas.res));
|
|
153
161
|
|
|
154
162
|
return flow;
|
|
@@ -179,7 +187,11 @@ export default class Schemas {
|
|
|
179
187
|
if (!schema.body) schema.body = null;
|
|
180
188
|
if (!schema.res) schema.res = null;
|
|
181
189
|
|
|
182
|
-
return
|
|
190
|
+
return {
|
|
191
|
+
query: schema.query,
|
|
192
|
+
body: schema.body,
|
|
193
|
+
res: schema.res
|
|
194
|
+
};
|
|
183
195
|
}
|
|
184
196
|
|
|
185
197
|
/**
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"type": "module",
|
|
4
4
|
"root": true,
|
|
5
5
|
"main": "index.js",
|
|
6
|
-
"version": "
|
|
6
|
+
"version": "7.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>",
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"doc": "documentation build index.js -f html -o docs && documentation lint index.js"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
+
"@openaddresses/batch-error": "^1.0.1",
|
|
21
22
|
"ajv": "^8.6.3",
|
|
22
23
|
"ajv-formats": "^2.1.1",
|
|
23
24
|
"body-parser": "^1.20.0",
|
package/routes/schema.js
CHANGED
package/test/schema.srv.test.js
CHANGED
package/lib/error.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @class
|
|
3
|
-
*/
|
|
4
|
-
export default class PublicError {
|
|
5
|
-
constructor(status, err, safe, print = true) {
|
|
6
|
-
// Wrap postgres errors to ensure stack trace (line nums) are returned
|
|
7
|
-
if (err && err.severity) err = new Error(err);
|
|
8
|
-
|
|
9
|
-
if (print && ![400, 401, 402, 403, 404].includes(status)) console.error(err ? err : 'Error: ' + safe);
|
|
10
|
-
|
|
11
|
-
this.status = status;
|
|
12
|
-
this.err = err;
|
|
13
|
-
this.safe = safe;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
static respond(err, res, messages = []) {
|
|
17
|
-
if (err instanceof PublicError) {
|
|
18
|
-
res.status(err.status).json({
|
|
19
|
-
status: err.status,
|
|
20
|
-
message: err.safe,
|
|
21
|
-
messages: messages
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
// Duplicate Express supported Error extensions
|
|
25
|
-
} else if (err instanceof Error && (err.status || err.statusCode)) {
|
|
26
|
-
res.status(err.status || err.statusCode).json({
|
|
27
|
-
status: err.status || err.statusCode,
|
|
28
|
-
message: err.message || 'Generic Error',
|
|
29
|
-
messages: messages
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
// Handle generic errors
|
|
33
|
-
} else {
|
|
34
|
-
console.error(err);
|
|
35
|
-
|
|
36
|
-
res.status(500).json({
|
|
37
|
-
status: 500,
|
|
38
|
-
message: 'Internal Server Error',
|
|
39
|
-
messages: messages
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|