Guest User

Untitled

a guest
Mar 7th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 1.53 KB | Source Code | 0 0
  1. // services/request.ts
  2. import axios from 'axios';
  3.  
  4. // Crear una instancia de Axios con configuraciones predeterminadas
  5. const apiClient = axios.create({
  6.   baseURL: '/proxy/example', // URL Api
  7.   headers: {
  8.     'Content-Type': 'application/json',
  9.   },
  10.   withCredentials: true
  11. });
  12.  
  13. // Método para peticiones GET
  14. export const get = async (url: string, params = {}) => {
  15.   try {
  16.     const response = await apiClient.get(url, { params, withCredentials: true });
  17.     return response.data;
  18.   } catch (error) {
  19.     throw error;
  20.   }
  21. };
  22.  
  23. // Método para peticiones POST
  24. export const post = async (url: string, data: any) => {
  25.   try {
  26.     const response = await apiClient.post(url, data, { withCredentials: true });
  27.     return response.data;
  28.   } catch (error) {
  29.     throw error;
  30.   }
  31. };
  32.  
  33. // Método para peticiones PUT
  34. export const put = async (url: string, data: any) => {
  35.   try {
  36.     const response = await apiClient.put(url, data, { withCredentials: true });
  37.     return response.data;
  38.   } catch (error) {
  39.     throw error;
  40.   }
  41. };
  42.  
  43. // Método para peticiones PATCH
  44. export const patch = async (url: string, data: any) => {
  45.   try {
  46.     const response = await apiClient.patch(url, data, { withCredentials: true });
  47.     return response.data;
  48.   } catch (error) {
  49.     throw error;
  50.   }
  51. };
  52.  
  53. // Método para peticiones DELETE
  54. export const remove = async (url: string) => {
  55.   try {
  56.     const response = await apiClient.delete(url, { withCredentials: true });
  57.     return response.data;
  58.   } catch (error) {
  59.     throw error;
  60.   }
  61. };
Advertisement
Add Comment
Please, Sign In to add comment