UNPKG

tmdbrjs

Version:

A TypeScript wrapper for The Movie Database (TMDB) API

68 lines 2.52 kB
import Movies from './movies/movies'; import People from './people/people'; import Tv from './tv/tv'; import { camelCase } from 'change-case'; import { applyCaseMiddleware } from './utils/applyCaseMiddleware'; class Client { config; apiClient; movies; people; tv; version; baseUrl; language; constructor(config) { this.config = config; this.version = config.version ?? '3'; this.baseUrl = config.baseUrl ?? 'https://api.themoviedb.org'; this.language = config.language ?? 'en-US'; this.apiClient = { get: async (pathname, options = {}) => { if (!this.config.apiKey) { throw new Error('No API key provided'); } const url = new URL(pathname, `${this.baseUrl}/${this.version}/`); url.searchParams.append('language', this.language); const response = await fetch(url, { method: options.method, body: options.body, credentials: options.credentials, headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${this.config.apiKey}`, ...(options.headers ? Object.fromEntries(Object.entries(options.headers).filter(([, value]) => value !== undefined)) : {}), }, }); if (!response.ok) { if (response.status === 401) { throw new Error('Invalid API key'); } if (response.status === 404) { throw new Error('Resource not found'); } if (response.status === 429) { throw new Error('Too many requests'); } throw new Error(`HTTP error! status: ${String(response.status)}`); } let data = await response.json(); data = applyCaseMiddleware(data, camelCase); return data; }, }; this.movies = new Movies(this.apiClient); this.people = new People(this.apiClient); this.tv = new Tv(this.apiClient); } getVersion() { return this.version; } getLanguage() { return this.language; } } export { Client }; //# sourceMappingURL=index.js.map