@sleeksky/alt-swagger 2.1.0 → 2.2.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sleeksky/alt-swagger",
3
- "version": "2.1.0",
3
+ "version": "2.2.1",
4
4
  "description": "Quickly spec your APIs for Swagger / OpenAPI interface",
5
5
  "main": "./src/index.js",
6
6
  "scripts": {
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
 
@@ -121,9 +121,9 @@ function tag(name, description) {
121
121
  tags.push({ name, description });
122
122
  }
123
123
 
124
- function swaggerDoc(title = "API Docs") {
124
+ function swaggerDoc(title) {
125
125
  return {
126
- info: { title, version: "1.0.0" },
126
+ info: { title: title || "", version: "1.0.0" },
127
127
  openapi: "3.0.0",
128
128
  servers,
129
129
  tags,
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) {
@@ -34,9 +38,9 @@ function toSwaggerSchema(str) {
34
38
  if (def !== '') {
35
39
  if (type === 'number' || type === 'integer') def = def*1;
36
40
  if (type === 'boolean') def = ['true','1'].indexOf(def) > -1 ? true : false;
37
- schema.default = def;
41
+ schema.example = def;
38
42
  } else if (optional) {
39
- schema.default = null;
43
+ // schema.default = null;
40
44
  }
41
45
  if (!optional) schema.required = true;
42
46
  }
@@ -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(/[\{\}\:]/g, "");
64
- return { in: "path", name: p, schema: { type: "string" }, required: true };
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;
@@ -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};