mockapi-msi 1.1.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.mockapi-config +4 -3
- package/LICENSE +165 -201
- package/README.md +11 -0
- package/main.js +18 -85
- package/modules/constants.js +1 -1
- package/modules/core.js +111 -0
- package/modules/readers.js +7 -2
- package/package.json +1 -1
package/.mockapi-config
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
# basic Mock API configuration
|
|
3
3
|
port: 8001
|
|
4
4
|
enableCors: true
|
|
5
|
-
#externalModulesPath: "
|
|
5
|
+
#externalModulesPath: "apiHandlers/"
|
|
6
6
|
|
|
7
7
|
# dummy example configurations
|
|
8
8
|
# PATH must point to a existing CSV file
|
|
@@ -22,9 +22,9 @@ data:
|
|
|
22
22
|
#0...n starting index (always reset to 0 when reach EOR)
|
|
23
23
|
|
|
24
24
|
# custom http response handlers section
|
|
25
|
+
# use: handler unique name : file name within the previously declared folder
|
|
25
26
|
#customHandlers:
|
|
26
|
-
# "custom":
|
|
27
|
-
# "myCustomHandler"
|
|
27
|
+
# "custom": "myCustomHandler"
|
|
28
28
|
|
|
29
29
|
# Mock API available endpoints, return types and
|
|
30
30
|
# general configuration
|
|
@@ -39,6 +39,7 @@ endpoints:
|
|
|
39
39
|
data: myRows
|
|
40
40
|
responseStatus: 200
|
|
41
41
|
responseContentType: "application/json"
|
|
42
|
+
#handler: "custom"
|
|
42
43
|
|
|
43
44
|
log: verbose
|
|
44
45
|
#debug
|
package/LICENSE
CHANGED
|
@@ -1,201 +1,165 @@
|
|
|
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
|
-
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
-
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
-
or other liability obligations and/or rights consistent with this
|
|
169
|
-
License. However, in accepting such obligations, You may act only
|
|
170
|
-
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
-
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
-
defend, and hold each Contributor harmless for any liability
|
|
173
|
-
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
-
of your accepting any such warranty or additional liability.
|
|
175
|
-
|
|
176
|
-
END OF TERMS AND CONDITIONS
|
|
177
|
-
|
|
178
|
-
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
-
|
|
180
|
-
To apply the Apache License to your work, attach the following
|
|
181
|
-
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
-
replaced with your own identifying information. (Don't include
|
|
183
|
-
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
-
comment syntax for the file format. We also recommend that a
|
|
185
|
-
file or class name and description of purpose be included on the
|
|
186
|
-
same "printed page" as the copyright notice for easier
|
|
187
|
-
identification within third-party archives.
|
|
188
|
-
|
|
189
|
-
Copyright [yyyy] [name of copyright owner]
|
|
190
|
-
|
|
191
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
-
you may not use this file except in compliance with the License.
|
|
193
|
-
You may obtain a copy of the License at
|
|
194
|
-
|
|
195
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
-
|
|
197
|
-
Unless required by applicable law or agreed to in writing, software
|
|
198
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
-
See the License for the specific language governing permissions and
|
|
201
|
-
limitations under the License.
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
package/README.md
CHANGED
|
@@ -6,6 +6,17 @@ MockAPI let you create fake responses with pre defined and dynamic data for defi
|
|
|
6
6
|
|
|
7
7
|
MockAPI also is intended to help you when, during testing phase, you cannot afford complex and expensive products (And you do not need them) that requires bulky configuration steps or depends directly on third party providers that you cannot control.
|
|
8
8
|
|
|
9
|
+
## Version 2.0.1 notes
|
|
10
|
+
|
|
11
|
+
- A bug related to custom handlers was detected and fixed.
|
|
12
|
+
|
|
13
|
+
## Version 2.0.0 notes
|
|
14
|
+
|
|
15
|
+
- Using NodeJS managers such as NVM causes configuration file not being picked from the execution/working folder.
|
|
16
|
+
- New CORE class created and code moved from the main module.
|
|
17
|
+
- Additional checking for the configuration file.
|
|
18
|
+
- Folder file readear incorrect path contactenation fixed.
|
|
19
|
+
|
|
9
20
|
## Configuration file
|
|
10
21
|
|
|
11
22
|
MockAPI can be used as it is. Without any additional coding activity. Just configure your endpoints and run ```main.js``` file.
|
package/main.js
CHANGED
|
@@ -2,119 +2,52 @@
|
|
|
2
2
|
|
|
3
3
|
'use strict';
|
|
4
4
|
|
|
5
|
-
const http = require('http');
|
|
6
5
|
const LOG = require('./modules/log');
|
|
7
6
|
const YAML = require('yaml');
|
|
8
7
|
const constants = require('./modules/constants');
|
|
9
8
|
const readers = require('./modules/readers');
|
|
10
|
-
const parser = require('./modules/urlParser');
|
|
11
|
-
const handlerInjector = require('./modules/configurationParser');
|
|
12
9
|
const ModuleProxy = require('./modules/moduleProxy');
|
|
13
10
|
const CLI = require('./modules/cli');
|
|
14
|
-
const
|
|
11
|
+
const CORE = require('./modules/core');
|
|
15
12
|
|
|
16
|
-
const
|
|
13
|
+
const rootPath = process.cwd();
|
|
14
|
+
const configFilePath = `${rootPath}/${constants.CONFIG_FILE_NAME}`;
|
|
15
|
+
|
|
16
|
+
const cli = new CLI(configFilePath);
|
|
17
17
|
|
|
18
18
|
if (cli.hasCommands()) {
|
|
19
19
|
cli.executeCommandLine();
|
|
20
20
|
return false;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
if (!readers.file_exists(configFilePath)) {
|
|
24
|
+
console.log(`Configuration file not found. Please run ${constants.COLOR.fgGreen}--init${constants.COLOR.reset} using the CLI.`);
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const configFile = readers.text_reader(configFilePath);
|
|
24
29
|
const parsedConfiguration = YAML.parse(configFile());
|
|
25
30
|
|
|
26
31
|
if (parsedConfiguration.port === undefined) throw new Error("port property is required");
|
|
27
32
|
|
|
28
|
-
const port = parsedConfiguration.port;
|
|
29
|
-
const enableCors = parsedConfiguration.enableCors;
|
|
30
|
-
const endpointList = parsedConfiguration.endpoints;
|
|
31
33
|
const logLevel = parsedConfiguration.log || constants.LOG_LEVELS.ALL;
|
|
32
34
|
const log = new LOG(logLevel);
|
|
33
|
-
const moduleProxy = new ModuleProxy(parsedConfiguration.externalModulesPath || constants.EXTERNAL_MODULES_PATH
|
|
35
|
+
const moduleProxy = new ModuleProxy(`file://${rootPath}/${(parsedConfiguration.externalModulesPath || constants.EXTERNAL_MODULES_PATH)}`, log);
|
|
34
36
|
|
|
35
37
|
if (parsedConfiguration.customHandlers !== undefined) {
|
|
36
38
|
moduleProxy.load(parsedConfiguration.customHandlers);
|
|
37
39
|
}
|
|
38
40
|
|
|
39
|
-
let data = parsedConfiguration.data || { };
|
|
40
|
-
handlerInjector.loadHandlersFromConfiguration(data);
|
|
41
|
-
|
|
42
41
|
log.message(``);
|
|
43
42
|
log.message(`Mock API configuration:`);
|
|
44
|
-
log.message(` PORT: ${constants.COLOR.fgGreen}${port}${constants.COLOR.reset}`);
|
|
45
|
-
log.message(` CORS enabled: ${enableCors ? constants.COLOR.fgGreen : constants.COLOR.fgRed}${enableCors}${constants.COLOR.reset}`);
|
|
43
|
+
log.message(` PORT: ${constants.COLOR.fgGreen}${parsedConfiguration.port}${constants.COLOR.reset}`);
|
|
44
|
+
log.message(` CORS enabled: ${parsedConfiguration.enableCors ? constants.COLOR.fgGreen : constants.COLOR.fgRed}${parsedConfiguration.enableCors}${constants.COLOR.reset}`);
|
|
46
45
|
log.message(``);
|
|
47
46
|
|
|
48
|
-
log.message(`> Mock API attempting to use port: ${constants.COLOR.fgRed}${port}${constants.COLOR.reset}`)
|
|
49
|
-
|
|
50
|
-
http.createServer((request, response) => {
|
|
51
|
-
let bodyPayload = [];
|
|
52
|
-
|
|
53
|
-
request.on('data', (chunk) => {
|
|
54
|
-
bodyPayload.push(chunk);
|
|
55
|
-
}).on('end', () => {
|
|
56
|
-
bodyPayload = Buffer.concat(bodyPayload).toString();
|
|
57
|
-
|
|
58
|
-
const urlInformation = parser.parse(request.url);
|
|
59
|
-
|
|
60
|
-
log.info(`Requesting: ${urlInformation.base} - Verb: ${request.method}`);
|
|
61
|
-
|
|
62
|
-
if (bodyPayload !== '') {
|
|
63
|
-
log.info(`Incoming body: ${bodyPayload}`);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
let actionFound = false;
|
|
67
|
-
let responseBody = null;
|
|
68
|
-
let responseStatus = null;
|
|
69
|
-
let contentType = null;
|
|
70
|
-
|
|
71
|
-
for (const endpointUrl in endpointList) {
|
|
72
|
-
if (Object.hasOwnProperty.call(endpointList, endpointUrl)) {
|
|
73
|
-
const endpoint = endpointList[endpointUrl];
|
|
74
|
-
const requestMethod = request.method.toLowerCase();
|
|
75
|
-
|
|
76
|
-
if (endpointUrl === urlInformation.base && (endpoint.verb === "any" || endpoint.verb.toLowerCase() === requestMethod)) {
|
|
77
|
-
|
|
78
|
-
if (endpoint.data !== undefined && data[endpoint.data] === undefined) {
|
|
79
|
-
log.error("No matching data variable for this request");
|
|
80
|
-
break;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
actionFound = true;
|
|
84
|
-
contentType = endpoint.responseContentType;
|
|
85
|
-
|
|
86
|
-
try {
|
|
87
|
-
const processData = endpoint.data === undefined ? "" : data[endpoint.data].dataHandler(urlInformation);
|
|
88
|
-
|
|
89
|
-
responseBody = endpoint.handler !== undefined ?
|
|
90
|
-
moduleProxy.execute(endpoint.handler, { method: requestMethod, url: endpointUrl, body: bodyPayload }, processData) :
|
|
91
|
-
processData;
|
|
92
|
-
|
|
93
|
-
responseStatus = endpoint.responseStatus;
|
|
94
|
-
} catch(ex) {
|
|
95
|
-
log.error(`${ex.message}`);
|
|
96
|
-
|
|
97
|
-
responseStatus = ex.httpStatusCode;
|
|
98
|
-
responseBody = ex.message;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
break;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
response.statusCode = responseStatus ||
|
|
107
|
-
(!actionFound ?
|
|
108
|
-
constants.HTTP_STATUS_CODES.NOT_FOUND :
|
|
109
|
-
constants.HTTP_STATUS_CODES.OK);
|
|
110
|
-
|
|
111
|
-
response.setHeader('Content-Type', contentType || constants.DEFAULT_CONTENT_TYPE);
|
|
112
|
-
|
|
113
|
-
enableCors && response.setHeader('Access-Control-Allow-Origin', '*');
|
|
47
|
+
log.message(`> Mock API attempting to use port: ${constants.COLOR.fgRed}${parsedConfiguration.port}${constants.COLOR.reset}`)
|
|
114
48
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}).listen(port);
|
|
49
|
+
const core = new CORE(log, parsedConfiguration, moduleProxy);
|
|
50
|
+
core.run();
|
|
118
51
|
|
|
119
|
-
log.message(`> Mock API listening on port: ${constants.COLOR.fgGreen}${port}${constants.COLOR.reset}`)
|
|
52
|
+
log.message(`> Mock API listening on port: ${constants.COLOR.fgGreen}${parsedConfiguration.port}${constants.COLOR.reset}`)
|
|
120
53
|
log.message(``);
|
package/modules/constants.js
CHANGED
package/modules/core.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
const parser = require('./urlParser');
|
|
2
|
+
const http = require('http');
|
|
3
|
+
const constants = require('./constants');
|
|
4
|
+
const handlerLoader = require('./configurationParser');
|
|
5
|
+
|
|
6
|
+
class Core {
|
|
7
|
+
|
|
8
|
+
_logger = null;
|
|
9
|
+
_port = 0;
|
|
10
|
+
_enableCors = false;
|
|
11
|
+
_endpointList = [];
|
|
12
|
+
_logLevel = ""
|
|
13
|
+
_modulesProxy = null;
|
|
14
|
+
_configurations = null;
|
|
15
|
+
_data = null;
|
|
16
|
+
|
|
17
|
+
constructor(logger, configurations, modulesProxy) {
|
|
18
|
+
this._configurations = configurations;
|
|
19
|
+
this._logger = logger;
|
|
20
|
+
this._port = configurations.port;
|
|
21
|
+
this._enableCors = configurations.enableCors;
|
|
22
|
+
this._endpointList = configurations.endpoints;
|
|
23
|
+
this._modulesProxy = modulesProxy;
|
|
24
|
+
|
|
25
|
+
this._data = configurations.data || { };
|
|
26
|
+
handlerLoader.loadHandlersFromConfiguration(this._data);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
run() {
|
|
30
|
+
const self = this;
|
|
31
|
+
|
|
32
|
+
http.createServer((request, response) => {
|
|
33
|
+
let bodyPayload = [];
|
|
34
|
+
|
|
35
|
+
request.on('data', (chunk) => {
|
|
36
|
+
bodyPayload.push(chunk);
|
|
37
|
+
}).on('end', () => {
|
|
38
|
+
bodyPayload = Buffer.concat(bodyPayload).toString();
|
|
39
|
+
|
|
40
|
+
const urlInformation = parser.parse(request.url);
|
|
41
|
+
|
|
42
|
+
self._logger.info(`Requesting: ${urlInformation.base} - Verb: ${request.method}`);
|
|
43
|
+
|
|
44
|
+
if (bodyPayload !== '') {
|
|
45
|
+
self._logger.info(`Incoming body: ${bodyPayload}`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let actionFound = false;
|
|
49
|
+
let responseBody = null;
|
|
50
|
+
let responseStatus = null;
|
|
51
|
+
let contentType = null;
|
|
52
|
+
|
|
53
|
+
for (const endpointUrl in self._endpointList) {
|
|
54
|
+
if (Object.hasOwnProperty.call(self._endpointList, endpointUrl)) {
|
|
55
|
+
const endpoint = self._endpointList[endpointUrl];
|
|
56
|
+
const requestMethod = request.method.toLowerCase();
|
|
57
|
+
|
|
58
|
+
if (
|
|
59
|
+
endpointUrl === urlInformation.base &&
|
|
60
|
+
(endpoint.verb === "any" || endpoint.verb.toLowerCase() === requestMethod)
|
|
61
|
+
) {
|
|
62
|
+
|
|
63
|
+
if (endpoint.data !== undefined && self._data[endpoint.data] === undefined) {
|
|
64
|
+
self._logger.error("No matching data variable for this request");
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
actionFound = true;
|
|
69
|
+
contentType = endpoint.responseContentType;
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
const processData = endpoint.data === undefined ?
|
|
73
|
+
"" :
|
|
74
|
+
self._data[endpoint.data].dataHandler(urlInformation);
|
|
75
|
+
|
|
76
|
+
responseBody = endpoint.handler !== undefined ?
|
|
77
|
+
self._modulesProxy.execute(endpoint.handler, {
|
|
78
|
+
method: requestMethod,
|
|
79
|
+
url: endpointUrl,
|
|
80
|
+
body: bodyPayload
|
|
81
|
+
}, processData) : processData;
|
|
82
|
+
|
|
83
|
+
responseStatus = endpoint.responseStatus;
|
|
84
|
+
} catch(ex) {
|
|
85
|
+
self._logger.error(`${ex.message}`);
|
|
86
|
+
|
|
87
|
+
responseStatus = ex.httpStatusCode;
|
|
88
|
+
responseBody = ex.message;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
response.statusCode = responseStatus ||
|
|
97
|
+
(!actionFound ?
|
|
98
|
+
constants.HTTP_STATUS_CODES.NOT_FOUND :
|
|
99
|
+
constants.HTTP_STATUS_CODES.OK);
|
|
100
|
+
|
|
101
|
+
response.setHeader('Content-Type', contentType || constants.DEFAULT_CONTENT_TYPE);
|
|
102
|
+
|
|
103
|
+
self._enableCors && response.setHeader('Access-Control-Allow-Origin', '*');
|
|
104
|
+
|
|
105
|
+
response.end(responseBody);
|
|
106
|
+
});
|
|
107
|
+
}).listen(self._port);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
module.exports = Core;
|
package/modules/readers.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
2
3
|
const constants = require("./constants");
|
|
3
4
|
const CSV = require('./csv');
|
|
4
5
|
const HttpException = require('./HttpException');
|
|
@@ -13,6 +14,8 @@ const text_reader = (file) => {
|
|
|
13
14
|
};
|
|
14
15
|
};
|
|
15
16
|
|
|
17
|
+
const file_exists = (file) => fs.existsSync(file);
|
|
18
|
+
|
|
16
19
|
const folder_reader = (folder) => {
|
|
17
20
|
return (urlInformation) => {
|
|
18
21
|
|
|
@@ -20,7 +23,8 @@ const folder_reader = (folder) => {
|
|
|
20
23
|
throw new HttpException(constants.HTTP_STATUS_CODES.NOT_ACCEPTABLE, "File not provided");
|
|
21
24
|
}
|
|
22
25
|
|
|
23
|
-
|
|
26
|
+
const file = path.join(folder, urlInformation.file);
|
|
27
|
+
return text_reader(file)()
|
|
24
28
|
};
|
|
25
29
|
};
|
|
26
30
|
|
|
@@ -34,5 +38,6 @@ const csv_reader = function (file, parameters) {
|
|
|
34
38
|
module.exports = {
|
|
35
39
|
text_reader: text_reader,
|
|
36
40
|
folder_reader: folder_reader,
|
|
37
|
-
csv_reader: csv_reader
|
|
41
|
+
csv_reader: csv_reader,
|
|
42
|
+
file_exists: file_exists
|
|
38
43
|
};
|