diff --git a/README.md b/README.md index e6db587..c14579a 100644 --- a/README.md +++ b/README.md @@ -213,25 +213,16 @@ const { pagination, contents } = await tabNews.contents.getMy({ }); ``` -**Criar Conteúdo** - -Na rota de criação de conteúdos, todos os campos são opcionais exceto o `body`, -que é o seu post ou comentario, e o `title` que é opcional apenas quando há um `parent_id` +**Buscar Detalhe do Conteúdo** ```js import { TabNews } from 'tabnews-sdk'; const tabNews = new TabNews(); -await tabNews.session.create(); - -const response = await tabNews.contents.create({ - parent_id: undefined, - slug: 'e-opcional', - title: 'test', - body: 'test', - status: 'published', - source_url: 'https://google.com', +const content = await tabNews.contents.getBySlug({ + slug: '', + username: '', }); ``` @@ -240,25 +231,23 @@ import { TabNews } from 'tabnews-sdk'; const tabNews = new TabNews(); -const { contents } = await tabNews.contents.getAll(); - await tabNews.session.create(); -const response = await tabNews.contents.create({ - parent_id: contents[0].id, - body: 'comentando em um conteúdo', - status: 'published', +// Não é preciso passar o username pois internamente a bliblioteca ira realizar o fecth do usuario atual + +const content = await tabNews.contents.getBySlug({ + slug: '', }); ``` -**Buscar Detalhe do Conteúdo** +**Buscar Comentarios do Conteúdo** ```js import { TabNews } from 'tabnews-sdk'; const tabNews = new TabNews(); -const content = await tabNews.contents.getBySlug({ +const contentChildren = await tabNews.contents.getChildren({ slug: '', username: '', }); @@ -273,11 +262,49 @@ await tabNews.session.create(); // Não é preciso passar o username pois internamente a bliblioteca ira realizar o fecth do usuario atual -const content = await tabNews.contents.getBySlug({ +const contentChildren = await tabNews.contents.getChildren({ slug: '', }); ``` +**Criar Conteúdo** + +Na rota de criação de conteúdos, todos os campos são opcionais exceto o `body`, +que é o seu post ou comentario, e o `title` que é opcional apenas quando há um `parent_id` + +```js +import { TabNews } from 'tabnews-sdk'; + +const tabNews = new TabNews(); + +await tabNews.session.create(); + +const response = await tabNews.contents.create({ + parent_id: undefined, + slug: 'e-opcional', + title: 'test', + body: 'test', + status: 'published', + source_url: 'https://google.com', +}); +``` + +```js +import { TabNews } from 'tabnews-sdk'; + +const tabNews = new TabNews(); + +const { contents } = await tabNews.contents.getAll(); + +await tabNews.session.create(); + +const response = await tabNews.contents.create({ + parent_id: contents[0].id, + body: 'comentando em um conteúdo', + status: 'published', +}); +``` + **Atualizar Conteúdo** ```js diff --git a/src/content/__snapshots__/content.spec.ts.snap b/src/content/__snapshots__/content.spec.ts.snap index 51c594c..3a55fd5 100644 --- a/src/content/__snapshots__/content.spec.ts.snap +++ b/src/content/__snapshots__/content.spec.ts.snap @@ -205,6 +205,68 @@ exports[`Content > get > should get content by slug for current user 1`] = ` } `; +exports[`Content > get > should get content children 1`] = ` +[ + { + "body": "body", + "children": [ + { + "body": "body", + "children": [], + "children_deep_count": 0, + "created_at": 2023-04-02T12:25:34.810Z, + "deleted_at": null, + "id": "id", + "owner_id": "owner_id", + "owner_username": "username", + "parent_id": "parent_id", + "published_at": 2023-04-02T12:25:34.865Z, + "slug": "slug", + "source_url": null, + "status": "published", + "tabcoins": 1, + "title": null, + "updated_at": 2023-04-02T12:25:34.810Z, + }, + ], + "children_deep_count": 1, + "created_at": 2023-04-02T12:25:34.810Z, + "deleted_at": null, + "id": "id", + "owner_id": "owner_id", + "owner_username": "username", + "parent_id": "parent_id", + "published_at": 2023-04-02T12:25:34.865Z, + "slug": "slug", + "source_url": null, + "status": "published", + "tabcoins": 0, + "title": null, + "updated_at": 2023-04-02T12:25:34.810Z, + }, +] +`; + +exports[`Content > get > should get content children 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, @@ -241,6 +303,8 @@ exports[`Content > get > should return correct page when is last page 2`] = ` exports[`Content > get > should throw an error when content not found 1`] = `"O conteúdo informado não foi encontrado no sistema."`; +exports[`Content > get > should throw an error when content of content children 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 5150b19..0cff7e6 100644 --- a/src/content/content.spec.ts +++ b/src/content/content.spec.ts @@ -53,6 +53,46 @@ const contentDetail = { body: 'body', }; +const contentChildren = [ + { + id: 'id', + parent_id: 'parent_id', + owner_id: 'owner_id', + slug: 'slug', + title: null, + body: 'body', + status: 'published', + source_url: null, + published_at: '2023-04-02T12:25:34.865Z', + created_at: '2023-04-02T12:25:34.810Z', + updated_at: '2023-04-02T12:25:34.810Z', + deleted_at: null, + owner_username: 'username', + tabcoins: 0, + children: [ + { + id: 'id', + parent_id: 'parent_id', + owner_id: 'owner_id', + slug: 'slug', + title: null, + body: 'body', + status: 'published', + source_url: null, + published_at: '2023-04-02T12:25:34.865Z', + created_at: '2023-04-02T12:25:34.810Z', + updated_at: '2023-04-02T12:25:34.810Z', + deleted_at: null, + owner_username: 'username', + tabcoins: 1, + children: [], + children_deep_count: 0, + }, + ], + children_deep_count: 1, + }, +]; + describe('Content', () => { const mockContent = (slug: string, user: string = username) => { mockOnceResponse( @@ -85,6 +125,13 @@ describe('Content', () => { ); }; + const mockContentChildren = (slug: string, user: string = username) => { + mockOnceResponse( + `${TABNEWS_ENDPOINTS.content}/${user}/${slug}/children`, + contentChildren, + ); + }; + it('should get all contents and pagination', async () => { mockContents(linkHeader); @@ -255,6 +302,66 @@ describe('Content', () => { }), ).rejects.toThrowErrorMatchingSnapshot(); }); + + it('should get content children', async () => { + const slug = 'slug'; + + mockContentChildren(slug); + + const content = await tabNews.content.getChildren({ + slug, + username, + }); + + expect(content).toMatchSnapshot(); + + const request = mockedRequest(); + + expectRequest(request).method.toBeGet(); + }); + + it('should get content children for current user', async () => { + const slug = 'slug'; + + mockOnceCurrentUser(); + + mockContent(slug); + + const content = await tabNews.content.getBySlug({ + slug, + }); + + expect(content).toMatchSnapshot(); + + const request = mockedRequest(); + + expectRequest(request).method.toBeGet(); + }); + + it('should throw an error when content of content children not found', () => { + const slug = 'slug'; + + mockOnceApiError( + `${TABNEWS_ENDPOINTS.content}/${username}/${slug}/children`, + { + 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.getChildren({ + slug, + username, + }), + ).rejects.toThrowErrorMatchingSnapshot(); + }); }); describe('create', () => { diff --git a/src/content/content.ts b/src/content/content.ts index 418dd48..0a86159 100644 --- a/src/content/content.ts +++ b/src/content/content.ts @@ -104,6 +104,17 @@ export class Content { return content; } + async getChildren(params: GetContentParams) { + const url = await this.getUrlForSlugAndUsername(params); + + const { body: contentChildren } = + await this.tabNews.get({ + path: `${url}/children`, + }); + + return contentChildren; + } + private async getUrlForSlugAndUsername({ slug, username }: GetContentParams) { if (username) { return `${TABNEWS_ENDPOINTS.content}/${username}/${slug}`; diff --git a/src/content/interfaces.ts b/src/content/interfaces.ts index c2af69b..a7708a0 100644 --- a/src/content/interfaces.ts +++ b/src/content/interfaces.ts @@ -62,3 +62,7 @@ export type UpdateContent = Partial< status?: ContentStatus; username?: string; }; + +export interface ContentChildrenResponse extends ContentDetailResponse { + children: ContentChildrenResponse[]; +}