@sleeksky/alt-swagger 2.0.0 → 2.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/README.md +4 -0
- package/package.json +1 -1
- package/src/index.js +2 -2
- package/src/utils.js +14 -6
package/README.md
CHANGED
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
require("./utils");
|
|
5
5
|
const _ = require("lodash");
|
|
6
|
-
const { toSwaggerSchema, pathParameters, toParameter} = require("./utils")
|
|
6
|
+
const { toSwaggerSchema, pathParameters, pathClean, toParameter} = require("./utils")
|
|
7
7
|
|
|
8
8
|
const paths = {};
|
|
9
9
|
const components = {};
|
|
@@ -26,7 +26,7 @@ function api(opt) {
|
|
|
26
26
|
const pathParams = pathParameters(opt.path);
|
|
27
27
|
if (pathParams.length > 0) spec.parameters = pathParams;
|
|
28
28
|
|
|
29
|
-
_.set(paths, `${opt.path}.${opt.method}`, spec);
|
|
29
|
+
_.set(paths, `${pathClean(opt.path)}.${opt.method}`, spec);
|
|
30
30
|
|
|
31
31
|
let ext = {};
|
|
32
32
|
|
package/src/utils.js
CHANGED
|
@@ -12,6 +12,10 @@ const RX_NESTED = /^\:?(\$[0-9]+)$/;
|
|
|
12
12
|
const RX_FLAT_ARR = /^\[([^\{\}\[\]]+)\]$/;
|
|
13
13
|
const RX_FLAT_OBJ = /^\{([^\{\}\[\]]+)\}$/;
|
|
14
14
|
|
|
15
|
+
const RX_BRACE = /\{([^\}]+)\}/g;
|
|
16
|
+
const RX_COLON = /\:([^\/]+)/g;
|
|
17
|
+
|
|
18
|
+
|
|
15
19
|
function toSwaggerSchema(str) {
|
|
16
20
|
|
|
17
21
|
function traverse(schema, obj) {
|
|
@@ -54,19 +58,23 @@ function toSwaggerSchema(str) {
|
|
|
54
58
|
|
|
55
59
|
function pathParameters(str) {
|
|
56
60
|
let params = [];
|
|
57
|
-
const RX_BRACE = /\{([^\}]+)\}/g;
|
|
58
|
-
const RX_COLON = /\:([^\/]+)/g;
|
|
59
61
|
let matches = str.match(RX_BRACE);
|
|
60
62
|
if (!matches) matches = str.match(RX_COLON);
|
|
61
63
|
if (matches) {
|
|
62
64
|
params = matches.map(m => {
|
|
63
|
-
let p = m.replace(
|
|
64
|
-
|
|
65
|
+
let [p, def] = m.replace(/^[\{\:]/,"").replace(/[\}]$/,"").split(":");
|
|
66
|
+
if (!def) def = "";
|
|
67
|
+
return { in: "path", name: p, schema: { type: "string", example: def }, required: true };
|
|
65
68
|
});
|
|
66
69
|
}
|
|
67
70
|
return params;
|
|
68
71
|
}
|
|
69
72
|
|
|
73
|
+
function pathClean(path) {
|
|
74
|
+
if (path.match(RX_BRACE)) return path.replace(RX_BRACE, m => `${m.split(":")[0]}}`)
|
|
75
|
+
return path;
|
|
76
|
+
}
|
|
77
|
+
|
|
70
78
|
// header:?token
|
|
71
79
|
function toParameter(inType, str) {
|
|
72
80
|
if (!_.isString(str)) return str;
|
|
@@ -80,7 +88,7 @@ function toParameter(inType, str) {
|
|
|
80
88
|
if (type && TYPES[type]) type = TYPES[type];
|
|
81
89
|
else type = "string";
|
|
82
90
|
let schema = { type };
|
|
83
|
-
if (def) schema.
|
|
91
|
+
if (def) schema.example = def;
|
|
84
92
|
|
|
85
93
|
return {
|
|
86
94
|
name,
|
|
@@ -90,4 +98,4 @@ function toParameter(inType, str) {
|
|
|
90
98
|
}
|
|
91
99
|
}
|
|
92
100
|
|
|
93
|
-
module.exports = {toSwaggerSchema, pathParameters, toParameter};
|
|
101
|
+
module.exports = {toSwaggerSchema, pathParameters, pathClean, toParameter};
|