@sleeksky/alt-swagger 2.0.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/LICENSE +21 -0
- package/README.md +20 -0
- package/package.json +45 -0
- package/src/index.js +146 -0
- package/src/utils.js +93 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 SleekSky LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Spec API
|
|
2
|
+
|
|
3
|
+
Quickly spec your APIs for Swagger / OpenAPI interface. Uses @sleeksky/alt-schema for specifying JSON schema.
|
|
4
|
+
|
|
5
|
+
# Example
|
|
6
|
+
```JavaScript
|
|
7
|
+
let refRequest = docs.ref.schema('example','{id:i:1,name:s:foo,label:{id:i:2,name:?s:bar},arr:[{a:b:false}]}');
|
|
8
|
+
|
|
9
|
+
docs.put('/:id').tag("dot-notation").req(refRequest).res(200, '{hello,world}');
|
|
10
|
+
```
|
|
11
|
+

|
|
12
|
+
# Installation
|
|
13
|
+
|
|
14
|
+
npm install -s @sleeksky/alt-swagger
|
|
15
|
+
|
|
16
|
+
const docs = require('@sleeksky/alt-swagger');
|
|
17
|
+
|
|
18
|
+
# License
|
|
19
|
+
|
|
20
|
+
MIT © SleekSky
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sleeksky/alt-swagger",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Quickly spec your APIs for Swagger / OpenAPI interface",
|
|
5
|
+
"main": "./src/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "mocha",
|
|
8
|
+
"cover": "nyc --check-coverage npm run test",
|
|
9
|
+
"prepublish": "npm run test"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"src"
|
|
13
|
+
],
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/sleeksky-dev/alt-swagger"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"swagger",
|
|
20
|
+
"open-api",
|
|
21
|
+
"api-spec",
|
|
22
|
+
"express",
|
|
23
|
+
"javascript"
|
|
24
|
+
],
|
|
25
|
+
"author": "Yusuf <yusuf@sleeksky.com>",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/sleeksky-dev/alt-swagger/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/sleeksky-dev/alt-swagger#readme",
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"chai": "^4.1.2",
|
|
33
|
+
"cross-env": "^5.1.3",
|
|
34
|
+
"express": "^4.17.1",
|
|
35
|
+
"mocha": "^6.1.3",
|
|
36
|
+
"nyc": "^13.3.0",
|
|
37
|
+
"swagger-ui-express": "^4.1.6"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"lodash": "^4.17.21"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"@sleeksky/alt-schema": "2.x"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Author: Yusuf Bhabhrawala
|
|
3
|
+
*/
|
|
4
|
+
require("./utils");
|
|
5
|
+
const _ = require("lodash");
|
|
6
|
+
const { toSwaggerSchema, pathParameters, toParameter} = require("./utils")
|
|
7
|
+
|
|
8
|
+
const paths = {};
|
|
9
|
+
const components = {};
|
|
10
|
+
const tags = [];
|
|
11
|
+
const servers = [];
|
|
12
|
+
|
|
13
|
+
function reset() {
|
|
14
|
+
Object.keys(paths).forEach((key) => delete paths[key]);
|
|
15
|
+
Object.keys(components).forEach((key) => delete paths[key]);
|
|
16
|
+
tags.splice(0, tags.length);
|
|
17
|
+
servers.splice(0, servers.length);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function server(url, description) {
|
|
21
|
+
servers.push({ url, description });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function api(opt) {
|
|
25
|
+
const spec = { parameters: [], responses: {}, description: "" };
|
|
26
|
+
const pathParams = pathParameters(opt.path);
|
|
27
|
+
if (pathParams.length > 0) spec.parameters = pathParams;
|
|
28
|
+
|
|
29
|
+
_.set(paths, `${opt.path}.${opt.method}`, spec);
|
|
30
|
+
|
|
31
|
+
let ext = {};
|
|
32
|
+
|
|
33
|
+
const req = (flatSch) => {
|
|
34
|
+
let schema = flatSch.match(/^#/) ? { $ref: flatSch } : toSwaggerSchema(flatSch);
|
|
35
|
+
spec.requestBody = {
|
|
36
|
+
content: { "application/json": { schema } },
|
|
37
|
+
};
|
|
38
|
+
return ext;
|
|
39
|
+
};
|
|
40
|
+
const res = (code, flatSch) => {
|
|
41
|
+
let schema = flatSch.match(/^#/) ? { $ref: flatSch } : toSwaggerSchema(flatSch);
|
|
42
|
+
spec.responses[code] = {
|
|
43
|
+
content: { [schema.type === "string" ? "text/plain" : "application/json"]: { schema } },
|
|
44
|
+
description: "",
|
|
45
|
+
};
|
|
46
|
+
return ext;
|
|
47
|
+
};
|
|
48
|
+
const query = (strArr) => {
|
|
49
|
+
if (!_.isArray(strArr)) strArr = strArr.split(",");
|
|
50
|
+
strArr.forEach((str) => {
|
|
51
|
+
spec.parameters.push(toParameter('query',str));
|
|
52
|
+
});
|
|
53
|
+
return ext;
|
|
54
|
+
};
|
|
55
|
+
const header = (strArr) => {
|
|
56
|
+
if (!_.isArray(strArr)) strArr = strArr.split(",");
|
|
57
|
+
strArr.forEach((str) => {
|
|
58
|
+
spec.parameters.push(toParameter('header',str));
|
|
59
|
+
});
|
|
60
|
+
return ext;
|
|
61
|
+
};
|
|
62
|
+
const tag = (str) => { spec.tags = [str]; return ext; };
|
|
63
|
+
const summary = (str) => { spec.summary = str; return ext; };
|
|
64
|
+
const desc = (str) => { spec.description = str; return ext; };
|
|
65
|
+
const deprecate = () => { spec.deprecated = true; return ext; };
|
|
66
|
+
|
|
67
|
+
Object.assign(ext, { req, res, query, header, tag, summary, desc, deprecate });
|
|
68
|
+
|
|
69
|
+
// support all ext in parameters
|
|
70
|
+
Object.keys(ext).forEach(k => {
|
|
71
|
+
if (opt[k]) ext[k](opt[k]);
|
|
72
|
+
});
|
|
73
|
+
Object.keys(opt).filter(k => k.match(/^[0-9]+$/)).forEach(k => ext.res(k, opt[k]));
|
|
74
|
+
|
|
75
|
+
return ext;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function get(path = "", opt = {}) {
|
|
79
|
+
opt.method = "get";
|
|
80
|
+
opt.path = path;
|
|
81
|
+
return api(opt);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function post(path = "", opt = {}) {
|
|
85
|
+
opt.method = "post";
|
|
86
|
+
opt.path = path;
|
|
87
|
+
return api(opt);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function put(path = "", opt = {}) {
|
|
91
|
+
opt.method = "put";
|
|
92
|
+
opt.path = path;
|
|
93
|
+
return api(opt);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function patch(path = "", opt = {}) {
|
|
97
|
+
opt.method = "patch";
|
|
98
|
+
opt.path = path;
|
|
99
|
+
return api(opt);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function del(path = "", opt = {}) {
|
|
103
|
+
opt.method = "delete";
|
|
104
|
+
opt.path = path;
|
|
105
|
+
return api(opt);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function _ref({ type, name, def }) {
|
|
109
|
+
components[type] = components[type] || {};
|
|
110
|
+
components[type][name] = def;
|
|
111
|
+
return `#/components/${type}/${name}`;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const ref = {
|
|
115
|
+
schema(name, flatSch) {
|
|
116
|
+
return _ref({ type: "schemas", name, def: toSwaggerSchema(flatSch) });
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function tag(name, description) {
|
|
121
|
+
tags.push({ name, description });
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function swaggerDoc(title = "API Docs") {
|
|
125
|
+
return {
|
|
126
|
+
info: { title, version: "1.0.0" },
|
|
127
|
+
openapi: "3.0.0",
|
|
128
|
+
servers,
|
|
129
|
+
tags,
|
|
130
|
+
paths,
|
|
131
|
+
components,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
module.exports = {
|
|
136
|
+
tag,
|
|
137
|
+
server,
|
|
138
|
+
ref,
|
|
139
|
+
get,
|
|
140
|
+
post,
|
|
141
|
+
put,
|
|
142
|
+
patch,
|
|
143
|
+
del,
|
|
144
|
+
swaggerDoc,
|
|
145
|
+
reset,
|
|
146
|
+
};
|
package/src/utils.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Author: Yusuf Bhabhrawala
|
|
3
|
+
*/
|
|
4
|
+
const _ = require("lodash");
|
|
5
|
+
|
|
6
|
+
const {typeShape} = require("@sleeksky/alt-schema");
|
|
7
|
+
|
|
8
|
+
const TYPES = {i: "integer", s: "string", b: "boolean", n: "number", o: "object", a: "array"};
|
|
9
|
+
const RX_OBJ = /(\{[^\{\}\[\]]+\})/;
|
|
10
|
+
const RX_ARR = /(\[[^\{\}\[\]]+\])/;
|
|
11
|
+
const RX_NESTED = /^\:?(\$[0-9]+)$/;
|
|
12
|
+
const RX_FLAT_ARR = /^\[([^\{\}\[\]]+)\]$/;
|
|
13
|
+
const RX_FLAT_OBJ = /^\{([^\{\}\[\]]+)\}$/;
|
|
14
|
+
|
|
15
|
+
function toSwaggerSchema(str) {
|
|
16
|
+
|
|
17
|
+
function traverse(schema, obj) {
|
|
18
|
+
if (_.isArray(obj)) {
|
|
19
|
+
schema.type = "array";
|
|
20
|
+
schema.items = {};
|
|
21
|
+
traverse(schema.items, obj[0]);
|
|
22
|
+
} else if (_.isObject(obj)) {
|
|
23
|
+
schema.type = "object";
|
|
24
|
+
schema.properties = {};
|
|
25
|
+
_.forOwn(obj, (v, k) => {
|
|
26
|
+
schema.properties[k] = {};
|
|
27
|
+
traverse(schema.properties[k], v);
|
|
28
|
+
});
|
|
29
|
+
} else {
|
|
30
|
+
let [optional, type, def] = obj.split(":");
|
|
31
|
+
optional = (optional === '?') ? true : false;
|
|
32
|
+
if (['string','number','boolean','integer','array','object'].indexOf(type) < 0) type = "string";
|
|
33
|
+
schema.type = type;
|
|
34
|
+
if (def !== '') {
|
|
35
|
+
if (type === 'number' || type === 'integer') def = def*1;
|
|
36
|
+
if (type === 'boolean') def = ['true','1'].indexOf(def) > -1 ? true : false;
|
|
37
|
+
schema.default = def;
|
|
38
|
+
} else if (optional) {
|
|
39
|
+
schema.default = null;
|
|
40
|
+
}
|
|
41
|
+
if (!optional) schema.required = true;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
let obj = typeShape(str);
|
|
47
|
+
let schema = {};
|
|
48
|
+
traverse(schema, obj);
|
|
49
|
+
return schema;
|
|
50
|
+
} catch (error) {
|
|
51
|
+
throw new Error(`Malformed schema: ${str}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function pathParameters(str) {
|
|
56
|
+
let params = [];
|
|
57
|
+
const RX_BRACE = /\{([^\}]+)\}/g;
|
|
58
|
+
const RX_COLON = /\:([^\/]+)/g;
|
|
59
|
+
let matches = str.match(RX_BRACE);
|
|
60
|
+
if (!matches) matches = str.match(RX_COLON);
|
|
61
|
+
if (matches) {
|
|
62
|
+
params = matches.map(m => {
|
|
63
|
+
let p = m.replace(/[\{\}\:]/g, "");
|
|
64
|
+
return { in: "path", name: p, schema: { type: "string" }, required: true };
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return params;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// header:?token
|
|
71
|
+
function toParameter(inType, str) {
|
|
72
|
+
if (!_.isString(str)) return str;
|
|
73
|
+
str = str.replace(/ /g, "");
|
|
74
|
+
let [name, type, def] = str.split(":");
|
|
75
|
+
let required = true;
|
|
76
|
+
if (type && type.match(/^\?/)) {
|
|
77
|
+
type = type.replace(/^\?/, "");
|
|
78
|
+
required = false;
|
|
79
|
+
}
|
|
80
|
+
if (type && TYPES[type]) type = TYPES[type];
|
|
81
|
+
else type = "string";
|
|
82
|
+
let schema = { type };
|
|
83
|
+
if (def) schema.default = def;
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
name,
|
|
87
|
+
in: inType,
|
|
88
|
+
required,
|
|
89
|
+
schema
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
module.exports = {toSwaggerSchema, pathParameters, toParameter};
|