typescript-mock-server 0.0.12 → 1.0.1

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
@@ -58,6 +58,13 @@ export const data: User[] = [{
58
58
  lastName: 'Development',
59
59
  creationDate: newDate()
60
60
  }];
61
+
62
+ export const config: Config = {
63
+ server: {
64
+ delay: 2000,
65
+ statusCode: 418
66
+ }
67
+ }
61
68
  ```
62
69
 
63
70
  ## Dependencies
@@ -72,19 +79,21 @@ Following dependencies are being used:
72
79
  ## Roadmap
73
80
  - [x] Support other server port
74
81
  - [x] Improve paths/way to start
75
- - [ ] Support different headers/configurations (delays, status codes, ...)
82
+ - [x] Support different headers/configurations (delays, status codes, ...)
76
83
  - [x] Support most used HTTP methods
77
84
  - [ ] Add tests
78
- - [ ] Refactor, split up in separate classes (first check if people actually want to use the tool)
85
+ - [x] Refactor, split up in separate classes (first check if people actually want to use the tool)
79
86
  - [ ] Setup CI/CD (+code quality + coverage tooling)
80
87
  - [ ] Setup website
81
88
  - [ ] Create a JVM compatible version
82
- - [ ] Create interface to force implementation of required properties and make it more stable
83
- - [ ] Improve error handling (missing properties etc.)
89
+ - [x] Create interface to force implementation of required properties and make it more stable
90
+ - [x] Improve error handling (missing properties etc.)
84
91
  - [ ] Create an optional persistent state
85
92
 
86
93
 
87
94
  ## Release notes (will be moved to GitHub in the future)
95
+ - v1.0.1 - Include correct files in GIT tag
96
+ - v1.0.0 - Breaking change: renamed Server config to Request and added interval for delay
88
97
  - v0.0.11 - Add items to roadmap, bug fixes
89
98
  - v0.0.10 - Support multiple http verbs
90
99
 
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "typescript-mock-server",
3
- "version": "0.0.12",
3
+ "version": "1.0.1",
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
7
  "example": "ts-node-dev src/index.ts --path=tms-models --port=5000",
8
8
  "start": "ts-node-dev src/index.ts",
9
9
  "update-deps": "npm update",
10
- "publish-to-npm": "npm publish"
10
+ "get-version": "echo $npm_package_version",
11
+ "publish-to-npm": "git tag -a ${npm_package_version} -m \"v${npm_package_version}\" && git push && npm publish"
11
12
  },
12
13
  "repository": {
13
14
  "type": "git",
@@ -32,12 +33,12 @@
32
33
  "prettier": "^2.6.2"
33
34
  },
34
35
  "dependencies": {
35
- "@types/express": "^4.17.9",
36
- "@types/node": "^14.18.12",
37
- "express": "^4.17.3",
38
- "ts-node-dev": "^1.1.8",
36
+ "@types/express": "^4.17.13",
37
+ "@types/node": "^18.0.5",
38
+ "express": "^4.18.1",
39
+ "ts-node-dev": "2.0.0",
39
40
  "tslog": "^3.3.3",
40
- "typescript": "^4.0.5"
41
+ "typescript": "^4.7.4"
41
42
  },
42
43
  "prettier": {
43
44
  "arrowParens": "avoid",
@@ -1,6 +1,5 @@
1
1
  export interface CommandLine {
2
2
  getCommands(): Map<Command, string>;
3
-
4
3
  getCommand(command: Command): string | undefined;
5
4
  }
6
5
 
@@ -8,6 +8,7 @@ import { opendir } from 'fs/promises';
8
8
  import { LoggerImpl } from './logger-impl';
9
9
  import { Logger } from '../logger';
10
10
  import { TypescriptMockServer } from '../typescript-mock-server';
11
+ import { Interval } from '../models/config';
11
12
 
12
13
  export class TypescriptMockServerImpl implements TypescriptMockServer{
13
14
 
@@ -53,7 +54,25 @@ export class TypescriptMockServerImpl implements TypescriptMockServer{
53
54
  }
54
55
 
55
56
  private addEndpoint(endpoint: string, httpVerb: HttpVerb, model: any) {
56
- this.app[httpVerb](endpoint, (req, res) => res.send(model.data));
57
+ this.app[httpVerb](endpoint, (req, res) => {
58
+ if (model?.config?.statusCode) {
59
+ res.statusCode = model?.config?.statusCode;
60
+ }
61
+ if (model?.config?.delay) {
62
+ setTimeout(() => res.send(model.data), this.getDelayValue(model?.config?.delay));
63
+ } else {
64
+ return res.send(model.data);
65
+ }
66
+ });
67
+ }
68
+
69
+ private getDelayValue(delay: number | Interval): number {
70
+ if (typeof delay === 'number') {
71
+ return delay;
72
+ } else if (delay.min && delay.max) {
73
+ return Math.floor(delay.min + Math.random() * delay.max);
74
+ }
75
+ return 0;
57
76
  }
58
77
 
59
78
  private handleRequest(path: string, dirent: Dirent, httpVerb: HttpVerb) {
@@ -0,0 +1,13 @@
1
+ export interface DefaultConfig {
2
+ request: RequestConfig;
3
+ }
4
+
5
+ export interface RequestConfig {
6
+ delay?: number | Interval; // Delay response (in ms), or an interval
7
+ statusCode?: number; // Status code of response
8
+ }
9
+
10
+ export interface Interval {
11
+ min: number; // Minimum boundary, including the value
12
+ max: number; // Maximum boundary, including the value
13
+ }
@@ -1,11 +1,20 @@
1
+ import { RequestConfig } from '../../src/models/config';
2
+
1
3
  interface User {
2
- id: number;
3
- firstName: string;
4
- lastName: string;
4
+ id: number;
5
+ firstName: string;
6
+ lastName: string;
5
7
  }
6
8
 
7
9
  export const data: User = {
8
- id: 1,
9
- firstName: 'Guy',
10
- lastName: 'Theuws'
10
+ id: 1,
11
+ firstName: 'Guy',
12
+ lastName: 'Theuws',
13
+ };
14
+
15
+ export const config: RequestConfig = {
16
+ delay: {
17
+ min: 1000,
18
+ max: 5000,
19
+ },
11
20
  };
@@ -1,3 +1,5 @@
1
+ import { RequestConfig } from '../../src/models/config';
2
+
1
3
  export interface User {
2
4
  id: number;
3
5
  firstName: string;
@@ -18,3 +20,8 @@ export const data: User[] = [{
18
20
  lastName: 'Development',
19
21
  creationDate: newDate()
20
22
  }];
23
+
24
+ export const config: RequestConfig = {
25
+ delay: 2000,
26
+ statusCode: 418
27
+ }
package/tsconfig.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "target": "ES2021", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
3
+ "target": "es6", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
4
4
 
5
5
  "module": "commonjs", /* Specify what module code is generated. */
6
6