{"version":3,"file":"fetch.mjs","names":["undici"],"sources":["../src/fetch.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n                       🗲 Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website:                  https://stormsoftware.com\n Repository:               https://github.com/storm-software/stryke\n Documentation:            https://docs.stormsoftware.com/projects/stryke\n Contact:                  https://stormsoftware.com/contact\n\n SPDX-License-Identifier:  Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isSetString } from \"@stryke/type-checks/is-set-string\";\nimport { isValidURL } from \"@stryke/url/helpers\";\nimport { defu } from \"defu\";\nimport { contentType } from \"mime-types\";\nimport { parseFilename } from \"ufo\";\nimport type { RequestInfo, RequestInit } from \"undici\";\nimport { fetch as undici } from \"undici\";\n\nexport type FetchRequestOptions = RequestInit & {\n  /**\n   * Timeout in milliseconds\n   *\n   * @defaultValue 5000\n   */\n  timeout?: number;\n\n  /**\n   * Number of retries for the fetch request\n   *\n   * @defaultValue 3\n   */\n  retries?: number;\n\n  /**\n   * Delay between retries in milliseconds\n   *\n   * @defaultValue 1000\n   */\n  retryDelay?: number;\n\n  /**\n   * Function to determine if a retry should be attempted based on the error and attempt number\n   */\n  retryOn?: (error: any, attempt: number) => boolean;\n};\n\nexport const DEFAULT_USER_AGENT =\n  \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36\";\n\n/**\n * Fetches a resource from a URL.\n *\n * @param input - The URL to fetch.\n * @param options - Additional fetch options.\n * @returns The fetched response.\n */\nexport async function fetchRequest(\n  input: RequestInfo,\n  options: FetchRequestOptions = {}\n) {\n  if (isSetString(input) && !isValidURL(input)) {\n    throw new Error(`Invalid URL format provided: ${input}`);\n  }\n\n  const {\n    retries = 3,\n    retryDelay = 1000,\n    retryOn,\n    timeout = 5000,\n    ...requestOptions\n  } = options;\n\n  const type = contentType(parseFilename(input.toString()) || \"\");\n\n  const delay = async (ms: number) =>\n    new Promise(resolve => {\n      setTimeout(resolve, ms);\n    });\n\n  for (let attempt = 0; ; attempt++) {\n    const abort = new AbortController();\n    const timeoutId = setTimeout(() => abort.abort(), timeout);\n\n    try {\n      const response = await undici(\n        input,\n        defu(\n          requestOptions,\n          {\n            signal: abort.signal,\n            headers: {\n              \"User-Agent\": DEFAULT_USER_AGENT\n            }\n          },\n          type\n            ? {\n                headers: {\n                  \"Content-Type\": type,\n                  Accept: type\n                }\n              }\n            : {}\n        )\n      );\n\n      clearTimeout(timeoutId);\n      return response;\n    } catch (error) {\n      clearTimeout(timeoutId);\n\n      const shouldRetry = retryOn ? retryOn(error, attempt + 1) : true;\n      if (attempt >= retries || !shouldRetry) {\n        throw error;\n      }\n\n      if (retryDelay > 0) {\n        await delay(retryDelay);\n      }\n    }\n  }\n}\n\nexport const fetch = fetchRequest;\n"],"mappings":";;;;;;;;;;;;;;;AAsDA,MAAa,qBACX;;;;;;;;AASF,eAAsB,aACpB,OACA,UAA+B,CAAC,GAChC;CACA,IAAI,YAAY,KAAK,KAAK,CAAC,WAAW,KAAK,GACzC,MAAM,IAAI,MAAM,gCAAgC,OAAO;CAGzD,MAAM,EACJ,UAAU,GACV,aAAa,KACb,SACA,UAAU,KACV,GAAG,mBACD;CAEJ,MAAM,OAAO,YAAY,cAAc,MAAM,SAAS,CAAC,KAAK,EAAE;CAE9D,MAAM,QAAQ,OAAO,OACnB,IAAI,SAAQ,YAAW;EACrB,WAAW,SAAS,EAAE;CACxB,CAAC;CAEH,KAAK,IAAI,UAAU,IAAK,WAAW;EACjC,MAAM,QAAQ,IAAI,gBAAgB;EAClC,MAAM,YAAY,iBAAiB,MAAM,MAAM,GAAG,OAAO;EAEzD,IAAI;GACF,MAAM,WAAW,MAAMA,QACrB,OACA,KACE,gBACA;IACE,QAAQ,MAAM;IACd,SAAS,EACP,cAAc,mBAChB;GACF,GACA,OACI,EACE,SAAS;IACP,gBAAgB;IAChB,QAAQ;GACV,EACF,IACA,CAAC,CACP,CACF;GAEA,aAAa,SAAS;GACtB,OAAO;EACT,SAAS,OAAO;GACd,aAAa,SAAS;GAEtB,MAAM,cAAc,UAAU,QAAQ,OAAO,UAAU,CAAC,IAAI;GAC5D,IAAI,WAAW,WAAW,CAAC,aACzB,MAAM;GAGR,IAAI,aAAa,GACf,MAAM,MAAM,UAAU;EAE1B;CACF;AACF;AAEA,MAAa,QAAQ"}