From 0bee9ae6da29e7da5ac7468b0fa90247776e460e Mon Sep 17 00:00:00 2001 From: Ldoppea Date: Thu, 19 Sep 2024 14:58:07 +0200 Subject: [PATCH] feat: Handle queries that fetch Konnectors by channel `fetchJSON()` calls are not compatible with offline mode unless we wrap them in the `.query()` dsl Recently we added a `fetchJSON()` call inside of cozy-home in order to display the new grouped-by-category folders in the home's konnectors section So we want to move this call inside of the AppsRegistryCollection in order to make it compatible with offline mode Related PR: cozy/cozy-home#2186 --- packages/cozy-client/src/CozyClient.js | 2 +- .../src/AppsRegistryCollection.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/cozy-client/src/CozyClient.js b/packages/cozy-client/src/CozyClient.js index 243cd28fc7..11669ec522 100644 --- a/packages/cozy-client/src/CozyClient.js +++ b/packages/cozy-client/src/CozyClient.js @@ -1126,7 +1126,7 @@ client.query(Q('io.cozy.bills'))`) return await this.persistVirtualDocument( { _type: 'io.cozy.apps_registry', - _id: 'maintenance', + _id: definition.id, // @ts-ignore cozyPouchData: data }, diff --git a/packages/cozy-stack-client/src/AppsRegistryCollection.js b/packages/cozy-stack-client/src/AppsRegistryCollection.js index 1967a14854..a34da894b9 100644 --- a/packages/cozy-stack-client/src/AppsRegistryCollection.js +++ b/packages/cozy-stack-client/src/AppsRegistryCollection.js @@ -27,6 +27,16 @@ export const normalizeAppFromRegistry = (data, doctype) => { } } +const fetchKonnectorsByChannel = async (channel, doctype, stackClient) => { + const resp = await stackClient.fetchJSON( + 'GET', + `/registry?versionsChannel=${channel}&filter[type]=konnector&limit=500` + ) + return { + data: resp.data.map(data => normalizeAppFromRegistry(data, doctype)) + } +} + /** * Extends `DocumentCollection` API along with specific methods for `io.cozy.apps_registry`. */ @@ -44,6 +54,12 @@ class AppsRegistryCollection extends DocumentCollection { * @throws {FetchError} */ async get(slug) { + if (slug.startsWith('konnectors/')) { + const channel = slug.split('/')[1] + + return fetchKonnectorsByChannel(channel, this.doctype, this.stackClient) + } + const resp = await this.stackClient.fetchJSON( 'GET', `${this.endpoint}${slug}`