Files
react-logo/frontend/src/services/api-client.service.js

63 lines
1.2 KiB
JavaScript
Raw Normal View History

import { UnauthorizedError } from '../errors/unauthorized.error.js';
export const UnauthorizedStatusCode = 401;
2020-02-25 10:14:21 +01:00
export class APIClient {
constructor(baseURL = 'http://localhost:8001/api/v1') {
this.baseURL = baseURL;
2020-02-25 10:14:21 +01:00
this.login = this.login.bind(this);
this.logout = this.logout.bind(this);
2020-02-25 14:35:47 +01:00
this.retrieveSessionUser = this.retrieveSessionUser.bind(this);
2020-02-25 10:14:21 +01:00
this.listUsers = this.listUsers.bind(this);
this.listRequests = this.listRequests.bind(this);
}
login(username, password) {
return this._callAPI('/login', { username, password }, 'POST')
}
logout() {
return this._callAPI('/logout')
}
2020-02-25 14:35:47 +01:00
retrieveSessionUser() {
2020-02-25 10:14:21 +01:00
return this._callAPI('/me')
}
listUsers() {
return this._callAPI('/users')
}
listRequests() {
}
createRequest(request) {
}
updateRequestStatus(reqID, newStatus) {
}
_callAPI(path, body, method='GET') {
2020-02-25 10:15:59 +01:00
return fetch(this.baseURL + path, {
2020-02-25 10:14:21 +01:00
method,
headers: {
'Content-Type': 'application/json',
},
mode: 'cors',
2020-02-25 10:15:59 +01:00
credentials: 'include',
2020-02-25 10:14:21 +01:00
body: JSON.stringify(body),
})
.then(res => {
if (res.status === UnauthorizedStatusCode) {
throw new UnauthorizedError();
}
return res;
})
2020-02-25 10:14:21 +01:00
.then(res => res.json())
}
}