Skip to content

Instantly share code, notes, and snippets.

@miraris
Last active March 25, 2022 08:41
Show Gist options
  • Save miraris/5c9e89406633132fd3ca57172070086f to your computer and use it in GitHub Desktop.
Save miraris/5c9e89406633132fd3ca57172070086f to your computer and use it in GitHub Desktop.
smol jikan request helper
// const fetch = require('node-fetch'); /* if in node.js env */
const buildParams = ps =>
Object.keys(ps)
.map(k => `${k}=${ps[k]}`)
.join("&");
const buildURL = (args, params) =>
"https://api.jikan.moe/v3" +
`/${args.filter(Boolean).join("/")}` +
`?${buildParams(params)}`;
const requestJikan = (args, params = {}) =>
fetch(buildURL(args, params)).then(res => res.json());
// module.exports = requestJikan;
// import fetch from 'node-fetch';
const buildParams = (ps: any) =>
Object.keys(ps)
.map((k) => `${k}=${ps[k]}`)
.join("&");
const buildUrl = (args: Array<string | number>, params: any) =>
"https://api.jikan.moe/v3" +
`/${args.filter(Boolean).join("/")}` +
`?${buildParams(params)}`;
const requestJikan = (args: Array<string | number>, params = {}) =>
fetch(buildUrl(args, params), { redirect: "follow" }).then((res: any) => res.json());
export = { request: requestJikan };
// usage
// import jikanWrapper from "./jikan";
// jikanWrapper.request(["anime", 1]).then((res: any) => console.log(res));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment