Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add type to path #870

Merged
merged 3 commits into from
Feb 8, 2021
Merged

fix: Add type to path #870

merged 3 commits into from
Feb 8, 2021

Conversation

flohhhh
Copy link
Contributor

@flohhhh flohhhh commented Feb 8, 2021

When query definition had a type, /data/io.cozy.triggers was fetched
instead of '/jobs/triggers'.

When query definition had a type, /data/io.cozy.triggers was fetched
instead of '/jobs/triggers'.
@@ -107,9 +107,9 @@ class TriggerCollection extends DocumentCollection {
* @throws {FetchError}
*/
async find(selector = {}, options = {}) {
if (Object.keys(selector).length === 1 && selector.worker) {
if (Object.keys(selector).length > 0 && selector.worker && selector.type) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is a breaking change, and I'm not sure we really want to force the use of the type. In some case, it is wanted to not select on the type.

We should handle those cases:

  • only worker
  • only type
  • both of them

WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Moreover the stack implements a way to filter through multiple types (/jobs/triggers?Type=@Cron,@webhook). Since Mango already supports this via $in, I propose that we support:

Q('io.cozy.triggers').where({
  type: {
    $in: ['@cron', '@webhook']
  }
})

Q('io.cozy.triggers').where({
  type: '@cron'
})

Q('io.cozy.triggers').where({
  type: '@webhook'
})

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @nono, are there other APIs from the stack that have the same filtering ability ? Would it make sense for those APIs to support the mango selector so that it is not done client-side ? It would make the client-side simpler.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there other APIs from the stack that have the same filtering ability ?

I think we can find 2 or 3 endpoints with filtering like that. Scanning the full docs should answer that.

Would it make sense for those APIs to support the mango selector so that it is not done client-side ?

Meh, I don't like that. It means creating a new endpoint with POST, and parsing the mango selectors, and making a response compatible with the expected format. And it is probably a good way to have silent errors (like putting a limit in the mango filter that will be ignored).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made changes taking into account your comments

} else {
url += `Type=${type}`
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the URLSearchParams API be used instead of building the params manually ?

https://developer.mozilla.org/fr/docs/Web/API/URLSearchParams

sp = new URLSearchParams()
URLSearchParams {  }

sp.append('Worker', 'toto')
sp.toString()
"Worker=toto"
sp.append('Type', '@cron')
sp.toString()
"Worker=toto&Type=%40cron"

@nono Will the "%40" be correctly parsed by the stack ? I see that in the doc, the "@" is not url-encoded. cf https://github.com/cozy/cozy-stack/pull/2884/files

const selectorLen = Object.keys(selector).length
const { worker, type } = selector
const hasOneKey = selectorLen === 1
const hasWorkerAndType = worker && type && selectorLen === 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmh, I think we could simplify a bit the logic:

const { worker, type, ...rest } = selector
const hasOnlyWorkerAndType = Object.keys(rest).length === 0
if (hasOnlyWorkerAndType) {
  // use the /jobs/triggers URL.
}

Copy link
Contributor

@ptbrowne ptbrowne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer if the URLSearchParams could be used since it is more robust than manually joining parameters via & but I am not sure that the stack will parse url-encoded "@".

LGTM.

@flohhhh flohhhh force-pushed the fix-trigger-collections branch 3 times, most recently from ffe5c09 to 0c32776 Compare February 8, 2021 16:48
@flohhhh flohhhh force-pushed the fix-trigger-collections branch from 0c32776 to f7d7d55 Compare February 8, 2021 16:49
@ptbrowne
Copy link
Contributor

ptbrowne commented Feb 8, 2021

Thanks for the changes, LGTM, it seems that the PR is red because of linting issues.

`/jobs/trigger`is called in some conditions:
if there is only type or worker
if there are type and worker
@flohhhh flohhhh force-pushed the fix-trigger-collections branch from f7d7d55 to 3ea6132 Compare February 8, 2021 17:08
@flohhhh flohhhh merged commit a73470c into master Feb 8, 2021
@flohhhh flohhhh deleted the fix-trigger-collections branch February 8, 2021 17:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants