UNPKG

@composite-fetcher/with-retries

Version:

A fetcher core plugin to handle and retry failed requests

91 lines (88 loc) 3.22 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { withRetries: () => withRetriesPlugin }); module.exports = __toCommonJS(src_exports); // src/withRetries.ts var import_core = require("@composite-fetcher/core"); var import_isomorphic_fetch = __toESM(require("isomorphic-fetch"), 1); var withRetriesPlugin = class extends import_core.BasePlugin { defaultMaxRetries; constructor(defaultMaxRetries = 3) { super(); this.defaultMaxRetries = defaultMaxRetries; } async retryOperation(promiseFn, onSuccess, onFailure, attempt = 0, retries = 0) { try { const result = await promiseFn(); if (result instanceof Response && !result.ok) { throw new Error(`HTTP error: ${result.status}`); } return onSuccess(result); } catch (err) { if (attempt < retries - 1) { return await this.retryOperation( promiseFn, onSuccess, onFailure, attempt + 1, retries ); } console.error(`Failed after ${retries} attempts`, err); onFailure(err); } } async onPostRequest(context) { if (!context.response.ok && !context.originalRequest.headers.has("x-fetcher-no-retry")) { let retries = this.defaultMaxRetries; if (context.originalRequest.headers.has("x-fetcher-retry-times")) { retries = parseInt( context.originalRequest.headers.get("x-fetcher-retry-times"), 10 ); } const response = await this.retryOperation( async () => (0, import_isomorphic_fetch.default)(context.originalRequest), (res) => res, (error) => error, 0, retries ); return response; } } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { withRetries });