{"version":3,"sources":["../../../src/gateway/fetch.ts"],"sourcesContent":["import { Gateway } from './gateway'\nimport Response from '../response'\nimport { configs } from '../mappersmith'\n// Fetch can be used in nodejs, so it should always use the btoa util\nimport { assign, btoa } from '../utils/index'\nimport { createTimeoutError } from './timeout-error'\nimport type { Method } from './types'\n\n/**\n * Gateway which uses the \"fetch\" implementation configured in \"configs.fetch\".\n * By default \"configs.fetch\" will receive the global fetch, this gateway doesn't\n * use browser specific code, with a proper \"fetch\" implementation it can also be\n * used with node.js\n */\nexport class Fetch extends Gateway {\n  get() {\n    this.performRequest('get')\n  }\n\n  head() {\n    this.performRequest('head')\n  }\n\n  post() {\n    this.performRequest('post')\n  }\n\n  put() {\n    this.performRequest('put')\n  }\n\n  patch() {\n    this.performRequest('patch')\n  }\n\n  delete() {\n    this.performRequest('delete')\n  }\n\n  performRequest(requestMethod: Method) {\n    const fetch = configs.fetch\n\n    if (!fetch) {\n      throw new Error(\n        `[Mappersmith] global fetch does not exist, please assign \"configs.fetch\" to a valid implementation`\n      )\n    }\n\n    const customHeaders: Record<string, string> = {}\n    const body = this.prepareBody(requestMethod, customHeaders) as BodyInit\n    const auth = this.request.auth()\n\n    if (auth) {\n      const username = auth.username || ''\n      const password = auth.password || ''\n      customHeaders['authorization'] = `Basic ${btoa(`${username}:${password}`)}`\n    }\n\n    const headers = assign(customHeaders, this.request.headers())\n    const method = this.shouldEmulateHTTP() ? 'post' : requestMethod\n    const signal = this.request.signal()\n    const init: RequestInit = assign({ method, headers, body, signal }, this.options().Fetch)\n    const timeout = this.request.timeout()\n\n    let timer: ReturnType<typeof setTimeout> | null = null\n    let canceled = false\n\n    if (timeout) {\n      timer = setTimeout(() => {\n        canceled = true\n        const error = createTimeoutError(`Timeout (${timeout}ms)`)\n        this.dispatchClientError(error.message, error)\n      }, timeout)\n    }\n\n    fetch(this.request.url(), init)\n      .then((fetchResponse) => {\n        if (canceled) {\n          return\n        }\n\n        timer && clearTimeout(timer)\n\n        let responseData: Promise<ArrayBuffer> | Promise<string> | Promise<Buffer>\n        if (this.request.isBinary()) {\n          // eslint-disable-next-line @typescript-eslint/no-explicit-any\n          if (typeof (fetchResponse as any).buffer === 'function') {\n            // eslint-disable-next-line @typescript-eslint/no-explicit-any\n            responseData = (fetchResponse as any).buffer()\n          } else {\n            responseData = fetchResponse.arrayBuffer()\n          }\n        } else {\n          responseData = fetchResponse.text()\n        }\n\n        responseData.then((data) => {\n          this.dispatchResponse(this.createResponse(fetchResponse, data))\n        })\n      })\n      .catch((error) => {\n        if (canceled) {\n          return\n        }\n\n        timer && clearTimeout(timer)\n        this.dispatchClientError(error.message, error)\n      })\n  }\n\n  createResponse(fetchResponse: globalThis.Response, data: string | ArrayBuffer | Buffer) {\n    const status = fetchResponse.status\n    const responseHeaders: Record<string, string> = {}\n    fetchResponse.headers.forEach((value, key) => {\n      responseHeaders[key] = value\n    })\n\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    return new Response(this.request, status, data as any, responseHeaders)\n  }\n}\n\nexport default Fetch\n"],"mappings":";AAAA,SAAS,eAAe;AACxB,OAAO,cAAc;AACrB,SAAS,eAAe;AAExB,SAAS,QAAQ,YAAY;AAC7B,SAAS,0BAA0B;AAS5B,IAAM,QAAN,cAAoB,QAAQ;AAAA,EACjC,MAAM;AACJ,SAAK,eAAe,KAAK;AAAA,EAC3B;AAAA,EAEA,OAAO;AACL,SAAK,eAAe,MAAM;AAAA,EAC5B;AAAA,EAEA,OAAO;AACL,SAAK,eAAe,MAAM;AAAA,EAC5B;AAAA,EAEA,MAAM;AACJ,SAAK,eAAe,KAAK;AAAA,EAC3B;AAAA,EAEA,QAAQ;AACN,SAAK,eAAe,OAAO;AAAA,EAC7B;AAAA,EAEA,SAAS;AACP,SAAK,eAAe,QAAQ;AAAA,EAC9B;AAAA,EAEA,eAAe,eAAuB;AACpC,UAAM,QAAQ,QAAQ;AAEtB,QAAI,CAAC,OAAO;AACV,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA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