Open ozum opened 1 year ago
This would be fabulous if implemented to work with #1162 and could replace our usage of nestjs and potentially some FastAPI Python backends.
It would be more natural if those types where instead generic arguments to defineEventHandler
Would love this, I had ssumed that since h3's defineEventHandler
takes:
interface EventHandlerRequest {
body?: any;
query?: QueryObject;
routerParams?: Record<string, string>;
}
that I could pass a custom one to it's generic parameter and have it be e2e typed into $fetch
, but sadly isn't the case :(
Describe the feature
$fetch provides type safety for return types which is great. It would be greater if it optionally checks types for
query
andbody
parameters for internal API requests.Below is a rough proposal:
QuerySchema
andBodySchema
. -> Developer's responsibility/.nuxt/types/nitro.d.ts
. -> Below is an example./node_modules/nitropack/dist/index.d.ts
-> Below is a proposal./server/api/product.units.ts
/.nuxt/types/nitro.d.ts
/node_modules/nitropack/dist/index.d.ts
Problems I stumbled upon:
Excessive stack depth comparing types...
error from TypeScript. This error is present even I copy-paste the types without changing them. The problem is caused byAvailableRouterMethod<R>
type. If I switch it withRouterMethod
, it works. In this case we sacrifice "method" safety. TBH, I preferquery
andpost
safety to the "method" safety. a. Query and body parameters are much more error prone compared to a simple method name. b.AvailableRouterMethod<R>
type seems much more expensive compared to simple object types./.nuxt/types/nitro.d.ts
. I guess it would be easy to utilize already existing type generation function.POC
Below is the POC: A composable for Nuxt representing
Excessive stack...
problem mentioned above.POC Code
**/server/api/units.get.ts** ```ts import { useValidatedQuery, useValidatedBody, z } from "h3-zod"; import type { H3Event } from "h3"; const querySchema = z.object({ language: z.string() }); const bodySchema = z.object({ color: z.number() }); export type QuerySchema = z.inferAdditional information