Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions nodejs/src/index.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -165,7 +165,7 @@ export class API {
return this.unwrapData(this.axios.patch<SingleNote>(`notes/${noteId}`, { content }), options.unwrapData, true) as unknown as OptionReturnType<Opt, SingleNote>
}

async updateNote<Opt extends RequestOptions> (noteId: string, payload: Partial<Pick<SingleNote, 'content' | 'readPermission' | 'writePermission' | 'permalink'>>, options = defaultOption as Opt): Promise<OptionReturnType<Opt, SingleNote>> {
async updateNote<Opt extends RequestOptions> (noteId: string, payload: UpdateNoteOptions, options = defaultOption as Opt): Promise<OptionReturnType<Opt, SingleNote>> {
return this.unwrapData(this.axios.patch<SingleNote>(`notes/${noteId}`, payload), options.unwrapData, true) as unknown as OptionReturnType<Opt, SingleNote>
}

Expand All @@ -189,7 +189,7 @@ export class API {
return this.axios.patch<AxiosResponse>(`teams/${teamPath}/notes/${noteId}`, { content })
}

async updateTeamNote (teamPath: string, noteId: string, options: Partial<Pick<SingleNote, 'content' | 'readPermission' | 'writePermission' | 'permalink'>>): Promise<AxiosResponse> {
async updateTeamNote (teamPath: string, noteId: string, options: UpdateNoteOptions): Promise<AxiosResponse> {
return this.axios.patch<AxiosResponse>(`teams/${teamPath}/notes/${noteId}`, options)
}

Expand Down
3 changes: 2 additions & 1 deletion nodejs/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ export type SingleNote = Note & {
content: string
}

export type UpdateNoteOptions = Partial<Pick<SingleNote, 'content' | 'title' | 'tags' | 'readPermission' | 'writePermission' | 'permalink'>>

// User
export type GetMe = User

Expand All @@ -105,4 +107,3 @@ export type CreateTeamNote = SingleNote
export type UpdateTeamNote = void
export type DeleteTeamNote = void


30 changes: 30 additions & 0 deletions nodejs/tests/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
39 changes: 39 additions & 0 deletions nodejs/tests/etag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
Loading