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[]> => {
|
export const getScores = async ({ token }: { token: string }): Promise<Score[]> => {
|
||||||
const { data, status } = await axios.get(`${process.env.NEXT_PUBLIC_API_URL}/leaderboard`, {
|
const { data, status } = await fetcher.get(`/leaderboard`, {
|
||||||
insecureHTTPParser: true
|
headers: {
|
||||||
|
Authorization: `Bearer ${token}`
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const scores = data;
|
const scores = data;
|
||||||
|
@ -24,4 +26,5 @@ export type Score = {
|
||||||
completions: number;
|
completions: number;
|
||||||
pseudo: string;
|
pseudo: string;
|
||||||
group: 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[]> => {
|
export const getChapters = async ({ token }: { token: string }): Promise<Chapter[]> => {
|
||||||
const { data, status } = await axios.get(`${process.env.NEXT_PUBLIC_API_URL}/chapters`, {
|
const { data, status } = await fetcher.get(`/chapters`, {
|
||||||
insecureHTTPParser: true
|
headers: {
|
||||||
|
Authorization: `Bearer ${token}`
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let chapters = data;
|
let chapters = data;
|
||||||
|
@ -20,36 +22,44 @@ export const getChapters = async (): Promise<Chapter[]> => {
|
||||||
return chapters as Chapter[];
|
return chapters as Chapter[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getPuzzlesByChapter = async (chapitre: number): Promise<Chapter | null> => {
|
export const getChapter = async ({
|
||||||
const { data, status } = await axios.get(
|
token,
|
||||||
`${process.env.NEXT_PUBLIC_API_URL}/chapter/${chapitre}`,
|
id
|
||||||
{ insecureHTTPParser: true }
|
}: {
|
||||||
);
|
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) {
|
if (status !== 200) {
|
||||||
throw new Error('Failed to fetch puzzles');
|
throw new Error('Failed to fetch puzzles');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!puzzles) {
|
if (!chapter) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return chapter as Chapter;
|
||||||
name,
|
|
||||||
id,
|
|
||||||
puzzles
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getPuzzles = async (): Promise<{ chapters: Chapter[]; puzzles: Puzzle[] }> => {
|
export const getPuzzles = async ({
|
||||||
const chapters = await getChapters();
|
token
|
||||||
let puzzles: Puzzle[] = [];
|
}: {
|
||||||
|
token: string;
|
||||||
|
}): Promise<{ chapters: Chapter[]; puzzles: Puzzle[] }> => {
|
||||||
|
const chapters = await getChapters({ token });
|
||||||
|
const puzzles: Puzzle[] = [];
|
||||||
|
|
||||||
for (const chapter of chapters) {
|
for (const chapter of chapters) {
|
||||||
const puzzlesByChapter = await getPuzzlesByChapter(chapter.id);
|
const puzzlesByChapter = await getChapter({ token, id: chapter.id });
|
||||||
puzzles = [...puzzles, ...puzzlesByChapter!.puzzles];
|
if (!puzzlesByChapter?.puzzles) continue;
|
||||||
|
puzzles.push(...puzzlesByChapter!.puzzles);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -58,9 +68,11 @@ export const getPuzzles = async (): Promise<{ chapters: Chapter[]; puzzles: Puzz
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getPuzzle = async (id: number): Promise<Puzzle> => {
|
export const getPuzzle = async ({ token, id }: { token: string; id: number }): Promise<Puzzle> => {
|
||||||
const { data, status } = await axios.get(`${process.env.NEXT_PUBLIC_API_URL}/puzzle/${id}`, {
|
const { data, status } = await fetcher.get(`/puzzle/${id}`, {
|
||||||
insecureHTTPParser: true
|
headers: {
|
||||||
|
Authorization: `Bearer ${token}`
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const puzzle = data;
|
const puzzle = data;
|
||||||
|
@ -77,8 +89,8 @@ export const getPuzzle = async (id: number): Promise<Puzzle> => {
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Puzzle = {
|
export type Puzzle = {
|
||||||
name: string;
|
|
||||||
id: number;
|
id: number;
|
||||||
|
name: string;
|
||||||
content: string;
|
content: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue