Skip to content

Commit

Permalink
feat(router): allow to generate OCS URL for a given base (current ins…
Browse files Browse the repository at this point in the history
…tance by default)

Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
  • Loading branch information
Antreesy committed Jan 25, 2024
1 parent 5092cfe commit 9826c00
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
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
24 changes: 20 additions & 4 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ export interface UrlOptions {
* @default 2
*/
ocsVersion?: number


Check failure on line 23 in lib/index.ts

View workflow job for this annotation

GitHub Actions / NPM lint

More than 1 blank line not allowed
/**
* URL to use as a base (defaults to current instance)
* @default ''
*/
baseURL?: string
}

/**
Expand All @@ -42,8 +49,12 @@ 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) => getBaseUrl() + linkToRemoteBase(service)
export const generateRemoteUrl = (service: string, options?: UrlOptions) => {
const baseURL = options?.baseURL ?? getBaseUrl()

Check failure on line 55 in lib/index.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Expected indentation of 1 tab but found 4 spaces
return baseURL + linkToRemoteBase(service)

Check failure on line 56 in lib/index.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Expected indentation of 1 tab but found 4 spaces
}

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

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

return getBaseUrl() + '/ocs/v' + version + '.php' + _generateUrlPath(url, params, options)
return baseURL + '/ocs/v' + version + '.php' + _generateUrlPath(url, params, options)
}


Check failure on line 78 in lib/index.ts

View workflow job for this annotation

GitHub Actions / NPM lint

More than 1 blank line not allowed
/**
* Generate a url path, which can contain parameters
*
Expand Down Expand Up @@ -101,6 +114,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 +128,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

0 comments on commit 9826c00

Please sign in to comment.