tmdbrjs
Version:
A TypeScript wrapper for The Movie Database (TMDB) API
34 lines • 1.39 kB
JavaScript
import ApiURL from './apiURL';
import { camelToSnakeCaseArray } from './caseConversion';
export class BaseService {
apiClient;
constructor(apiClient) {
this.apiClient = apiClient;
}
async getByIdWithAppendToResponse(endpoint, id, options) {
const { include } = options ?? {};
let appendToResponse;
if (include && include.length > 0) {
// Convert camelCase options to snake_case for the API
appendToResponse = camelToSnakeCaseArray([...include]).join(',');
}
const url = new ApiURL(endpoint.replace('{id}', id.toString()));
if (appendToResponse) {
url.appendParam('append_to_response', appendToResponse);
}
try {
return await this.apiClient.get(url.toString());
}
catch (error) {
if (error instanceof Error) {
// Enhanced error handling for append_to_response specific errors
if (error.message.includes('append_to_response')) {
throw new Error(`Failed to fetch ${endpoint} with append_to_response options: ${error.message}`);
}
throw new Error(`Failed to fetch ${endpoint}: ${error.message}`);
}
throw new Error(`Failed to fetch ${endpoint}: Something went wrong`);
}
}
}
//# sourceMappingURL=BaseService.js.map