Skip to content

Instantly share code, notes, and snippets.

@ackvf
Created February 10, 2021 15:19
Show Gist options
  • Save ackvf/ad7d94720526929b9799c899d7e74f8f to your computer and use it in GitHub Desktop.
Save ackvf/ad7d94720526929b9799c899d7e74f8f to your computer and use it in GitHub Desktop.
Services
import CoreAPIService from './CoreAPIService'
/* Copy/paste this file to create new Service */
class RequestService {
// CREATE
// READ
async getBlankPromise() {
return new Promise<{ blank: any }>((resolve, reject) => {
CoreAPIService.get('')
.then(response => {
resolve(response.data)
})
.catch(err => {
console.error(err)
reject(err)
})
})
}
// method syntax
async getBlankMethod(params) {
return CoreAPIService.get('')
.then<{ blank: any }>(response => response.data)
}
// field syntax
getBlankField = async params =>
CoreAPIService.get('')
.then<{ blank: any }>(response => response.data)
// UPDATE
}
export default new RequestService()
import CoreAPIService from './CoreAPIService';
import { CommentInterface } from '../interfaces/requestInterface';
class CommService {
// CREATE
addCommentToRequest = async (requestId: number, data: FormData) =>
CoreAPIService.post(`comments/${requestId}`, data, { 'Content-Type': 'multipart/form-data' })
.then<CommentInterface>(response => response.data);
addNoteToRequest = async (requestId: number, data: FormData) =>
CoreAPIService.post(`notes/${requestId}`, data, { 'Content-Type': 'multipart/form-data' })
.then<CommentInterface>(response => response.data);
// READ
/* Don'tforget about the Comments-to-Files mapper in RequestsDuck.extractFilesFromMessages */
getCommentsByRequestId = async (requestId: number) =>
CoreAPIService.get(`comments/${requestId}`)
.then<CommentInterface[]>(response => response.data);
}
export default new CommService();
import axios from 'axios';
import { TOKEN_STORAGE_NAME } from './variables';
import StorageService from './StorageService';
const API = process.env.REACT_APP_API_URL;
// tslint:disable-next-line: no-console
export const logAndRethrow = err => {
console.error('API %O', err);
throw err;
};
const headers = { 'Authorization': `Bearer ${StorageService.get(TOKEN_STORAGE_NAME)}` };
class CoreAPIService {
async get(url, params = {}) {
return axios.request({
method: 'get',
url: `${API}/${url}`,
headers,
params,
}).catch(logAndRethrow);
}
async post(url, data = {}, header = {}) {
return axios.request({
method: 'post',
url: `${API}/${url}`,
headers: { ...headers, ...header },
data,
}).catch(logAndRethrow);
}
async put(url, data) {
return axios.request({
method: 'put',
url: `${API}/${url}`,
headers,
data,
}).catch(logAndRethrow);
}
async patch(url, data) {
return axios.request({
method: 'patch',
url: `${API}/${url}`,
headers,
data,
}).catch(logAndRethrow);
}
async delete(url, data = {}) {
return axios.request({
method: 'delete',
url: `${API}/${url}`,
headers,
data,
}).catch(logAndRethrow);
}
}
export default new CoreAPIService();
import CoreAPIService from './CoreAPIService';
import { RequestStateEnum, RequestInterface, RequestStateCount, CommentInterface, NoteInterface, FileInteface } from '../interfaces/requestInterface';
import { extractFilesFromMessages } from '../utils/utils';
// TODO delete this after DB is purged of old data
const normalizeRequests = (requests: RequestInterface[]) => requests.map(normalizeRequest)
const normalizeRequest = (request: RequestInterface) => {
// @ts-ignore
if (!request.service) request.service = { type: 'DB_OLD_DATA', description: 'DB_OLD_DATA' }
// @ts-ignore
if (!request.user) request.user = { first_name: 'DB_OLD_DATA', last_name: 'DB_OLD_DATA', phone_number: 'DB_OLD_DATA', email: 'DB_OLD_DATA' }
if (!request.state) request.state = RequestStateEnum.PAID
return request
}
const mapFilesFromMessages = (request: RequestInterface) => {
request._commentsFiles = extractFilesFromMessages(request.comments)
request._notesFiles = extractFilesFromMessages(request.notes)
return request
}
class RequestService {
// CREATE
createRequest = async (userId: string, request?: RequestInterface) =>
CoreAPIService.post(`requests/${userId}`, request)
.then<RequestInterface>(response => response.data)
// READ
getRequestsCount = async () =>
CoreAPIService.get('requests/status-count')
.then<RequestStateCount>(response => response.data)
getRequests = async (state?: RequestStateEnum) =>
CoreAPIService.get('requests' + (state ? `?state=${state}` : ''))
.then<RequestInterface[]>(response => response.data)
.then(normalizeRequests) // TODO delete this after DB is purged of old data
getUserRequests = async (userId: string) =>
CoreAPIService.get(`requests/user/${userId}`)
.then<RequestInterface[]>(response => response.data)
getRequestById = async (id) =>
CoreAPIService.get(`requests/${id}`)
.then<RequestInterface>(response => response.data)
.then(mapFilesFromMessages)
// UPDATE
updateRequest = async (id, data: Partial<RequestInterface> = {}) =>
CoreAPIService.put(`requests/${id}`, data)
.then<RequestInterface>(response => response.data)
cancelRequest = async (id) =>
CoreAPIService.post(`requests/cancel/${id}`)
.then<RequestInterface>(response => response.data)
closeRequest = async (id) =>
CoreAPIService.post(`requests/close/${id}`)
.then<RequestInterface>(response => response.data)
deliverRequest = async (id) =>
CoreAPIService.post(`requests/delivered/${id}`)
.then<RequestInterface>(response => response.data)
}
export default new RequestService();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment