tmdbrjs
Version:
A TypeScript wrapper for The Movie Database (TMDB) API
74 lines • 2.92 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Client = void 0;
const movies_1 = __importDefault(require("./movies/movies"));
const people_1 = __importDefault(require("./people/people"));
const tv_1 = __importDefault(require("./tv/tv"));
const change_case_1 = require("change-case");
const applyCaseMiddleware_1 = require("./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 = (0, applyCaseMiddleware_1.applyCaseMiddleware)(data, change_case_1.camelCase);
return data;
},
};
this.movies = new movies_1.default(this.apiClient);
this.people = new people_1.default(this.apiClient);
this.tv = new tv_1.default(this.apiClient);
}
getVersion() {
return this.version;
}
getLanguage() {
return this.language;
}
}
exports.Client = Client;
//# sourceMappingURL=index.js.map