typescript-mock-server 0.0.6 → 0.0.7
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/README.md +2 -2
- package/package.json +1 -1
- package/src/index.ts +69 -70
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# Typescript mock server
|
|
2
2
|
Simple mock/stub server that can be used in front end development. Instead of creating json files you can just publish TypeScript objects as json.
|
|
3
3
|
This makes it easier to maintain your mocks because you can load your own models. When you change your implementation you also
|
|
4
|
-
have to update your mock, otherwise you will receive compile errors.
|
|
4
|
+
have to update your mock, otherwise you will receive compile errors.
|
|
5
5
|
|
|
6
6
|
# Quickstart
|
|
7
7
|
The easiest way to check out this stub/mock server is by installing it as a (dev)dependency and then
|
|
@@ -10,7 +10,7 @@ Please note we are changing the directory and therefore, to set the path paramet
|
|
|
10
10
|
I am looking for a cleaner way of doing this. Your models should export a data const and your file should be named as `^(get|post){1}(-\d)?.ts$`.
|
|
11
11
|
Changes are being picked up automatically, so no need for a restart.
|
|
12
12
|
|
|
13
|
-
Check out [the examples](https://github.com/GuyT07/typescript-mock-server/tree/main/tms-models/users).
|
|
13
|
+
Check out the [working example project](https://github.com/GuyT07/typescript-mock-server-examle) and source [the examples](https://github.com/GuyT07/typescript-mock-server/tree/main/tms-models/users).
|
|
14
14
|
|
|
15
15
|
## Adding GET mocks/stubs
|
|
16
16
|
Examples talk, so lets start with an example.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typescript-mock-server",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"description": "Simple mock server that can be used in front end development. Instead of creating json files you can just publish TypeScript objects as json",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "echo \"Error: no test specified\" && exit 1",
|
package/src/index.ts
CHANGED
|
@@ -1,90 +1,89 @@
|
|
|
1
1
|
#!./../node_modules/.bin/ts-node-dev
|
|
2
2
|
|
|
3
|
-
import express from 'express';
|
|
4
|
-
import { Express } from 'express';
|
|
3
|
+
import express, { Express } from 'express';
|
|
5
4
|
import * as fs from 'fs';
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
6
|
+
const baseDirPath = process.cwd();
|
|
7
|
+
console.log(baseDirPath);
|
|
8
|
+
|
|
9
|
+
const argv = (() => {
|
|
10
|
+
const args = {};
|
|
11
|
+
process.argv.slice(2).map((element) => {
|
|
12
|
+
const matches = element.match('--([a-zA-Z0-9]+)=(.*)');
|
|
13
|
+
if (matches) {
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
args[matches[1]] = matches[2]
|
|
16
|
+
.replace(/^['"]/, '').replace(/['"]$/, '');
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
return args;
|
|
20
|
+
})();
|
|
22
21
|
|
|
23
|
-
|
|
22
|
+
console.log(argv);
|
|
24
23
|
|
|
25
24
|
// Create a new express app instance
|
|
26
|
-
|
|
25
|
+
const app: Express = express();
|
|
27
26
|
|
|
28
27
|
// @ts-ignore
|
|
29
|
-
|
|
28
|
+
const args = argv['path'];
|
|
30
29
|
|
|
31
30
|
// @ts-ignore
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
}
|
|
31
|
+
const basePath = `${baseDirPath}/${args}`;
|
|
32
|
+
|
|
33
|
+
console.log('basePath:' + basePath);
|
|
34
|
+
|
|
35
|
+
async function print(path: string) {
|
|
36
|
+
console.log(path);
|
|
37
|
+
const dir = await fs.promises.opendir(path);
|
|
38
|
+
for await (const dirent of dir) {
|
|
39
|
+
if (dirent.isDirectory()) {
|
|
40
|
+
await print(`${path}/${dirent.name}`);
|
|
41
|
+
} else {
|
|
42
|
+
handleFile(path, dirent);
|
|
46
43
|
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
47
46
|
|
|
48
|
-
|
|
47
|
+
print(basePath).catch(console.error);
|
|
49
48
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
49
|
+
async function loadModule(moduleName: string) {
|
|
50
|
+
return await import(moduleName);
|
|
51
|
+
}
|
|
53
52
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
app.listen(3000, function() {
|
|
54
|
+
console.log('App is listening on port 3000!');
|
|
55
|
+
});
|
|
57
56
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
57
|
+
function handleFile(path: string, dirent: fs.Dirent) {
|
|
58
|
+
console.log('File name: ' + dirent.name);
|
|
59
|
+
if (dirent.name.startsWith('get')) {
|
|
60
|
+
handleGetRequest(path, dirent);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
64
63
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
64
|
+
function addEndpoint(endpoint: string, model: any) {
|
|
65
|
+
app.get(endpoint, function(req, res) {
|
|
66
|
+
res.send(model.data);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
70
69
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
70
|
+
function handleGetRequest(path: string, dirent: fs.Dirent) {
|
|
71
|
+
console.log('Adding GET request');
|
|
72
|
+
const endpoint = convertFileNameToEndpoint(path, dirent);
|
|
73
|
+
console.log('Endpoint: ' + endpoint);
|
|
74
|
+
const modulePath = `${path}/${dirent.name}`;
|
|
75
|
+
console.log('Resolve module: ' + modulePath);
|
|
76
|
+
loadModule(modulePath)
|
|
77
|
+
.then(model => addEndpoint(endpoint, model))
|
|
78
|
+
.catch(err => console.error(err));
|
|
79
|
+
}
|
|
81
80
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
81
|
+
function convertFileNameToEndpoint(path: string, dirent: fs.Dirent): string {
|
|
82
|
+
let endpoint = `${path.replace(basePath, '')}/${dirent.name}`;
|
|
83
|
+
endpoint = endpoint.replace('.ts', '');
|
|
84
|
+
endpoint = endpoint.replace('get', '');
|
|
85
|
+
if (endpoint !== '') {
|
|
86
|
+
endpoint = endpoint.replace('-', '');
|
|
87
|
+
}
|
|
88
|
+
return endpoint;
|
|
90
89
|
}
|