parent
1e92edeae4
commit
449d95109e
@ -0,0 +1,37 @@ |
||||
import { describe, it, expect } from '@jest/globals'; |
||||
|
||||
import { createOmdbClient } from './omdb'; |
||||
|
||||
describe('createOmdbClient', () => { |
||||
const client = createOmdbClient('68fd98ab'); |
||||
it('returns some data for the sample movie id', async () => { |
||||
const result = await client.getMovieMetadata('tt11873472'); |
||||
expect(result).toMatchObject({ |
||||
'Actors': 'Cheryl Isheja, Elvis Ngabo, Diogène Ntarindwa', |
||||
'Awards': expect.any(String), |
||||
'BoxOffice': expect.any(String), |
||||
'Country': 'Rwanda, France, Canada, United Kingdom, United States', |
||||
'DVD': expect.any(String), |
||||
'Director': 'Anisia Uzeyman, Saul Williams', |
||||
'Genre': 'Musical, Sci-Fi', |
||||
'Language': 'Kinyarwanda, Kirundi, Swahili, French, English', |
||||
'Metascore': expect.stringMatching(/^\d+$/), |
||||
'Plot': expect.any(String), |
||||
'Poster': expect.stringMatching(/^http/), |
||||
'Production': expect.any(String), |
||||
'Rated': expect.any(String), |
||||
'Ratings': expect.any(Array), |
||||
'Released': '10 May 2023', |
||||
'Response': 'True', |
||||
'Runtime': '105 min', |
||||
'Title': 'Neptune Frost', |
||||
'Type': 'movie', |
||||
'Website': expect.any(String), |
||||
'Writer': 'Saul Williams', |
||||
'Year': '2021', |
||||
'imdbID': 'tt11873472', |
||||
'imdbRating': expect.stringMatching(/^\d?\d(\.\d)?$/), |
||||
'imdbVotes': expect.stringMatching(/^\d+$/), |
||||
}); |
||||
}, 10_000); |
||||
}); |
@ -0,0 +1,26 @@ |
||||
import fetch from 'node-fetch'; |
||||
import PQueue from 'p-queue'; |
||||
|
||||
export const createOmdbClient = (apiKey: string) => { |
||||
// Rate limit (according to readme, it's 10k per day;
|
||||
// here we set it 1 per second, should be enough for the demo app goal)
|
||||
const queue = new PQueue({ |
||||
interval: 1000, |
||||
intervalCap: 1, |
||||
timeout: 2000, |
||||
throwOnTimeout: true, |
||||
}); |
||||
|
||||
return { |
||||
getMovieMetadata: async (movieId: string) => |
||||
queue.add(async () => { |
||||
const url = new URL('https://www.omdbapi.com/'); |
||||
url.searchParams.append('i', movieId); |
||||
url.searchParams.append('apikey', apiKey); |
||||
url.searchParams.append('plot', 'full'); |
||||
const response = await fetch(url); |
||||
const body = (await response.json()) as unknown; |
||||
return body; |
||||
}), |
||||
}; |
||||
}; |
Loading…
Reference in new issue