Skip to content

Commit

Permalink
feat: Handle queries that fetch Konnectors by channel
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
Ldoppea committed Sep 24, 2024
1 parent 31f4951 commit 0bee9ae
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/cozy-client/src/CozyClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
16 changes: 16 additions & 0 deletions packages/cozy-stack-client/src/AppsRegistryCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
*/
Expand All @@ -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}`
Expand Down

0 comments on commit 0bee9ae

Please sign in to comment.