Skip to content

Commit

Permalink
Merge pull request #14185 from nextcloud/backport/14175/stable31
Browse files Browse the repository at this point in the history
[stable31] refactor: remove `IS_DESKTOP` flag check on `generateAbsoluteUrl`
  • Loading branch information
nickvergessen authored Jan 23, 2025
2 parents 4786b37 + d194a4c commit 373244b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
17 changes: 6 additions & 11 deletions src/utils/__tests__/handleUrl.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,14 @@ jest.mock('@nextcloud/dialogs', () => ({

describe('handleUrl', () => {
describe('generateAbsoluteUrl', () => {
it('should generate url with IS_DESKTOP=false correctly', () => {
const output = generateAbsoluteUrl('/path')
expect(output).toBe('http://localhost/nc-webroot/path')
it('should generate absolute url', () => {
const output = generateAbsoluteUrl('/path/{foo}', { foo: 'bar' })
expect(output).toBe('http://localhost/nc-webroot/path/bar')
})

it('should generate url with IS_DESKTOP=true correctly', () => {
const originalIsDesktop = global.IS_DESKTOP
global.IS_DESKTOP = true

const output = generateAbsoluteUrl('/path')
expect(output).toBe('/nc-webroot/path')

global.IS_DESKTOP = originalIsDesktop
it('should generate absolute url with specified base', () => {
const output = generateAbsoluteUrl('/path/{foo}', { foo: 'bar' }, { baseURL: 'https://external.ltd/root' })
expect(output).toBe('https://external.ltd/root/path/bar')
})
})

Expand Down
19 changes: 8 additions & 11 deletions src/utils/handleUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,22 @@

import { showError, showSuccess } from '@nextcloud/dialogs'
import { t } from '@nextcloud/l10n'
import { generateUrl } from '@nextcloud/router'
import { generateUrl, getBaseUrl } from '@nextcloud/router'
import type { UrlOptions } from '@nextcloud/router'

/**
* Generate a full absolute link with @nextcloud/router.generateUrl
*
* @param url - path
* @param [params] parameters to be replaced into the address
* @param [options] options for the parameter replacement
* @param params - parameters to be replaced into the address
* @param options - options for the parameter replacement
*/
export function generateAbsoluteUrl(url: string, params?: object, options?: UrlOptions): string {
export function generateAbsoluteUrl(url: string, params?: object, options: UrlOptions = {}): string {
// TODO: add this function to @nextcloud/router?
const fullPath = generateUrl(url, params, options)
if (!IS_DESKTOP) {
return `${window.location.protocol}//${window.location.host}${fullPath}`
} else {
// On the Desktop generateUrl creates absolute url by default
return fullPath
}
return generateUrl(url, params, {
baseURL: getBaseUrl(),
...options,
})
}

/**
Expand Down

0 comments on commit 373244b

Please sign in to comment.