dyte-io / express-backend-sample

An express backend sample using Dyte's v2 APIs
Other
6 stars 0 forks source link

TypeScript Types From Axios Responses #3

Open birksy89 opened 5 months ago

birksy89 commented 5 months ago

Hello, thanks for this example - It's very useful.

Is there anyway of getting the potential responses strongly typed?

Is there a Node SDK or just a list of types which could be imported?

const response = await DyteAPI.post<{
        id: string;
        title: string;
        record_on_start: boolean;
        live_stream_on_start: boolean;
        status: string;
        created_at: string;
        updated_at: string;
      }>("/meetings", {
        title: input.title,
      });

      if (response.status !== 200) {
        throw new Error("Failed to create meeting");
      }

      return response.data;

I'm just trialling Dyte, and at the moment I'm manually writing out the response shape.

Any help would be greatly appreciated

vaibhavshn commented 5 months ago

Yeah that is a nice idea, we can have the types exported.

For now though you can just copy the type after converting the zod schema into a type using type MyType = z.infer<typeof MySchema>

birksy89 commented 5 months ago

Thanks for the quick reply!

I hope what I said made sense, the example code above is me just manually writing a TypeScript type in the Axios generic.

I could have written it as a Zod Schema, and inferred the type from that - As you've said above. (Big fan of Zod, and doing exactly that)

But it would still mean at the moment that I'd need to write out the expected response shape as a Zod Schema?

I'm not sure exactly where the Types would be exported from at this stage - Is this something that already exists somewhere?

I found this, but wasn't sure if it was the right thing: https://www.npmjs.com/package/@dytesdk/web-core

So I just used the Axios setup as is shown in this example repo here: https://github.com/dyte-io/express-backend-sample/blob/main/src/utils/dyte-api.ts

Thanks again