Skip to content

Commit 67bb9a1

Browse files
committed
add rateLimitExtraTimeout option. code style fixes
1 parent 2ebc266 commit 67bb9a1

File tree

4 files changed

+39
-30
lines changed

4 files changed

+39
-30
lines changed

resources/structs.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@ export interface ClientConfig {
1111
apiKey?: string;
1212

1313
/**
14-
* The default language for all endpoints. Defaults to 'en'
14+
* The default language for all endpoints. Defaults to `en`
1515
*/
1616
language: Language;
17+
18+
/**
19+
* Extra timeout for stats ratelimits. Defaults to `0`.
20+
*
21+
* Normally the client will send 3 stats requests per 1100 milliseconds.
22+
* Setting this option to `100` increases the rate to 3 per 1200 milliseconds.
23+
*
24+
* You should increase this option if you're getting 429 responses
25+
*/
26+
rateLimitExtraTimeout: number;
1727
}
1828

1929
export interface ClientOptions extends Partial<ClientConfig> {}

src/client/Client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Client {
2121
constructor(config?: ClientOptions) {
2222
this.config = {
2323
language: Language.English,
24+
rateLimitExtraTimeout: 0,
2425
...config,
2526
};
2627

src/http/HTTP.ts

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* eslint-disable no-restricted-syntax */
22
import axios, { AxiosError, AxiosInstance } from 'axios';
33
import rateLimit, { RateLimitedAxiosInstance } from 'axios-rate-limit';
4-
import { URLSearchParams } from 'url';
54
import { version } from '../../package.json';
65
import Client from '../client/Client';
76
import FortniteAPIError from '../exceptions/FortniteAPIError';
87
import InvalidAPIKeyError from '../exceptions/InvalidAPIKeyError';
98
import MissingAPIKeyError from '../exceptions/MissingAPIKeyError';
9+
import { serializeParams } from '../util/util';
1010
import { FortniteAPIResponseData } from './httpStructs';
1111

1212
class HTTP {
@@ -29,27 +29,18 @@ class HTTP {
2929
},
3030
});
3131

32-
this.statsAxios = rateLimit(this.axios, { maxRequests: 3, perMilliseconds: 1100 });
32+
this.statsAxios = rateLimit(this.axios, {
33+
maxRequests: 3,
34+
perMilliseconds: 1100 + this.client.config.rateLimitExtraTimeout,
35+
});
3336
}
3437

3538
public async fetch(url: string, params?: any): Promise<FortniteAPIResponseData> {
3639
try {
3740
const response = await this.axios({
3841
url,
3942
params,
40-
paramsSerializer: (p) => {
41-
const searchParams = new URLSearchParams();
42-
43-
for (const [key, value] of Object.entries(p)) {
44-
if (Array.isArray(value)) {
45-
for (const singleValue of value) searchParams.append(key, singleValue);
46-
} else {
47-
searchParams.append(key, (value as any));
48-
}
49-
}
50-
51-
return searchParams.toString();
52-
},
43+
paramsSerializer: serializeParams,
5344
});
5445

5546
return response.data;
@@ -72,22 +63,12 @@ class HTTP {
7263

7364
public async fetchStats(url: string, params?: any): Promise<FortniteAPIResponseData> {
7465
try {
75-
const response = await this.statsAxios.get(url, {
66+
const response = await this.statsAxios({
67+
url,
7668
params,
77-
paramsSerializer: (p) => {
78-
const searchParams = new URLSearchParams();
79-
80-
for (const [key, value] of Object.entries(p)) {
81-
if (Array.isArray(value)) {
82-
for (const singleValue of value) searchParams.append(key, singleValue);
83-
} else {
84-
searchParams.append(key, (value as any));
85-
}
86-
}
87-
88-
return searchParams.toString();
89-
},
69+
paramsSerializer: serializeParams,
9070
});
71+
9172
return response.data;
9273
} catch (e) {
9374
if (e instanceof AxiosError && e.response?.data?.error) {

src/util/util.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* eslint-disable no-restricted-syntax */
2+
/* eslint-disable import/prefer-default-export */
3+
import { URLSearchParams } from 'url';
4+
5+
export const serializeParams = (params: any) => {
6+
const searchParams = new URLSearchParams();
7+
8+
for (const [key, value] of Object.entries(params)) {
9+
if (Array.isArray(value)) {
10+
for (const singleValue of value) searchParams.append(key, singleValue);
11+
} else {
12+
searchParams.append(key, (value as any));
13+
}
14+
}
15+
16+
return searchParams.toString();
17+
};

0 commit comments

Comments
 (0)