From fe8b3628e8f3e83a7eac8ac783b4499023e9cdd7 Mon Sep 17 00:00:00 2001 From: cptran777 Date: Mon, 23 Apr 2018 12:08:49 -0700 Subject: [PATCH] Encode wildcard symbol before sending api call for readdataset --- wherehows-web/app/routes/datasets/dataset.ts | 10 +++++-- wherehows-web/app/utils/validators/urn.ts | 29 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/wherehows-web/app/routes/datasets/dataset.ts b/wherehows-web/app/routes/datasets/dataset.ts index 9b7a27d875..12fb3527e4 100644 --- a/wherehows-web/app/routes/datasets/dataset.ts +++ b/wherehows-web/app/routes/datasets/dataset.ts @@ -6,7 +6,13 @@ import ComputedProperty from '@ember/object/computed'; import Configurator from 'wherehows-web/services/configurator'; import Notifications, { NotificationEvent } from 'wherehows-web/services/notifications'; import { refreshModelQueryParams } from 'wherehows-web/utils/helpers/routes'; -import isUrn, { convertWhUrnToLiUrn, decodeUrn, isLiUrn, isWhUrn } from 'wherehows-web/utils/validators/urn'; +import isUrn, { + convertWhUrnToLiUrn, + decodeUrn, + isLiUrn, + isWhUrn, + encodeWildcard +} from 'wherehows-web/utils/validators/urn'; import { datasetIdToUrn, readDatasetByUrn } from 'wherehows-web/utils/api/datasets/dataset'; import { IDatasetView } from 'wherehows-web/typings/api/datasets/dataset'; import DatasetController from 'wherehows-web/controllers/datasets/dataset'; @@ -57,7 +63,7 @@ export default class DatasetRoute extends Route { } if (isLiUrn(decodeUrn(resolvedUrn))) { - return await readDatasetByUrn(resolvedUrn); + return await readDatasetByUrn(encodeWildcard(resolvedUrn)); } get(this, 'notifications').notify(NotificationEvent.error, { diff --git a/wherehows-web/app/utils/validators/urn.ts b/wherehows-web/app/utils/validators/urn.ts index ebf87ef71a..9f97ba1fe0 100644 --- a/wherehows-web/app/utils/validators/urn.ts +++ b/wherehows-web/app/utils/validators/urn.ts @@ -119,6 +119,33 @@ const encodeForwardSlash = (urn: string): string => urn.replace(/\//g, encodeURI */ const decodeForwardSlash = (urn: string): string => urn.replace(encodedSlashRegExp, decodeURIComponent('/')); +/** + * Stores the encoded URL for the asterisk/wildcard symbol since encodeURIComponent doesn't catch these + * as a reserved symbol + * @type {string} + */ +const encodedWildcard = '%2A'; + +/** + * Cached RegExp object for a global search of / + * @type {RegExp} + */ +const encodedWildcardRegExp = new RegExp(encodedWildcard, 'g'); + +/** + * Replaces any occurence of * with the encoded equivalent + * @param {string} urn + * @return {string} + */ +const encodeWildcard = (urn: string): string => urn.replace(/\*/g, encodedWildcard); + +/** + * Replaces encoded slashes with / + * @param {string} urn + * @return {string} + */ +const decodeWildcard = (urn: string): string => urn.replace(encodedWildcardRegExp, decodeURIComponent('*')); + /** * Replaces occurrences of / with the encoded counterpart in a urn string * @param {string} urn @@ -146,6 +173,8 @@ export { convertWhUrnToLiUrn, convertWhDatasetPathToLiPath, encodeForwardSlash, + encodeWildcard, + decodeWildcard, encodeUrn, decodeUrn };