@@ -2,41 +2,52 @@ import express, { Request, Response, Express } from "express";
22import cors from "cors" ;
33
44import MockServerHandler from "./common/mockHandler" ;
5- import IConfigFetcher from "../interfaces/configFetcherInterface " ;
5+ import { Config } from "../interfaces/config " ;
66import storageService from "../services/storageService" ;
77import { MockServerResponse } from "../types" ;
8+ import { HarMiddleware } from "../middlewares/har" ;
89import { cleanupPath } from "./utils" ;
910
10- interface MockServerConfig {
11+ interface MockServerOptions {
1112 port : number ;
1213 pathPrefix : string ;
14+ storageConfig : Config ;
1315}
1416
17+ /* To make the constructor options optional except for storageConfig */
18+ type MockServerConstructorOptions = Pick < MockServerOptions , 'storageConfig' > & Partial < MockServerOptions > ;
19+
1520class MockServer {
16- config : MockServerConfig ;
17- configFetcher : IConfigFetcher ;
21+ serverOptions : MockServerOptions ;
1822 app : Express
1923
20- constructor ( port : number = 3000 , configFetcher : IConfigFetcher , pathPrefix : string = "" ) {
21- this . config = {
22- port,
23- pathPrefix
24+ constructor ( options : MockServerConstructorOptions ) {
25+ this . serverOptions = {
26+ storageConfig : options . storageConfig ,
27+ port : options . port ?? 3000 ,
28+ pathPrefix : options . pathPrefix ?? "" ,
2429 } ;
25- this . configFetcher = configFetcher ;
30+
2631
2732 this . app = this . setup ( ) ;
2833 }
2934
3035 start = ( ) => {
31- this . app . listen ( this . config . port , ( ) => {
32- console . log ( `Mock Server Listening on port ${ this . config . port } ` ) ;
36+ this . app . listen ( this . serverOptions . port , ( ) => {
37+ console . log ( `Mock Server Listening on port ${ this . serverOptions . port } ` ) ;
3338 } )
3439 }
3540
3641 setup = ( ) : Express => {
3742 this . initStorageService ( ) ;
3843
3944 const app = express ( ) ;
45+
46+ // Use middleware to parse `application/json` and `application/x-www-form-urlencoded` body data
47+ app . use ( express . json ( ) ) ;
48+ app . use ( express . urlencoded ( { extended : true } ) ) ;
49+
50+ app . use ( HarMiddleware ) ;
4051
4152 app . use ( ( _ , res , next ) => {
4253 res . set ( {
@@ -56,32 +67,34 @@ class MockServer {
5667 } ) ) ;
5768
5869 // pathPrefix to handle /mockv2 prefix in cloud functions
59- const regex = new RegExp ( `${ this . config . pathPrefix } \/(.+)` ) ;
70+ const regex = new RegExp ( `${ this . serverOptions . pathPrefix } \/(.+)` ) ;
6071 app . all ( regex , async ( req : Request , res : Response ) => {
6172 console . log ( `Initial Request` ) ;
6273 console . log ( `Path: ${ req . path } ` ) ;
6374 console . log ( `Query Params: ${ JSON . stringify ( req . query ) } ` ) ;
6475
6576 // Stripping URL prefix
66- if ( req . path . indexOf ( this . config . pathPrefix ) === 0 ) {
67- console . log ( `Stripping pathPrefix: ${ this . config . pathPrefix } ` ) ;
77+ if ( req . path . indexOf ( this . serverOptions . pathPrefix ) === 0 ) {
78+ console . log ( `Stripping pathPrefix: ${ this . serverOptions . pathPrefix } ` ) ;
6879 Object . defineProperty ( req , 'path' , {
69- value : cleanupPath ( req . path . slice ( this . config . pathPrefix . length ) ) ,
80+ value : cleanupPath ( req . path . slice ( this . serverOptions . pathPrefix . length ) ) ,
7081 writable : true
7182 } ) ;
7283 console . log ( `Path after stripping prefix and cleanup: ${ req . path } ` ) ;
7384 }
7485
7586 const mockResponse : MockServerResponse = await MockServerHandler . handleEndpoint ( req ) ;
76- // console.debug("[Debug] Final Mock Response", mockResponse);
77- return res . status ( mockResponse . statusCode ) . set ( mockResponse . headers ) . end ( mockResponse . body ) ;
87+ console . debug ( "[Debug] Final Mock Response" , mockResponse ) ;
88+
89+ res . locals . rq_metadata = mockResponse . metadata ;
90+ return res . status ( mockResponse . statusCode ) . set ( mockResponse . headers ) . send ( mockResponse . body ) ;
7891 } ) ;
7992
8093 return app ;
8194 }
8295
8396 initStorageService = ( ) => {
84- storageService . setConfigFetcher ( this . configFetcher ) ;
97+ storageService . setConfig ( this . serverOptions . storageConfig ) ;
8598 }
8699}
87100
0 commit comments