typescript-mock-server 0.0.11 → 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
@@ -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,20 @@ 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.0 - Breaking change: renamed Server config to Request and added interval for delay
88
96
  - v0.0.11 - Add items to roadmap, bug fixes
89
97
  - v0.0.10 - Support multiple http verbs
90
98
 
package/package.json CHANGED
@@ -1,35 +1,44 @@
1
1
  {
2
2
  "name": "typescript-mock-server",
3
- "version": "0.0.11",
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
- "start": "ts-node-dev src/index.ts"
8
+ "start": "ts-node-dev src/index.ts",
9
+ "update-deps": "npm update",
10
+ "get-version": "echo $npm_package_version",
11
+ "publish-to-npm": "git tag -a ${npm_package_version} -m \"v${npm_package_version}\" && npm publish"
9
12
  },
10
13
  "repository": {
11
14
  "type": "git",
12
15
  "url": "git+https://github.com/GuyT07/typescript-mock-server.git"
13
16
  },
14
- "keywords": ["mock server", "testing", "stub", "typescript"],
17
+ "keywords": [
18
+ "mock server",
19
+ "testing",
20
+ "stub",
21
+ "typescript"
22
+ ],
15
23
  "author": "Guy Theuws",
16
24
  "license": "ISC",
17
25
  "bugs": {
18
26
  "url": "https://github.com/GuyT07/typescript-mock-server/issues"
19
27
  },
20
- "homepage": "https://github.com/GuyT07/typescript-mock-server#readme",
28
+ "homepage": "https://genydev.nl",
21
29
  "funding": {
22
- "url" : "https://genydev.nl"
30
+ "url": "https://genydev.nl"
23
31
  },
24
32
  "devDependencies": {
25
33
  "prettier": "^2.6.2"
26
34
  },
27
35
  "dependencies": {
28
- "express": "^4.17.3",
29
- "ts-node-dev": "^1.1.8",
30
- "typescript": "^4.0.5",
31
- "@types/express": "^4.17.9",
32
- "@types/node": "^14.18.12"
36
+ "@types/express": "^4.17.13",
37
+ "@types/node": "^18.0.5",
38
+ "express": "^4.18.1",
39
+ "ts-node-dev": "2.0.0",
40
+ "tslog": "^3.3.3",
41
+ "typescript": "^4.7.4"
33
42
  },
34
43
  "prettier": {
35
44
  "arrowParens": "avoid",
@@ -0,0 +1,9 @@
1
+ export interface CommandLine {
2
+ getCommands(): Map<Command, string>;
3
+ getCommand(command: Command): string | undefined;
4
+ }
5
+
6
+ export enum Command {
7
+ PATH = 'path',
8
+ PORT = 'port'
9
+ }
@@ -0,0 +1,33 @@
1
+ import { Command, CommandLine } from '../command-line';
2
+ import { Logger } from '../logger';
3
+ import { LoggerImpl } from './logger-impl';
4
+
5
+ export class CommandLineImpl implements CommandLine {
6
+
7
+ private arguments: Map<Command, string> = new Map<Command, string>();
8
+ private log: Logger = new LoggerImpl();
9
+
10
+ constructor() {
11
+ this.parseCommandLineArguments();
12
+ }
13
+
14
+ getCommands(): Map<Command, string> {
15
+ return this.arguments;
16
+ }
17
+
18
+ getCommand(command: Command): string | undefined {
19
+ return this.arguments.get(command);
20
+ }
21
+
22
+ private parseCommandLineArguments(): void {
23
+ process.argv.slice(2).map((element) => {
24
+ const matches = element.match('--([a-zA-Z0-9]+)=(.*)');
25
+ if (matches) {
26
+ const value = matches[2].replace(/^['"]/, '').replace(/['"]$/, '');
27
+ this.arguments.set(matches[1] as Command, value);
28
+ }
29
+ });
30
+
31
+ this.log.debug(`Passed arguments ${[...this.arguments.keys()].join(',')}`);
32
+ }
33
+ }
@@ -0,0 +1,39 @@
1
+ import { Logger as LoggerInterface } from '../logger';
2
+ import { Logger } from 'tslog';
3
+
4
+ export class LoggerImpl implements LoggerInterface {
5
+
6
+ private readonly log: Logger;
7
+
8
+ constructor() {
9
+ this.log = new Logger({ignoreStackLevels: 4, displayFunctionName: false});
10
+ }
11
+
12
+ private static getArgumentsToPass(args: unknown[]): unknown | unknown[] {
13
+ return LoggerImpl.getNumberOfArguments(args) === 1 ? args[0] : args;
14
+ }
15
+
16
+ private static getNumberOfArguments(args: unknown[]): number {
17
+ return args.length;
18
+ }
19
+
20
+ debug(...args: unknown[]): void {
21
+ this.log.debug(LoggerImpl.getArgumentsToPass(args));
22
+ }
23
+
24
+ error(...args: unknown[]): void {
25
+ this.log.error(LoggerImpl.getArgumentsToPass(args));
26
+ }
27
+
28
+ trace(...args: unknown[]): void {
29
+ this.log.trace(LoggerImpl.getArgumentsToPass(args));
30
+ }
31
+
32
+ warn(...args: unknown[]): void {
33
+ this.log.warn(LoggerImpl.getArgumentsToPass(args));
34
+ }
35
+
36
+ info(...args: unknown[]): void {
37
+ this.log.info(LoggerImpl.getArgumentsToPass(args));
38
+ }
39
+ }
@@ -0,0 +1,105 @@
1
+ import express, { Express } from 'express';
2
+ import { CommandLineImpl } from './command-line-impl';
3
+ import { Command, CommandLine } from '../command-line';
4
+ import { RegisteredEndpoint } from '../models/registered-endpoint';
5
+ import { HttpVerb } from '../types/http-verbs';
6
+ import { Dirent } from 'fs';
7
+ import { opendir } from 'fs/promises';
8
+ import { LoggerImpl } from './logger-impl';
9
+ import { Logger } from '../logger';
10
+ import { TypescriptMockServer } from '../typescript-mock-server';
11
+ import { Interval } from '../models/config';
12
+
13
+ export class TypescriptMockServerImpl implements TypescriptMockServer{
14
+
15
+ private readonly log: Logger = new LoggerImpl();
16
+ private readonly commandLine: CommandLine = new CommandLineImpl();
17
+ private readonly app: Express;
18
+ private readonly basePath;
19
+ private registeredEndpoints: RegisteredEndpoint[] = [];
20
+
21
+ constructor() {
22
+ this.app = express();
23
+ this.basePath = this.getPath();
24
+ }
25
+
26
+ private static async loadModule(moduleName: string) {
27
+ return await import(moduleName);
28
+ }
29
+
30
+ public start() {
31
+ this.log.info(`basePath: ${this.basePath}`);
32
+ this.readRoutes(this.basePath).catch(error => this.log.error(error));
33
+ const port = this.commandLine.getCommand(Command.PORT) || 3000;
34
+ this.app.listen(port, () => {
35
+ this.log.info(`App is listening on port ${port}!`);
36
+ });
37
+ }
38
+
39
+ private async readRoutes(path: string) {
40
+ const dir = await opendir(path);
41
+ for await (const dirent of dir) {
42
+ if (dirent.isDirectory()) {
43
+ await this.readRoutes(`${path}/${dirent.name}`);
44
+ } else {
45
+ this.handleFile(path, dirent);
46
+ }
47
+ }
48
+ this.registeredEndpoints.forEach(endpoint => this.log.info(`${endpoint.httpVerb.toUpperCase()} http://localhost:${this.commandLine.getCommand(Command.PORT)}${endpoint.endpoint}`));
49
+ }
50
+
51
+ private handleFile(path: string, dirent: Dirent) {
52
+ const httpVerb = (dirent.name.indexOf('-') > -1 ? dirent.name.split('-')[0] : dirent.name.split('.')[0]) as HttpVerb;
53
+ this.handleRequest(path, dirent, httpVerb);
54
+ }
55
+
56
+ private addEndpoint(endpoint: string, httpVerb: HttpVerb, model: any) {
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;
76
+ }
77
+
78
+ private handleRequest(path: string, dirent: Dirent, httpVerb: HttpVerb) {
79
+ const endpoint = this.convertFileNameToEndpoint(path, dirent, httpVerb);
80
+ const modulePath = `${path}/${dirent.name}`;
81
+ this.registeredEndpoints.push({ httpVerb, endpoint });
82
+ TypescriptMockServerImpl.loadModule(modulePath)
83
+ .then(model => this.addEndpoint(endpoint, httpVerb, model))
84
+ .catch(error => this.log.error(error));
85
+ }
86
+
87
+ private convertFileNameToEndpoint(path: string, dirent: Dirent, httpVerb: HttpVerb): string {
88
+ const endpoint = `${path.replace(this.basePath, '')}/${dirent.name}`
89
+ .replace('.ts', '')
90
+ .replace(httpVerb, '')
91
+ .replace('-', '');
92
+ if (endpoint.endsWith('/')) {
93
+ return endpoint.substring(0, endpoint.length - 1);
94
+ }
95
+ return endpoint;
96
+ }
97
+
98
+ private getPath(): string {
99
+ if (!this.commandLine.getCommands().has(Command.PATH)) {
100
+ this.log.warn(`Path parameter not set, fallback to default tms-models`);
101
+ return 'tms-models';
102
+ }
103
+ return `${process.cwd()}/${this.commandLine.getCommand(Command.PATH)}`;
104
+ }
105
+ }
package/src/index.ts CHANGED
@@ -1,88 +1,8 @@
1
1
  #!./node_modules/.bin/ts-node-dev
2
2
 
3
- import express, { Express } from 'express';
4
- import * as fs from 'fs';
3
+ import { TypescriptMockServer } from './typescript-mock-server';
4
+ import { TypescriptMockServerImpl } from './impl/typescript-mock-server-impl';
5
5
 
6
- type HttpVerb = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
6
+ const server: TypescriptMockServer = new TypescriptMockServerImpl();
7
7
 
8
- const argv: { [key: string]: string } = (() => {
9
- const args = {};
10
- process.argv.slice(2).map((element) => {
11
- const matches = element.match('--([a-zA-Z0-9]+)=(.*)');
12
- if (matches) {
13
- // @ts-ignore
14
- args[matches[1]] = matches[2]
15
- .replace(/^['"]/, '').replace(/['"]$/, '');
16
- }
17
- });
18
- return args;
19
- })();
20
-
21
- console.log(`Passed arguments %o`, argv);
22
-
23
- // Create a new express app instance
24
- const app: Express = express();
25
-
26
- const { path, port } = argv;
27
-
28
- const basePath = `${process.cwd()}/${path}`;
29
-
30
- interface RegisteredEndpoint {
31
- httpVerb: string;
32
- endpoint: string;
33
- }
34
-
35
- const registeredEndpoints: RegisteredEndpoint[] = [];
36
-
37
- console.log('basePath:' + basePath);
38
-
39
- async function readRoutes(path: string) {
40
- const dir = await fs.promises.opendir(path);
41
- for await (const dirent of dir) {
42
- if (dirent.isDirectory()) {
43
- await readRoutes(`${path}/${dirent.name}`);
44
- } else {
45
- handleFile(path, dirent);
46
- }
47
- }
48
- registeredEndpoints.forEach(endpoint => console.log(`${endpoint.httpVerb.toUpperCase()} http://localhost:${port}${endpoint.endpoint}`))
49
- }
50
-
51
- readRoutes(basePath).catch(console.error);
52
-
53
- app.listen(port || 3000, function() {
54
- console.log(`App is listening on port ${port || 3000}!`);
55
- });
56
-
57
- async function loadModule(moduleName: string) {
58
- return await import(moduleName);
59
- }
60
-
61
- function handleFile(path: string, dirent: fs.Dirent) {
62
- const httpVerb = (dirent.name.indexOf('-') > -1 ? dirent.name.split('-')[0] : dirent.name.split('.')[0]) as HttpVerb;
63
- handleRequest(path, dirent, httpVerb);
64
- }
65
-
66
- function addEndpoint(endpoint: string, httpVerb: HttpVerb, model: any) {
67
- app[httpVerb](endpoint, (req, res) => res.send(model.data));
68
- }
69
-
70
- function handleRequest(path: string, dirent: fs.Dirent, httpVerb: HttpVerb) {
71
- const endpoint = convertFileNameToEndpoint(path, dirent, httpVerb);
72
- const modulePath = `${path}/${dirent.name}`;
73
- registeredEndpoints.push({httpVerb, endpoint});
74
- loadModule(modulePath)
75
- .then(model => addEndpoint(endpoint, httpVerb, model))
76
- .catch(err => console.error(err));
77
- }
78
-
79
- function convertFileNameToEndpoint(path: string, dirent: fs.Dirent, httpVerb: HttpVerb): string {
80
- const endpoint = `${path.replace(basePath, '')}/${dirent.name}`
81
- .replace('.ts', '')
82
- .replace(httpVerb, '')
83
- .replace('-', '');
84
- if (endpoint.endsWith('/')) {
85
- return endpoint.substring(0, endpoint.length - 1);
86
- }
87
- return endpoint;
88
- }
8
+ server.start();
package/src/logger.ts ADDED
@@ -0,0 +1,7 @@
1
+ export interface Logger {
2
+ debug(...args: unknown[]): void;
3
+ trace(...args: unknown[]): void;
4
+ warn(...args: unknown[]): void;
5
+ error(...args: unknown[]): void;
6
+ info(...args: unknown[]): void
7
+ }
@@ -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
+ }
@@ -0,0 +1,4 @@
1
+ export interface RegisteredEndpoint {
2
+ httpVerb: string;
3
+ endpoint: string;
4
+ }
@@ -0,0 +1 @@
1
+ export type HttpVerb = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
@@ -0,0 +1,3 @@
1
+ export interface TypescriptMockServer {
2
+ start(): void;
3
+ }
@@ -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
+ }