UNPKG

@planet-a/affinity-node

Version:
56 lines (55 loc) 2.14 kB
import { defaultTransformers } from './axios_default_transformers.js'; import { rateLimitUrl } from './urls.js'; /** * @module */ export class RateLimit { /** @hidden */ constructor(axios) { Object.defineProperty(this, "axios", { enumerable: true, configurable: true, writable: true, value: axios }); } /** * The rate limit endpoint allows you to see your monthly account-level and per minute user-level API limits and usage. * The monthly account-level call limit resets at the end of each calendar month. * * More details [here](https://api-docs.affinity.co/#rate-limits). * * @returns The rate limit resource, a JSON body of data including limits, calls remaining, seconds until reset and call count. * * @example * ```typescript * const rateLimit = await affinity.rateLimit.get() * console.log(`You have ${rateLimit.rate.org_monthly.remaining} calls left this month.`) * ``` */ async get() { const response = await this.axios.get(rateLimitUrl(), { transformResponse: [ ...defaultTransformers(), (json) => { return { rate: { org_monthly: { ...json.rate.org_monthly, limit: unlimitedToNumber(json.rate.org_monthly.limit), remaining: unlimitedToNumber(json.rate.org_monthly.remaining), }, api_key_per_minute: { ...json.rate.api_key_per_minute, limit: unlimitedToNumber(json.rate.api_key_per_minute.limit), remaining: unlimitedToNumber(json.rate.api_key_per_minute.remaining), }, }, }; }, ], }); return response.data; } } const unlimitedToNumber = (value) => value === 'unlimited' ? Infinity : value;