@requestly/mock-server
Advanced tools
Comparing version 0.2.13 to 0.3.0
@@ -35,3 +35,3 @@ "use strict"; | ||
const mockResponse = yield mockProcessor_1.default.process(mockData, req, queryParams[queryParams_1.RQ_PASSWORD]); | ||
return mockResponse; | ||
return Object.assign(Object.assign({}, mockResponse), { metadata: { mockId: mockData.id } }); | ||
} | ||
@@ -38,0 +38,0 @@ console.debug("[Debug] No Mock Selected"); |
import { Express } from "express"; | ||
import IConfigFetcher from "../interfaces/configFetcherInterface"; | ||
interface MockServerConfig { | ||
import { Config } from "../interfaces/config"; | ||
interface MockServerOptions { | ||
port: number; | ||
pathPrefix: string; | ||
storageConfig: Config; | ||
} | ||
type MockServerConstructorOptions = Pick<MockServerOptions, 'storageConfig'> & Partial<MockServerOptions>; | ||
declare class MockServer { | ||
config: MockServerConfig; | ||
configFetcher: IConfigFetcher; | ||
serverOptions: MockServerOptions; | ||
app: Express; | ||
constructor(port: number | undefined, configFetcher: IConfigFetcher, pathPrefix?: string); | ||
constructor(options: MockServerConstructorOptions); | ||
start: () => void; | ||
@@ -13,0 +14,0 @@ setup: () => Express; |
@@ -19,8 +19,10 @@ "use strict"; | ||
const storageService_1 = __importDefault(require("../services/storageService")); | ||
const har_1 = require("../middlewares/har"); | ||
const utils_1 = require("./utils"); | ||
class MockServer { | ||
constructor(port = 3000, configFetcher, pathPrefix = "") { | ||
constructor(options) { | ||
var _a, _b; | ||
this.start = () => { | ||
this.app.listen(this.config.port, () => { | ||
console.log(`Mock Server Listening on port ${this.config.port}`); | ||
this.app.listen(this.serverOptions.port, () => { | ||
console.log(`Mock Server Listening on port ${this.serverOptions.port}`); | ||
}); | ||
@@ -31,2 +33,6 @@ }; | ||
const app = (0, express_1.default)(); | ||
// Use middleware to parse `application/json` and `application/x-www-form-urlencoded` body data | ||
app.use(express_1.default.json()); | ||
app.use(express_1.default.urlencoded({ extended: true })); | ||
app.use(har_1.HarMiddleware); | ||
app.use((_, res, next) => { | ||
@@ -48,3 +54,3 @@ res.set({ | ||
// pathPrefix to handle /mockv2 prefix in cloud functions | ||
const regex = new RegExp(`${this.config.pathPrefix}\/(.+)`); | ||
const regex = new RegExp(`${this.serverOptions.pathPrefix}\/(.+)`); | ||
app.all(regex, (req, res) => __awaiter(this, void 0, void 0, function* () { | ||
@@ -55,6 +61,6 @@ console.log(`Initial Request`); | ||
// Stripping URL prefix | ||
if (req.path.indexOf(this.config.pathPrefix) === 0) { | ||
console.log(`Stripping pathPrefix: ${this.config.pathPrefix}`); | ||
if (req.path.indexOf(this.serverOptions.pathPrefix) === 0) { | ||
console.log(`Stripping pathPrefix: ${this.serverOptions.pathPrefix}`); | ||
Object.defineProperty(req, 'path', { | ||
value: (0, utils_1.cleanupPath)(req.path.slice(this.config.pathPrefix.length)), | ||
value: (0, utils_1.cleanupPath)(req.path.slice(this.serverOptions.pathPrefix.length)), | ||
writable: true | ||
@@ -65,4 +71,5 @@ }); | ||
const mockResponse = yield mockHandler_1.default.handleEndpoint(req); | ||
// console.debug("[Debug] Final Mock Response", mockResponse); | ||
return res.status(mockResponse.statusCode).set(mockResponse.headers).end(mockResponse.body); | ||
console.debug("[Debug] Final Mock Response", mockResponse); | ||
res.locals.rq_metadata = mockResponse.metadata; | ||
return res.status(mockResponse.statusCode).set(mockResponse.headers).send(mockResponse.body); | ||
})); | ||
@@ -72,9 +79,9 @@ return app; | ||
this.initStorageService = () => { | ||
storageService_1.default.setConfigFetcher(this.configFetcher); | ||
storageService_1.default.setConfig(this.serverOptions.storageConfig); | ||
}; | ||
this.config = { | ||
port, | ||
pathPrefix | ||
this.serverOptions = { | ||
storageConfig: options.storageConfig, | ||
port: (_a = options.port) !== null && _a !== void 0 ? _a : 3000, | ||
pathPrefix: (_b = options.pathPrefix) !== null && _b !== void 0 ? _b : "", | ||
}; | ||
this.configFetcher = configFetcher; | ||
this.app = this.setup(); | ||
@@ -81,0 +88,0 @@ } |
@@ -1,4 +0,5 @@ | ||
import IConfigFetcher from "./interfaces/configFetcherInterface"; | ||
import { Config, ISink, ISource } from "./interfaces/config"; | ||
import MockServer from "./core/server"; | ||
import { Mock as MockSchema, MockMetadata as MockMetadataSchema, Response as MockResponseSchema } from "./types/mock"; | ||
export { MockServer, IConfigFetcher, MockSchema, MockMetadataSchema, MockResponseSchema, }; | ||
import { Log as MockLog } from "./types"; | ||
export { MockServer, Config, ISink, ISource, MockSchema, MockMetadataSchema, MockResponseSchema, MockLog, }; |
@@ -6,6 +6,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.IConfigFetcher = exports.MockServer = void 0; | ||
const configFetcherInterface_1 = __importDefault(require("./interfaces/configFetcherInterface")); | ||
exports.IConfigFetcher = configFetcherInterface_1.default; | ||
exports.ISource = exports.ISink = exports.MockServer = void 0; | ||
const config_1 = require("./interfaces/config"); | ||
Object.defineProperty(exports, "ISink", { enumerable: true, get: function () { return config_1.ISink; } }); | ||
Object.defineProperty(exports, "ISource", { enumerable: true, get: function () { return config_1.ISource; } }); | ||
const server_1 = __importDefault(require("./core/server")); | ||
exports.MockServer = server_1.default; |
@@ -1,10 +0,13 @@ | ||
import IConfigFetcher from "../interfaces/configFetcherInterface"; | ||
import { Config, ISink, ISource } from "../interfaces/config"; | ||
import { Log } from "../types"; | ||
declare class StorageService { | ||
configFetcher?: IConfigFetcher | null; | ||
constructor(configFetcher?: IConfigFetcher); | ||
setConfigFetcher: (configFetcher: IConfigFetcher) => void; | ||
source: ISource | null; | ||
sink: ISink | null; | ||
constructor(config?: Config); | ||
setConfig: (config: Config) => void; | ||
getMockSelectorMap: (kwargs?: any) => Promise<any>; | ||
getMock: (id: string, kwargs?: any) => Promise<any>; | ||
storeLog: (log: Log) => Promise<void>; | ||
} | ||
declare const storageService: StorageService; | ||
export default storageService; |
@@ -13,17 +13,24 @@ "use strict"; | ||
class StorageService { | ||
constructor(configFetcher) { | ||
this.configFetcher = null; | ||
constructor(config) { | ||
this.source = null; | ||
this.sink = null; | ||
// TODO: This should be set when starting the mock server | ||
this.setConfigFetcher = (configFetcher) => { | ||
this.configFetcher = configFetcher; | ||
this.setConfig = (config) => { | ||
this.source = config.src || null; | ||
this.sink = config.sink || null; | ||
}; | ||
this.getMockSelectorMap = (kwargs) => __awaiter(this, void 0, void 0, function* () { | ||
var _a; | ||
return (_a = this.configFetcher) === null || _a === void 0 ? void 0 : _a.getMockSelectorMap(kwargs); | ||
return (_a = this.source) === null || _a === void 0 ? void 0 : _a.getMockSelectorMap(kwargs); | ||
}); | ||
this.getMock = (id, kwargs) => __awaiter(this, void 0, void 0, function* () { | ||
var _b; | ||
return (_b = this.configFetcher) === null || _b === void 0 ? void 0 : _b.getMock(id, kwargs); | ||
return (_b = this.source) === null || _b === void 0 ? void 0 : _b.getMock(id, kwargs); | ||
}); | ||
this.configFetcher = configFetcher; | ||
this.storeLog = (log) => __awaiter(this, void 0, void 0, function* () { | ||
var _c; | ||
yield ((_c = this.sink) === null || _c === void 0 ? void 0 : _c.sendLog(log)); | ||
}); | ||
this.source = (config === null || config === void 0 ? void 0 : config.src) || null; | ||
this.sink = (config === null || config === void 0 ? void 0 : config.sink) || null; | ||
} | ||
@@ -30,0 +37,0 @@ } |
@@ -7,5 +7,9 @@ "use strict"; | ||
const server_1 = __importDefault(require("../core/server")); | ||
const firebaseConfigFetcher_1 = __importDefault(require("./firebaseConfigFetcher")); | ||
const server = new server_1.default(3001, firebaseConfigFetcher_1.default, "/mocksv2"); | ||
console.log(server.app); | ||
const testConfig_1 = __importDefault(require("./testConfig")); | ||
const server = new server_1.default({ | ||
port: 3001, | ||
pathPrefix: "/mocksv2", | ||
storageConfig: testConfig_1.default | ||
}); | ||
console.debug(server.app); | ||
server.start(); |
@@ -0,1 +1,2 @@ | ||
import type { Entry } from "har-format"; | ||
import { HttpStatusCode } from "../enums/mockServerResponse"; | ||
@@ -20,2 +21,9 @@ export declare enum RequestMethod { | ||
}; | ||
metadata?: { | ||
mockId: string; | ||
}; | ||
} | ||
export interface Log { | ||
mockId: string; | ||
HarEntry: Partial<Entry>; | ||
} |
{ | ||
"name": "@requestly/mock-server", | ||
"version": "0.2.13", | ||
"version": "0.3.0", | ||
"description": "- Methods: GET, POST, PUT, OPTIONS - Description - Endpoint (can be full path) (/api/v1/users) - Multiple Responses - Shuffle Response - Sequential Response - Rules in Response - Status (Any status code 2xx, 4xx) - Latency - Body - Templating - Faker js - Headers", | ||
@@ -26,2 +26,3 @@ "main": "build/index.js", | ||
"dependencies": { | ||
"@types/har-format": "^1.2.14", | ||
"cors": "^2.8.5", | ||
@@ -28,0 +29,0 @@ "express": "^4.18.2", |
@@ -27,6 +27,6 @@ # requestly-mock-server | ||
import { MockServer } from '@requestly/mock-server'; | ||
import firebaseConfigFetcher from '../firebaseConfigFetcher'; | ||
import firebaseConfig from '../firebaseConfig'; | ||
const startMockServer = () => { | ||
const expressApp = new MockServer(3000, firebaseConfigFetcher, '/api/mockv2').app; | ||
const expressApp = new MockServer(3000, firebaseConfig, '/api/mockv2').app; | ||
@@ -40,3 +40,3 @@ return functions.runWith({ minInstances: isProdEnv() ? 1 : 0 }).https.onRequest(expressApp); | ||
``` javascript | ||
class FirebaseConfigFetcher implements IConfigFetcher { | ||
class FirebaseConfig implements IConfig { | ||
getMockSelectorMap = (kwargs?: any) => { | ||
@@ -59,6 +59,12 @@ /** | ||
} | ||
storeLog? = (log: Log) => { | ||
/** | ||
* Store log in cloud storages | ||
*/ | ||
} | ||
} | ||
const firebaseConfigFetcher = new FirebaseConfigFetcher(); | ||
export default firebaseConfigFetcher; | ||
const firebaseConfig = new FirebaseConfig(); | ||
export default firebaseConfig; | ||
``` | ||
@@ -75,7 +81,7 @@ | ||
3. @requestly/mock-server - MockSelector | ||
a. Fetches all the available mocks using `IConfigFetcher.getMockSelectorMap()` (Firestore in case of Requestly) | ||
a. Fetches all the available mocks using `IConfig.getMockSelectorMap()` (Firestore in case of Requestly) | ||
b. Select mock if any endpoint+method matches the incoming request (GET /users) | ||
c. Fetch Mock using `IConfigFetcher.getMock(mockId)` and pass it to MockProcessor | ||
c. Fetch Mock using `IConfig.getMock(mockId)` and pass it to MockProcessor | ||
4. @requestly/mock-server - MockProcessor | ||
a. Process Mock - Response Rendering | ||
b. Return Response |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
81082
55
1071
84
5
2
+ Added@types/har-format@^1.2.14
+ Added@types/har-format@1.2.15(transitive)