Facebook
From Subtle Eider, 2 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 141
  1. export type TrendingProductsResponse = {
  2.     count: number;
  3.     results: TrendingProduct[];
  4.     params: ParamsProducts;
  5.     type: string;
  6.     pagination: PaginationProducts;
  7. };
  8.  
  9. export type TrendingProduct = {
  10.     listing_id: number;
  11.     title: string;
  12.     price: string;
  13.     description: string;
  14.     currency_code: string;
  15.     url: string;
  16.     sku: string[];
  17.     is_vintage: boolean;
  18.     Images: Images[];
  19. };
  20.  
  21. export type Images = {
  22.     url_170x135: string;
  23. };
  24.  
  25. export type ParamsProducts = {
  26.     limit: string;
  27.     offset: number;
  28.     page: string;
  29. };
  30.  
  31. export type PaginationProducts = {
  32.     effective_limit: number;
  33.     effective_offset: number;
  34.     next_offset: number;
  35.     effective_page: number;
  36.     next_page: number;
  37. };
  38.  
  39. const PRODUCTS_LIST_FETCH_ERROR = "Could not fetch products";
  40.  
  41. const api_key = "zfbmo6g0aar1s1ppg350oqro";
  42. const API = "https://community-etsy.p.rapidapi.com/listings/trending";
  43. const FIELDS = [
  44.     "title",
  45.     "description",
  46.     "price",
  47.     "currency_code",
  48.     "url",
  49.     "listing_id",
  50.     "price",
  51. ];
  52. const INCLUDES = ["Images(url_170x135)"];
  53.  
  54. const options = () => ({
  55.     method: "GET",
  56.     headers: {
  57.         "x-rapidapi-host": "community-etsy.p.rapidapi.com",
  58.         "x-rapidapi-key": "10a5116c48msh3a20ca2f36faebbp1bf756jsnb5eda315068b",
  59.         useQueryString: "true",
  60.     },
  61. });
  62.  
  63. export const getTrendingProducts = (
  64.     page = "1",
  65.     offset = "0",
  66.     limit = "100"
  67. ): Promise<TrendingProduct[]> => {
  68.     return fetch(
  69.         `${API}?${new URLSearchParams({
  70.             api_key,
  71.             page,
  72.             limit,
  73.             offset,
  74.             fields: FIELDS.join(","),
  75.             includes: INCLUDES.join(", "),
  76.         })}`,
  77.         options()
  78.     )
  79.         .then(checkStatus(PRODUCTS_LIST_FETCH_ERROR))
  80.         .then((res) => res.json())
  81.         .then(filterErrorProducts);
  82. };
  83.  
  84. export const filterErrorProducts = (
  85.     response: TrendingProductsResponse
  86. ): TrendingProduct[] => {
  87.     return response.results.filter(
  88.         (product) =>
  89.             !product.hasOwnProperty("error_messages") && product.price !== undefined
  90.     );
  91. };
  92.  
  93. export const checkStatus = (errorMessage: string) => (res: Response) => {
  94.     if (res.status < 500) {
  95.         return res;
  96.     }
  97.     throw Error(errorMessage);
  98. }
  99.  
  100. export const filterByName = (name: string) => (product: TrendingProduct) =>
  101.     product.title.includes(name);
  102.  
  103.