unofficial-wanikani
Version:
An unofficial JavaScript client for the WaniKani API.
139 lines (138 loc) • 4.45 kB
JavaScript
import f from "cross-fetch";
const h = () => "https://api.wanikani.com/v2/";
async function c({
endpoint: t,
method: e = "GET",
data: s,
apiKey: o
}) {
const i = {
method: e,
headers: {
Authorization: `Bearer ${o}`,
"Content-Type": "application/json"
}
};
s && (i.body = JSON.stringify(s));
const r = await f(`${h()}${t}`, i);
if (!r.ok) {
const u = await r.json();
throw new Error(`Error ${u.code}: ${u.error}`);
}
return r.json();
}
const a = (t) => {
const e = new URLSearchParams(t).toString();
return e ? `?${e}` : "";
};
async function n(t, e, { params: s } = {}) {
const o = a(s || {});
return c({
apiKey: t,
endpoint: `${e}${o}`
});
}
async function g(t, e, s) {
return c({
apiKey: t,
endpoint: e,
method: "PUT",
data: s
});
}
async function v(t, e, s) {
return c({
apiKey: t,
endpoint: e,
method: "POST",
data: s
});
}
const m = (t, e) => n(t, "assignments", e), w = (t, e, s) => n(t, `assignments/${e}`, s), $ = (t, e, s) => g(t, `assignments/${e}`, s), l = (t, e) => n(
t,
"level_progressions",
e
), R = (t, e, s) => n(t, `level_progressions/${e}`, s), d = (t, e) => n(t, "reviews", e), S = (t, e, s) => n(t, `reviews/${e}`, s), j = (t, e) => v(t, "reviews", e), b = (t, e) => n(t, "subjects", e), p = (t, e, s) => n(t, `subjects/${e}`, s);
function P(t) {
return {
/**
* Fetches a collection of assignments from the WaniKani API.
*
* @param options - Optional object containing pagination parameters.
* @returns A promise that resolves to a WaniKaniCollection of Assignments.
*/
fetchAssignments: (e) => m(t, e),
/**
* Fetches a specific assignment from the WaniKani API.
*
* @param id - The ID of the assignment to fetch.
* @param options - Optional object containing pagination parameters.
* @returns A promise that resolves to a WaniKaniResource of an Assignment.
*/
fetchAssignment: (e, s) => w(t, e, s),
/**
* Updates a specific assignment in the WaniKani API.
*
* @param id - The ID of the assignment to update.
* @param data - The data to update.
* @returns A promise that resolves to a WaniKaniResource of an Assignment.
*/
updateAssignment: (e, s) => $(t, e, s),
/**
* Fetches a collection of level progressions from the WaniKani API.
*
* @param options - Optional object containing pagination parameters.
* @returns A promise that resolves to a WaniKaniCollection of LevelProgressions.
*/
fetchLevelProgressions: (e) => l(t, e),
/**
* Fetches a specific level progression from the WaniKani API.
*
* @param id - The ID of the level progression to fetch.
* @param options - Optional object containing pagination parameters.
* @returns A promise that resolves to a WaniKaniResource of a LevelProgression.
*/
fetchLevelProgression: (e, s) => R(t, e, s),
/**
* Fetches a collection of reviews from the WaniKani API.
*
* @param options - Optional object containing pagination parameters.
* @returns A promise that resolves to a WaniKaniCollection of Reviews.
*/
fetchReviews: (e) => d(t, e),
/**
* Fetches a specific review from the WaniKani API.
*
* @param id - The ID of the review to fetch.
* @param options - Optional object containing pagination parameters.
* @returns A promise that resolves to a WaniKaniResource of a Review.
*/
fetchReview: (e, s) => S(t, e, s),
/**
* Creates a new review in the WaniKani API.
*
* @param data - The data to create.
* @returns A promise that resolves to a WaniKaniResource of a Review.
*/
createReview: (e) => j(t, e),
/**
* Fetches a collection of subjects from the WaniKani API.
*
* @param options - Optional object containing pagination parameters.
* @returns A promise that resolves to a WaniKaniCollection of Subjects.
*/
fetchSubjects: (e) => b(t, e),
/**
* Fetches a specific subject from the WaniKani API.
*
* @param id - The ID of the subject to fetch.
* @param options - Optional object containing pagination parameters.
* @returns A promise that resolves to a WaniKaniResource of a Subject.
*/
fetchSubject: (e, s) => p(t, e, s)
};
}
export {
P as default
};
//# sourceMappingURL=unofficial-wanikani.js.map