forked from a/lifeto-shop
134 lines
3.3 KiB
TypeScript
134 lines
3.3 KiB
TypeScript
import axios, { AxiosResponse, Method } from "axios";
|
|
import qs from "qs";
|
|
import { getCookie, removeCookie } from "typescript-cookie";
|
|
|
|
|
|
export const SITE_ROOT = "/lifeto/"
|
|
|
|
export const API_ROOT = "api/lifeto/"
|
|
export const BANK_ROOT = "api/lifeto/v2/item-manager/"
|
|
export const MARKET_ROOT = "marketplace-api/"
|
|
|
|
const login_endpoint = (name:string)=>{
|
|
return SITE_ROOT + name
|
|
}
|
|
export const api_endpoint = (name:string):string =>{
|
|
return SITE_ROOT+API_ROOT + name
|
|
}
|
|
export const bank_endpoint = (name:string):string =>{
|
|
return SITE_ROOT+BANK_ROOT + name
|
|
}
|
|
|
|
export const market_endpoint = (name:string):string =>{
|
|
return SITE_ROOT+MARKET_ROOT+ name
|
|
}
|
|
|
|
export const EndpointCreators = [
|
|
api_endpoint,
|
|
bank_endpoint,
|
|
market_endpoint,
|
|
]
|
|
|
|
export type EndpointCreator = typeof EndpointCreators[number]
|
|
|
|
export interface Session {
|
|
user:string
|
|
xsrf:string
|
|
csrf:string
|
|
request:(verb:Method,url:string,data:any,c?:EndpointCreator)=>Promise<any>
|
|
}
|
|
|
|
export class LoginHelper {
|
|
user:string
|
|
pass:string
|
|
csrf?:string
|
|
constructor(user:string, pass:string){
|
|
this.user = user;
|
|
this.pass = pass;
|
|
}
|
|
login = async ():Promise<TokenSession> =>{
|
|
return axios.get(login_endpoint("login"),{withCredentials:false})
|
|
.then(async (x)=>{
|
|
return axios.post(login_endpoint("login"),{
|
|
login:this.user,
|
|
password:this.pass,
|
|
redirectTo:"lifeto"
|
|
},{withCredentials:false})
|
|
.then(async (x)=>{
|
|
await sleep(100)
|
|
let xsrf= getCookie("XSRF-TOKEN")
|
|
return new TokenSession(this.user,this.csrf!, xsrf!)
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
export class LogoutHelper{
|
|
constructor(){
|
|
}
|
|
logout = async ():Promise<void> =>{
|
|
return axios.get(login_endpoint("logout"),{withCredentials:false}).catch((e)=>{})
|
|
}
|
|
}
|
|
const sleep = async(ms:number)=> {
|
|
return new Promise(resolve => setTimeout(resolve, ms))
|
|
}
|
|
|
|
export class TokenSession implements Session {
|
|
csrf:string
|
|
xsrf:string
|
|
user:string
|
|
constructor(name:string, csrf:string, xsrf: string){
|
|
this.user = name
|
|
this.csrf = csrf
|
|
this.xsrf = xsrf;
|
|
}
|
|
|
|
request = async (verb:string,url:string,data:any, c:EndpointCreator = api_endpoint):Promise<AxiosResponse> => {
|
|
let promise
|
|
switch (verb.toLowerCase()){
|
|
case "post":
|
|
promise = axios.post(c(url),data,this.genHeaders())
|
|
break;
|
|
case "postform":
|
|
promise = axios.postForm(c(url),data)
|
|
break;
|
|
case "postraw":
|
|
const querystring = qs.stringify(data)
|
|
promise = axios.post(c(url),querystring,this.genHeaders())
|
|
break;
|
|
case "get":
|
|
default:
|
|
promise = axios.get(c(url),this.genHeaders())
|
|
}
|
|
return promise.then(x=>{
|
|
if(x.data){
|
|
try{
|
|
this.xsrf = x.data.split("xsrf-token")[1].split('\">')[0].replace("\" content=\"",'')
|
|
}catch(e){
|
|
}
|
|
}
|
|
if(x.headers['set-cookie']){
|
|
const cookies = x.headers['set-cookie'].map((y)=>{
|
|
return y.split("=")[1].split(";")[0];
|
|
})
|
|
this.xsrf = cookies[0]
|
|
}
|
|
return x
|
|
})
|
|
}
|
|
genHeaders = ()=>{
|
|
const out = {
|
|
headers:{
|
|
Accept: "application/json",
|
|
"Update-Insecure-Requests": 1,
|
|
},
|
|
withCredentials:true
|
|
}
|
|
if(this.xsrf){
|
|
(out.headers as any)["X-XSRF-TOKEN"] = this.xsrf.replace("%3D","=")
|
|
}
|
|
return out
|
|
}
|
|
}
|