openapistack / openapi-client-axios

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

Can't use a local definition file #171

Closed drernie closed 10 months ago

drernie commented 10 months ago

Is this not a feature, or am I doing something silly?

There seem to be examples of passing a local path to 'definition'. But whenever I try it, I get an error where either:

Is there some documentation I should be looking at that explains this?

drernie commented 10 months ago

In particular, it seems that I'm getting an error when fetching: 'https://tower.nf/openapi/nextflow-tower-api-latest.yml' so I was hoping downloading it first locally would solve the problem.

          '            schema:\n' +
          "              $ref: '#/components/schemas/CreateActionRequest'\n" +
 FAIL  test/openapi.test.ts (6.481 s)
  OpenAPI
    ✓ should init PetStore Client (525 ms)
    ✓ should init Benchling Client (1272 ms)
    ✕ should init Tower Client (880 ms)

  ● OpenAPI › should init Tower Client

    expect(received).toBeUndefined()

    Received: [Error: Invalid response fetching OpenAPI definition: [object Object]]
anttiviljami commented 10 months ago

Hi @drernie! Support for directly passing a local filesystem path for the definition was pulled quite some time ago.

This decision was made to reduce frontend bundle size since openapi-client-axios is commonly shipped as part of a browser-side js bundle.

But loading a local definition file is still fairly easy in a NodeJS environment using the fs or js-yaml module for YAML files, or directly importing a JSON file using import().

I’ll make sure to add an example in the docs

drernie commented 10 months ago

loading a local definition file is still fairly easy Thanks... but I'm confused. I get the bit about reading a file, but how exactly do we pass that into the constructor? Can it take an object rather than a URL?

anttiviljami commented 10 months ago

@drernie yes, exactly. The constructor also accepts a definition object 👍

drernie commented 10 months ago

Ah, thank you! This now works (in case anyone sees this before you update the documentation):

import { readFileSync } from 'fs';
import yaml from 'js-yaml';
import { Document, OpenAPIClientAxios } from 'openapi-client-axios';

const TOWER_YAML = './api/tower.yaml';
const yaml_doc = yaml.load(readFileSync(TOWER_YAML, 'utf8')) as Document;
const api = new OpenAPIClientAxios({
      definition: yaml_doc,
});

Note that this requires installing:

    'openapi-client-axios',
    'js-yaml',
    '@types/js-yaml',
raress96 commented 1 month ago

Hi @drernie! Support for directly passing a local filesystem path for the definition was pulled quite some time ago.

This decision was made to reduce frontend bundle size since openapi-client-axios is commonly shipped as part of a browser-side js bundle.

But loading a local definition file is still fairly easy in a NodeJS environment using the fs or js-yaml module for YAML files, or directly importing a JSON file using import().

I’ll make sure to add an example in the docs

Seems that the documentation is still not yet updated... https://openapistack.co/docs/openapi-client-axios/api/ The examples here say you can use a local URL and even a yaml file if you have js-yaml which did not work for me.

Thankfully the above comment seems to work! 👌