Skip to content

Commit

Permalink
feat: The query function accepts a queryDefinition param as a function
Browse files Browse the repository at this point in the history
Solve this issue #1367
  • Loading branch information
Merkur39 committed Feb 29, 2024
1 parent f9d1a76 commit d72514f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
19 changes: 11 additions & 8 deletions packages/cozy-client/src/CozyClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ const securiseUri = uri => {

return uri
}
const resolveToValue = fnOrValue => {
return typeof fnOrValue === 'function' ? fnOrValue() : fnOrValue
}

const DOC_CREATION = 'creation'
const DOC_UPDATE = 'update'
Expand Down Expand Up @@ -905,20 +908,20 @@ client.query(Q('io.cozy.bills'))`)
* `getQueryFromState` or directly using `<Query />`. `<Query />` automatically
* executes its query when mounted if no fetch policy has been indicated.
*
* @param {QueryDefinition} queryDefinition - Definition that will be executed
* @param {QueryDefinition|(() => QueryDefinition)} queryDefinition - Definition that will be executed
* @param {import("./types").QueryOptions} [options] - Options
* @returns {Promise<import("./types").QueryResult>}
*/
async query(queryDefinition, { update, ...options } = {}) {
this.ensureStore()
const queryId =
options.as || this.queryIdGenerator.generateId(queryDefinition)
const definition = resolveToValue(queryDefinition)
const queryId = options.as || this.queryIdGenerator.generateId(definition)
const existingQuery = this.getQueryFromState(queryId, options)
if (options.enabled !== undefined) {
if ('boolean' !== typeof options.enabled) {
throw new Error(
`option.enabled should be a boolean for this query: ${JSON.stringify(
queryDefinition
definition
)}`
)
}
Expand All @@ -939,10 +942,10 @@ client.query(Q('io.cozy.bills'))`)
// have in the promiseCache
if (existingQuery && Object.keys(existingQuery).length > 0) {
if (existingQuery.fetchStatus === 'loading') {
return this._promiseCache.get(() => stringify(queryDefinition))
return this._promiseCache.get(() => stringify(definition))
}
}
this.ensureQueryExists(queryId, queryDefinition, options)
this.ensureQueryExists(queryId, definition, options)

try {
const backgroundFetching =
Expand All @@ -952,8 +955,8 @@ client.query(Q('io.cozy.bills'))`)
this.dispatch(loadQuery(queryId, { backgroundFetching }))

const response = await this._promiseCache.exec(
() => this.requestQuery(queryDefinition),
() => stringify(queryDefinition)
() => this.requestQuery(definition),
() => stringify(definition)
)

this.dispatch(
Expand Down
6 changes: 6 additions & 0 deletions packages/cozy-client/src/CozyClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,12 @@ describe('CozyClient', () => {
query = Q('io.cozy.todos')
fakeResponse = { data: 'FAKE!!!' }
})
it('should pass queryDefinion function', async () => {
requestHandler.mockResolvedValueOnce(fakeResponse)
await client.query(query)
expect(requestHandler).toHaveBeenCalledTimes(1)
expect(requestHandler.mock.calls[0][0]).toBe(query)
})
it('should throw an error if the option.enabled is not a boolean', async () => {
await expect(
client.query(query, { as: 'allTodos', enabled: '' })
Expand Down

0 comments on commit d72514f

Please sign in to comment.