seepur/resources/scripts/applications/services/api.service.ts

147 lines
3.5 KiB
TypeScript
Raw Normal View History

2020-03-17 22:16:34 +00:00
export default class ApiService {
static async getUser(userId?: number) {
2020-05-01 17:24:22 +00:00
try {
return (await fetch('/api/v1/client/user/')).json();
} catch (e) {
console.error(`getUser ERROR: ${e.message}`);
return e;
}
2020-03-17 22:16:34 +00:00
}
2020-04-12 14:25:42 +00:00
2020-03-17 22:16:34 +00:00
static async getAllUsers() {
2020-05-01 17:24:22 +00:00
try {
return (await fetch('/api/v1/admin/users')).json();
} catch (e) {
console.error(`getAllUsers ERROR: ${e.message}`);
return e;
}
2020-03-17 22:16:34 +00:00
}
static async uploadBook(payload: { title: string, author: string, pages: string[], ltr: boolean }) {
// console.log(payload);
// return { code: 0 }
const options = {
method: 'POST',
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json'
}
}
try {
return (await fetch('/api/v1/client/book/create', options)).json();
} catch (e) {
console.error(`uploadBook ERROR: ${e.message}`);
return e;
}
}
2020-05-23 21:32:25 +00:00
static async updateUser(payload: { name?: string; avatar?: string; profile_cover?: string; email?: string }) {
const options = {
method: 'PUT',
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json'
}
}
try {
return (await fetch('/api/v1/client/user/', options)).json();
} catch (e) {
console.error(`updateUser ERROR: ${e.message}`);
return e;
}
}
2020-04-12 14:25:42 +00:00
static async getChild(id: number): Promise<IApiResponse> {
2020-05-01 17:24:22 +00:00
try {
return (await fetch(`/api/v1/client/child/${id}`)).json();
} catch (e) {
console.error(`getChild ERROR: ${e.message}`);
return e;
}
2020-04-12 14:25:42 +00:00
}
static async createConnection(payload: { child_id: number, email: string, is_parent: boolean }) {
const options = {
method: 'POST',
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json'
}
}
try {
return (await fetch('/api/v1/client/connections/create', options)).json();
2020-05-01 17:24:22 +00:00
} catch (e) {
console.error(`createConnection ERROR: ${e.message}`);
return e;
2020-04-12 14:25:42 +00:00
}
}
static async updateChild(child_id: number, data: any) {
2020-04-12 14:25:42 +00:00
const options = {
method: 'POST',
body: JSON.stringify(data),
2020-04-12 14:25:42 +00:00
headers: {
'Content-Type': 'application/json'
}
};
try {
const response = await fetch(`/api/v1/client/child/${child_id}`, options);
2020-04-12 14:25:42 +00:00
console.log(response);
return response.json();
} catch (e) {
2020-05-01 17:24:22 +00:00
console.error(`updateChildCover ERROR: ${e.message}`);
2020-04-12 14:25:42 +00:00
return false;
}
};
static async createCall(payload: { connection_id: number, child_id: number }) {
const options = {
method: 'POST',
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json'
}
};
try {
const response = await fetch('/api/v1/client/call/create', options);
return response.json();
} catch (e) {
2020-05-01 17:24:22 +00:00
console.error(`createCall ERROR: ${e.message}`);
2020-04-12 14:25:42 +00:00
return false;
}
}
2020-03-17 22:16:34 +00:00
static async createChild(name: string, dob: Date, avatar: string): Promise<any> {
const options = {
method: 'POST',
body: JSON.stringify({
name,
dob,
avatar
}),
headers: {
'Content-Type': 'application/json'
}
};
try {
const response = await fetch('/api/v1/client/child/', options);
return response.json();
} catch (e) {
2020-05-01 17:24:22 +00:00
console.error(`createChild ERROR: ${e.message}`);
2020-03-17 22:16:34 +00:00
return false;
}
}
}
2020-04-12 14:25:42 +00:00
interface IApiResponse {
code: number;
data?: any;
message?: string;
}