typescript-mock-server 0.0.1 → 0.0.4
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 +4 -6
- package/src/index.ts +67 -67
- package/src/index.js +0 -162
package/package.json
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typescript-mock-server",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
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
|
-
"main": "src/index.ts",
|
|
6
|
-
"bin": "ts-node-dev src/index.ts",
|
|
7
5
|
"scripts": {
|
|
8
6
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
9
7
|
"start": "ts-node-dev src/index.ts --path=tms-models"
|
|
@@ -20,14 +18,14 @@
|
|
|
20
18
|
},
|
|
21
19
|
"homepage": "https://github.com/GuyT07/typescript-mock-server#readme",
|
|
22
20
|
"devDependencies": {
|
|
23
|
-
"@types/express": "^4.17.9",
|
|
24
|
-
"@types/node": "^14.18.12",
|
|
25
21
|
"prettier": "^2.6.2"
|
|
26
22
|
},
|
|
27
23
|
"dependencies": {
|
|
28
24
|
"express": "^4.17.3",
|
|
29
25
|
"ts-node-dev": "^1.1.8",
|
|
30
|
-
"typescript": "^4.0.5"
|
|
26
|
+
"typescript": "^4.0.5",
|
|
27
|
+
"@types/express": "^4.17.9",
|
|
28
|
+
"@types/node": "^14.18.12"
|
|
31
29
|
},
|
|
32
30
|
"prettier": {
|
|
33
31
|
"arrowParens": "avoid",
|
package/src/index.ts
CHANGED
|
@@ -4,87 +4,87 @@ import express from 'express';
|
|
|
4
4
|
import { Express } from 'express';
|
|
5
5
|
import * as fs from 'fs';
|
|
6
6
|
|
|
7
|
-
const baseDirPath = process.cwd()
|
|
8
|
-
console.log(baseDirPath);
|
|
9
|
-
|
|
10
|
-
const argv = (() => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
})();
|
|
22
|
-
|
|
23
|
-
console.log(argv);
|
|
7
|
+
const baseDirPath = process.cwd()
|
|
8
|
+
console.log(baseDirPath);
|
|
9
|
+
|
|
10
|
+
const argv = (() => {
|
|
11
|
+
const args = {};
|
|
12
|
+
process.argv.slice(2).map((element) => {
|
|
13
|
+
const matches = element.match('--([a-zA-Z0-9]+)=(.*)');
|
|
14
|
+
if (matches) {
|
|
15
|
+
// @ts-ignore
|
|
16
|
+
args[matches[1]] = matches[2]
|
|
17
|
+
.replace(/^['"]/, '').replace(/['"]$/, '');
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
return args;
|
|
21
|
+
})();
|
|
22
|
+
|
|
23
|
+
console.log(argv);
|
|
24
24
|
|
|
25
25
|
// Create a new express app instance
|
|
26
|
-
const app: Express = express();
|
|
26
|
+
const app: Express = express();
|
|
27
27
|
|
|
28
28
|
// @ts-ignore
|
|
29
|
-
const args = argv['path'];
|
|
29
|
+
const args = argv['path'];
|
|
30
30
|
|
|
31
31
|
// @ts-ignore
|
|
32
|
-
const basePath = `${baseDirPath}/${args}`;
|
|
33
|
-
|
|
34
|
-
console.log('basePath:' + basePath);
|
|
35
|
-
|
|
36
|
-
async function print(path: string) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
32
|
+
const basePath = `${baseDirPath}/${args}`;
|
|
33
|
+
|
|
34
|
+
console.log('basePath:' + basePath);
|
|
35
|
+
|
|
36
|
+
async function print(path: string) {
|
|
37
|
+
console.log(path);
|
|
38
|
+
const dir = await fs.promises.opendir(path);
|
|
39
|
+
for await (const dirent of dir) {
|
|
40
|
+
if (dirent.isDirectory()) {
|
|
41
|
+
await print(`${path}/${dirent.name}`);
|
|
42
|
+
} else {
|
|
43
|
+
handleFile(path, dirent);
|
|
44
|
+
}
|
|
44
45
|
}
|
|
45
46
|
}
|
|
46
|
-
}
|
|
47
47
|
|
|
48
|
-
print(basePath).catch(console.error);
|
|
48
|
+
print(basePath).catch(console.error);
|
|
49
49
|
|
|
50
|
-
async function loadModule(moduleName: string) {
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
app.listen(3000, function () {
|
|
55
|
-
console.log('App is listening on port 3000!');
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
function handleFile(path: string, dirent: fs.Dirent) {
|
|
59
|
-
console.log('File name: ' + dirent.name);
|
|
60
|
-
if (dirent.name.startsWith('get')) {
|
|
61
|
-
handleGetRequest(path, dirent);
|
|
50
|
+
async function loadModule(moduleName: string) {
|
|
51
|
+
return await import(moduleName);
|
|
62
52
|
}
|
|
63
|
-
}
|
|
64
53
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
res.send(model.data)
|
|
54
|
+
app.listen(3000, function() {
|
|
55
|
+
console.log('App is listening on port 3000!');
|
|
68
56
|
});
|
|
69
|
-
}
|
|
70
57
|
|
|
71
|
-
function
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
loadModule(modulePath)
|
|
78
|
-
.then(model => addEndpoint(endpoint, model))
|
|
79
|
-
.catch(err => console.error(err));
|
|
80
|
-
}
|
|
58
|
+
function handleFile(path: string, dirent: fs.Dirent) {
|
|
59
|
+
console.log('File name: ' + dirent.name);
|
|
60
|
+
if (dirent.name.startsWith('get')) {
|
|
61
|
+
handleGetRequest(path, dirent);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
81
64
|
|
|
82
|
-
function
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (endpoint !== '') {
|
|
87
|
-
endpoint = endpoint.replace('-', '');
|
|
65
|
+
function addEndpoint(endpoint: string, model: any) {
|
|
66
|
+
app.get(endpoint, function(req, res) {
|
|
67
|
+
res.send(model.data)
|
|
68
|
+
});
|
|
88
69
|
}
|
|
89
|
-
|
|
70
|
+
|
|
71
|
+
function handleGetRequest(path: string, dirent: fs.Dirent) {
|
|
72
|
+
console.log('Adding GET request');
|
|
73
|
+
const endpoint = convertFileNameToEndpoint(path, dirent);
|
|
74
|
+
console.log('Endpoint: ' + endpoint);
|
|
75
|
+
const modulePath = `${path}/${dirent.name}`;
|
|
76
|
+
console.log('Resolve module: ' + modulePath);
|
|
77
|
+
loadModule(modulePath)
|
|
78
|
+
.then(model => addEndpoint(endpoint, model))
|
|
79
|
+
.catch(err => console.error(err));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function convertFileNameToEndpoint(path: string, dirent: fs.Dirent): string {
|
|
83
|
+
let endpoint = `${path.replace(basePath, '')}/${dirent.name}`;
|
|
84
|
+
endpoint = endpoint.replace('.ts', '');
|
|
85
|
+
endpoint = endpoint.replace('get', '');
|
|
86
|
+
if (endpoint !== '') {
|
|
87
|
+
endpoint = endpoint.replace('-', '');
|
|
88
|
+
}
|
|
89
|
+
return endpoint;
|
|
90
90
|
}
|
package/src/index.js
DELETED
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
#!./../node_modules/.bin/ts-node
|
|
2
|
-
"use strict";
|
|
3
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
5
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
6
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
7
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
8
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
9
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
|
-
});
|
|
11
|
-
};
|
|
12
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
13
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
14
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
15
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
16
|
-
function step(op) {
|
|
17
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
18
|
-
while (_) try {
|
|
19
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
20
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
21
|
-
switch (op[0]) {
|
|
22
|
-
case 0: case 1: t = op; break;
|
|
23
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
24
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
25
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
26
|
-
default:
|
|
27
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
28
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
29
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
30
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
31
|
-
if (t[2]) _.ops.pop();
|
|
32
|
-
_.trys.pop(); continue;
|
|
33
|
-
}
|
|
34
|
-
op = body.call(thisArg, _);
|
|
35
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
36
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
40
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
41
|
-
var m = o[Symbol.asyncIterator], i;
|
|
42
|
-
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);
|
|
43
|
-
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); }); }; }
|
|
44
|
-
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
45
|
-
};
|
|
46
|
-
exports.__esModule = true;
|
|
47
|
-
var express_1 = require("express");
|
|
48
|
-
var fs = require("fs");
|
|
49
|
-
var baseDirPath = process.cwd();
|
|
50
|
-
console.log(baseDirPath);
|
|
51
|
-
var argv = (function () {
|
|
52
|
-
var args = {};
|
|
53
|
-
process.argv.slice(2).map(function (element) {
|
|
54
|
-
var matches = element.match('--([a-zA-Z0-9]+)=(.*)');
|
|
55
|
-
if (matches) {
|
|
56
|
-
// @ts-ignore
|
|
57
|
-
args[matches[1]] = matches[2]
|
|
58
|
-
.replace(/^['"]/, '').replace(/['"]$/, '');
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
return args;
|
|
62
|
-
})();
|
|
63
|
-
console.log(argv);
|
|
64
|
-
// Create a new express app instance
|
|
65
|
-
var app = (0, express_1["default"])();
|
|
66
|
-
// @ts-ignore
|
|
67
|
-
var args = argv['path'];
|
|
68
|
-
// @ts-ignore
|
|
69
|
-
var basePath = "".concat(baseDirPath, "/").concat(argv['path']);
|
|
70
|
-
console.log('basePath:' + basePath);
|
|
71
|
-
function print(path) {
|
|
72
|
-
var e_1, _a;
|
|
73
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
74
|
-
var dir, dir_1, dir_1_1, dirent, e_1_1;
|
|
75
|
-
return __generator(this, function (_b) {
|
|
76
|
-
switch (_b.label) {
|
|
77
|
-
case 0: return [4 /*yield*/, fs.promises.opendir(args)];
|
|
78
|
-
case 1:
|
|
79
|
-
dir = _b.sent();
|
|
80
|
-
_b.label = 2;
|
|
81
|
-
case 2:
|
|
82
|
-
_b.trys.push([2, 9, 10, 15]);
|
|
83
|
-
dir_1 = __asyncValues(dir);
|
|
84
|
-
_b.label = 3;
|
|
85
|
-
case 3: return [4 /*yield*/, dir_1.next()];
|
|
86
|
-
case 4:
|
|
87
|
-
if (!(dir_1_1 = _b.sent(), !dir_1_1.done)) return [3 /*break*/, 8];
|
|
88
|
-
dirent = dir_1_1.value;
|
|
89
|
-
if (!dirent.isDirectory()) return [3 /*break*/, 6];
|
|
90
|
-
return [4 /*yield*/, print("".concat(path, "/").concat(dirent.name))];
|
|
91
|
-
case 5:
|
|
92
|
-
_b.sent();
|
|
93
|
-
return [3 /*break*/, 7];
|
|
94
|
-
case 6:
|
|
95
|
-
handleFile(path, dirent);
|
|
96
|
-
_b.label = 7;
|
|
97
|
-
case 7: return [3 /*break*/, 3];
|
|
98
|
-
case 8: return [3 /*break*/, 15];
|
|
99
|
-
case 9:
|
|
100
|
-
e_1_1 = _b.sent();
|
|
101
|
-
e_1 = { error: e_1_1 };
|
|
102
|
-
return [3 /*break*/, 15];
|
|
103
|
-
case 10:
|
|
104
|
-
_b.trys.push([10, , 13, 14]);
|
|
105
|
-
if (!(dir_1_1 && !dir_1_1.done && (_a = dir_1["return"]))) return [3 /*break*/, 12];
|
|
106
|
-
return [4 /*yield*/, _a.call(dir_1)];
|
|
107
|
-
case 11:
|
|
108
|
-
_b.sent();
|
|
109
|
-
_b.label = 12;
|
|
110
|
-
case 12: return [3 /*break*/, 14];
|
|
111
|
-
case 13:
|
|
112
|
-
if (e_1) throw e_1.error;
|
|
113
|
-
return [7 /*endfinally*/];
|
|
114
|
-
case 14: return [7 /*endfinally*/];
|
|
115
|
-
case 15: return [2 /*return*/];
|
|
116
|
-
}
|
|
117
|
-
});
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
print(basePath)["catch"](console.error);
|
|
121
|
-
function loadModule(moduleName) {
|
|
122
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
123
|
-
return __generator(this, function (_a) {
|
|
124
|
-
switch (_a.label) {
|
|
125
|
-
case 0: return [4 /*yield*/, Promise.resolve().then(function () { return require(moduleName); })];
|
|
126
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
app.listen(3000, function () {
|
|
132
|
-
console.log('App is listening on port 3000!');
|
|
133
|
-
});
|
|
134
|
-
function handleFile(path, dirent) {
|
|
135
|
-
console.log('File name: ' + dirent.name);
|
|
136
|
-
if (dirent.name.startsWith('get')) {
|
|
137
|
-
handleGetRequest(path, dirent);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
function addEndpoint(endpoint, model) {
|
|
141
|
-
app.get(endpoint, function (req, res) {
|
|
142
|
-
res.send(model.data);
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
function handleGetRequest(path, dirent) {
|
|
146
|
-
console.log('Adding GET request');
|
|
147
|
-
var endpoint = convertFileNameToEndpoint(path, dirent);
|
|
148
|
-
console.log('Endpoint: ' + endpoint);
|
|
149
|
-
var modulePath = "".concat(basePath, "/").concat(dirent.name);
|
|
150
|
-
console.log('Resolve module: ' + modulePath);
|
|
151
|
-
loadModule(modulePath)
|
|
152
|
-
.then(function (model) { return addEndpoint(endpoint, model); })["catch"](function (err) { return console.error(err); });
|
|
153
|
-
}
|
|
154
|
-
function convertFileNameToEndpoint(path, dirent) {
|
|
155
|
-
var endpoint = "".concat(path.replace(basePath, ''), "/").concat(dirent.name);
|
|
156
|
-
endpoint = endpoint.replace('.ts', '');
|
|
157
|
-
endpoint = endpoint.replace('get', '');
|
|
158
|
-
if (endpoint !== '') {
|
|
159
|
-
endpoint = endpoint.replace('-', '');
|
|
160
|
-
}
|
|
161
|
-
return endpoint;
|
|
162
|
-
}
|