chore: throw explicit error if required TMDB_ACCESS_TOKEN env var is not set

This commit is contained in:
WebStreamr 2025-10-17 14:25:30 +00:00
parent 0018ccc929
commit 6f60c49bd1
No known key found for this signature in database
4 changed files with 24 additions and 3 deletions

View file

@ -1,5 +1,9 @@
process.env['CACHE_DIR'] = '/dev/null';
if (!process.env['TMDB_ACCESS_TOKEN']) {
process.env['TMDB_ACCESS_TOKEN'] = 'some access token';
}
jest.mock('randomstring', () => ({
generate: jest.fn(() => 'mocked-random-string'),
}));

View file

@ -1,12 +1,20 @@
import * as os from 'node:os';
import { Request } from 'express';
import { envGet, envGetAppId, envGetAppName, envIsProd, getCacheDir, isElfHostedInstance } from './env';
import { envGet, envGetAppId, envGetAppName, envGetRequired, envIsProd, getCacheDir, isElfHostedInstance } from './env';
describe('env', () => {
test('envGet', () => {
expect(envGet('NODE_ENV')).toBe('test');
});
test('envGetRequired set', () => {
expect(envGetRequired('NODE_ENV')).toBe('test');
});
test('envGetRequired not set', () => {
expect(() => envGetRequired('NOT_SET')).toThrow('Environment variable "NOT_SET" is not configured.');
});
test('envGetAppId', () => {
expect(envGetAppId()).toBe('webstreamr');

View file

@ -3,6 +3,15 @@ import { Request } from 'express';
export const envGet = (name: string): string | undefined => process.env[name];
export const envGetRequired = (name: string): string => {
const value = envGet(name);
if (!value) {
throw new Error(`Environment variable "${name}" is not configured.`);
}
return value;
};
export const envGetAppId = (): string => process.env['MANIFEST_ID'] || 'webstreamr';
export const envGetAppName = (): string => process.env['MANIFEST_NAME'] || 'WebStreamr';

View file

@ -1,7 +1,7 @@
import { Mutex } from 'async-mutex';
import { NotFoundError } from '../error';
import { Context } from '../types';
import { envGet } from './env';
import { envGetRequired } from './env';
import { CustomRequestInit, Fetcher } from './Fetcher';
import { ImdbId, TmdbId } from './id';
@ -34,7 +34,7 @@ const mutexes = new Map<string, Mutex>();
const tmdbFetch = async (ctx: Context, fetcher: Fetcher, path: string, searchParams?: Record<string, string | undefined>): Promise<unknown> => {
const config: CustomRequestInit = {
headers: {
'Authorization': 'Bearer ' + envGet('TMDB_ACCESS_TOKEN'),
'Authorization': 'Bearer ' + envGetRequired('TMDB_ACCESS_TOKEN'),
'Content-Type': 'application/json',
},
queueLimit: 50,