Skip to content

Commit 4487de9

Browse files
committed
feat: support non-async schemas
1 parent 6ff6052 commit 4487de9

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

src/parsers.ts

+26-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import type { BaseIssue, BaseSchemaAsync } from 'valibot'
21
import { createError, getQuery, getRouterParams, type H3Event, readBody } from 'h3'
3-
import { parseAsync } from 'valibot'
2+
import * as v from 'valibot'
3+
4+
type VSchema<TInput, TOutput, TIssue extends v.BaseIssue<unknown>> =
5+
| v.BaseSchema<TInput, TOutput, TIssue>
6+
| v.BaseSchemaAsync<TInput, TOutput, TIssue>
47

58
const DEFAULT_ERROR_MESSAGE = 'Bad Request'
69
const DEFAULT_ERROR_STATUS = 400
710

8-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
911
function createBadRequest(error: any) {
1012
return createError({
1113
statusCode: DEFAULT_ERROR_STATUS,
@@ -19,13 +21,17 @@ function createBadRequest(error: any) {
1921
* @param event - A H3 event object.
2022
* @param schema - A Valibot Schema
2123
*/
22-
export async function useValidatedQuery<TInput, TOutput, TIssue extends BaseIssue<unknown>>(
24+
export async function useValidatedQuery<
25+
TInput,
26+
TOutput,
27+
TIssue extends v.BaseIssue<unknown>,
28+
>(
2329
event: H3Event,
24-
schema: BaseSchemaAsync<TInput, TOutput, TIssue>,
30+
schema: VSchema<TInput, TOutput, TIssue>,
2531
): Promise<TOutput> {
2632
try {
2733
const query = getQuery(event)
28-
const parsed = await parseAsync(schema, query)
34+
const parsed = await v.parseAsync(schema, query)
2935
return parsed
3036
}
3137
catch (error) {
@@ -38,13 +44,17 @@ export async function useValidatedQuery<TInput, TOutput, TIssue extends BaseIssu
3844
* @param event - A H3 event object.
3945
* @param schema - A Valibot Schema
4046
*/
41-
export async function useValidatedBody<TInput, TOutput, TIssue extends BaseIssue<unknown>>(
47+
export async function useValidatedBody<
48+
TInput,
49+
TOutput,
50+
TIssue extends v.BaseIssue<unknown>,
51+
>(
4252
event: H3Event,
43-
schema: BaseSchemaAsync<TInput, TOutput, TIssue>,
53+
schema: VSchema<TInput, TOutput, TIssue>,
4454
): Promise<TOutput> {
4555
try {
4656
const body = await readBody(event)
47-
const parsed = await parseAsync(schema, body)
57+
const parsed = await v.parseAsync(schema, body)
4858
return parsed
4959
}
5060
catch (error) {
@@ -57,13 +67,17 @@ export async function useValidatedBody<TInput, TOutput, TIssue extends BaseIssue
5767
* @param event - A H3 event object.
5868
* @param schema - A Valibot Schema
5969
*/
60-
export async function useValidatedParams<TInput, TOutput, TIssue extends BaseIssue<unknown>>(
70+
export async function useValidatedParams<
71+
TInput,
72+
TOutput,
73+
TIssue extends v.BaseIssue<unknown>,
74+
>(
6175
event: H3Event,
62-
schema: BaseSchemaAsync<TInput, TOutput, TIssue>,
76+
schema: VSchema<TInput, TOutput, TIssue>,
6377
): Promise<TOutput> {
6478
try {
6579
const params = getRouterParams(event)
66-
const parsed = await parseAsync(schema, params)
80+
const parsed = await v.parseAsync(schema, params)
6781
return parsed
6882
}
6983
catch (error) {

0 commit comments

Comments
 (0)