Skip to content

Commit a19fa03

Browse files
author
Callan Peter Milne
committed
Fooo
1 parent 8e2c182 commit a19fa03

File tree

4 files changed

+277
-13
lines changed

4 files changed

+277
-13
lines changed

src/Thing.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ module.exports = class Thing {
9292
routes (prefix) {
9393

9494
let typeName = this.name;
95+
let pluralUriName = this.pluralUriName;
9596
let r = [];
9697

9798
let thisRouteURI;
@@ -132,6 +133,7 @@ module.exports = class Thing {
132133
uri: uri,
133134
operationId: operationId,
134135
modelType: typeName,
136+
pluralUriName: pluralUriName,
135137
});
136138
}
137139

src/allTypes.js

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
/**
2+
* Copyright (c) 2017 Callan Peter Milne
3+
*
4+
* Permission to use, copy, modify, and/or distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9+
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10+
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11+
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12+
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
13+
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14+
* PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
'use strict';
17+
18+
module.exports = function () {
19+
return {
20+
"NewEntity": {
21+
"type": "object",
22+
"additionalProperties": false,
23+
"required": ["TypeID", "Attributes"],
24+
"properties": {
25+
"TypeID": {
26+
"type": "number",
27+
"example": 1420117
28+
},
29+
"Attributes": {
30+
"type": "array",
31+
"items": {
32+
"$ref": "#/definitions/NewEntityAttribute"
33+
}
34+
}
35+
}
36+
},
37+
"Entity": {
38+
"type": "object",
39+
"additionalProperties": false,
40+
"required": ["ID"],
41+
"properties": {
42+
"ID": {
43+
"type": "number",
44+
"example": 1381007
45+
},
46+
"TypeID": {
47+
"type": "number",
48+
"example": 1420117
49+
},
50+
"UserID": {
51+
"type": "number",
52+
"example": 1775619
53+
},
54+
"Created": {
55+
"type": "string",
56+
"format": "date-time"
57+
},
58+
"Deleted": {
59+
"type": "string",
60+
"format": "date-time",
61+
"default": null
62+
},
63+
"Attributes": {
64+
"type": "array",
65+
"items": {
66+
"$ref": "#/definitions/EntityAttribute"
67+
}
68+
}
69+
}
70+
},
71+
"NewEntityType": {
72+
"type": "object",
73+
"additionalProperties": false,
74+
"required": ["ClassName", "Label"],
75+
"properties": {
76+
"ParentID": {
77+
"type": "number",
78+
"default": null
79+
},
80+
"ClassName": {
81+
"type": "string",
82+
"example": "DomainName"
83+
},
84+
"Slug": {
85+
"type": "string",
86+
"example": "domain-name"
87+
},
88+
"Label": {
89+
"type": "string",
90+
"example": "Domain Name"
91+
},
92+
"Attributes": {
93+
"type": "array",
94+
"items": {
95+
"$ref": "#/definitions/NewTypeAttribute"
96+
}
97+
}
98+
}
99+
},
100+
"EntityType": {
101+
"type": "object",
102+
"additionalProperties": false,
103+
"required": ["ID"],
104+
"properties": {
105+
"ID": {
106+
"type": "number",
107+
"example": 1381007
108+
},
109+
"UserID": {
110+
"type": "number",
111+
"example": 1775619
112+
},
113+
"ParentID": {
114+
"type": "number"
115+
},
116+
"ClassName": {
117+
"type": "string",
118+
"example": "DomainName"
119+
},
120+
"Slug": {
121+
"type": "string",
122+
"example": "domain-name"
123+
},
124+
"Label": {
125+
"type": "string",
126+
"example": "Domain Name"
127+
},
128+
"Created": {
129+
"type": "string",
130+
"format": "date-time"
131+
},
132+
"Modified": {
133+
"type": "string",
134+
"format": "date-time"
135+
},
136+
"Deleted": {
137+
"type": "string",
138+
"format": "date-time",
139+
"default": null
140+
},
141+
"Attributes": {
142+
"type": "array",
143+
"items": {
144+
"$ref": "#/definitions/TypeAttribute"
145+
}
146+
}
147+
}
148+
},
149+
"NewEntityAttribute": {
150+
"type": "object",
151+
"additionalProperties": false,
152+
"properties": {
153+
"AttributeKey": {
154+
"type": "string",
155+
"example": "FirstGivenName"
156+
},
157+
"AttributeValue": {
158+
"type": "string",
159+
"example": "Charlton"
160+
}
161+
}
162+
},
163+
"EntityAttribute": {
164+
"type": "object",
165+
"additionalProperties": false,
166+
"properties": {
167+
"EntityID": {
168+
"type": "number"
169+
},
170+
"AttributeKey": {
171+
"type": "string"
172+
},
173+
"AttributeValue": {
174+
"type": "string"
175+
},
176+
"Created": {
177+
"type": "string",
178+
"format": "date-time"
179+
},
180+
"Modified": {
181+
"type": "string",
182+
"format": "date-time"
183+
},
184+
"Deleted": {
185+
"type": "string",
186+
"format": "date-time",
187+
"default": null
188+
}
189+
}
190+
},
191+
"NewTypeAttribute": {
192+
"type": "object",
193+
"additionalProperties": false,
194+
"properties": {
195+
"AttributeKey": {
196+
"type": "string",
197+
"example": "FurColour"
198+
},
199+
"Label": {
200+
"type": "string",
201+
"example": "Fur Colour"
202+
},
203+
"Required": {
204+
"type": "boolean",
205+
"default": false
206+
},
207+
"DefaultValue": {
208+
"type": "string"
209+
}
210+
}
211+
},
212+
"TypeAttribute": {
213+
"type": "object",
214+
"additionalProperties": false,
215+
"properties": {
216+
"TypeID": {
217+
"type": "number"
218+
},
219+
"AttributeKey": {
220+
"type": "string",
221+
"example": "FurColour"
222+
},
223+
"Label": {
224+
"type": "string",
225+
"example": "Fur Colour"
226+
},
227+
"Required": {
228+
"type": "boolean"
229+
},
230+
"DefaultValue": {
231+
"type": "string"
232+
},
233+
"Created": {
234+
"type": "string",
235+
"format": "date-time"
236+
},
237+
"Modified": {
238+
"type": "string",
239+
"format": "date-time"
240+
},
241+
"Deleted": {
242+
"type": "string",
243+
"format": "date-time",
244+
"default": null
245+
}
246+
}
247+
}
248+
}
249+
};

src/swagger.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,22 @@ module.exports = function (thing) {
2626

2727
let spec = newSwaggerSpec();
2828

29-
initPaths(thing.routes, spec);
29+
defineRoutes(thing.routes, spec);
30+
defineTypes(thing.types, spec);
3031

3132
return spec;
3233

3334
};
3435

35-
function initPaths (routes, spec) {
36+
function defineTypes (types, spec) {
37+
console.log(types);
38+
Object.keys(types).forEach((typeName) => {
39+
let type = types[typeName];
40+
spec.definitions[typeName] = type;
41+
});
42+
}
43+
44+
function defineRoutes (routes, spec) {
3645
routes.forEach((route) => {
3746

3847
let method = route.method.toLowerCase();
@@ -75,6 +84,18 @@ function initPaths (routes, spec) {
7584

7685
}
7786

87+
if ('get' === method && route.uri.match(/List$/)) {
88+
89+
e.summary = `Retrieves a list of ${route.pluralUriName}`;
90+
91+
}
92+
93+
if ('get' === method && !route.uri.match(/List$/)) {
94+
95+
e.summary = `Retrieves an existing ${route.modelType}`;
96+
97+
}
98+
7899
if ('post' === method) {
79100

80101
e.summary = `Creates a new ${route.modelType}`;

src/thing.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
'use strict';
1717

1818
const Thing = require('./Thing');
19+
const allTypes = require('./allTypes');
1920

2021
module.exports = function () {
2122

2223
let things = {};
23-
let schemas = {};
24+
let types = allTypes();
2425

2526
let routes;
2627

@@ -32,21 +33,12 @@ module.exports = function () {
3233

3334
routes = things.User.routes();
3435

35-
Object.values(things).forEach((thing) => {
36-
schemas[thing.name] = {
37-
type: 'object',
38-
};
39-
schemas[`New${thing.name}`] = {
40-
type: 'object',
41-
};
42-
});
43-
4436
process.stdout.write(JSON.stringify(routes, undefined, ' '));
4537
process.stdout.write(''+routes.length);
4638

4739
return {
4840
routes: routes,
49-
schemas: schemas,
41+
types: types,
5042
};
5143

5244
function requireThing (name) {

0 commit comments

Comments
 (0)