Modification du composable useTMDB en ajoutant et export de la fonction fetchPop…
…ularMovies, suppresion des export des const apiUrl et apiKey.
Showing
1 changed file
with
20 additions
and
2 deletions
@@ -2,9 +2,27 @@ import type { RuntimeConfig } from "nuxt/schema"; | @@ -2,9 +2,27 @@ import type { RuntimeConfig } from "nuxt/schema"; | ||
2 | 2 | ||
3 | export const useTMDB = function() { | 3 | export const useTMDB = function() { |
4 | const runtimeconfig: RuntimeConfig = useRuntimeConfig(); | 4 | const runtimeconfig: RuntimeConfig = useRuntimeConfig(); |
5 | - | ||
6 | const apiUrl = runtimeconfig.public.apiTMDBUrl; | 5 | const apiUrl = runtimeconfig.public.apiTMDBUrl; |
7 | const apiKey = runtimeconfig.public.apiTMDBSecret; | 6 | const apiKey = runtimeconfig.public.apiTMDBSecret; |
8 | 7 | ||
9 | - return {apiUrl, apiKey} | 8 | + /** |
9 | + * Fetch popular movies. | ||
10 | + * @param page | ||
11 | + */ | ||
12 | + const fetchPopularMovies = async (page: number) => { | ||
13 | + try { | ||
14 | + const response = await fetch( | ||
15 | + `${apiUrl}/movie/popular?api_key=${apiKey}&language=fr-FR&page=${page}`, | ||
16 | + ); | ||
17 | + if (!response.ok) { | ||
18 | + console.error("An error occured when fetching popular movies:"); | ||
19 | + } else { | ||
20 | + return await response.json(); | ||
21 | + } | ||
22 | + } catch (error) { | ||
23 | + console.error("Error fetching popular movies:", error); | ||
24 | + } | ||
25 | + }; | ||
26 | + | ||
27 | + return { fetchPopularMovies } | ||
10 | } | 28 | } |
-
Please register or login to post a comment