Skip to content

Commit

Permalink
feat: support /episodes/:id/translations/:lang
Browse files Browse the repository at this point in the history
  • Loading branch information
falsepopsky committed Nov 2, 2023
1 parent 979b815 commit 8e7f5ad
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-apricots-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@untidy/thetvdb': minor
---

feat: support `/episodes/:id/translations/:language` endpoint
11 changes: 11 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,8 @@ type GetEpisode<O extends Options> = O['extended'] extends true
: Data<EpisodeExtended>
: Data<Episode>;

type GetEpisodeByLanguage = Data<Pick<TranslationHelper, 'name' | 'overview' | 'language'>>;

type GetEpisodesByPage = DataLink<Episode[]>;

type GetMovie<O extends MovieOptions> = O['extended'] extends true
Expand Down Expand Up @@ -587,6 +589,15 @@ export class TheTVDB extends Base {
return await this.fetcher<GetEpisode<O>>(endpoint);
}

public async getEpisodeByLanguage(id: string, language: string): Promise<GetEpisodeByLanguage> {
this.validateInput(id, 'Required episode id');
this.validateInput(language, 'Required language id');

const endpoint = this.api + '/v4/episodes/' + id + '/translations/' + language;

return await this.fetcher<GetEpisodeByLanguage>(endpoint);
}

public async getEpisodesByPage(page?: string): Promise<GetEpisodesByPage> {
let endpoint = this.api + '/v4/episodes';
if (typeof page === 'string' && page.length > 0 && page.length <= 5) {
Expand Down
18 changes: 18 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,24 @@ describe('getEpisode()', () => {
});
});

describe('getEpisodeByLanguage()', () => {
it('throws an error if no id is provided', async () => {
// @ts-expect-error: Required id
await expect(async () => await client.getEpisodeByLanguage()).rejects.toThrow('Required episode id');
});

it('throws an error if no language is provided', async () => {
// @ts-expect-error: Required language
await expect(async () => await client.getEpisodeByLanguage('40')).rejects.toThrow('Required language id');
});

it('returns a successful response', async () => {
const { data } = await client.getEpisodeByLanguage('40', 'spa');
expect(data.name).toBe('El Baile');
expect(data.language).toBe('spa');
});
});

describe('getEpisodesByPage()', () => {
it('returns a successful response', async () => {
const { data } = await client.getEpisodesByPage();
Expand Down
25 changes: 17 additions & 8 deletions tests/mocks/handlers/episodes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { HttpResponse, http, type HttpHandler } from 'msw';

// https://api4.thetvdb.com/v4/episodes/40/translations/spa
const episodeLang = {
data: {
name: 'El Baile',
language: 'spa',
},
};

// https://api4.thetvdb.com/v4/episodes/127396/extended?meta=translations
const episodesEM = {
data: {
Expand Down Expand Up @@ -56,15 +64,16 @@ const episodes = {
export const episodesHandlers: HttpHandler[] = [
http.get<never>('https://api4.thetvdb.com/v4/episodes/*', ({ request }) => {
const url = new URL(request.url);
const meta = url.searchParams.get('meta');

if (meta === 'translations') {
return HttpResponse.json(episodesEM);
}
if (request.url === 'https://api4.thetvdb.com/v4/episodes/127396/extended') {
return HttpResponse.json(episodeE);
} else {
return HttpResponse.json(episode);
switch (url.href) {
case 'https://api4.thetvdb.com/v4/episodes/40/translations/spa':
return HttpResponse.json(episodeLang);
case 'https://api4.thetvdb.com/v4/episodes/127396/extended?meta=translations':
return HttpResponse.json(episodesEM);
case 'https://api4.thetvdb.com/v4/episodes/127396/extended':
return HttpResponse.json(episodeE);
default:
return HttpResponse.json(episode);
}
}),

Expand Down

0 comments on commit 8e7f5ad

Please sign in to comment.