-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): added util to parse query
- Loading branch information
Showing
4 changed files
with
65 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { QueryParams, QueryParamsSimple } from './queryTypes.js'; | ||
|
||
export function parseQuery(querystring: string | null | undefined): QueryParamsSimple; | ||
export function parseQuery( | ||
querystring: string | null | undefined, | ||
options: { multiple: false }, | ||
): QueryParamsSimple; | ||
export function parseQuery( | ||
querystring: string | null | undefined, | ||
options: { multiple: true }, | ||
): QueryParams; | ||
export function parseQuery( | ||
querystring: string | null | undefined, | ||
options?: { multiple: boolean }, | ||
): QueryParams { | ||
const query: QueryParams = {}; | ||
const multiple = options?.multiple || false; | ||
|
||
if (!querystring) { | ||
return query; | ||
} | ||
|
||
if (querystring.startsWith('?')) { | ||
querystring = querystring.slice(1); | ||
} | ||
|
||
for (const item of querystring.split('&')) { | ||
const [rawKey, rawValue] = item.split('='); | ||
const key = decodeURIComponent(rawKey); | ||
const value = decodeURIComponent(rawValue); | ||
|
||
// Sometimes a query string can have multiple values | ||
// for the same key, so to factor that case in, you | ||
// could collect an array of values for the same key | ||
let entry = query[key]; | ||
|
||
if (entry === undefined) { | ||
query[key] = value; | ||
continue; | ||
} | ||
|
||
if (!multiple) { | ||
// Allows only for a single value per key | ||
continue; | ||
} | ||
|
||
// If the value for this key was not previously an array, update it | ||
if (!Array.isArray(entry)) { | ||
query[key] = entry = [entry]; | ||
} | ||
|
||
entry.push(value); | ||
} | ||
|
||
return query; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
type QueryParamValue = string | null; | ||
export type QueryParam = QueryParamValue | QueryParamValue[]; | ||
export type QueryParams = Record<string, QueryParam | undefined>; | ||
export type QueryParamsSimple = Record<string, string | undefined | null>; |
4 changes: 1 addition & 3 deletions
4
packages/utils/src/stringifyQuery.ts → packages/utils/src/url/stringifyQuery.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters