|
@@ -1,4 +1,5 @@
|
|
-import type { TPaginacion, DefaultResponse } from "../types/responses";
|
|
|
|
|
|
+import { API_URL } from "../constants";
|
|
|
|
+import type { DefaultResponse, TPaginacion } from "../types/responses";
|
|
|
|
|
|
export interface IRequestParams {
|
|
export interface IRequestParams {
|
|
expand?: string;
|
|
expand?: string;
|
|
@@ -17,8 +18,6 @@ export interface IRequest {
|
|
body: any;
|
|
body: any;
|
|
}
|
|
}
|
|
|
|
|
|
-const API_URL = "https://pos.api.turquessacoffee.com/";
|
|
|
|
-
|
|
|
|
export interface IHttpService {
|
|
export interface IHttpService {
|
|
get: <T>(
|
|
get: <T>(
|
|
endpoint: string,
|
|
endpoint: string,
|
|
@@ -44,17 +43,15 @@ export class HttpService implements IHttpService {
|
|
this.API_URL = API_URL;
|
|
this.API_URL = API_URL;
|
|
}
|
|
}
|
|
|
|
|
|
- static DEFAULT_HEADERS = () => {
|
|
|
|
|
|
+ DEFAULT_HEADERS(): Record<string, string> {
|
|
return {
|
|
return {
|
|
"Content-Type": "application/json",
|
|
"Content-Type": "application/json",
|
|
- // Authorization: `Bearer ${localStorage.getItem("token")}`,
|
|
|
|
};
|
|
};
|
|
};
|
|
};
|
|
|
|
|
|
- static FILE_HEADERS = () => {
|
|
|
|
|
|
+ FILE_HEADERS(): Record<string, string> {
|
|
return {
|
|
return {
|
|
"Content-Type": "multipart/form-data",
|
|
"Content-Type": "multipart/form-data",
|
|
- // Authorization: `Bearer ${localStorage.getItem("token")}`,
|
|
|
|
};
|
|
};
|
|
};
|
|
};
|
|
|
|
|
|
@@ -134,10 +131,12 @@ export class HttpService implements IHttpService {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ const headers = this.DEFAULT_HEADERS();
|
|
const _response = await fetch(url, {
|
|
const _response = await fetch(url, {
|
|
method: "GET",
|
|
method: "GET",
|
|
- headers: HttpService.DEFAULT_HEADERS(),
|
|
|
|
|
|
+ headers: headers,
|
|
});
|
|
});
|
|
|
|
+
|
|
const response = (await _response.json()) as DefaultResponse<T>;
|
|
const response = (await _response.json()) as DefaultResponse<T>;
|
|
|
|
|
|
return {
|
|
return {
|
|
@@ -149,10 +148,12 @@ export class HttpService implements IHttpService {
|
|
};
|
|
};
|
|
|
|
|
|
getBlob = async (endpoint: string, data: any) => {
|
|
getBlob = async (endpoint: string, data: any) => {
|
|
- const _response = await fetch(`${this.API_URL}${endpoint}`, {
|
|
|
|
|
|
+ const headers = this.DEFAULT_HEADERS();
|
|
|
|
+ const queryParams = new URLSearchParams(data).toString();
|
|
|
|
+ const url = `${this.API_URL}${endpoint}?${queryParams}`;
|
|
|
|
+ const _response = await fetch(url, {
|
|
method: "GET",
|
|
method: "GET",
|
|
- headers: HttpService.DEFAULT_HEADERS(),
|
|
|
|
- body: JSON.stringify(data),
|
|
|
|
|
|
+ headers: headers,
|
|
});
|
|
});
|
|
const response = await _response.blob();
|
|
const response = await _response.blob();
|
|
return response;
|
|
return response;
|
|
@@ -163,9 +164,10 @@ export class HttpService implements IHttpService {
|
|
data: any,
|
|
data: any,
|
|
fileName: string = "fileName"
|
|
fileName: string = "fileName"
|
|
) => {
|
|
) => {
|
|
|
|
+ const headers = this.DEFAULT_HEADERS();
|
|
const _response = await fetch(`${this.API_URL}${endpoint}`, {
|
|
const _response = await fetch(`${this.API_URL}${endpoint}`, {
|
|
method: "GET",
|
|
method: "GET",
|
|
- headers: HttpService.DEFAULT_HEADERS(),
|
|
|
|
|
|
+ headers: headers,
|
|
body: JSON.stringify(data),
|
|
body: JSON.stringify(data),
|
|
});
|
|
});
|
|
const blob = await _response.blob();
|
|
const blob = await _response.blob();
|
|
@@ -181,9 +183,10 @@ export class HttpService implements IHttpService {
|
|
};
|
|
};
|
|
|
|
|
|
post = async <T>(endpoint: string, body: any) => {
|
|
post = async <T>(endpoint: string, body: any) => {
|
|
|
|
+ const headers = this.DEFAULT_HEADERS();
|
|
const _response = await fetch(`${this.API_URL}${endpoint}`, {
|
|
const _response = await fetch(`${this.API_URL}${endpoint}`, {
|
|
method: "POST",
|
|
method: "POST",
|
|
- headers: HttpService.DEFAULT_HEADERS(),
|
|
|
|
|
|
+ headers: headers,
|
|
body: JSON.stringify(body),
|
|
body: JSON.stringify(body),
|
|
});
|
|
});
|
|
const status = _response.status;
|
|
const status = _response.status;
|
|
@@ -197,9 +200,10 @@ export class HttpService implements IHttpService {
|
|
};
|
|
};
|
|
|
|
|
|
postFormData = async (endpoint: string, data: any) => {
|
|
postFormData = async (endpoint: string, data: any) => {
|
|
|
|
+ const headers = this.FILE_HEADERS();
|
|
const _response = await fetch(`${this.API_URL}${endpoint}`, {
|
|
const _response = await fetch(`${this.API_URL}${endpoint}`, {
|
|
method: "POST",
|
|
method: "POST",
|
|
- headers: HttpService.FILE_HEADERS(),
|
|
|
|
|
|
+ headers: headers,
|
|
body: data,
|
|
body: data,
|
|
});
|
|
});
|
|
|
|
|
|
@@ -213,10 +217,11 @@ export class HttpService implements IHttpService {
|
|
};
|
|
};
|
|
|
|
|
|
delete = async <T = any>(endpoint: string, body: T) => {
|
|
delete = async <T = any>(endpoint: string, body: T) => {
|
|
|
|
+ const headers = this.DEFAULT_HEADERS();
|
|
const response = await fetch(`${this.API_URL}${endpoint}`, {
|
|
const response = await fetch(`${this.API_URL}${endpoint}`, {
|
|
body: JSON.stringify(body),
|
|
body: JSON.stringify(body),
|
|
method: "DELETE",
|
|
method: "DELETE",
|
|
- headers: HttpService.DEFAULT_HEADERS(),
|
|
|
|
|
|
+ headers: headers,
|
|
});
|
|
});
|
|
|
|
|
|
const status = response.status;
|
|
const status = response.status;
|
|
@@ -229,9 +234,10 @@ export class HttpService implements IHttpService {
|
|
};
|
|
};
|
|
|
|
|
|
put = async (endpoint: string, body: any) => {
|
|
put = async (endpoint: string, body: any) => {
|
|
|
|
+ const headers = this.DEFAULT_HEADERS();
|
|
const response = await fetch(`${this.API_URL}${endpoint}`, {
|
|
const response = await fetch(`${this.API_URL}${endpoint}`, {
|
|
method: "PUT",
|
|
method: "PUT",
|
|
- headers: HttpService.DEFAULT_HEADERS(),
|
|
|
|
|
|
+ headers: headers,
|
|
body: JSON.stringify(body),
|
|
body: JSON.stringify(body),
|
|
});
|
|
});
|
|
return response.json();
|
|
return response.json();
|