feat: mock when running locally
This commit is contained in:
parent
12f4929dd3
commit
6757a60134
22 changed files with 1342 additions and 300 deletions
|
@ -1,23 +1,12 @@
|
|||
import EleventyFetch from '@11ty/eleventy-fetch'
|
||||
import MoviesMock from './json/mocks/movies.js'
|
||||
|
||||
export default async function () {
|
||||
const TV_KEY = process.env.API_KEY_TRAKT
|
||||
const MOVIEDB_KEY = process.env.API_KEY_MOVIEDB
|
||||
const url = 'https://api.trakt.tv/users/cdransf/history/movies?page=1&limit=6&extended=full'
|
||||
const res = EleventyFetch(url, {
|
||||
duration: '1h',
|
||||
type: 'json',
|
||||
fetchOptions: {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'trakt-api-version': 2,
|
||||
'trakt-api-key': TV_KEY,
|
||||
},
|
||||
},
|
||||
}).catch()
|
||||
const data = await res
|
||||
const movies = data.map((item) => {
|
||||
return {
|
||||
const formatMovieData = (movies) => movies.map((item) => {
|
||||
const movie = {
|
||||
title: item['movie']['title'],
|
||||
dateAdded: item['watched_at'],
|
||||
url: `https://trakt.tv/movies/${item['movie']['ids']['slug']}`,
|
||||
|
@ -26,19 +15,38 @@ export default async function () {
|
|||
description: `${item['movie']['overview']}<br/><br/>`,
|
||||
type: 'movie',
|
||||
}
|
||||
if (process.env.ELEVENTY_PRODUCTION === 'false') movie.image = 'https://cd-movies.b-cdn.net'
|
||||
return movie;
|
||||
})
|
||||
|
||||
for (const movie of movies) {
|
||||
const tmdbId = movie['tmdbId']
|
||||
const tmdbUrl = `https://api.themoviedb.org/3/movie/${tmdbId}?api_key=${MOVIEDB_KEY}`
|
||||
const tmdbRes = EleventyFetch(tmdbUrl, {
|
||||
if (process.env.ELEVENTY_PRODUCTION) {
|
||||
const res = EleventyFetch(url, {
|
||||
duration: '1h',
|
||||
type: 'json',
|
||||
})
|
||||
const tmdbData = await tmdbRes
|
||||
const posterPath = tmdbData['poster_path']
|
||||
movie.image = `https://cd-movies.b-cdn.net/t/p/w500${posterPath}`
|
||||
}
|
||||
fetchOptions: {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'trakt-api-version': 2,
|
||||
'trakt-api-key': TV_KEY,
|
||||
},
|
||||
},
|
||||
}).catch()
|
||||
const data = await res
|
||||
const movies = formatMovieData(data)
|
||||
|
||||
return movies
|
||||
for (const movie of movies) {
|
||||
const tmdbId = movie['tmdbId']
|
||||
const tmdbUrl = `https://api.themoviedb.org/3/movie/${tmdbId}?api_key=${MOVIEDB_KEY}`
|
||||
const tmdbRes = EleventyFetch(tmdbUrl, {
|
||||
duration: '1h',
|
||||
type: 'json',
|
||||
})
|
||||
const tmdbData = await tmdbRes
|
||||
const posterPath = tmdbData['poster_path']
|
||||
movie.image = `https://cd-movies.b-cdn.net/t/p/w500${posterPath}`
|
||||
}
|
||||
return movies;
|
||||
} else {
|
||||
return formatMovieData(MoviesMock)
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue