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 | handlers: {
|
21 | path: './handlers',
|
22 | template: path.join(__dirname, '..', 'template', 'handler.mustache'),
|
23 | generate: true
|
24 | },
|
25 | authorizers: {
|
26 | path: './security',
|
27 | template: path.join(__dirname, '..', 'template', 'authorizer.mustache'),
|
28 | generate: true
|
29 | },
|
30 | unusedFilePrefix: '_'
|
31 | }
|
32 |
|
33 | const DEFAULT_SPEC_OPTIONS = {
|
34 | specs: {
|
35 | path: './tests/api',
|
36 | template: path.join(__dirname, '..', 'template', 'spec.mustache'),
|
37 | generate: true,
|
38 | startServer: done => done(),
|
39 | stopServer: done => done()
|
40 | },
|
41 | fileType: '.spec.yml',
|
42 | unusedFilePrefix: '_'
|
43 | }
|
44 |
|
45 | exports.DEFAULT_OPTIONS = DEFAULT_OPTIONS
|
46 | exports.DEFAULT_EXPRESS_OPTIONS = DEFAULT_EXPRESS_OPTIONS
|
47 | exports.applyDefaultOptions = applyDefaultOptions
|
48 | exports.applyDefaultSpecOptions = applyDefaultSpecOptions
|
49 | exports.applyDefaultAppOptions = applyDefaultAppOptions
|
50 |
|
51 | function applyDefaultOptions(options, extra) {
|
52 | const handlers = expandFlattenedOption('handlers', options)
|
53 | const authorizers = expandFlattenedOption('authorizers', options)
|
54 |
|
55 | options = Object.assign({}, DEFAULT_OPTIONS, options, extra)
|
56 | options.handlers = Object.assign({}, DEFAULT_OPTIONS.handlers, handlers)
|
57 | options.authorizers = Object.assign({}, DEFAULT_OPTIONS.authorizers, authorizers)
|
58 | options = applyTemplateOptions('handlers', fileHandlers, options)
|
59 | options = applyTemplateOptions('authorizers', fileAuthorizers, options)
|
60 |
|
61 | return options
|
62 | }
|
63 |
|
64 | function expandFlattenedOption(type, options) {
|
65 | const flattened = options[type]
|
66 | if (typeof flattened === 'string') return { path: flattened }
|
67 | else if (typeof flattened === 'function') return { create: flattened }
|
68 | else return flattened
|
69 | }
|
70 |
|
71 | function applyTemplateOptions(type, fileModule, options) {
|
72 | if (options[type].generate) {
|
73 | options[type].template = fileUtil.getTemplate(type, options)
|
74 | if (!options[type].getTemplateView) {
|
75 | options[type].getTemplateView = fileModule.getTemplateView
|
76 | }
|
77 | }
|
78 | return options
|
79 | }
|
80 |
|
81 | function applyDefaultSpecOptions(options) {
|
82 | const specs = expandFlattenedOption('specs', options)
|
83 | options = Object.assign({}, DEFAULT_SPEC_OPTIONS, options)
|
84 | options.specs = Object.assign({}, DEFAULT_SPEC_OPTIONS.specs, specs)
|
85 | options = applyTemplateOptions('specs', fileSpecs, options)
|
86 | if (options.sortByStatus === undefined) options.sortByStatus = true
|
87 | return options
|
88 | }
|
89 |
|
90 | function applyDefaultAppOptions(app) {
|
91 | if (isExpress(app)) {
|
92 | applyDefaultExpressOptions(app)
|
93 | }
|
94 | return app
|
95 | }
|
96 |
|
97 | function isExpress(app) {
|
98 | return (!!app && !!app.set && !!app.render)
|
99 | }
|
100 |
|
101 | function applyDefaultExpressOptions(app) {
|
102 | Object.keys(DEFAULT_EXPRESS_OPTIONS)
|
103 | .forEach(name =>
|
104 | app.set(name, DEFAULT_EXPRESS_OPTIONS[name]))
|
105 | }
|