npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pip-services4-swagger-node

v0.0.2

Published

Swagger UI for HTTP/REST Communication Components in Node.js / ES2017

Downloads

22

Readme

Swagger UI for HTTP/REST Communication Components in Node.js / ES2017

This module is a part of the Pip.Services polyglot microservices toolkit.

The swagger module provides a Swagger UI that can be added into microservices and seamlessly integrated with existing REST and Commandable HTTP services.

The module contains the following packages:

  • Build - Swagger service factory
  • Services - Swagger UI service

Quick links:

Use

Install the NPM package as

npm install pip-services4-swagger-node --save

Develop a RESTful service component. For example, it may look the following way. In the register method we load an Open API specification for the controller. You can also enable swagger by default in the constractor by setting _swaggerEnable property.

export class MyRestController extends RestController {
    public constructor() {
        super();
        this._baseRoute = "myservice";
        this._swaggerEnable = true;
    }

    private greeting(req, res) {
        let name = req.params.name;
        let response = "Hello, " + name + "!";
        this.sendResult(req, res, response);
    }
        
    public register() {
        this.registerRoute(
            'get', '/greeting', 
            new ObjectSchema(true)
                .withRequiredProperty("name", TypeCode.String),
            this.greeting
        );
        
        this.registerOpenApiSpecFromFile('./src/controllers/mycontroller.yml');
    }
}

The Open API specification for the controller shall be prepared either manually or using Swagger Editor

openapi: '3.0.2'
info:
  title: 'MyService'
  description: 'MyService REST API'
  version: '1'
paths:
  /myservice/greeting:
    get:
      tags:
        - myservice
      operationId: 'greeting'
      parameters:
      - name: trace_id
        in: query
        description: Correlation ID
        required: false
        schema:
          type: string
      - name: name
        in: query
        description: Name of a person
        required: true
        schema:
          type: string
      responses:
        200:
          description: 'Successful response'
          content:
            application/json:
              schema:
                type: 'string'

Include Swagger service into config.yml file and enable swagger for your REST or Commandable HTTP controllers. Also explicitely adding HttpEndpoint allows to share the same port between REST controllers and the Swagger controller.

---
...
# Shared HTTP Endpoint
- descriptor: "pip-services:endpoint:http:default:1.0"
  connection:
    protocol: http
    host: localhost
    port: 8080

# Swagger Controller
- descriptor: "pip-services:swagger-controller:http:default:1.0"

# My RESTful Controller
- descriptor: "myservice:service:rest:default:1.0"
  swagger:
    enable: true

Finally, remember to add factories to your container, to allow it creating required components.

...
import { DefaultRpcFactory } from 'pip-services4-rpc-node';
import { DefaultSwaggerFactory } from 'pip-services4-swagger-node';

export class MyProcess extends ProcessContainer {
  public constructor() {
    super("myservice", "MyService microservice");
    
    this._factories.add(new DefaultHttpFactory());
    this._factories.add(new DefaultSwaggerFactory());
    this._factories.add(new MyServiceFactory());
    ...
  }
}

Launch the microservice and open the browser to open the Open API specification at http://localhost:8080/greeting/swagger

Then open the Swagger UI using the link http://localhost:8080/swagger. The result shall look similar to the picture below.

Develop

For development you shall install the following prerequisites:

  • Node.js 14+
  • Visual Studio Code or another IDE of your choice
  • Docker
  • Typescript

Install dependencies:

npm install

Compile the code:

tsc

Run automated tests:

npm test

Generate API documentation:

./docgen.ps1

Before committing changes run dockerized build and test as:

./build.ps1
./test.ps1
./clear.ps1

Contacts

The Node.js version of Pip.Services is created and maintained by:

  • Sergey Seroukhov
  • Danil Prisyazhiy