{"version":3,"sources":["../src/index.ts","../src/polly-fetch.ts"],"sourcesContent":["export { pollyFetch, HttpError, type PollyFetchOptions } from './polly-fetch';\n","import type { IPolicy } from 'polly-ts-core';\n\n/**\n * Options for configuring pollyFetch.\n */\nexport interface PollyFetchOptions {\n  /**\n   * Predicate to determine if a response should be treated as a failure.\n   * If true, an HTTPError will be thrown, triggering policy failure handling.\n   * Default: Returns true for status 429 and 5xx.\n   */\n  shouldFail?: (response: Response) => boolean;\n}\n\n/**\n * Custom error class for HTTP failures.\n */\nexport class HttpError extends Error {\n  constructor(\n    public readonly response: Response,\n    message?: string,\n  ) {\n    super(message ?? `HTTP ${String(response.status)} ${response.statusText}`);\n    this.name = 'HttpError';\n  }\n}\n\nconst defaultShouldFail = (response: Response): boolean => {\n  return response.status === 429 || response.status >= 500;\n};\n\n/**\n * Wraps the global fetch function (or any custom fetch) with a Polly policy.\n *\n * @param policy The policy to wrap the fetch call with.\n * @param options Configuration options.\n * @returns A decorated fetch function.\n */\nexport function pollyFetch(\n  policy: IPolicy<Response>,\n  options: PollyFetchOptions = {},\n): (input: RequestInfo | URL, init?: RequestInit) => Promise<Response> {\n  const shouldFail = options.shouldFail ?? defaultShouldFail;\n\n  return (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {\n    // Extract signal to pass to policy execution context if present\n    const signal = init?.signal as AbortSignal | undefined;\n\n    return policy.execute(async (context) => {\n      // Merge context signal with init signal if both exist\n      // Priority: Context signal (controlled by Timeout/Cancellation policies) > Init signal (user)\n      // Actually, we should respect both. If policy controls it, we use that.\n      const fetchSignal = context.signal;\n\n      const modifiedInit: RequestInit = {\n        ...init,\n        signal: fetchSignal,\n      };\n\n      const response = await fetch(input, modifiedInit);\n\n      if (shouldFail(response)) {\n        throw new HttpError(response);\n      }\n\n      return response;\n    }, signal);\n  };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACiBO,IAAM,YAAN,cAAwB,MAAM;AAAA,EACnC,YACkB,UAChB,SACA;AACA,UAAM,WAAW,QAAQ,OAAO,SAAS,MAAM,CAAC,IAAI,SAAS,UAAU,EAAE;AAHzD;AAIhB,SAAK,OAAO;AAAA,EACd;AACF;AAEA,IAAM,oBAAoB,CAAC,aAAgC;AACzD,SAAO,SAAS,WAAW,OAAO,SAAS,UAAU;AACvD;AASO,SAAS,WACd,QACA,UAA6B,CAAC,GACuC;AACrE,QAAM,aAAa,QAAQ,cAAc;AAEzC,SAAO,CAAC,OAA0B,SAA0C;AAE1E,UAAM,SAAS,MAAM;AAErB,WAAO,OAAO,QAAQ,OAAO,YAAY;AAIvC,YAAM,cAAc,QAAQ;AAE5B,YAAM,eAA4B;AAAA,QAChC,GAAG;AAAA,QACH,QAAQ;AAAA,MACV;AAEA,YAAM,WAAW,MAAM,MAAM,OAAO,YAAY;AAEhD,UAAI,WAAW,QAAQ,GAAG;AACxB,cAAM,IAAI,UAAU,QAAQ;AAAA,MAC9B;AAEA,aAAO;AAAA,IACT,GAAG,MAAM;AAAA,EACX;AACF;","names":[]}