diff --git a/package.json b/package.json index 372dd7b..894ad0a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ui", - "version": "6.3.63", + "version": "6.3.64", "license": "BUSL-1.1", "private": true, "packageManager": "pnpm@9.14.4", diff --git a/src/lib/modules/auth/client.ts b/src/lib/modules/auth/client.ts index f875c82..f5a4825 100644 --- a/src/lib/modules/auth/client.ts +++ b/src/lib/modules/auth/client.ts @@ -29,7 +29,7 @@ export default new class AuthAggregator { } kitsu () { - return !!kitsu.viewer.value?.id + return !!kitsu.id() } checkAuth () { @@ -38,14 +38,14 @@ export default new class AuthAggregator { id () { if (this.anilist()) return client.viewer.value!.viewer?.id - if (this.kitsu()) return kitsu.viewer.value?.id + if (this.kitsu()) return kitsu.id() return -1 } profile (): ResultOf | undefined { if (this.anilist()) return client.viewer.value?.viewer ?? undefined - if (this.kitsu()) return kitsu.viewer.value + if (this.kitsu()) return kitsu.profile() } mediaListEntry (media: Pick) { diff --git a/src/lib/modules/auth/kitsu.ts b/src/lib/modules/auth/kitsu.ts index dfe864a..0d0bce6 100644 --- a/src/lib/modules/auth/kitsu.ts +++ b/src/lib/modules/auth/kitsu.ts @@ -1,5 +1,6 @@ import { writable } from 'simple-store-svelte' -import { derived, readable } from 'svelte/store' +import { derived, get, readable } from 'svelte/store' +import { persisted } from 'svelte-persisted-store' import { toast } from 'svelte-sonner' import { client, type Media } from '../anilist' @@ -10,7 +11,7 @@ import type { Anime, Fav, KEntry, KitsuError, KitsuMediaStatus, Mapping, OAuth, import type { Entry, FullMediaList, UserFrag } from '../anilist/queries' import type { ResultOf, VariablesOf } from 'gql.tada' -import { arrayEqual, safeLocalStorage } from '$lib/utils' +import { arrayEqual } from '$lib/utils' const ENDPOINTS = { API_OAUTH: 'https://kitsu.app/api/oauth/token', @@ -39,8 +40,8 @@ const AL_TO_KITSU_STATUS: Record = { } export default new class KitsuSync { - auth = writable(safeLocalStorage('kitsuAuth')) - viewer = writable | undefined>(safeLocalStorage('kitsuViewer')) + auth = persisted('kitsuAuth', undefined) + viewer = persisted | undefined>('kitsuViewer', undefined) userlist = writable>>({}) // al id to al mapped kitsu entry favorites = writable>({}) // kitsu anime id to kitsu fav id kitsuToAL: Record = {} @@ -90,20 +91,16 @@ export default new class KitsuSync { constructor () { this.auth.subscribe((auth) => { - if (auth) localStorage.setItem('kitsuAuth', JSON.stringify(auth)) - this._user() - }) - - this.viewer.subscribe((viewer) => { - if (viewer) localStorage.setItem('kitsuViewer', JSON.stringify(viewer)) + if (auth) this._user() }) } // eslint-disable-next-line @typescript-eslint/no-explicit-any async _request (url: string | URL, method: string, body?: any): Promise { + const auth = get(this.auth) try { - if (this.auth.value) { - const expiresAt = (this.auth.value.created_at + this.auth.value.expires_in) * 1000 + if (auth) { + const expiresAt = (auth.created_at + auth.expires_in) * 1000 if (expiresAt < Date.now() - 1000 * 60 * 5) { // 5 minutes before expiry await this._refresh() @@ -113,7 +110,7 @@ export default new class KitsuSync { method, headers: { 'Content-Type': 'application/vnd.api+json', - Authorization: this.auth.value ? `Bearer ${this.auth.value.access_token}` : '' + Authorization: auth ? `Bearer ${auth.access_token}` : '' }, body: body ? JSON.stringify(body) : undefined }) @@ -167,17 +164,17 @@ export default new class KitsuSync { } async _refresh () { + const auth = get(this.auth) const data = await this._post( ENDPOINTS.API_OAUTH, { grant_type: 'refresh_token', - refresh_token: this.auth.value?.refresh_token + refresh_token: auth?.refresh_token } ) + if ('access_token' in data) { - this.auth.value = data - } else { - this.auth.value = undefined + this.auth.set(data) } } @@ -193,9 +190,7 @@ export default new class KitsuSync { ) if ('access_token' in data) { - this.auth.value = data - } else { - this.auth.value = undefined + this.auth.set(data) } } @@ -224,7 +219,7 @@ export default new class KitsuSync { const { id, attributes } = res.data[0] - this.viewer.value = { + this.viewer.set({ id: Number(id), name: attributes.name ?? '', about: attributes.about ?? '', @@ -238,7 +233,7 @@ export default new class KitsuSync { donatorBadge: null, options: null, statistics: null - } + }) } _kitsuEntryToAl (entry: Resource): ResultOf { @@ -312,12 +307,13 @@ export default new class KitsuSync { } async _makeFavourite (kitsuAnimeId: string) { + const viewer = get(this.viewer) const data = await this._post>( ENDPOINTS.API_FAVOURITES, { data: { relationships: { - user: { data: { type: 'users', id: this.viewer.value?.id.toString() ?? '' } }, + user: { data: { type: 'users', id: viewer?.id.toString() ?? '' } }, item: { data: { type: 'anime', id: kitsuAnimeId } } }, type: 'favorites' @@ -331,6 +327,7 @@ export default new class KitsuSync { } async _addEntry (id: string, attributes: Omit, alId: number) { + const viewer = get(this.viewer) const data = await this._post>( ENDPOINTS.API_USER_LIBRARY, { @@ -338,7 +335,7 @@ export default new class KitsuSync { attributes, relationships: { anime: { data: { id, type: 'anime' } }, - user: { data: { type: 'users', id: this.viewer.value?.id.toString() ?? '' } } + user: { data: { type: 'users', id: viewer?.id.toString() ?? '' } } }, type: 'library-entries' } @@ -387,11 +384,11 @@ export default new class KitsuSync { }) id () { - return this.viewer.value?.id ?? -1 + return get(this.viewer)?.id } profile (): ResultOf | undefined { - return this.viewer.value + return get(this.viewer) } // QUERIES/MUTATIONS