export type TrendingProductsResponse = {
count: number;
results: TrendingProduct[];
params: ParamsProducts;
type: string;
pagination: PaginationProducts;
};
export type TrendingProduct = {
listing_id: number;
title: string;
price: string;
description: string;
currency_code: string;
url: string;
sku: string[];
is_vintage: boolean;
Images: Images[];
};
export type Images = {
url_170x135: string;
};
export type ParamsProducts = {
limit: string;
offset: number;
page: string;
};
export type PaginationProducts = {
effective_limit: number;
effective_offset: number;
next_offset: number;
effective_page: number;
next_page: number;
};
const PRODUCTS_LIST_FETCH_ERROR = "Could not fetch products";
const api_key = "zfbmo6g0aar1s1ppg350oqro";
const API = "https://community-etsy.p.rapidapi.com/listings/trending";
const FIELDS = [
"title",
"description",
"price",
"currency_code",
"url",
"listing_id",
"price",
];
const INCLUDES = ["Images(url_170x135)"];
const options = () => ({
method: "GET",
headers: {
"x-rapidapi-host": "community-etsy.p.rapidapi.com",
"x-rapidapi-key": "10a5116c48msh3a20ca2f36faebbp1bf756jsnb5eda315068b",
useQueryString: "true",
},
});
export const getTrendingProducts = (
page = "1",
offset = "0",
limit = "100"
): Promise<TrendingProduct[]> => {
return fetch(
`${API}?${new URLSearchParams({
api_key,
page,
limit,
offset,
fields: FIELDS.join(","),
includes: INCLUDES.join(", "),
})}`,
options()
)
.then(checkStatus(PRODUCTS_LIST_FETCH_ERROR))
.then((res) => res.json())
.then(filterErrorProducts);
};
export const filterErrorProducts = (
response: TrendingProductsResponse
): TrendingProduct[] => {
return response.results.filter(
(product) =>
!product.hasOwnProperty("error_messages") && product.price !== undefined
);
};
export const checkStatus = (errorMessage: string) => (res: Response) => {
if (res.status < 500) {
return res;
}
throw Error(errorMessage);
}
export const filterByName = (name: string) => (product: TrendingProduct) =>
product.title.includes(name);
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}