Advertisement
Miii7aka

api get all for search

Aug 7th, 2024
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as request from './requester';
  2.  
  3. const BASE_URL = 'http://localhost:3030/data/artists';
  4.  
  5. const getAll = async () => {
  6.     try {
  7.         const result = await request.get(BASE_URL);
  8.         const artists = Object.values(result);
  9.         return artists;
  10.     } catch (err) {
  11.         console.error('Error fetching getAll:', err);
  12.         throw err;
  13.     }
  14. };
  15.  
  16. const getOne = async (artistId) => {
  17.     try {
  18.         const result = await request.get(`${BASE_URL}/${artistId}`);
  19.         return result;
  20.     } catch (err) {
  21.         console.error('Error fetching getOne:', err);
  22.         throw err;
  23.     }
  24. };
  25.  
  26. const create = async (artistData) => {
  27.     try {
  28.         const result = await request.post(`${BASE_URL}`, artistData);
  29.         return result;
  30.     } catch (err) {
  31.         console.error('Error fetching create:', err);
  32.         throw err;
  33.     }
  34. };
  35.  
  36. const remove = async (artistId) => {
  37.     try {
  38.         const result = await request.del(`${BASE_URL}/${artistId}`);
  39.         return result;
  40.     } catch (err) {
  41.         console.error('Error fetching remove:', err);
  42.         throw err;
  43.     }
  44. };
  45.  
  46. const update = async (artistId, artistData) => {
  47.     try {
  48.         const result = await request.put(`${BASE_URL}/${artistId}`, artistData);
  49.         return result;
  50.     } catch (err) {
  51.         console.error('Error fetching update:', err);
  52.         throw err;
  53.     }
  54. };
  55.  
  56. const getLatest = async () => {
  57.     try {
  58.         const urlSearchParams = new URLSearchParams({
  59.             sortBy: `_createdOn desc`,
  60.             pageSize: 8,
  61.         });
  62.  
  63.         const res = await request.get(`${BASE_URL}?${urlSearchParams.toString()}`);
  64.         const result = Object.values(res);
  65.         return result;
  66.     } catch (err) {
  67.         console.error('Error fetching getLatest:', err);
  68.         throw err;
  69.     }
  70. };
  71.  
  72. const artistAPI = {
  73.     getAll,
  74.     getOne,
  75.     create,
  76.     remove,
  77.     update,
  78.     getLatest,
  79. };
  80.  
  81. export default artistAPI;
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement