Skip to content

Commit 3349290

Browse files
author
Callan Peter Milne
committed
(fix):Split 'Thing' definitions into separate files
1 parent 593d9f7 commit 3349290

File tree

8 files changed

+172
-68
lines changed

8 files changed

+172
-68
lines changed

src/Thing.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
*/
1616
'use strict';
1717

18+
const HTTP_VERB_GET = 'GET';
19+
const HTTP_VERB_PUT = 'PUT';
20+
const HTTP_VERB_POST = 'POST';
21+
const HTTP_VERB_DELETE = 'DELETE';
22+
1823
module.exports = class Thing {
1924

2025
constructor (opt) {
@@ -95,24 +100,20 @@ module.exports = class Thing {
95100

96101
thisRouteURI = `${prefix}/${this.uriPrefix}{${this.idProperty}}`;
97102

98-
let canCreate = this.hasCreateRoute;
99-
if (canCreate) {
100-
route('POST', `${prefix}/${this.pluralUriName}`, `create${this.name}`);
101-
}
103+
this.hasCreateRoute &&
104+
route(HTTP_VERB_POST, `${prefix}/${this.pluralUriName}`, `create${this.name}`);
102105

103-
let canList = this.hasListRoute;
104-
if (canList) {
105-
route('GET', `${prefix}/${this.uriName}/List`, `get${this.name}List`);
106-
}
106+
this.hasListRoute &&
107+
route(HTTP_VERB_GET, `${prefix}/${this.uriName}/List`, `get${this.name}List`);
107108

108109
this.hasReadRoute &&
109-
route('GET', thisRouteURI, `get${this.name}By${this.idProperty}`);
110+
route(HTTP_VERB_GET, thisRouteURI, `get${this.name}By${this.idProperty}`);
110111

111112
this.hasUpdateRoute &&
112-
route('PUT', thisRouteURI, `update${this.name}By${this.idProperty}`);
113+
route(HTTP_VERB_PUT, thisRouteURI, `update${this.name}By${this.idProperty}`);
113114

114115
this.hasDeleteRoute &&
115-
route('DELETE', thisRouteURI, `delete${this.name}By${this.idProperty}`);
116+
route(HTTP_VERB_DELETE, thisRouteURI, `delete${this.name}By${this.idProperty}`);
116117

117118
this.additionalSubRoutes.forEach((route) => {
118119
route(route.method, `${thisRouteURI}/${route.uri}`, route.operationId);

src/swagger.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ module.exports = function (routes) {
8686

8787
e.responses['200'] = {
8888
description: 'Success',
89+
$ref: `#/definitions/${route.modelType}`,
8990
};
9091

9192
e.responses['404'] = {

src/thing.js

Lines changed: 11 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -21,69 +21,23 @@ module.exports = function () {
2121

2222
let things = {};
2323

24-
things.User = new Thing({
25-
name: 'User',
26-
uriPrefix: 'U/',
27-
idProperty: 'UserID',
28-
});
24+
let routes;
2925

30-
things.Type = new Thing({
31-
name: 'Type',
32-
uriPrefix: 'T/',
33-
idProperty: 'ClassName',
34-
parentThing: things.User,
35-
provide: {
36-
l: true,
37-
c: true,
38-
r: true,
39-
},
40-
});
26+
things.User = requireThing('User');
27+
things.Type = requireThing('Type');
28+
things.TypeAttribute = requireThing('TypeAttribute');
29+
things.Entity = requireThing('Entity');
30+
things.EntityAttribute = requireThing('EntityAttribute');
4131

42-
things.TypeAttribute = new Thing({
43-
name: 'TypeAttribute',
44-
uriName: 'Attribute',
45-
uriPrefix: 'A/',
46-
idProperty: 'AttributeKey',
47-
parentThing: things.Type,
48-
provide: {
49-
c: true,
50-
r: true,
51-
u: true,
52-
},
53-
});
54-
55-
things.Entity = new Thing({
56-
name: 'Entity',
57-
pluralUriName: 'Entities',
58-
uriPrefix: 'E/',
59-
idProperty: 'EntityID',
60-
parentThing: things.Type,
61-
provide: {
62-
l: true,
63-
c: true,
64-
r: true,
65-
d: true,
66-
},
67-
});
68-
69-
things.EntityAttribute = new Thing({
70-
name: 'EntityAttribute',
71-
uriName: 'Attribute',
72-
uriPrefix: 'A/',
73-
idProperty: 'AttributeKey',
74-
parentThing: things.Entity,
75-
provide: {
76-
r: true,
77-
u: true,
78-
d: true,
79-
},
80-
});
81-
82-
let routes = things.User.routes();
32+
routes = things.User.routes();
8333

8434
process.stdout.write(JSON.stringify(routes, undefined, ' '));
8535
process.stdout.write(''+routes.length);
8636

8737
return routes;
8838

39+
function requireThing (name) {
40+
return require(`./things/${name}`)(Thing, things);
41+
}
42+
8943
};

src/things/Entity.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 (Thing, things) {
19+
return things.Entity = new Thing({
20+
name: 'Entity',
21+
pluralUriName: 'Entities',
22+
uriPrefix: 'E/',
23+
idProperty: 'EntityID',
24+
parentThing: things.Type,
25+
provide: {
26+
l: true,
27+
c: true,
28+
r: true,
29+
d: true,
30+
},
31+
});
32+
};

src/things/EntityAttribute.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 (Thing, things) {
19+
return things.EntityAttribute = new Thing({
20+
name: 'EntityAttribute',
21+
uriName: 'Attribute',
22+
uriPrefix: 'A/',
23+
idProperty: 'AttributeKey',
24+
parentThing: things.Entity,
25+
provide: {
26+
r: true,
27+
u: true,
28+
d: true,
29+
},
30+
});
31+
};

src/things/Type.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 (Thing, things) {
19+
return things.Type = new Thing({
20+
name: 'Type',
21+
uriPrefix: 'T/',
22+
idProperty: 'ClassName',
23+
parentThing: things.User,
24+
provide: {
25+
l: true,
26+
c: true,
27+
r: true,
28+
},
29+
});
30+
};

src/things/TypeAttribute.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 (Thing, things) {
19+
return things.TypeAttribute = new Thing({
20+
name: 'TypeAttribute',
21+
uriName: 'Attribute',
22+
uriPrefix: 'A/',
23+
idProperty: 'AttributeKey',
24+
parentThing: things.Type,
25+
provide: {
26+
c: true,
27+
r: true,
28+
u: true,
29+
},
30+
});
31+
};

src/things/User.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 (Thing, things) {
19+
return things.User = new Thing({
20+
name: 'User',
21+
uriPrefix: 'U/',
22+
idProperty: 'UserID',
23+
});
24+
};

0 commit comments

Comments
 (0)