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

113 lines
2.6 KiB
TypeScript

export default class ApiService {
static async getUser(userId?: number) {
try {
return (await fetch('/api/v1/client/user/')).json();
} catch (e) {
console.error(`getUser ERROR: ${e.message}`);
return e;
}
}
static async getAllUsers() {
try {
return (await fetch('/api/v1/admin/users')).json();
} catch (e) {
console.error(`getAllUsers ERROR: ${e.message}`);
return e;
}
}
static async getChild(id: number): Promise<IApiResponse> {
try {
return (await fetch(`/api/v1/client/child/${id}`)).json();
} catch (e) {
console.error(`getChild ERROR: ${e.message}`);
return e;
}
}
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();
} catch (e) {
console.error(`createConnection ERROR: ${e.message}`);
return e;
}
}
static async updateChild(child_id: number, data: any) {
const options = {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
}
};
try {
const response = await fetch(`/api/v1/client/child/${child_id}`, options);
console.log(response);
return response.json();
} catch (e) {
console.error(`updateChildCover ERROR: ${e.message}`);
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) {
console.error(`createCall ERROR: ${e.message}`);
return false;
}
}
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) {
console.error(`createChild ERROR: ${e.message}`);
return false;
}
}
}
interface IApiResponse {
code: number;
data?: any;
message?: string;
}