-
-
Notifications
You must be signed in to change notification settings - Fork 109
feat(inject-params): add required parameter validation and error handling for injectParams #670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
3638cc3
64ff8cb
bcaa3ec
24a1d64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,11 +6,18 @@ import { | |||||
| DefaultValueOptions, | ||||||
| InjectorOptions, | ||||||
| ParseOptions, | ||||||
| RequiredOptions, | ||||||
| } from 'ngxtension/shared'; | ||||||
| import { map } from 'rxjs'; | ||||||
|
|
||||||
| type QueryParamsTransformFn<ReadT> = (params: Params) => ReadT; | ||||||
|
|
||||||
| function missingRequiredParamError(key: string | undefined): Error { | ||||||
| return new Error( | ||||||
| `[ngxtension:injectQueryParams] Query parameter ${key} is required but was not provided.`, | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * The `QueryParamsOptions` type defines options for configuring the behavior of the `injectQueryParams` function. | ||||||
| * | ||||||
|
|
@@ -23,7 +30,8 @@ export type QueryParamsOptions<ReadT, DefaultValueT> = ParseOptions< | |||||
| string | null | ||||||
| > & | ||||||
| DefaultValueOptions<DefaultValueT> & | ||||||
| InjectorOptions & { | ||||||
| InjectorOptions & | ||||||
| RequiredOptions & { | ||||||
| /** | ||||||
| * The initial value to use if the query parameter is not present or undefined. | ||||||
| * | ||||||
|
|
@@ -55,6 +63,18 @@ export function injectQueryParams(): Signal<Params>; | |||||
| */ | ||||||
| export function injectQueryParams(key: string): Signal<string | null>; | ||||||
|
|
||||||
| /** | ||||||
| * The `injectQueryParams` function allows you to access and manipulate query parameters from the current route. | ||||||
| * | ||||||
| * @param {string} key - The name of the query parameter to retrieve. | ||||||
| * @param {QueryParamsOptions} options - Configuration options with required flag that ensures a non-null return. | ||||||
| * @returns {Signal} A `Signal` that emits the value of the specified query parameter. Throws an error if the query parameter is not present. | ||||||
| */ | ||||||
| export function injectQueryParams<ReadT = string>( | ||||||
| key: string, | ||||||
| options: QueryParamsOptions<ReadT, ReadT> & { required: true }, | ||||||
| ): Signal<ReadT>; | ||||||
|
|
||||||
| /** | ||||||
| * The `injectQueryParams` function allows you to access and manipulate query parameters from the current route. | ||||||
| * | ||||||
|
|
@@ -105,7 +125,7 @@ export function injectQueryParams<ReadT>( | |||||
| const route = inject(ActivatedRoute); | ||||||
| const queryParams = route.snapshot.queryParams || {}; | ||||||
|
|
||||||
| const { parse, transform, initialValue, defaultValue } = options; | ||||||
| const { parse, transform, initialValue, defaultValue, required } = options; | ||||||
|
|
||||||
| if (!keyOrParamsTransform) { | ||||||
| return toSignal(route.queryParams, { initialValue: queryParams }); | ||||||
|
|
@@ -124,11 +144,21 @@ export function injectQueryParams<ReadT>( | |||||
| | undefined; | ||||||
|
|
||||||
| if (!param) { | ||||||
| if (required) { | ||||||
| throw missingRequiredParamError( | ||||||
| keyOrParamsTransform as string | undefined, | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the
Suggested change
|
||||||
| ); | ||||||
| } | ||||||
| return defaultValue ?? initialValue ?? null; | ||||||
| } | ||||||
|
|
||||||
| if (Array.isArray(param)) { | ||||||
| if (param.length < 1) { | ||||||
| if (required) { | ||||||
| throw missingRequiredParamError( | ||||||
| keyOrParamsTransform as string | undefined, | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
| ); | ||||||
| } | ||||||
| return defaultValue ?? initialValue ?? null; | ||||||
| } | ||||||
| return parse | ||||||
|
|
@@ -151,6 +181,18 @@ export function injectQueryParams<ReadT>( | |||||
| * The `injectQueryParams` function namespace provides additional functionality for handling array query parameters. | ||||||
| */ | ||||||
| export namespace injectQueryParams { | ||||||
| /** | ||||||
| * Retrieve an array query parameter with optional configuration options. | ||||||
| * | ||||||
| * @param {string} key - The name of the array query parameter to retrieve. | ||||||
| * @param {QueryParamsOptions} options - Configuration options with required flag that ensures a non-null return. | ||||||
| * @returns {Signal} A `Signal` that emits an array of values for the specified query parameter. Throws an error if the query parameter is not present. | ||||||
| */ | ||||||
| export function array<ReadT = string>( | ||||||
| key: string, | ||||||
| options: QueryParamsOptions<ReadT, ReadT[]> & { required: true }, | ||||||
| ): Signal<ReadT[]>; | ||||||
|
|
||||||
| /** | ||||||
| * Retrieve an array query parameter with optional configuration options. | ||||||
| * | ||||||
|
|
@@ -191,16 +233,23 @@ export namespace injectQueryParams { | |||||
| const route = inject(ActivatedRoute); | ||||||
| const queryParams = route.snapshot.queryParams || {}; | ||||||
|
|
||||||
| const { parse, transform, initialValue, defaultValue } = options; | ||||||
| const { parse, transform, initialValue, defaultValue, required } = | ||||||
| options; | ||||||
|
|
||||||
| const transformParam = ( | ||||||
| param: string | string[] | null, | ||||||
| ): (ReadT | string)[] | null => { | ||||||
| if (!param) { | ||||||
| if (required) { | ||||||
| throw missingRequiredParamError(key); | ||||||
| } | ||||||
| return defaultValue ?? initialValue ?? null; | ||||||
| } | ||||||
| if (Array.isArray(param)) { | ||||||
| if (param.length < 1) { | ||||||
| if (required) { | ||||||
| throw missingRequiredParamError(key); | ||||||
| } | ||||||
| return defaultValue ?? initialValue ?? null; | ||||||
| } | ||||||
| // Avoid passing the parse function directly into the map function, | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from './default-value'; | ||
| export * from './injector'; | ||
| export * from './parse'; | ||
| export * from './required'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export type RequiredOptions = { | ||
| /** | ||
| * If true, throws an error if the parameter is not present. | ||
| */ | ||
| required?: boolean; | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this context,
keyOrParamsTransformis guaranteed to be astringbecause the precedingifconditions handleundefinedandfunctiontypes. Theas string | undefinedassertion is therefore redundant and can be simplified tokeyOrParamsTransform.