UNPKG

svelte-tenor

Version:

Tenor GIF components, implemented in Svelte.

72 lines (71 loc) 2.74 kB
/** * A direct implementation of Tenor's API. * * @module */ /* ███████ ███ ██ ██████ ██████ ██████ ██ ███ ██ ████████ ███████ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ █████ ██ ██ ██ ██ ██ ██████ ██ ██ ██ ██ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███████ ██ ████ ██████ ██ ██████ ██ ██ ████ ██ ███████ */ /** Creates an endpoint. */ export const endpoint = (name) => async (options) => { const url = new URL(`https://g.tenor.com/v1/${name}`); for (const [name, value] of Object.entries(options)) if (value !== undefined) url.searchParams.set(name, value.toString()); const response = await fetch(url.toString()); if (response.status >= 400) { const error = (await response.json()); throw new Error(`${error.code}: ${error.error}`); } return response.json(); }; /** All endpoints, untyped. */ export const endpoints = { // GIF endpoints gifs: endpoint('gifs'), search: endpoint('search'), trending: endpoint('trending'), random: endpoint('random'), // Textual endpoints searchSuggestions: endpoint('search_suggestions'), autocomplete: endpoint('autocomplete'), trendingTerms: endpoint('trending_terms'), // Other endpoints categories: endpoint('categories'), registerShare: endpoint('registershare'), anonid: endpoint('anonid'), }; export async function search(options) { return endpoints.search(options); } export async function trending(options) { return endpoints.trending(options); } export async function categories(options) { return endpoints.categories(options); } export async function searchSuggestions(options) { return endpoints.searchSuggestions(options); } export async function autocomplete(options) { return endpoints.autocomplete(options); } export async function trendingTerms(options) { return endpoints.trendingTerms(options); } export async function registerShare(options) { return endpoints.registerShare(options); } export async function gifs(options) { return endpoints.gifs(options); } export async function random(options) { return endpoints.random(options); } export async function anonId(options) { return endpoints.anonid(options); }