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 1/5] 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 { From ea1f032ffc64d838bfaf4e80bb0c435c0e896acb Mon Sep 17 00:00:00 2001 From: JustTemmie <47639983+JustTemmie@users.noreply.github.com> Date: Wed, 16 Jul 2025 10:47:03 +0200 Subject: [PATCH 2/5] feat: implemend fetchMyGuilds() api --- composables/api.ts | 5 +++++ layouts/client.vue | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/composables/api.ts b/composables/api.ts index 1b467a4..220e98f 100644 --- a/composables/api.ts +++ b/composables/api.ts @@ -17,6 +17,10 @@ export const useApi = () => { return await fetchWithApi(`/guilds/${guildId}`); } + async function fetchMyGuilds(): Promise { + return ensureIsArray(await fetchWithApi(`/me/guilds`)); + } + async function fetchChannels(guildId: string): Promise { return ensureIsArray(await fetchWithApi(`/guilds/${guildId}/channels`)); } @@ -85,6 +89,7 @@ export const useApi = () => { return { fetchGuilds, fetchGuild, + fetchMyGuilds, fetchChannels, fetchChannel, fetchMembers, diff --git a/layouts/client.vue b/layouts/client.vue index 46bdf89..26a5ac9 100644 --- a/layouts/client.vue +++ b/layouts/client.vue @@ -95,7 +95,7 @@ const options = [ if (invite.length == 6) { try { const joinedGuild = await api.joinGuild(invite); - guilds?.push(joinedGuild); + guilds.push(joinedGuild); return await navigateTo(`/servers/${joinedGuild.uuid}`); } catch (error) { alert(`Couldn't use invite: ${error}`); @@ -151,7 +151,7 @@ const options = [ } ]; -const guilds: GuildResponse[] | undefined = await fetchWithApi("/me/guilds"); +const guilds = await api.fetchMyGuilds(); function createDropdown() { const dropdown = h(GuildDropdown, { options }); From 100d5e80eb4a375ba1941b270d2d266149db11d4 Mon Sep 17 00:00:00 2001 From: JustTemmie <47639983+JustTemmie@users.noreply.github.com> Date: Wed, 16 Jul 2025 10:47:13 +0200 Subject: [PATCH 3/5] style: remove unused import --- components/User/UserPopup.vue | 2 -- 1 file changed, 2 deletions(-) diff --git a/components/User/UserPopup.vue b/components/User/UserPopup.vue index 86b5a48..6674c9e 100644 --- a/components/User/UserPopup.vue +++ b/components/User/UserPopup.vue @@ -21,8 +21,6 @@