{"version":3,"file":"index.mjs","names":[],"sources":["../src/throw-exception.ts","../src/catch-exception.ts","../src/create-exception-by-status-code.ts","../src/validate-status-code.ts"],"sourcesContent":["import { KeqContext, KeqMiddleware } from 'keq'\nimport { Promisable } from 'type-fest'\n\n\nexport type Check = (ctx: KeqContext) => Promisable<void>\n\nexport function throwException(check: Check): KeqMiddleware {\n  return async function throwException(ctx, next) {\n    await next()\n\n    await check(ctx)\n  }\n}\n","import { KeqMiddleware } from 'keq'\nimport { Promisable } from 'type-fest'\n\n\nexport function catchException(handler: (e: unknown) => Promisable<void>): KeqMiddleware {\n  return async function catchException(ctx, next) {\n    try {\n      await next()\n    } catch (err) {\n      await handler(err)\n    }\n  }\n}\n","import {\n  BadGatewayException,\n  BadRequestException,\n  ConflictException,\n  ForbiddenException,\n  GatewayTimeoutException,\n  InternalServerErrorException,\n  NotAcceptableException,\n  NotFoundedException,\n  PreconditionFailedException,\n  RequestException,\n  ServiceUnavailableException,\n  UnauthorizedException,\n  ImATeapotException,\n  MethodNotAllowedException,\n  UriTooLongException,\n  ContentTooLargeException,\n  ProxyAuthenticationRequiredException,\n  RequestTimeoutException,\n  TooManyRequestsException,\n  NotImplementedException,\n  UnsupportedMediaTypeException,\n} from 'keq'\n\nexport function createExceptionByStatusCode(response: Response): Error {\n  const { status, statusText } = response\n\n  // 4xx client errors\n  if (status >= 400 && status < 500) {\n    switch (status) {\n      case 400:\n        return new BadRequestException(statusText, { response })\n      case 401:\n        return new UnauthorizedException(statusText, { response })\n      case 403:\n        return new ForbiddenException(statusText, { response })\n      case 404:\n        return new NotFoundedException(statusText, { response })\n      case 405:\n        return new MethodNotAllowedException(statusText, { response })\n      case 406:\n        return new NotAcceptableException(statusText, { response })\n      case 407:\n        return new ProxyAuthenticationRequiredException(statusText, { response })\n      case 408:\n        return new RequestTimeoutException(statusText, { response })\n      case 409:\n        return new ConflictException(statusText, { response })\n      case 412:\n        return new PreconditionFailedException(statusText, { response })\n      case 413:\n        return new ContentTooLargeException(statusText, { response })\n      case 414:\n        return new UriTooLongException(statusText, { response })\n      case 415:\n        return new UnsupportedMediaTypeException(statusText, { response })\n      case 418:\n        return new ImATeapotException(statusText, { response })\n      case 429:\n        return new TooManyRequestsException(statusText, { response })\n      default:\n        // Other 4xx errors, don't retry by default\n        return new RequestException(status, statusText, { fatal: true, response })\n    }\n  }\n\n  // 5xx server errors\n  if (status >= 500) {\n    switch (status) {\n      case 500:\n        return new InternalServerErrorException(statusText, { response })\n      case 501:\n        return new NotImplementedException(statusText, { response })\n      case 502:\n        return new BadGatewayException(statusText, { response })\n      case 503:\n        return new ServiceUnavailableException(statusText, { response })\n      case 504:\n        return new GatewayTimeoutException(statusText, { response })\n      default:\n        // Other 5xx errors, retry by default\n        return new RequestException(status, statusText, { fatal: false, response })\n    }\n  }\n\n  return new RequestException(status, statusText, { fatal: false, response })\n}\n","import { KeqMiddleware } from 'keq'\nimport { createExceptionByStatusCode } from './create-exception-by-status-code.js'\n\nexport function validateStatusCode(): KeqMiddleware {\n  return async function validateStatusCode(context, next) {\n    await next()\n\n    const response = context.response\n    if (!response) return\n\n    const { status } = response\n\n    // 2xx success status codes - no error\n    if (status >= 200 && status < 300) return\n\n    // 3xx redirection status codes - no error (handled by fetch)\n    if (status >= 300 && status < 400) return\n\n    throw createExceptionByStatusCode(response)\n  }\n}\n"],"mappings":";;AAMA,SAAgB,eAAe,OAA6B;AAC1D,QAAO,eAAe,eAAe,KAAK,MAAM;AAC9C,QAAM,MAAM;AAEZ,QAAM,MAAM,IAAI;;;;;ACNpB,SAAgB,eAAe,SAA0D;AACvF,QAAO,eAAe,eAAe,KAAK,MAAM;AAC9C,MAAI;AACF,SAAM,MAAM;WACL,KAAK;AACZ,SAAM,QAAQ,IAAI;;;;;;ACexB,SAAgB,4BAA4B,UAA2B;CACrE,MAAM,EAAE,QAAQ,eAAe;AAG/B,KAAI,UAAU,OAAO,SAAS,IAC5B,SAAQ,QAAR;EACE,KAAK,IACH,QAAO,IAAI,oBAAoB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