openapistack / openapi-client-axios

JavaScript client library for consuming OpenAPI-enabled APIs with axios
https://openapistack.co
MIT License
558 stars 67 forks source link

Static (offline) generation #39

Closed blaluc closed 4 years ago

blaluc commented 4 years ago

Is it possible to generate the client code offline, starting from the api json spec file?

anttiviljami commented 4 years ago

Yes.

// api.js
import OpenAPIClientAxios from 'openapi-client-axios';
const api = new OpenAPIClientAxios({ definition: './api.json' });
export const getClient = () => api.getClient();
// usage
import { getClient } from './api.js';
getClient().then(client => {
  // client.getPets();
});
anttiviljami commented 4 years ago

Or synchronously:

npm install --save openapi-client-axios json-schema-deref-sync
// api.js
import fs from 'fs';
import OpenAPIClientAxios from 'openapi-client-axios';
const api = new OpenAPIClientAxios({ definition: JSON.parse(fs.readFileSync('./api.json')) });
export const client = api.initSync();
// usage
import { client } from './api.js';
// client.getPets();
paul-hristea commented 3 years ago

Hello @anttiviljami

Does this also work from a React web app? Currently when I try to use this, it keeps trying to get the definitions from 'http://mybaseurl.com/swagger.json' instead of the static file called './swagger.json' Is this expected?

Thanks!

khokm commented 1 year ago

@paul-hristea you can use:

import definition from "./swagger.json"