Skip to content

Commit

Permalink
Fix getNodeParams decoding issue (SAP#2686)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndricimrr authored May 2, 2022
1 parent 46a1532 commit e0e7918
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
9 changes: 7 additions & 2 deletions client/luigi-client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -643,16 +643,21 @@ export type getContext = () => Context;
*/
export function addNodeParams(params: NodeParams, keepBrowserHistory: Boolean): void;
export type addNodeParams = (params: NodeParams, keepBrowserHistory: Boolean) => void;

/**
* Returns the node parameters of the active URL.
* Node parameters are defined like URL query parameters but with a specific prefix allowing Luigi to pass them to the micro frontend view. The default prefix is **~** and you can use it in the following way: `https://my.luigi.app/home/products?~sort=asc&~page=3`.
* <!-- add-attribute:class:warning -->
* > **NOTE:** some special characters (`<`, `>`, `"`, `'`, `/`) in node parameters are HTML-encoded.
* @param {boolean} shouldDesanitise defines whether the specially encoded characters should be desanitised
* @returns {Object} node parameters, where the object property name is the node parameter name without the prefix, and its value is the value of the node parameter. For example `{sort: 'asc', page: 3}`
* @memberof Lifecycle
* @example
* const nodeParams = LuigiClient.getNodeParams()
* const nodeParams = LuigiClient.getNodeParams(true)
*/
export function getNodeParams(): NodeParams;
export type getNodeParams = () => NodeParams;
export function getNodeParams(shouldDesanitise?: boolean): NodeParams;
export type getNodeParams = (shouldDesanitise?: boolean) => NodeParams;

/**
* @returns {Object} node parameters, where the object property name is the node parameter name without the prefix, and its value is the value of the node parameter. For example `{sort: 'asc', page: 3}`
Expand Down
16 changes: 16 additions & 0 deletions client/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ class Helpers {
hasIntent(path) {
return !!path && path.toLowerCase().includes('#?intent=');
}

deSanitizeParamsMap(paramsMap) {
return Object.entries(paramsMap).reduce((sanitizedMap, paramPair) => {
sanitizedMap[this.deSanitizeParam(paramPair[0])] = this.deSanitizeParam(paramPair[1]);
return sanitizedMap;
}, {});
}

deSanitizeParam(param = '') {
return String(param)
.replaceAll('&lt;', '<')
.replaceAll('&gt;', '>')
.replaceAll('&quot;', '"')
.replaceAll('&#39;', "'")
.replaceAll('&sol;', '/');
}
}

export const helpers = new Helpers();
8 changes: 6 additions & 2 deletions client/src/lifecycleManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,17 @@ class LifecycleManager extends LuigiClientBase {
* Node parameters are defined like URL query parameters but with a specific prefix allowing Luigi to pass them to the micro frontend view. The default prefix is **~** and you can use it in the following way: `https://my.luigi.app/home/products?~sort=asc&~page=3`.
* <!-- add-attribute:class:warning -->
* > **NOTE:** some special characters (`<`, `>`, `"`, `'`, `/`) in node parameters are HTML-encoded.
* @param {boolean} shouldDesanitise defines whether the specially encoded characters should be desanitised
* @returns {Object} node parameters, where the object property name is the node parameter name without the prefix, and its value is the value of the node parameter. For example `{sort: 'asc', page: 3}`
* @memberof Lifecycle
* @example
* const nodeParams = LuigiClient.getNodeParams()
* const nodeParams = LuigiClient.getNodeParams(true)
*/
getNodeParams() {
return this.currentContext.nodeParams;
getNodeParams(shouldDesanitise = false) {
return shouldDesanitise
? helpers.deSanitizeParamsMap(this.currentContext.nodeParams)
: this.currentContext.nodeParams;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions client/src/luigi-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class LuigiClient {
addNodeParams(params, keepBrowserHistory) {
return lifecycleManager.addNodeParams(params, keepBrowserHistory);
}
getNodeParams() {
return lifecycleManager.getNodeParams();
getNodeParams(shouldDesanitise) {
return lifecycleManager.getNodeParams(shouldDesanitise);
}
getActiveFeatureToggles() {
return lifecycleManager.getActiveFeatureToggles();
Expand Down
10 changes: 5 additions & 5 deletions docs/luigi-client-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,15 @@ Node parameters are defined like URL query parameters but with a specific prefix

> **NOTE:** some special characters (`<`, `>`, `"`, `'`, `/`) in node parameters are HTML-encoded.
##### Parameters

- `shouldDesanitise` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** defines whether the specially encoded characters should be desanitised (optional, default `false`)

##### Examples

```javascript
const nodeParams = LuigiClient.getNodeParams()
const nodeParams = LuigiClient.getNodeParams(true)
```

Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** node parameters, where the object property name is the node parameter name without the prefix, and its value is the value of the node parameter. For example `{sort: 'asc', page: 3}`
Expand Down Expand Up @@ -568,11 +573,6 @@ Updates path of the modalPathParam when internal navigation occurs.
LuigiClient.linkManager().updateModalPathInternalNavigation('microfrontend')
```

**Meta**

- **since**: NEXTRELEASE


#### openAsModal

Opens a view in a modal. You can specify the modal's title and size. If you don't specify the title, it is the node label. If there is no node label, the title remains empty. The default size of the modal is `l`, which means 80%. You can also use `m` (60%) and `s` (40%) to set the modal size. Optionally, use it in combination with any of the navigation functions.
Expand Down

0 comments on commit e0e7918

Please sign in to comment.