{"version":3,"sources":["../../../src/core/handlers/HttpHandler.ts"],"sourcesContent":["import { ResponseResolutionContext } from '../utils/executeHandlers'\nimport { devUtils } from '../utils/internal/devUtils'\nimport { isStringEqual } from '../utils/internal/isStringEqual'\nimport { getStatusCodeColor } from '../utils/logging/getStatusCodeColor'\nimport { getTimestamp } from '../utils/logging/getTimestamp'\nimport { serializeRequest } from '../utils/logging/serializeRequest'\nimport { serializeResponse } from '../utils/logging/serializeResponse'\nimport {\n matchRequestUrl,\n Match,\n Path,\n PathParams,\n} from '../utils/matching/matchRequestUrl'\nimport { toPublicUrl } from '../utils/request/toPublicUrl'\nimport { getAllRequestCookies } from '../utils/request/getRequestCookies'\nimport { cleanUrl, getSearchParams } from '../utils/url/cleanUrl'\nimport {\n RequestHandler,\n RequestHandlerDefaultInfo,\n RequestHandlerOptions,\n ResponseResolver,\n} from './RequestHandler'\n\ntype HttpHandlerMethod = string | RegExp\n\nexport interface HttpHandlerInfo extends RequestHandlerDefaultInfo {\n method: HttpHandlerMethod\n path: Path\n}\n\nexport enum HttpMethods {\n HEAD = 'HEAD',\n GET = 'GET',\n POST = 'POST',\n PUT = 'PUT',\n PATCH = 'PATCH',\n OPTIONS = 'OPTIONS',\n DELETE = 'DELETE',\n}\n\nexport type RequestQuery = {\n [queryName: string]: string\n}\n\nexport type HttpRequestParsedResult = {\n match: Match\n cookies: Record\n}\n\nexport type HttpRequestResolverExtras = {\n params: Params\n cookies: Record\n}\n\n/**\n * Request handler for HTTP requests.\n * Provides request matching based on method and URL.\n */\nexport class HttpHandler extends RequestHandler<\n HttpHandlerInfo,\n HttpRequestParsedResult,\n HttpRequestResolverExtras\n> {\n constructor(\n method: HttpHandlerMethod,\n path: Path,\n resolver: ResponseResolver, any, any>,\n options?: RequestHandlerOptions,\n ) {\n super({\n info: {\n header: `${method} ${path}`,\n path,\n method,\n },\n resolver,\n options,\n })\n\n this.checkRedundantQueryParameters()\n }\n\n private checkRedundantQueryParameters() {\n const { method, path } = this.info\n\n if (path instanceof RegExp) {\n return\n }\n\n const url = cleanUrl(path)\n\n // Bypass request handler URLs that have no redundant characters.\n if (url === path) {\n return\n }\n\n const searchParams = getSearchParams(path)\n const queryParams: string[] = []\n\n searchParams.forEach((_, paramName) => {\n queryParams.push(paramName)\n })\n\n devUtils.warn(\n `Found a redundant usage of query parameters in the request handler URL for \"${method} ${path}\". Please match against a path instead and access query parameters using \"new URL(request.url).searchParams\" instead. Learn more: https://mswjs.io/docs/recipes/query-parameters`,\n )\n }\n\n async parse(args: {\n request: Request\n resolutionContext?: ResponseResolutionContext\n }) {\n const url = new URL(args.request.url)\n const match = matchRequestUrl(\n url,\n this.info.path,\n args.resolutionContext?.baseUrl,\n )\n const cookies = getAllRequestCookies(args.request)\n\n return {\n match,\n cookies,\n }\n }\n\n predicate(args: { request: Request; parsedResult: HttpRequestParsedResult }) {\n const hasMatchingMethod = this.matchMethod(args.request.method)\n const hasMatchingUrl = args.parsedResult.match.matches\n return hasMatchingMethod && hasMatchingUrl\n }\n\n private matchMethod(actualMethod: string): boolean {\n return this.info.method instanceof RegExp\n ? this.info.method.test(actualMethod)\n : isStringEqual(this.info.method, actualMethod)\n }\n\n protected extendResolverArgs(args: {\n request: Request\n parsedResult: HttpRequestParsedResult\n }) {\n return {\n params: args.parsedResult.match?.params || {},\n cookies: args.parsedResult.cookies,\n }\n }\n\n async log(args: { request: Request; response: Response }) {\n const publicUrl = toPublicUrl(args.request.url)\n const loggedRequest = await serializeRequest(args.request)\n const loggedResponse = await serializeResponse(args.response)\n const statusColor = getStatusCodeColor(loggedResponse.status)\n\n // eslint-disable-next-line no-console\n console.groupCollapsed(\n devUtils.formatMessage(\n `${getTimestamp()} ${args.request.method} ${publicUrl} (%c${\n loggedResponse.status\n } ${loggedResponse.statusText}%c)`,\n ),\n `color:${statusColor}`,\n 'color:inherit',\n )\n // eslint-disable-next-line no-console\n console.log('Request', loggedRequest)\n // eslint-disable-next-line no-console\n console.log('Handler:', this)\n // eslint-disable-next-line no-console\n console.log('Response', loggedResponse)\n // eslint-disable-next-line no-console\n console.groupEnd()\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,sBAAyB;AACzB,2BAA8B;AAC9B,gCAAmC;AACnC,0BAA6B;AAC7B,8BAAiC;AACjC,+BAAkC;AAClC,6BAKO;AACP,yBAA4B;AAC5B,+BAAqC;AACrC,sBAA0C;AAC1C,4BAKO;AASA,IAAK,cAAL,kBAAKA,iBAAL;AACL,EAAAA,aAAA,UAAO;AACP,EAAAA,aAAA,SAAM;AACN,EAAAA,aAAA,UAAO;AACP,EAAAA,aAAA,SAAM;AACN,EAAAA,aAAA,WAAQ;AACR,EAAAA,aAAA,aAAU;AACV,EAAAA,aAAA,YAAS;AAPC,SAAAA;AAAA,GAAA;AA4BL,MAAM,oBAAoB,qCAI/B;AAAA,EACA,YACE,QACA,MACA,UACA,SACA;AACA,UAAM;AAAA,MACJ,MAAM;AAAA,QACJ,QAAQ,GAAG,MAAM,IAAI,IAAI;AAAA,QACzB;AAAA,QACA;AAAA,MACF;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC;AAED,SAAK,8BAA8B;AAAA,EACrC;AAAA,EAEQ,gCAAgC;AACtC,UAAM,EAAE,QAAQ,KAAK,IAAI,KAAK;AAE9B,QAAI,gBAAgB,QAAQ;AAC1B;AAAA,IACF;AAEA,UAAM,UAAM,0BAAS,IAAI;AAGzB,QAAI,QAAQ,MAAM;AAChB;AAAA,IACF;AAEA,UAAM,mBAAe,iCAAgB,IAAI;AACzC,UAAM,cAAwB,CAAC;AAE/B,iBAAa,QAAQ,CAAC,GAAG,cAAc;AACrC,kBAAY,KAAK,SAAS;AAAA,IAC5B,CAAC;AAED,6BAAS;AAAA,MACP,+EAA+E,MAAM,IAAI,IAAI;AAAA,IAC/F;AAAA,EACF;AAAA,EAEA,MAAM,MAAM,MAGT;AACD,UAAM,MAAM,IAAI,IAAI,KAAK,QAAQ,GAA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