diff --git a/docs/api/cozy-stack-client.md b/docs/api/cozy-stack-client.md index 6b0f04f2d..a95ec2aee 100644 --- a/docs/api/cozy-stack-client.md +++ b/docs/api/cozy-stack-client.md @@ -1181,12 +1181,25 @@ Implements `DocumentCollection` API to interact with the /notes endpoint of the **Kind**: global class * [NotesCollection](#NotesCollection) + * [.get(id)](#NotesCollection+get) ⇒ Object * [.all()](#NotesCollection+all) ⇒ Object * [.destroy(note)](#NotesCollection+destroy) ⇒ Object * [.create(options)](#NotesCollection+create) ⇒ Object * [.fetchURL(note)](#NotesCollection+fetchURL) ⇒ Object * [.getDefaultSchema()](#NotesCollection+getDefaultSchema) ⇒ object + + +### notesCollection.get(id) ⇒ Object +Fetches the note data + +**Kind**: instance method of [NotesCollection](#NotesCollection) +**Returns**: Object - Information about the note + +| Param | Type | Description | +| --- | --- | --- | +| id | string | Note id | + ### notesCollection.all() ⇒ Object diff --git a/packages/cozy-stack-client/src/NotesCollection.js b/packages/cozy-stack-client/src/NotesCollection.js index c279f0a3b..6b536b233 100644 --- a/packages/cozy-stack-client/src/NotesCollection.js +++ b/packages/cozy-stack-client/src/NotesCollection.js @@ -23,6 +23,20 @@ class NotesCollection extends DocumentCollection { super(NOTES_DOCTYPE, stackClient) } + /** + * Fetches the note data + * + * @param {string} id Note id + * @returns {{data}} Information about the note + */ + async get(id) { + const resp = await this.stackClient.fetchJSON('GET', `/notes/${id}`) + + return { + data: normalizeNote(resp.data) + } + } + /** * Fetches all notes * diff --git a/packages/cozy-stack-client/src/NotesCollection.spec.js b/packages/cozy-stack-client/src/NotesCollection.spec.js index 2903ab017..78ea05a18 100644 --- a/packages/cozy-stack-client/src/NotesCollection.spec.js +++ b/packages/cozy-stack-client/src/NotesCollection.spec.js @@ -17,6 +17,37 @@ describe('NotesCollection', () => { jest.restoreAllMocks() }) + describe('get', () => { + const { stackClient, collection } = setup() + + it('should call the appropriate route', async () => { + jest.spyOn(stackClient, 'fetchJSON').mockResolvedValue({ + data: [], + links: {}, + meta: { count: 0 } + }) + + await collection.get('id123') + expect(stackClient.fetchJSON).toHaveBeenCalledWith('GET', '/notes/id123') + }) + + it('should normalize document', async () => { + jest.spyOn(stackClient, 'fetchJSON').mockResolvedValue({ + data: { _id: '1', attributes: { title: 'Test Note' } }, + links: {}, + meta: { count: 0 } + }) + const result = await collection.get('id123') + expect(result.data).toEqual({ + _id: '1', + _type: 'io.cozy.notes', + id: '1', + title: 'Test Note', + attributes: { title: 'Test Note' } + }) + }) + }) + describe('all', () => { const { stackClient, collection } = setup()