teobler / swr-request-generator

A tool for generating TypeScript code and interface from swagger by using SWR and axios as client.
https://www.npmjs.com/package/@openapi-integration/swr-request-generator
MIT License
45 stars 10 forks source link

AxiosResponse Inside AxiosResponse #18

Open MonsignorEduardo opened 1 year ago

MonsignorEduardo commented 1 year ago

This is maybe an error on my side. But for some reason the result type of trigger is an AxiosResponse inside an AxiosResponse.

export function UserAuthForm({ className, ...props }: UserAuthFormProps) {
    const { trigger, isMutating } = useLoginForAccessTokenAuthPostRequest();

    const form = useForm<z.infer<typeof formSchema>>({
        resolver: zodResolver(formSchema),
        defaultValues: {
            username: "",
            password: "",
        },
    });

    async function onSubmit(values: z.infer<typeof formSchema>) {
        try {
            // According to the types this is a AxiosResponse<AxiosResponse<Token, any>, any> | undefined
            // But it is actually a AxiosResponse<Token, any> | undefined
            const result = await trigger({ body: { username: values.username, password: values.password } });

            if (!result) {
                throw new Error("No response");
            }
            console.dir(result);
            console.dir(result.data);
            // This does not work because the types are wrong
            const { access_token } = result.data.data;

This is my openAPI.json if you want to try openAPI.txt

teobler commented 1 month ago

You may used wrong useMutationRequest function, the type returned by trigger should be controlled by useMutationRequest function, you can refer to example folder to get more details.