Skip to main content
Version: v8.0.0

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

Purpose

By using the code generator, you can:

  1. Automatically generate a ready-to-use Hyper-Fetch client.
  2. Create typed requests for all your API endpoints.
  3. Define data models based on your schema components.
  4. 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.

Initialize a project

If you don't have a project yet, you can initialize one with the init command.

npx @hyper-fetch/cli@latest init
  1. 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.

  1. 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
Initialize a cli project
Learn how to initialize a cli project and generate code from your api

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

Congratulations!

You've learned how to use the code generator in Hyper-fetch!

  1. You can use the wizard to guide you through the process.
  2. You can pass individual arguments to the command for more control.