Type generation in openapi-fetch

27 views Asked by At

I want to generate a return type for the openapi-fetch client. Here is my example:

import createClient from "openapi-fetch";
import qs from "qs";
import { paths } from "@/types.d/apitypes";

export const BASE_URL = "http://localhost:1337";

const BASE_URL_API = "http://localhost:1337/api";

export const openAPIClient = createClient<paths>({
  baseUrl: `${BASE_URL_API}`,
  headers: { "Content-Type": "application/json", Accept: "application/json" },
  querySerializer(params) {
    console.log("queryserializer", params, qs.stringify(params));
    return qs.stringify(params, { encodeValuesOnly: true });
  },
});

export async function getProductById(productId: number) {
  return await openAPIClient.GET("/products/{id}", {
    params: {
      path: { id: productId },
      query: {
        populate: {
          images: true,
          categories: true,
        },
      },
    },
  });
}

getProductByID() returns an object that contains error and data.

I need a way to create a type alias for the data property or the contents of the property such as Product so I can write a function

ProductView(product: Product)

How would I do that?

0

There are 0 answers