typescript-mock-server 1.11.4 → 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 check out this stub/mock server is by installing it as a (dev)dependency and then
8
- add a script to you scripts section: `npm run --prefix node_modules/typescript-mock-server start -- --path=$INIT_CWD/tms-models`.
9
- Your models should export a data const (or a function receiving `req` and `res`) and your file should be named as `^(get|post){1}(-\d)?.ts$`.
10
- Changes are being picked up automatically, so no need for a restart. When you add files, you have to restart.
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,7 @@ 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.
102
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.
103
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.
104
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.
@@ -178,16 +178,6 @@ class TypescriptMockServerImpl {
178
178
  const endpoint = this.convertFileNameToEndpoint(dirPath, dirent, httpVerb);
179
179
  let modulePath = `${dirPath}/${dirent.name}`;
180
180
  this.registeredEndpoints.push({ httpVerb, endpoint });
181
- if (__filename.endsWith('.js') && modulePath.endsWith('.ts')) {
182
- const distPath = path_1.default.join(process.cwd(), 'dist');
183
- const potentialJsPath = modulePath
184
- .replace(process.cwd(), distPath)
185
- .replace(/\.ts$/, '.js');
186
- const fs = require('fs');
187
- if (fs.existsSync(potentialJsPath)) {
188
- modulePath = potentialJsPath;
189
- }
190
- }
191
181
  yield TypescriptMockServerImpl.loadModule(modulePath)
192
182
  .then(model => this.addEndpoint(endpoint, httpVerb, model))
193
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.4",
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",
package/tsconfig.json CHANGED
@@ -1,5 +1,7 @@
1
1
  {
2
2
  "compilerOptions": {
3
+ "rootDir": ".",
4
+
3
5
  "target": "es6", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
4
6
 
5
7
  "module": "commonjs", /* Specify what module code is generated. */