speedruncom.js 1.0.0
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/LICENSE +504 -0
- package/README.md +49 -0
- package/dist/BaseClient.d.ts +27 -0
- package/dist/BaseClient.js +110 -0
- package/dist/Client.d.ts +214 -0
- package/dist/Client.js +663 -0
- package/dist/build-client.d.ts +1 -0
- package/dist/build-client.js +42 -0
- package/dist/endpoints/endpoints.get.d.ts +410 -0
- package/dist/endpoints/endpoints.get.js +2 -0
- package/dist/endpoints/endpoints.post.d.ts +923 -0
- package/dist/endpoints/endpoints.post.js +2 -0
- package/dist/enums.d.ts +363 -0
- package/dist/enums.js +404 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/interfaces.d.ts +1267 -0
- package/dist/interfaces.js +1 -0
- package/dist/responses.d.ts +633 -0
- package/dist/responses.js +1 -0
- package/dist/types.d.ts +3 -0
- package/dist/types.js +1 -0
- package/package.json +28 -0
- package/src/BaseClient.ts +136 -0
- package/src/Client.ts +880 -0
- package/src/build-client.ts +70 -0
- package/src/endpoints/endpoints.get.ts +674 -0
- package/src/endpoints/endpoints.post.ts +1253 -0
- package/src/enums.ts +404 -0
- package/src/index.ts +6 -0
- package/src/interfaces.ts +1621 -0
- package/src/responses.ts +741 -0
- package/src/types.ts +5 -0
- package/tsconfig.json +17 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import axios, { AxiosResponse, AxiosInstance, AxiosError } from 'axios';
|
|
2
|
+
import * as GetEndpoints from './endpoints/endpoints.get.js';
|
|
3
|
+
import * as PostEndpoints from './endpoints/endpoints.post.js'
|
|
4
|
+
import * as Responses from './responses.js';
|
|
5
|
+
|
|
6
|
+
const BASE_USER_AGENT = 'speedrun.js';
|
|
7
|
+
const BASE_URL = 'https://www.speedrun.com/api/v2/';
|
|
8
|
+
const HEADERS = {
|
|
9
|
+
'Accept-Language': 'en',
|
|
10
|
+
'Accept': 'application/json'
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const isBrowser = typeof window !== 'undefined';
|
|
14
|
+
|
|
15
|
+
const objectToBase64 = (obj: object) => {
|
|
16
|
+
const jsonString = JSON.stringify(obj).replace(/\s+/g, '');
|
|
17
|
+
return Buffer.from(jsonString).toString('base64');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface config {
|
|
21
|
+
PHPSESSID?: string;
|
|
22
|
+
userAgent?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
class APIError extends Error {
|
|
26
|
+
status: number;
|
|
27
|
+
|
|
28
|
+
constructor(message: string, status: number) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.message = message;
|
|
31
|
+
this.status = status;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export default class Client {
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* `AxiosInstance` used on instance-called methods (called with `POST`).
|
|
39
|
+
*/
|
|
40
|
+
axiosClient = axios.create({
|
|
41
|
+
baseURL: BASE_URL,
|
|
42
|
+
method: 'POST',
|
|
43
|
+
withCredentials: true,
|
|
44
|
+
headers: HEADERS
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* `AxiosInstance` used on Client-called methods (called with `GET`).
|
|
49
|
+
*/
|
|
50
|
+
static axiosClient = axios.create({
|
|
51
|
+
baseURL: BASE_URL,
|
|
52
|
+
method: 'GET',
|
|
53
|
+
headers: HEADERS
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
private username!: string;
|
|
57
|
+
private password!: string;
|
|
58
|
+
|
|
59
|
+
private headers = this.axiosClient.defaults.headers.common;
|
|
60
|
+
|
|
61
|
+
constructor(config?: config) {
|
|
62
|
+
if (config) this.config(config);
|
|
63
|
+
|
|
64
|
+
this.axiosClient.interceptors.response.use(
|
|
65
|
+
(response: AxiosResponse) => response,
|
|
66
|
+
(error: AxiosError) => {
|
|
67
|
+
const data = error.response.data as { error?: string };
|
|
68
|
+
throw new APIError(data.error || 'Unknown error', error.response.status);
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
config(config: config) {
|
|
74
|
+
if (!isBrowser) this.headers['User-Agent'] = BASE_USER_AGENT + (config.userAgent ? `/${config.userAgent}` : '');
|
|
75
|
+
|
|
76
|
+
if (config.PHPSESSID) {
|
|
77
|
+
if (isBrowser) {
|
|
78
|
+
console.error('You cannot use a PHPSESSID to authenticate in a browser environment.');
|
|
79
|
+
} else {
|
|
80
|
+
this.headers['Cookie'] = `PHPSESSID=${config.PHPSESSID}`;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async request<T>(endpoint: string, params: object = {}): Promise<T> {
|
|
86
|
+
const response = await this.axiosClient.post<T>(endpoint, params);
|
|
87
|
+
|
|
88
|
+
const cookie = response.headers['set-cookie'];
|
|
89
|
+
if (cookie && !isBrowser) this.headers['Cookie'] = cookie[0].split(';')[0];
|
|
90
|
+
|
|
91
|
+
return response.data;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
static async request<T>(endpoint: string, params: object): Promise<T> {
|
|
95
|
+
return (await this.axiosClient.get(`${endpoint}?_r=${objectToBase64(params)}`)).data;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
//Built-in endpoints for auth
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Attempts to authorize your cookies if using a browser, or authorizes this Client if otherwise.
|
|
102
|
+
* If the account has two factor authentication, you have to use `setToken` with the token sent to the account's email address.
|
|
103
|
+
*/
|
|
104
|
+
async login(username: string, password: string) {
|
|
105
|
+
this.username = username;
|
|
106
|
+
this.password = password;
|
|
107
|
+
|
|
108
|
+
return await this.request('PutAuthLogin', {
|
|
109
|
+
name: username,
|
|
110
|
+
password
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Attempts to authorize your cookies if using a browser, or authorizes this Client if otherwise, with the token that `login()` sent to the account's email address.
|
|
116
|
+
* @param token The 5-digit code sent to your email after a successful `login()`.
|
|
117
|
+
*/
|
|
118
|
+
async setToken(token: string) {
|
|
119
|
+
return await this.request('PutAuthLogin', {
|
|
120
|
+
name: this.username,
|
|
121
|
+
password: this.password,
|
|
122
|
+
token
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Attempts to remove the PHPSESSID cookie if using a browser, otherwise removes your Client's authentication.
|
|
128
|
+
*/
|
|
129
|
+
async logout() {
|
|
130
|
+
if (isBrowser) return await this.request('PutAuthLogout');
|
|
131
|
+
|
|
132
|
+
delete this.headers['Cookie'];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Endpoints (auto-generated with build-client)
|
|
136
|
+
}
|