diff --git a/lib/fetcher.ts b/lib/fetcher.ts new file mode 100644 index 0000000..1803a9c --- /dev/null +++ b/lib/fetcher.ts @@ -0,0 +1,12 @@ +import axios from 'axios'; + +const fetcher = axios.create({ + baseURL: process.env.NEXT_PUBLIC_API_URL, + headers: { + 'Content-Type': 'application/json', + Accept: 'application/json' + }, + insecureHTTPParser: true +}); + +export default fetcher; diff --git a/lib/leaderboard.ts b/lib/leaderboard.ts index 0f3d387..54d79f7 100644 --- a/lib/leaderboard.ts +++ b/lib/leaderboard.ts @@ -1,8 +1,10 @@ -import axios from 'axios'; +import fetcher from './fetcher'; -export const getScores = async (): Promise => { - const { data, status } = await axios.get(`${process.env.NEXT_PUBLIC_API_URL}/leaderboard`, { - insecureHTTPParser: true +export const getScores = async ({ token }: { token: string }): Promise => { + const { data, status } = await fetcher.get(`/leaderboard`, { + headers: { + Authorization: `Bearer ${token}` + } }); const scores = data; @@ -24,4 +26,5 @@ export type Score = { completions: number; pseudo: string; group: string; + avatar: string; }; diff --git a/lib/players.ts b/lib/players.ts new file mode 100644 index 0000000..afdc3be --- /dev/null +++ b/lib/players.ts @@ -0,0 +1,41 @@ +import fetcher from './fetcher'; + +export const getPlayer = async ({ + token, + username = '' +}: { + token: string; + username?: string; +}): Promise => { + const { data, status } = await fetcher.get(`/player/${username}`, { + headers: { + Authorization: `Bearer ${token}` + } + }); + + const player = data; + + if (status !== 200) { + throw new Error('Failed to fetch player'); + } + + if (!player) { + return null; + } + + return player as Player; +}; + +export type Player = { + email: string; + firstnames: string; + lastname: string; + description: string; + avatar: string; + group: string; + score: number; + tries: number; + completions: number; + pseudo: string; + badges: any[]; +}; diff --git a/lib/puzzles.ts b/lib/puzzles.ts index 5b374d7..1187721 100644 --- a/lib/puzzles.ts +++ b/lib/puzzles.ts @@ -1,8 +1,10 @@ -import axios from 'axios'; +import fetcher from './fetcher'; -export const getChapters = async (): Promise => { - const { data, status } = await axios.get(`${process.env.NEXT_PUBLIC_API_URL}/chapters`, { - insecureHTTPParser: true +export const getChapters = async ({ token }: { token: string }): Promise => { + const { data, status } = await fetcher.get(`/chapters`, { + headers: { + Authorization: `Bearer ${token}` + } }); let chapters = data; @@ -20,36 +22,44 @@ export const getChapters = async (): Promise => { return chapters as Chapter[]; }; -export const getPuzzlesByChapter = async (chapitre: number): Promise => { - const { data, status } = await axios.get( - `${process.env.NEXT_PUBLIC_API_URL}/chapter/${chapitre}`, - { insecureHTTPParser: true } - ); +export const getChapter = async ({ + token, + id +}: { + token: string; + id: number; +}): Promise => { + const { data, status } = await fetcher.get(`/chapter/${id}`, { + headers: { + Authorization: `Bearer ${token}` + } + }); - const { puzzles, name, id } = data; + const chapter = data; if (status !== 200) { throw new Error('Failed to fetch puzzles'); } - if (!puzzles) { + if (!chapter) { return null; } - return { - name, - id, - puzzles - }; + return chapter as Chapter; }; -export const getPuzzles = async (): Promise<{ chapters: Chapter[]; puzzles: Puzzle[] }> => { - const chapters = await getChapters(); - let puzzles: Puzzle[] = []; +export const getPuzzles = async ({ + token +}: { + token: string; +}): Promise<{ chapters: Chapter[]; puzzles: Puzzle[] }> => { + const chapters = await getChapters({ token }); + const puzzles: Puzzle[] = []; for (const chapter of chapters) { - const puzzlesByChapter = await getPuzzlesByChapter(chapter.id); - puzzles = [...puzzles, ...puzzlesByChapter!.puzzles]; + const puzzlesByChapter = await getChapter({ token, id: chapter.id }); + if (!puzzlesByChapter?.puzzles) continue; + puzzles.push(...puzzlesByChapter!.puzzles); } return { @@ -58,9 +68,11 @@ export const getPuzzles = async (): Promise<{ chapters: Chapter[]; puzzles: Puzz }; }; -export const getPuzzle = async (id: number): Promise => { - const { data, status } = await axios.get(`${process.env.NEXT_PUBLIC_API_URL}/puzzle/${id}`, { - insecureHTTPParser: true +export const getPuzzle = async ({ token, id }: { token: string; id: number }): Promise => { + const { data, status } = await fetcher.get(`/puzzle/${id}`, { + headers: { + Authorization: `Bearer ${token}` + } }); const puzzle = data; @@ -77,8 +89,8 @@ export const getPuzzle = async (id: number): Promise => { }; export type Puzzle = { - name: string; id: number; + name: string; content: string; };