Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import serverConfig from "./ServerConfig";
- import APIHelpers from "./APIHelpers";
- export default class API {
- constructor() {
- }
- static Authenticate() {
- let token = 'Basic ';
- return (
- this.get()(token, '/session').then((body) => {
- return (body);
- })
- );
- }
- static post(url, data, authorization = '', auth = false): Response {
- let request = {
- method: 'POST',
- headers: APIHelpers.defineHeaderWithContent(authorization, auth),
- body: JSON.stringify(data)
- };
- return (
- fetch(serverConfig.host + url, request).then((response) => {
- return (response);
- })
- );
- }
- static put(url, data, authorization = '', auth = false): Response {
- let request = {
- method: 'PUT',
- headers: APIHelpers.defineHeaderWithContent(authorization, auth),
- body: JSON.stringify(data)
- };
- return (
- fetch(serverConfig.host + url, request).then((response) => {
- return (response);
- })
- );
- }
- static delete(url, authorization = '', auth = false): Response {
- let request = {
- method: 'DELETE',
- headers: APIHelpers.defineHeaderWithoutContent(authorization, auth),
- };
- return (
- fetch(serverConfig.host + url, request)
- );
- }
- static get(url, authorization = '', auth = false): Response {
- let request = {
- method: 'GET',
- headers: APIHelpers.defineHeaderWithoutContent(authorization, auth),
- };
- return (
- fetch(serverConfig.host + url, request).then((response) => {
- return (response);
- })
- );
- }
- static patch(url, data, authorization = '', auth = undefined): Response {
- let request = {
- method: 'PATCH',
- headers: APIHelpers.defineHeaderWithContent(authorization, auth),
- body: JSON.stringify(data)
- };
- return (
- fetch(serverConfig.host + url, request).then((response) => {
- return (response);
- })
- );
- }
- static asyncGet(url, callback, authorization = '', auth = false): void {
- const request = async () => {
- let request = {
- method: 'GET',
- headers: await APIHelpers.defineHeaderWithoutContent(authorization, auth),
- };
- const response = await fetch(serverConfig.host + url, request);
- if (await response.ok) {
- const json = await response.json();
- await callback(json);
- }
- else
- console.log(response);
- };
- request();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement