typescript-mock-server 1.0.8 → 1.10.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/package.json +8 -8
- package/src/impl/typescript-mock-server-impl.ts +6 -2
- package/src/index.ts +2 -2
- package/src/typescript-mock-server.ts +1 -1
- package/tsconfig.json +4 -1
- package/src/index.js +0 -133
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typescript-mock-server",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.10.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",
|
|
@@ -30,17 +30,17 @@
|
|
|
30
30
|
"url": "https://genydev.nl"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@types/cors": "^2.8.
|
|
34
|
-
"prettier": "^3.
|
|
33
|
+
"@types/cors": "^2.8.19",
|
|
34
|
+
"prettier": "^3.6.2"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@types/express": "^
|
|
38
|
-
"@types/node": "^
|
|
37
|
+
"@types/express": "^5.0.5",
|
|
38
|
+
"@types/node": "^24.10.1",
|
|
39
39
|
"cors": "^2.8.5",
|
|
40
|
-
"express": "^
|
|
41
|
-
"ts-node-dev": "2.0.0",
|
|
40
|
+
"express": "^5.1.0",
|
|
42
41
|
"tslog": "^3.3.3",
|
|
43
|
-
"typescript": "^5.
|
|
42
|
+
"typescript": "^5.9.3",
|
|
43
|
+
"ts-node-dev": "2.0.0"
|
|
44
44
|
},
|
|
45
45
|
"prettier": {
|
|
46
46
|
"arrowParens": "avoid",
|
|
@@ -28,18 +28,22 @@ export class TypescriptMockServerImpl implements TypescriptMockServer{
|
|
|
28
28
|
return await import(moduleName);
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
public start() {
|
|
31
|
+
public async start() {
|
|
32
32
|
this.log.info(`basePath: ${this.basePath}`);
|
|
33
|
-
this.readRoutes(this.basePath).catch(error => this.log.error(error));
|
|
34
33
|
const port = this.commandLine.getCommand(Command.PORT) || 3000;
|
|
35
34
|
const corsSetting: CorsOptions = {
|
|
36
35
|
origin: this.commandLine.getCommand(Command.CORS) || '*'
|
|
37
36
|
};
|
|
38
37
|
|
|
39
38
|
this.app.use(cors(corsSetting))
|
|
39
|
+
await this.readRoutes(this.basePath).catch(error => this.log.error(error));
|
|
40
40
|
this.app.listen(port, () => {
|
|
41
41
|
this.log.info(`App is listening on port ${port}!`);
|
|
42
42
|
});
|
|
43
|
+
|
|
44
|
+
// add started endpoint
|
|
45
|
+
this.addEndpoint('state', 'get', { data: "{\"status\": \"started\"}" });
|
|
46
|
+
this.log.info(`Started mock server on port ${this.commandLine.getCommand(Command.PORT)}`);
|
|
43
47
|
}
|
|
44
48
|
|
|
45
49
|
private async readRoutes(path: string) {
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
#!./node_modules/.bin/ts-node
|
|
1
|
+
#!./node_modules/.bin/ts-node
|
|
2
2
|
|
|
3
3
|
import { TypescriptMockServer } from './typescript-mock-server';
|
|
4
4
|
import { TypescriptMockServerImpl } from './impl/typescript-mock-server-impl';
|
|
5
5
|
|
|
6
6
|
const server: TypescriptMockServer = new TypescriptMockServerImpl();
|
|
7
7
|
|
|
8
|
-
server.start();
|
|
8
|
+
server.start().then(() => console.log('Server started')).catch(console.error);
|
package/tsconfig.json
CHANGED
package/src/index.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
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
|
-
}
|