11import { join } from "path" ;
22import { readdir , readJson , readFile } from "fs-extra" ;
3- import { DefaultBodyType , MockedRequest , rest , RestHandler } from "msw" ;
3+ import { RequestHandler , rest } from "msw" ;
44import {
55 GitHubApiRequest ,
66 isAutoModelRequest ,
@@ -14,8 +14,6 @@ import {
1414
1515const baseUrl = "https://api.github.com" ;
1616
17- type RequestHandler = RestHandler < MockedRequest < DefaultBodyType > > ;
18-
1917export async function createRequestHandlers (
2018 scenarioDirPath : string ,
2119) : Promise < RequestHandler [ ] > {
@@ -82,11 +80,10 @@ function createGetRepoRequestHandler(
8280
8381 const getRepoRequest = getRepoRequests [ 0 ] ;
8482
85- return rest . get ( `${ baseUrl } /repos/:owner/:name` , ( _req , res , ctx ) => {
86- return res (
87- ctx . status ( getRepoRequest . response . status ) ,
88- ctx . json ( getRepoRequest . response . body ) ,
89- ) ;
83+ return rest . get ( `${ baseUrl } /repos/:owner/:name` , ( ) => {
84+ return new Response ( JSON . stringify ( getRepoRequest . response . body ) , {
85+ status : getRepoRequest . response . status ,
86+ } ) ;
9087 } ) ;
9188}
9289
@@ -105,11 +102,10 @@ function createSubmitVariantAnalysisRequestHandler(
105102
106103 return rest . post (
107104 `${ baseUrl } /repositories/:controllerRepoId/code-scanning/codeql/variant-analyses` ,
108- ( _req , res , ctx ) => {
109- return res (
110- ctx . status ( getRepoRequest . response . status ) ,
111- ctx . json ( getRepoRequest . response . body ) ,
112- ) ;
105+ ( ) => {
106+ return new Response ( JSON . stringify ( getRepoRequest . response . body ) , {
107+ status : getRepoRequest . response . status ,
108+ } ) ;
113109 } ,
114110 ) ;
115111}
@@ -127,18 +123,17 @@ function createGetVariantAnalysisRequestHandler(
127123 // request, so keep an index of the request and return the appropriate response.
128124 return rest . get (
129125 `${ baseUrl } /repositories/:controllerRepoId/code-scanning/codeql/variant-analyses/:variantAnalysisId` ,
130- ( _req , res , ctx ) => {
126+ ( ) => {
131127 const request = getVariantAnalysisRequests [ requestIndex ] ;
132128
133129 if ( requestIndex < getVariantAnalysisRequests . length - 1 ) {
134130 // If there are more requests to come, increment the index.
135131 requestIndex ++ ;
136132 }
137133
138- return res (
139- ctx . status ( request . response . status ) ,
140- ctx . json ( request . response . body ) ,
141- ) ;
134+ return new Response ( JSON . stringify ( request . response . body ) , {
135+ status : request . response . status ,
136+ } ) ;
142137 } ,
143138 ) ;
144139}
@@ -152,18 +147,17 @@ function createGetVariantAnalysisRepoRequestHandler(
152147
153148 return rest . get (
154149 `${ baseUrl } /repositories/:controllerRepoId/code-scanning/codeql/variant-analyses/:variantAnalysisId/repositories/:repoId` ,
155- ( req , res , ctx ) => {
150+ ( { request , params } ) => {
156151 const scenarioRequest = getVariantAnalysisRepoRequests . find (
157- ( r ) => r . request . repositoryId . toString ( ) === req . params . repoId ,
152+ ( r ) => r . request . repositoryId . toString ( ) === params . repoId ,
158153 ) ;
159154 if ( ! scenarioRequest ) {
160- throw Error ( `No scenario request found for ${ req . url } ` ) ;
155+ throw Error ( `No scenario request found for ${ request . url } ` ) ;
161156 }
162157
163- return res (
164- ctx . status ( scenarioRequest . response . status ) ,
165- ctx . json ( scenarioRequest . response . body ) ,
166- ) ;
158+ return new Response ( JSON . stringify ( scenarioRequest . response . body ) , {
159+ status : scenarioRequest . response . status ,
160+ } ) ;
167161 } ,
168162 ) ;
169163}
@@ -177,22 +171,23 @@ function createGetVariantAnalysisRepoResultRequestHandler(
177171
178172 return rest . get (
179173 "https://objects-origin.githubusercontent.com/codeql-query-console/codeql-variant-analysis-repo-tasks/:variantAnalysisId/:repoId/*" ,
180- ( req , res , ctx ) => {
174+ ( { request , params } ) => {
181175 const scenarioRequest = getVariantAnalysisRepoResultRequests . find (
182- ( r ) => r . request . repositoryId . toString ( ) === req . params . repoId ,
176+ ( r ) => r . request . repositoryId . toString ( ) === params . repoId ,
183177 ) ;
184178 if ( ! scenarioRequest ) {
185- throw Error ( `No scenario request found for ${ req . url } ` ) ;
179+ throw Error ( `No scenario request found for ${ request . url } ` ) ;
186180 }
187181
188182 if ( scenarioRequest . response . body ) {
189- return res (
190- ctx . status ( scenarioRequest . response . status ) ,
191- ctx . set ( "Content-Type" , scenarioRequest . response . contentType ) ,
192- ctx . body ( scenarioRequest . response . body ) ,
193- ) ;
183+ return new Response ( scenarioRequest . response . body , {
184+ status : scenarioRequest . response . status ,
185+ headers : {
186+ "Content-Type" : scenarioRequest . response . contentType ,
187+ } ,
188+ } ) ;
194189 } else {
195- return res ( ctx . status ( scenarioRequest . response . status ) ) ;
190+ return new Response ( null , { status : scenarioRequest . response . status } ) ;
196191 }
197192 } ,
198193 ) ;
@@ -207,18 +202,17 @@ function createCodeSearchRequestHandler(
207202 // During a code search, there are multiple request to get pages of results. We
208203 // need to return different responses for each request, so keep an index of the
209204 // request and return the appropriate response.
210- return rest . get ( `${ baseUrl } /search/code?q=*` , ( _req , res , ctx ) => {
205+ return rest . get ( `${ baseUrl } /search/code?q=*` , ( ) => {
211206 const request = codeSearchRequests [ requestIndex ] ;
212207
213208 if ( requestIndex < codeSearchRequests . length - 1 ) {
214209 // If there are more requests to come, increment the index.
215210 requestIndex ++ ;
216211 }
217212
218- return res (
219- ctx . status ( request . response . status ) ,
220- ctx . json ( request . response . body ) ,
221- ) ;
213+ return new Response ( JSON . stringify ( request . response . body ) , {
214+ status : request . response . status ,
215+ } ) ;
222216 } ) ;
223217}
224218
@@ -233,18 +227,17 @@ function createAutoModelRequestHandler(
233227 // so keep an index of the request and return the appropriate response.
234228 return rest . post (
235229 `${ baseUrl } /repos/github/codeql/code-scanning/codeql/auto-model` ,
236- ( _req , res , ctx ) => {
230+ ( ) => {
237231 const request = autoModelRequests [ requestIndex ] ;
238232
239233 if ( requestIndex < autoModelRequests . length - 1 ) {
240234 // If there are more requests to come, increment the index.
241235 requestIndex ++ ;
242236 }
243237
244- return res (
245- ctx . status ( request . response . status ) ,
246- ctx . json ( request . response . body ) ,
247- ) ;
238+ return new Response ( JSON . stringify ( request . response . body ) , {
239+ status : request . response . status ,
240+ } ) ;
248241 } ,
249242 ) ;
250243}
0 commit comments