From 56ccd61107b480d5668179fdc79907cb7870f337 Mon Sep 17 00:00:00 2001 From: JustTemmie <47639983+JustTemmie@users.noreply.github.com> Date: Wed, 16 Jul 2025 10:44:23 +0200 Subject: [PATCH] style: ensure all api requests that return an array to return an empty array instead of undefined --- composables/api.ts | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) 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 {