lilnasy / gratelets

A collection of purpose-built, small, third-party integrations for Astro.
The Unlicense
105 stars 6 forks source link

`typed-api` fails instanceof check for Zod object schemas #52

Closed NuroDev closed 8 months ago

NuroDev commented 10 months ago

Just testing out the astro-typed-api package & so far it's looking very promising. However one quick issue that I ran into immediately is that this check is running if you provide a Zod object schema (z.object({ ... })).

Example

// src/pages/api/hello.ts
import { defineApiRoute } from 'astro-typed-api/server';
import { z } from 'zod';

export const GET = defineApiRoute({
    schema: z.object({
        name: z.string(),
    }),
    fetch: ({ name }) => `Hello, ${name}!`,
});

If you try to query this you get the following error:

API Route defines a schema, but the schema is not a valid zod schema.

The schema must be an instance of ZodType.

Cause

The issue being, as I mentioned above, being this check which seems to only check if the provided schema if of one possible sort of Zod schema. In this case something like a string.

Maybe switching this to a nullish check for a _def property inside the schema object may be better? And then if you want to get more specific check the schema definition type to limit which ones are allowed / supported. Otherwise you may need a lengthy check for all possible Zod classes that are and aren't supported.

lilnasy commented 10 months ago

That is odd. ZodType is the abstract class all other schema shapes inherit from. Could it be conflicting zod versions?

NuroDev commented 10 months ago

Ah, you are correct. My mistake. I didn't have zod installed locally. For whatever reason the Zod nullish check didn't get flagged. I guess it imported Astro's version of Zod probably.

Either way that seemed to fix that error. However I now get a new error, naturally, complaining that top-level await is not supported (yet)

Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2020", "firefox78", "safari14" + 2 overrides)
lilnasy commented 10 months ago

Yep, it was brought up before. Cloudflare's settings are kinda strict, I think it's from the adapter's extra processing. Doesn't matter though, the import doesn't need to be top-level.

lilnasy commented 10 months ago

@NuroDev I thought it was the cloudflare adapter but maybe not, I was able to build correctly with it. Is there something else in your project that could be involved?

amanketebo commented 10 months ago

Hey y'all I'm also seeing that same error:

Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2020", "firefox78", "safari14" + 2 overrides)

It seems to be referencing this part of the code:

Screenshot 2024-01-02 at 7 19 28 PM

I've tried changing the target to es2022 in the astro config file and a few other things in stackoverflow with no luck. Going to keep looking into it but just wanted to post here too

amanketebo commented 10 months ago

Oh wait, I missed your PR haha okay glad that will fix it. Thanks @lilnasy!

lilnasy commented 10 months ago

Released astro-typed-api@0.1.2 with a fix. Since I couldn't reproduce the error, I'll let one of you confirm before closing.

amanketebo commented 8 months ago

@lilnasy My bad forgot to circle back here, yup all is good now. Thanks!

lilnasy commented 8 months ago

Thanks!