Skip to content

Commit

Permalink
Merge pull request #43 from falsepopsky/dev
Browse files Browse the repository at this point in the history
feat: support companies endpoint
  • Loading branch information
falsepopsky authored Oct 20, 2023
2 parents 03f8482 + da104fd commit 665635a
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 74 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
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Setup pnpm
uses: pnpm/action-setup@v2.4.0
with:
version: 8.9.0
version: 8.9.2

- name: Setup Node
uses: actions/setup-node@v3.8.1
Expand Down
2 changes: 1 addition & 1 deletion docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default defineConfig({
nav: [
{ text: 'Guide', link: '/guide/getting-started' },
{
text: '0.3.0',
text: '0.4.0',
items: [
{
text: 'Changelog',
Expand Down
62 changes: 62 additions & 0 deletions docs/api/thetvdb.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,68 @@ Returns a character base record
await client.getCharacter('64140522');
```

## getCompanies

Returns a paginated list of companies records

| params | type | Required | Description |
| ------ | -------- | :------: | ------------------------------------ |
| page | `string` | Optional | The `page` of the Companies records. |

### Supported endpoint <Badge type="warning" text="endpoint" />

| method | endpoint |
| ------------------------------- | ------------ |
| <Badge type="tip" text="GET" /> | `/companies` |

### First 500 Companies records <Badge type="tip" text="example" />

```js
await client.getCompanies();
```

### Companies records for the page 94 <Badge type="tip" text="example" />

```js
await client.getCompanies('94');
```

## getCompaniesTypes

This method returns a list of companies records and does not require any parameters.

### Supported endpoint <Badge type="warning" text="endpoint" />

| method | endpoint |
| ------------------------------- | ------------------ |
| <Badge type="tip" text="GET" /> | `/companies/types` |

### Companies types records <Badge type="tip" text="example" />

```js
await client.getCompaniesTypes();
```

## getCompanyById

Returns a company record

| params | type | Required | Description |
| ------ | -------- | :------: | ------------------------ |
| id | `string` | Yes | The `id` of the Company. |

### Supported endpoint <Badge type="warning" text="endpoint" />

| method | endpoint |
| ------------------------------- | ---------------- |
| <Badge type="tip" text="GET" /> | `/companies/:id` |

### Single company record <Badge type="tip" text="example" />

```js
await client.getCompanyById('4');
```

## getEpisode

Returns an episode base or extended record
Expand Down
6 changes: 3 additions & 3 deletions docs/guide/supported-endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ List of endpoints from [TheTVDB API V4](https://thetvdb.github.io/v4-api/).
| `/awards/categories/{id}` | :white_check_mark: |
| `/awards/categories/{id}/extended` | :white_check_mark: |
| `/characters/{id}` | :white_check_mark: |
| `/companies` | :interrobang: |
| `/companies/types` | :interrobang: |
| `/companies/{id}` | :interrobang: |
| `/companies` | :white_check_mark: |
| `/companies/types` | :white_check_mark: |
| `/companies/{id}` | :white_check_mark: |
| `/content/ratings` | :white_check_mark: |
| `/countries` | :white_check_mark: |
| `/entities` | :interrobang: |
Expand Down
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
Loading

0 comments on commit 665635a

Please sign in to comment.