improvement: add api modules

This commit is contained in:
Francesco Esposito 2019-07-25 09:52:27 +02:00
parent d8a990af71
commit 053b47e72d
4 changed files with 96 additions and 0 deletions

35
src/api/authentication.js Normal file
View File

@ -0,0 +1,35 @@
import { config } from "../config";
import { getAuthHeader } from "../utils/auth";
import { httpClient } from "./httpClient";
export const login = (email, password) => {
const header = new Headers({
"Content-Type": "application/json"
});
const body = {
email,
password
};
const options = {
method: "POST",
headers: header,
body: JSON.stringify(body)
};
return httpClient(`${config.baseURL}users/login`, options);
};
export const validate = () => {
const header = new Headers({
authorization: getAuthHeader()
});
const options = {
method: "GET",
headers: header
};
return httpClient(`${config.baseURL}users/validate`, options);
};

16
src/api/categories.js Normal file
View File

@ -0,0 +1,16 @@
import { config } from '../config';
import { httpClient } from './httpClient';
export const getAllTicketStatus = () => {
const header = new Headers({
'Content-Type': 'application/json'
});
const options = {
method: 'GET',
headers: header
};
return httpClient(`${config.baseURL}categories?type=TicketStatus`, options)
}

14
src/api/httpClient.js Normal file
View File

@ -0,0 +1,14 @@
export const httpClient = (url, options) => {
return fetch(url, options)
.then(response => {
if (!response.ok) {
throw new Error(response.status);
}
return response.json();
}).catch(err => {
throw new Error(err);
});
}

31
src/api/tickets.js Normal file
View File

@ -0,0 +1,31 @@
import { config } from '../config';
import { getAuthHeader } from '../utils/auth';
import { httpClient } from './httpClient';
export const getTickets = (page = 1, status='') => {
const header = new Headers({
'Content-Type': 'application/json',
'authorization': getAuthHeader()
});
const options = {
method: 'GET',
headers: header
};
return httpClient(`${config.baseURL}tickets?page=${page}&status=${status}`, options);
}
export const findTicket = (ticketId) => {
const header = new Headers({
'Content-Type': 'application/json',
'authorization': getAuthHeader()
});
const options = {
method: 'GET',
headers: header
};
return httpClient(`${config.baseURL}tickets?id=${ticketId}`, options);
}