Skip to content

Commit

Permalink
feat: support /series endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
falsepopsky committed Nov 7, 2023
1 parent 812d34e commit ee55cb4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/spicy-zebras-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@untidy/thetvdb': minor
---

feat: support `/series` endpoint
10 changes: 10 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,8 @@ type GetSerie<O extends SeriesOptions> = O['extended'] extends true
: Data<SerieExtended>
: Data<Serie>;

type GetSeriesByPage = DataLink<Serie[]>;

export class TheTVDB extends Base {
public async getArtwork<O extends ArtworkOptions>(options: O): Promise<GetArtwork<O>> {
this.validateInput(options?.id, 'Required artwork id');
Expand Down Expand Up @@ -745,4 +747,12 @@ export class TheTVDB extends Base {

return await this.fetcher<GetSerie<O>>(endpoint.href);
}

public async getSeriesByPage(page?: string): Promise<GetSeriesByPage> {
let endpoint = this.api + '/v4/series';
if (typeof page === 'string' && page.length > 0 && page.length <= 3) {
endpoint += `?page=${page}`;
}
return await this.fetcher<GetSeriesByPage>(endpoint);
}
}
20 changes: 20 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,3 +607,23 @@ describe('getSeries()', () => {
expect(data.episodes[0]?.aired).toBe('2018-09-28');
});
});

describe('getSeriesByPage()', () => {
it('returns a successful response', async () => {
const { data } = await client.getSeriesByPage();
expect(Array.isArray(data)).toBe(true);
expect(data).toHaveLength(1);
expect(data[0]?.id).toBe(70327);
expect(data[0]?.name).toBe('Buffy the Vampire Slayer');
});

it('returns a successful response with query page', async () => {
const { data } = await client.getSeriesByPage('294');
expect(Array.isArray(data)).toBe(true);
expect(data).toHaveLength(2);
expect(data[0]?.id).toBe(441532);
expect(data[0]?.name).toBe('Geddy Lee Asks: Are Bass Players Human Too?');
expect(data[1]?.id).toBe(441533);
expect(data[1]?.name).toBe('LEGO DUPLO Nursery Rhymes');
});
});
36 changes: 35 additions & 1 deletion tests/mocks/handlers/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const seriesFilterCountryLang = {
};

// https://api4.thetvdb.com/v4/series/78878
const series = {
const serie = {
data: {
id: 78878,
},
Expand Down Expand Up @@ -101,6 +101,30 @@ const seriesEE = {
},
};

// https://api4.thetvdb.com/v4/series?page=294
const seriesPage = {
data: [
{
id: 441532,
name: 'Geddy Lee Asks: Are Bass Players Human Too?',
},
{
id: 441533,
name: 'LEGO DUPLO Nursery Rhymes',
},
],
};

// https://api4.thetvdb.com/v4/series
const series = {
data: [
{
id: 70327,
name: 'Buffy the Vampire Slayer',
},
],
};

export const seriesHandlers: HttpHandler[] = [
http.get<never>('https://api4.thetvdb.com/v4/series/*', ({ request }) => {
const url = new URL(request.url);
Expand All @@ -124,6 +148,16 @@ export const seriesHandlers: HttpHandler[] = [
return HttpResponse.json(seriesES);
case 'https://api4.thetvdb.com/v4/series/78878/extended':
return HttpResponse.json(seriesE);
default:
return HttpResponse.json(serie);
}
}),
http.get<never>('https://api4.thetvdb.com/v4/series', ({ request }) => {
const url = new URL(request.url);

switch (url.href) {
case 'https://api4.thetvdb.com/v4/series?page=294':
return HttpResponse.json(seriesPage);
default:
return HttpResponse.json(series);
}
Expand Down

0 comments on commit ee55cb4

Please sign in to comment.