msw 2.0.11 → 2.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (405) hide show
  1. package/lib/browser/index.d.mts +104 -0
  2. package/lib/browser/index.d.ts +1 -1
  3. package/lib/browser/index.js +1 -0
  4. package/lib/browser/index.js.map +1 -0
  5. package/lib/browser/index.mjs +1 -0
  6. package/lib/browser/index.mjs.map +1 -0
  7. package/lib/core/{GraphQLHandler-907fc607.d.ts → GraphQLHandler-SHlRCcy4.d.ts} +9 -2
  8. package/lib/core/GraphQLHandler-zlmUDnN6.d.mts +97 -0
  9. package/lib/core/HttpResponse.d.mts +2 -0
  10. package/lib/core/HttpResponse.d.ts +1 -1
  11. package/lib/core/HttpResponse.js +1 -0
  12. package/lib/core/HttpResponse.js.map +1 -0
  13. package/lib/core/HttpResponse.mjs +1 -0
  14. package/lib/core/HttpResponse.mjs.map +1 -0
  15. package/lib/core/{RequestHandler-50ddea0c.d.ts → RequestHandler-rBDJQrEf.d.ts} +3 -1
  16. package/lib/core/RequestHandler-vxZdj6Tw.d.mts +179 -0
  17. package/lib/core/SetupApi.d.mts +26 -0
  18. package/lib/core/SetupApi.d.ts +1 -1
  19. package/lib/core/SetupApi.js +1 -0
  20. package/lib/core/SetupApi.js.map +1 -0
  21. package/lib/core/SetupApi.mjs +1 -0
  22. package/lib/core/SetupApi.mjs.map +1 -0
  23. package/lib/core/bypass.d.mts +16 -0
  24. package/lib/core/bypass.d.ts +1 -1
  25. package/lib/core/bypass.js +1 -0
  26. package/lib/core/bypass.js.map +1 -0
  27. package/lib/core/bypass.mjs +1 -0
  28. package/lib/core/bypass.mjs.map +1 -0
  29. package/lib/core/delay.d.mts +18 -0
  30. package/lib/core/delay.d.ts +1 -1
  31. package/lib/core/delay.js +1 -0
  32. package/lib/core/delay.js.map +1 -0
  33. package/lib/core/delay.mjs +1 -0
  34. package/lib/core/delay.mjs.map +1 -0
  35. package/lib/core/graphql.d.mts +109 -0
  36. package/lib/core/graphql.d.ts +3 -3
  37. package/lib/core/graphql.js +1 -0
  38. package/lib/core/graphql.js.map +1 -0
  39. package/lib/core/graphql.mjs +1 -0
  40. package/lib/core/graphql.mjs.map +1 -0
  41. package/lib/core/handlers/GraphQLHandler.d.mts +5 -0
  42. package/lib/core/handlers/GraphQLHandler.d.ts +2 -2
  43. package/lib/core/handlers/GraphQLHandler.js +28 -8
  44. package/lib/core/handlers/GraphQLHandler.js.map +1 -0
  45. package/lib/core/handlers/GraphQLHandler.mjs +28 -8
  46. package/lib/core/handlers/GraphQLHandler.mjs.map +1 -0
  47. package/lib/core/handlers/HttpHandler.d.mts +62 -0
  48. package/lib/core/handlers/HttpHandler.d.ts +2 -2
  49. package/lib/core/handlers/HttpHandler.js +1 -0
  50. package/lib/core/handlers/HttpHandler.js.map +1 -0
  51. package/lib/core/handlers/HttpHandler.mjs +1 -0
  52. package/lib/core/handlers/HttpHandler.mjs.map +1 -0
  53. package/lib/core/handlers/RequestHandler.d.mts +2 -0
  54. package/lib/core/handlers/RequestHandler.d.ts +1 -1
  55. package/lib/core/handlers/RequestHandler.js +19 -4
  56. package/lib/core/handlers/RequestHandler.js.map +1 -0
  57. package/lib/core/handlers/RequestHandler.mjs +19 -4
  58. package/lib/core/handlers/RequestHandler.mjs.map +1 -0
  59. package/lib/core/http.d.mts +26 -0
  60. package/lib/core/http.d.ts +1 -1
  61. package/lib/core/http.js +1 -0
  62. package/lib/core/http.js.map +1 -0
  63. package/lib/core/http.mjs +1 -0
  64. package/lib/core/http.mjs.map +1 -0
  65. package/lib/core/index.d.mts +18 -0
  66. package/lib/core/index.d.ts +2 -2
  67. package/lib/core/index.js +1 -0
  68. package/lib/core/index.js.map +1 -0
  69. package/lib/core/index.mjs +1 -0
  70. package/lib/core/index.mjs.map +1 -0
  71. package/lib/core/passthrough.d.mts +17 -0
  72. package/lib/core/passthrough.js +1 -0
  73. package/lib/core/passthrough.js.map +1 -0
  74. package/lib/core/passthrough.mjs +1 -0
  75. package/lib/core/passthrough.mjs.map +1 -0
  76. package/lib/core/sharedOptions.d.mts +66 -0
  77. package/lib/core/sharedOptions.d.ts +2 -2
  78. package/lib/core/sharedOptions.js +1 -0
  79. package/lib/core/sharedOptions.js.map +1 -0
  80. package/lib/core/sharedOptions.mjs +1 -0
  81. package/lib/core/sharedOptions.mjs.map +1 -0
  82. package/lib/core/typeUtils.d.mts +7 -0
  83. package/lib/core/typeUtils.d.ts +1 -1
  84. package/lib/core/typeUtils.js +1 -0
  85. package/lib/core/typeUtils.js.map +1 -0
  86. package/lib/core/typeUtils.mjs +1 -0
  87. package/lib/core/typeUtils.mjs.map +1 -0
  88. package/lib/core/utils/HttpResponse/decorators.d.mts +12 -0
  89. package/lib/core/utils/HttpResponse/decorators.d.ts +2 -2
  90. package/lib/core/utils/HttpResponse/decorators.js +1 -0
  91. package/lib/core/utils/HttpResponse/decorators.js.map +1 -0
  92. package/lib/core/utils/HttpResponse/decorators.mjs +1 -0
  93. package/lib/core/utils/HttpResponse/decorators.mjs.map +1 -0
  94. package/lib/core/utils/getResponse.d.mts +2 -0
  95. package/lib/core/utils/getResponse.d.ts +1 -1
  96. package/lib/core/utils/getResponse.js +1 -0
  97. package/lib/core/utils/getResponse.js.map +1 -0
  98. package/lib/core/utils/getResponse.mjs +1 -0
  99. package/lib/core/utils/getResponse.mjs.map +1 -0
  100. package/lib/core/utils/handleRequest.d.mts +37 -0
  101. package/lib/core/utils/handleRequest.d.ts +2 -2
  102. package/lib/core/utils/handleRequest.js +1 -0
  103. package/lib/core/utils/handleRequest.js.map +1 -0
  104. package/lib/core/utils/handleRequest.mjs +1 -0
  105. package/lib/core/utils/handleRequest.mjs.map +1 -0
  106. package/lib/core/utils/internal/Disposable.d.mts +7 -0
  107. package/lib/core/utils/internal/Disposable.d.ts +1 -1
  108. package/lib/core/utils/internal/Disposable.js +1 -0
  109. package/lib/core/utils/internal/Disposable.js.map +1 -0
  110. package/lib/core/utils/internal/Disposable.mjs +1 -0
  111. package/lib/core/utils/internal/Disposable.mjs.map +1 -0
  112. package/lib/core/utils/internal/checkGlobals.d.mts +3 -0
  113. package/lib/core/utils/internal/checkGlobals.js +1 -0
  114. package/lib/core/utils/internal/checkGlobals.js.map +1 -0
  115. package/lib/core/utils/internal/checkGlobals.mjs +1 -0
  116. package/lib/core/utils/internal/checkGlobals.mjs.map +1 -0
  117. package/lib/core/utils/internal/devUtils.d.mts +19 -0
  118. package/lib/core/utils/internal/devUtils.js +1 -0
  119. package/lib/core/utils/internal/devUtils.js.map +1 -0
  120. package/lib/core/utils/internal/devUtils.mjs +1 -0
  121. package/lib/core/utils/internal/devUtils.mjs.map +1 -0
  122. package/lib/core/utils/internal/getCallFrame.d.mts +6 -0
  123. package/lib/core/utils/internal/getCallFrame.js +1 -0
  124. package/lib/core/utils/internal/getCallFrame.js.map +1 -0
  125. package/lib/core/utils/internal/getCallFrame.mjs +1 -0
  126. package/lib/core/utils/internal/getCallFrame.mjs.map +1 -0
  127. package/lib/core/utils/internal/isIterable.d.mts +6 -0
  128. package/lib/core/utils/internal/isIterable.js +1 -0
  129. package/lib/core/utils/internal/isIterable.js.map +1 -0
  130. package/lib/core/utils/internal/isIterable.mjs +1 -0
  131. package/lib/core/utils/internal/isIterable.mjs.map +1 -0
  132. package/lib/core/utils/internal/isObject.d.mts +6 -0
  133. package/lib/core/utils/internal/isObject.js +1 -0
  134. package/lib/core/utils/internal/isObject.js.map +1 -0
  135. package/lib/core/utils/internal/isObject.mjs +1 -0
  136. package/lib/core/utils/internal/isObject.mjs.map +1 -0
  137. package/lib/core/utils/internal/isStringEqual.d.mts +6 -0
  138. package/lib/core/utils/internal/isStringEqual.js +1 -0
  139. package/lib/core/utils/internal/isStringEqual.js.map +1 -0
  140. package/lib/core/utils/internal/isStringEqual.mjs +1 -0
  141. package/lib/core/utils/internal/isStringEqual.mjs.map +1 -0
  142. package/lib/core/utils/internal/jsonParse.d.mts +7 -0
  143. package/lib/core/utils/internal/jsonParse.js +1 -0
  144. package/lib/core/utils/internal/jsonParse.js.map +1 -0
  145. package/lib/core/utils/internal/jsonParse.mjs +1 -0
  146. package/lib/core/utils/internal/jsonParse.mjs.map +1 -0
  147. package/lib/core/utils/internal/mergeRight.d.mts +7 -0
  148. package/lib/core/utils/internal/mergeRight.js +1 -0
  149. package/lib/core/utils/internal/mergeRight.js.map +1 -0
  150. package/lib/core/utils/internal/mergeRight.mjs +1 -0
  151. package/lib/core/utils/internal/mergeRight.mjs.map +1 -0
  152. package/lib/core/utils/internal/parseGraphQLRequest.d.mts +5 -0
  153. package/lib/core/utils/internal/parseGraphQLRequest.d.ts +2 -2
  154. package/lib/core/utils/internal/parseGraphQLRequest.js +1 -0
  155. package/lib/core/utils/internal/parseGraphQLRequest.js.map +1 -0
  156. package/lib/core/utils/internal/parseGraphQLRequest.mjs +1 -0
  157. package/lib/core/utils/internal/parseGraphQLRequest.mjs.map +1 -0
  158. package/lib/core/utils/internal/parseMultipartData.d.mts +10 -0
  159. package/lib/core/utils/internal/parseMultipartData.d.ts +1 -1
  160. package/lib/core/utils/internal/parseMultipartData.js +1 -0
  161. package/lib/core/utils/internal/parseMultipartData.js.map +1 -0
  162. package/lib/core/utils/internal/parseMultipartData.mjs +1 -0
  163. package/lib/core/utils/internal/parseMultipartData.mjs.map +1 -0
  164. package/lib/core/utils/internal/pipeEvents.d.mts +8 -0
  165. package/lib/core/utils/internal/pipeEvents.js +1 -0
  166. package/lib/core/utils/internal/pipeEvents.js.map +1 -0
  167. package/lib/core/utils/internal/pipeEvents.mjs +1 -0
  168. package/lib/core/utils/internal/pipeEvents.mjs.map +1 -0
  169. package/lib/core/utils/internal/requestHandlerUtils.d.mts +8 -0
  170. package/lib/core/utils/internal/requestHandlerUtils.d.ts +1 -1
  171. package/lib/core/utils/internal/requestHandlerUtils.js +1 -0
  172. package/lib/core/utils/internal/requestHandlerUtils.js.map +1 -0
  173. package/lib/core/utils/internal/requestHandlerUtils.mjs +1 -0
  174. package/lib/core/utils/internal/requestHandlerUtils.mjs.map +1 -0
  175. package/lib/core/utils/internal/toReadonlyArray.d.mts +6 -0
  176. package/lib/core/utils/internal/toReadonlyArray.js +1 -0
  177. package/lib/core/utils/internal/toReadonlyArray.js.map +1 -0
  178. package/lib/core/utils/internal/toReadonlyArray.mjs +1 -0
  179. package/lib/core/utils/internal/toReadonlyArray.mjs.map +1 -0
  180. package/lib/core/utils/internal/tryCatch.d.mts +3 -0
  181. package/lib/core/utils/internal/tryCatch.js +1 -0
  182. package/lib/core/utils/internal/tryCatch.js.map +1 -0
  183. package/lib/core/utils/internal/tryCatch.mjs +1 -0
  184. package/lib/core/utils/internal/tryCatch.mjs.map +1 -0
  185. package/lib/core/utils/internal/uuidv4.d.mts +3 -0
  186. package/lib/core/utils/internal/uuidv4.js +1 -0
  187. package/lib/core/utils/internal/uuidv4.js.map +1 -0
  188. package/lib/core/utils/internal/uuidv4.mjs +1 -0
  189. package/lib/core/utils/internal/uuidv4.mjs.map +1 -0
  190. package/lib/core/utils/logging/getStatusCodeColor.d.mts +11 -0
  191. package/lib/core/utils/logging/getStatusCodeColor.js +1 -0
  192. package/lib/core/utils/logging/getStatusCodeColor.js.map +1 -0
  193. package/lib/core/utils/logging/getStatusCodeColor.mjs +1 -0
  194. package/lib/core/utils/logging/getStatusCodeColor.mjs.map +1 -0
  195. package/lib/core/utils/logging/getTimestamp.d.mts +6 -0
  196. package/lib/core/utils/logging/getTimestamp.js +1 -0
  197. package/lib/core/utils/logging/getTimestamp.js.map +1 -0
  198. package/lib/core/utils/logging/getTimestamp.mjs +1 -0
  199. package/lib/core/utils/logging/getTimestamp.mjs.map +1 -0
  200. package/lib/core/utils/logging/serializeRequest.d.mts +12 -0
  201. package/lib/core/utils/logging/serializeRequest.d.ts +1 -1
  202. package/lib/core/utils/logging/serializeRequest.js +1 -0
  203. package/lib/core/utils/logging/serializeRequest.js.map +1 -0
  204. package/lib/core/utils/logging/serializeRequest.mjs +1 -0
  205. package/lib/core/utils/logging/serializeRequest.mjs.map +1 -0
  206. package/lib/core/utils/logging/serializeResponse.d.mts +9 -0
  207. package/lib/core/utils/logging/serializeResponse.d.ts +1 -1
  208. package/lib/core/utils/logging/serializeResponse.js +1 -0
  209. package/lib/core/utils/logging/serializeResponse.js.map +1 -0
  210. package/lib/core/utils/logging/serializeResponse.mjs +1 -0
  211. package/lib/core/utils/logging/serializeResponse.mjs.map +1 -0
  212. package/lib/core/utils/matching/matchRequestUrl.d.mts +19 -0
  213. package/lib/core/utils/matching/matchRequestUrl.d.ts +1 -1
  214. package/lib/core/utils/matching/matchRequestUrl.js +1 -0
  215. package/lib/core/utils/matching/matchRequestUrl.js.map +1 -0
  216. package/lib/core/utils/matching/matchRequestUrl.mjs +1 -0
  217. package/lib/core/utils/matching/matchRequestUrl.mjs.map +1 -0
  218. package/lib/core/utils/matching/normalizePath.d.mts +12 -0
  219. package/lib/core/utils/matching/normalizePath.js +1 -0
  220. package/lib/core/utils/matching/normalizePath.js.map +1 -0
  221. package/lib/core/utils/matching/normalizePath.mjs +1 -0
  222. package/lib/core/utils/matching/normalizePath.mjs.map +1 -0
  223. package/lib/core/utils/request/getPublicUrlFromRequest.d.mts +7 -0
  224. package/lib/core/utils/request/getPublicUrlFromRequest.js +1 -0
  225. package/lib/core/utils/request/getPublicUrlFromRequest.js.map +1 -0
  226. package/lib/core/utils/request/getPublicUrlFromRequest.mjs +1 -0
  227. package/lib/core/utils/request/getPublicUrlFromRequest.mjs.map +1 -0
  228. package/lib/core/utils/request/getRequestCookies.d.mts +8 -0
  229. package/lib/core/utils/request/getRequestCookies.js +1 -0
  230. package/lib/core/utils/request/getRequestCookies.js.map +1 -0
  231. package/lib/core/utils/request/getRequestCookies.mjs +1 -0
  232. package/lib/core/utils/request/getRequestCookies.mjs.map +1 -0
  233. package/lib/core/utils/request/onUnhandledRequest.d.mts +12 -0
  234. package/lib/core/utils/request/onUnhandledRequest.d.ts +2 -2
  235. package/lib/core/utils/request/onUnhandledRequest.js +1 -0
  236. package/lib/core/utils/request/onUnhandledRequest.js.map +1 -0
  237. package/lib/core/utils/request/onUnhandledRequest.mjs +1 -0
  238. package/lib/core/utils/request/onUnhandledRequest.mjs.map +1 -0
  239. package/lib/core/utils/request/readResponseCookies.d.mts +3 -0
  240. package/lib/core/utils/request/readResponseCookies.js +1 -0
  241. package/lib/core/utils/request/readResponseCookies.js.map +1 -0
  242. package/lib/core/utils/request/readResponseCookies.mjs +1 -0
  243. package/lib/core/utils/request/readResponseCookies.mjs.map +1 -0
  244. package/lib/core/utils/toResponseInit.d.mts +3 -0
  245. package/lib/core/utils/toResponseInit.js +1 -0
  246. package/lib/core/utils/toResponseInit.js.map +1 -0
  247. package/lib/core/utils/toResponseInit.mjs +1 -0
  248. package/lib/core/utils/toResponseInit.mjs.map +1 -0
  249. package/lib/core/utils/url/cleanUrl.d.mts +7 -0
  250. package/lib/core/utils/url/cleanUrl.js +1 -0
  251. package/lib/core/utils/url/cleanUrl.js.map +1 -0
  252. package/lib/core/utils/url/cleanUrl.mjs +1 -0
  253. package/lib/core/utils/url/cleanUrl.mjs.map +1 -0
  254. package/lib/core/utils/url/getAbsoluteUrl.d.mts +6 -0
  255. package/lib/core/utils/url/getAbsoluteUrl.js +1 -0
  256. package/lib/core/utils/url/getAbsoluteUrl.js.map +1 -0
  257. package/lib/core/utils/url/getAbsoluteUrl.mjs +1 -0
  258. package/lib/core/utils/url/getAbsoluteUrl.mjs.map +1 -0
  259. package/lib/core/utils/url/isAbsoluteUrl.d.mts +6 -0
  260. package/lib/core/utils/url/isAbsoluteUrl.js +1 -0
  261. package/lib/core/utils/url/isAbsoluteUrl.js.map +1 -0
  262. package/lib/core/utils/url/isAbsoluteUrl.mjs +1 -0
  263. package/lib/core/utils/url/isAbsoluteUrl.mjs.map +1 -0
  264. package/lib/iife/index.js +59 -25
  265. package/lib/iife/index.js.map +1 -0
  266. package/lib/mockServiceWorker.js +1 -1
  267. package/lib/native/index.d.mts +75 -0
  268. package/lib/native/index.d.ts +0 -1
  269. package/lib/native/index.js +1 -3
  270. package/lib/native/index.js.map +1 -0
  271. package/lib/native/index.mjs +1 -3
  272. package/lib/native/index.mjs.map +1 -0
  273. package/lib/node/index.d.mts +75 -0
  274. package/lib/node/index.d.ts +1 -2
  275. package/lib/node/index.js +2 -35
  276. package/lib/node/index.js.map +1 -0
  277. package/lib/node/index.mjs +2 -35
  278. package/lib/node/index.mjs.map +1 -0
  279. package/package.json +6 -5
  280. package/src/browser/global.browser.d.ts +1 -0
  281. package/src/browser/index.ts +3 -0
  282. package/src/browser/setupWorker/glossary.ts +259 -0
  283. package/src/browser/setupWorker/setupWorker.node.test.ts +10 -0
  284. package/src/browser/setupWorker/setupWorker.ts +199 -0
  285. package/src/browser/setupWorker/start/createFallbackRequestListener.ts +67 -0
  286. package/src/browser/setupWorker/start/createFallbackStart.ts +21 -0
  287. package/src/browser/setupWorker/start/createRequestListener.ts +127 -0
  288. package/src/browser/setupWorker/start/createResponseListener.ts +58 -0
  289. package/src/browser/setupWorker/start/createStartHandler.ts +143 -0
  290. package/src/browser/setupWorker/start/utils/createMessageChannel.ts +32 -0
  291. package/src/browser/setupWorker/start/utils/enableMocking.ts +32 -0
  292. package/src/browser/setupWorker/start/utils/getWorkerByRegistration.ts +25 -0
  293. package/src/browser/setupWorker/start/utils/getWorkerInstance.ts +94 -0
  294. package/src/browser/setupWorker/start/utils/prepareStartHandler.test.ts +59 -0
  295. package/src/browser/setupWorker/start/utils/prepareStartHandler.ts +44 -0
  296. package/src/browser/setupWorker/start/utils/printStartMessage.test.ts +84 -0
  297. package/src/browser/setupWorker/start/utils/printStartMessage.ts +40 -0
  298. package/src/browser/setupWorker/start/utils/validateWorkerScope.ts +18 -0
  299. package/src/browser/setupWorker/stop/createFallbackStop.ts +11 -0
  300. package/src/browser/setupWorker/stop/createStop.ts +29 -0
  301. package/src/browser/setupWorker/stop/utils/printStopMessage.test.ts +26 -0
  302. package/src/browser/setupWorker/stop/utils/printStopMessage.ts +12 -0
  303. package/src/browser/tsconfig.browser.build.json +6 -0
  304. package/src/browser/tsconfig.browser.json +9 -0
  305. package/src/browser/utils/deferNetworkRequestsUntil.test.ts +48 -0
  306. package/src/browser/utils/deferNetworkRequestsUntil.ts +29 -0
  307. package/src/browser/utils/getAbsoluteWorkerUrl.test.ts +31 -0
  308. package/src/browser/utils/getAbsoluteWorkerUrl.ts +7 -0
  309. package/src/browser/utils/parseWorkerRequest.ts +15 -0
  310. package/src/browser/utils/pruneGetRequestBody.test.ts +53 -0
  311. package/src/browser/utils/pruneGetRequestBody.ts +21 -0
  312. package/src/browser/utils/requestIntegrityCheck.ts +23 -0
  313. package/src/browser/utils/supportsReadableStreamTransfer.ts +17 -0
  314. package/src/core/HttpResponse.test.ts +200 -0
  315. package/src/core/HttpResponse.ts +134 -0
  316. package/src/core/SetupApi.ts +95 -0
  317. package/src/core/bypass.test.ts +47 -0
  318. package/src/core/bypass.ts +36 -0
  319. package/src/core/delay.ts +70 -0
  320. package/src/core/graphql.test.ts +11 -0
  321. package/src/core/graphql.ts +138 -0
  322. package/src/core/handlers/GraphQLHandler.test.ts +820 -0
  323. package/src/core/handlers/GraphQLHandler.ts +263 -0
  324. package/src/core/handlers/HttpHandler.test.ts +218 -0
  325. package/src/core/handlers/HttpHandler.ts +169 -0
  326. package/src/core/handlers/RequestHandler.ts +338 -0
  327. package/src/core/http.test.ts +15 -0
  328. package/src/core/http.ts +51 -0
  329. package/src/core/index.ts +56 -0
  330. package/src/core/passthrough.test.ts +13 -0
  331. package/src/core/passthrough.ts +23 -0
  332. package/src/core/sharedOptions.ts +66 -0
  333. package/src/core/typeUtils.ts +20 -0
  334. package/src/core/utils/HttpResponse/decorators.ts +56 -0
  335. package/src/core/utils/getResponse.ts +55 -0
  336. package/src/core/utils/handleRequest.test.ts +554 -0
  337. package/src/core/utils/handleRequest.ts +132 -0
  338. package/src/core/utils/internal/Disposable.ts +9 -0
  339. package/src/core/utils/internal/checkGlobals.ts +17 -0
  340. package/src/core/utils/internal/devUtils.ts +31 -0
  341. package/src/core/utils/internal/getCallFrame.test.ts +154 -0
  342. package/src/core/utils/internal/getCallFrame.ts +35 -0
  343. package/src/core/utils/internal/isIterable.test.ts +23 -0
  344. package/src/core/utils/internal/isIterable.ts +12 -0
  345. package/src/core/utils/internal/isObject.test.ts +20 -0
  346. package/src/core/utils/internal/isObject.ts +6 -0
  347. package/src/core/utils/internal/isStringEqual.test.ts +45 -0
  348. package/src/core/utils/internal/isStringEqual.ts +6 -0
  349. package/src/core/utils/internal/jsonParse.test.ts +13 -0
  350. package/src/core/utils/internal/jsonParse.ts +13 -0
  351. package/src/core/utils/internal/mergeRight.test.ts +43 -0
  352. package/src/core/utils/internal/mergeRight.ts +27 -0
  353. package/src/core/utils/internal/parseGraphQLRequest.test.ts +99 -0
  354. package/src/core/utils/internal/parseGraphQLRequest.ts +205 -0
  355. package/src/core/utils/internal/parseMultipartData.test.ts +76 -0
  356. package/src/core/utils/internal/parseMultipartData.ts +104 -0
  357. package/src/core/utils/internal/pipeEvents.test.ts +14 -0
  358. package/src/core/utils/internal/pipeEvents.ts +25 -0
  359. package/src/core/utils/internal/requestHandlerUtils.ts +21 -0
  360. package/src/core/utils/internal/toReadonlyArray.test.ts +30 -0
  361. package/src/core/utils/internal/toReadonlyArray.ts +8 -0
  362. package/src/core/utils/internal/tryCatch.test.ts +29 -0
  363. package/src/core/utils/internal/tryCatch.ts +11 -0
  364. package/src/core/utils/internal/uuidv4.ts +3 -0
  365. package/src/core/utils/logging/getStatusCodeColor.test.ts +22 -0
  366. package/src/core/utils/logging/getStatusCodeColor.ts +20 -0
  367. package/src/core/utils/logging/getTimestamp.test.ts +18 -0
  368. package/src/core/utils/logging/getTimestamp.ts +12 -0
  369. package/src/core/utils/logging/serializeRequest.test.ts +23 -0
  370. package/src/core/utils/logging/serializeRequest.ts +23 -0
  371. package/src/core/utils/logging/serializeResponse.test.ts +77 -0
  372. package/src/core/utils/logging/serializeResponse.ts +31 -0
  373. package/src/core/utils/matching/matchRequestUrl.test.ts +114 -0
  374. package/src/core/utils/matching/matchRequestUrl.ts +73 -0
  375. package/src/core/utils/matching/normalizePath.node.test.ts +44 -0
  376. package/src/core/utils/matching/normalizePath.test.ts +50 -0
  377. package/src/core/utils/matching/normalizePath.ts +21 -0
  378. package/src/core/utils/request/getPublicUrlFromRequest.test.ts +26 -0
  379. package/src/core/utils/request/getPublicUrlFromRequest.ts +15 -0
  380. package/src/core/utils/request/getRequestCookies.node.test.ts +29 -0
  381. package/src/core/utils/request/getRequestCookies.test.ts +64 -0
  382. package/src/core/utils/request/getRequestCookies.ts +75 -0
  383. package/src/core/utils/request/onUnhandledRequest.test.ts +215 -0
  384. package/src/core/utils/request/onUnhandledRequest.ts +247 -0
  385. package/src/core/utils/request/readResponseCookies.ts +9 -0
  386. package/src/core/utils/toResponseInit.ts +7 -0
  387. package/src/core/utils/url/cleanUrl.test.ts +17 -0
  388. package/src/core/utils/url/cleanUrl.ts +12 -0
  389. package/src/core/utils/url/getAbsoluteUrl.node.test.ts +19 -0
  390. package/src/core/utils/url/getAbsoluteUrl.test.ts +29 -0
  391. package/src/core/utils/url/getAbsoluteUrl.ts +26 -0
  392. package/src/core/utils/url/isAbsoluteUrl.test.ts +32 -0
  393. package/src/core/utils/url/isAbsoluteUrl.ts +6 -0
  394. package/src/iife/index.ts +2 -0
  395. package/src/mockServiceWorker.js +287 -0
  396. package/src/native/index.ts +17 -0
  397. package/src/node/SetupServerApi.ts +113 -0
  398. package/src/node/glossary.ts +62 -0
  399. package/src/node/index.ts +3 -0
  400. package/src/node/setupServer.ts +21 -0
  401. package/src/node/utils/isNodeExceptionLike.ts +14 -0
  402. package/src/tsconfig.core.build.json +6 -0
  403. package/src/tsconfig.node.build.json +6 -0
  404. package/src/tsconfig.node.json +8 -0
  405. package/src/tsconfig.src.json +15 -0
@@ -0,0 +1,338 @@
1
+ import { invariant } from 'outvariant'
2
+ import { getCallFrame } from '../utils/internal/getCallFrame'
3
+ import { isIterable } from '../utils/internal/isIterable'
4
+ import type { ResponseResolutionContext } from '../utils/getResponse'
5
+ import type { MaybePromise } from '../typeUtils'
6
+ import { StrictRequest, StrictResponse } from '..//HttpResponse'
7
+
8
+ export type DefaultRequestMultipartBody = Record<
9
+ string,
10
+ string | File | Array<string | File>
11
+ >
12
+
13
+ export type DefaultBodyType =
14
+ | Record<string, any>
15
+ | DefaultRequestMultipartBody
16
+ | string
17
+ | number
18
+ | boolean
19
+ | null
20
+ | undefined
21
+
22
+ export type JsonBodyType =
23
+ | Record<string, any>
24
+ | string
25
+ | number
26
+ | boolean
27
+ | null
28
+ | undefined
29
+
30
+ export interface RequestHandlerDefaultInfo {
31
+ header: string
32
+ }
33
+
34
+ export interface RequestHandlerInternalInfo {
35
+ callFrame?: string
36
+ }
37
+
38
+ export type ResponseResolverReturnType<
39
+ BodyType extends DefaultBodyType = undefined,
40
+ > =
41
+ | ([BodyType] extends [undefined] ? Response : StrictResponse<BodyType>)
42
+ | undefined
43
+ | void
44
+
45
+ export type MaybeAsyncResponseResolverReturnType<
46
+ BodyType extends DefaultBodyType,
47
+ > = MaybePromise<ResponseResolverReturnType<BodyType>>
48
+
49
+ export type AsyncResponseResolverReturnType<BodyType extends DefaultBodyType> =
50
+ | MaybeAsyncResponseResolverReturnType<BodyType>
51
+ | Generator<
52
+ MaybeAsyncResponseResolverReturnType<BodyType>,
53
+ MaybeAsyncResponseResolverReturnType<BodyType>,
54
+ MaybeAsyncResponseResolverReturnType<BodyType>
55
+ >
56
+
57
+ export type ResponseResolverInfo<
58
+ ResolverExtraInfo extends Record<string, unknown>,
59
+ RequestBodyType extends DefaultBodyType = DefaultBodyType,
60
+ > = {
61
+ request: StrictRequest<RequestBodyType>
62
+ } & ResolverExtraInfo
63
+
64
+ export type ResponseResolver<
65
+ ResolverExtraInfo extends Record<string, unknown> = Record<string, unknown>,
66
+ RequestBodyType extends DefaultBodyType = DefaultBodyType,
67
+ ResponseBodyType extends DefaultBodyType = undefined,
68
+ > = (
69
+ info: ResponseResolverInfo<ResolverExtraInfo, RequestBodyType>,
70
+ ) => AsyncResponseResolverReturnType<ResponseBodyType>
71
+
72
+ export interface RequestHandlerArgs<
73
+ HandlerInfo,
74
+ HandlerOptions extends RequestHandlerOptions,
75
+ > {
76
+ info: HandlerInfo
77
+ resolver: ResponseResolver<any>
78
+ options?: HandlerOptions
79
+ }
80
+
81
+ export interface RequestHandlerOptions {
82
+ once?: boolean
83
+ }
84
+
85
+ export interface RequestHandlerExecutionResult<
86
+ ParsedResult extends Record<string, unknown> | undefined,
87
+ > {
88
+ handler: RequestHandler
89
+ parsedResult?: ParsedResult
90
+ request: Request
91
+ response?: Response
92
+ }
93
+
94
+ export abstract class RequestHandler<
95
+ HandlerInfo extends RequestHandlerDefaultInfo = RequestHandlerDefaultInfo,
96
+ ParsedResult extends Record<string, any> | undefined = any,
97
+ ResolverExtras extends Record<string, unknown> = any,
98
+ HandlerOptions extends RequestHandlerOptions = RequestHandlerOptions,
99
+ > {
100
+ static cache = new WeakMap<
101
+ StrictRequest<DefaultBodyType>,
102
+ StrictRequest<DefaultBodyType>
103
+ >()
104
+
105
+ public info: HandlerInfo & RequestHandlerInternalInfo
106
+ /**
107
+ * Indicates whether this request handler has been used
108
+ * (its resolver has successfully executed).
109
+ */
110
+ public isUsed: boolean
111
+
112
+ protected resolver: ResponseResolver<ResolverExtras, any, any>
113
+ private resolverGenerator?: Generator<
114
+ MaybeAsyncResponseResolverReturnType<any>,
115
+ MaybeAsyncResponseResolverReturnType<any>,
116
+ MaybeAsyncResponseResolverReturnType<any>
117
+ >
118
+ private resolverGeneratorResult?: Response | StrictResponse<any>
119
+ private options?: HandlerOptions
120
+
121
+ constructor(args: RequestHandlerArgs<HandlerInfo, HandlerOptions>) {
122
+ this.resolver = args.resolver
123
+ this.options = args.options
124
+
125
+ const callFrame = getCallFrame(new Error())
126
+
127
+ this.info = {
128
+ ...args.info,
129
+ callFrame,
130
+ }
131
+
132
+ this.isUsed = false
133
+ }
134
+
135
+ /**
136
+ * Determine if the intercepted request should be mocked.
137
+ */
138
+ abstract predicate(args: {
139
+ request: Request
140
+ parsedResult: ParsedResult
141
+ resolutionContext?: ResponseResolutionContext
142
+ }): boolean
143
+
144
+ /**
145
+ * Print out the successfully handled request.
146
+ */
147
+ abstract log(args: {
148
+ request: Request
149
+ response: Response
150
+ parsedResult: ParsedResult
151
+ }): void
152
+
153
+ /**
154
+ * Parse the intercepted request to extract additional information from it.
155
+ * Parsed result is then exposed to other methods of this request handler.
156
+ */
157
+ async parse(_args: {
158
+ request: Request
159
+ resolutionContext?: ResponseResolutionContext
160
+ }): Promise<ParsedResult> {
161
+ return {} as ParsedResult
162
+ }
163
+
164
+ /**
165
+ * Test if this handler matches the given request.
166
+ *
167
+ * This method is not used internally but is exposed
168
+ * as a convenience method for consumers writing custom
169
+ * handlers.
170
+ */
171
+ public async test(args: {
172
+ request: Request
173
+ resolutionContext?: ResponseResolutionContext
174
+ }): Promise<boolean> {
175
+ const parsedResult = await this.parse({
176
+ request: args.request,
177
+ resolutionContext: args.resolutionContext,
178
+ })
179
+
180
+ return this.predicate({
181
+ request: args.request,
182
+ parsedResult,
183
+ resolutionContext: args.resolutionContext,
184
+ })
185
+ }
186
+
187
+ protected extendResolverArgs(_args: {
188
+ request: Request
189
+ parsedResult: ParsedResult
190
+ }): ResolverExtras {
191
+ return {} as ResolverExtras
192
+ }
193
+
194
+ // Clone the request instance before it's passed to the handler phases
195
+ // and the response resolver so we can always read it for logging.
196
+ // We only clone it once per request to avoid unnecessary overhead.
197
+ private cloneRequestOrGetFromCache(
198
+ request: StrictRequest<DefaultBodyType>,
199
+ ): StrictRequest<DefaultBodyType> {
200
+ const existingClone = RequestHandler.cache.get(request)
201
+
202
+ if (typeof existingClone !== 'undefined') {
203
+ return existingClone
204
+ }
205
+
206
+ const clonedRequest = request.clone()
207
+ RequestHandler.cache.set(request, clonedRequest)
208
+
209
+ return clonedRequest
210
+ }
211
+
212
+ /**
213
+ * Execute this request handler and produce a mocked response
214
+ * using the given resolver function.
215
+ */
216
+ public async run(args: {
217
+ request: StrictRequest<any>
218
+ resolutionContext?: ResponseResolutionContext
219
+ }): Promise<RequestHandlerExecutionResult<ParsedResult> | null> {
220
+ if (this.isUsed && this.options?.once) {
221
+ return null
222
+ }
223
+
224
+ // Clone the request.
225
+ // If this is the first time MSW handles this request, a fresh clone
226
+ // will be created and cached. Upon further handling of the same request,
227
+ // the request clone from the cache will be reused to prevent abundant
228
+ // "abort" listeners and save up resources on cloning.
229
+ const requestClone = this.cloneRequestOrGetFromCache(args.request)
230
+
231
+ const parsedResult = await this.parse({
232
+ request: args.request,
233
+ resolutionContext: args.resolutionContext,
234
+ })
235
+ const shouldInterceptRequest = this.predicate({
236
+ request: args.request,
237
+ parsedResult,
238
+ resolutionContext: args.resolutionContext,
239
+ })
240
+
241
+ if (!shouldInterceptRequest) {
242
+ return null
243
+ }
244
+
245
+ // Re-check isUsed, in case another request hit this handler while we were
246
+ // asynchronously parsing the request.
247
+ if (this.isUsed && this.options?.once) {
248
+ return null
249
+ }
250
+
251
+ this.isUsed = true
252
+
253
+ // Create a response extraction wrapper around the resolver
254
+ // since it can be both an async function and a generator.
255
+ const executeResolver = this.wrapResolver(this.resolver)
256
+
257
+ const resolverExtras = this.extendResolverArgs({
258
+ request: args.request,
259
+ parsedResult,
260
+ })
261
+ const mockedResponse = (await executeResolver({
262
+ ...resolverExtras,
263
+ request: args.request,
264
+ })) as Response
265
+
266
+ const executionResult = this.createExecutionResult({
267
+ // Pass the cloned request to the result so that logging
268
+ // and other consumers could read its body once more.
269
+ request: requestClone,
270
+ response: mockedResponse,
271
+ parsedResult,
272
+ })
273
+
274
+ return executionResult
275
+ }
276
+
277
+ private wrapResolver(
278
+ resolver: ResponseResolver<ResolverExtras>,
279
+ ): ResponseResolver<ResolverExtras> {
280
+ return async (info): Promise<ResponseResolverReturnType<any>> => {
281
+ const result = this.resolverGenerator || (await resolver(info))
282
+
283
+ if (isIterable<AsyncResponseResolverReturnType<any>>(result)) {
284
+ // Immediately mark this handler as unused.
285
+ // Only when the generator is done, the handler will be
286
+ // considered used.
287
+ this.isUsed = false
288
+
289
+ const { value, done } = result[Symbol.iterator]().next()
290
+ const nextResponse = await value
291
+
292
+ if (done) {
293
+ this.isUsed = true
294
+ }
295
+
296
+ // If the generator is done and there is no next value,
297
+ // return the previous generator's value.
298
+ if (!nextResponse && done) {
299
+ invariant(
300
+ this.resolverGeneratorResult,
301
+ 'Failed to returned a previously stored generator response: the value is not a valid Response.',
302
+ )
303
+
304
+ // Clone the previously stored response from the generator
305
+ // so that it could be read again.
306
+ return this.resolverGeneratorResult.clone() as StrictResponse<any>
307
+ }
308
+
309
+ if (!this.resolverGenerator) {
310
+ this.resolverGenerator = result
311
+ }
312
+
313
+ if (nextResponse) {
314
+ // Also clone the response before storing it
315
+ // so it could be read again.
316
+ this.resolverGeneratorResult = nextResponse?.clone()
317
+ }
318
+
319
+ return nextResponse
320
+ }
321
+
322
+ return result
323
+ }
324
+ }
325
+
326
+ private createExecutionResult(args: {
327
+ request: Request
328
+ parsedResult: ParsedResult
329
+ response?: Response
330
+ }): RequestHandlerExecutionResult<ParsedResult> {
331
+ return {
332
+ handler: this,
333
+ request: args.request,
334
+ response: args.response,
335
+ parsedResult: args.parsedResult,
336
+ }
337
+ }
338
+ }
@@ -0,0 +1,15 @@
1
+ import { http } from './http'
2
+
3
+ test('exports all REST API methods', () => {
4
+ expect(http).toBeDefined()
5
+ expect(Object.keys(http)).toEqual([
6
+ 'all',
7
+ 'head',
8
+ 'get',
9
+ 'post',
10
+ 'put',
11
+ 'delete',
12
+ 'patch',
13
+ 'options',
14
+ ])
15
+ })
@@ -0,0 +1,51 @@
1
+ import {
2
+ DefaultBodyType,
3
+ RequestHandlerOptions,
4
+ ResponseResolver,
5
+ } from './handlers/RequestHandler'
6
+ import {
7
+ HttpMethods,
8
+ HttpHandler,
9
+ HttpRequestResolverExtras,
10
+ } from './handlers/HttpHandler'
11
+ import type { Path, PathParams } from './utils/matching/matchRequestUrl'
12
+
13
+ function createHttpHandler<Method extends HttpMethods | RegExp>(
14
+ method: Method,
15
+ ) {
16
+ return <
17
+ Params extends PathParams<keyof Params> = PathParams,
18
+ RequestBodyType extends DefaultBodyType = DefaultBodyType,
19
+ ResponseBodyType extends DefaultBodyType = undefined,
20
+ >(
21
+ path: Path,
22
+ resolver: ResponseResolver<
23
+ HttpRequestResolverExtras<Params>,
24
+ RequestBodyType,
25
+ ResponseBodyType
26
+ >,
27
+ options: RequestHandlerOptions = {},
28
+ ) => {
29
+ return new HttpHandler(method, path, resolver, options)
30
+ }
31
+ }
32
+
33
+ /**
34
+ * A namespace to intercept and mock HTTP requests.
35
+ *
36
+ * @example
37
+ * http.get('/user', resolver)
38
+ * http.post('/post/:id', resolver)
39
+ *
40
+ * @see {@link https://mswjs.io/docs/api/http `http` API reference}
41
+ */
42
+ export const http = {
43
+ all: createHttpHandler(/.+/),
44
+ head: createHttpHandler(HttpMethods.HEAD),
45
+ get: createHttpHandler(HttpMethods.GET),
46
+ post: createHttpHandler(HttpMethods.POST),
47
+ put: createHttpHandler(HttpMethods.PUT),
48
+ delete: createHttpHandler(HttpMethods.DELETE),
49
+ patch: createHttpHandler(HttpMethods.PATCH),
50
+ options: createHttpHandler(HttpMethods.OPTIONS),
51
+ }
@@ -0,0 +1,56 @@
1
+ import { checkGlobals } from './utils/internal/checkGlobals'
2
+
3
+ export { SetupApi } from './SetupApi'
4
+
5
+ /* Request handlers */
6
+ export { RequestHandler } from './handlers/RequestHandler'
7
+ export { http } from './http'
8
+ export { HttpHandler, HttpMethods } from './handlers/HttpHandler'
9
+ export { graphql } from './graphql'
10
+ export { GraphQLHandler } from './handlers/GraphQLHandler'
11
+
12
+ /* Utils */
13
+ export { matchRequestUrl } from './utils/matching/matchRequestUrl'
14
+ export * from './utils/handleRequest'
15
+ export { cleanUrl } from './utils/url/cleanUrl'
16
+
17
+ /**
18
+ * Type definitions.
19
+ */
20
+
21
+ export type { SharedOptions, LifeCycleEventsMap } from './sharedOptions'
22
+
23
+ export type {
24
+ ResponseResolver,
25
+ ResponseResolverReturnType,
26
+ AsyncResponseResolverReturnType,
27
+ RequestHandlerOptions,
28
+ DefaultBodyType,
29
+ DefaultRequestMultipartBody,
30
+ JsonBodyType,
31
+ } from './handlers/RequestHandler'
32
+
33
+ export type {
34
+ RequestQuery,
35
+ HttpRequestParsedResult,
36
+ } from './handlers/HttpHandler'
37
+
38
+ export type {
39
+ GraphQLVariables,
40
+ GraphQLRequestBody,
41
+ GraphQLJsonRequestBody,
42
+ } from './handlers/GraphQLHandler'
43
+
44
+ export type { Path, PathParams, Match } from './utils/matching/matchRequestUrl'
45
+ export type { ParsedGraphQLRequest } from './utils/internal/parseGraphQLRequest'
46
+
47
+ export * from './HttpResponse'
48
+ export * from './delay'
49
+ export { bypass } from './bypass'
50
+ export { passthrough } from './passthrough'
51
+
52
+ // Validate environmental globals before executing any code.
53
+ // This ensures that the library gives user-friendly errors
54
+ // when ran in the environments that require additional polyfills
55
+ // from the end user.
56
+ checkGlobals()
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @vitest-environment node
3
+ */
4
+ import { passthrough } from './passthrough'
5
+
6
+ it('creates a 302 response with the intention header', () => {
7
+ const response = passthrough()
8
+
9
+ expect(response).toBeInstanceOf(Response)
10
+ expect(response.status).toBe(302)
11
+ expect(response.statusText).toBe('Passthrough')
12
+ expect(response.headers.get('x-msw-intention')).toBe('passthrough')
13
+ })
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Performs the intercepted request as-is.
3
+ *
4
+ * This stops request handler lookup so no other handlers
5
+ * can affect this request past this point.
6
+ * Unlike `bypass()`, this will not trigger an additional request.
7
+ *
8
+ * @example
9
+ * http.get('/resource', () => {
10
+ * return passthrough()
11
+ * })
12
+ *
13
+ * @see {@link https://mswjs.io/docs/api/passthrough `passthrough()` API reference}
14
+ */
15
+ export function passthrough(): Response {
16
+ return new Response(null, {
17
+ status: 302,
18
+ statusText: 'Passthrough',
19
+ headers: {
20
+ 'x-msw-intention': 'passthrough',
21
+ },
22
+ })
23
+ }
@@ -0,0 +1,66 @@
1
+ import type { Emitter } from 'strict-event-emitter'
2
+ import type { UnhandledRequestStrategy } from './utils/request/onUnhandledRequest'
3
+
4
+ export interface SharedOptions {
5
+ /**
6
+ * Specifies how to react to a request that has no corresponding
7
+ * request handler. Warns on unhandled requests by default.
8
+ *
9
+ * @example worker.start({ onUnhandledRequest: 'bypass' })
10
+ * @example worker.start({ onUnhandledRequest: 'warn' })
11
+ * @example server.listen({ onUnhandledRequest: 'error' })
12
+ */
13
+ onUnhandledRequest?: UnhandledRequestStrategy
14
+ }
15
+
16
+ export type LifeCycleEventsMap = {
17
+ 'request:start': [
18
+ args: {
19
+ request: Request
20
+ requestId: string
21
+ },
22
+ ]
23
+ 'request:match': [
24
+ args: {
25
+ request: Request
26
+ requestId: string
27
+ },
28
+ ]
29
+ 'request:unhandled': [
30
+ args: {
31
+ request: Request
32
+ requestId: string
33
+ },
34
+ ]
35
+ 'request:end': [
36
+ args: {
37
+ request: Request
38
+ requestId: string
39
+ },
40
+ ]
41
+ 'response:mocked': [
42
+ args: {
43
+ response: Response
44
+ request: Request
45
+ requestId: string
46
+ },
47
+ ]
48
+ 'response:bypass': [
49
+ args: {
50
+ response: Response
51
+ request: Request
52
+ requestId: string
53
+ },
54
+ ]
55
+ unhandledException: [
56
+ args: {
57
+ error: Error
58
+ request: Request
59
+ requestId: string
60
+ },
61
+ ]
62
+ }
63
+
64
+ export type LifeCycleEventEmitter<
65
+ EventsMap extends Record<string | symbol, any>,
66
+ > = Pick<Emitter<EventsMap>, 'on' | 'removeListener' | 'removeAllListeners'>
@@ -0,0 +1,20 @@
1
+ type Fn = (...arg: any[]) => any
2
+
3
+ export type MaybePromise<T> = T | Promise<T>
4
+
5
+ export type RequiredDeep<
6
+ Type,
7
+ U extends Record<string, unknown> | Fn | undefined = undefined,
8
+ > = Type extends Fn
9
+ ? Type
10
+ : /**
11
+ * @note The "Fn" type satisfies the predicate below.
12
+ * It must always come first, before the Record check.
13
+ */
14
+ Type extends Record<string, any>
15
+ ? {
16
+ [Key in keyof Type]-?: NonNullable<Type[Key]> extends NonNullable<U>
17
+ ? NonNullable<Type[Key]>
18
+ : RequiredDeep<NonNullable<Type[Key]>, U>
19
+ }
20
+ : Type
@@ -0,0 +1,56 @@
1
+ import statuses from '@bundled-es-modules/statuses'
2
+ import type { HttpResponseInit } from '../../HttpResponse'
3
+
4
+ const { message } = statuses
5
+
6
+ export interface HttpResponseDecoratedInit extends HttpResponseInit {
7
+ status: number
8
+ statusText: string
9
+ headers: Headers
10
+ }
11
+
12
+ export function normalizeResponseInit(
13
+ init: HttpResponseInit = {},
14
+ ): HttpResponseDecoratedInit {
15
+ const status = init?.status || 200
16
+ const statusText = init?.statusText || message[status] || ''
17
+ const headers = new Headers(init?.headers)
18
+
19
+ return {
20
+ ...init,
21
+ headers,
22
+ status,
23
+ statusText,
24
+ }
25
+ }
26
+
27
+ export function decorateResponse(
28
+ response: Response,
29
+ init: HttpResponseDecoratedInit,
30
+ ): Response {
31
+ // Allow to mock the response type.
32
+ if (init.type) {
33
+ Object.defineProperty(response, 'type', {
34
+ value: init.type,
35
+ enumerable: true,
36
+ writable: false,
37
+ })
38
+ }
39
+
40
+ // Cookie forwarding is only relevant in the browser.
41
+ if (typeof document !== 'undefined') {
42
+ // Write the mocked response cookies to the document.
43
+ // Note that Fetch API Headers will concatenate multiple "Set-Cookie"
44
+ // headers into a single comma-separated string, just as it does
45
+ // with any other multi-value headers.
46
+ const responseCookies = init.headers.get('Set-Cookie')?.split(',') || []
47
+
48
+ for (const cookieString of responseCookies) {
49
+ // No need to parse the cookie headers because it's defined
50
+ // as the valid cookie string to begin with.
51
+ document.cookie = cookieString
52
+ }
53
+ }
54
+
55
+ return response
56
+ }
@@ -0,0 +1,55 @@
1
+ import {
2
+ RequestHandler,
3
+ RequestHandlerExecutionResult,
4
+ } from '../handlers/RequestHandler'
5
+
6
+ export interface ResponseLookupResult {
7
+ handler: RequestHandler
8
+ parsedResult?: any
9
+ response?: Response
10
+ }
11
+
12
+ export interface ResponseResolutionContext {
13
+ baseUrl?: string
14
+ }
15
+
16
+ /**
17
+ * Returns a mocked response for a given request using following request handlers.
18
+ */
19
+ export const getResponse = async <Handler extends Array<RequestHandler>>(
20
+ request: Request,
21
+ handlers: Handler,
22
+ resolutionContext?: ResponseResolutionContext,
23
+ ): Promise<ResponseLookupResult | null> => {
24
+ let matchingHandler: RequestHandler | null = null
25
+ let result: RequestHandlerExecutionResult<any> | null = null
26
+
27
+ for (const handler of handlers) {
28
+ result = await handler.run({ request, resolutionContext })
29
+
30
+ // If the handler produces some result for this request,
31
+ // it automatically becomes matching.
32
+ if (result !== null) {
33
+ matchingHandler = handler
34
+ }
35
+
36
+ // Stop the lookup if this handler returns a mocked response.
37
+ // If it doesn't, it will still be considered the last matching
38
+ // handler until any of them returns a response. This way we can
39
+ // distinguish between fallthrough handlers without responses
40
+ // and the lack of a matching handler.
41
+ if (result?.response) {
42
+ break
43
+ }
44
+ }
45
+
46
+ if (matchingHandler) {
47
+ return {
48
+ handler: matchingHandler,
49
+ parsedResult: result?.parsedResult,
50
+ response: result?.response,
51
+ }
52
+ }
53
+
54
+ return null
55
+ }