{"version":3,"file":"unofficial-wanikani.umd.cjs","sources":["../src/v2/index.ts"],"sourcesContent":["import {\n  Assignment,\n  AssignmentsParams,\n  CreateReviewData,\n  ErrorResponse,\n  LevelProgression,\n  PaginationParams,\n  RequestOptions,\n  Review,\n  ReviewsParams,\n  Subject,\n  SubjectsParams,\n  WaniKaniCollection,\n} from \"./types\";\nimport fetch from 'cross-fetch';\n\n// request util functions\n\nconst getBaseUrl = () => `https://api.wanikani.com/v2/`;\n\nasync function apiRequest<T>({\n  endpoint,\n  method = \"GET\",\n  data,\n  apiKey,\n}: RequestOptions): Promise<T> {\n  const options: RequestInit = {\n    method,\n    headers: {\n      Authorization: `Bearer ${apiKey}`,\n      \"Content-Type\": \"application/json\",\n    },\n  };\n\n  if (data) {\n    options.body = JSON.stringify(data);\n  }\n\n  const response = await fetch(`${getBaseUrl()}${endpoint}`, options);\n\n  if (!response.ok) {\n    const errorResponse: ErrorResponse = await response.json();\n    throw new Error(`Error ${errorResponse.code}: ${errorResponse.error}`);\n  }\n\n  return response.json();\n}\n\nconst buildQueryString = (params: PaginationParams): string => {\n  const query = new URLSearchParams(params as any).toString();\n  return query ? `?${query}` : \"\";\n};\n\nasync function fetchResource<T>(\n  apiKey: string,\n  endpoint: string,\n  { params }: { params?: PaginationParams } = {}\n): Promise<T> {\n  const queryString = buildQueryString(params || {});\n  return apiRequest<T>({\n    apiKey,\n    endpoint: `${endpoint}${queryString}`,\n  });\n}\n\nasync function updateResource<T>(\n  apiKey: string,\n  endpoint: string,\n  data: any\n): Promise<T> {\n  return apiRequest<T>({\n    apiKey,\n    endpoint,\n    method: \"PUT\",\n    data,\n  });\n}\n\nasync function createResource<T>(\n  apiKey: string,\n  endpoint: string,\n  data: any\n): Promise<T> {\n  return apiRequest<T>({\n    apiKey,\n    endpoint,\n    method: \"POST\",\n    data,\n  });\n}\n\n// api helper functions\n\n/**\n * Fetches a collection of assignments from the WaniKani API.\n *\n * @param apiKey - The API key for the WaniKani API.\n * @param options - Optional object containing pagination parameters.\n * @returns A promise that resolves to a WaniKaniCollection of Assignments.\n */\nexport const fetchAssignments = (\n  apiKey: string,\n  options?: { params?: AssignmentsParams }\n) =>\n  fetchResource<WaniKaniCollection<Assignment>>(apiKey, \"assignments\", options);\n\n/**\n * Fetches a specific assignment from the WaniKani API.\n *\n * @param apiKey - The API key for the WaniKani API.\n * @param id - The ID of the assignment to fetch.\n * @param options - Optional object containing pagination parameters.\n * @returns A promise that resolves to a WaniKaniResource of an Assignment.\n */\nexport const fetchAssignment = (apiKey: string, id: number, options?: {}) =>\n  fetchResource<Assignment>(apiKey, `assignments/${id}`, options);\n\n/**\n * Updates a specific assignment in the WaniKani API.\n *\n * @param apiKey - The API key for the WaniKani API.\n * @param id - The ID of the assignment to update.\n * @param data - The data to update.\n * @param options - Optional object containing pagination parameters.\n * @returns A promise that resolves to a WaniKaniResource of an Assignment.\n */\nexport const updateAssignment = (\n  apiKey: string,\n  id: number,\n  data: Partial<Pick<Assignment, \"data\">>\n) => updateResource<Assignment>(apiKey, `assignments/${id}`, data);\n\n/**\n * Fetches a collection of level progressions from the WaniKani API.\n *\n * @param apiKey - The API key for the WaniKani API.\n * @param options - Optional object containing pagination parameters.\n * @returns A promise that resolves to a WaniKaniCollection of LevelProgressions.\n */\nexport const fetchLevelProgressions = (\n  apiKey: string,\n  options?: { params?: PaginationParams }\n) =>\n  fetchResource<WaniKaniCollection<LevelProgression>>(\n    apiKey,\n    \"level_progressions\",\n    options\n  );\n\n/**\n * Fetches a specific level progression from the WaniKani API.\n *\n * @param apiKey - The API key for the WaniKani API.\n * @param id - The ID of the level progression to fetch.\n * @param options - Optional object containing pagination parameters.\n * @returns A promise that resolves to a WaniKaniResource of a LevelProgression.\n */\nexport const fetchLevelProgression = (\n  apiKey: string,\n  id: number,\n  options?: {}\n) =>\n  fetchResource<LevelProgression>(apiKey, `level_progressions/${id}`, options);\n\n/**\n * Fetches a collection of reviews from the WaniKani API.\n *\n * @param apiKey - The API key for the WaniKani API.\n * @param options - Optional object containing pagination parameters.\n * @returns A promise that resolves to a WaniKaniCollection of Reviews.\n */\nexport const fetchReviews = (\n  apiKey: string,\n  options?: { params?: ReviewsParams }\n) => fetchResource<WaniKaniCollection<Review>>(apiKey, \"reviews\", options);\n\n/**\n * Fetches a specific review from the WaniKani API.\n *\n * @param apiKey - The API key for the WaniKani API.\n * @param id - The ID of the review to fetch.\n * @param options - Optional object containing pagination parameters.\n * @returns A promise that resolves to a WaniKaniResource of a Review.\n */\nexport const fetchReview = (apiKey: string, id: number, options?: {}) =>\n  fetchResource<Review>(apiKey, `reviews/${id}`, options);\n\n/**\n * Creates a new review in the WaniKani API.\n *\n * @param apiKey - The API key for the WaniKani API.\n * @param data - The data to create.\n * @returns A promise that resolves to a WaniKaniResource of a Review.\n */\nexport const createReview = (apiKey: string, data: CreateReviewData) =>\n  createResource<Review>(apiKey, \"reviews\", data);\n\n/**\n * Fetches a collection of subjects from the WaniKani API.\n *\n * @param apiKey - The API key for the WaniKani API.\n * @param options - Optional object containing pagination parameters.\n * @returns A promise that resolves to a WaniKaniCollection of Subjects.\n */\nexport const fetchSubjects = (\n  apiKey: string,\n  options?: { params?: SubjectsParams }\n) => fetchResource<WaniKaniCollection<Subject>>(apiKey, \"subjects\", options);\n\n/**\n * Fetches a specific subject from the WaniKani API.\n *\n * @param apiKey - The API key for the WaniKani API.\n * @param id - The ID of the subject to fetch.\n * @param options - Optional object containing pagination parameters.\n * @returns A promise that resolves to a WaniKaniResource of a Subject.\n */\nexport const fetchSubject = (apiKey: string, id: number, options?: {}) =>\n  fetchResource<Subject>(apiKey, `subjects/${id}`, options);\n\n/**\n * Creates a WaniKani client instance.\n *\n * @param apiKey - The API key for accessing the WaniKani API.\n * @returns An object with methods for fetching various data from the WaniKani API.\n */\nexport default function WaniKani(apiKey: string) {\n  return {\n    /**\n     * Fetches a collection of assignments from the WaniKani API.\n     *\n     * @param options - Optional object containing pagination parameters.\n     * @returns A promise that resolves to a WaniKaniCollection of Assignments.\n     */\n    fetchAssignments: (options?: { params?: AssignmentsParams }) =>\n      fetchAssignments(apiKey, options),\n\n    /**\n     * Fetches a specific assignment from the WaniKani API.\n     *\n     * @param id - The ID of the assignment to fetch.\n     * @param options - Optional object containing pagination parameters.\n     * @returns A promise that resolves to a WaniKaniResource of an Assignment.\n     */\n    fetchAssignment: (id: number, options?: {}) =>\n      fetchAssignment(apiKey, id, options),\n\n    /**\n     * Updates a specific assignment in the WaniKani API.\n     *\n     * @param id - The ID of the assignment to update.\n     * @param data - The data to update.\n     * @returns A promise that resolves to a WaniKaniResource of an Assignment.\n     */\n    updateAssignment: (id: number, data: Partial<Pick<Assignment, \"data\">>) =>\n      updateAssignment(apiKey, id, data),\n\n    /**\n     * Fetches a collection of level progressions from the WaniKani API.\n     *\n     * @param options - Optional object containing pagination parameters.\n     * @returns A promise that resolves to a WaniKaniCollection of LevelProgressions.\n     */\n    fetchLevelProgressions: (options?: { params?: PaginationParams }) =>\n      fetchLevelProgressions(apiKey, options),\n\n    /**\n     * Fetches a specific level progression from the WaniKani API.\n     *\n     * @param id - The ID of the level progression to fetch.\n     * @param options - Optional object containing pagination parameters.\n     * @returns A promise that resolves to a WaniKaniResource of a LevelProgression.\n     */\n    fetchLevelProgression: (id: number, options?: {}) =>\n      fetchLevelProgression(apiKey, id, options),\n\n    /**\n     * Fetches a collection of reviews from the WaniKani API.\n     *\n     * @param options - Optional object containing pagination parameters.\n     * @returns A promise that resolves to a WaniKaniCollection of Reviews.\n     */\n    fetchReviews: (options?: { params?: ReviewsParams }) =>\n      fetchReviews(apiKey, options),\n\n    /**\n     * Fetches a specific review from the WaniKani API.\n     *\n     * @param id - The ID of the review to fetch.\n     * @param options - Optional object containing pagination parameters.\n     * @returns A promise that resolves to a WaniKaniResource of a Review.\n     */\n    fetchReview: (id: number, options?: {}) => fetchReview(apiKey, id, options),\n\n    /**\n     * Creates a new review in the WaniKani API.\n     *\n     * @param data - The data to create.\n     * @returns A promise that resolves to a WaniKaniResource of a Review.\n     */\n    createReview: (data: CreateReviewData) => createReview(apiKey, data),\n\n    /**\n     * Fetches a collection of subjects from the WaniKani API.\n     *\n     * @param options - Optional object containing pagination parameters.\n     * @returns A promise that resolves to a WaniKaniCollection of Subjects.\n     */\n    fetchSubjects: (options?: { params?: SubjectsParams }) =>\n      fetchSubjects(apiKey, options),\n\n    /**\n     * Fetches a specific subject from the WaniKani API.\n     *\n     * @param id - The ID of the subject to fetch.\n     * @param options - Optional object containing pagination parameters.\n     * @returns A promise that resolves to a WaniKaniResource of a Subject.\n     */\n    fetchSubject: (id: number, options?: {}) =>\n      fetchSubject(apiKey, id, options),\n  };\n}\n"],"names":["getBaseUrl","apiRequest","endpoint","method","data","apiKey","options","response","fetch","errorResponse","buildQueryString","params","query","fetchResource","queryString","updateResource","createResource","fetchAssignments","fetchAssignment","id","updateAssignment","fetchLevelProgressions","fetchLevelProgression","fetchReviews","fetchReview","createReview","fetchSubjects","fetchSubject","WaniKani"],"mappings":"sRAkBA,MAAMA,EAAa,IAAM,+BAEzB,eAAeC,EAAc,CAC3B,SAAAC,EACA,OAAAC,EAAS,MACT,KAAAC,EACA,OAAAC,CACF,EAA+B,CAC7B,MAAMC,EAAuB,CAC3B,OAAAH,EACA,QAAS,CACP,cAAe,UAAUE,CAAM,GAC/B,eAAgB,kBAAA,CAEpB,EAEID,IACME,EAAA,KAAO,KAAK,UAAUF,CAAI,GAG9B,MAAAG,EAAW,MAAMC,EAAM,GAAGR,GAAY,GAAGE,CAAQ,GAAII,CAAO,EAE9D,GAAA,CAACC,EAAS,GAAI,CACV,MAAAE,EAA+B,MAAMF,EAAS,KAAK,EACnD,MAAA,IAAI,MAAM,SAASE,EAAc,IAAI,KAAKA,EAAc,KAAK,EAAE,CAAA,CAGvE,OAAOF,EAAS,KAAK,CACvB,CAEA,MAAMG,EAAoBC,GAAqC,CAC7D,MAAMC,EAAQ,IAAI,gBAAgBD,CAAa,EAAE,SAAS,EACnD,OAAAC,EAAQ,IAAIA,CAAK,GAAK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