UNPKG

unofficial-wanikani

Version:

An unofficial JavaScript client for the WaniKani API.

3 lines (2 loc) 1.67 kB
(function(o,c){typeof exports=="object"&&typeof module<"u"?module.exports=c(require("cross-fetch")):typeof define=="function"&&define.amd?define(["cross-fetch"],c):(o=typeof globalThis<"u"?globalThis:o||self,o.UnofficialWaniKani=c(o.crossFetch))})(this,function(o){"use strict";const c=()=>"https://api.wanikani.com/v2/";async function r({endpoint:s,method:e="GET",data:t,apiKey:i}){const u={method:e,headers:{Authorization:`Bearer ${i}`,"Content-Type":"application/json"}};t&&(u.body=JSON.stringify(t));const f=await o(`${c()}${s}`,u);if(!f.ok){const h=await f.json();throw new Error(`Error ${h.code}: ${h.error}`)}return f.json()}const a=s=>{const e=new URLSearchParams(s).toString();return e?`?${e}`:""};async function n(s,e,{params:t}={}){const i=a(t||{});return r({apiKey:s,endpoint:`${e}${i}`})}async function d(s,e,t){return r({apiKey:s,endpoint:e,method:"PUT",data:t})}async function g(s,e,t){return r({apiKey:s,endpoint:e,method:"POST",data:t})}const m=(s,e)=>n(s,"assignments",e),v=(s,e,t)=>n(s,`assignments/${e}`,t),w=(s,e,t)=>d(s,`assignments/${e}`,t),l=(s,e)=>n(s,"level_progressions",e),$=(s,e,t)=>n(s,`level_progressions/${e}`,t),p=(s,e)=>n(s,"reviews",e),R=(s,e,t)=>n(s,`reviews/${e}`,t),j=(s,e)=>g(s,"reviews",e),S=(s,e)=>n(s,"subjects",e),b=(s,e,t)=>n(s,`subjects/${e}`,t);function A(s){return{fetchAssignments:e=>m(s,e),fetchAssignment:(e,t)=>v(s,e,t),updateAssignment:(e,t)=>w(s,e,t),fetchLevelProgressions:e=>l(s,e),fetchLevelProgression:(e,t)=>$(s,e,t),fetchReviews:e=>p(s,e),fetchReview:(e,t)=>R(s,e,t),createReview:e=>j(s,e),fetchSubjects:e=>S(s,e),fetchSubject:(e,t)=>b(s,e,t)}}return A}); //# sourceMappingURL=unofficial-wanikani.umd.cjs.map