character.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // noinspection JSCheckFunctionSignatures
  2. export const characterQueryKeys = {
  3. root: ["characterList"],
  4. options: () => [...characterQueryKeys.root, "options"],
  5. byId: (id) => ["character", id]
  6. }
  7. export const characterListQuery = defineQueryOptions({
  8. key: characterQueryKeys.root,
  9. query: () => useEmit("character:list")
  10. })
  11. export const characterOptionsQuery = defineQueryOptions({
  12. key: characterQueryKeys.options,
  13. query: () => useEmit("character:options")
  14. })
  15. export const characterByIdQuery = defineQueryOptions((id) => ({
  16. key: characterQueryKeys.byId(id),
  17. query: async () => {
  18. const { data: characterList } = await usePrefetchCharacterList()
  19. if (_isNil(characterList) || _isEmpty(characterList)) {
  20. throw new QueryError("error fetching characterList", { key: characterQueryKeys.byId(id) })
  21. }
  22. const character = _find(characterList, { id })
  23. if (_isNil(character)) {
  24. throw new QueryError(`character with id ${id} not found`, { key: characterQueryKeys.byId(id) })
  25. }
  26. return character
  27. }
  28. }))