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 method to get a base URL, allow to pass remote base #558

Merged
merged 2 commits into from
Jan 29, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 29 additions & 3 deletions __tests__/urls.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,27 @@ describe('URL generation', () => {
}
})

test('generateRemoteUrl', () => {
window._oc_webroot = '/nextcloud'
expect(generateRemoteUrl('dav')).toBe(`${window.location.href}nextcloud/remote.php/dav`)
describe('generateRemoteUrl', () => {
beforeEach(() => {
window._oc_webroot = ''
})

it('uses base URL by default', () => {
expect(generateRemoteUrl('dav')).toBe(`${window.location.origin}/remote.php/dav`)
})

it('replaces base URL with given one', () => {
const baseURL = 'https://remote-url.com'
expect(generateRemoteUrl('dav', { baseURL })).toBe(`${baseURL}/remote.php/dav`)
})

it('includes webroot', () => {
window._oc_webroot = '/nextcloud'
expect(generateRemoteUrl('dav')).toBe(`${window.location.origin}/nextcloud/remote.php/dav`)
})
})


describe('generateOcsUrl', () => {
beforeEach(() => {
window._oc_webroot = ''
Expand All @@ -56,6 +72,11 @@ describe('URL generation', () => {
expect(generateOcsUrl('/foo/bar', undefined, { ocsVersion: 1 })).toBe(`${window.location.href}ocs/v1.php/foo/bar`)
})

it('replaces base URL with given one', () => {
const baseURL = 'https://remote-url.com'
expect(generateOcsUrl('/foo/bar', undefined, { baseURL })).toBe(`${baseURL}/ocs/v2.php/foo/bar`)
})

it('starts with webroot', () => {
window._oc_webroot = '/nextcloud'
expect(generateOcsUrl('/foo/bar')).toBe(`${window.location.href}nextcloud/ocs/v2.php/foo/bar`)
Expand Down Expand Up @@ -83,6 +104,11 @@ describe('URL generation', () => {
expect(generateUrl('foo')).toBe('/foo')
})

it('replaces base URL with given one', () => {
const baseURL = 'https://remote-url.com'
expect(generateUrl('/foo/bar', undefined, { baseURL })).toBe(`${baseURL}/index.php/foo/bar`)
})

it('respects disabled mod-rewrite', () => {
expect(generateUrl('/foo/bar')).toMatch(/index\.php/)
})
Expand Down
7 changes: 6 additions & 1 deletion __tests__/webroot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/

import { describe, expect, test } from 'vitest'
import { getAppRootUrl, getRootUrl } from '../lib/index'
import { getAppRootUrl, getBaseUrl, getRootUrl } from '../lib/index'

declare global {
interface Window {
Expand All @@ -34,30 +34,35 @@ describe('Web root handling', () => {
test('empty web root', () => {
window._oc_webroot = ''
expect(getRootUrl()).toBe('')
expect(getBaseUrl()).toBe(window.location.origin)
})

test('with given web root', () => {
window._oc_webroot = '/nextcloud'
expect(getRootUrl()).toBe('/nextcloud')
expect(getBaseUrl()).toBe(`${window.location.origin}/nextcloud`)
})

test('without web root configured', () => {
window._oc_webroot = undefined
window.location.pathname = '/index.php/apps/files'
expect(getRootUrl()).toBe('')
expect(getBaseUrl()).toBe(window.location.origin)
})

test('with implicit web root', () => {
window._oc_webroot = undefined
window.location.pathname = '/nextcloud/index.php/apps/files'
expect(getRootUrl()).toBe('/nextcloud')
expect(getBaseUrl()).toBe(`${window.location.origin}/nextcloud`)
})

// TODO: This seems to be wrong, would expect `/nextcloud`
test('with implicit web root and path rename', () => {
window._oc_webroot = undefined
window.location.pathname = '/nextcloud/apps/files'
expect(getRootUrl()).toBe('/nextcloud/apps')
expect(getBaseUrl()).toBe(`${window.location.origin}/nextcloud/apps`)
})
})

Expand Down
33 changes: 28 additions & 5 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export interface UrlOptions {
* @default 2
*/
ocsVersion?: number

/**
* URL to use as a base (defaults to current instance)
* @default ''
*/
baseURL?: string
}

/**
Expand All @@ -36,14 +42,18 @@ export const linkTo = (app: string, file: string) => generateFilePath(app, '', f
* @param {string} service id
* @return {string} the url
*/
const linkToRemoteBase = (service: string) => getRootUrl() + '/remote.php/' + service
const linkToRemoteBase = (service: string) => '/remote.php/' + service

/**
* Creates an absolute url for remote use
* @param {string} service id
* @return {string} the url
* @param {UrlOptions} [options] options for the parameter replacement
*/
export const generateRemoteUrl = (service: string) => window.location.protocol + '//' + window.location.host + linkToRemoteBase(service)
export const generateRemoteUrl = (service: string, options?: UrlOptions) => {
const baseURL = options?.baseURL ?? getBaseUrl()
return baseURL + linkToRemoteBase(service)
}

/**
* Get the base path for the given OCS API service
Expand All @@ -59,8 +69,9 @@ export const generateOcsUrl = (url: string, params?: object, options?: UrlOption
}, options || {})

const version = (allOptions.ocsVersion === 1) ? 1 : 2
const baseURL = options?.baseURL ?? getBaseUrl()

return window.location.protocol + '//' + window.location.host + getRootUrl() + '/ocs/v' + version + '.php' + _generateUrlPath(url, params, options)
return baseURL + '/ocs/v' + version + '.php' + _generateUrlPath(url, params, options)
}

/**
Expand Down Expand Up @@ -101,6 +112,7 @@ const _generateUrlPath = (url: string, params?: object, options?: UrlOptions) =>

/**
* Generate the url with webroot for the given relative url, which can contain parameters
* If options.baseURL is provided, generate the absolute url pointing ro remote server
*
* Parameters will be URL encoded automatically
*
Expand All @@ -114,11 +126,13 @@ export const generateUrl = (url: string, params?: object, options?: UrlOptions)
noRewrite: false,
}, options || {})

const baseOrRootURL = options?.baseURL ?? getRootUrl()

if (window?.OC?.config?.modRewriteWorking === true && !allOptions.noRewrite) {
return getRootUrl() + _generateUrlPath(url, params, options)
return baseOrRootURL + _generateUrlPath(url, params, options)
}

return getRootUrl() + '/index.php' + _generateUrlPath(url, params, options)
return baseOrRootURL + '/index.php' + _generateUrlPath(url, params, options)
}

/**
Expand Down Expand Up @@ -189,6 +203,15 @@ export const generateFilePath = (app: string, type: string, file: string) => {
return link
}

/**
* Return the full base URL where this Nextcloud instance
* is accessible, with a web root included.
* For example "https://company.com/nextcloud".
*
* @return {string} base URL
*/
export const getBaseUrl = () => window.location.protocol + '//' + window.location.host + getRootUrl()

/**
* Return the web root path where this Nextcloud instance
* is accessible, with a leading slash.
Expand Down