From 3ab64e9f05966d3cb3a4449cabf697e3ac81b7b9 Mon Sep 17 00:00:00 2001 From: JF-Cozy Date: Tue, 17 Jan 2023 15:26:22 +0100 Subject: [PATCH] feat: Deprecates `generateWebLink` in AppLinker if you used `generateWebLink` from `cozy-ui AppLinker` component, you have now to use it from `cozy-client`. Simply replace `nativePath` prop by `hash` and add `pathname: '/'` prop. --- react/AppLinker/index.jsx | 2 +- react/AppLinker/native.js | 57 +++++++++++++--------------------- react/AppLinker/native.spec.js | 13 +++++--- 3 files changed, 31 insertions(+), 41 deletions(-) diff --git a/react/AppLinker/index.jsx b/react/AppLinker/index.jsx index 824666c7d8..2b1a80aa58 100644 --- a/react/AppLinker/index.jsx +++ b/react/AppLinker/index.jsx @@ -1,7 +1,7 @@ import React from 'react' import PropTypes from 'prop-types' -import { withClient } from 'cozy-client' +import { withClient } from 'cozy-client' import { checkApp, startApp, diff --git a/react/AppLinker/native.js b/react/AppLinker/native.js index a2e78e8b6b..fc47353ea3 100644 --- a/react/AppLinker/native.js +++ b/react/AppLinker/native.js @@ -1,44 +1,19 @@ +import { + generateWebLink as generateWebLinkClient, + ensureFirstSlash +} from 'cozy-client' + import { UNIVERSAL_LINK_URL } from 'cozy-ui/transpiled/react/AppLinker/native.config.js' export const getUniversalLinkDomain = () => { return UNIVERSAL_LINK_URL } -const ensureFirstSlash = path => { - if (!path) { - return '/' - } else { - return path.startsWith('/') ? path : '/' + path - } -} - -/** - * generateWebLink - Construct a link to a web app - * - * @param {object} Options Object of options - * @param {string} options.cozyUrl Base URL of the cozy, eg. http://cozy.localhost:8080 or https://test.mycozy.cloud:8080 - * @param {string} options.nativePath Path inside the app, eg. /files/test.jpg - * @param {string} options.slug Slug of the app - * @param {string} options.subDomainType Whether the cozy is using flat or nested subdomains. Defaults to flat. - * - * @return {string} Generated URL - */ -export const generateWebLink = ({ - cozyUrl, - nativePath, - slug, - subDomainType -}) => { - const url = new URL(cozyUrl) - url.host = - subDomainType === 'nested' - ? `${slug}.${url.host}` - : url.host - .split('.') - .map((x, i) => (i === 0 ? x + '-' + slug : x)) - .join('.') - url.hash = ensureFirstSlash(nativePath) - return url.toString() +export const generateWebLink = ({ nativePath, ...props }) => { + console.warn( + 'Deprecated: you should use generateWebLink from cozy-client instead' + ) + return generateWebLinkClient({ pathname: '/', hash: nativePath, ...props }) } /** @@ -55,16 +30,26 @@ export const generateUniversalLink = options => { const { slug, cozyUrl, subDomainType } = options let { fallbackUrl, nativePath } = options nativePath = ensureFirstSlash(nativePath) + if (!cozyUrl && !fallbackUrl) { throw new Error( 'Must have either cozyUrl or fallbackUrl to generate universal link.' ) } + if (cozyUrl && !fallbackUrl) { - fallbackUrl = generateWebLink({ cozyUrl, nativePath, slug, subDomainType }) + fallbackUrl = generateWebLinkClient({ + cozyUrl, + pathname: '/', + hash: nativePath, + slug, + subDomainType + }) } + const url = getUniversalLinkDomain(cozyUrl) + '/' + slug + nativePath const urlObj = new URL(url) urlObj.searchParams.append('fallback', fallbackUrl) + return urlObj.toString() } diff --git a/react/AppLinker/native.spec.js b/react/AppLinker/native.spec.js index 174c2fc220..d65a7fe1bc 100644 --- a/react/AppLinker/native.spec.js +++ b/react/AppLinker/native.spec.js @@ -1,11 +1,14 @@ -import { generateUniversalLink, generateWebLink } from './native' +import { generateWebLink } from 'cozy-client' + +import { generateUniversalLink } from './native' describe('native functions', () => { describe('generating a web link', () => { it('should generate a web link', () => { const webLink = generateWebLink({ cozyUrl: 'https://test.cozy.tools:8080', - nativePath: '/files/1', + pathname: '/', + hash: '/files/1', slug: 'drive' }) @@ -15,7 +18,8 @@ describe('native functions', () => { it('should handle both types of subdomains', () => { const flatLink = generateWebLink({ cozyUrl: 'https://test.cozy.tools:8080', - nativePath: '/files/1', + pathname: '/', + hash: '/files/1', slug: 'drive', subDomainType: 'flat' }) @@ -23,7 +27,8 @@ describe('native functions', () => { const nestedLink = generateWebLink({ cozyUrl: 'https://cozy.tools:8080', - nativePath: '/files/1', + pathname: '/', + hash: '/files/1', slug: 'drive', subDomainType: 'nested' })