mockapi-msi 2.0.1 → 2.5.0
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/LICENSE +164 -164
- package/README.md +357 -120
- package/apiHandlers/myCustomHandler.js +26 -9
- package/main.js +87 -53
- package/modules/HttpException.js +8 -30
- package/modules/banner.js +22 -0
- package/modules/cli.js +110 -107
- package/modules/configWatcher.js +60 -0
- package/modules/configurationParser.js +43 -43
- package/modules/constants.js +95 -95
- package/modules/core.js +381 -111
- package/modules/csv.js +121 -77
- package/modules/log.js +42 -38
- package/modules/moduleProxy.js +51 -54
- package/modules/openApi.js +178 -0
- package/modules/readers.js +42 -42
- package/modules/urlParser.js +53 -22
- package/package.json +38 -20
- package/testdata/data.csv +5 -5
- package/.dockerignore +0 -24
- package/.mockapi-config +0 -48
- package/Dockerfile +0 -8
- package/docker-compose.debug.yml +0 -14
- package/docker-compose.yml +0 -12
package/modules/urlParser.js
CHANGED
|
@@ -1,23 +1,54 @@
|
|
|
1
|
-
const constants = require("./constants");
|
|
2
|
-
|
|
3
|
-
const parser = (url) => {
|
|
4
|
-
|
|
5
|
-
const parsedUrl = new URL(url, constants.BASE_URL);
|
|
6
|
-
const urlSections = parsedUrl.pathname.split("/").filter((e) => e !== "");
|
|
7
|
-
|
|
8
|
-
const baseUrl = "/" + urlSections.filter((e) => e.indexOf(".") < 0).join("/");
|
|
9
|
-
const file = urlSections.filter((e) => e.indexOf(".") >= 0);
|
|
10
|
-
const hasFile = file.length > 0;
|
|
11
|
-
|
|
12
|
-
return {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
hasFile:
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
const constants = require("./constants");
|
|
2
|
+
|
|
3
|
+
const parser = (url) => {
|
|
4
|
+
|
|
5
|
+
const parsedUrl = new URL(url, constants.BASE_URL);
|
|
6
|
+
const urlSections = parsedUrl.pathname.split("/").filter((e) => e !== "");
|
|
7
|
+
|
|
8
|
+
const baseUrl = "/" + urlSections.filter((e) => e.indexOf(".") < 0).join("/");
|
|
9
|
+
const file = urlSections.filter((e) => e.indexOf(".") >= 0);
|
|
10
|
+
const hasFile = file.length > 0;
|
|
11
|
+
|
|
12
|
+
return {
|
|
13
|
+
pathname: parsedUrl.pathname,
|
|
14
|
+
base: baseUrl,
|
|
15
|
+
file: hasFile ? file[0] : "",
|
|
16
|
+
hasFile: hasFile,
|
|
17
|
+
search: parsedUrl.searchParams
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Matches a request path against an endpoint pattern that may contain
|
|
24
|
+
* path parameters (e.g., /users/:id/orders/:orderId).
|
|
25
|
+
*
|
|
26
|
+
* @param {string} pattern - The endpoint pattern from the configuration.
|
|
27
|
+
* @param {string} requestPath - The actual incoming request base path.
|
|
28
|
+
* @returns {{ match: boolean, params: Object }} Whether it matched and extracted path parameters.
|
|
29
|
+
*/
|
|
30
|
+
const matchPath = (pattern, requestPath) => {
|
|
31
|
+
const patternParts = pattern.split("/").filter((e) => e !== "");
|
|
32
|
+
const requestParts = requestPath.split("/").filter((e) => e !== "");
|
|
33
|
+
|
|
34
|
+
if (patternParts.length !== requestParts.length) {
|
|
35
|
+
return { match: false, params: {} };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const params = {};
|
|
39
|
+
|
|
40
|
+
for (let i = 0; i < patternParts.length; i++) {
|
|
41
|
+
if (patternParts[i].startsWith(":")) {
|
|
42
|
+
params[patternParts[i].substring(1)] = requestParts[i];
|
|
43
|
+
} else if (patternParts[i] !== requestParts[i]) {
|
|
44
|
+
return { match: false, params: {} };
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return { match: true, params };
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
module.exports = {
|
|
52
|
+
parse: parser,
|
|
53
|
+
matchPath: matchPath
|
|
23
54
|
};
|
package/package.json
CHANGED
|
@@ -1,21 +1,39 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "mockapi-msi",
|
|
3
|
-
"version": "2.0
|
|
4
|
-
"description": "Mock API is a lightweight configurable HTTP API for testing and prototyping.",
|
|
5
|
-
"main": "main.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"start": "node ./main.js",
|
|
8
|
-
"test": "
|
|
9
|
-
},
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "mockapi-msi",
|
|
3
|
+
"version": "2.5.0",
|
|
4
|
+
"description": "Mock API is a lightweight configurable HTTP API for testing and prototyping.",
|
|
5
|
+
"main": "main.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"start": "node ./main.js",
|
|
8
|
+
"test": "node --test tests/*.test.js"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"mock",
|
|
12
|
+
"api",
|
|
13
|
+
"testing",
|
|
14
|
+
"prototyping",
|
|
15
|
+
"http",
|
|
16
|
+
"rest",
|
|
17
|
+
"fake-api",
|
|
18
|
+
"stub"
|
|
19
|
+
],
|
|
20
|
+
"author": "Matias Iacono",
|
|
21
|
+
"license": "ISC",
|
|
22
|
+
"bin": {
|
|
23
|
+
"mockapi": "main.js"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"main.js",
|
|
27
|
+
"modules/",
|
|
28
|
+
"apiHandlers/",
|
|
29
|
+
"testdata/",
|
|
30
|
+
"LICENSE",
|
|
31
|
+
"README.md"
|
|
32
|
+
],
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=18"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"yaml": "2.2.2"
|
|
38
|
+
}
|
|
21
39
|
}
|
package/testdata/data.csv
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
id,name,money
|
|
2
|
-
1,"test 1",100
|
|
3
|
-
2,"test 2",100
|
|
4
|
-
3,"test 3",100
|
|
5
|
-
4,"test, 4",100
|
|
1
|
+
id,name,money
|
|
2
|
+
1,"test 1",100
|
|
3
|
+
2,"test 2",100
|
|
4
|
+
3,"test 3",100
|
|
5
|
+
4,"test, 4",100
|
|
6
6
|
5,"test 5",100
|
package/.dockerignore
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
**/.classpath
|
|
2
|
-
**/.dockerignore
|
|
3
|
-
**/.env
|
|
4
|
-
**/.git
|
|
5
|
-
**/.gitignore
|
|
6
|
-
**/.project
|
|
7
|
-
**/.settings
|
|
8
|
-
**/.toolstarget
|
|
9
|
-
**/.vs
|
|
10
|
-
**/.vscode
|
|
11
|
-
**/*.*proj.user
|
|
12
|
-
**/*.dbmdl
|
|
13
|
-
**/*.jfm
|
|
14
|
-
**/azds.yaml
|
|
15
|
-
**/charts
|
|
16
|
-
**/docker-compose*
|
|
17
|
-
**/compose*
|
|
18
|
-
**/Dockerfile*
|
|
19
|
-
**/node_modules
|
|
20
|
-
**/npm-debug.log
|
|
21
|
-
**/obj
|
|
22
|
-
**/secrets.dev.yaml
|
|
23
|
-
**/values.dev.yaml
|
|
24
|
-
README.md
|
package/.mockapi-config
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
# basic Mock API configuration
|
|
3
|
-
port: 8001
|
|
4
|
-
enableCors: true
|
|
5
|
-
#externalModulesPath: "apiHandlers/"
|
|
6
|
-
|
|
7
|
-
# dummy example configurations
|
|
8
|
-
# PATH must point to a existing CSV file
|
|
9
|
-
# 3 native readers are supported: text | csv | folder
|
|
10
|
-
data:
|
|
11
|
-
myRows:
|
|
12
|
-
path: "./testdata/data.csv"
|
|
13
|
-
reader: csv
|
|
14
|
-
properties:
|
|
15
|
-
- json
|
|
16
|
-
- seq
|
|
17
|
-
- 0
|
|
18
|
-
#options
|
|
19
|
-
#json || text
|
|
20
|
-
#seq || rand
|
|
21
|
-
#-1 = return every record on every request
|
|
22
|
-
#0...n starting index (always reset to 0 when reach EOR)
|
|
23
|
-
|
|
24
|
-
# custom http response handlers section
|
|
25
|
-
# use: handler unique name : file name within the previously declared folder
|
|
26
|
-
#customHandlers:
|
|
27
|
-
# "custom": "myCustomHandler"
|
|
28
|
-
|
|
29
|
-
# Mock API available endpoints, return types and
|
|
30
|
-
# general configuration
|
|
31
|
-
# verb: any | get | post | delete | ...
|
|
32
|
-
# data: refers to previously configured data and readers
|
|
33
|
-
# responseStatus: 200 | 404 | 500 | ...
|
|
34
|
-
# responseContentType: any MIME available type
|
|
35
|
-
# handler: refers to any previously configured custom handler
|
|
36
|
-
endpoints:
|
|
37
|
-
"/data":
|
|
38
|
-
verb: get
|
|
39
|
-
data: myRows
|
|
40
|
-
responseStatus: 200
|
|
41
|
-
responseContentType: "application/json"
|
|
42
|
-
#handler: "custom"
|
|
43
|
-
|
|
44
|
-
log: verbose
|
|
45
|
-
#debug
|
|
46
|
-
#error
|
|
47
|
-
#verbose
|
|
48
|
-
#none
|
package/Dockerfile
DELETED
package/docker-compose.debug.yml
DELETED