Generate
The generate command is a tool that allows you to generate a ready-to-use Hyper-Fetch client, typed requests and data
models based on your schema. It supports templates, including OpenAPI and Swagger.
npx @hyper-fetch/cli@latest generate
By using the code generator, you can:
- Automatically generate a ready-to-use Hyper-Fetch client.
- Create typed requests for all your API endpoints.
- Define data models based on your schema components.
- Boost productivity by eliminating manual setup for API integration.
Usage
You can use it in two ways - by going through wizard or passing individual arguments.
Remember to initialize a project first - this way hyper-fetch will be able to find the project's configuration and generate files in the correct location.
If you don't have a project yet, you can initialize one with the init command.
npx @hyper-fetch/cli@latest init
-
Wizard
To use the wizard, run the following command:
npx @hyper-fetch/cli@latest generate
It will guide you through the process of generating the client, requests and data models.
-
Custom arguments
You can also pass individual arguments to the command. Which is useful for the scripts.
npx @hyper-fetch/cli@latest generate --template openapi --url https://petstore3.swagger.io/api/v3/openapi.json --output ./client.ts
Example
npx @hyper-fetch/cli@latest generate --template openapi --url https://petstore3.swagger.io/api/v3/openapi.json --output ./client.ts
Then to use generated SDK you can do the following:
import { createClient } from "@hyper-fetch/core";
// This is the path to the generated SDK
import { createSdk } from "./path-to-my-sdk";
const client = createClient({
url: "http://localhost:3000",
});
// Export the sdk
export const sdk = createSdk(client);
import { sdk } from "./client";
// Use the sdk
const { data, error } = await sdk.store.inventory.get.send();
if (error) {
console.error(error);
}
console.log(data);
Available arguments
Run the following command to see the available arguments:
npx @hyper-fetch/cli@latest generate --help
You've learned how to use the code generator in Hyper-fetch!
- You can use the wizard to guide you through the process.
- You can pass individual arguments to the command for more control.
