svelte-tenor
Version:
Tenor GIF components, implemented in Svelte.
66 lines (65 loc) • 2.88 kB
JavaScript
import { autocomplete as rawAutocomplete, categories as rawCategories, gifs as rawGifs, random as rawRandom, registerShare as rawRegisterShare, search as rawSearch, searchSuggestions as rawSearchSuggestions, trending as rawTrending, trendingTerms as rawTrendingTerms, } from './raw-api.js';
/** Transforms a `raw-api` response into a friendlier object. */
const formatResponse = ({ results, next }, quality = 'medium') => ({
results: results.map(({ id, title, content_description, media }) => {
const prefix = { low: 'nano', medium: 'tiny', high: '' }[quality];
return {
id,
description: title.length > 0 ? title : content_description,
width: media[0][`${prefix}gif`].dims[0],
height: media[0][`${prefix}gif`].dims[1],
gif: media[0][`${prefix}gif`].url,
};
}),
next,
});
/** Gets details about one or several GIFs. */
export const gifDetails = async ({ key, ids, quality, limit, page, }) => formatResponse(await rawGifs({
key,
ids: ids.join(','),
limit,
pos: page,
media_filter: 'basic',
}), quality);
/** Searches for GIFs. */
export const search = async ({ key, q, locale, quality, safety, ratio, limit, page, }) => formatResponse(await rawSearch({
key,
q,
locale,
contentfilter: safety,
ar_range: ratio,
limit,
pos: page,
media_filter: 'basic',
}), quality);
/** Searches for GIFs, but shuffles the result. */
export const shuffledSearch = async ({ key, q, locale, quality, safety, ratio, limit, page, }) => formatResponse(await rawRandom({
key,
q,
locale,
contentfilter: safety,
ar_range: ratio,
limit,
pos: page,
media_filter: 'basic',
}), quality);
/** Fetches trending GIFs. */
export const trending = async ({ key, locale, quality, safety, ratio, limit, page, }) => formatResponse(await rawTrending({
key,
locale,
contentfilter: safety,
ar_range: ratio,
limit,
pos: page,
media_filter: 'basic',
}), quality);
/** Registers a user’s sharing of a GIF. */
export const registerShare = async (options) => rawRegisterShare(options);
/** Completes the user's search. */
export const autocomplete = async ({ key, q, locale, limit, }) => (await rawAutocomplete({ key, q, locale, limit })).results;
/** Returns related search terms. */
export const related = async ({ key, q, locale, limit, }) => (await rawSearchSuggestions({ key, q, locale, limit })).results;
/** Returns trending search terms. */
export const trendingTerms = async ({ key, locale, limit, }) => (await rawTrendingTerms({ key, locale, limit })).results;
/** Returns Tenor GIF categories. */
export const categories = async ({ key, type, locale, safety, }) => (await rawCategories({ key, type, locale, contentfilter: safety })).tags.map(({ searchterm, image }) => ({ term: searchterm, gif: image }));