First, create an apiGenerator.config.js
file in the project root directory.
And then write the settings as shown below.
module.exports = {
generateDir: 'src/api/generated',
type: {
basic: 'https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/api-with-examples.json',
// basic: proccess.env.BASIC_API_DOCS_URL,
},
directResponseTypes: ['basic']
};
generateDir specifies the directory path where the result files created when OpenApiGenerator is executed are stored.
For type, if the written Swagger API document is divided by domain, write a key value to specify and specify the api-docs URL where you can view Swagger's json results as the value. (This type value is important!!)
When using axios, directResponseTypes directly takes out the response.data value, not the response type, and when you want to define the type of the return value, you put the key value of the type (basic is the key value in the example above) as an array in directResponseTypes. If directResponseTypes is not defined, the default value [] is automatically set.
Now add the script to package.json as shown below.
"scripts": {
...
"generate-api": "node src/modules/openApiGenerator"
},
Now you are ready to use OpenApiGenerator!!!
First:
npm run install
# or
yarn install
Second:
npm run generate-api
# or
yarn generate-api
If you use prettier or eslint, be sure to exclude the directory where the generated result file is created from prettier and eslint.
The development source is as follows: We would like to inform you that this is the result of Open API 2.0 version compatibility, application of the openApiGenerator.config.js configuration file, and various bug fixes in the source code written by the original author.