-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
194 lines (183 loc) · 5.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
module.exports = function(){
var me = this
var clone = function(d){
return JSON.parse( JSON.stringify(d) )
}
this.addGet = function(resourcename,method,swagger,endpoint,resourceConfig){
if( method == "get" ){
if( !endpoint.responses[200] ){
endpoint.responses[200] = {
"schema":{
type: "object",
properties: clone(resourceConfig.properties)
}
}
}
if( !endpoint.parameters ){
endpoint.parameters = [
{
"in": "path",
"name": "id",
"description": resourcename+" id (hash-string)",
"required": true,
"type": "string"
}
]
}
if( !swagger.paths["/"+resourcename+"/{id}"] ) swagger.paths["/"+resourcename+"/{id}"] = {}
if( !endpoint.description ) endpoint.description = "retrieve a "+resourcename+" item with given id"
swagger.paths["/"+resourcename+"/{id}"][method] = endpoint
var endpoint2 = {
responses: {
200: {
"schema":{
type: "array",
items: [{
type:"object",
properties: clone(resourceConfig.properties)
}]
}
}
}
}
if( !swagger.paths["/"+resourcename] ) swagger.paths["/"+resourcename] = {}
endpoint2.description = "retrieve array of "+resourcename+" items"
swagger.paths["/"+resourcename][method] = endpoint2
}
}
this.addPost = function(resourcename,method,swagger,endpoint,resourceConfig){
if( method == "post" ){
if( !swagger.paths["/"+resourcename+"/{id}"] ) swagger.paths["/"+resourcename+"/{id}"] = {}
if( !endpoint.parameters ){
endpoint.parameters = [
{
"in": "body",
"name": "body",
"description": resourcename+" object that needs to be added",
"required": true,
"schema": {
type: "object",
properties: clone(resourceConfig.properties)
}
}
]
}
if( !endpoint.description ) endpoint.description = "create a "+resourcename+" item"
swagger.paths["/"+resourcename][method] = endpoint
}
}
this.addPut = function(resourcename,method,swagger,endpoint,resourceConfig){
if( method == "put" ){
if( !swagger.paths["/"+resourcename+"/{id}"] ) swagger.paths["/"+resourcename+"/{id}"] = {}
if( !endpoint.parameters ){
endpoint.parameters = [
{
"in": "path",
"name": "id",
"description": resourcename+" id (hash-string)",
"required": true,
"type": "string"
},
{
"in": "body",
"name": "payload",
"description": resourcename+" field(s) that needs to be updated",
"required": true,
"schema": {
type: "object",
properties: clone(resourceConfig.properties)
}
}
]
}
if( !endpoint.description ) endpoint.description = "update "+resourcename+" item-value(s) based on a given id"
swagger.paths["/"+resourcename+"/{id}"][method] = endpoint
}
}
this.addDelete = function(resourcename,method,swagger,endpoint,resourceConfig){
if( method == "delete" && !endpoint.parameters ){
if( !swagger.paths["/"+resourcename+"/{id}"] ) swagger.paths["/"+resourcename+"/{id}"] = {}
swagger.paths["/"+resourcename+"/{id}"][method] = endpoint
if( !endpoint.description ) endpoint.description = "deletes an "+resourcename+" item based on a given id"
}
}
this.addUserEndpoints = function (resourcename, swagger, resourceConfig ){
swagger.paths["/"+resourcename+"/login"] = {
"post":{
"description":"log user in and receive user object with sessionid (or error)",
"responses": {
},
"parameters": [
{
"in": "body",
"name": "payload",
"description": "object with credentials",
"required": true,
"schema":{
"type": "object",
"properties":{
"username":{
type:"string",
default: "john",
required:true
},
"password":{
type:"string",
default: "foobar",
required:true
}
}
}
}
]
}
}
swagger.paths["/"+resourcename+"/logout"] = {
"post":{
"description":"log user out and end session",
"responses": {
},
"parameters": []
}
}
swagger.paths["/"+resourcename+"/me"] = {
"get":{
"description":"get current user",
"responses": {
},
"parameters": []
}
}
}
this.initResources = function(swagger, dpd){
for( var i in dpd ){
if( i == "swagger" ) continue
var resource = dpd[i]
var resourceConfig = resource.getResource().config
var sdef = resourceConfig.swagger ? resourceConfig.swagger : false
if( !sdef ){ // only auto-document Collections
var types = ["Collection","UserCollection"]
if( types.indexOf(resourceConfig.type) != -1){
sdef = {methods:{}}
var methods = ["get","post","put","delete"]
methods.map( function(method){
if( !sdef.methods[method] ) sdef.methods[method] = {}
})
for( var method in sdef.methods ){
var endpoint = sdef.methods[method]
if( endpoint.public != undefined && !endpoint.public ) continue
endpoint.responses = endpoint.responses || {}
me.addGet(i,method,swagger,endpoint,resourceConfig)
me.addPost(i,method,swagger,endpoint,resourceConfig)
me.addDelete(i,method,swagger,endpoint,resourceConfig)
me.addPut(i,method,swagger,endpoint,resourceConfig)
}
}
if( resourceConfig.type == "UserCollection" )
me.addUserEndpoints(i, swagger, resourceConfig )
}
}
}
return this
}
module.exports = module.exports()