typescript-mock-server 0.0.4 → 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 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
- 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
- # Dependencies
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
+ ## 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,10 +1,11 @@
1
1
  {
2
2
  "name": "typescript-mock-server",
3
- "version": "0.0.4",
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",
7
- "start": "ts-node-dev src/index.ts --path=tms-models"
7
+ "example": "ts-node-dev src/index.ts --path=tms-models",
8
+ "start": "ts-node-dev src/index.ts"
8
9
  },
9
10
  "repository": {
10
11
  "type": "git",
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
- const baseDirPath = process.cwd()
8
- console.log(baseDirPath);
9
-
10
- const argv = (() => {
11
- const args = {};
12
- process.argv.slice(2).map((element) => {
13
- const matches = element.match('--([a-zA-Z0-9]+)=(.*)');
14
- if (matches) {
15
- // @ts-ignore
16
- args[matches[1]] = matches[2]
17
- .replace(/^['"]/, '').replace(/['"]$/, '');
18
- }
19
- });
20
- return args;
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
- console.log(argv);
22
+ console.log(argv);
24
23
 
25
24
  // Create a new express app instance
26
- const app: Express = express();
25
+ const app: Express = express();
27
26
 
28
27
  // @ts-ignore
29
- const args = argv['path'];
28
+ const args = argv['path'];
30
29
 
31
30
  // @ts-ignore
32
- const basePath = `${baseDirPath}/${args}`;
33
-
34
- console.log('basePath:' + basePath);
35
-
36
- async function print(path: string) {
37
- console.log(path);
38
- const dir = await fs.promises.opendir(path);
39
- for await (const dirent of dir) {
40
- if (dirent.isDirectory()) {
41
- await print(`${path}/${dirent.name}`);
42
- } else {
43
- handleFile(path, dirent);
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
- print(basePath).catch(console.error);
47
+ print(basePath).catch(console.error);
49
48
 
50
- async function loadModule(moduleName: string) {
51
- return await import(moduleName);
52
- }
49
+ async function loadModule(moduleName: string) {
50
+ return await import(moduleName);
51
+ }
53
52
 
54
- app.listen(3000, function() {
55
- console.log('App is listening on port 3000!');
56
- });
53
+ app.listen(3000, function() {
54
+ console.log('App is listening on port 3000!');
55
+ });
57
56
 
58
- function handleFile(path: string, dirent: fs.Dirent) {
59
- console.log('File name: ' + dirent.name);
60
- if (dirent.name.startsWith('get')) {
61
- handleGetRequest(path, dirent);
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
- function addEndpoint(endpoint: string, model: any) {
66
- app.get(endpoint, function(req, res) {
67
- res.send(model.data)
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
- function handleGetRequest(path: string, dirent: fs.Dirent) {
72
- console.log('Adding GET request');
73
- const endpoint = convertFileNameToEndpoint(path, dirent);
74
- console.log('Endpoint: ' + endpoint);
75
- const modulePath = `${path}/${dirent.name}`;
76
- console.log('Resolve module: ' + modulePath);
77
- loadModule(modulePath)
78
- .then(model => addEndpoint(endpoint, model))
79
- .catch(err => console.error(err));
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
- function convertFileNameToEndpoint(path: string, dirent: fs.Dirent): string {
83
- let endpoint = `${path.replace(basePath, '')}/${dirent.name}`;
84
- endpoint = endpoint.replace('.ts', '');
85
- endpoint = endpoint.replace('get', '');
86
- if (endpoint !== '') {
87
- endpoint = endpoint.replace('-', '');
88
- }
89
- return endpoint;
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
  }
@@ -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 myNewMethod = () => new Date();
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
  }];