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

DataOf not really working? #148

Open
etodanik opened this issue May 6, 2024 · 3 comments
Open

DataOf not really working? #148

etodanik opened this issue May 6, 2024 · 3 comments

Comments

@etodanik
Copy link

etodanik commented May 6, 2024

I'm trying to use DataOf on a project, and TypeScript keeps resolving everything to any.

For example, I have:

    requestBody: z.object({
      address: z.string(),
      chainId: z.string(),
      token: z.string(),
    }),

And then I do:

    data: DataOf<typeof CreateSignInDataPost.schema>

But the resolved types are always:
image

@carafelix
Copy link

carafelix commented May 8, 2024

This is kind of the default behavior, since it looks like they haven't implement it with full functionality yet, defeating the a lot of purpose in the package, since validation it's crucially needed for POST requests, and using it with type inference. If you specified requestBody it also prevents you from reading/parsing it manually since the body was already used to construct the data object. That procedure it's documented here. But it's a contradiction, since if you define BodyRequest, it's not possbile to request.json() the body, it's already used and inside the data object.
In the docs they also use data.any as specified here. The data does indeed goes trough the validation process and all, but it's type it's not inferred into the data object.

Also the RequestBody constructor , besides not being documented, does not pass down it's types to its content object, it's always any, it could accept a type parameter at least. Also it does not comply with being a zodType.

the related type inference code is here:
https://github.com/cloudflare/itty-router-openapi/blob/5cd88e4496a07cd3707ca59bc4700646303640b7/src/types.ts#L221-L226

@G4brym

@carafelix
Copy link

carafelix commented May 8, 2024

you can always declare it manually, but that defeats the purpose.

export class ExampleRoute extends OpenAPIRoute {
    static schema: OpenAPIRouteSchema = {
       // more things
        requestBody: MySchema
        }
          
     // in the handler:
     async handle(
        request: Request,
        data: DataOf<typeof ExampleRoute.schema> & {
            body: z.TypeOf<typeof MySchema>>
        }
    ){/** doer */}
}
  
 export const MySchema = z.object({
  foo: fooSchema,
  bar: barSchema
 })

@G4brym
Copy link
Member

G4brym commented May 23, 2024

Hello, this part will get reworked on the next major release, i just didn't have enough time yet to get around to it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants