Skip to content

Commit

Permalink
Merge pull request #34 from leoferreiralima/feat/get-root-content
Browse files Browse the repository at this point in the history
feat: add get root content
  • Loading branch information
leoferreiralima authored Sep 27, 2023
2 parents c58ebf5 + f980c08 commit d10e1fa
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,33 @@ const parentContent = await tabNews.contents.getParent({
});
```

**Buscar Conteúdo Raiz**

```js
import { TabNews } from 'tabnews-sdk';

const tabNews = new TabNews();

const rootContent = await tabNews.contents.getRoot({
slug: '<slug>',
username: '<username>',
});
```

```js
import { TabNews } from 'tabnews-sdk';

const tabNews = new TabNews();

await tabNews.session.create();

// Não é preciso passar o username pois internamente a bliblioteca ira realizar o fecth do usuario atual

const rootContent = await tabNews.contents.getRoot({
slug: '<slug>',
});
```

**Criar Conteúdo**

Na rota de criação de conteúdos, todos os campos são opcionais exceto o `body`,
Expand Down
42 changes: 42 additions & 0 deletions src/content/__snapshots__/content.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,46 @@ exports[`Content > get > should get parent content for current user 1`] = `
}
`;

exports[`Content > get > should get root content 1`] = `
{
"body": "body",
"children_deep_count": 2,
"created_at": 2023-09-19T12:16:04.812Z,
"deleted_at": null,
"id": "id",
"owner_id": "owner_id",
"owner_username": "username",
"parent_id": null,
"published_at": 2023-09-19T12:16:04.837Z,
"slug": "slug",
"source_url": "https://source.url.com/source",
"status": "published",
"tabcoins": 1,
"title": "title",
"updated_at": 2023-09-19T12:16:04.812Z,
}
`;

exports[`Content > get > should get root content for current user 1`] = `
{
"body": "body",
"children_deep_count": 2,
"created_at": 2023-09-19T12:16:04.812Z,
"deleted_at": null,
"id": "id",
"owner_id": "owner_id",
"owner_username": "username",
"parent_id": null,
"published_at": 2023-09-19T12:16:04.837Z,
"slug": "slug",
"source_url": "https://source.url.com/source",
"status": "published",
"tabcoins": 1,
"title": "title",
"updated_at": 2023-09-19T12:16:04.812Z,
}
`;

exports[`Content > get > should return correct page when is last page 1`] = `
{
"first_page": 1,
Expand Down Expand Up @@ -369,6 +409,8 @@ exports[`Content > get > should throw an error when content of content children

exports[`Content > get > should throw an error when content of parent not found 1`] = `"O conteúdo informado não foi encontrado no sistema."`;

exports[`Content > get > should throw an error when content of root not found 1`] = `"O conteúdo informado não foi encontrado no sistema."`;

exports[`Content > get > should throw an error when parameter is invalid 1`] = `"\\"page\\" deve possuir um valor mínimo de 1."`;

exports[`Content > update > should throw a api erro when content not found 1`] = `"O conteúdo informado não foi encontrado no sistema."`;
Expand Down
67 changes: 67 additions & 0 deletions src/content/content.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ describe('Content', () => {
);
};

const mockRootContent = (slug: string, user: string = username) => {
mockOnceResponse(
`${TABNEWS_ENDPOINTS.content}/${user}/${slug}/root`,
contentDetail,
);
};

it('should get all contents and pagination', async () => {
mockContents(linkHeader);

Expand Down Expand Up @@ -429,6 +436,66 @@ describe('Content', () => {
}),
).rejects.toThrowErrorMatchingSnapshot();
});

it('should get root content', async () => {
const slug = 'slug';

mockRootContent(slug);

const content = await tabNews.content.getRoot({
slug,
username,
});

expect(content).toMatchSnapshot();

const request = mockedRequest();

expectRequest(request).method.toBeGet();
});

it('should get root content for current user', async () => {
const slug = 'slug';

mockOnceCurrentUser();

mockRootContent(slug);

const content = await tabNews.content.getRoot({
slug,
});

expect(content).toMatchSnapshot();

const request = mockedRequest();

expectRequest(request).method.toBeGet();
});

it('should throw an error when content of root not found', () => {
const slug = 'slug';

mockOnceApiError(
`${TABNEWS_ENDPOINTS.content}/${username}/${slug}/root`,
{
name: 'NotFoundError',
message: 'O conteúdo informado não foi encontrado no sistema.',
action: 'Verifique se o "slug" está digitado corretamente.',
status_code: 404,
error_id: '3ea15e67-97c8-4671-916f-0344934c8300',
request_id: '11815650-d56e-4b90-97dd-dcdf23df8412',
error_location_code: 'CONTROLLER:CONTENT:GET_HANDLER:SLUG_NOT_FOUND',
key: 'slug',
},
);

expect(() =>
tabNews.content.getRoot({
slug,
username,
}),
).rejects.toThrowErrorMatchingSnapshot();
});
});

describe('create', () => {
Expand Down
12 changes: 12 additions & 0 deletions src/content/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ export class Content {
return parentContent;
}

async getRoot(params: GetContentParams) {
const url = await this.getUrlForSlugAndUsername(params);

const { body: rootContent } = await this.tabNews.get<ContentDetailResponse>(
{
path: `${url}/root`,
},
);

return rootContent;
}

private async getUrlForSlugAndUsername({ slug, username }: GetContentParams) {
if (username) {
return `${TABNEWS_ENDPOINTS.content}/${username}/${slug}`;
Expand Down

0 comments on commit d10e1fa

Please sign in to comment.