123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- import type { TPaginacion, DefaultResponse } from "../types/responses";
- export interface IRequestParams {
- expand?: string;
- ordenar?: string | "id-desc" | "id-asc";
- limite?: number;
- pagina?: number;
- buscar?: string;
- [key: string]: any;
- }
- export interface IRequest {
- req: string;
- endpoint: string;
- params: any;
- body: any;
- }
- const API_URL = "https://pos.api.turquessacoffee.com/";
- export interface IHttpService {
- get: <T>(
- endpoint: string,
- params?: any,
- updateQueryParams?: boolean
- ) => Promise<DefaultResponse<T>>;
- getBlob: (endpoint: string, data: any) => Promise<Blob>;
- downloadBlob: (
- endpoint: string,
- data: any,
- fileName: string
- ) => Promise<void>;
- post: <T>(endpoint: string, body: any) => Promise<DefaultResponse<T>>;
- postFormData: (endpoint: string, data: any) => Promise<DefaultResponse<any>>;
- delete: <T>(endpoint: string, body: T) => Promise<DefaultResponse<any>>;
- put: (endpoint: string, body: any) => Promise<any>;
- }
- export class HttpService implements IHttpService {
- API_URL: string;
- constructor(API_URL: string) {
- this.API_URL = API_URL;
- }
- static DEFAULT_HEADERS = () => {
- return {
- "Content-Type": "application/json",
- // Authorization: `Bearer ${localStorage.getItem("token")}`,
- };
- };
- static FILE_HEADERS = () => {
- return {
- "Content-Type": "multipart/form-data",
- // Authorization: `Bearer ${localStorage.getItem("token")}`,
- };
- };
- static DEFAULT_REQUEST_PARAMS = {
- limite: 10,
- pagina: 1,
- ordenar: "id-desc",
- };
- static DEFAULT_PAGINACION: TPaginacion = {
- total: 0,
- pagina: 1,
- limite: 10,
- };
- static paramsToQuery = (params: any) => {
- return Object.keys(params)
- .map(
- (key) => encodeURIComponent(key) + "=" + encodeURIComponent(params[key])
- )
- .join("&");
- };
- static EMPTY_REQUEST = (): IRequest => ({
- req: "",
- endpoint: "",
- params: null,
- body: null,
- });
- static GET_REQUEST = (endpoint: string, params: any = {}): IRequest => ({
- req: "GET",
- endpoint,
- params,
- body: null,
- });
- static POST_REQUEST = <T = any>(
- endpoint: string,
- body: T,
- params: any = {}
- ): IRequest => ({
- req: "POST",
- endpoint,
- params,
- body,
- });
- static DELETE_REQUEST = (endpoint: string, params: any = {}) =>
- ({
- req: "DELETE",
- endpoint: `${endpoint}/eliminar`,
- body: {
- ...params,
- },
- } as IRequest);
- get = async <T>(
- endpoint: string,
- params: any = HttpService.DEFAULT_REQUEST_PARAMS,
- updateQueryParams: boolean = false
- ) => {
- const stringParams = params ? HttpService.paramsToQuery(params) : "";
- const queryParams = `?${new URLSearchParams(stringParams).toString()}`;
- let url = `${this.API_URL}${endpoint}`;
- if (queryParams) {
- url = `${this.API_URL}${endpoint}${queryParams}`;
- if (updateQueryParams && window.location.search !== queryParams) {
- //Actualizar los queryparams de la url actual
- window.history.pushState(
- {},
- "",
- window.location.pathname + `${queryParams}`
- );
- }
- }
- const _response = await fetch(url, {
- method: "GET",
- headers: HttpService.DEFAULT_HEADERS(),
- });
- console.log("response", _response);
- const response = (await _response.json()) as DefaultResponse<T>;
- return {
- ...response,
- isError: response?.status !== 200 ? true : false,
- status: response?.status,
- resultado: response?.resultado || response,
- } as DefaultResponse<T>;
- };
- getBlob = async (endpoint: string, data: any) => {
- const _response = await fetch(`${this.API_URL}${endpoint}`, {
- method: "GET",
- headers: HttpService.DEFAULT_HEADERS(),
- body: JSON.stringify(data),
- });
- const response = await _response.blob();
- return response;
- };
- downloadBlob = async (
- endpoint: string,
- data: any,
- fileName: string = "fileName"
- ) => {
- const _response = await fetch(`${this.API_URL}${endpoint}`, {
- method: "GET",
- headers: HttpService.DEFAULT_HEADERS(),
- body: JSON.stringify(data),
- });
- const blob = await _response.blob();
- const urlBlob = URL.createObjectURL(blob);
- const link = document.createElement("a");
- link.href = urlBlob;
- link.setAttribute("download", fileName);
- document.body.appendChild(link);
- link.click();
- URL.revokeObjectURL(urlBlob);
- link.remove();
- };
- post = async <T>(endpoint: string, body: any) => {
- const _response = await fetch(`${this.API_URL}${endpoint}`, {
- method: "POST",
- headers: HttpService.DEFAULT_HEADERS(),
- body: JSON.stringify(body),
- });
- const status = _response.status;
- const response = (await _response.json()) as DefaultResponse<T>;
- return {
- ...response,
- isError: status !== 200 ? true : false,
- status: status,
- } as DefaultResponse<T>;
- };
- postFormData = async (endpoint: string, data: any) => {
- const _response = await fetch(`${this.API_URL}${endpoint}`, {
- method: "POST",
- headers: HttpService.FILE_HEADERS(),
- body: data,
- });
- const response = await _response.json;
- return {
- ...response,
- isError: _response?.status !== 200 ? true : false,
- status: _response?.status,
- } as DefaultResponse<any>;
- };
- delete = async <T = any>(endpoint: string, body: T) => {
- const response = await fetch(`${this.API_URL}${endpoint}`, {
- body: JSON.stringify(body),
- method: "DELETE",
- headers: HttpService.DEFAULT_HEADERS(),
- });
- const status = response.status;
- const responseJson = await response.json();
- return {
- ...responseJson,
- isError: status !== 200 ? true : false,
- status: status,
- } as DefaultResponse<any>;
- };
- put = async (endpoint: string, body: any) => {
- const response = await fetch(`${this.API_URL}${endpoint}`, {
- method: "PUT",
- headers: HttpService.DEFAULT_HEADERS(),
- body: JSON.stringify(body),
- });
- return response.json();
- };
- }
- export const http = new HttpService(API_URL);
|