first commit
This commit is contained in:
62
src/http/api.ts
Normal file
62
src/http/api.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
|
||||
import { db } from '../stores/storage/message'
|
||||
|
||||
interface Data {}
|
||||
interface ResponseData {
|
||||
code: number;
|
||||
message: string;
|
||||
data: Array;
|
||||
url: string;
|
||||
}
|
||||
|
||||
export function generateSessionId() {
|
||||
var characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||
var sessionId = '';
|
||||
for (var i = 0; i < 32; i++) {
|
||||
sessionId += characters.charAt(Math.floor(Math.random() * characters.length));
|
||||
}
|
||||
return sessionId;
|
||||
}
|
||||
|
||||
|
||||
export function baseUrl(){
|
||||
return 'https://x--mo.com:8888';
|
||||
}
|
||||
|
||||
export function getToken(){
|
||||
|
||||
const sessionId = localStorage.getItem('AnsnidSessionId') || generateSessionId();
|
||||
|
||||
localStorage.setItem('AnsnidSessionId', sessionId)
|
||||
|
||||
console.log(sessionId)
|
||||
|
||||
return sessionId;
|
||||
}
|
||||
|
||||
export function fetchData(data: Data | null, callback: (data: ResponseData) => void, url: string, method ? : string): void {
|
||||
let fetchOptions = {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": getToken()
|
||||
}
|
||||
};
|
||||
if (method && method.toUpperCase() === "POST" && data) {
|
||||
fetchOptions = { ...fetchOptions,
|
||||
method: "POST",
|
||||
body: JSON.stringify(data)
|
||||
};
|
||||
} else {
|
||||
fetchOptions = { ...fetchOptions,
|
||||
method: "GET"
|
||||
};
|
||||
}
|
||||
url = baseUrl() + url;
|
||||
fetch(url, fetchOptions).then(response => response.json().then(data => {
|
||||
callback(data);
|
||||
})).catch(error => {
|
||||
throw new Error('Request failed', {
|
||||
cause: error?.error,
|
||||
})
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user