From 6f60c49bd1fb6c14915734418ac1b245451c839e Mon Sep 17 00:00:00 2001 From: WebStreamr <210764791+webstreamr@users.noreply.github.com> Date: Fri, 17 Oct 2025 14:25:30 +0000 Subject: [PATCH] chore: throw explicit error if required TMDB_ACCESS_TOKEN env var is not set --- jest.setup.ts | 4 ++++ src/utils/env.test.ts | 10 +++++++++- src/utils/env.ts | 9 +++++++++ src/utils/tmdb.ts | 4 ++-- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/jest.setup.ts b/jest.setup.ts index 6fb1507..ddf8cc8 100644 --- a/jest.setup.ts +++ b/jest.setup.ts @@ -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'), })); diff --git a/src/utils/env.test.ts b/src/utils/env.test.ts index 1c60eb9..024e7b0 100644 --- a/src/utils/env.test.ts +++ b/src/utils/env.test.ts @@ -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'); diff --git a/src/utils/env.ts b/src/utils/env.ts index 7e91cb0..aa145cb 100644 --- a/src/utils/env.ts +++ b/src/utils/env.ts @@ -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'; diff --git a/src/utils/tmdb.ts b/src/utils/tmdb.ts index 21af393..6e2f24b 100644 --- a/src/utils/tmdb.ts +++ b/src/utils/tmdb.ts @@ -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(); const tmdbFetch = async (ctx: Context, fetcher: Fetcher, path: string, searchParams?: Record): Promise => { const config: CustomRequestInit = { headers: { - 'Authorization': 'Bearer ' + envGet('TMDB_ACCESS_TOKEN'), + 'Authorization': 'Bearer ' + envGetRequired('TMDB_ACCESS_TOKEN'), 'Content-Type': 'application/json', }, queueLimit: 50,