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

`typegen` command reads file from `node_modules\@apidevtools\json-schema-ref-parser\dist` directory #167

Open dzikoysk opened 1 year ago

dzikoysk commented 1 year ago

Docs references that definition can be read from file located in project dir:

typegen ./openapi.yml > client.d.ts - generate a type definition file

Unfortunately, it's not the case 7.3.2:

$ typegen xxx.json > src/types/openapi.d.ts

node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

JSONParserError: Error opening file "P:\xxx\xxx-frontend\node_modules\@apidevtools\json-schema-ref-parser\dist\xxx.json"
ENOENT: no such file or directory, open 'P:\xxx\xxx-frontend\node_modules\@apidevtools\json-schema-ref-parser\dist\xxx.json'        
    at Object.<anonymous> (P:\xxx\xxx-frontend\node_modules\@apidevtools\json-schema-ref-parser\dist\lib\resolvers\file.js:72:23)        
    at Generator.throw (<anonymous>)
    at rejected (P:\xxx\xxx-frontend\node_modules\@apidevtools\json-schema-ref-parser\dist\lib\resolvers\file.js:29:65) {
  code: 'ERESOLVER',
  name: 'ResolverError',
  source: 'P:\\xxx\\xxx-frontend\\node_modules\\@apidevtools\\json-schema-ref-parser\\dist\\xxx.json',
  path: null,
  toJSON: [Function: toJSON],
  ioErrorCode: 'ENOENT',
  [Symbol(nodejs.util.inspect.custom)]: [Function: inspect]
}

I've replaced it with the following command and it works:

$ curl http://localhost:8080/v3/api-docs > node_modules/@apidevtools/json-schema-ref-parser/dist/xxx.json && typegen xxx.json > src/types/openapi.d.ts"

But it doesn't seems to be right. Am I missing something or it's just a bug?

bzp99 commented 11 months ago

I just discovered the same. You can give an absolute path to typegen instead of placing the spec in the node_modules directory. This is most definitely a bug IMO, but I cannot afford to look into what causes it at the moment.

pcolot01 commented 9 months ago

I had the problem during modularization of an multi purposes REST API

$ref is a powerful mechanism to connect to remote, local resources or even database, ... The ref problem is to identify the exact context local, remote, ...

The best solution is to:

  1. Define an entry point service

openapi.yaml:

... components: schemas: GetService: $ref: './component.yaml#/components/schemas/GetService' ... paths: /service: $ref: './component.yaml#/components/pathitems/service' ...

component.yaml:

... components: schemas: GetService: ... pathitems: service:

  1. Convert yaml to JSON using the defrrencing of all dependencies to get an openapi.json without $ref

    openapi read api/openapi.yaml --format=json --dereference > ./dist/openapi.json

  2. Convert JSON openapi.json to openapi.d.ts

    npx openapicmd typegen dist/openapi.json > ./src/types/openapi.d.ts

An example is available on https://github.com/pcolot01/BlackmagicRestOpenApi

Best