Skip to content

Commit 4eddff4

Browse files
Merge pull request #210 from nextcloud/feature/194/ocs-url-parameters
Allow to provide/inject URL parameters on OCS urls too
2 parents 8a2c14a + 7485bfc commit 4eddff4

2 files changed

Lines changed: 56 additions & 20 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## 2.0.0 - 20YY-MM-DD
6+
### Changed
7+
- generateOcsUrl can now replace routing parameters like generateUrl
8+
- generateOcsUrl no longer contains a trailing slash unless given in the URL
9+
510
## 1.2.0 - 2020-08-20
611
### Added
712
- Nextcloud 20 support

lib/index.ts

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
declare var OC: Nextcloud.v16.OC | Nextcloud.v17.OC | Nextcloud.v18.OC | Nextcloud.v19.OC | Nextcloud.v20.OC;
44

55
/**
6-
* Get an absolute url to a file in an app
6+
* Get an url with webroot to a file in an app
77
*
88
* @param {string} app the id of the app the file belongs to
99
* @param {string} file the file path relative to the app folder
10-
* @return {string} Absolute URL to a file
10+
* @return {string} URL with webroot to a file
1111
*/
1212
export const linkTo = (app: string, file: string) => generateFilePath(app, '', file)
1313

@@ -29,31 +29,42 @@ export const generateRemoteUrl = (service: string) => window.location.protocol +
2929
/**
3030
* Get the base path for the given OCS API service
3131
*
32-
* @param {string} service name
33-
* @param {int} version OCS API version
34-
* @return {string} OCS API base path
32+
* @param {string} url OCS API service url
33+
* @param {object} params parameters to be replaced into the service url
34+
* @param {UrlOptions} options options for the parameter replacement
35+
* @param {boolean} options.escape Set to false if parameters should not be URL encoded (default true)
36+
* @param {Number} options.ocsVersion OCS version to use (defaults to 2)
37+
* @return {string} Absolute path for the OCS URL
3538
*/
36-
export const generateOcsUrl = (service: string, version: Number) => {
37-
version = (version !== 2) ? 1 : 2
38-
return window.location.protocol + '//' + window.location.host + getRootUrl() + '/ocs/v' + version + '.php/' + service + '/'
39+
export const generateOcsUrl = (url: string, params?: object, options?: UrlOptions) => {
40+
const allOptions = Object.assign({
41+
ocsVersion: 2
42+
}, options || {})
43+
44+
const version = (allOptions.ocsVersion === 1) ? 1 : 2
45+
46+
return window.location.protocol + '//' + window.location.host + getRootUrl() + '/ocs/v' + version + '.php' + _generateUrlPath(url, params, options);
3947
}
4048

4149
export interface UrlOptions {
4250
escape: boolean,
43-
noRewrite: boolean
51+
noRewrite: boolean,
52+
ocsVersion: Number
4453
}
4554

4655
/**
47-
* Generate the absolute url for the given relative url, which can contain parameters
56+
* Generate a url path, which can contain parameters
4857
*
4958
* Parameters will be URL encoded automatically
5059
*
51-
* @return {string} Absolute URL for the given relative URL
60+
* @param {string} url address (can contain placeholders e.g. /call/{token} would replace {token} with the value of params.token
61+
* @param {object} params parameters to be replaced into the address
62+
* @param {UrlOptions} options options for the parameter replacement
63+
* @return {string} Path part for the given URL
5264
*/
53-
export const generateUrl = (url: string, params?: object, options?: UrlOptions) => {
65+
const _generateUrlPath = (url: string, params?: object, options?: UrlOptions) => {
5466
const allOptions = Object.assign({
55-
escape: true,
56-
noRewrite: false
67+
escape: true
5768
}, options || {})
5869

5970
const _build = function (text: string, vars: object) {
@@ -69,20 +80,40 @@ export const generateUrl = (url: string, params?: object, options?: UrlOptions)
6980
}
7081
);
7182
};
83+
7284
if (url.charAt(0) !== '/') {
7385
url = '/' + url;
74-
7586
}
7687

88+
return _build(url, params || {});
89+
}
90+
91+
/**
92+
* Generate the url with webroot for the given relative url, which can contain parameters
93+
*
94+
* Parameters will be URL encoded automatically
95+
*
96+
* @param {string} url address (can contain placeholders e.g. /call/{token} would replace {token} with the value of params.token
97+
* @param {object} params parameters to be replaced into the url
98+
* @param {UrlOptions} options options for the parameter replacement
99+
* @param {boolean} options.noRewrite True if you want to force index.php being added
100+
* @param {boolean} options.escape Set to false if parameters should not be URL encoded (default true)
101+
* @return {string} URL with webroot for the given relative URL
102+
*/
103+
export const generateUrl = (url: string, params?: object, options?: UrlOptions) => {
104+
const allOptions = Object.assign({
105+
noRewrite: false
106+
}, options || {})
107+
77108
if (OC.config.modRewriteWorking === true && !allOptions.noRewrite) {
78-
return getRootUrl() + _build(url, params || {});
109+
return getRootUrl() + _generateUrlPath(url, params, options);
79110
}
80111

81-
return getRootUrl() + '/index.php' + _build(url, params || {});
112+
return getRootUrl() + '/index.php' + _generateUrlPath(url, params, options);
82113
}
83114

84115
/**
85-
* Get the absolute path to an image file
116+
* Get the path with webroot to an image file
86117
* if no extension is given for the image, it will automatically decide
87118
* between .png and .svg based on what the browser supports
88119
*
@@ -100,12 +131,12 @@ export const imagePath = (app: string, file: string) => {
100131
}
101132

102133
/**
103-
* Get the absolute url for a file in an app
134+
* Get the url with webroot for a file in an app
104135
*
105136
* @param {string} app the id of the app
106137
* @param {string} type the type of the file to link to (e.g. css,img,ajax.template)
107138
* @param {string} file the filename
108-
* @return {string} Absolute URL for a file in an app
139+
* @return {string} URL with webroot for a file in an app
109140
*/
110141
export const generateFilePath = (app: string, type: string, file: string) => {
111142
const isCore = OC.coreApps.indexOf(app) !== -1

0 commit comments

Comments
 (0)