diff options
author | Brandon Millman <brandon.millman@gmail.com> | 2018-06-01 06:11:04 +0800 |
---|---|---|
committer | Brandon Millman <brandon.millman@gmail.com> | 2018-06-02 01:37:44 +0800 |
commit | 31f1a9e5aa2b99dd7d802e67c5cff5854880e884 (patch) | |
tree | f459853f7e59733fe42cfa8f4e00227faba43d59 /packages/website/ts/utils | |
parent | 6387aae4711ea4cb9db67c57567494fedb56bb3e (diff) | |
download | dexon-0x-contracts-31f1a9e5aa2b99dd7d802e67c5cff5854880e884.tar.gz dexon-0x-contracts-31f1a9e5aa2b99dd7d802e67c5cff5854880e884.tar.zst dexon-0x-contracts-31f1a9e5aa2b99dd7d802e67c5cff5854880e884.zip |
Refactor order parser and add shared order support to new portal
Diffstat (limited to 'packages/website/ts/utils')
-rw-r--r-- | packages/website/ts/utils/order_parser.ts | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/packages/website/ts/utils/order_parser.ts b/packages/website/ts/utils/order_parser.ts new file mode 100644 index 000000000..be08da80e --- /dev/null +++ b/packages/website/ts/utils/order_parser.ts @@ -0,0 +1,33 @@ +import { logUtils } from '@0xproject/utils'; +import * as _ from 'lodash'; + +import { portalOrderSchema } from 'ts/schemas/portal_order_schema'; +import { validator } from 'ts/schemas/validator'; +import { Order } from 'ts/types'; + +export const orderParser = { + parse(queryString: string): Order | undefined { + if (queryString.length === 0) { + return undefined; + } + const queryParams = queryString.substring(1).split('&'); + const orderQueryParam = _.find(queryParams, queryParam => { + const queryPair = queryParam.split('='); + return queryPair[0] === 'order'; + }); + if (_.isUndefined(orderQueryParam)) { + return undefined; + } + const orderPair = orderQueryParam.split('='); + if (orderPair.length !== 2) { + return undefined; + } + const order = JSON.parse(decodeURIComponent(orderPair[1])); + const validationResult = validator.validate(order, portalOrderSchema); + if (validationResult.errors.length > 0) { + logUtils.log(`Invalid shared order: ${validationResult.errors}`); + return undefined; + } + return order; + }, +}; |