typescript-mock-server 0.0.7 → 0.0.8

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
@@ -12,6 +12,10 @@ Changes are being picked up automatically, so no need for a restart.
12
12
 
13
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).
14
14
 
15
+ # Options
16
+ `--port=x`: Port number the server runs on
17
+ `--path=x`: Path to your models
18
+
15
19
  ## Adding GET mocks/stubs
16
20
  Examples talk, so lets start with an example.
17
21
 
@@ -65,7 +69,7 @@ Following dependencies are being used:
65
69
  - @types/node
66
70
 
67
71
  ## Roadmap
68
- - [ ] Support other server port
72
+ - [x] Support other server port
69
73
  - [ ] Improve paths/way to start
70
74
  - [ ] Support different headers
71
75
  - [ ] Support all HTTP methods
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "typescript-mock-server",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
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
- "example": "ts-node-dev src/index.ts --path=tms-models",
7
+ "example": "ts-node-dev src/index.ts --path=tms-models --port=5000",
8
8
  "start": "ts-node-dev src/index.ts"
9
9
  },
10
10
  "repository": {
package/src/index.js ADDED
@@ -0,0 +1,133 @@
1
+ #!./../node_modules/.bin/ts-node-dev
2
+ "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
26
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
27
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
28
+ return new (P || (P = Promise))(function (resolve, reject) {
29
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
30
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
31
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
32
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
33
+ });
34
+ };
35
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
36
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
37
+ var m = o[Symbol.asyncIterator], i;
38
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
39
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
40
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
41
+ };
42
+ var __importDefault = (this && this.__importDefault) || function (mod) {
43
+ return (mod && mod.__esModule) ? mod : { "default": mod };
44
+ };
45
+ Object.defineProperty(exports, "__esModule", { value: true });
46
+ const express_1 = __importDefault(require("express"));
47
+ const fs = __importStar(require("fs"));
48
+ const baseDirPath = process.cwd();
49
+ console.log(baseDirPath);
50
+ const argv = (() => {
51
+ const args = {};
52
+ process.argv.slice(2).map((element) => {
53
+ const matches = element.match('--([a-zA-Z0-9]+)=(.*)');
54
+ if (matches) {
55
+ // @ts-ignore
56
+ args[matches[1]] = matches[2]
57
+ .replace(/^['"]/, '').replace(/['"]$/, '');
58
+ }
59
+ });
60
+ return args;
61
+ })();
62
+ console.log(argv);
63
+ // Create a new express app instance
64
+ const app = (0, express_1.default)();
65
+ // @ts-ignore
66
+ const args = argv['path'];
67
+ // @ts-ignore
68
+ const basePath = `${baseDirPath}/${args}`;
69
+ console.log('basePath:' + basePath);
70
+ function print(path) {
71
+ var e_1, _a;
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ console.log(path);
74
+ const dir = yield fs.promises.opendir(path);
75
+ try {
76
+ for (var dir_1 = __asyncValues(dir), dir_1_1; dir_1_1 = yield dir_1.next(), !dir_1_1.done;) {
77
+ const dirent = dir_1_1.value;
78
+ if (dirent.isDirectory()) {
79
+ yield print(`${path}/${dirent.name}`);
80
+ }
81
+ else {
82
+ handleFile(path, dirent);
83
+ }
84
+ }
85
+ }
86
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
87
+ finally {
88
+ try {
89
+ if (dir_1_1 && !dir_1_1.done && (_a = dir_1.return)) yield _a.call(dir_1);
90
+ }
91
+ finally { if (e_1) throw e_1.error; }
92
+ }
93
+ });
94
+ }
95
+ print(basePath).catch(console.error);
96
+ function loadModule(moduleName) {
97
+ return __awaiter(this, void 0, void 0, function* () {
98
+ return yield Promise.resolve().then(() => __importStar(require(moduleName)));
99
+ });
100
+ }
101
+ app.listen(3000, function () {
102
+ console.log('App is listening on port 3000!');
103
+ });
104
+ function handleFile(path, dirent) {
105
+ console.log('File name: ' + dirent.name);
106
+ if (dirent.name.startsWith('get')) {
107
+ handleGetRequest(path, dirent);
108
+ }
109
+ }
110
+ function addEndpoint(endpoint, model) {
111
+ app.get(endpoint, function (req, res) {
112
+ res.send(model.data);
113
+ });
114
+ }
115
+ function handleGetRequest(path, dirent) {
116
+ console.log('Adding GET request');
117
+ const endpoint = convertFileNameToEndpoint(path, dirent);
118
+ console.log('Endpoint: ' + endpoint);
119
+ const modulePath = `${path}/${dirent.name}`;
120
+ console.log('Resolve module: ' + modulePath);
121
+ loadModule(modulePath)
122
+ .then(model => addEndpoint(endpoint, model))
123
+ .catch(err => console.error(err));
124
+ }
125
+ function convertFileNameToEndpoint(path, dirent) {
126
+ let endpoint = `${path.replace(basePath, '')}/${dirent.name}`;
127
+ endpoint = endpoint.replace('.ts', '');
128
+ endpoint = endpoint.replace('get', '');
129
+ if (endpoint !== '') {
130
+ endpoint = endpoint.replace('-', '');
131
+ }
132
+ return endpoint;
133
+ }
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- #!./../node_modules/.bin/ts-node-dev
1
+ #!./node_modules/.bin/ts-node-dev
2
2
 
3
3
  import express, { Express } from 'express';
4
4
  import * as fs from 'fs';
@@ -6,7 +6,7 @@ import * as fs from 'fs';
6
6
  const baseDirPath = process.cwd();
7
7
  console.log(baseDirPath);
8
8
 
9
- const argv = (() => {
9
+ const argv: { [key: string]: string } = (() => {
10
10
  const args = {};
11
11
  process.argv.slice(2).map((element) => {
12
12
  const matches = element.match('--([a-zA-Z0-9]+)=(.*)');
@@ -24,11 +24,10 @@ console.log(argv);
24
24
  // Create a new express app instance
25
25
  const app: Express = express();
26
26
 
27
- // @ts-ignore
28
- const args = argv['path'];
27
+ const {path, port} = argv;
29
28
 
30
29
  // @ts-ignore
31
- const basePath = `${baseDirPath}/${args}`;
30
+ const basePath = `${baseDirPath}/${path}`;
32
31
 
33
32
  console.log('basePath:' + basePath);
34
33
 
@@ -50,8 +49,8 @@ async function loadModule(moduleName: string) {
50
49
  return await import(moduleName);
51
50
  }
52
51
 
53
- app.listen(3000, function() {
54
- console.log('App is listening on port 3000!');
52
+ app.listen(port || 3000, function() {
53
+ console.log(`App is listening on port ${port || 3000}!`);
55
54
  });
56
55
 
57
56
  function handleFile(path: string, dirent: fs.Dirent) {
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.data = void 0;
4
+ exports.data = {
5
+ id: 1,
6
+ firstName: 'Guy',
7
+ lastName: 'Theuws'
8
+ };
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.data = exports.newDate = void 0;
4
+ const newDate = () => new Date();
5
+ exports.newDate = newDate;
6
+ exports.data = [{
7
+ id: 1,
8
+ firstName: 'Guy',
9
+ lastName: 'Theuws',
10
+ creationDate: (0, exports.newDate)()
11
+ }, {
12
+ id: 2,
13
+ firstName: 'Generation Y',
14
+ lastName: 'Development',
15
+ creationDate: (0, exports.newDate)()
16
+ }];
@@ -5,7 +5,7 @@ export interface User {
5
5
  creationDate: Date;
6
6
  }
7
7
 
8
- const newDate = () => new Date();
8
+ export const newDate = () => new Date();
9
9
 
10
10
  export const data: User[] = [{
11
11
  id: 1,
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.data = void 0;
4
+ const get_1 = require("../get");
5
+ exports.data = {
6
+ id: 1, title: 'Hello profile', user: {
7
+ id: 1,
8
+ firstName: 'Guy',
9
+ lastName: 'Theuws',
10
+ creationDate: (0, get_1.newDate)()
11
+ }
12
+ };
@@ -1,4 +1,4 @@
1
- import { User } from '../get';
1
+ import { newDate, User } from '../get';
2
2
 
3
3
  interface Profile {
4
4
  id: number;
@@ -10,6 +10,7 @@ export const data: Profile = {
10
10
  id: 1, title: 'Hello profile', user: {
11
11
  id: 1,
12
12
  firstName: 'Guy',
13
- lastName: 'Theuws'
13
+ lastName: 'Theuws',
14
+ creationDate: newDate()
14
15
  }
15
16
  };
package/nodemon.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "watch": [
3
- "src",
4
- "tms-models"
5
- ],
6
- "ext": ".ts,.js",
7
- "ignore": [],
8
- "exec": "ts-node ./src/index.ts"
9
- }