1 | class OptionsResolver {
|
2 |
|
3 |
|
4 | constructor(options) {
|
5 | this.options = options;
|
6 | }
|
7 |
|
8 | get isPackage() {
|
9 | if (!this.options.hasOwnProperty('package')) {
|
10 | return false;
|
11 | }
|
12 | return this.options.package && (this.options.package === true || this.options.package === "true")
|
13 | }
|
14 |
|
15 | get isCorsEnabled() {
|
16 | if (!this.options.hasOwnProperty('cors')) {
|
17 |
|
18 | return false;
|
19 | }
|
20 | return this.options.cors && (this.options.cors === true || this.options.cors === "true")
|
21 | }
|
22 |
|
23 | get isAutoMockEnabled() {
|
24 | if (!this.options.hasOwnProperty('autoMock')) {
|
25 |
|
26 | return false;
|
27 | }
|
28 | return this.options.autoMock && (this.options.autoMock === true || this.options.autoMock === "true")
|
29 | }
|
30 |
|
31 | get isValidationEnabled() {
|
32 | if (!this.options.hasOwnProperty('validation')) {
|
33 |
|
34 | return false;
|
35 | }
|
36 | return this.options.validation && (this.options.validation === true || this.options.validation === "true")
|
37 | }
|
38 |
|
39 | get inputFile() {
|
40 | if (!this.options.inputFile) {
|
41 | throw new TypeError('Missing argument inputFile');
|
42 | }
|
43 |
|
44 | return this.options.inputFile;
|
45 | }
|
46 |
|
47 | get inputDirectory() {
|
48 | if (!this.options.inputDirectory) {
|
49 | return './';
|
50 | }
|
51 |
|
52 | return this.options.inputDirectory;
|
53 | }
|
54 |
|
55 | get outputFile() {
|
56 | if (!this.options.outputFile) {
|
57 | return 'api.yml';
|
58 | }
|
59 |
|
60 | return this.options.outputFile;
|
61 | }
|
62 |
|
63 | get outputDirectory() {
|
64 | if (!this.options.outputDirectory) {
|
65 | return 'openapi-integration/';
|
66 | }
|
67 |
|
68 | return this.options.outputDirectory.replace(/\/?$/, '/');
|
69 | }
|
70 |
|
71 | get apiResourceName() {
|
72 | return this.options.apiResourceName;
|
73 | }
|
74 |
|
75 | _mappingPath(stage) {
|
76 | let path = null;
|
77 | if (this.options.mapping) {
|
78 | this.options.mapping.forEach(function (mapping) {
|
79 | if (Array.isArray(mapping.stage)) {
|
80 |
|
81 | if (mapping.stage.includes(stage)) {
|
82 | path = mapping.path;
|
83 | }
|
84 | } else {
|
85 | if (mapping.stage === stage) {
|
86 | path = mapping.path;
|
87 | }
|
88 | }
|
89 | });
|
90 | }
|
91 |
|
92 | if (path === null && !this.options.autoMock) {
|
93 | throw new TypeError(`Missing integration mapping for the ${stage} stage. Either define a mapping file or enable the AUTO-MOCK option`);
|
94 | }
|
95 |
|
96 | return path;
|
97 | }
|
98 |
|
99 | _proxyManager(stage) {
|
100 | let proxy = null;
|
101 | let options = this.options;
|
102 |
|
103 | if (options.mapping) {
|
104 | options.mapping.forEach(function (mapping) {
|
105 | if (mapping.proxyManager) {
|
106 | if (Array.isArray(mapping.stage)) {
|
107 |
|
108 | if (mapping.stage.includes(stage) && mapping.hasOwnProperty('proxyManager')) {
|
109 | proxy = mapping.proxyManager;
|
110 | }
|
111 | } else {
|
112 | if (mapping.stage === stage) {
|
113 | proxy = mapping.proxyManager;
|
114 | }
|
115 | }
|
116 |
|
117 | if (proxy && proxy.type !== 'http_proxy') {
|
118 | throw new TypeError(`Currently only type http_proxy is allowed`);
|
119 | }
|
120 | }
|
121 | });
|
122 | }
|
123 | return proxy;
|
124 | }
|
125 |
|
126 | resolve(stage = "dev") {
|
127 | return {
|
128 | package: this.isPackage,
|
129 | cors: this.isCorsEnabled,
|
130 | autoMock: this.isAutoMockEnabled,
|
131 | validation: this.isValidationEnabled,
|
132 | inputFile: this.inputFile,
|
133 | inputDirectory: this.inputDirectory,
|
134 | inputFullPath: this.inputDirectory + this.inputFile,
|
135 | outputFile: this.outputFile,
|
136 | outputDirectory: this.outputDirectory,
|
137 | outputFullPath: this.outputDirectory + this.outputFile,
|
138 | integrationPath: this._mappingPath(stage),
|
139 | proxy: this._proxyManager(stage),
|
140 | apiResourceName: this.apiResourceName,
|
141 | }
|
142 | }
|
143 | }
|
144 |
|
145 | module.exports = OptionsResolver;
|