UNPKG

@flatfile/safe-api

Version:

Flatfile Safe API client with streaming capabilities

30 lines (29 loc) 1.13 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useApiQuota = void 0; const p_throttle_1 = __importDefault(require("p-throttle")); const throttle = (0, p_throttle_1.default)({ limit: 20, interval: 1000, strict: true }); /* * This throttle function is meant to be used for API requests that are limited by the API quota * It makes safer to use this function to call the API without worrying about exceeding the API rate limits * It should not be used to call api request more than once, if there's a need to call api multiple times, just call the function again * * @param callback * @returns The result of the callback function * @example * // Fetch workbooks list respecting the API quota * const result = await useApiQuota(() => api.workbooks.list({ spaceId })); */ const useApiQuota = (callback) => { return throttle(async (callback) => { return await callback(); })(callback); }; exports.useApiQuota = useApiQuota;