Last active
March 25, 2022 08:41
-
-
Save miraris/5c9e89406633132fd3ca57172070086f to your computer and use it in GitHub Desktop.
smol jikan request helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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