-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add option for encoding and decoding (#46)
* fix: better encoding of URL parts * Add options to control encoding * Add tests * Move query params and encoding options to constructor * Update README
- Loading branch information
Showing
6 changed files
with
311 additions
and
91 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
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,71 @@ | ||
/** | ||
* We encode using encodeURIComponent but we want to | ||
* preserver certain characters which are commonly used | ||
* (sub delimiters and ':') | ||
* | ||
* https://www.ietf.org/rfc/rfc3986.txt | ||
* | ||
* reserved = gen-delims / sub-delims | ||
* | ||
* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" | ||
* | ||
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")" | ||
/ "*" / "+" / "," / ";" / "=" | ||
*/ | ||
|
||
const excludeSubDelimiters = /[^!$'()*+,;|:]/g | ||
|
||
export type URLParamsEncodingType = | ||
| 'default' | ||
| 'uri' | ||
| 'uriComponent' | ||
| 'none' | ||
| 'legacy' | ||
|
||
export const encodeURIComponentExcludingSubDelims = (segment: string): string => | ||
segment.replace(excludeSubDelimiters, match => encodeURIComponent(match)) | ||
|
||
const encodingMethods: Record< | ||
URLParamsEncodingType, | ||
(param: string) => string | ||
> = { | ||
default: encodeURIComponentExcludingSubDelims, | ||
uri: encodeURI, | ||
uriComponent: encodeURIComponent, | ||
none: val => val, | ||
legacy: encodeURI | ||
} | ||
|
||
const decodingMethods: Record< | ||
URLParamsEncodingType, | ||
(param: string) => string | ||
> = { | ||
default: decodeURIComponent, | ||
uri: decodeURI, | ||
uriComponent: decodeURIComponent, | ||
none: val => val, | ||
legacy: decodeURIComponent | ||
} | ||
|
||
export const encodeParam = ( | ||
param: string | number | boolean, | ||
encoding: URLParamsEncodingType, | ||
isSpatParam: boolean | ||
): string => { | ||
const encoder = | ||
encodingMethods[encoding] || encodeURIComponentExcludingSubDelims | ||
|
||
if (isSpatParam) { | ||
return String(param) | ||
.split('/') | ||
.map(encoder) | ||
.join('/') | ||
} | ||
|
||
return encoder(String(param)) | ||
} | ||
|
||
export const decodeParam = ( | ||
param: string, | ||
encoding: URLParamsEncodingType | ||
): string => (decodingMethods[encoding] || decodeURIComponent)(param) |
Oops, something went wrong.