purrbot-api
Version:
The official API wrapper for the Purrbot.site API 100% API Coverage 100% Type-Script coverage
50 lines (44 loc) • 1.65 kB
text/typescript
import { Categories, GetAPIResponse, ListAPIResponse } from './types.js';
export const baseURL = 'https://api.purrbot.site/v2';
/**
* @param imgType {string} `gif` or `img`
* @param nsfw {boolean}
* @param category {string} one of SFWCategories<T> or NSFWCategories<T> - see https://docs.purrbot.site/api/
* @returns {Promise<string>}
* @example
* import PurrBot from 'purrbot-api';
* PurrBot.get('img', true, 'neko').then(console.log);
* // => https://purrbot.site/img/xxx.jpg
*/
export const get = async <T extends keyof Categories<N>, N extends boolean>(
imgType: T,
nsfw: N,
category: Categories<N>[T],
): Promise<string> => {
const res = (await fetch(
`${baseURL}/img/${nsfw ? 'nsfw' : 'sfw'}/${category}/${String(imgType)}`,
).then((r) => r.json())) as GetAPIResponse;
if (res.error) throw new Error(res.message);
return res.link;
};
/**
* @param imgType {string} `gif` or `img`
* @param nsfw {boolean}
* @param category {string} one of SFWCategories<T> or NSFWCategories<T> - see https://docs.purrbot.site/api/
* @returns {Promise<string[]>}
* @example
* import PurrBot from 'purrbot-api';
* PurrBot.list('img', true, 'neko').then(console.log);
* // => ['https://purrbot.site/img/xxx.jpg', 'https://purrbot.site/img/xxx.jpg', ...]
*/
export const list = async <T extends keyof Categories<N>, N extends boolean>(
imgType: T,
nsfw: N,
category: Categories<N>[T],
): Promise<string[]> => {
const res = (await fetch(
`${baseURL}/list/${nsfw ? 'nsfw' : 'sfw'}/${category}/${String(imgType)}`,
).then((r) => r.json())) as ListAPIResponse;
if (res.error) throw new Error(res.message);
return res.links;
};