diff --git a/lib/index.ts b/lib/index.ts index 537f4a0..f555ee4 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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 @@ -30,6 +35,7 @@ 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) => { @@ -37,15 +43,17 @@ export const generateOcsUrl = (url: string, params?: object, options?: UrlOption 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, } /** @@ -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 * @@ -94,6 +103,7 @@ 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) => { @@ -101,11 +111,13 @@ export const generateUrl = (url: string, params?: object, options?: UrlOptions) 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); } /**