Last active
June 2, 2023 15:32
-
-
Save connebs/10ce24204e0d39bc5dc03e1f411079d4 to your computer and use it in GitHub Desktop.
Demonstrate creating cards (simply), finding them again (using filtered select), and then modifying them using the Supernotes API.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // please note that this script contains limited error / success checking, | |
| // and a production script should probably have more (e.g. checking the | |
| // status of requests before you try to do things with the data) | |
| const baseUrl = 'https://api.supernotes.app/v1' | |
| const apiKey = 'MY_API_KEY' | |
| const headers = new Headers({ | |
| 'Api-Key': apiKey, | |
| 'Content-Type': 'application/json', | |
| }) | |
| const MY_SPECIAL_TAG = 'special tag' | |
| // create two cards with `MY_SPECIAL_TAG` tags, don't bother storing IDs for now | |
| // create first card | |
| await fetch(`${baseUrl}/cards/simple`, { | |
| headers, | |
| method: 'POST', | |
| body: JSON.stringify({ | |
| name: 'First card', | |
| markup: 'Some **markdown** content.', | |
| tags: [MY_SPECIAL_TAG], | |
| }), | |
| }) | |
| // make second card | |
| await fetch(`${baseUrl}/cards/simple`, { | |
| headers, | |
| method: 'POST', | |
| body: JSON.stringify({ | |
| name: 'Second card', | |
| markup: 'More markdown !!content!!.', | |
| tags: [MY_SPECIAL_TAG], | |
| }), | |
| }) | |
| // now get all cards with my tag | |
| const filteredSelect = { | |
| filter_group: { | |
| operator: 'and', | |
| filters: [ | |
| { | |
| type: 'tag', | |
| operator: 'contains', | |
| arg: MY_SPECIAL_TAG, | |
| }, | |
| ], | |
| } | |
| } | |
| const response = await fetch(`${baseUrl}/cards/get/select`, { | |
| headers, | |
| method: 'POST', | |
| body: JSON.stringify(filteredSelect), | |
| }) | |
| const foundCardIds = Object.keys(await response.json()); | |
| // send a request to change all these cards to have a different tag set | |
| const ADDITIONAL_TAG = "second" | |
| const modificationPayload = {}; | |
| foundCardIds.forEach(cardId => { | |
| modificationPayload[cardId] = { data: { tags: [MY_SPECIAL_TAG, ADDITIONAL_TAG] } } | |
| }) | |
| await fetch(`${baseUrl}/cards/`, { | |
| headers, | |
| method: 'PATCH', | |
| body: JSON.stringify(modificationPayload), | |
| }) | |
| // get the count of cards with the second tag to ensure we did everything right | |
| const filteredSelect2 = { | |
| filter_group: { | |
| operator: 'and', | |
| filters: [ | |
| { | |
| type: 'tag', | |
| operator: 'contains', | |
| arg: ADDITIONAL_TAG, | |
| }, | |
| ], | |
| } | |
| } | |
| const response2 = await fetch(`${baseUrl}/cards/get/select`, { | |
| headers, | |
| method: 'POST', | |
| body: JSON.stringify(filteredSelect2), | |
| }) | |
| if (response.ok) { | |
| console.log(Object.keys(await response2.json()).length) | |
| // should be `2` | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment