@@ -73,20 +73,31 @@ export const fetchRequestData = async (requestId: string) => {
7373 return { request, environment, settings, clientCertificates, caCert, activeEnvironmentId, timelinePath, responseId } ;
7474} ;
7575
76- export const tryToExecutePreRequestScript = async ( request : Request , environment : Environment , timelinePath : string , responseId : string ) => {
76+ export const tryToExecutePreRequestScript = async (
77+ request : Request ,
78+ environment : Environment ,
79+ timelinePath : string ,
80+ responseId : string ,
81+ baseEnvironment : Environment ,
82+ ) => {
7783 if ( ! request . preRequestScript ) {
7884 return {
7985 request,
8086 environment : undefined ,
87+ baseEnvironment : undefined ,
8188 } ;
8289 }
90+
8391 try {
8492 const output = await cancellableRunPreRequestScript ( {
8593 script : request . preRequestScript ,
8694 context : {
8795 request,
8896 timelinePath,
89- environment : environment ?. data || { } ,
97+ // it inputs empty environment data when active environment is the base environment
98+ // this is more deterministic and avoids that script accidently manipulates baseEnvironment instead of environment
99+ environment : environment . _id === baseEnvironment . _id ? { } : ( environment ?. data || { } ) ,
100+ baseEnvironment : baseEnvironment ?. data || { } ,
90101 } ,
91102 } ) ;
92103 console . log ( '[network] Pre-request script succeeded' , output ) ;
@@ -98,10 +109,18 @@ export const tryToExecutePreRequestScript = async (request: Request, environment
98109 ) ;
99110 environment . data = output . environment ;
100111 environment . dataPropertyOrder = envPropertyOrder . map ;
112+ const baseEnvPropertyOrder = orderedJSON . parse (
113+ JSON . stringify ( output . baseEnvironment ) ,
114+ JSON_ORDER_PREFIX ,
115+ JSON_ORDER_SEPARATOR ,
116+ ) ;
117+ baseEnvironment . data = output . baseEnvironment ;
118+ baseEnvironment . dataPropertyOrder = baseEnvPropertyOrder . map ;
101119
102120 return {
103121 request : output . request ,
104- environment : environment ,
122+ environment,
123+ baseEnvironment,
105124 } ;
106125 } catch ( err ) {
107126 await fs . promises . appendFile ( timelinePath , JSON . stringify ( { value : err . message , name : 'Text' , timestamp : Date . now ( ) } ) + '\n' ) ;
@@ -126,12 +145,14 @@ export const tryToInterpolateRequest = async (
126145 request : Request ,
127146 environment : string | Environment ,
128147 purpose ?: RenderPurpose ,
129- extraInfo ?: ExtraRenderInfo
148+ extraInfo ?: ExtraRenderInfo ,
149+ baseEnvironment ?: Environment ,
130150) => {
131151 try {
132152 return await getRenderedRequestAndContext ( {
133153 request : request ,
134154 environment,
155+ baseEnvironment,
135156 purpose,
136157 extraInfo,
137158 } ) ;
0 commit comments