typescript-mock-server 0.0.3 → 0.0.6
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 +65 -3
- package/package.json +3 -4
- package/src/index.ts +1 -5
- package/tms-models/users/get.ts +6 -3
- package/index.js +0 -5
package/README.md
CHANGED
|
@@ -1,10 +1,72 @@
|
|
|
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
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 examples](https://github.com/GuyT07/typescript-mock-server/tree/main/tms-models/users).
|
|
8
14
|
|
|
15
|
+
## Adding GET mocks/stubs
|
|
16
|
+
Examples talk, so lets start with an example.
|
|
17
|
+
|
|
18
|
+
Your requirement is to have the following endpoints `/users`, `/users/1` and `/users/profile/1`
|
|
19
|
+
|
|
20
|
+
Create a root folder that contains your models, like `models`. Then add a folder `users` and within the newly created folder
|
|
21
|
+
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
|
|
22
|
+
have the following structure:
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
--models
|
|
26
|
+
-- users
|
|
27
|
+
- get.ts
|
|
28
|
+
- get-1.ts
|
|
29
|
+
-- profile
|
|
30
|
+
- get-1.ts
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Within the model file you can import/create your model:
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
export interface User {
|
|
37
|
+
id: number;
|
|
38
|
+
firstName: string;
|
|
39
|
+
lastName: string;
|
|
40
|
+
creationDate: Date;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const newDate = () => new Date();
|
|
44
|
+
|
|
45
|
+
export const data: User[] = [{
|
|
46
|
+
id: 1,
|
|
47
|
+
firstName: 'Guy',
|
|
48
|
+
lastName: 'Theuws',
|
|
49
|
+
creationDate: newDate()
|
|
50
|
+
}, {
|
|
51
|
+
id: 2,
|
|
52
|
+
firstName: 'Generation Y',
|
|
53
|
+
lastName: 'Development',
|
|
54
|
+
creationDate: newDate()
|
|
55
|
+
}];
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Dependencies
|
|
59
|
+
Following dependencies are being used:
|
|
60
|
+
|
|
61
|
+
- express
|
|
62
|
+
- ts-node-dev
|
|
63
|
+
- typescript
|
|
64
|
+
- @types/express
|
|
65
|
+
- @types/node
|
|
66
|
+
|
|
67
|
+
## Roadmap
|
|
68
|
+
- [ ] Support other server port
|
|
69
|
+
- [ ] Improve paths/way to start
|
|
70
|
+
- [ ] Support different headers
|
|
71
|
+
- [ ] Support all HTTP methods
|
|
9
72
|
|
|
10
|
-
#
|
package/package.json
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typescript-mock-server",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
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
|
-
"main": "index.js",
|
|
6
|
-
"bin": "index.js",
|
|
7
5
|
"scripts": {
|
|
8
6
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
9
|
-
"
|
|
7
|
+
"example": "ts-node-dev src/index.ts --path=tms-models",
|
|
8
|
+
"start": "ts-node-dev src/index.ts"
|
|
10
9
|
},
|
|
11
10
|
"repository": {
|
|
12
11
|
"type": "git",
|
package/src/index.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
#!./../node_modules/.bin/ts-node-dev
|
|
2
2
|
|
|
3
3
|
import express from 'express';
|
|
4
4
|
import { Express } from 'express';
|
|
5
5
|
import * as fs from 'fs';
|
|
6
6
|
|
|
7
|
-
// @ts-ignore
|
|
8
|
-
export default function mockServer() {
|
|
9
|
-
|
|
10
7
|
const baseDirPath = process.cwd()
|
|
11
8
|
console.log(baseDirPath);
|
|
12
9
|
|
|
@@ -90,5 +87,4 @@ export default function mockServer() {
|
|
|
90
87
|
endpoint = endpoint.replace('-', '');
|
|
91
88
|
}
|
|
92
89
|
return endpoint;
|
|
93
|
-
}
|
|
94
90
|
}
|
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
|
+
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
|
}];
|