speedruncom.js 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/BaseClient.d.ts +18 -8
- package/dist/BaseClient.js +41 -49
- package/dist/Client.d.ts +205 -192
- package/dist/Client.js +257 -256
- package/dist/build-client.js +44 -23
- package/dist/endpoints/endpoints.get.d.ts +214 -62
- package/dist/endpoints/endpoints.post.d.ts +161 -23
- package/dist/interfaces.d.ts +576 -443
- package/dist/responses.d.ts +4 -2
- package/package.json +3 -2
- package/src/BaseClient.ts +4 -32
- package/src/build-client.ts +12 -5
- package/src/endpoints/endpoints.get.ts +18 -1
- package/src/Client.ts +0 -880
package/dist/BaseClient.d.ts
CHANGED
|
@@ -1,15 +1,24 @@
|
|
|
1
1
|
import { AxiosInstance } from 'axios';
|
|
2
|
+
interface config {
|
|
3
|
+
PHPSESSID?: string;
|
|
4
|
+
userAgent?: string;
|
|
5
|
+
}
|
|
2
6
|
export default class Client {
|
|
7
|
+
/**
|
|
8
|
+
* `AxiosInstance` used on instance-called methods (called with `POST`).
|
|
9
|
+
*/
|
|
3
10
|
axiosClient: AxiosInstance;
|
|
11
|
+
/**
|
|
12
|
+
* `AxiosInstance` used on Client-called methods (called with `GET`).
|
|
13
|
+
*/
|
|
4
14
|
static axiosClient: AxiosInstance;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
request<T>(endpoint: string, params?: object
|
|
12
|
-
static request<T>(endpoint: string, params?: object, method?: string): Promise<T>;
|
|
15
|
+
private username;
|
|
16
|
+
private password;
|
|
17
|
+
private headers;
|
|
18
|
+
constructor(config?: config);
|
|
19
|
+
config(config: config): void;
|
|
20
|
+
request<T>(endpoint: string, params?: object): Promise<T>;
|
|
21
|
+
static request<T>(endpoint: string, params?: object): Promise<T>;
|
|
13
22
|
/**
|
|
14
23
|
* Attempts to authorize your cookies if using a browser, or authorizes this Client if otherwise.
|
|
15
24
|
* If the account has two factor authentication, you have to use `setToken` with the token sent to the account's email address.
|
|
@@ -25,3 +34,4 @@ export default class Client {
|
|
|
25
34
|
*/
|
|
26
35
|
logout(): Promise<unknown>;
|
|
27
36
|
}
|
|
37
|
+
export {};
|
package/dist/BaseClient.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import axios from 'axios';
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
const
|
|
2
|
+
const BASE_USER_AGENT = 'speedruncom.js';
|
|
3
|
+
const BASE_URL = 'https://www.speedrun.com/api/v2/';
|
|
4
|
+
const HEADERS = {
|
|
5
|
+
'Accept-Language': 'en',
|
|
6
|
+
'Accept': 'application/json'
|
|
7
|
+
};
|
|
5
8
|
const isBrowser = typeof window !== 'undefined';
|
|
6
9
|
const objectToBase64 = (obj) => {
|
|
7
10
|
const jsonString = JSON.stringify(obj).replace(/\s+/g, '');
|
|
@@ -15,57 +18,45 @@ class APIError extends Error {
|
|
|
15
18
|
}
|
|
16
19
|
}
|
|
17
20
|
class Client {
|
|
18
|
-
constructor(
|
|
21
|
+
constructor(config) {
|
|
22
|
+
/**
|
|
23
|
+
* `AxiosInstance` used on instance-called methods (called with `POST`).
|
|
24
|
+
*/
|
|
19
25
|
this.axiosClient = axios.create({
|
|
20
|
-
baseURL:
|
|
26
|
+
baseURL: BASE_URL,
|
|
27
|
+
method: 'POST',
|
|
21
28
|
withCredentials: true,
|
|
22
|
-
headers:
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
29
|
+
headers: HEADERS
|
|
30
|
+
});
|
|
31
|
+
this.headers = this.axiosClient.defaults.headers.common;
|
|
32
|
+
if (config)
|
|
33
|
+
this.config(config);
|
|
34
|
+
this.axiosClient.interceptors.response.use((response) => response, (error) => {
|
|
35
|
+
const data = error.response.data;
|
|
36
|
+
throw new APIError(data.error || 'Unknown error', error.response.status);
|
|
26
37
|
});
|
|
38
|
+
}
|
|
39
|
+
config(config) {
|
|
27
40
|
if (!isBrowser)
|
|
28
|
-
this.
|
|
29
|
-
if (PHPSESSID) {
|
|
41
|
+
this.headers['User-Agent'] = BASE_USER_AGENT + (config.userAgent ? `/${config.userAgent}` : '');
|
|
42
|
+
if (config.PHPSESSID) {
|
|
30
43
|
if (isBrowser) {
|
|
31
44
|
console.error('You cannot use a PHPSESSID to authenticate in a browser environment.');
|
|
32
45
|
}
|
|
33
46
|
else {
|
|
34
|
-
this.
|
|
47
|
+
this.headers['Cookie'] = `PHPSESSID=${config.PHPSESSID}`;
|
|
35
48
|
}
|
|
36
49
|
}
|
|
37
|
-
this.axiosClient.interceptors.response.use((response) => response, (error) => {
|
|
38
|
-
if (error.response) {
|
|
39
|
-
const data = error.response?.data;
|
|
40
|
-
throw new APIError(data?.error || 'Unknown error', error.response.status);
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
50
|
}
|
|
44
|
-
async request(endpoint, params = {}
|
|
45
|
-
|
|
46
|
-
if (method === 'post') {
|
|
47
|
-
response = await this.axiosClient.post(endpoint, params);
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
response = await this.axiosClient.get(`${endpoint}?_r=${objectToBase64(params)}`);
|
|
51
|
-
}
|
|
51
|
+
async request(endpoint, params = {}) {
|
|
52
|
+
const response = await this.axiosClient.post(endpoint, params);
|
|
52
53
|
const cookie = response.headers['set-cookie'];
|
|
53
54
|
if (cookie && !isBrowser)
|
|
54
|
-
this.
|
|
55
|
+
this.headers['Cookie'] = cookie[0].split(';')[0];
|
|
55
56
|
return response.data;
|
|
56
57
|
}
|
|
57
|
-
static async request(endpoint, params = {}
|
|
58
|
-
|
|
59
|
-
if (method === 'post') {
|
|
60
|
-
response = await this.axiosClient.post(endpoint, params);
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
response = await this.axiosClient.get(`${endpoint}?_r=${objectToBase64(params)}`);
|
|
64
|
-
}
|
|
65
|
-
const cookie = response.headers['set-cookie'];
|
|
66
|
-
if (cookie && !isBrowser)
|
|
67
|
-
this.axiosClient.defaults.headers.common['Cookie'] = `PHPSESSID=${cookie[0].split('=')[1].split(';')[0]}`;
|
|
68
|
-
return response.data;
|
|
58
|
+
static async request(endpoint, params = {}) {
|
|
59
|
+
return (await this.axiosClient.get(`${endpoint}?_r=${objectToBase64(params)}`)).data;
|
|
69
60
|
}
|
|
70
61
|
//Built-in endpoints for auth
|
|
71
62
|
/**
|
|
@@ -73,8 +64,8 @@ class Client {
|
|
|
73
64
|
* If the account has two factor authentication, you have to use `setToken` with the token sent to the account's email address.
|
|
74
65
|
*/
|
|
75
66
|
async login(username, password) {
|
|
76
|
-
this.
|
|
77
|
-
this.
|
|
67
|
+
this.username = username;
|
|
68
|
+
this.password = password;
|
|
78
69
|
return await this.request('PutAuthLogin', {
|
|
79
70
|
name: username,
|
|
80
71
|
password
|
|
@@ -86,8 +77,8 @@ class Client {
|
|
|
86
77
|
*/
|
|
87
78
|
async setToken(token) {
|
|
88
79
|
return await this.request('PutAuthLogin', {
|
|
89
|
-
name: this.
|
|
90
|
-
password: this.
|
|
80
|
+
name: this.username,
|
|
81
|
+
password: this.password,
|
|
91
82
|
token
|
|
92
83
|
});
|
|
93
84
|
}
|
|
@@ -97,14 +88,15 @@ class Client {
|
|
|
97
88
|
async logout() {
|
|
98
89
|
if (isBrowser)
|
|
99
90
|
return await this.request('PutAuthLogout');
|
|
100
|
-
delete this.
|
|
91
|
+
delete this.headers['Cookie'];
|
|
101
92
|
}
|
|
102
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* `AxiosInstance` used on Client-called methods (called with `GET`).
|
|
96
|
+
*/
|
|
103
97
|
Client.axiosClient = axios.create({
|
|
104
|
-
baseURL:
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
'Accept': ACCEPT,
|
|
108
|
-
}
|
|
98
|
+
baseURL: BASE_URL,
|
|
99
|
+
method: 'GET',
|
|
100
|
+
headers: HEADERS
|
|
109
101
|
});
|
|
110
102
|
export default Client;
|