@ashvajs/mock-server
Advanced tools
Comparing version 0.0.6 to 0.0.7
{ | ||
"name": "@ashvajs/mock-server", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ "fs": "^0.0.1-security", |
@@ -14,18 +14,21 @@ # @ashvajs/mock-server | ||
## Folder structure | ||
``` | ||
mocks | ||
api | ||
v1 | ||
test | ||
post.json // serves as /api/v1/test [post method] | ||
get.json // serves as /api/v1/test [get method] | ||
delete.json // serves as /api/v1/test [delete method] | ||
patch.json // serves as /api/v1/test [patch method] | ||
dynamic // for dynamic routes | ||
post | ||
index.js | ||
get | ||
index.js | ||
..api | ||
....v1 | ||
......test // route | ||
........post.json // serves as /api/v1/test [post method] | ||
........get.json // serves as /api/v1/test [get method] | ||
........delete.json // serves as /api/v1/test [delete method] | ||
........patch.json // serves as /api/v1/test [patch method] | ||
......dynamic // route | ||
........post // for dynamic routes | ||
..........index.js | ||
........get | ||
..........index.js | ||
``` | ||
## Example | ||
``` | ||
@@ -32,0 +35,0 @@ import path from 'path'; |
@@ -6,5 +6,5 @@ declare class MockServer { | ||
constructor({ fse, ...options }: any, app: any); | ||
ensureSlash(path: any): any; | ||
ensureNoSlash(path: any): any; | ||
init(): void; | ||
} | ||
export { MockServer }; |
@@ -6,2 +6,3 @@ "use strict"; | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const response_1 = require("./response"); | ||
@@ -16,4 +17,6 @@ const error_1 = require("./error"); | ||
} | ||
ensureSlash(path) { | ||
return path.substr(path.length - 1, 1) === '/' ? path : `${path}/`; | ||
ensureNoSlash(path) { | ||
return path.substr(path.length - 1, 1) === '/' | ||
? path.substr(0, path.length - 1) | ||
: `${path}`; | ||
} | ||
@@ -23,16 +26,17 @@ init() { | ||
this.app.all(this.config.staticMocks, (req, res) => { | ||
const path = req._parsedUrl.pathname; | ||
const reqPath = req._parsedUrl.pathname; | ||
const method = req.method.toLowerCase(); | ||
let finalPath = this.ensureSlash(this.config.staticApiPath) + path; | ||
let finalPath = reqPath.resolve(this.ensureNoSlash(this.config.staticApiPath), reqPath); | ||
// console.log(finalPath); | ||
let response = new response_1.default(error_1.default.notfound()).error(); | ||
let isJs = false; | ||
if (this.fse.existsSync(`${finalPath}/${method}/index.js`)) { | ||
finalPath = `${finalPath}/${method}/index.js`; | ||
if (this.fse.existsSync(path.join(finalPath, method, 'index.js'))) { | ||
finalPath = path.join(finalPath, method, 'index.js'); | ||
isJs = true; | ||
} | ||
else if (this.fse.existsSync(`${finalPath}/${method}.json`)) { | ||
finalPath = `${finalPath}/${method}.json`; | ||
else if (this.fse.existsSync(path.join(finalPath, `${method}.json`))) { | ||
finalPath = path.join(finalPath, `${method}.json`); | ||
} | ||
else if (this.fse.existsSync(`${finalPath}/index.json`)) { | ||
finalPath = `${finalPath}/index.json`; | ||
else if (this.fse.existsSync(path.join(finalPath, `index.json`))) { | ||
finalPath = path.join(finalPath, `index.json`); | ||
} | ||
@@ -39,0 +43,0 @@ console.log(`mock serving_path=${finalPath}`); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
11813
15
0
156
56