Files
vercel/packages/cli/src/util/dev/parse-query-string.ts
Ethan Arrowood 253b4fd1d2 [cli][client][error-utils][frameworks][fs-detectors][hydrogen][next][node-bridge][node][redwood][remix][routing-utils][static-config] update @types/node to v14 across repo (#8842)
### Related Issues

Updates @types/node to the latest version within the v14 major (based on `npm view @types/node`)

```
❯ npm view @types/node@'>=14.0.0 <15.0.0' version | tail -1
@types/node@14.18.33 '14.18.33'
```

This PR also fixes the various necessary type changes

### 📋 Checklist

<!--
  Please keep your PR as a Draft until the checklist is complete
-->

#### Tests

- [ ] The code changed/added as part of this PR has been covered with tests
- [ ] All tests pass locally with `yarn test-unit`

#### Code Review

- [ ] This PR has a concise title and thorough description useful to a reviewer
- [ ] Issue from task tracker has a link to this PR
2022-11-04 20:21:13 +00:00

60 lines
1.5 KiB
TypeScript

/**
* This function is necessary to account for the difference between
* `?a=` and `?a` because native `url.parse(str, true)` can't tell.
* @param querystring - The querystring to parse, also known as the "search" string.
*/
export function parseQueryString(
querystring?: string | null
): Record<string, string[]> {
const query: Record<string, string[]> = Object.create(null);
if (!querystring || !querystring.startsWith('?') || querystring === '?') {
return query;
}
const params = querystring.slice(1).split('&');
for (let param of params) {
let [key, value] = param.split('=');
if (key !== undefined) {
key = decodeURIComponent(key);
}
if (value !== undefined) {
value = decodeURIComponent(value);
}
let existing = query[key];
if (!existing) {
existing = [];
query[key] = existing;
}
existing.push(value);
}
return query;
}
/**
* This function is necessary to account for the difference between
* `?a=` and `?a` because native `url.format({ query })` can't tell.
* @param query - The query object to stringify.
*/
export function formatQueryString(
query: Record<string, string[]> | undefined
): string | null {
if (!query) {
return null;
}
let s = '';
let prefix = '?';
for (let [key, values] of Object.entries(query)) {
for (let value of values) {
s += prefix;
s += encodeURIComponent(key);
if (value !== undefined) {
s += '=';
s += encodeURIComponent(value);
}
prefix = '&';
}
}
return s || null;
}