diff --git a/README.md b/README.md index 99bafcd..41ca225 100644 --- a/README.md +++ b/README.md @@ -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: '', + 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: '', +}); +``` + **Criar Conteúdo** Na rota de criação de conteúdos, todos os campos são opcionais exceto o `body`, diff --git a/src/content/__snapshots__/content.spec.ts.snap b/src/content/__snapshots__/content.spec.ts.snap index a001ea6..81e7021 100644 --- a/src/content/__snapshots__/content.spec.ts.snap +++ b/src/content/__snapshots__/content.spec.ts.snap @@ -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, @@ -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."`; diff --git a/src/content/content.spec.ts b/src/content/content.spec.ts index f5a6dc2..b9acc36 100644 --- a/src/content/content.spec.ts +++ b/src/content/content.spec.ts @@ -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); @@ -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', () => { diff --git a/src/content/content.ts b/src/content/content.ts index 248dc12..55afa4b 100644 --- a/src/content/content.ts +++ b/src/content/content.ts @@ -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( + { + path: `${url}/root`, + }, + ); + + return rootContent; + } + private async getUrlForSlugAndUsername({ slug, username }: GetContentParams) { if (username) { return `${TABNEWS_ENDPOINTS.content}/${username}/${slug}`;