-
Notifications
You must be signed in to change notification settings - Fork 0
/
rate_limit.ts
102 lines (95 loc) · 3.46 KB
/
rate_limit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import type { AxiosInstance } from 'axios'
import { defaultTransformers } from './axios_default_transformers.ts'
import { rateLimitUrl } from './urls.ts'
type RateLimitRaw = {
limit: number | 'unlimited'
remaining: number | 'unlimited'
reset: number
used: number
}
type RateLimitResponseRaw = {
rate: {
org_monthly: RateLimitRaw
api_key_per_minute: RateLimitRaw
}
}
export type RateLimitBoundary = {
/** The maximum number of calls for each period. Please note, this can be `Infinity`. */
limit: number | typeof Infinity
/** The number of calls remaining before reset. Please note, this can be `Infinity`. */
remaining: number | typeof Infinity
/** Time in seconds until call count resets. */
reset: number
/** The number of calls that have been used. */
used: number
}
export type RateLimitResponse = {
/**
* Type of rate limit.
*/
rate: {
/** Monthly rate limit per organization. */
org_monthly: RateLimitBoundary
/** Per minute rate limit per API key. */
api_key_per_minute: RateLimitBoundary
}
}
/**
* @module
*/
export class RateLimit {
/** @hidden */
constructor(private readonly axios: AxiosInstance) {
}
/**
* 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(): Promise<RateLimitResponse> {
const response = await this.axios.get<RateLimitResponse>(
rateLimitUrl(),
{
transformResponse: [
...defaultTransformers(),
(json: RateLimitResponseRaw) => {
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: number | 'unlimited') =>
value === 'unlimited' ? Infinity : value