api: update entity adapter and queries

This commit is contained in:
Justin Edmund 2025-11-30 20:06:40 -08:00
parent db5bfe9f7f
commit c3ed9b2885
2 changed files with 64 additions and 3 deletions

View file

@ -110,6 +110,32 @@ export interface Character {
}>
}
/**
* Weapon key data for customizing weapons
*/
export interface WeaponKey {
id: string
granblue_id: number
name: {
en: string
ja: string
}
slug: string
series: number[]
slot: number
group: number
order: number
}
/**
* Query parameters for fetching weapon keys
*/
export interface WeaponKeyQueryParams {
series?: number
slot?: number
group?: number
}
/**
* Canonical summon data from the game
*/
@ -232,10 +258,28 @@ export class EntityAdapter extends BaseAdapter {
return Promise.all(promises)
}
/**
* Gets weapon keys with optional filtering
*/
async getWeaponKeys(params?: WeaponKeyQueryParams): Promise<WeaponKey[]> {
const searchParams = new URLSearchParams()
if (params?.series !== undefined) searchParams.set('series', String(params.series))
if (params?.slot !== undefined) searchParams.set('slot', String(params.slot))
if (params?.group !== undefined) searchParams.set('group', String(params.group))
const queryString = searchParams.toString()
const url = queryString ? `/weapon_keys?${queryString}` : '/weapon_keys'
return this.request<WeaponKey[]>(url, {
method: 'GET',
cacheTTL: 3600000 // Cache for 1 hour - weapon keys rarely change
})
}
/**
* Clears entity cache
*/
clearEntityCache(type?: 'weapons' | 'characters' | 'summons') {
clearEntityCache(type?: 'weapons' | 'characters' | 'summons' | 'weapon_keys') {
if (type) {
this.clearCache(`/${type}`)
} else {
@ -243,6 +287,7 @@ export class EntityAdapter extends BaseAdapter {
this.clearCache('/weapons')
this.clearCache('/characters')
this.clearCache('/summons')
this.clearCache('/weapon_keys')
}
}
}

View file

@ -8,7 +8,7 @@
*/
import { queryOptions } from '@tanstack/svelte-query'
import { entityAdapter } from '$lib/api/adapters/entity.adapter'
import { entityAdapter, type WeaponKeyQueryParams } from '$lib/api/adapters/entity.adapter'
/**
* Entity query options factory
@ -72,6 +72,20 @@ export const entityQueries = {
enabled: !!id,
staleTime: 1000 * 60 * 60, // 1 hour - canonical data rarely changes
gcTime: 1000 * 60 * 60 * 24 // 24 hours
}),
/**
* Weapon keys query options with optional filtering
*
* @param params - Optional filter parameters (series, slot, group)
* @returns Query options for fetching weapon keys
*/
weaponKeys: (params?: WeaponKeyQueryParams) =>
queryOptions({
queryKey: ['weaponKeys', params?.series, params?.slot, params?.group] as const,
queryFn: () => entityAdapter.getWeaponKeys(params),
staleTime: 1000 * 60 * 60, // 1 hour - weapon keys rarely change
gcTime: 1000 * 60 * 60 * 24 // 24 hours
})
}
@ -98,5 +112,7 @@ export const entityKeys = {
characters: () => ['character'] as const,
character: (id: string) => [...entityKeys.characters(), id] as const,
summons: () => ['summon'] as const,
summon: (id: string) => [...entityKeys.summons(), id] as const
summon: (id: string) => [...entityKeys.summons(), id] as const,
weaponKeys: (params?: WeaponKeyQueryParams) =>
['weaponKeys', params?.series, params?.slot, params?.group] as const
}