typescript-mock-server 0.0.5 → 0.0.8
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 +70 -4
- package/package.json +2 -2
- package/src/index.js +133 -0
- package/src/index.ts +70 -72
- package/tms-models/users/get-1.js +8 -0
- package/tms-models/users/get.js +16 -0
- package/tms-models/users/get.ts +6 -3
- package/tms-models/users/profile/get-1.js +12 -0
- package/tms-models/users/profile/get-1.ts +3 -2
- package/nodemon.json +0 -9
package/README.md
CHANGED
|
@@ -1,10 +1,76 @@
|
|
|
1
1
|
# Typescript mock server
|
|
2
|
-
Simple mock server that can be used in front end development. Instead of creating json files you can just publish TypeScript objects as json.
|
|
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
|
+
The easiest way to check out this stub/mock server is by installing it as a (dev)dependency and then
|
|
8
|
+
add a script to you scripts section: `cd node_modules/typescript-mock-server && npm run start -- --path=../../path-to-your-model-directory`.
|
|
9
|
+
Please note we are changing the directory and therefore, to set the path parameter, you need to go 2 levels up to be in your root.
|
|
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
|
+
Changes are being picked up automatically, so no need for a restart.
|
|
7
12
|
|
|
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).
|
|
8
14
|
|
|
15
|
+
# Options
|
|
16
|
+
`--port=x`: Port number the server runs on
|
|
17
|
+
`--path=x`: Path to your models
|
|
18
|
+
|
|
19
|
+
## Adding GET mocks/stubs
|
|
20
|
+
Examples talk, so lets start with an example.
|
|
21
|
+
|
|
22
|
+
Your requirement is to have the following endpoints `/users`, `/users/1` and `/users/profile/1`
|
|
23
|
+
|
|
24
|
+
Create a root folder that contains your models, like `models`. Then add a folder `users` and within the newly created folder
|
|
25
|
+
create a new folder `profile`. Add following files `get.ts` and `get-1.ts` in the `users` directory, `get-1.ts` in the `profile` dir. You should
|
|
26
|
+
have the following structure:
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
--models
|
|
30
|
+
-- users
|
|
31
|
+
- get.ts
|
|
32
|
+
- get-1.ts
|
|
33
|
+
-- profile
|
|
34
|
+
- get-1.ts
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Within the model file you can import/create your model:
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
export interface User {
|
|
41
|
+
id: number;
|
|
42
|
+
firstName: string;
|
|
43
|
+
lastName: string;
|
|
44
|
+
creationDate: Date;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const newDate = () => new Date();
|
|
48
|
+
|
|
49
|
+
export const data: User[] = [{
|
|
50
|
+
id: 1,
|
|
51
|
+
firstName: 'Guy',
|
|
52
|
+
lastName: 'Theuws',
|
|
53
|
+
creationDate: newDate()
|
|
54
|
+
}, {
|
|
55
|
+
id: 2,
|
|
56
|
+
firstName: 'Generation Y',
|
|
57
|
+
lastName: 'Development',
|
|
58
|
+
creationDate: newDate()
|
|
59
|
+
}];
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Dependencies
|
|
63
|
+
Following dependencies are being used:
|
|
64
|
+
|
|
65
|
+
- express
|
|
66
|
+
- ts-node-dev
|
|
67
|
+
- typescript
|
|
68
|
+
- @types/express
|
|
69
|
+
- @types/node
|
|
70
|
+
|
|
71
|
+
## Roadmap
|
|
72
|
+
- [x] Support other server port
|
|
73
|
+
- [ ] Improve paths/way to start
|
|
74
|
+
- [ ] Support different headers
|
|
75
|
+
- [ ] Support all HTTP methods
|
|
9
76
|
|
|
10
|
-
#
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typescript-mock-server",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
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",
|
|
7
|
-
"example": "ts-node-dev src/index.ts --path=tms-models",
|
|
7
|
+
"example": "ts-node-dev src/index.ts --path=tms-models --port=5000",
|
|
8
8
|
"start": "ts-node-dev src/index.ts"
|
|
9
9
|
},
|
|
10
10
|
"repository": {
|
package/src/index.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
#!./../node_modules/.bin/ts-node-dev
|
|
2
|
+
"use strict";
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
15
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
16
|
+
}) : function(o, v) {
|
|
17
|
+
o["default"] = v;
|
|
18
|
+
});
|
|
19
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
20
|
+
if (mod && mod.__esModule) return mod;
|
|
21
|
+
var result = {};
|
|
22
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
23
|
+
__setModuleDefault(result, mod);
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
26
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
27
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
28
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
29
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
30
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
31
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
32
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
36
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
37
|
+
var m = o[Symbol.asyncIterator], i;
|
|
38
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
39
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
40
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
41
|
+
};
|
|
42
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
43
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
44
|
+
};
|
|
45
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
|
+
const express_1 = __importDefault(require("express"));
|
|
47
|
+
const fs = __importStar(require("fs"));
|
|
48
|
+
const baseDirPath = process.cwd();
|
|
49
|
+
console.log(baseDirPath);
|
|
50
|
+
const argv = (() => {
|
|
51
|
+
const args = {};
|
|
52
|
+
process.argv.slice(2).map((element) => {
|
|
53
|
+
const matches = element.match('--([a-zA-Z0-9]+)=(.*)');
|
|
54
|
+
if (matches) {
|
|
55
|
+
// @ts-ignore
|
|
56
|
+
args[matches[1]] = matches[2]
|
|
57
|
+
.replace(/^['"]/, '').replace(/['"]$/, '');
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
return args;
|
|
61
|
+
})();
|
|
62
|
+
console.log(argv);
|
|
63
|
+
// Create a new express app instance
|
|
64
|
+
const app = (0, express_1.default)();
|
|
65
|
+
// @ts-ignore
|
|
66
|
+
const args = argv['path'];
|
|
67
|
+
// @ts-ignore
|
|
68
|
+
const basePath = `${baseDirPath}/${args}`;
|
|
69
|
+
console.log('basePath:' + basePath);
|
|
70
|
+
function print(path) {
|
|
71
|
+
var e_1, _a;
|
|
72
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
console.log(path);
|
|
74
|
+
const dir = yield fs.promises.opendir(path);
|
|
75
|
+
try {
|
|
76
|
+
for (var dir_1 = __asyncValues(dir), dir_1_1; dir_1_1 = yield dir_1.next(), !dir_1_1.done;) {
|
|
77
|
+
const dirent = dir_1_1.value;
|
|
78
|
+
if (dirent.isDirectory()) {
|
|
79
|
+
yield print(`${path}/${dirent.name}`);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
handleFile(path, dirent);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
87
|
+
finally {
|
|
88
|
+
try {
|
|
89
|
+
if (dir_1_1 && !dir_1_1.done && (_a = dir_1.return)) yield _a.call(dir_1);
|
|
90
|
+
}
|
|
91
|
+
finally { if (e_1) throw e_1.error; }
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
print(basePath).catch(console.error);
|
|
96
|
+
function loadModule(moduleName) {
|
|
97
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
98
|
+
return yield Promise.resolve().then(() => __importStar(require(moduleName)));
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
app.listen(3000, function () {
|
|
102
|
+
console.log('App is listening on port 3000!');
|
|
103
|
+
});
|
|
104
|
+
function handleFile(path, dirent) {
|
|
105
|
+
console.log('File name: ' + dirent.name);
|
|
106
|
+
if (dirent.name.startsWith('get')) {
|
|
107
|
+
handleGetRequest(path, dirent);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function addEndpoint(endpoint, model) {
|
|
111
|
+
app.get(endpoint, function (req, res) {
|
|
112
|
+
res.send(model.data);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
function handleGetRequest(path, dirent) {
|
|
116
|
+
console.log('Adding GET request');
|
|
117
|
+
const endpoint = convertFileNameToEndpoint(path, dirent);
|
|
118
|
+
console.log('Endpoint: ' + endpoint);
|
|
119
|
+
const modulePath = `${path}/${dirent.name}`;
|
|
120
|
+
console.log('Resolve module: ' + modulePath);
|
|
121
|
+
loadModule(modulePath)
|
|
122
|
+
.then(model => addEndpoint(endpoint, model))
|
|
123
|
+
.catch(err => console.error(err));
|
|
124
|
+
}
|
|
125
|
+
function convertFileNameToEndpoint(path, dirent) {
|
|
126
|
+
let endpoint = `${path.replace(basePath, '')}/${dirent.name}`;
|
|
127
|
+
endpoint = endpoint.replace('.ts', '');
|
|
128
|
+
endpoint = endpoint.replace('get', '');
|
|
129
|
+
if (endpoint !== '') {
|
|
130
|
+
endpoint = endpoint.replace('-', '');
|
|
131
|
+
}
|
|
132
|
+
return endpoint;
|
|
133
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,90 +1,88 @@
|
|
|
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: { [key: string]: string } = (() => {
|
|
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
|
-
|
|
29
|
-
const args = argv['path'];
|
|
27
|
+
const {path, port} = argv;
|
|
30
28
|
|
|
31
29
|
// @ts-ignore
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
}
|
|
30
|
+
const basePath = `${baseDirPath}/${path}`;
|
|
31
|
+
|
|
32
|
+
console.log('basePath:' + basePath);
|
|
33
|
+
|
|
34
|
+
async function print(path: string) {
|
|
35
|
+
console.log(path);
|
|
36
|
+
const dir = await fs.promises.opendir(path);
|
|
37
|
+
for await (const dirent of dir) {
|
|
38
|
+
if (dirent.isDirectory()) {
|
|
39
|
+
await print(`${path}/${dirent.name}`);
|
|
40
|
+
} else {
|
|
41
|
+
handleFile(path, dirent);
|
|
46
42
|
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
47
45
|
|
|
48
|
-
|
|
46
|
+
print(basePath).catch(console.error);
|
|
49
47
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
48
|
+
async function loadModule(moduleName: string) {
|
|
49
|
+
return await import(moduleName);
|
|
50
|
+
}
|
|
53
51
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
app.listen(port || 3000, function() {
|
|
53
|
+
console.log(`App is listening on port ${port || 3000}!`);
|
|
54
|
+
});
|
|
57
55
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
56
|
+
function handleFile(path: string, dirent: fs.Dirent) {
|
|
57
|
+
console.log('File name: ' + dirent.name);
|
|
58
|
+
if (dirent.name.startsWith('get')) {
|
|
59
|
+
handleGetRequest(path, dirent);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
64
62
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
63
|
+
function addEndpoint(endpoint: string, model: any) {
|
|
64
|
+
app.get(endpoint, function(req, res) {
|
|
65
|
+
res.send(model.data);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
70
68
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
69
|
+
function handleGetRequest(path: string, dirent: fs.Dirent) {
|
|
70
|
+
console.log('Adding GET request');
|
|
71
|
+
const endpoint = convertFileNameToEndpoint(path, dirent);
|
|
72
|
+
console.log('Endpoint: ' + endpoint);
|
|
73
|
+
const modulePath = `${path}/${dirent.name}`;
|
|
74
|
+
console.log('Resolve module: ' + modulePath);
|
|
75
|
+
loadModule(modulePath)
|
|
76
|
+
.then(model => addEndpoint(endpoint, model))
|
|
77
|
+
.catch(err => console.error(err));
|
|
78
|
+
}
|
|
81
79
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
80
|
+
function convertFileNameToEndpoint(path: string, dirent: fs.Dirent): string {
|
|
81
|
+
let endpoint = `${path.replace(basePath, '')}/${dirent.name}`;
|
|
82
|
+
endpoint = endpoint.replace('.ts', '');
|
|
83
|
+
endpoint = endpoint.replace('get', '');
|
|
84
|
+
if (endpoint !== '') {
|
|
85
|
+
endpoint = endpoint.replace('-', '');
|
|
86
|
+
}
|
|
87
|
+
return endpoint;
|
|
90
88
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.data = exports.newDate = void 0;
|
|
4
|
+
const newDate = () => new Date();
|
|
5
|
+
exports.newDate = newDate;
|
|
6
|
+
exports.data = [{
|
|
7
|
+
id: 1,
|
|
8
|
+
firstName: 'Guy',
|
|
9
|
+
lastName: 'Theuws',
|
|
10
|
+
creationDate: (0, exports.newDate)()
|
|
11
|
+
}, {
|
|
12
|
+
id: 2,
|
|
13
|
+
firstName: 'Generation Y',
|
|
14
|
+
lastName: 'Development',
|
|
15
|
+
creationDate: (0, exports.newDate)()
|
|
16
|
+
}];
|
package/tms-models/users/get.ts
CHANGED
|
@@ -2,16 +2,19 @@ export interface User {
|
|
|
2
2
|
id: number;
|
|
3
3
|
firstName: string;
|
|
4
4
|
lastName: string;
|
|
5
|
+
creationDate: Date;
|
|
5
6
|
}
|
|
6
7
|
|
|
7
|
-
const
|
|
8
|
+
export const newDate = () => new Date();
|
|
8
9
|
|
|
9
10
|
export const data: User[] = [{
|
|
10
11
|
id: 1,
|
|
11
12
|
firstName: 'Guy',
|
|
12
|
-
lastName: 'Theuws'
|
|
13
|
+
lastName: 'Theuws',
|
|
14
|
+
creationDate: newDate()
|
|
13
15
|
}, {
|
|
14
16
|
id: 2,
|
|
15
17
|
firstName: 'Generation Y',
|
|
16
|
-
lastName: 'Development'
|
|
18
|
+
lastName: 'Development',
|
|
19
|
+
creationDate: newDate()
|
|
17
20
|
}];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.data = void 0;
|
|
4
|
+
const get_1 = require("../get");
|
|
5
|
+
exports.data = {
|
|
6
|
+
id: 1, title: 'Hello profile', user: {
|
|
7
|
+
id: 1,
|
|
8
|
+
firstName: 'Guy',
|
|
9
|
+
lastName: 'Theuws',
|
|
10
|
+
creationDate: (0, get_1.newDate)()
|
|
11
|
+
}
|
|
12
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { User } from '../get';
|
|
1
|
+
import { newDate, User } from '../get';
|
|
2
2
|
|
|
3
3
|
interface Profile {
|
|
4
4
|
id: number;
|
|
@@ -10,6 +10,7 @@ export const data: Profile = {
|
|
|
10
10
|
id: 1, title: 'Hello profile', user: {
|
|
11
11
|
id: 1,
|
|
12
12
|
firstName: 'Guy',
|
|
13
|
-
lastName: 'Theuws'
|
|
13
|
+
lastName: 'Theuws',
|
|
14
|
+
creationDate: newDate()
|
|
14
15
|
}
|
|
15
16
|
};
|