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

ut-openapi

v1.3.5

Published

Open api utilities

Downloads

21

Readme

UT OpenAPI

This module provides automatic message formatting for http clients consuming swagger-enabled APIs. Currently only request format is supported.

Usage

ut-openapi exports a factory function which returns an object with the following methods:

  • async load (documents) - loads a set of documents and binds message formatters in the local cache. Example:

      const utOpenApi = require('ut-openapi')();
      utOpenApi.load({ // documents
          'providerX.segmentX': require.resolve('./providerX/segmentX-swagger.json'),
          'providerX.segmentY': require.resolve('./providerX/segmentY-swagger.json'),
          'providerY': require('./providerY-swagger.json'),
          'providerZ': 'http://www.providerZ.com/swagger.json'
      });
    
      /*
          there is a short syntax
          if just one document must be loaded.
          Instead of a map, just pass the namespace
          and the document as separate arguments:
      */
     utOpenApi.load('someProvider', require('./someProviderSwagger.json'));

    The documents argument represents a map where:

    • the keys determine how the operation IDs of the document will be prefixed. For example if the document is prefixed with key providerX.segmentY and contains a route with operationId operationZ, then it will be accessible via the method providerX.segmentY.operationZ. Example:
      const utOpenApi = require('ut-openapi')();
      utOpenApi.load({ // documents
          'providerX.segmentY': {
              "swagger": "2.0",
              // ...
              "paths": {
                  "/path": {
                      "post": {
                          "operationId": "operationZ"
                      }
                  }
              }
          }
      });
    • the values are the documents themselves. All three approaches are acceptable as a value:

      • path to a local swagger document
      • the content of the swagger document
      • url for obtaining the document with http GET request
  • format (msg, method, format)

    • msg (object) - the message to be sent
    • method (string) - the bound formatter function
    • format (string) - the format. Only request is currently supported.

    The message format for calling these auto-generated swagger methods has the following specifics:

    • can have a body property which will be used as a payload
    • can have a params property which ill be used for extracting path, query and header parameters.
      const utOpenApi = require('ut-openapi')();
      utOpenApi.load({ // documents
          'providerX.segmentY': {
              "swagger": "2.0",
              "host": "127.0.0.1:8080",
              "schemes": ["http"],
              "basePath": "/test",
              // ...
              "paths": {
                  "/path/{pathParam}": {
                      "post": {
                          "operationId": "operationZ",
                          "parameters": [
                              {
                                  "in": "path",
                                  "name": "pathParam",
                                  "type": "string"
                              },
                              {
                                  "in": "body",
                                  "name": "body",
                                  "schema": {
                                      "type": "object",
                                      "properties": {
                                          "y": {
                                              "type": "boolean"
                                          }
                                      }
                                  }
                              }
                          ]
                      }
                  }
              }
          }
      });
      const requestMessage = utOpenApi.format({
          params: {
              pathParam: 'x'
          },
          body: {
              y: true
          }
      }, 'providerX.segmentY.operationZ', 'request');
      /* requestMessage will result in a format suitable
      for usage with the node.js 'request' module:
          {
              url: 'http://127.0.0.1:8080/test/path/x',
              method: 'get',
              body: {
                  y: true
              },
              headers: {},
              qs: {}
          }
      */
  • formatRequest (msg, method) Shorthand for format function. E.e. formatRequest({/*...*/}, 'providerX.segmentY.operationZ') equals format({/*...*/}, 'providerX.segmentY.operationZ', 'request')

  • export (format, pattern) This is a convenient wrapper for obtaining multiple formatters simultaneously based on a pattern.

E.g.

    const utOpenApi = require('ut-openapi')();
    utOpenApi.load({ // documents
        'providerX.segmentX': {
            "swagger": "2.0",
            // ...
            "paths": {
                "/path1": {
                    "post": {
                        "operationId": "a.operation"
                    }
                },
                 "/path1": {
                    "post": {
                        "operationId": "b.operation"
                    }
                }
            }
        },
        'providerX.segmentY': {
            "swagger": "2.0",
            // ...
            "paths": {
                "/path1": {
                    "post": {
                        "operationId": "a.operation"
                    }
                },
                 "/path1": {
                    "post": {
                        "operationId": "b.operation"
                    }
                }
            }
        }
    });

    const formatters1 = utOpenApi.export();
    /*
        {
            'providerX.segmentX.a.operation': params => {},
            'providerX.segmentX.b.operation': params => {},
            'providerX.segmentY.a.operation': params => {},
            'providerX.segmentY.b.operation': params => {}
        }
    */

   const formatters2 = utOpenApi.export('request', 'providerX.segmentX');
    /*
        {
            'providerX.segmentX.a.operation': params => {},
            'providerX.segmentX.b.operation': params => {}
        }
    */

   const formatters3 = utOpenApi.export('request', 'a.operation');
    /*
        {
            'providerX.segmentX.a.operation': params => {},
            'providerX.segmentY.a.operation': params => {}
        }
    */

   // also regular expressions are supported
   const formatters4 = utOpenApi.export('request', /\.a\./);
    /*
        {
            'providerX.segmentX.a.operation': params => {},
            'providerX.segmentY.a.operation': params => {}
        }
    */