BRIKEV / express-jsdoc-swagger

Swagger OpenAPI 3.x generator
https://brikev.github.io/express-jsdoc-swagger-docs/#/
MIT License
218 stars 30 forks source link

import file contained typedef is not recognized #231

Closed juicycool92 closed 1 year ago

juicycool92 commented 2 years ago

Is your feature request related to a problem? Please describe.

I love to use express-jsdoc-swagger. but recently, I replace my server to typescripts and have a problems. since back js code, I wrap my code with babel, but after change to ts, I think I don't need babel in my case, and i removed it. ( this is my guess why js codes are working fine but not ts )

soon I realized that there was error on swagger page. its failed to recognize my @typedef so I ran tests. results are in bottom.

Describe the solution you'd like it will be great to import @typedef like jsdoc. like this

/**
 * @typedef { allof | import('./Typedefs').MyType | object } MyNewType
 * @properties { number } fooo
**/

or just import typedef files onto root, and recognize any child files.

// root.ts
import('./Typedefs');
import ChildClass from './ChildClass';
...
//ChildClass.ts
...
/**
 * @typedef { allOf | PreDefinedType | object }  NewDefineType
 * @properties { number } fooo
**/

Describe alternatives you've considered my point is : there is ton of exactly same typedefs are in use on my app. but I don't like declare typedef every files top.

if there is any other solutions available, please let me know.

Additional context

this is sample test run codes.

//root.ts
import express from 'express';
import expressJSDocSwagger from "express-jsdoc-swagger";

const app = express();
const swagger_options = {
    info: {
        version: '1.0.0',
        title: 'HR API',
        license: {
            name: 'MIT',
        },
    },
    security: {},
    filesPattern: [ './*.js' ], // Glob pattern to find your jsdoc files
    swaggerUIPath: `/swagger`, // SwaggerUI will be render in this url. Default: '/api-docs'
    baseDir: __dirname,
    exposeSwaggerUI: true, // Expose OpenAPI UI. Default true
    exposeApiDocs: true, // Expose Open API JSON Docs documentation in `apiDocsPath` path. Default false.
    apiDocsPath: '/v3/api-docs', // Open API JSON Docs endpoint. Default value '/v3/api-docs'.
};
expressJSDocSwagger( app )( swagger_options );

/**
 * @typedef {object} foo
 * @property {string} bar
 */
/**
 * @typedef {allOf|foo|object} far
 * @property {string} barrr
 */
app.get( '/', ( req, res ) => {
    res.send(200);
});
app.listen(3000, () => {
    console.log('server up and running on port [3000]');
})

reslut image

working as i expect

add separate file which contains @typedef only.

//root.ts
import express from 'express';
import expressJSDocSwagger from "express-jsdoc-swagger";

const app = express();
const swagger_options = {
    info: {
        version: '1.0.0',
        title: 'API',
        license: {
            name: 'MIT',
        },
    },
    security: {},
    filesPattern: [ './*.js' ], // Glob pattern to find your jsdoc files
    swaggerUIPath: `/swagger`, // SwaggerUI will be render in this url. Default: '/api-docs'
    baseDir: __dirname,
    exposeSwaggerUI: true, // Expose OpenAPI UI. Default true
    exposeApiDocs: true, // Expose Open API JSON Docs documentation in `apiDocsPath` path. Default false.
    apiDocsPath: '/v3/api-docs', // Open API JSON Docs endpoint. Default value '/v3/api-docs'.
};
expressJSDocSwagger( app )( swagger_options );

import './typedefs'
/**
 * @typedef {allOf|foo|object} far
 * @property {string} barrr
 */
app.get( '/', ( req, res ) => {
    res.send(200);
});
app.listen(3000, () => {
    console.log('server up and running on port [3000]');
})
// typedefs.ts
/**
 * @typedef {object} foo
 * @property {string} bar
 */

result image

seems like its not recognize type foo

puthita-sam commented 1 year ago

I also found this issue. This might be worked around but for me, it works fine. instead of import type def like that. I added it in filesPattern in swagger_options like this

const swagger_options = {
    info: {
        version: '1.0.0',
        title: 'API',
        license: {
            name: 'MIT',
        },
    },
    security: {},
    filesPattern: [ './typedef.ts', './*.js' ], // Glob pattern to find your jsdoc files
    swaggerUIPath: `/swagger`, // SwaggerUI will be render in this url. Default: '/api-docs'
    baseDir: __dirname,
    exposeSwaggerUI: true, // Expose OpenAPI UI. Default true
    exposeApiDocs: true, // Expose Open API JSON Docs documentation in `apiDocsPath` path. Default false.
    apiDocsPath: '/v3/api-docs', // Open API JSON Docs endpoint. Default value '/v3/api-docs'.
};
juicycool92 commented 1 year ago

I also found this issue. This might be worked around but for me, it works fine. instead of import type def like that. I added it in filesPattern in swagger_options like this

const swagger_options = {
    info: {
        version: '1.0.0',
        title: 'API',
        license: {
            name: 'MIT',
        },
    },
    security: {},
    filesPattern: [ './typedef.ts', './*.js' ], // Glob pattern to find your jsdoc files
    swaggerUIPath: `/swagger`, // SwaggerUI will be render in this url. Default: '/api-docs'
    baseDir: __dirname,
    exposeSwaggerUI: true, // Expose OpenAPI UI. Default true
    exposeApiDocs: true, // Expose Open API JSON Docs documentation in `apiDocsPath` path. Default false.
    apiDocsPath: '/v3/api-docs', // Open API JSON Docs endpoint. Default value '/v3/api-docs'.
};

yeah, its works indeed!

I was totally forgot about this threads, im sorry for late response. 🤣 Thankyou for your kind answer!