1
0
forked from a/lifeto-shop
lifeto-shop/src/lib/session.ts

134 lines
3.3 KiB
TypeScript
Raw Normal View History

2022-07-03 10:25:12 +00:00
import axios, { AxiosResponse, Method } from "axios";
2022-07-01 01:27:18 +00:00
import qs from "qs";
import { getCookie, removeCookie } from "typescript-cookie";
2022-07-03 10:25:12 +00:00
export const SITE_ROOT = "/lifeto/"
2022-07-07 07:54:36 +00:00
2022-07-03 10:25:12 +00:00
export const API_ROOT = "api/lifeto/"
2024-04-09 03:42:34 +00:00
export const BANK_ROOT = "api/lifeto/v2/item-manager/"
2022-08-10 01:39:44 +00:00
export const MARKET_ROOT = "marketplace-api/"
2022-07-01 01:27:18 +00:00
2022-07-03 10:25:12 +00:00
const login_endpoint = (name:string)=>{
return SITE_ROOT + name
2022-07-01 01:27:18 +00:00
}
2022-07-07 07:54:36 +00:00
export const api_endpoint = (name:string):string =>{
2022-07-03 10:25:12 +00:00
return SITE_ROOT+API_ROOT + name
}
2022-07-07 07:54:36 +00:00
export const bank_endpoint = (name:string):string =>{
return SITE_ROOT+BANK_ROOT + name
}
2022-08-10 01:39:44 +00:00
export const market_endpoint = (name:string):string =>{
return SITE_ROOT+MARKET_ROOT+ name
}
2022-07-07 07:54:36 +00:00
export const EndpointCreators = [
api_endpoint,
bank_endpoint,
2022-08-10 01:39:44 +00:00
market_endpoint,
2022-07-07 07:54:36 +00:00
]
2022-08-10 01:39:44 +00:00
export type EndpointCreator = typeof EndpointCreators[number]
2022-07-07 07:54:36 +00:00
export interface Session {
user:string
xsrf:string
csrf:string
request:(verb:Method,url:string,data:any,c?:EndpointCreator)=>Promise<any>
}
2022-07-01 01:27:18 +00:00
export class LoginHelper {
user:string
pass:string
csrf?:string
constructor(user:string, pass:string){
this.user = user;
this.pass = pass;
}
login = async ():Promise<TokenSession> =>{
2022-07-03 10:25:12 +00:00
return axios.get(login_endpoint("login"),{withCredentials:false})
2022-07-01 01:27:18 +00:00
.then(async (x)=>{
2022-07-03 10:25:12 +00:00
return axios.post(login_endpoint("login"),{
2022-07-01 01:27:18 +00:00
login:this.user,
password:this.pass,
redirectTo:"lifeto"
},{withCredentials:false})
.then(async (x)=>{
await sleep(100)
let xsrf= getCookie("XSRF-TOKEN")
2022-07-03 10:25:12 +00:00
return new TokenSession(this.user,this.csrf!, xsrf!)
2022-07-01 01:27:18 +00:00
})
})
}
}
export class LogoutHelper{
constructor(){
}
logout = async ():Promise<void> =>{
return axios.get(login_endpoint("logout"),{withCredentials:false}).catch((e)=>{})
}
}
2022-08-10 01:39:44 +00:00
const sleep = async(ms:number)=> {
2022-07-01 01:27:18 +00:00
return new Promise(resolve => setTimeout(resolve, ms))
}
2022-07-03 10:25:12 +00:00
export class TokenSession implements Session {
2022-07-01 01:27:18 +00:00
csrf:string
xsrf:string
user:string
2022-07-03 10:25:12 +00:00
constructor(name:string, csrf:string, xsrf: string){
2022-07-01 01:27:18 +00:00
this.user = name
this.csrf = csrf
this.xsrf = xsrf;
}
2022-07-07 07:54:36 +00:00
request = async (verb:string,url:string,data:any, c:EndpointCreator = api_endpoint):Promise<AxiosResponse> => {
2022-07-01 01:27:18 +00:00
let promise
2022-07-03 10:25:12 +00:00
switch (verb.toLowerCase()){
2022-07-01 01:27:18 +00:00
case "post":
2022-07-07 07:54:36 +00:00
promise = axios.post(c(url),data,this.genHeaders())
2022-07-01 01:27:18 +00:00
break;
2022-08-10 01:39:44 +00:00
case "postform":
promise = axios.postForm(c(url),data)
break;
2022-07-01 01:27:18 +00:00
case "postraw":
const querystring = qs.stringify(data)
2022-07-07 07:54:36 +00:00
promise = axios.post(c(url),querystring,this.genHeaders())
2022-07-01 01:27:18 +00:00
break;
case "get":
default:
2022-07-07 07:54:36 +00:00
promise = axios.get(c(url),this.genHeaders())
2022-07-01 01:27:18 +00:00
}
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:{
2022-07-03 10:25:12 +00:00
Accept: "application/json",
2022-07-01 01:27:18 +00:00
"Update-Insecure-Requests": 1,
},
withCredentials:true
}
if(this.xsrf){
(out.headers as any)["X-XSRF-TOKEN"] = this.xsrf.replace("%3D","=")
}
return out
}
}