0.0.1 • Published 21 days ago

@sandumo/openapi-client-generator v0.0.1

Weekly downloads
-
License
-
Repository
github
Last release
21 days ago

@sandumo/openapi-client-generator

This package generates a client for an OpenAPI specification.

Installation

npm install @sandumo/openapi-client-generator

Usage

Generating client code

import { OpenAPIClientGenerator } from '@sandumo/openapi-client-generator';

const schema = JSON.parse(fs.readFileSync('path/to/openapi-schema.json', 'utf8'));
const outputFile = 'path/to/output/file.ts';

new OpenAPIClientGenerator()
  .fromSchema(schema)
  .toPath(outputFile)
  .generate();

Using the generated client

import api from './path/to/output/file';

api.axios.defaults.baseURL = 'https://api.example.com'; // api.axios is an Axios instance

await api.users.getUsers();

react-query

import api from './path/to/output/file';

export function ContextSettings() {
  // Set the queryClient for the api-client
  const queryClient = useQueryClient();

  useEffect(() => {
    api.config.queryClient = queryClient;
  }, [queryClient]);

  return <></>;
}

export function Component() {
  const { data: users } = useQuery(api.users.getUsersQuery());

  return <></>
}
0.0.1

21 days ago