mirror of
https://github.com/ascribe/onion.git
synced 2024-12-22 09:23:13 +01:00
Use js-utility-belt's url utils
This commit is contained in:
parent
2ba96780f1
commit
8689585d74
@ -9,7 +9,7 @@ import S3Fetcher from '../../fetchers/s3_fetcher';
|
||||
import AppConstants from '../../constants/application_constants';
|
||||
|
||||
import { getLangText } from '../../utils/lang';
|
||||
import { queryParamsToArgs } from '../../utils/url';
|
||||
import { parseQueryParamStr } from '../../utils/url';
|
||||
|
||||
|
||||
const { string } = React.PropTypes;
|
||||
@ -52,7 +52,7 @@ const S3DownloadButton = React.createClass({
|
||||
|
||||
// The signed url, however can expire, which is why we need to set up a timer to
|
||||
// renew it when it expires.
|
||||
const expires = parseInt(queryParamsToArgs(downloadUrl.split('?')[1]).expires, 10);
|
||||
const expires = parseInt(parseQueryParamStr(downloadUrl).expires, 10);
|
||||
const now = new Date().getTime() / 1000;
|
||||
|
||||
// Amazon uses seconds as their signature unix timestamp while `setTimeout` uses
|
||||
|
@ -6,7 +6,7 @@ import AppConstants from '../constants/application_constants';
|
||||
|
||||
import { getCookie } from '../utils/fetch_api';
|
||||
import { omitFromObject } from '../utils/general';
|
||||
import { argsToQueryParams } from '../utils/url';
|
||||
import { stringifyAsQueryParam } from '../utils/url';
|
||||
|
||||
|
||||
class Requests {
|
||||
@ -108,7 +108,7 @@ class Requests {
|
||||
});
|
||||
|
||||
if (attachParamsToQuery && params && Object.keys(params).length > 0) {
|
||||
newUrl += argsToQueryParams(params);
|
||||
newUrl += stringifyAsQueryParam(params);
|
||||
}
|
||||
|
||||
return newUrl;
|
||||
|
@ -1,71 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
import camelCase from 'camelcase';
|
||||
import decamelize from 'decamelize';
|
||||
import queryString from 'query-string';
|
||||
|
||||
import { sanitize } from './general';
|
||||
|
||||
// TODO: Create Unittests that test all functions
|
||||
|
||||
/**
|
||||
* Takes a key-value dictionary of this form:
|
||||
*
|
||||
* {
|
||||
* 'page': 1,
|
||||
* 'pageSize': 10
|
||||
* }
|
||||
*
|
||||
* and converts it to a query-parameter, which you can append to your URL.
|
||||
* The return looks like this:
|
||||
*
|
||||
* ?page=1&page_size=10
|
||||
*
|
||||
* CamelCase gets converted to snake_case!
|
||||
*
|
||||
* @param {object} obj Query params dictionary
|
||||
* @return {string} Query params string
|
||||
*/
|
||||
export function argsToQueryParams(obj) {
|
||||
const sanitizedObj = sanitize(obj);
|
||||
const queryParamObj = {};
|
||||
|
||||
Object
|
||||
.keys(sanitizedObj)
|
||||
.forEach((key) => {
|
||||
queryParamObj[decamelize(key)] = sanitizedObj[key];
|
||||
});
|
||||
|
||||
return '?' + queryString.stringify(queryParamObj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current url's query params as an key-val dictionary.
|
||||
* snake_case gets converted to CamelCase!
|
||||
* @return {object} Query params dictionary
|
||||
*/
|
||||
export function getCurrentQueryParams() {
|
||||
return queryParamsToArgs(window.location.search.substring(1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the given query param string into a key-val dictionary.
|
||||
* snake_case gets converted to CamelCase!
|
||||
* @param {string} queryParamString Query params string
|
||||
* @return {object} Query params dictionary
|
||||
*/
|
||||
export function queryParamsToArgs(queryParamString) {
|
||||
const queryParamObj = queryString.parse(queryParamString);
|
||||
const camelCaseParamObj = {};
|
||||
|
||||
Object
|
||||
.keys(queryParamObj)
|
||||
.forEach((key) => {
|
||||
camelCaseParamObj[camelCase(key)] = queryParamObj[key];
|
||||
});
|
||||
|
||||
return camelCaseParamObj;
|
||||
}
|
||||
// Re-export related utilities from js-utility-belt for easier access
|
||||
export { getCurrentQueryParams, stringifyAsQueryParam, parseQueryParamStr } from 'js-utility-belt/es6/url';
|
||||
|
||||
/**
|
||||
* Takes a string and a boolean and generates a string query parameter for
|
||||
|
Loading…
Reference in New Issue
Block a user