1

I was wondering if it's possible to use swagger-autogen module in a Typescript project. I have try many diffents ways but nothing to do. It always just says "Failed". In a swagger.js file :

const swaggerAutogen = require("swagger-autogen")();
const options = {...}

const outputFile = './docs/swagger-output.json';
const endpointsFiles = ['./app.ts'];

swaggerAutogen(outputFile, endpointsFiles, options);

I've tried to put the file in the dist directory but same error... Sorry if it's a stupid question or if I've just miss a key point.

2 Answers 2

3

I tried wrapping the endpoint file with path.join and it successfully works! Just make sure we build first, then run the swagger.js file from the build/dist folder.

import path from 'path';
import swaggerAutogen from 'swagger-autogen';

const doc = {...} // update doc
const outputFile = './swagger-output.json';
const endpointsFiles = [path.join(__dirname, 'routes/routes.js')];
swaggerAutogen()(outputFile, endpointsFiles, doc);
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, you use 'routes/routes.js' but project contains *.ts files instead of '*.js'
Ok, I changed to src/routes.ts and it works
Can you share a test project, I'm stuck at this point, the generation is only showing doc
0

One thing you can do is try to specify your route file (such as router.ts or router/index.ts) in the endpointsFiles variable. And remember that swagger-autogen only does the static analysis of the files, that is, if your route properties are dynamically generated at runtime, swagger-autogen won't be able to interpret your code.

NOTE: Update to the latest version of the lib, because it contains some bug fixes about TypeScript.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.