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.
@@ -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
- user: string;
6
- pass: string;
7
- constructor({ PHPSESSID, userAgent }: {
8
- PHPSESSID?: string;
9
- userAgent?: string;
10
- });
11
- request<T>(endpoint: string, params?: object, method?: string): Promise<T>;
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 {};
@@ -1,7 +1,10 @@
1
1
  import axios from 'axios';
2
- const LANG = 'en';
3
- const ACCEPT = 'application/json';
4
- const BASE_USER_AGENT = 'speedrun.js';
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({ PHPSESSID, userAgent }) {
21
+ constructor(config) {
22
+ /**
23
+ * `AxiosInstance` used on instance-called methods (called with `POST`).
24
+ */
19
25
  this.axiosClient = axios.create({
20
- baseURL: 'https://www.speedrun.com/api/v2/',
26
+ baseURL: BASE_URL,
27
+ method: 'POST',
21
28
  withCredentials: true,
22
- headers: {
23
- 'Accept-Language': LANG,
24
- 'Accept': ACCEPT,
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.axiosClient.defaults.headers.common['User-Agent'] = BASE_USER_AGENT + (userAgent ? `/${userAgent}` : '');
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.axiosClient.defaults.headers.common['Cookie'] = `PHPSESSID=${PHPSESSID}`;
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 = {}, method = 'post') {
45
- let response;
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.axiosClient.defaults.headers.common['Cookie'] = `PHPSESSID=${cookie[0].split('=')[1].split(';')[0]}`;
55
+ this.headers['Cookie'] = cookie[0].split(';')[0];
55
56
  return response.data;
56
57
  }
57
- static async request(endpoint, params = {}, method = 'post') {
58
- let response;
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.user = username;
77
- this.pass = password;
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.user,
90
- password: this.pass,
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.axiosClient.defaults.headers.common['Cookie'];
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: 'https://www.speedrun.com/api/v2/',
105
- headers: {
106
- 'Accept-Language': LANG,
107
- 'Accept': ACCEPT,
108
- }
98
+ baseURL: BASE_URL,
99
+ method: 'GET',
100
+ headers: HEADERS
109
101
  });
110
102
  export default Client;