diff --git a/nodejs/src/index.ts b/nodejs/src/index.ts index e625ee5..f816370 100644 --- a/nodejs/src/index.ts +++ b/nodejs/src/index.ts @@ -1,5 +1,5 @@ import axios, { AxiosInstance, AxiosError, AxiosResponse, InternalAxiosRequestConfig } from 'axios' -import { CreateNoteOptions, GetMe, GetUserHistory, GetUserNotes, GetUserNote, CreateUserNote, GetUserTeams, GetTeamNotes, CreateTeamNote, SingleNote } from './type' +import { CreateNoteOptions, GetMe, GetUserHistory, GetUserNotes, GetUserNote, CreateUserNote, GetUserTeams, GetTeamNotes, CreateTeamNote, SingleNote, UpdateNoteOptions } from './type' import * as HackMDErrors from './error' export type RequestOptions = { @@ -165,7 +165,7 @@ export class API { return this.unwrapData(this.axios.patch(`notes/${noteId}`, { content }), options.unwrapData, true) as unknown as OptionReturnType } - async updateNote (noteId: string, payload: Partial>, options = defaultOption as Opt): Promise> { + async updateNote (noteId: string, payload: UpdateNoteOptions, options = defaultOption as Opt): Promise> { return this.unwrapData(this.axios.patch(`notes/${noteId}`, payload), options.unwrapData, true) as unknown as OptionReturnType } @@ -189,7 +189,7 @@ export class API { return this.axios.patch(`teams/${teamPath}/notes/${noteId}`, { content }) } - async updateTeamNote (teamPath: string, noteId: string, options: Partial>): Promise { + async updateTeamNote (teamPath: string, noteId: string, options: UpdateNoteOptions): Promise { return this.axios.patch(`teams/${teamPath}/notes/${noteId}`, options) } diff --git a/nodejs/src/type.ts b/nodejs/src/type.ts index 766b292..76b24c9 100644 --- a/nodejs/src/type.ts +++ b/nodejs/src/type.ts @@ -85,6 +85,8 @@ export type SingleNote = Note & { content: string } +export type UpdateNoteOptions = Partial> + // User export type GetMe = User @@ -105,4 +107,3 @@ export type CreateTeamNote = SingleNote export type UpdateTeamNote = void export type DeleteTeamNote = void - diff --git a/nodejs/tests/api.spec.ts b/nodejs/tests/api.spec.ts index 74a9aaf..2ba704b 100644 --- a/nodejs/tests/api.spec.ts +++ b/nodejs/tests/api.spec.ts @@ -98,3 +98,33 @@ test('should throw HackMD error object', async () => { expect(error).toHaveProperty('resetAfter') } }) + +test('should support updating team note title and tags metadata', async () => { + const updatedTags = ['team', 'metadata'] + let requestBody: unknown + + server.use( + http.patch('https://api.hackmd.io/v1/teams/test-team/notes/test-note-id', async ({ request }) => { + requestBody = await request.json() + + return HttpResponse.json( + { + id: 'test-note-id', + title: 'Updated Team Note', + tags: updatedTags + } + ) + }) + ) + + const response = await client.updateTeamNote('test-team', 'test-note-id', { + title: 'Updated Team Note', + tags: updatedTags + }) + + expect(requestBody).toEqual({ + title: 'Updated Team Note', + tags: updatedTags + }) + expect(response).toHaveProperty('status', 200) +}) diff --git a/nodejs/tests/etag.spec.ts b/nodejs/tests/etag.spec.ts index 1da5788..ce0d2ae 100644 --- a/nodejs/tests/etag.spec.ts +++ b/nodejs/tests/etag.spec.ts @@ -268,5 +268,44 @@ describe('Etag support', () => { expect(response).toHaveProperty('title', 'Updated Test Note') expect(response).toHaveProperty('content', 'Updated content via updateNote') }) + + test('should support updating note title and tags metadata', async () => { + const mockEtag = 'W/"metadata-etag"' + const updatedTags = ['api', 'metadata'] + let requestBody: unknown + + server.use( + http.patch('https://api.hackmd.io/v1/notes/test-note-id', async ({ request }) => { + requestBody = await request.json() + + return HttpResponse.json( + { + id: 'test-note-id', + title: 'Updated Metadata Title', + tags: updatedTags, + content: 'Updated content via updateNote' + }, + { + headers: { + 'ETag': mockEtag + } + } + ) + }) + ) + + const response = await client.updateNote('test-note-id', { + title: 'Updated Metadata Title', + tags: updatedTags + }) + + expect(requestBody).toEqual({ + title: 'Updated Metadata Title', + tags: updatedTags + }) + expect(response).toHaveProperty('etag', mockEtag) + expect(response).toHaveProperty('title', 'Updated Metadata Title') + expect(response.tags).toEqual(updatedTags) + }) }) })