vitess.io/vitess@v0.16.2/web/vtadmin/src/util/queryString.ts (about) 1 /** 2 * Copyright 2021 The Vitess Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 import { merge } from 'lodash-es'; 17 import qs from 'query-string'; 18 19 export type QueryParams = ParsedQuery<string | number | boolean>; 20 21 export interface ParsedQuery<T = string> { 22 [key: string]: T | T[] | null | undefined; 23 } 24 25 // See https://github.com/sindresorhus/query-string#arrayformat 26 export type ArrayFormatType = 'bracket' | 'index' | 'comma' | 'separator' | 'none'; 27 28 const DEFAULT_ARRAY_FORMAT = 'none'; 29 30 const DEFAULT_PARSE_OPTIONS: qs.ParseOptions = { 31 arrayFormat: DEFAULT_ARRAY_FORMAT, 32 parseBooleans: false, 33 parseNumbers: false, 34 }; 35 36 /** 37 * `parse` is a simple wrapper around query-string's `parse` function, specifying 38 * VTAdmin's query parsing defaults. See https://github.com/sindresorhus/query-string. 39 */ 40 export const parse = (search: string, opts: qs.ParseOptions = {}) => 41 qs.parse(search, merge({}, DEFAULT_PARSE_OPTIONS, opts)); 42 43 const DEFAULT_STRINGIFY_OPTIONS: qs.StringifyOptions = { 44 arrayFormat: DEFAULT_ARRAY_FORMAT, 45 }; 46 47 /** 48 * `stringify` is a simple wrapper around query-string's `stringify` function, specifying 49 * VTAdmin's query stringification defaults. See https://github.com/sindresorhus/query-string. 50 */ 51 export const stringify = (query: ParsedQuery<string | number | boolean>, opts: qs.StringifyOptions = {}) => 52 qs.stringify(query, merge({}, DEFAULT_STRINGIFY_OPTIONS, opts));