Save and load notes in Zeichen using an external API.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

index.ts 1.5 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { Plugin, StoragePlugin } from '../../core/src/plugin'
  2. import Storage from './Storage'
  3. type PluginConfig = {
  4. baseUrl: string,
  5. idAttribute?: string,
  6. }
  7. const AVAILABLE_COLLECTIONS = ['notes', 'folders'] as const
  8. const RemoteStoragePlugin: Plugin<PluginConfig> = ({
  9. baseUrl,
  10. idAttribute = 'id',
  11. }) => class RemoteStorage implements StoragePlugin {
  12. private readonly storageMap: Record<string, Storage>
  13. public static readonly type: 'storage'
  14. constructor({ currentUserId, }) {
  15. this.storageMap = AVAILABLE_COLLECTIONS.reduce(
  16. (theStorages, collectionId) => ({
  17. ...theStorages,
  18. [collectionId]: new Storage(currentUserId, baseUrl, collectionId)
  19. }),
  20. {}
  21. )
  22. }
  23. async save(collectionId, item) {
  24. const { [collectionId]: storage = null } = this.storageMap
  25. if (storage === null) {
  26. throw new Error(`Invalid collection "${collectionId}"`)
  27. }
  28. return storage.saveItem(item)
  29. }
  30. async load(collectionId, itemId) {
  31. const { [collectionId]: storage = null } = this.storageMap
  32. if (storage === null) {
  33. throw new Error(`Invalid collection "${collectionId}"`)
  34. }
  35. const items = await storage.queryItems()
  36. if (!itemId) {
  37. return items
  38. }
  39. return items.filter(i => i[idAttribute] === itemId)
  40. }
  41. async remove(collectionId, item) {
  42. const { [collectionId]: storage = null } = this.storageMap
  43. if (storage === null) {
  44. throw new Error(`Invalid collection "${collectionId}"`)
  45. }
  46. return storage.deleteItem(item)
  47. }
  48. }
  49. export default RemoteStoragePlugin