typescript-mock-server 0.0.13 → 1.0.0

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
@@ -79,19 +79,20 @@ Following dependencies are being used:
79
79
  ## Roadmap
80
80
  - [x] Support other server port
81
81
  - [x] Improve paths/way to start
82
- - [ ] Support different headers/configurations (delays, status codes, ...)
82
+ - [x] Support different headers/configurations (delays, status codes, ...)
83
83
  - [x] Support most used HTTP methods
84
84
  - [ ] Add tests
85
- - [ ] 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)
86
86
  - [ ] Setup CI/CD (+code quality + coverage tooling)
87
87
  - [ ] Setup website
88
88
  - [ ] Create a JVM compatible version
89
- - [ ] Create interface to force implementation of required properties and make it more stable
90
- - [ ] 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.)
91
91
  - [ ] Create an optional persistent state
92
92
 
93
93
 
94
94
  ## Release notes (will be moved to GitHub in the future)
95
+ - v1.0.0 - Breaking change: renamed Server config to Request and added interval for delay
95
96
  - v0.0.11 - Add items to roadmap, bug fixes
96
97
  - v0.0.10 - Support multiple http verbs
97
98
 
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "typescript-mock-server",
3
- "version": "0.0.13",
3
+ "version": "1.0.0",
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}\" && npm publish"
11
12
  },
12
13
  "repository": {
13
14
  "type": "git",
@@ -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
 
@@ -54,17 +55,26 @@ export class TypescriptMockServerImpl implements TypescriptMockServer{
54
55
 
55
56
  private addEndpoint(endpoint: string, httpVerb: HttpVerb, model: any) {
56
57
  this.app[httpVerb](endpoint, (req, res) => {
57
- if (model?.config?.server?.statusCode) {
58
- res.statusCode = model?.config?.server?.statusCode;
58
+ if (model?.config?.statusCode) {
59
+ res.statusCode = model?.config?.statusCode;
59
60
  }
60
- if (model?.config?.server?.delay) {
61
- setTimeout(() => res.send(model.data), model?.config?.server?.delay);
61
+ if (model?.config?.delay) {
62
+ setTimeout(() => res.send(model.data), this.getDelayValue(model?.config?.delay));
62
63
  } else {
63
64
  return res.send(model.data);
64
65
  }
65
66
  });
66
67
  }
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;
76
+ }
77
+
68
78
  private handleRequest(path: string, dirent: Dirent, httpVerb: HttpVerb) {
69
79
  const endpoint = this.convertFileNameToEndpoint(path, dirent, httpVerb);
70
80
  const modulePath = `${path}/${dirent.name}`;
@@ -1,8 +1,13 @@
1
- export interface Config {
2
- server?: Server;
1
+ export interface DefaultConfig {
2
+ request: RequestConfig;
3
3
  }
4
4
 
5
- export interface Server {
6
- delay?: number; // Delay response (in ms)
5
+ export interface RequestConfig {
6
+ delay?: number | Interval; // Delay response (in ms), or an interval
7
7
  statusCode?: number; // Status code of response
8
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,4 +1,4 @@
1
- import { Config } from '../../src/models/config';
1
+ import { RequestConfig } from '../../src/models/config';
2
2
 
3
3
  export interface User {
4
4
  id: number;
@@ -21,9 +21,7 @@ export const data: User[] = [{
21
21
  creationDate: newDate()
22
22
  }];
23
23
 
24
- export const config: Config = {
25
- server: {
26
- delay: 2000,
27
- statusCode: 418
28
- }
24
+ export const config: RequestConfig = {
25
+ delay: 2000,
26
+ statusCode: 418
29
27
  }