Skip to content
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

feat: add getValidatedRouterParams util #573

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ H3 has a concept of composable utilities that accept `event` (from `eventHandler
- `getQuery(event)`
- `getValidatedQuery(event, validate)`
- `getRouterParams(event)`
- `getValidatedRouterParams(event, validate)`
- `getRouterParam(event, name)`
- `getMethod(event, default?)`
- `isMethod(event, expected, allowHead?)`
- `assertMethod(event, expected, allowHead?)`
Expand Down
5 changes: 2 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ export interface H3EventContext extends Record<string, any> {
export type EventHandlerResponse<T = any> = T | Promise<T>;

export interface EventHandlerRequest {
// TODO: Default to unknown in next major version
body?: any;

body?: any; // TODO: Default to unknown in next major version
query?: QueryObject;
routerParams?: Record<string, string>;
}

export type InferEventInput<
Expand Down
10 changes: 10 additions & 0 deletions src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ export function getRouterParams(
return event.context.params || {};
}

export function getValidatedRouterParams<
T,
Event extends H3Event = H3Event,
_T = InferEventInput<"routerParams", Event, T>,
>(event: Event, validate: ValidateFunction<_T>): Promise<_T> {
const routerParams = getRouterParams(event);

return validateData(routerParams, validate);
}

export function getRouterParam(
event: H3Event,
name: string,
Expand Down