-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
71 lines (63 loc) · 2.05 KB
/
Copy pathindex.js
File metadata and controls
71 lines (63 loc) · 2.05 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
const swaggerUi = require('swagger-ui-express');
const composeMiddlewares = require('./src/compose-middlewares');
const {createGmailDomainRestrictionMiddleware, createApiDocsAutorizationMiddleware} = require('./src/gmail-domain-restriction-middleware');
module.exports = class InternalApiDocs {
constructor({ info, googleOauthConfig }) {
this.swaggerDocument = {
swagger: '2.0',
info,
paths: {},
definitions: {}
};
this._googleOauthConfig = googleOauthConfig;
}
apiDocsAuthorizationMiddleware() {
return createApiDocsAutorizationMiddleware(this._googleOauthConfig)
}
expressHandler() {
let handler;
return (req, res, next) => {
if (!handler) {
// Lazy-setup the swagger-UI at the last moment, in case
// some api docs are added *after* the express handler is registered.
handler = composeMiddlewares(
createGmailDomainRestrictionMiddleware(this._googleOauthConfig),
swaggerUi.serve,
swaggerUi.setup(this.swaggerDocument)
)
}
handler(req, res, next)
}
}
_addPathDocumentation(httpVerb, path, document) {
if (!this.swaggerDocument.paths[path]) {
this.swaggerDocument.paths[path] = {};
}
if (this.swaggerDocument.paths[path][httpVerb]) {
throw new Error(`${httpVerb} ${path} is declared twice in InternalApiDocs`);
}
this.swaggerDocument.paths[path][httpVerb] = document;
}
get(...params) {
this._addPathDocumentation('get', ...params);
}
post(...params) {
this._addPathDocumentation('post', ...params);
}
patch(...params) {
this._addPathDocumentation('patch', ...params);
}
put(...params) {
this._addPathDocumentation('put', ...params);
}
delete(...params) {
this._addPathDocumentation('delete', ...params);
}
definition(name, document) {
if (this.swaggerDocument.definitions[name]) {
throw new Error(`${httpVerb} ${path} is declared twice in InternalApiDocs`);
}
this.swaggerDocument.definitions[name] = document
return `#/definitions/${name}`
}
};