-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
32 lines (27 loc) · 869 Bytes
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const axios = require('axios')
const formatResult = (result) => ({
title: result.Title,
year: parseInt(result.Year),
imdbid: result.imdbID,
imageurl: result.Poster,
})
exports.findMovies = async (term) => {
const apiKey = process.env.OMDB_API_KEY
const fetchUrl = `http://www.omdbapi.com/?apikey=${apiKey}&s=${term}`
try {
const response = await axios.get(fetchUrl)
return response.data.Search.map(formatResult)
} catch (error) {
throw new Error(`Movie search API error: ${String(error)}`)
}
}
exports.findMovie = async (imdbid) => {
const apiKey = process.env.OMDB_API_KEY
const fetchUrl = `http://www.omdbapi.com/?apikey=${apiKey}&i=${imdbid}`
try {
const response = await axios.get(fetchUrl)
return formatResult(response.data)
} catch (error) {
throw new Error(`Movie search API error: ${String(error)}`)
}
}