90 lines
2.0 KiB
JavaScript
90 lines
2.0 KiB
JavaScript
|
const serverUrl = "http://localhost:8081";
|
||
|
|
||
|
// export function createPostObject(
|
||
|
// userId,
|
||
|
// createdDateTime,
|
||
|
// text,
|
||
|
// img,
|
||
|
// ) {
|
||
|
// return {
|
||
|
// userId,
|
||
|
// createdDateTime,
|
||
|
// text,
|
||
|
// img,
|
||
|
// };
|
||
|
// }
|
||
|
|
||
|
export class ApiEndpoint {
|
||
|
constructor(endpoint) {
|
||
|
this.endpoint = endpoint;
|
||
|
}
|
||
|
|
||
|
async getObjects() {
|
||
|
const response = await fetch(`${serverUrl}/${this.endpoint}`);
|
||
|
if (!response.ok) {
|
||
|
throw response.statusText;
|
||
|
}
|
||
|
return response.json();
|
||
|
}
|
||
|
|
||
|
async getObject(id) {
|
||
|
const response = await fetch(`${serverUrl}/${this.endpoint}/${id}`);
|
||
|
if (!response.ok) {
|
||
|
throw response.statusText;
|
||
|
}
|
||
|
return response.json();
|
||
|
}
|
||
|
|
||
|
async createObject(obj) {
|
||
|
const options = {
|
||
|
method: "POST",
|
||
|
body: JSON.stringify(obj),
|
||
|
headers: {
|
||
|
"Accept": "application/json",
|
||
|
"Content-Type": "application/json",
|
||
|
},
|
||
|
};
|
||
|
|
||
|
const response = await fetch(`${serverUrl}/${this.endpoint}`, options);
|
||
|
if (!response.ok) {
|
||
|
throw response.statusText;
|
||
|
}
|
||
|
return response.json();
|
||
|
}
|
||
|
|
||
|
async updateObject(obj) {
|
||
|
const options = {
|
||
|
method: "PUT",
|
||
|
body: JSON.stringify(obj),
|
||
|
headers: {
|
||
|
"Accept": "application/json",
|
||
|
"Content-Type": "application/json",
|
||
|
},
|
||
|
};
|
||
|
|
||
|
const response = await fetch(
|
||
|
`${serverUrl}/${this.endpoint}/${obj.id}`,
|
||
|
options,
|
||
|
);
|
||
|
if (!response.ok) {
|
||
|
throw response.statusText;
|
||
|
}
|
||
|
return response.json();
|
||
|
}
|
||
|
|
||
|
async deleteObject(id) {
|
||
|
const options = {
|
||
|
method: "DELETE",
|
||
|
};
|
||
|
|
||
|
const response = await fetch(
|
||
|
`${serverUrl}/${this.endpoint}/${id}`,
|
||
|
options,
|
||
|
);
|
||
|
if (!response.ok) {
|
||
|
throw response.statusText;
|
||
|
}
|
||
|
return response.json();
|
||
|
}
|
||
|
}
|