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 24, 2024
1 parent d4ec45a commit 9640d02
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.baseURL 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?.baseURL || 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.baseURL 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?.baseURL || 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,
baseURL: 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.baseURL 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.baseURL 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?.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 9640d02

Please sign in to comment.