react-query-lightbase-codegen

2.5.6 • Public • Published

react-query-lightbase-codegen

Generate type-safe Axios API clients and React Query hooks from OpenAPI specifications.

Features

  • 🔄 Automatic code generation from OpenAPI/Swagger specs
  • 📝 Type-safe API client functions
  • 🎣 React Query integration with query options
  • 📦 Support for multiple API specifications
  • 📤 Handle multipart/form-data uploads
  • 💪 Full TypeScript support
  • 🔒 Type-safe request/response handling

Installation

npm install react-query-lightbase-codegen

Quick Start

1. Configure Generation

Create a script to generate your API code (e.g., scripts/generate.ts):

import { codegenerate } from 'react-query-lightbase-codegen';
await codegenerate({
  specSource: './specs/api.yaml', // or array of specs
  exportDir: './src/generated'
});

2. Configure API Instance

import axios from "axios";
import { setApiClient } from "./src/generated/apiClient";

const api = axios.create({ baseURL: "https://api.example.com" });
// Set the API client instance to be used by the generated client functions
setApiClient(api);

3. Use Generated Query Options

import { useQuery } from '@tanstack/react-query';
import { characteristicListQueryOptions } from './src/generated/pokiApi.queryOptions';

const { data, isLoading, error } = useQuery(characteristicListQueryOptions());

and more complex queries:

import { useQuery } from '@tanstack/react-query';
import { characteristicListQueryOptions } from './src/generated/pokiApi.queryOptions';

const { data, isLoading, error } = useQuery({
  ...characteristicListQueryOptions(),
  select: (data) => data.results.find(item => item.id === itemId),
});

easier query invalidation:

queryClient.invalidateQueries(characteristicListQueryOptions());

Generated Files

A single apiClient.ts file is generated to be used as a global Axios instance for all generated clients.

For each API specification, the following files are generated:

  • {api}.schema.ts - TypeScript types for requests/responses
  • {api}.client.ts - Type-safe API client functions
  • {api}.queryOptions.ts - React Query integration

Multiple API Specifications

await codegenerate({
specSource: [
  './specs/auth-api.yaml',
  './specs/user-api.json',
  'https://api.example.com/openapi.yaml'
],
exportDir: './src/generated'
});

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
2.5.655latest

Version History

VersionDownloads (Last 7 Days)Published
2.5.655
2.5.539
2.5.438
2.5.317
2.5.22
2.5.12
2.5.01
2.4.11
2.4.00
2.3.20
2.3.11
2.3.00
2.2.10
2.2.00
2.1.40
2.1.30
2.1.20
2.1.10
2.1.00
2.0.90
2.0.80
2.0.70
2.0.60
2.0.50
2.0.40
2.0.30
2.0.20
2.0.10
2.0.00
1.6.425
1.6.30
1.6.21
1.6.10
1.6.00
1.5.130
1.5.120
1.5.111
1.5.100
1.5.91
1.5.81
1.5.70
1.5.60
1.5.50
1.5.41
1.5.30
1.5.21
1.5.10
1.5.00
1.4.10
1.4.00
1.3.30
1.3.20
1.3.11
1.3.00
1.2.031
1.1.80
1.1.70
1.1.60
1.1.50
1.1.40
1.1.31
1.1.20
1.1.10
1.1.00
1.0.61
1.0.50
1.0.40
1.0.30
1.0.20
1.0.10
1.0.00
0.5.00
0.3.00
0.2.10
0.2.00
0.1.100
0.1.90
0.1.80
0.1.70
0.1.61
0.1.50
0.1.41
0.1.31
0.1.21
0.1.10
0.1.00
0.0.210
0.0.200
0.0.190
0.0.180
0.0.170
0.0.161
0.0.150
0.0.140
0.0.131
0.0.121
0.0.110
0.0.100
0.0.91
0.0.81
0.0.70
0.0.60
0.0.50
0.0.40
0.0.30
0.0.20
0.0.11

Package Sidebar

Install

npm i react-query-lightbase-codegen

Weekly Downloads

231

Version

2.5.6

License

MIT

Unpacked Size

130 kB

Total Files

46

Last publish

Collaborators

  • owinter86