Created fetcher and added token to requests
This commit is contained in:
parent
0c55d7cf8a
commit
c2d417cbc9
4 changed files with 97 additions and 29 deletions
12
lib/fetcher.ts
Normal file
12
lib/fetcher.ts
Normal file
|
@ -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;
|
|
@ -1,8 +1,10 @@
|
|||
import axios from 'axios';
|
||||
import fetcher from './fetcher';
|
||||
|
||||
export const getScores = async (): Promise<Score[]> => {
|
||||
const { data, status } = await axios.get(`${process.env.NEXT_PUBLIC_API_URL}/leaderboard`, {
|
||||
insecureHTTPParser: true
|
||||
export const getScores = async ({ token }: { token: string }): Promise<Score[]> => {
|
||||
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;
|
||||
};
|
||||
|
|
41
lib/players.ts
Normal file
41
lib/players.ts
Normal file
|
@ -0,0 +1,41 @@
|
|||
import fetcher from './fetcher';
|
||||
|
||||
export const getPlayer = async ({
|
||||
token,
|
||||
username = ''
|
||||
}: {
|
||||
token: string;
|
||||
username?: string;
|
||||
}): Promise<Player | null> => {
|
||||
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[];
|
||||
};
|
|
@ -1,8 +1,10 @@
|
|||
import axios from 'axios';
|
||||
import fetcher from './fetcher';
|
||||
|
||||
export const getChapters = async (): Promise<Chapter[]> => {
|
||||
const { data, status } = await axios.get(`${process.env.NEXT_PUBLIC_API_URL}/chapters`, {
|
||||
insecureHTTPParser: true
|
||||
export const getChapters = async ({ token }: { token: string }): Promise<Chapter[]> => {
|
||||
const { data, status } = await fetcher.get(`/chapters`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
let chapters = data;
|
||||
|
@ -20,36 +22,44 @@ export const getChapters = async (): Promise<Chapter[]> => {
|
|||
return chapters as Chapter[];
|
||||
};
|
||||
|
||||
export const getPuzzlesByChapter = async (chapitre: number): Promise<Chapter | null> => {
|
||||
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<Chapter | null> => {
|
||||
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<Puzzle> => {
|
||||
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<Puzzle> => {
|
||||
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<Puzzle> => {
|
|||
};
|
||||
|
||||
export type Puzzle = {
|
||||
name: string;
|
||||
id: number;
|
||||
name: string;
|
||||
content: string;
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue