Skip to content

Commit

Permalink
feat: support /companies endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
falsepopsky committed Oct 20, 2023
1 parent 8887742 commit 6e8850e
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 69 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-poets-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@untidy/thetvdb': minor
---

feat: support `/companies` endpoint
44 changes: 44 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,29 @@ interface Character extends SharedProps {
personImgURL: string | null;
}

interface Company extends Omit<SharedProps, 'image'> {
activeDate: string;
aliases: Aliases[];
country: string;
inactiveDate: string;
primaryCompanyType: number;
slug: string;
parentCompany: {
id: number;
name: string;
relation: {
id: number;
typeName: string;
};
};
tagOptions: TagOptions[];
}

interface CompanyType {
companyTypeId: number;
companyTypeName: string;
}

interface Episode extends SharedProps {
seriesId: number;
aired: string | null;
Expand Down Expand Up @@ -410,6 +433,9 @@ type GetAwardsCategoriesById = Data<AwardCategory>;
type GetAwardsCategoriesByIdExtended = Data<AwardCategoryExtended>;

type GetCharacter = Data<Character>;
type GetCompanies = DataLink<Company[]>;
type GetCompaniesTypes = Data<CompanyType[]>;
type GetCompanyById = Data<Company>;

type GetFilteredMovie = DataLink<Movie[]>;

Expand Down Expand Up @@ -509,6 +535,24 @@ export class TheTVDB extends Base {
return await this.fetcher<GetCharacter>(endpoint);
}

public async getCompanies(page?: string): Promise<GetCompanies> {
let endpoint = this.api + '/v4/companies';
if (typeof page === 'string' && page.length > 0 && page.length < 3) {
endpoint += `?page=${page}`;
}
return await this.fetcher<GetCompanies>(endpoint);
}

public async getCompaniesTypes(): Promise<GetCompaniesTypes> {
return await this.fetcher<GetCompaniesTypes>(this.api + '/v4/companies/types');
}

public async getCompanyById(id: string): Promise<GetCompanyById> {
this.validateInput(id, 'Required company id');

return await this.fetcher<GetCompanyById>(`${this.api}/v4/companies/${id}`);
}

public async getFilteredMovie(options: FilterOptions): Promise<GetFilteredMovie> {
this.validateInput(options?.country, 'Required country of origin');
this.validateInput(options?.lang, 'Required language');
Expand Down
7 changes: 4 additions & 3 deletions src/playground.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { config } from 'dotenv';
import { join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { TheTVDB, TheTVDBExtended } from './index.js';
import { TheTVDB } from './index.js';

const ROOT_DIR = resolve(fileURLToPath(import.meta.url), '../..');
const ENVFILE = join(ROOT_DIR, '.env');
Expand All @@ -10,10 +10,11 @@ config({ path: ENVFILE });

const TOKEN = process.env.TVDB_API_TOKEN;
const client = new TheTVDB(TOKEN);
/*
const clientExtended = new TheTVDBExtended(TOKEN);
const langs = await clientExtended.getLanguages();
console.log(langs);
console.log(langs); */

const movie = await client.getMovie({ id: '12586' });
const movie = await client.getCompanies('94');
console.log(movie);
43 changes: 43 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,49 @@ describe('getCharacter()', () => {
});
});

describe('getCompanies()', () => {
test('returns a successful response without a page', async () => {
const { data } = await client.getCompanies();
expect(Array.isArray(data)).toBe(true);
expect(data).toHaveLength(1);
expect(data[0]?.id).toBe(48649);
expect(data[0]?.name).toBe('Ananey');
});

test('returns a successful response with page', async () => {
const { data } = await client.getCompanies('94');
expect(Array.isArray(data)).toBe(true);
expect(data).toHaveLength(2);
expect(data[0]?.id).toBe(48646);
expect(data[1]?.name).toBe('New Group Productions');
});
});

describe('getCompaniesTypes()', () => {
test('returns a successful response', async () => {
const { data } = await client.getCompaniesTypes();
expect(Array.isArray(data)).toBe(true);
expect(data).toHaveLength(2);
expect(data[0]?.companyTypeId).toBe(1);
expect(data[1]?.companyTypeName).toBe('Studio');
});
});

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

test('returns a successful response', async () => {
const { data } = await client.getCompanyById('4');

expect(data.id).toBe(4);
expect(data.name).toBe('Aaj TV');
expect(data.country).toBe('pak');
});
});

describe('getEpisode()', () => {
it('throws an error if no id is provided', async () => {
// @ts-expect-error: expect a parameter id
Expand Down
19 changes: 19 additions & 0 deletions tests/mocks/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import {
awardsId,
awardsIdExtended,
character,
companies,
companiesPage,
companiesTypes,
companyId,
contentRatings,
countries,
episodes,
Expand Down Expand Up @@ -64,6 +68,21 @@ export const handlers: RestHandler[] = [
rest.get('https://api4.thetvdb.com/v4/awards', async (_req, res, ctx) => {
return await res(ctx.json(awards));
}),
rest.get('https://api4.thetvdb.com/v4/companies', async (req, res, ctx) => {
if (req.url.href === 'https://api4.thetvdb.com/v4/companies?page=94') {
return await res(ctx.json(companiesPage));
} else {
return await res(ctx.json(companies));
}
}),
rest.get('https://api4.thetvdb.com/v4/companies/:path', async (req, res, ctx) => {
switch (req.url.href) {
case 'https://api4.thetvdb.com/v4/companies/types':
return await res(ctx.json(companiesTypes));
default:
return await res(ctx.json(companyId));
}
}),
rest.get('https://api4.thetvdb.com/v4/content/ratings', async (_req, res, ctx) => {
return await res(ctx.json(contentRatings));
}),
Expand Down
117 changes: 51 additions & 66 deletions tests/mocks/response.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
// https://api4.thetvdb.com/v4/artwork/63237874/extended
const artworkExtended = {
data: {
movieId: 145830,
},
data: { movieId: 145830 },
};

// https://api4.thetvdb.com/v4/artwork/63237874
const artwork = {
status: 'success',
data: {
id: 63237874,
},
data: { id: 63237874 },
};

// https://api4.thetvdb.com/v4/artwork/statuses
Expand All @@ -22,86 +18,84 @@ const artworkStatuses = {
// https://api4.thetvdb.com/v4/artwork/types
const artworkTypes = {
status: 'success',
data: [
{
thumbHeight: 140,
},
{
id: 2,
imageFormat: 'JPG',
},
],
data: [{ thumbHeight: 140 }, { id: 2, imageFormat: 'JPG' }],
};

// https://api4.thetvdb.com/v4/awards
const awards = {
status: 'success',
data: [
{
id: 1,
name: 'Academy Awards',
},
],
data: [{ id: 1, name: 'Academy Awards' }],
};

// https://api4.thetvdb.com/v4/awards/1
const awardsId = {
data: {
id: 1,
name: 'Academy Awards',
},
data: { id: 1, name: 'Academy Awards' },
};

// https://api4.thetvdb.com/v4/awards/1/extended
const awardsIdExtended = {
data: {
categories: [
{
id: 1,
name: 'Best Picture',
},
],
categories: [{ id: 1, name: 'Best Picture' }],
},
};

// https://api4.thetvdb.com/v4/awards/categories/42
const awardsCategoryId = {
data: {
id: 42,
name: 'Best Actor in a Television Series – Drama',
},
data: { id: 42, name: 'Best Actor in a Television Series – Drama' },
};

// https://api4.thetvdb.com/v4/awards/categories/42/extended
const awardsCategoryIdExtended = {
data: {
nominees: [
{
id: 6352,
isWinner: true,
},
],
nominees: [{ id: 6352, isWinner: true }],
},
};

// https://api4.thetvdb.com/v4/characters/64140522
const character = {
data: {
id: 64140522,
name: 'Spike Spiegel',
},
data: { id: 64140522, name: 'Spike Spiegel' },
};

// https://api4.thetvdb.com/v4/content/ratings
const contentRatings = {
status: 'success',
// https://api4.thetvdb.com/v4/companies
const companies = {
data: [{ id: 48649, name: 'Ananey' }],
};

// https://api4.thetvdb.com/v4/companies?page=94
const companiesPage = {
data: [
{
name: 'ATP',
id: 48646,
},
{
name: 'New Group Productions',
},
],
};

// https://api4.thetvdb.com/v4/companies/types
const companiesTypes = {
data: [
{
companyTypeId: 1,
},
{
companyTypeName: 'Studio',
},
],
};

// https://api4.thetvdb.com/v4/companies/4
const companyId = {
data: { id: 4, name: 'Aaj TV', country: 'pak' },
};

// https://api4.thetvdb.com/v4/content/ratings
const contentRatings = {
status: 'success',
data: [{ name: 'ATP' }],
};

// https://api4.thetvdb.com/v4/countries
const countries = {
status: 'success',
Expand Down Expand Up @@ -312,35 +306,22 @@ const movieEM = {

// https://api4.thetvdb.com/v4/movies/3646/extended?short=true
const movieES = {
data: {
characters: null,
artworks: null,
trailers: null,
},
data: { characters: null, artworks: null, trailers: null },
};

// https://api4.thetvdb.com/v4/movies/3646/extended
const movieE = {
data: {
trailers: [
{
id: 143117,
language: 'spa',
},
{
id: 143118,
language: 'eng',
},
{ id: 143117, language: 'spa' },
{ id: 143118, language: 'eng' },
],
},
};

// https://api4.thetvdb.com/v4/movies/12586
const movie = {
data: {
id: 12586,
slug: 'macross-do-you-remember-love',
},
data: { id: 12586, slug: 'macross-do-you-remember-love' },
};

// https://api4.thetvdb.com/v4/seasons/6365/extended?meta=translations
Expand Down Expand Up @@ -462,6 +443,10 @@ export {
awardsId,
awardsIdExtended,
character,
companies,
companiesPage,
companiesTypes,
companyId,
contentRatings,
countries,
episodes,
Expand Down

0 comments on commit 6e8850e

Please sign in to comment.