{"version":3,"sources":["../../src/core/HttpResponse.ts"],"sourcesContent":["import { FetchResponse } from '@mswjs/interceptors'\nimport type { DefaultBodyType, JsonBodyType } from './handlers/RequestHandler'\nimport type { NoInfer } from './typeUtils'\nimport {\n  decorateResponse,\n  normalizeResponseInit,\n} from './utils/HttpResponse/decorators'\n\nexport interface HttpResponseInit extends ResponseInit {\n  type?: ResponseType\n}\n\nexport const bodyType: unique symbol = Symbol('bodyType')\nexport type DefaultUnsafeFetchResponse = Response & {\n  [bodyType]?: never\n}\n\nexport interface StrictRequest<BodyType extends JsonBodyType> extends Request {\n  json(): Promise<BodyType>\n  clone(): StrictRequest<BodyType>\n}\n\n/**\n * Opaque `Response` type that supports strict body type.\n *\n * @deprecated Please use {@link HttpResponse} instead.\n */\nexport type StrictResponse<BodyType extends DefaultBodyType> =\n  HttpResponse<BodyType>\n\nexport const kDefaultContentType = Symbol.for('kDefaultContentType')\n\n/**\n * A drop-in replacement for the standard `Response` class\n * to allow additional features, like mocking the response `Set-Cookie` header.\n *\n * @example\n * new HttpResponse('Hello world', { status: 201 })\n * HttpResponse.json({ name: 'John' })\n * HttpResponse.formData(form)\n *\n * @see {@link https://mswjs.io/docs/api/http-response `HttpResponse` API reference}\n */\nexport class HttpResponse<\n  BodyType extends DefaultBodyType,\n> extends FetchResponse {\n  readonly [bodyType]: BodyType = null as any\n\n  constructor(body?: NoInfer<BodyType> | null, init?: HttpResponseInit) {\n    const responseInit = normalizeResponseInit(init)\n    super(body as BodyInit, responseInit)\n    decorateResponse(this, responseInit)\n  }\n\n  static error(): HttpResponse<any> {\n    return super.error() as HttpResponse<any>\n  }\n\n  /**\n   * Create a `Response` with a `Content-Type: \"text/plain\"` body.\n   * @example\n   * HttpResponse.text('hello world')\n   * HttpResponse.text('Error', { status: 500 })\n   */\n  static text<BodyType extends string>(\n    body?: NoInfer<BodyType> | null,\n    init?: HttpResponseInit,\n  ): HttpResponse<BodyType> {\n    const responseInit = normalizeResponseInit(init)\n    const hasExplicitContentType = responseInit.headers.has('Content-Type')\n\n    if (!hasExplicitContentType) {\n      responseInit.headers.set('Content-Type', 'text/plain')\n    }\n\n    // Automatically set the \"Content-Length\" response header\n    // for non-empty text responses. This enforces consistency and\n    // brings mocked responses closer to production.\n    if (!responseInit.headers.has('Content-Length')) {\n      responseInit.headers.set(\n        'Content-Length',\n        body ? new Blob([body]).size.toString() : '0',\n      )\n    }\n\n    const response = new HttpResponse(body, responseInit)\n\n    if (!hasExplicitContentType) {\n      Object.defineProperty(response, kDefaultContentType, {\n        value: true,\n        enumerable: false,\n      })\n    }\n\n    return response\n  }\n\n  /**\n   * Create a `Response` with a `Content-Type: \"application/json\"` body.\n   * @example\n   * HttpResponse.json({ firstName: 'John' })\n   * HttpResponse.json({ error: 'Not Authorized' }, { status: 401 })\n   */\n  static json<BodyType extends JsonBodyType>(\n    body?: NoInfer<BodyType> | null | undefined,\n    init?: HttpResponseInit,\n  ): HttpResponse<BodyType> {\n    const responseInit = normalizeResponseInit(init)\n    const hasExplicitContentType = responseInit.headers.has('Content-Type')\n\n    if (!hasExplicitContentType) {\n      responseInit.headers.set('Content-Type', 'application/json')\n    }\n\n    /**\n     * @note TypeScript is incorrect here.\n     * Stringifying undefined will return undefined.\n     */\n    const responseText = JSON.stringify(body) as string | undefined\n\n    if (!responseInit.headers.has('Content-Length')) {\n      responseInit.headers.set(\n        'Content-Length',\n        responseText ? new Blob([responseText]).size.toString() : '0',\n      )\n    }\n\n    const response = new HttpResponse(responseText, responseInit)\n\n    if (!hasExplicitContentType) {\n      Object.defineProperty(response, kDefaultContentType, {\n        value: true,\n        enumerable: false,\n      })\n    }\n\n    return response as HttpResponse<BodyType>\n  }\n\n  /**\n   * Create a `Response` with a `Content-Type: \"application/xml\"` body.\n   * @example\n   * HttpResponse.xml(`<user name=\"John\" />`)\n   * HttpResponse.xml(`<article id=\"abc-123\" />`, { status: 201 })\n   */\n  static xml<BodyType extends string>(\n    body?: BodyType | null,\n    init?: HttpResponseInit,\n  ): HttpResponse<BodyType> {\n    const responseInit = normalizeResponseInit(init)\n    const hasExplicitContentType = responseInit.headers.has('Content-Type')\n\n    if (!hasExplicitContentType) {\n      responseInit.headers.set('Content-Type', 'text/xml')\n    }\n\n    const response = new HttpResponse(body, responseInit)\n\n    if (!hasExplicitContentType) {\n      Object.defineProperty(response, kDefaultContentType, {\n        value: true,\n        enumerable: false,\n      })\n    }\n\n    return response as HttpResponse<BodyType>\n  }\n\n  /**\n   * Create a `Response` with a `Content-Type: \"text/html\"` body.\n   * @example\n   * HttpResponse.html(`<p class=\"author\">Jane Doe</p>`)\n   * HttpResponse.html(`<main id=\"abc-123\">Main text</main>`, { status: 201 })\n   */\n  static html<BodyType extends string>(\n    body?: BodyType | null,\n    init?: HttpResponseInit,\n  ): HttpResponse<BodyType> {\n    const responseInit = normalizeResponseInit(init)\n    const hasExplicitContentType = responseInit.headers.has('Content-Type')\n\n    if (!hasExplicitContentType) {\n      responseInit.headers.set('Content-Type', 'text/html')\n    }\n\n    const response = new HttpResponse(body, responseInit)\n\n    if (!hasExplicitContentType) {\n      Object.defineProperty(response, kDefaultContentType, {\n        value: true,\n        enumerable: false,\n      })\n    }\n\n    return response as HttpResponse<BodyType>\n  }\n\n  /**\n   * Create a `Response` with an `ArrayBuffer` body.\n   * @example\n   * const buffer = new ArrayBuffer(3)\n   * const view = new Uint8Array(buffer)\n   * view.set([1, 2, 3])\n   *\n   * HttpResponse.arrayBuffer(buffer)\n   */\n  static arrayBuffer<BodyType extends ArrayBuffer | SharedArrayBuffer>(\n    body?: BodyType,\n    init?: HttpResponseInit,\n  ): HttpResponse<BodyType> {\n    const responseInit = normalizeResponseInit(init)\n    const hasExplicitContentType = responseInit.headers.has('Content-Type')\n\n    if (!hasExplicitContentType) {\n      responseInit.headers.set('Content-Type', 'application/octet-stream')\n    }\n\n    if (body && !responseInit.headers.has('Content-Length')) {\n      responseInit.headers.set('Content-Length', body.byteLength.toString())\n    }\n\n    const response = new HttpResponse(body, responseInit)\n\n    if (!hasExplicitContentType) {\n      Object.defineProperty(response, kDefaultContentType, {\n        value: true,\n        enumerable: false,\n      })\n    }\n\n    return response as HttpResponse<BodyType>\n  }\n\n  /**\n   * Create a `Response` with a `FormData` body.\n   * @example\n   * const data = new FormData()\n   * data.set('name', 'Alice')\n   *\n   * HttpResponse.formData(data)\n   */\n  static formData(\n    body?: FormData,\n    init?: HttpResponseInit,\n  ): HttpResponse<FormData> {\n    return new HttpResponse(body, normalizeResponseInit(init))\n  }\n}\n"],"mappings":"AAAA,SAAS,qBAAqB;AAG9B;AAAA,EACE;AAAA,EACA;AAAA,OACK;AAMA,MAAM,WAA0B,OAAO,UAAU;AAkBjD,MAAM,sBAAsB,OAAO,IAAI,qBAAqB;AAa5D,MAAM,qBAEH,cAAc;AAAA,EACtB,CAAU,QAAQ,IAAc;AAAA,EAEhC,YAAY,MAAiC,MAAyB;AACpE,UAAM,eAAe,sBAAsB,IAAI;AAC/C,UAAM,MAAkB,YAAY;AACpC,qBAAiB,MAAM,YAAY;AAAA,EACrC;AAAA,EAEA,OAAO,QAA2B;AAChC,WAAO,MAAM,MAAM;AAAA,EACrB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAO,KACL,MACA,MACwB;AACxB,UAAM,eAAe,sBAAsB,IAAI;AAC/C,UAAM,yBAAyB,aAAa,QAAQ,IAAI,cAAc;AAEtE,QAAI,CAAC,wBAAwB;AAC3B,mBAAa,QAAQ,IAAI,gBAAgB,YAAY;AAAA,IACvD;AAKA,QAAI,CAAC,aAAa,QAAQ,IAAI,gBAAgB,GAAG;AAC/C,mBAAa,QAAQ;AAAA,QACnB;AAAA,QACA,OAAO,IAAI,KAAK,CAAC,IAAI,CAAC,EAAE,KAAK,SAAS,IAAI;AAAA,MAC5C;AAAA,IACF;AAEA,UAAM,WAAW,IAAI,aAAa,MAAM,YAAY;AAEpD,QAAI,CAAC,wBAAwB;AAC3B,aAAO,eAAe,UAAU,qBAAqB;AAAA,QACnD,OAA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