GithubHelp home page GithubHelp logo

didikmulyadi / nodejs-api-doc Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 189 KB

NPM package to generate the API docs based on the open API file with multiple UI support ๐Ÿš€โšก๏ธ

Home Page: https://www.npmjs.com/package/@didik-mulyadi/nodejs-api-doc

License: MIT License

Shell 0.40% TypeScript 99.60%
api-docs api-documentation nodejs redoc stoplight swagger typescript expressjs nestjs multiple-ui-apidoc

nodejs-api-doc's Introduction

Node JS API Docโšก๏ธ

NPM package to generate the API docs based on the open API file with multiple UI support

npm latest version npm bundle size Visitors count NPM license


Bug report ยท Feature request



This package already supports NestJS and ExpressJS.

At this point, we are focusing on adding examples with many js-doc libraries. In the future, we will create our open API doc generation, "One Code for All Frameworks", but don't worry we are still making it support with other libraries.

Features ๐Ÿš€

  1. Support multiple API doc platform UI, options: Swagger, Stoplight, and Redoc
  2. Easy to change the UI by query params ?ui=swagger|stoplight|redoc or UI Switcher at the bottom right of the page
  3. Easy implementation, no need to refactor your existing code

Installation ๐Ÿš€

Install @didik-mulyadi/nodejs-api-doc using pnpm/npm/yarn:

pnpm add @didik-mulyadi/nodejs-api-doc

# OR

npm install @didik-mulyadi/nodejs-api-doc

# OR

yarn add @didik-mulyadi/nodejs-api-doc

Usage ๐Ÿ’ป

After we set up the package correctly, we can switch the UI in your docs with this.

For example, your API doc location is `http://localhost:3000/docs

  1. Use Swagger UI, http://localhost:3000/docs?ui=swagger

image

  1. Use Stoplight UI, http://localhost:3000/docs?ui=stoplight

image

  1. Use Redoc UI, http://localhost:3000/docs?ui=redoc

image

For code examples, see the examples directory or click.

Compatibility โš™๏ธ

This package supports any package/library that returns the open API object. These are tested framework and library that is compatible with our package:

Library Support
Nest JS + Fastify with @nestjs/swagger โœ…
Nest JS + Express with @nestjs/swagger โœ…
Express with swagger-jsdoc โœ…

Implementation ๐Ÿ’ป

Here's an example of how to use this package:

nodejs-api-doc with Nest Fastify + @nest/swagger

Step:

  1. Modify your src/main.ts with the below steps
  2. Create a swagger config with new DocumentBuilder()
  3. Generate open API document object with SwaggerModule.createDocument(app, config)
  4. Setup nodejs-api-doc with new NestApiDoc
  5. Start nodejs-api-doc with nestApiDoc.start()
import { NestApiDoc } from '@didik-mulyadi/nodejs-api-doc'

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter()
  )
  // swagger
  const config = new DocumentBuilder()
    .setTitle('Node.js API Docs')
    .setDescription('This API provides ...')
    .setVersion('0.0.1')
    .build()
  const document = SwaggerModule.createDocument(app, config)

  // nodejs-api-doc
  const nestApiDoc = new NestApiDoc(app, document, {
    defaultUI: 'stoplight',
    customPath: '/docs',
  })
  nestApiDoc.start()

  // Run the nestjs
  await app.listen(8080, '0.0.0.0')
}

nodejs-api-doc with Nest Express + @nest/swagger

Step:

  1. Modify your src/main.ts with the below steps
  2. Create a swagger config with new DocumentBuilder()
  3. Generate open API document object with SwaggerModule.createDocument(app, config)
  4. Setup nodejs-api-doc with new NestApiDoc
  5. Start nodejs-api-doc with nestApiDoc.start()
import { NestApiDoc } from '@didik-mulyadi/nodejs-api-doc'

async function bootstrap() {
  const app = await NestFactory.create(AppModule)
  // swagger
  const config = new DocumentBuilder()
    .setTitle('Node.js API Docs')
    .setDescription('This API provides ...')
    .setVersion('0.0.1')
    .build()
  const document = SwaggerModule.createDocument(app, config)

  // nodejs-api-doc
  const nestApiDoc = new NestApiDoc(app, document, {
    defaultUI: 'stoplight',
    customPath: '/docs',
  })
  nestApiDoc.start()

  // Run the nestjs
  await app.listen(8080, '0.0.0.0')
}

nodejs-api-doc with Express + swagger-jsdoc

Step:

  1. Modify your server.js or main.js with the below steps
  2. Call the swagger-jsdoc const document = swaggerJsdoc(options)
  3. Setup nodejs-api-doc const expressApiDoc = new ExpressApiDoc(app, document)
  4. Start nodejs-api-doc expressApiDoc.start()
const { ExpressApiDoc } = require('@didik-mulyadi/nodejs-api-doc')

const app = express()

...

// swagger-jsdoc
const options = {
  definition: {
    openapi: '3.1.0',
    info: {
      title: 'Node JS API Docs with Express',
      version: '0.1.0',
      description:
        'This is a simple CRUD API application made with Express and documented with Swagger',
      license: {
        name: 'MIT',
        url: 'https://spdx.org/licenses/MIT.html',
      },
      contact: {
        name: 'Didik Mulyadi',
        url: 'https://www.linkedin.com/in/didikmulyadi/',
        email: '[email protected]',
      },
    },
    servers: [
      {
        url: 'http://localhost:3000',
      },
    ],
  },
  apis: ['./routes/*.js'],
}
const document = swaggerJsdoc(options)

// Nodejs-api-doc
const expressApiDoc = new ExpressApiDoc(app, document)
expressApiDoc.start()

app.listen(3000)

Configuration options โš™๏ธ

Working with Helmet

Nest.JS Fastify Helmet

import { NestApiDoc, helmetConfig } from '@didik-mulyadi/nodejs-api-doc';

async function bootstrap() {
  ...
  await app.register(helmet, helmetConfig.nodeApiDocHelmetOption);
  ...
}

Bugs or Requests ๐Ÿ›

If you found any issues or have a good suggestion, feel free to open an issue

TODO

We are still updating this package, to make it more useful and easy to use. Here are the next that author wants to do

  • Add readme for bug_report.md
  • Add readme for feature_request.md

Find Me ๐Ÿ“–

Reach me on:

LinkedIn Medium

nodejs-api-doc's People

Contributors

didikmulyadi avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

nodejs-api-doc's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.