1 | 'use strict'
|
2 |
|
3 | const path = require('path')
|
4 | const fileUtil = require('./fileUtil')
|
5 | const fileHandlers = require('./fileHandlers')
|
6 | const fileAuthorizers = require('./fileAuthorizers')
|
7 | const fileSpecs = require('./fileSpecs')
|
8 |
|
9 | const DEFAULT_EXPRESS_OPTIONS = {
|
10 | 'case sensitive routing': false,
|
11 | 'jsonp callback name': null,
|
12 | 'trust proxy': false,
|
13 | 'views': null,
|
14 | 'view cache': false,
|
15 | 'x-powered-by': false
|
16 | }
|
17 |
|
18 | const DEFAULT_OPTIONS = {
|
19 | docsPath: '/api-docs',
|
20 | docsMiddleware: function docsMiddleware(req, res, next) { next() },
|
21 | handlers: {
|
22 | path: './handlers',
|
23 | template: path.join(__dirname, '..', 'template', 'handler.mustache'),
|
24 | generate: true
|
25 | },
|
26 | authorizers: {
|
27 | path: './security',
|
28 | template: path.join(__dirname, '..', 'template', 'authorizer.mustache'),
|
29 | generate: true
|
30 | },
|
31 | unusedFilePrefix: '_'
|
32 | }
|
33 |
|
34 | const DEFAULT_SPEC_OPTIONS = {
|
35 | specs: {
|
36 | path: './tests/api',
|
37 | template: path.join(__dirname, '..', 'template', 'spec.mustache'),
|
38 | generate: true,
|
39 | startServer: done => done(),
|
40 | stopServer: done => done()
|
41 | },
|
42 | fileType: '.spec.yml',
|
43 | unusedFilePrefix: '_'
|
44 | }
|
45 |
|
46 | exports.DEFAULT_OPTIONS = DEFAULT_OPTIONS
|
47 | exports.DEFAULT_EXPRESS_OPTIONS = DEFAULT_EXPRESS_OPTIONS
|
48 | exports.applyDefaultOptions = applyDefaultOptions
|
49 | exports.applyDefaultSpecOptions = applyDefaultSpecOptions
|
50 | exports.applyDefaultAppOptions = applyDefaultAppOptions
|
51 |
|
52 | function applyDefaultOptions(options, extra) {
|
53 | const handlers = expandFlattenedOption('handlers', options)
|
54 | const authorizers = expandFlattenedOption('authorizers', options)
|
55 |
|
56 | options = Object.assign({}, DEFAULT_OPTIONS, options, extra)
|
57 | options.handlers = Object.assign({}, DEFAULT_OPTIONS.handlers, handlers)
|
58 | options.authorizers = Object.assign({}, DEFAULT_OPTIONS.authorizers, authorizers)
|
59 | options = applyTemplateOptions('handlers', fileHandlers, options)
|
60 | options = applyTemplateOptions('authorizers', fileAuthorizers, options)
|
61 |
|
62 | return options
|
63 | }
|
64 |
|
65 | function expandFlattenedOption(type, options) {
|
66 | const flattened = options[type]
|
67 | if (typeof flattened === 'string') return { path: flattened }
|
68 | else if (typeof flattened === 'function') return { create: flattened }
|
69 | else return flattened
|
70 | }
|
71 |
|
72 | function applyTemplateOptions(type, fileModule, options) {
|
73 | if (options[type].generate) {
|
74 | options[type].template = fileUtil.getTemplate(type, options)
|
75 | if (!options[type].getTemplateView) {
|
76 | options[type].getTemplateView = fileModule.getTemplateView
|
77 | }
|
78 | }
|
79 | return options
|
80 | }
|
81 |
|
82 | function applyDefaultSpecOptions(options) {
|
83 | const specs = expandFlattenedOption('specs', options)
|
84 | options = Object.assign({}, DEFAULT_SPEC_OPTIONS, options)
|
85 | options.specs = Object.assign({}, DEFAULT_SPEC_OPTIONS.specs, specs)
|
86 | options = applyTemplateOptions('specs', fileSpecs, options)
|
87 | if (options.sortByStatus === undefined) options.sortByStatus = true
|
88 | return options
|
89 | }
|
90 |
|
91 | function applyDefaultAppOptions(app) {
|
92 | if (isExpress(app)) {
|
93 | applyDefaultExpressOptions(app)
|
94 | }
|
95 | return app
|
96 | }
|
97 |
|
98 | function isExpress(app) {
|
99 | return (!!app && !!app.set && !!app.render)
|
100 | }
|
101 |
|
102 | function applyDefaultExpressOptions(app) {
|
103 | Object.keys(DEFAULT_EXPRESS_OPTIONS)
|
104 | .forEach(name =>
|
105 | app.set(name, DEFAULT_EXPRESS_OPTIONS[name]))
|
106 | }
|