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: Allow to enforce Stack link on request chain #1575

Merged
merged 2 commits into from
Dec 20, 2024
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
3 changes: 2 additions & 1 deletion docs/api/cozy-client/classes/CozyClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -1564,13 +1564,14 @@ Contains the fetched token and the client information.

### requestMutation

▸ **requestMutation**(`definition`): `any`
▸ **requestMutation**(`definition`, `options`): `any`

*Parameters*

| Name | Type |
| :------ | :------ |
| `definition` | `any` |
| `options` | `any` |

*Returns*

Expand Down
9 changes: 5 additions & 4 deletions docs/api/cozy-client/classes/CozyLink.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ Persist the given data into the links storage

*Defined in*

[packages/cozy-client/src/CozyLink.js:31](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/CozyLink.js#L31)
[packages/cozy-client/src/CozyLink.js:32](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/CozyLink.js#L32)

***

### request

**request**(`operation`, `result`, `forward`): `Promise`<`any`>
**request**(`operation`, `options`, `result`, `forward`): `Promise`<`any`>

Request the given operation from the link

Expand All @@ -63,6 +63,7 @@ Request the given operation from the link
| Name | Type | Description |
| :------ | :------ | :------ |
| `operation` | `any` | The operation to request |
| `options` | `any` | The request options |
| `result` | `any` | The result from the previous request of the chain |
| `forward` | `any` | The next request of the chain |

Expand All @@ -72,7 +73,7 @@ Request the given operation from the link

*Defined in*

[packages/cozy-client/src/CozyLink.js:20](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/CozyLink.js#L20)
[packages/cozy-client/src/CozyLink.js:21](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/CozyLink.js#L21)

***

Expand All @@ -88,4 +89,4 @@ Reset the link data

*Defined in*

[packages/cozy-client/src/CozyLink.js:40](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/CozyLink.js#L40)
[packages/cozy-client/src/CozyLink.js:41](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/CozyLink.js#L41)
6 changes: 4 additions & 2 deletions docs/api/cozy-client/classes/StackLink.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ Transfers queries and mutations to a remote stack

### executeMutation

▸ **executeMutation**(`mutation`, `result`, `forward`): `Promise`<`any`>
▸ **executeMutation**(`mutation`, `options`, `result`, `forward`): `Promise`<`any`>

*Parameters*

| Name | Type |
| :------ | :------ |
| `mutation` | `any` |
| `options` | `any` |
| `result` | `any` |
| `forward` | `any` |

Expand Down Expand Up @@ -153,7 +154,7 @@ Persist the given data into the links storage

### request

▸ **request**(`operation`, `result`, `forward`): `Promise`<`any`>
▸ **request**(`operation`, `options`, `result`, `forward`): `Promise`<`any`>

Request the given operation from the link

Expand All @@ -162,6 +163,7 @@ Request the given operation from the link
| Name | Type |
| :------ | :------ |
| `operation` | `any` |
| `options` | `any` |
| `result` | `any` |
| `forward` | `any` |

Expand Down
3 changes: 2 additions & 1 deletion docs/api/cozy-client/classes/WebFlagshipLink.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Persist the given data into the links storage

### request

**request**(`operation`, `result`, `forward`): `Promise`<`boolean`>
**request**(`operation`, `options`, `result`, `forward`): `Promise`<`boolean`>

Request the given operation from the link

Expand All @@ -99,6 +99,7 @@ Request the given operation from the link
| Name | Type |
| :------ | :------ |
| `operation` | `any` |
| `options` | `any` |
| `result` | `any` |
| `forward` | `any` |

Expand Down
40 changes: 21 additions & 19 deletions docs/api/cozy-pouch-link/classes/PouchLink.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ CozyLink.constructor

*Defined in*

[CozyPouchLink.js:833](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L833)
[CozyPouchLink.js:837](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L837)

***

Expand All @@ -182,7 +182,7 @@ CozyLink.constructor

*Defined in*

[CozyPouchLink.js:794](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L794)
[CozyPouchLink.js:798](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L798)

***

Expand All @@ -208,7 +208,7 @@ Create the PouchDB index if not existing

*Defined in*

[CozyPouchLink.js:599](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L599)
[CozyPouchLink.js:603](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L603)

***

Expand All @@ -229,7 +229,7 @@ Create the PouchDB index if not existing

*Defined in*

[CozyPouchLink.js:837](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L837)
[CozyPouchLink.js:841](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L841)

***

Expand All @@ -249,19 +249,20 @@ Create the PouchDB index if not existing

*Defined in*

[CozyPouchLink.js:822](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L822)
[CozyPouchLink.js:826](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L826)

***

### executeMutation

▸ **executeMutation**(`mutation`, `result`, `forward`): `Promise`<`any`>
▸ **executeMutation**(`mutation`, `options`, `result`, `forward`): `Promise`<`any`>

*Parameters*

| Name | Type |
| :------ | :------ |
| `mutation` | `any` |
| `options` | `any` |
| `result` | `any` |
| `forward` | `any` |

Expand All @@ -271,7 +272,7 @@ Create the PouchDB index if not existing

*Defined in*

[CozyPouchLink.js:756](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L756)
[CozyPouchLink.js:760](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L760)

***

Expand All @@ -291,7 +292,7 @@ Create the PouchDB index if not existing

*Defined in*

[CozyPouchLink.js:676](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L676)
[CozyPouchLink.js:680](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L680)

***

Expand All @@ -315,7 +316,7 @@ Retrieve the PouchDB index if exist, undefined otherwise

*Defined in*

[CozyPouchLink.js:623](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L623)
[CozyPouchLink.js:627](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L627)

***

Expand All @@ -341,7 +342,7 @@ The changes

*Defined in*

[CozyPouchLink.js:469](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L469)
[CozyPouchLink.js:473](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L473)

***

Expand All @@ -366,7 +367,7 @@ The db info

*Defined in*

[CozyPouchLink.js:484](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L484)
[CozyPouchLink.js:488](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L488)

***

Expand Down Expand Up @@ -512,7 +513,7 @@ Emits an event (pouchlink:sync:end) when the sync (all doctypes) is done

*Defined in*

[CozyPouchLink.js:585](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L585)
[CozyPouchLink.js:589](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L589)

***

Expand Down Expand Up @@ -567,7 +568,7 @@ the need to wait for the warmup

*Defined in*

[CozyPouchLink.js:571](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L571)
[CozyPouchLink.js:575](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L575)

***

Expand Down Expand Up @@ -626,7 +627,7 @@ CozyLink.persistCozyData

*Defined in*

[CozyPouchLink.js:519](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L519)
[CozyPouchLink.js:523](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L523)

***

Expand All @@ -652,13 +653,14 @@ CozyLink.persistCozyData

### request

▸ **request**(`operation`, `result?`, `forward?`): `Promise`<`any`>
▸ **request**(`operation`, `options`, `result?`, `forward?`): `Promise`<`any`>

*Parameters*

| Name | Type | Default value |
| :------ | :------ | :------ |
| `operation` | `any` | `undefined` |
| `options` | `any` | `undefined` |
| `result` | `any` | `null` |
| `forward` | (`operation`: `any`, `result`: `any`) => `void` | `doNothing` |

Expand Down Expand Up @@ -710,7 +712,7 @@ CozyLink.reset

*Defined in*

[CozyPouchLink.js:492](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L492)
[CozyPouchLink.js:496](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L496)

***

Expand Down Expand Up @@ -800,7 +802,7 @@ Emits pouchlink:sync:stop event

*Defined in*

[CozyPouchLink.js:867](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L867)
[CozyPouchLink.js:871](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L871)

***

Expand All @@ -820,7 +822,7 @@ Emits pouchlink:sync:stop event

*Defined in*

[CozyPouchLink.js:799](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L799)
[CozyPouchLink.js:803](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L803)

***

Expand All @@ -840,7 +842,7 @@ Emits pouchlink:sync:stop event

*Defined in*

[CozyPouchLink.js:804](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L804)
[CozyPouchLink.js:808](https://github.com/cozy/cozy-client/blob/master/packages/cozy-pouch-link/src/CozyPouchLink.js#L808)

***

Expand Down
8 changes: 4 additions & 4 deletions docs/link-authoring.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ In the chain example pictured above, the Dedup link would avoid re-fetching the
There are two ways of creating a new link. First, you can instantiate a `CozyLink` and pass a request handling function to its constructor:

```js
const logLink = new CozyLink((operation, result, forward) => {
const logLink = new CozyLink((operation, options, result, forward) => {
console.log(JSON.stringify(operation))
return forward(operation, result)
return forward(operation, options, result)
})
```

Or you can subclass `CozyLink`:

```js
class LogLink extends CozyLink {
request(operation, result, forward) {
request(operation, options, result, forward) {
console.log(JSON.stringify(operation))
return forward(operation, result)
return forward(operation, options, result)
}
}
```
Expand Down
12 changes: 6 additions & 6 deletions packages/cozy-client/src/CozyClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@
Promise.resolve(
executeQueryFromState(this.store.getState(), queryDefinition)
)
: () => this.requestQuery(queryDefinition)
: () => this.requestQuery(queryDefinition, options)
const response = await this._promiseCache.exec(requestFn, () =>
stringify(queryDefinition)
)
Expand Down Expand Up @@ -1083,7 +1083,7 @@
options.as || this.queryIdGenerator.generateId(mutationDefinition)
this.dispatch(initMutation(mutationId, mutationDefinition))
try {
const response = await this.requestMutation(mutationDefinition)
const response = await this.requestMutation(mutationDefinition, options)
this.dispatch(
receiveMutationResult(
mutationId,
Expand All @@ -1109,8 +1109,8 @@
* @param {QueryDefinition} definition QueryDefinition to be executed
* @returns {Promise<import("./types").ClientResponse>}
*/
async requestQuery(definition) {
const mainResponse = await this.chain.request(definition)
async requestQuery(definition, options) {
const mainResponse = await this.chain.request(definition, options)

await this.persistVirtualDocuments(definition, mainResponse.data)

Expand All @@ -1128,7 +1128,7 @@
* Save the document or array of documents into the persisted storage (if any)
*
* @private
* @param {CozyClientDocument | Array<CozyClientDocument>} data - Document or array of documents to be saved

Check warning on line 1131 in packages/cozy-client/src/CozyClient.js

View workflow job for this annotation

GitHub Actions / Build and publish

Expected @param names to be "definition, data". Got "data"
* @returns {Promise<void>}
*/
async persistVirtualDocuments(definition, data) {
Expand Down Expand Up @@ -1260,7 +1260,7 @@
}
}

async requestMutation(definition) {
async requestMutation(definition, options) {
if (Array.isArray(definition)) {
const [first, ...rest] = definition
const firstResponse = await this.requestMutation(first)
Expand All @@ -1273,7 +1273,7 @@
)
return firstResponse
}
return this.chain.request(definition)
return this.chain.request(definition, options)
}

getIncludesRelationships(queryDefinition) {
Expand Down Expand Up @@ -1877,8 +1877,8 @@
* @template {string} T
*
* @param {string} slug - the cozy-app's slug containing the setting (can be 'instance' for global settings)
* @param {T[]} keys - The names of the settings to retrieve

Check warning on line 1880 in packages/cozy-client/src/CozyClient.js

View workflow job for this annotation

GitHub Actions / Build and publish

The type 'T' is undefined
* @returns {Promise<Record<T, any>>} - The value of the requested setting

Check warning on line 1881 in packages/cozy-client/src/CozyClient.js

View workflow job for this annotation

GitHub Actions / Build and publish

The type 'T' is undefined
*/
async getSettings(slug, keys) {
return getSettings(this, slug, keys)
Expand Down
Loading
Loading