{"version":3,"file":"fetch.mjs","sources":["../../src/utils/fetch.ts"],"sourcesContent":["import type { Core, Modules } from '@strapi/types';\nimport { ProxyAgent } from 'undici';\n\n// TODO: once core Node exposes a stable way to create a ProxyAgent we will use that instead of undici\n\ninterface StrapiFetchOptions {\n  logs?: boolean;\n}\n\n// Create a wrapper for Node's Fetch API that applies a global proxy\nexport const createStrapiFetch = (\n  strapi: Core.Strapi,\n  options?: StrapiFetchOptions\n): Modules.Fetch.Fetch => {\n  const { logs = true } = options ?? {};\n\n  function strapiFetch(url: RequestInfo | URL, options?: RequestInit) {\n    const fetchOptions = {\n      ...(strapiFetch.dispatcher ? { dispatcher: strapiFetch.dispatcher } : {}),\n      ...options,\n    };\n\n    if (logs) {\n      strapi.log.debug(`Making request for ${url}`);\n    }\n\n    return fetch(url, fetchOptions);\n  }\n\n  const proxy =\n    strapi.config.get<ConstructorParameters<typeof ProxyAgent>[0]>('server.proxy.fetch') ||\n    strapi.config.get<string>('server.proxy.global');\n\n  if (proxy) {\n    if (logs) {\n      strapi.log.info(`Using proxy for Fetch requests: ${proxy}`);\n    }\n    strapiFetch.dispatcher = new ProxyAgent(proxy);\n  }\n\n  return strapiFetch;\n};\n\nexport type Fetch = Modules.Fetch.Fetch;\n"],"names":["createStrapiFetch","strapi","options","logs","strapiFetch","url","fetchOptions","dispatcher","log","debug","fetch","proxy","config","get","info","ProxyAgent"],"mappings":";;AASA;AACO,MAAMA,iBAAAA,GAAoB,CAC/BC,MAAAA,EACAC,OAAAA,GAAAA;AAEA,IAAA,MAAM,EAAEC,IAAAA,GAAO,IAAI,EAAE,GAAGD,WAAW,EAAC;IAEpC,SAASE,WAAAA,CAAYC,GAAsB,EAAEH,OAAqB,EAAA;AAChE,QAAA,MAAMI,YAAAA,GAAe;YACnB,GAAIF,WAAAA,CAAYG,UAAU,GAAG;AAAEA,gBAAAA,UAAAA,EAAYH,YAAYG;AAAW,aAAA,GAAI,EAAE;AACxE,YAAA,GAAGL;AACL,SAAA;AAEA,QAAA,IAAIC,IAAAA,EAAM;AACRF,YAAAA,MAAAA,CAAOO,GAAG,CAACC,KAAK,CAAC,CAAC,mBAAmB,EAAEJ,GAAAA,CAAAA,CAAK,CAAA;AAC9C,QAAA;AAEA,QAAA,OAAOK,MAAML,GAAAA,EAAKC,YAAAA,CAAAA;AACpB,IAAA;IAEA,MAAMK,KAAAA,GACJV,MAAAA,CAAOW,MAAM,CAACC,GAAG,CAA8C,oBAAA,CAAA,IAC/DZ,MAAAA,CAAOW,MAAM,CAACC,GAAG,CAAS,qBAAA,CAAA;AAE5B,IAAA,IAAIF,KAAAA,EAAO;AACT,QAAA,IAAIR,IAAAA,EAAM;AACRF,YAAAA,MAAAA,CAAOO,GAAG,CAACM,IAAI,CAAC,CAAC,gCAAgC,EAAEH,KAAAA,CAAAA,CAAO,CAAA;AAC5D,QAAA;QACAP,WAAAA,CAAYG,UAAU,GAAG,IAAIQ,UAAAA,CAAWJ,KAAAA,CAAAA;AAC1C,IAAA;IAEA,OAAOP,WAAAA;AACT;;;;"}