-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathSession.ts
30 lines (24 loc) · 980 Bytes
/
Session.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
import axios from 'axios';
import { wrapper } from 'axios-cookiejar-support';
import * as tough from 'tough-cookie';
import { AxiosRequestConfig, AxiosResponse, AxiosInstance } from 'axios';
import * as FormData from 'form-data';
export class Session {
axiosInstance!: AxiosInstance;
cookieJar: tough.CookieJar;
constructor(axios_config: AxiosRequestConfig) {
const cookieStore = new tough.MemoryCookieStore();
this.cookieJar = new tough.CookieJar(cookieStore);
this.axiosInstance = wrapper(axios.create({
...axios_config,
jar: this.cookieJar,
withCredentials: true
}));
}
async get(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse> {
return this.axiosInstance.get(url, config);
}
async post(url: string, data?: string | FormData, config?: AxiosRequestConfig): Promise<AxiosResponse> {
return this.axiosInstance.post(url, data, config);
}
}