-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #242 from tharropoulos/search-params
feat: Add search parameter normalization utilities and type safety
- Loading branch information
Showing
9 changed files
with
549 additions
and
33 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
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,50 @@ | ||
import { arrayableParams } from "./Documents"; | ||
import type { | ||
UnionArrayKeys, | ||
ExtractBaseTypes, | ||
SearchParams, | ||
} from "./Documents"; | ||
|
||
function hasNoArrayValues<T extends SearchParams>( | ||
params: T | ExtractBaseTypes<T>, | ||
): params is ExtractBaseTypes<T> { | ||
return Object.keys(arrayableParams) | ||
.filter((key) => params[key] !== undefined) | ||
.every((key) => isNonArrayValue(params[key])); | ||
} | ||
|
||
export function normalizeArrayableParams<T extends SearchParams>( | ||
params: T, | ||
): Prettify<ExtractBaseTypes<T>> { | ||
const result = { ...params }; | ||
|
||
const transformedValues = Object.keys(arrayableParams) | ||
.filter((key) => Array.isArray(result[key])) | ||
.map((key) => { | ||
result[key] = result[key].join(","); | ||
return key; | ||
}); | ||
|
||
if (!transformedValues.length && hasNoArrayValues(result)) { | ||
return result; | ||
} | ||
|
||
if (!hasNoArrayValues(result)) { | ||
throw new Error( | ||
`Failed to normalize arrayable params: ${JSON.stringify(result)}`, | ||
); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
function isNonArrayValue<T extends SearchParams, K extends UnionArrayKeys<T>>( | ||
value: T[K] | ExtractBaseTypes<T>[K], | ||
): value is ExtractBaseTypes<T>[K] { | ||
return !Array.isArray(value); | ||
} | ||
|
||
type Prettify<T> = { | ||
[K in keyof T]: T[K]; | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
} & {}; |
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
Oops, something went wrong.