-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move and rename fetchers, options
- Loading branch information
1 parent
a832ed5
commit 038ffc8
Showing
14 changed files
with
108 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +0,0 @@ | ||
import { getTokenClientSide } from "@/lib/getCookieClientSide"; | ||
import { tokenSchema } from "@/lib/schemas/user"; | ||
import { isServer } from "@tanstack/react-query"; | ||
import { jwtDecode } from "jwt-decode"; | ||
import { getTokenServerSide } from "./auth"; | ||
|
||
export const tryGetToken = async () => { | ||
const token = isServer ? await getTokenServerSide() : getTokenClientSide(); | ||
|
||
if (!token) { | ||
throw new Error( | ||
"Failed to retrieve JWT token on auth request initialization.", | ||
); | ||
} | ||
return token; | ||
}; | ||
|
||
export const tryGetParsedToken = async () => { | ||
const token = await tryGetToken(); | ||
return tokenSchema.parse(jwtDecode(token)); | ||
}; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
"use server"; | ||
import { fetchGuildApiData } from "@/lib/fetchGuildApi"; | ||
import type { Guild } from "@/lib/schemas/guild"; | ||
import type { PaginatedResponse } from "@/lib/types"; | ||
import type { Schemas } from "@guildxyz/types"; | ||
import { PAGE_SIZE } from "./constants"; | ||
|
||
export const getGuildSearch = async ({ | ||
pageParam, | ||
search, | ||
}: { pageParam: number; search: string }) => { | ||
return fetchGuildApiData<PaginatedResponse<Guild>>( | ||
return fetchGuildApiData<PaginatedResponse<Schemas["GuildFull"]>>( | ||
`guild/search?page=${pageParam}&pageSize=${PAGE_SIZE}&search=${search}`, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { fetchGuildApiData } from "@/lib/fetchGuildApi"; | ||
import { resolveIdLikeRequest } from "@/lib/resolveIdLikeRequest"; | ||
import { tryGetParsedToken } from "@/lib/token"; | ||
import type { ErrorLike, WithIdLike } from "@/lib/types"; | ||
import type { Schemas } from "@guildxyz/types"; | ||
|
||
export const fetchGuildLeave = async ({ guildId }: { guildId: string }) => { | ||
return fetchGuildApiData(`guild/${guildId}/leave`, { | ||
method: "POST", | ||
}); | ||
}; | ||
|
||
export const fetchGuild = async ({ idLike }: WithIdLike) => { | ||
return fetchGuildApiData<Schemas["GuildFull"]>( | ||
`guild/${resolveIdLikeRequest(idLike)}`, | ||
); | ||
}; | ||
|
||
export const fetchEntity = async <Data = object, Error = ErrorLike>({ | ||
idLike, | ||
entity, | ||
responseInit, | ||
}: { | ||
entity: string; | ||
idLike: string; | ||
responseInit?: Parameters<typeof fetch>[1]; | ||
}) => { | ||
const pathname = `${entity}/${resolveIdLikeRequest(idLike)}`; | ||
return fetchGuildApiData<Data, Error>(pathname, responseInit); | ||
}; | ||
|
||
export const fetchUser = async () => { | ||
const { userId } = await tryGetParsedToken(); | ||
return fetchEntity<Schemas["UserFull"]>({ | ||
entity: "user", | ||
idLike: userId, | ||
}); | ||
}; | ||
|
||
export const fetchPages = async ({ guildId }: { guildId: string }) => { | ||
const guild = await fetchGuild({ idLike: guildId }); | ||
return fetchGuildApiData<Schemas["PageFull"][]>("page/batch", { | ||
method: "POST", | ||
body: JSON.stringify({ ids: guild.pages?.map((p) => p.pageId!) ?? [] }), | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import z from "zod"; | ||
|
||
export const resolveIdLikeRequest = (idLike: string) => { | ||
const isId = z.string().uuid().safeParse(idLike).success; | ||
return `${isId ? "id" : "urlName"}/${idLike}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { getTokenServerSide } from "@/actions/auth"; | ||
import { getTokenClientSide } from "@/lib/getCookieClientSide"; | ||
import { tokenSchema } from "@/lib/schemas/user"; | ||
import { isServer } from "@tanstack/react-query"; | ||
import { jwtDecode } from "jwt-decode"; | ||
|
||
export const tryGetToken = async () => { | ||
const token = isServer ? await getTokenServerSide() : getTokenClientSide(); | ||
|
||
if (!token) { | ||
throw new Error( | ||
"Failed to retrieve JWT token on auth request initialization.", | ||
); | ||
} | ||
return token; | ||
}; | ||
|
||
export const tryGetParsedToken = async () => { | ||
const token = await tryGetToken(); | ||
return tokenSchema.parse(jwtDecode(token)); | ||
}; |
Empty file.