This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 604
Expand file tree
/
Copy pathapi-generator.coffee
More file actions
executable file
·360 lines (295 loc) · 9.76 KB
/
api-generator.coffee
File metadata and controls
executable file
·360 lines (295 loc) · 9.76 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/env coffee
# coffeelint: disable=cyclomatic_complexity
require 'coffee-cache'
fs = require 'fs'
path = require 'path'
bongo = require '../servers/lib/server/bongo'
docGen = require './docgen'
{ expect } = require 'chai'
swagger =
swagger: '2.0'
basePath: '/remote.api'
host: 'koding.com'
info:
title: 'Koding API'
version: '0.0.3'
description: 'Koding API for integrating your application with Koding services'
license:
name: 'Apache 2.0'
url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
securityDefinitions:
Bearer:
type: 'apiKey'
name: 'Authorization'
in: 'header'
tags: [
{
name: 'system'
description: 'System endpoints for various purposes'
}
]
definitions:
Error:
type: 'object'
properties:
message:
type: 'string'
description: 'Error description'
example: 'Something went wrong'
name:
type: 'string'
description: 'Name of the error'
example: 'KodingError'
DefaultSelector:
type: 'object'
DefaultResponse:
type: 'object'
properties:
ok:
type: 'boolean'
description: 'If the request processed by endpoint'
example: true
error:
type: 'object'
description: 'Error description'
example:
message: 'Something went wrong'
name: 'SomethingWentWrong'
data:
type: 'object'
description: 'Result of the operation'
example: 'Hello World'
UnauthorizedRequest:
type: 'object'
properties:
status:
type: 'integer'
description: 'HTTP Error Code'
example: 401
message:
type: 'string'
description: 'Error description'
example: 'The request is unauthorized, an api token is required.'
code:
type: 'string'
description: 'Error Code'
example: 'UnauthorizedRequest'
parameters:
instanceParam:
in: 'path'
name: 'id'
description: 'Mongo ID of target instance'
required: true
type: 'string'
bodyParam:
in: 'body'
name: 'body'
schema:
$ref: '#/definitions/DefaultSelector'
required: true
description: 'body of the request'
paths: {}
parseType = (type) ->
type = type.toLowerCase()
type = 'string' if type is 'objectid'
type = 'string' if type is 'date'
type = 'object' if type is 'meta'
return type
getProps = (prop, def, field) ->
try
prop.format = 'date' if prop.type is 'Date'
prop.type = parseType prop.type
prop.items.type = parseType prop.items.type if prop.items?.type
catch e
console.log 'Failed on field:', field
throw e
if prop.default? and prop.default?.type is 'default'
delete prop.default
if prop.required
def.required ?= []
def.required.push field
delete prop.required
return prop
generateDefinition = (model) ->
schema = model.describeSchema()
def = { type: 'object' }
props = { _id: { type: 'string' } }
for field, prop of schema
continue if field is 'default' and prop.type is 'default'
if 'type' not in Object.keys prop
props[field] = { properties: {} }
for subfield, subprop of prop
continue if subfield is 'default' and subprop.type is 'default'
props[field].properties[subfield] = getProps subprop, def, field
else
props[field] = getProps prop, def, field
def.properties = props
return def
generateMethodPaths = (model, definitions, paths, docs) ->
name = model.name
methods = model.getSharedMethods()
schema = if definitions[name]
then { allOf: [ { $ref: "#/definitions/#{name}" }, { $ref: '#/definitions/DefaultResponse' } ] }
else { $ref: '#/definitions/DefaultResponse' }
# currently swagger-codegen does not support multiple response types
# which causes to generate unexpected client api. So, for now we're
# only providing the DefaultResponse schema ~ GG
# ref: https://github.com/swagger-api/swagger-codegen/issues/358
schema = { $ref: '#/definitions/DefaultResponse' }
for method, signatures of methods.statik
response =
description : 'Request processed successfully'
schema :
$ref : '#/definitions/DefaultResponse'
if hasParams = signatures.length > 1 or signatures[0].split(',').length > 1
parameters = [{ $ref: '#/parameters/bodyParam' }]
examples = docs[name]['static'][method]?.examples ? []
if (returns = docs[name]['static'][method]?.returns) and Object.keys(returns).length
response =
description: returns.description ? 'Request processed successfully'
schema: { $ref: "#/definitions/#{returns.type}" }
for example in examples
if example.title is 'api'
parameters = [
{
in: 'body'
name: 'body'
schema: example.schema
required: true
description: 'body of the request'
}
]
else if example.title is 'return'
response.schema =
type: 'object'
properties:
ok:
type: 'boolean'
description: 'If the request processed by endpoint'
example: true
error:
type: 'object'
description: 'Error description'
example:
message: 'Something went wrong'
name: 'SomethingWentWrong'
data: example.schema
else
parameters = null
paths["/#{name}.#{method}"] =
post:
tags: [ name ]
consumes: [ 'application/json' ]
operationId: "#{method}"
parameters: parameters ? []
security: [ { Bearer: [] } ]
description: docs[name]['static'][method]?.description ? ''
responses:
'200':
description: response.description
schema: response.schema
'401':
description: 'Unauthorized request'
schema:
$ref: '#/definitions/UnauthorizedRequest'
for method, signatures of methods.instance
hasCustomParams = no
parameters = [{ $ref: '#/parameters/instanceParam' }]
response = { description: 'OK', schema }
if returns = docs[name]['instance'][method]?.returns
if Object.keys(returns).length and swagger.definitions[returns.type]
response =
description: returns.description
schema: { $ref: "#/definitions/#{returns.type}" }
if hasParams = signatures.length > 1 or signatures[0].split(',').length > 1
examples = docs[name]['instance'][method]?.examples ? []
for example in examples
if example.title is 'api'
parameters.push {
in: 'body'
name: 'body'
schema: example.schema
required: true
description: 'body of the request'
}
hasCustomParams = yes
else if example.title is 'return'
response.schema = example.schema
unless hasCustomParams
parameters.push { $ref: '#/parameters/bodyParam' }
paths["/#{name}.#{method}/{id}"] =
post:
tags: [ name ]
operationId: "#{method}"
security: [ { Bearer: [] } ]
consumes: [ 'application/json' ]
description: docs[name]['instance'][method]?.description ? ''
parameters: parameters
responses:
'200':
description: response.description
schema: response.schema
'401':
description: 'Unauthorized request'
schema:
$ref: '#/definitions/UnauthorizedRequest'
module.exports = generateApi = (callback) ->
bongo.on 'apiReady', ->
definitions = swagger.definitions
paths = swagger.paths
tags = swagger.tags
docs = docGen bongo.modelPaths[0], bongo.modelFiles
if docs.errors.length > 0
return callback
message: 'Docs has errors!'
details: { error: doc.errors }
else
docs = docs.doc
for name, model of bongo.models
swagger.tags.push {
description: docs[name].description
name
}
try
if model.schema? and model.describeSchema?
definitions[name] = generateDefinition model
catch error
schema = bongo.models[name].describeSchema()
return callback
message: 'Failed while building definitions!'
details: { name, error, schema }
try
generateMethodPaths model, definitions, paths, docs
catch error
methods = bongo.models[name].getSharedMethods()
return callback
message: 'Failed while building methods!'
details: { name, error, methods }
process.exit()
swagger.paths = paths
swagger.definitions = definitions
callback null, swagger
unless module.parent
swaggerFilePath = path.resolve path.join __dirname, '../website/swagger.json'
generateApi (err, swagger) ->
if err
console.error err
process.exit 1
swaggerInJson = JSON.stringify swagger, ' ', 2
if process.argv[2] is '--check'
try
oldDataInJson = (fs.readFileSync swaggerFilePath).toString()
oldData = JSON.parse oldDataInJson
expect(oldData).to.deep.equal JSON.parse swaggerInJson
console.log 'Swagger.json is up-to-date'
catch e
console.error '''
Swagger.json is outdated. Please run following commands to update it;
./run exec scripts/api-generator.coffee
and commit updated swagger.json file to current branch
'''
process.exit 1
else
fs.writeFileSync swaggerFilePath, swaggerInJson
console.log 'Swagger.json updated succesfully!'
process.exit()