diff --git a/docs/api/cozy-client/interfaces/models.note.FetchURLOptions.md b/docs/api/cozy-client/interfaces/models.note.FetchURLOptions.md new file mode 100644 index 000000000..369df5124 --- /dev/null +++ b/docs/api/cozy-client/interfaces/models.note.FetchURLOptions.md @@ -0,0 +1,41 @@ +[cozy-client](../README.md) / [models](../modules/models.md) / [note](../modules/models.note.md) / FetchURLOptions + +# Interface: FetchURLOptions<> + +[models](../modules/models.md).[note](../modules/models.note.md).FetchURLOptions + +## Properties + +### driveId + +• **driveId**: `string` + +Shared drive ID used to fetch the URL + +*Defined in* + +[packages/cozy-client/src/models/note.js:7](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/note.js#L7) + +*** + +### pathname + +• **pathname**: `string` + +Pathname to use in the URL + +*Defined in* + +[packages/cozy-client/src/models/note.js:6](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/note.js#L6) + +*** + +### returnUrl + +• **returnUrl**: `string` + +Return URL to add in query string + +*Defined in* + +[packages/cozy-client/src/models/note.js:8](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/note.js#L8) diff --git a/docs/api/cozy-client/modules/models.note.md b/docs/api/cozy-client/modules/models.note.md index f2585f6b3..adf7a0544 100644 --- a/docs/api/cozy-client/modules/models.note.md +++ b/docs/api/cozy-client/modules/models.note.md @@ -4,11 +4,15 @@ [models](models.md).note +## Interfaces + +* [FetchURLOptions](../interfaces/models.note.FetchURLOptions.md) + ## Functions ### fetchURL -▸ **fetchURL**(`client`, `file`, `options?`): `Promise`<`string`> +▸ **fetchURL**(`client`, `file`, `[options]?`): `Promise`<`string`> Fetch and build an URL to open a note. @@ -18,9 +22,7 @@ Fetch and build an URL to open a note. | :------ | :------ | :------ | | `client` | `any` | CozyClient instance | | `file` | `any` | io.cozy.file object | -| `options` | `Object` | Options | -| `options.driveId` | `string` | - | -| `options.pathname` | `string` | - | +| `[options]` | [`FetchURLOptions`](../interfaces/models.note.FetchURLOptions.md) | Options | *Returns* @@ -30,7 +32,7 @@ url *Defined in* -[packages/cozy-client/src/models/note.js:34](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/note.js#L34) +[packages/cozy-client/src/models/note.js:39](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/note.js#L39) *** @@ -52,7 +54,7 @@ url *Defined in* -[packages/cozy-client/src/models/note.js:9](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/note.js#L9) +[packages/cozy-client/src/models/note.js:16](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/note.js#L16) *** @@ -73,4 +75,4 @@ url *Defined in* -[packages/cozy-client/src/models/note.js:18](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/note.js#L18) +[packages/cozy-client/src/models/note.js:25](https://github.com/linagora/cozy-client/blob/master/packages/cozy-client/src/models/note.js#L25) diff --git a/packages/cozy-client/src/models/note.js b/packages/cozy-client/src/models/note.js index a7cef7537..30b6ee844 100644 --- a/packages/cozy-client/src/models/note.js +++ b/packages/cozy-client/src/models/note.js @@ -1,6 +1,13 @@ import { generateWebLink } from '../helpers' import logger from '../logger' +/** + * @typedef {Object} FetchURLOptions + * @property {string} [pathname] - Pathname to use in the URL + * @property {string} [driveId] - Shared drive ID used to fetch the URL + * @property {string} [returnUrl] - Return URL to add in query string + */ + /** * * @param {string} notesAppUrl URL to the Notes App (https://notes.foo.mycozy.cloud) @@ -26,33 +33,43 @@ export const generateUrlForNote = (notesAppUrl, file) => { * * @param {object} client CozyClient instance * @param {object} file io.cozy.file object - * @param {object} options Options - * @param {string} [options.pathname] Pathname to use in the URL - * @param {string} [options.driveId] Shared drive ID used to fetched the URL + * @param {FetchURLOptions} [options] Options * @returns {Promise} url */ -export const fetchURL = async (client, file, options = {}) => { +export const fetchURL = async ( + client, + file, + { pathname, driveId, returnUrl } = {} +) => { const { data: { note_id, subdomain, protocol, instance, sharecode, public_name } } = await client .getStackClient() - .collection('io.cozy.notes', { driveId: options.driveId }) + .collection('io.cozy.notes', { driveId }) .fetchURL({ _id: file.id }) if (sharecode) { const searchParams = [['id', note_id]] searchParams.push(['sharecode', sharecode]) - if (public_name) searchParams.push(['username', public_name]) + + if (public_name) { + searchParams.push(['username', public_name]) + } + + if (returnUrl) { + searchParams.push(['returnUrl', returnUrl]) + } + return generateWebLink({ cozyUrl: `${protocol}://${instance}`, searchParams, - pathname: options.pathname ?? '/public/', + pathname: pathname ?? '/public/', slug: 'notes', subDomainType: subdomain }) } else { return generateWebLink({ cozyUrl: `${protocol}://${instance}`, - pathname: options.pathname ?? '', + pathname: pathname ?? '', slug: 'notes', subDomainType: subdomain, hash: `/n/${note_id}` diff --git a/packages/cozy-client/types/models/note.d.ts b/packages/cozy-client/types/models/note.d.ts index 0ec88f001..f8ff70372 100644 --- a/packages/cozy-client/types/models/note.d.ts +++ b/packages/cozy-client/types/models/note.d.ts @@ -1,6 +1,17 @@ export function generatePrivateUrl(notesAppUrl: string, file: object, options?: {}): string; export function generateUrlForNote(notesAppUrl: any, file: any): string; -export function fetchURL(client: object, file: object, options?: { - pathname: string; - driveId: string; -}): Promise; +export function fetchURL(client: object, file: object, { pathname, driveId, returnUrl }?: FetchURLOptions): Promise; +export type FetchURLOptions = { + /** + * - Pathname to use in the URL + */ + pathname?: string; + /** + * - Shared drive ID used to fetch the URL + */ + driveId?: string; + /** + * - Return URL to add in query string + */ + returnUrl?: string; +};