diff --git a/composables/api.ts b/composables/api.ts index 48633e5..1b467a4 100644 --- a/composables/api.ts +++ b/composables/api.ts @@ -1,24 +1,32 @@ import type { ChannelResponse, GuildMemberResponse, GuildResponse, MessageResponse, StatsResponse, UserResponse } from "~/types/interfaces"; +function ensureIsArray(list: any) { + if (Array.isArray(list)) { + return list + } else { + return [] + } +} + export const useApi = () => { - async function fetchGuilds(): Promise { - return await fetchWithApi(`/guilds`); + async function fetchGuilds(): Promise { + return ensureIsArray(await fetchWithApi(`/guilds`)); } async function fetchGuild(guildId: string): Promise { return await fetchWithApi(`/guilds/${guildId}`); } - async function fetchChannels(guildId: string): Promise { - return await fetchWithApi(`/guilds/${guildId}/channels`); + async function fetchChannels(guildId: string): Promise { + return ensureIsArray(await fetchWithApi(`/guilds/${guildId}/channels`)); } async function fetchChannel(channelId: string): Promise { return await fetchWithApi(`/channels/${channelId}`) } - async function fetchMembers(guildId: string): Promise { - return await fetchWithApi(`/guilds/${guildId}/members`); + async function fetchMembers(guildId: string): Promise { + return ensureIsArray(await fetchWithApi(`/guilds/${guildId}/members`)); } async function fetchMember(guildId: string, memberId: string): Promise { @@ -34,12 +42,7 @@ export const useApi = () => { } async function fetchFriends(): Promise { - const response = await fetchWithApi('/me/friends') - if (Array.isArray(response)) { - return response - } else { - return [] - } + return ensureIsArray(await fetchWithApi('/me/friends')); } async function addFriend(username: string): Promise {