Skip to content

Commit 9de781f

Browse files
CopilotYukaii
andauthored
Support title/tag metadata updates in note PATCH APIs (#49)
Agent-Logs-Url: https://github.com/hackmdio/api-client/sessions/782df678-b097-4b7e-b0c7-451b5be8bbb7 Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Yukaii <4230968+Yukaii@users.noreply.github.com>
1 parent 8750936 commit 9de781f

4 files changed

Lines changed: 74 additions & 4 deletions

File tree

nodejs/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios, { AxiosInstance, AxiosError, AxiosResponse, InternalAxiosRequestConfig } from 'axios'
2-
import { CreateNoteOptions, GetMe, GetUserHistory, GetUserNotes, GetUserNote, CreateUserNote, GetUserTeams, GetTeamNotes, CreateTeamNote, SingleNote } from './type'
2+
import { CreateNoteOptions, GetMe, GetUserHistory, GetUserNotes, GetUserNote, CreateUserNote, GetUserTeams, GetTeamNotes, CreateTeamNote, SingleNote, UpdateNoteOptions } from './type'
33
import * as HackMDErrors from './error'
44

55
export type RequestOptions = {
@@ -165,7 +165,7 @@ export class API {
165165
return this.unwrapData(this.axios.patch<SingleNote>(`notes/${noteId}`, { content }), options.unwrapData, true) as unknown as OptionReturnType<Opt, SingleNote>
166166
}
167167

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

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

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

nodejs/src/type.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ export type SingleNote = Note & {
8585
content: string
8686
}
8787

88+
export type UpdateNoteOptions = Partial<Pick<SingleNote, 'content' | 'title' | 'tags' | 'readPermission' | 'writePermission' | 'permalink'>>
89+
8890
// User
8991
export type GetMe = User
9092

@@ -105,4 +107,3 @@ export type CreateTeamNote = SingleNote
105107
export type UpdateTeamNote = void
106108
export type DeleteTeamNote = void
107109

108-

nodejs/tests/api.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,33 @@ test('should throw HackMD error object', async () => {
9898
expect(error).toHaveProperty('resetAfter')
9999
}
100100
})
101+
102+
test('should support updating team note title and tags metadata', async () => {
103+
const updatedTags = ['team', 'metadata']
104+
let requestBody: unknown
105+
106+
server.use(
107+
http.patch('https://api.hackmd.io/v1/teams/test-team/notes/test-note-id', async ({ request }) => {
108+
requestBody = await request.json()
109+
110+
return HttpResponse.json(
111+
{
112+
id: 'test-note-id',
113+
title: 'Updated Team Note',
114+
tags: updatedTags
115+
}
116+
)
117+
})
118+
)
119+
120+
const response = await client.updateTeamNote('test-team', 'test-note-id', {
121+
title: 'Updated Team Note',
122+
tags: updatedTags
123+
})
124+
125+
expect(requestBody).toEqual({
126+
title: 'Updated Team Note',
127+
tags: updatedTags
128+
})
129+
expect(response).toHaveProperty('status', 200)
130+
})

nodejs/tests/etag.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,5 +268,44 @@ describe('Etag support', () => {
268268
expect(response).toHaveProperty('title', 'Updated Test Note')
269269
expect(response).toHaveProperty('content', 'Updated content via updateNote')
270270
})
271+
272+
test('should support updating note title and tags metadata', async () => {
273+
const mockEtag = 'W/"metadata-etag"'
274+
const updatedTags = ['api', 'metadata']
275+
let requestBody: unknown
276+
277+
server.use(
278+
http.patch('https://api.hackmd.io/v1/notes/test-note-id', async ({ request }) => {
279+
requestBody = await request.json()
280+
281+
return HttpResponse.json(
282+
{
283+
id: 'test-note-id',
284+
title: 'Updated Metadata Title',
285+
tags: updatedTags,
286+
content: 'Updated content via updateNote'
287+
},
288+
{
289+
headers: {
290+
'ETag': mockEtag
291+
}
292+
}
293+
)
294+
})
295+
)
296+
297+
const response = await client.updateNote('test-note-id', {
298+
title: 'Updated Metadata Title',
299+
tags: updatedTags
300+
})
301+
302+
expect(requestBody).toEqual({
303+
title: 'Updated Metadata Title',
304+
tags: updatedTags
305+
})
306+
expect(response).toHaveProperty('etag', mockEtag)
307+
expect(response).toHaveProperty('title', 'Updated Metadata Title')
308+
expect(response.tags).toEqual(updatedTags)
309+
})
271310
})
272311
})

0 commit comments

Comments
 (0)