open-consul/ui/packages/consul-ui/app/helpers/href-to.js

74 lines
2.3 KiB
JavaScript
Raw Normal View History

UI: [BUGFIX] Decode/encode urls (#5206) In https://github.com/hashicorp/consul/commit/858b05fc3127d3d20d9554e932353d767c7b5fdc#diff-46ef88aa04507fb9b039344277531584 we removed encoding values in pathnames as we thought they were eventually being encoded by `ember`. It looks like this isn't the case. Turns out sometimes they are encoded sometimes they aren't. It's complicated. If at all possible refer to the PR https://github.com/hashicorp/consul/pull/5206. It's related to the difference between `dynamic` routes and `wildcard` routes. Partly related to this is a decision on whether we urlencode the slashes within service names or not. Whilst historically we haven't done this, we feel its a good time to change this behaviour, so we'll also be changing services to use dynamic routes instead of wildcard routes. So service links will then look like /ui/dc-1/services/application%2Fservice rather than /ui/dc-1/services/application/service Here, we define our routes in a declarative format (for the moment at least JSON) outside of Router.map, and loop through this within Router.map to set all our routes using the standard this.route method. We essentially configure our Router from the outside. As this configuration is now done declaratively outside of Router.map we can also make this data available to href-to and paramsFor, allowing us to detect wildcard routes and therefore apply urlencoding/decoding. Where I mention 'conditionally' below, this is detection is what is used for the decision. We conditionally add url encoding to the `{{href-to}}` helper/addon. The reasoning here is, if we are asking for a 'href/url' then whatever we receive back should always be urlencoded. We've done this by reusing as much code from the original `ember-href-to` addon as possible, after this change every call to the `{{href-to}}` helper will be urlencoded. As all links using `{{href-to}}` are now properly urlencoded. We also need to decode them in the correct place 'on the other end', so.. We also override the default `Route.paramsFor` method to conditionally decode all params before passing them to the `Route.model` hook. Lastly (the revert), as we almost consistently use url params to construct API calls, we make sure we re-encode any slugs that have been passed in by the user/developer. The original API for the `createURL` function was to allow you to pass values that didn't need encoding, values that **did** need encoding, followed by query params (which again require url encoding) All in all this should make the entire ember app url encode/decode safe.
2019-01-23 13:46:59 +00:00
// This helper requires `ember-href-to` for the moment at least
// It's similar code but allows us to check on the type of route
// (dynamic or wildcard) and encode or not depending on the type
import Helper from '@ember/component/helper';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { getOwner } from '@ember/application';
UI: [BUGFIX] Decode/encode urls (#5206) In https://github.com/hashicorp/consul/commit/858b05fc3127d3d20d9554e932353d767c7b5fdc#diff-46ef88aa04507fb9b039344277531584 we removed encoding values in pathnames as we thought they were eventually being encoded by `ember`. It looks like this isn't the case. Turns out sometimes they are encoded sometimes they aren't. It's complicated. If at all possible refer to the PR https://github.com/hashicorp/consul/pull/5206. It's related to the difference between `dynamic` routes and `wildcard` routes. Partly related to this is a decision on whether we urlencode the slashes within service names or not. Whilst historically we haven't done this, we feel its a good time to change this behaviour, so we'll also be changing services to use dynamic routes instead of wildcard routes. So service links will then look like /ui/dc-1/services/application%2Fservice rather than /ui/dc-1/services/application/service Here, we define our routes in a declarative format (for the moment at least JSON) outside of Router.map, and loop through this within Router.map to set all our routes using the standard this.route method. We essentially configure our Router from the outside. As this configuration is now done declaratively outside of Router.map we can also make this data available to href-to and paramsFor, allowing us to detect wildcard routes and therefore apply urlencoding/decoding. Where I mention 'conditionally' below, this is detection is what is used for the decision. We conditionally add url encoding to the `{{href-to}}` helper/addon. The reasoning here is, if we are asking for a 'href/url' then whatever we receive back should always be urlencoded. We've done this by reusing as much code from the original `ember-href-to` addon as possible, after this change every call to the `{{href-to}}` helper will be urlencoded. As all links using `{{href-to}}` are now properly urlencoded. We also need to decode them in the correct place 'on the other end', so.. We also override the default `Route.paramsFor` method to conditionally decode all params before passing them to the `Route.model` hook. Lastly (the revert), as we almost consistently use url params to construct API calls, we make sure we re-encode any slugs that have been passed in by the user/developer. The original API for the `createURL` function was to allow you to pass values that didn't need encoding, values that **did** need encoding, followed by query params (which again require url encoding) All in all this should make the entire ember app url encode/decode safe.
2019-01-23 13:46:59 +00:00
import transitionable from 'consul-ui/utils/routing/transitionable';
UI: [BUGFIX] Decode/encode urls (#5206) In https://github.com/hashicorp/consul/commit/858b05fc3127d3d20d9554e932353d767c7b5fdc#diff-46ef88aa04507fb9b039344277531584 we removed encoding values in pathnames as we thought they were eventually being encoded by `ember`. It looks like this isn't the case. Turns out sometimes they are encoded sometimes they aren't. It's complicated. If at all possible refer to the PR https://github.com/hashicorp/consul/pull/5206. It's related to the difference between `dynamic` routes and `wildcard` routes. Partly related to this is a decision on whether we urlencode the slashes within service names or not. Whilst historically we haven't done this, we feel its a good time to change this behaviour, so we'll also be changing services to use dynamic routes instead of wildcard routes. So service links will then look like /ui/dc-1/services/application%2Fservice rather than /ui/dc-1/services/application/service Here, we define our routes in a declarative format (for the moment at least JSON) outside of Router.map, and loop through this within Router.map to set all our routes using the standard this.route method. We essentially configure our Router from the outside. As this configuration is now done declaratively outside of Router.map we can also make this data available to href-to and paramsFor, allowing us to detect wildcard routes and therefore apply urlencoding/decoding. Where I mention 'conditionally' below, this is detection is what is used for the decision. We conditionally add url encoding to the `{{href-to}}` helper/addon. The reasoning here is, if we are asking for a 'href/url' then whatever we receive back should always be urlencoded. We've done this by reusing as much code from the original `ember-href-to` addon as possible, after this change every call to the `{{href-to}}` helper will be urlencoded. As all links using `{{href-to}}` are now properly urlencoded. We also need to decode them in the correct place 'on the other end', so.. We also override the default `Route.paramsFor` method to conditionally decode all params before passing them to the `Route.model` hook. Lastly (the revert), as we almost consistently use url params to construct API calls, we make sure we re-encode any slugs that have been passed in by the user/developer. The original API for the `createURL` function was to allow you to pass values that didn't need encoding, values that **did** need encoding, followed by query params (which again require url encoding) All in all this should make the entire ember app url encode/decode safe.
2019-01-23 13:46:59 +00:00
import wildcard from 'consul-ui/utils/routing/wildcard';
import { routes } from 'consul-ui/router';
const isWildcard = wildcard(routes);
export const hrefTo = function(container, params, hash = {}) {
// TODO: consider getting this from @service('router')._router which is
// private but we don't need getOwner, and it ensures setupRouter is called
// How private is 'router:main'? If its less private maybe stick with it?
const location = container.lookup('router:main').location;
const router = container.lookup('service:router');
let _params = params.slice(0);
let routeName = _params.shift();
let _hash = hash.params || {};
// a period means use the same routeName we are currently at and therefore
// use transitionable to figure out all the missing params
if (routeName === '.') {
_params = transitionable(router.currentRoute, _hash, container);
// _hash = {};
routeName = _params.shift();
}
try {
// if the routeName is a wildcard (*) route url encode all of the params
if (isWildcard(routeName)) {
_params = _params.map((item, i) => {
return item
.split('/')
.map(encodeURIComponent)
.join('/');
});
}
return location.hrefTo(routeName, _params, _hash);
} catch (e) {
if (e.constructor === Error) {
e.message = `${e.message} For "${params[0]}:${JSON.stringify(params.slice(1))}"`;
UI: [BUGFIX] Decode/encode urls (#5206) In https://github.com/hashicorp/consul/commit/858b05fc3127d3d20d9554e932353d767c7b5fdc#diff-46ef88aa04507fb9b039344277531584 we removed encoding values in pathnames as we thought they were eventually being encoded by `ember`. It looks like this isn't the case. Turns out sometimes they are encoded sometimes they aren't. It's complicated. If at all possible refer to the PR https://github.com/hashicorp/consul/pull/5206. It's related to the difference between `dynamic` routes and `wildcard` routes. Partly related to this is a decision on whether we urlencode the slashes within service names or not. Whilst historically we haven't done this, we feel its a good time to change this behaviour, so we'll also be changing services to use dynamic routes instead of wildcard routes. So service links will then look like /ui/dc-1/services/application%2Fservice rather than /ui/dc-1/services/application/service Here, we define our routes in a declarative format (for the moment at least JSON) outside of Router.map, and loop through this within Router.map to set all our routes using the standard this.route method. We essentially configure our Router from the outside. As this configuration is now done declaratively outside of Router.map we can also make this data available to href-to and paramsFor, allowing us to detect wildcard routes and therefore apply urlencoding/decoding. Where I mention 'conditionally' below, this is detection is what is used for the decision. We conditionally add url encoding to the `{{href-to}}` helper/addon. The reasoning here is, if we are asking for a 'href/url' then whatever we receive back should always be urlencoded. We've done this by reusing as much code from the original `ember-href-to` addon as possible, after this change every call to the `{{href-to}}` helper will be urlencoded. As all links using `{{href-to}}` are now properly urlencoded. We also need to decode them in the correct place 'on the other end', so.. We also override the default `Route.paramsFor` method to conditionally decode all params before passing them to the `Route.model` hook. Lastly (the revert), as we almost consistently use url params to construct API calls, we make sure we re-encode any slugs that have been passed in by the user/developer. The original API for the `createURL` function was to allow you to pass values that didn't need encoding, values that **did** need encoding, followed by query params (which again require url encoding) All in all this should make the entire ember app url encode/decode safe.
2019-01-23 13:46:59 +00:00
}
throw e;
}
};
export default class HrefToHelper extends Helper {
@service('router') router;
init() {
super.init(...arguments);
this.router.on('routeWillChange', this.routeWillChange);
}
compute(params, hash) {
return hrefTo(getOwner(this), params, hash);
}
@action
routeWillChange(transition) {
this.recompute();
}
willDestroy() {
this.router.off('routeWillChange', this.routeWillChange);
super.willDestroy();
}
}