typescript-mock-server 1.11.2 → 1.11.5
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
|
@@ -4,10 +4,37 @@ This makes it easier to maintain your mocks because you can load your own models
|
|
|
4
4
|
have to update your mock, otherwise you will receive compile errors.
|
|
5
5
|
|
|
6
6
|
# Quickstart
|
|
7
|
-
The easiest way to
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
The easiest way to use this tool is by installing it globally:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install -g typescript-mock-server
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Then you can run it from anywhere using:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
typescript-mock-server --path=./tms-models
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Alternatively, you can install it as a (dev)dependency:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install --save-dev typescript-mock-server
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
And add a script to your `package.json`:
|
|
26
|
+
|
|
27
|
+
```json
|
|
28
|
+
"scripts": {
|
|
29
|
+
"mock": "typescript-mock-server --path=tms-models"
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Or run it directly using `npx`:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npx typescript-mock-server --path=tms-models
|
|
37
|
+
```
|
|
11
38
|
|
|
12
39
|
Check out the [working example project](https://github.com/GuyT07/typescript-mock-server-examle) and [the source](https://github.com/GuyT07/typescript-mock-server/tree/main/tms-models/users).
|
|
13
40
|
|
|
@@ -99,6 +126,10 @@ Following dependencies are being used:
|
|
|
99
126
|
|
|
100
127
|
|
|
101
128
|
## Release notes (will be moved to GitHub in the future)
|
|
129
|
+
- v1.11.5 - Ensured binary has executable permissions and updated README with better Quickstart instructions.
|
|
130
|
+
- v1.11.4 - Fixed "models not found" error when running as a dependency by removing incorrect path mapping for compiled models and relying on ts-node for raw TypeScript files.
|
|
131
|
+
- v1.11.3 - Fixed module resolution when used as a dependency by explicitly adding ts-node as a dependency and improving its configuration at runtime.
|
|
132
|
+
- v1.11.1 - Added binary support to the package, allowing it to be executed as a command-line tool when installed as a dependency.
|
|
102
133
|
- v1.11.0 - Support dynamic responses via request/response context, added comprehensive tests, and switched to a faster build-and-serve workflow
|
|
103
134
|
- v1.10.0 - Improved path resolution and library usage support
|
|
104
135
|
- v1.0.8 - Minor bugfixes
|
|
@@ -78,7 +78,15 @@ class TypescriptMockServerImpl {
|
|
|
78
78
|
static loadModule(moduleName) {
|
|
79
79
|
return __awaiter(this, void 0, void 0, function* () {
|
|
80
80
|
if (moduleName.endsWith('.ts')) {
|
|
81
|
-
require('ts-node').register({
|
|
81
|
+
require('ts-node').register({
|
|
82
|
+
transpileOnly: true,
|
|
83
|
+
compilerOptions: {
|
|
84
|
+
module: 'commonjs',
|
|
85
|
+
allowJs: true,
|
|
86
|
+
esModuleInterop: true,
|
|
87
|
+
resolveJsonModule: true
|
|
88
|
+
}
|
|
89
|
+
});
|
|
82
90
|
}
|
|
83
91
|
return yield Promise.resolve(`${moduleName}`).then(s => __importStar(require(s)));
|
|
84
92
|
});
|
|
@@ -170,16 +178,6 @@ class TypescriptMockServerImpl {
|
|
|
170
178
|
const endpoint = this.convertFileNameToEndpoint(dirPath, dirent, httpVerb);
|
|
171
179
|
let modulePath = `${dirPath}/${dirent.name}`;
|
|
172
180
|
this.registeredEndpoints.push({ httpVerb, endpoint });
|
|
173
|
-
if (__filename.endsWith('.js') && modulePath.endsWith('.ts')) {
|
|
174
|
-
const distPath = path_1.default.join(process.cwd(), 'dist');
|
|
175
|
-
const potentialJsPath = modulePath
|
|
176
|
-
.replace(process.cwd(), distPath)
|
|
177
|
-
.replace(/\.ts$/, '.js');
|
|
178
|
-
const fs = require('fs');
|
|
179
|
-
if (fs.existsSync(potentialJsPath)) {
|
|
180
|
-
modulePath = potentialJsPath;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
181
|
yield TypescriptMockServerImpl.loadModule(modulePath)
|
|
184
182
|
.then(model => this.addEndpoint(endpoint, httpVerb, model))
|
|
185
183
|
.catch(error => this.log.error(error));
|
|
@@ -77,6 +77,17 @@ class TypescriptMockServerImpl {
|
|
|
77
77
|
}
|
|
78
78
|
static loadModule(moduleName) {
|
|
79
79
|
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
+
if (moduleName.endsWith('.ts')) {
|
|
81
|
+
require('ts-node').register({
|
|
82
|
+
transpileOnly: true,
|
|
83
|
+
compilerOptions: {
|
|
84
|
+
module: 'commonjs',
|
|
85
|
+
allowJs: true,
|
|
86
|
+
esModuleInterop: true,
|
|
87
|
+
resolveJsonModule: true
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
80
91
|
return yield Promise.resolve(`${moduleName}`).then(s => __importStar(require(s)));
|
|
81
92
|
});
|
|
82
93
|
}
|
|
@@ -167,15 +178,6 @@ class TypescriptMockServerImpl {
|
|
|
167
178
|
const endpoint = this.convertFileNameToEndpoint(dirPath, dirent, httpVerb);
|
|
168
179
|
let modulePath = `${dirPath}/${dirent.name}`;
|
|
169
180
|
this.registeredEndpoints.push({ httpVerb, endpoint });
|
|
170
|
-
if (__filename.endsWith('.js')) {
|
|
171
|
-
const distPath = path_1.default.join(process.cwd(), 'dist');
|
|
172
|
-
if (modulePath.startsWith(process.cwd()) && !modulePath.startsWith(distPath)) {
|
|
173
|
-
modulePath = modulePath.replace(process.cwd(), distPath);
|
|
174
|
-
}
|
|
175
|
-
if (modulePath.endsWith('.ts')) {
|
|
176
|
-
modulePath = modulePath.replace(/\.ts$/, '.js');
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
181
|
yield TypescriptMockServerImpl.loadModule(modulePath)
|
|
180
182
|
.then(model => this.addEndpoint(endpoint, httpVerb, model))
|
|
181
183
|
.catch(error => this.log.error(error));
|
package/dist/src/index.js
CHANGED
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typescript-mock-server",
|
|
3
|
-
"version": "1.11.
|
|
3
|
+
"version": "1.11.5",
|
|
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
|
"main": "dist/src/index.js",
|
|
6
6
|
"bin": {
|
|
7
7
|
"typescript-mock-server": "dist/src/index.js"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
|
+
"prepare": "npm run build",
|
|
10
11
|
"test": "jest",
|
|
11
|
-
"build": "tsc --outDir dist && sed -i '' '1s|.*|#!/usr/bin/env node|' dist/src/index.js",
|
|
12
|
+
"build": "tsc --outDir dist && sed -i '' '1s|.*|#!/usr/bin/env node|' dist/src/index.js && chmod +x dist/src/index.js",
|
|
12
13
|
"example": "npm run build && node dist/src/index.js --path=tms-models --port=5200 --cors=http://localhost:5200",
|
|
13
14
|
"start": "npm run build && node dist/src/index.js",
|
|
14
15
|
"update-deps": "npm update",
|
|
@@ -43,6 +44,7 @@
|
|
|
43
44
|
"@types/node": "^24.10.1",
|
|
44
45
|
"cors": "^2.8.5",
|
|
45
46
|
"express": "^5.1.0",
|
|
47
|
+
"ts-node": "^10.9.2",
|
|
46
48
|
"tslog": "^3.3.3",
|
|
47
49
|
"typescript": "^5.9.3",
|
|
48
50
|
"ts-node-dev": "2.0.0"
|
|
@@ -33,7 +33,15 @@ export class TypescriptMockServerImpl implements TypescriptMockServer{
|
|
|
33
33
|
|
|
34
34
|
private static async loadModule(moduleName: string) {
|
|
35
35
|
if (moduleName.endsWith('.ts')) {
|
|
36
|
-
require('ts-node').register({
|
|
36
|
+
require('ts-node').register({
|
|
37
|
+
transpileOnly: true,
|
|
38
|
+
compilerOptions: {
|
|
39
|
+
module: 'commonjs',
|
|
40
|
+
allowJs: true,
|
|
41
|
+
esModuleInterop: true,
|
|
42
|
+
resolveJsonModule: true
|
|
43
|
+
}
|
|
44
|
+
});
|
|
37
45
|
}
|
|
38
46
|
return await import(moduleName);
|
|
39
47
|
}
|
|
@@ -113,18 +121,6 @@ export class TypescriptMockServerImpl implements TypescriptMockServer{
|
|
|
113
121
|
let modulePath = `${dirPath}/${dirent.name}`;
|
|
114
122
|
this.registeredEndpoints.push({ httpVerb, endpoint });
|
|
115
123
|
|
|
116
|
-
if (__filename.endsWith('.js') && modulePath.endsWith('.ts')) {
|
|
117
|
-
const distPath = path.join(process.cwd(), 'dist');
|
|
118
|
-
const potentialJsPath = modulePath
|
|
119
|
-
.replace(process.cwd(), distPath)
|
|
120
|
-
.replace(/\.ts$/, '.js');
|
|
121
|
-
|
|
122
|
-
const fs = require('fs');
|
|
123
|
-
if (fs.existsSync(potentialJsPath)) {
|
|
124
|
-
modulePath = potentialJsPath;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
124
|
await TypescriptMockServerImpl.loadModule(modulePath)
|
|
129
125
|
.then(model => this.addEndpoint(endpoint, httpVerb, model))
|
|
130
126
|
.catch(error => this.log.error(error));
|
package/tsconfig.json
CHANGED