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 23, 2024
1 parent 8800e7a commit 0585141
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ const linkToRemoteBase = (service: string) => '/remote.php/' + service
* @brief Creates an absolute url for remote use
* @param {string} service id
* @return {string} the url
* @param {UrlOptions} options options for the parameter replacement
* @param {Number} options.remoteBase URL to use as a base (defaults to current instance)
*/
export const generateRemoteUrl = (service: string) => getBaseUrl() + linkToRemoteBase(service)
export const generateRemoteUrl = (service: string, options?: UrlOptions) => {
const baseUrl = options?.remoteBase || getBaseUrl()
return baseUrl + linkToRemoteBase(service)
}

/**
* Get the base path for the given OCS API service
Expand All @@ -30,22 +35,25 @@ export const generateRemoteUrl = (service: string) => getBaseUrl() + linkToRemot
* @param {UrlOptions} options options for the parameter replacement
* @param {boolean} options.escape Set to false if parameters should not be URL encoded (default true)
* @param {Number} options.ocsVersion OCS version to use (defaults to 2)
* @param {Number} options.remoteBase URL to use as a base (defaults to current instance)
* @return {string} Absolute path for the OCS URL
*/
export const generateOcsUrl = (url: string, params?: object, options?: UrlOptions) => {
const allOptions = Object.assign({
ocsVersion: 2
}, options || {})

const version = (allOptions.ocsVersion === 1) ? 1 : 2
const version = (allOptions.ocsVersion === 1) ? 1 : 2
const baseUrl = options?.remoteBase || getBaseUrl()

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

export interface UrlOptions {
escape: boolean,
noRewrite: boolean,
ocsVersion: Number
ocsVersion: Number,
remoteBase: String,
}

/**
Expand Down Expand Up @@ -86,6 +94,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.remoteBase is provided, generate the absolute url pointing ro remote server
*
* Parameters will be URL encoded automatically
*
Expand All @@ -94,18 +103,21 @@ const _generateUrlPath = (url: string, params?: object, options?: UrlOptions) =>
* @param {UrlOptions} options options for the parameter replacement
* @param {boolean} options.noRewrite True if you want to force index.php being added
* @param {boolean} options.escape Set to false if parameters should not be URL encoded (default true)
* @param {Number} options.remoteBase URL to use as a base (defaults to current instance)
* @return {string} URL with webroot for the given relative URL
*/
export const generateUrl = (url: string, params?: object, options?: UrlOptions) => {
const allOptions = Object.assign({
noRewrite: false
}, options || {})

const baseOrRootUrl = options?.remoteBase || 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 0585141

Please sign in to comment.