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

feat: Add instance data in loadInstanceOptionsFromStack #1380

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/api/cozy-client/classes/CozyClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ loadInstanceOptionsFromDOM - Loads the dataset injected by the Stack in web page

loadInstanceOptionsFromStack - Loads the instance options from cozy-stack and exposes it through getInstanceOptions

For now only retrieving capabilities is supported
This method is not iso with loadInstanceOptionsFromDOM for now.

*Returns*

Expand Down Expand Up @@ -1604,7 +1604,7 @@ Saves multiple documents in one batch

*Defined in*

[packages/cozy-client/src/CozyClient.js:1729](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/CozyClient.js#L1729)
[packages/cozy-client/src/CozyClient.js:1735](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/CozyClient.js#L1735)

***

Expand All @@ -1628,7 +1628,7 @@ set some data in the store.

*Defined in*

[packages/cozy-client/src/CozyClient.js:1702](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/CozyClient.js#L1702)
[packages/cozy-client/src/CozyClient.js:1708](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/CozyClient.js#L1708)

***

Expand All @@ -1652,7 +1652,7 @@ At any time put an error function

*Defined in*

[packages/cozy-client/src/CozyClient.js:1715](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/CozyClient.js#L1715)
[packages/cozy-client/src/CozyClient.js:1721](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/CozyClient.js#L1721)

***

Expand Down Expand Up @@ -1728,7 +1728,7 @@ Contains the fetched token and the client information. These should be stored an

*Defined in*

[packages/cozy-client/src/CozyClient.js:1722](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/CozyClient.js#L1722)
[packages/cozy-client/src/CozyClient.js:1728](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/CozyClient.js#L1728)

***

Expand Down
10 changes: 8 additions & 2 deletions packages/cozy-client/src/CozyClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -1676,7 +1676,7 @@ instantiation of the client.`
/**
* loadInstanceOptionsFromStack - Loads the instance options from cozy-stack and exposes it through getInstanceOptions
*
* For now only retrieving capabilities is supported
* This method is not iso with loadInstanceOptionsFromDOM for now.
*
* @returns {Promise<void>}
*/
Expand All @@ -1685,8 +1685,14 @@ instantiation of the client.`
Q('io.cozy.settings').getById('io.cozy.settings.capabilities')
)

const { data: instanceData } = await this.query(
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be getOrFechFromState (I don’t know the name exactly) and you should add a fetchPolicy

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated. It should be always fresh so no fetchPolicy no?

Copy link
Contributor

Choose a reason for hiding this comment

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

@zatteo I think using fetchQueryAndGetFromState as proposed by @Crash-- is only useful if we add a fetchPolicy. That being said, I'm not sure we want this kind of logic at this level, I don't think I like the idea of a forced fetchPolicy that could lead to unexpected results from the caller.
In any case, the behaviour should be the same for the other query just above on io.cozy.settings.capabilities.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So :

  • no fetchPolicy because we need it fresh
  • same method for the two calls : decided to stay on query for the moment

Q('io.cozy.settings').getById('io.cozy.settings.instance')
)

this.instanceOptions = {
capabilities: data.attributes
capabilities: data.attributes,
locale: instanceData.attributes?.locale,
tracking: instanceData.attributes?.tracking
}

this.capabilities = this.instanceOptions.capabilities || null
Expand Down
44 changes: 32 additions & 12 deletions packages/cozy-client/src/CozyClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,14 @@ describe('CozyClient logout', () => {
})

it('should call reset on each link that can be reset', async () => {
client.query = jest.fn().mockResolvedValueOnce({
data: { attributes: { file_versioning: true, flat_subdomains: true } }
})
client.query = jest
.fn()
.mockResolvedValueOnce({
data: { attributes: { file_versioning: true, flat_subdomains: true } }
})
.mockResolvedValueOnce({
data: { attributes: { locale: 'fr', tracking: true } }
})
links[0].reset = jest.fn()
links[2].reset = jest.fn()
await client.login()
Expand All @@ -512,9 +517,14 @@ describe('CozyClient logout', () => {
})

it('should call all reset even if a reset throws an error', async () => {
client.query = jest.fn().mockResolvedValueOnce({
data: { attributes: { file_versioning: true, flat_subdomains: true } }
})
client.query = jest
.fn()
.mockResolvedValueOnce({
data: { attributes: { file_versioning: true, flat_subdomains: true } }
})
.mockResolvedValueOnce({
data: { attributes: { locale: 'fr', tracking: true } }
})
links[0].reset = jest.fn().mockRejectedValue(new Error('Async error'))
links[2].reset = jest.fn()
await client.login()
Expand All @@ -524,9 +534,14 @@ describe('CozyClient logout', () => {
})

it('should emit events', async () => {
client.query = jest.fn().mockResolvedValueOnce({
data: { attributes: { file_versioning: true, flat_subdomains: true } }
})
client.query = jest
.fn()
.mockResolvedValueOnce({
data: { attributes: { file_versioning: true, flat_subdomains: true } }
})
.mockResolvedValueOnce({
data: { attributes: { locale: 'fr', tracking: true } }
})
const originalLogout = client.logout
links[0].reset = jest.fn()
links[2].reset = jest.fn()
Expand All @@ -545,9 +560,14 @@ describe('CozyClient logout', () => {
})

it('should unregister an oauth client', async () => {
client.query = jest.fn().mockResolvedValueOnce({
data: { attributes: { file_versioning: true, flat_subdomains: true } }
})
client.query = jest
.fn()
.mockResolvedValueOnce({
data: { attributes: { file_versioning: true, flat_subdomains: true } }
})
.mockResolvedValueOnce({
data: { attributes: { locale: 'fr', tracking: true } }
})
await client.login()
stackClient.isRegistered.mockReturnValue(true)

Expand Down
2 changes: 1 addition & 1 deletion packages/cozy-client/types/CozyClient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ declare class CozyClient {
/**
* loadInstanceOptionsFromStack - Loads the instance options from cozy-stack and exposes it through getInstanceOptions
*
* For now only retrieving capabilities is supported
* This method is not iso with loadInstanceOptionsFromDOM for now.
*
* @returns {Promise<void>}
*/
Expand Down
Loading