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

@public.firetail.io/firetail-api

v0.5.2

Published

Frustrated with a lack of quality service level API tool? Firetail is here to help! [![npm version](https://badge.fury.io/js/@public.firetail.io%2Ffiretail-api.svg)](https://www.npmjs.com/package/@public.firetail.io/firetail-api) is a middleware for **Exp

Downloads

13

Readme

Firetail JS Library

Frustrated with a lack of quality service level API tool? Firetail is here to help! npm version is a middleware for Express and Node development. This document will cover setup and configuration. You can also find a complete working example, in the sample folder.

Code Coverage codecov License

What is Firetail Middleware?

Firetail is a Middleware that intercept Http/Rest requests based on OpenAPI Specification (formerly known as Swagger Spec) of your API described in YAML format. Firetail allows you to write an OpenAPI specification, then maps the endpoints to your Node functions. You can describe your REST API in as much detail as you want; then Firetail guarantees that it will work as you specified.

Firetail Features

  • Validates requests and endpoint parameters automatically, based on your specification
  • Setup firetail configuration in your package.json or in your app at runtime
  • Helpful developer error messages
  • Automatically filter out sensitive values from responses based on your definitions
  • Can use Express routes or use dynamic functions with the operationId

Jump to Quickstart

How to Use

Prerequisites

Node v14 with Express v4+ or AWS Lamdba

Installing It

In your command line, type:

$ npm install --save firetail-js

Express.js HelloWorld

Place your API YAML inside the root path of your application. Then run:

// ========== Lets import what we are going to need
const express = require('express')
const firetailSetup = require("@public.firetail.io/firetail-api");
// ========== Create our server
const app = express()

// ========== setup request Body stash
app.use(
  express.raw({
    inflate: true, limit: '50mb', type: () => true,
  }))

// ========== firetail options
const firetailOpts = { dev:true, addApi: "./swagger.yaml" }

// ========== install the firetail middleware
app.use(firetailSetup(firetailOpts))

// ========== Add the end-point you want
//...
app.get('/', (req, res) => {
  res.send("FireTail sample")
})
//... They should match whats in your YAML

const port = 3001
// ========== Start our server
app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

AWS Lamdba HelloWorld

Place your API YAML inside the root path of your application. Then run:

// ========== Lets import what we are going to need
const firetailSetup = require("@public.firetail.io/firetail-api");

// ========== firetail options
const firetailOpts = { addApi: "./swagger.yaml" }
// IF you run locally via 'serverless offline'
// firetailOpts.lamdba = true // Add the lamdba flag

// ========== install the firetail middleware
const firetailWrapper = firetailSetup(firetailOpts)

// ========== Add the end-point you want
//...
module.exports.app = firetailWrapper((event,context) => {
  return {
    statusCode:200,
    body: "FireTail sample"
  };
})
//... They should match whats in your YAML

Configuration options. Configuration can be loaded in one of three ways.

  1. Via environment variables
  2. Inside the package.json under a "firetail" attribute
  3. Pass to the middleware at runtime as a configuration object

Note: 1 & 2 can only reference static values. Where as the 3rd option can handle static and dynamic

Important ⚠️

  • The addApi is the only mandatory option that must be passed!
  • package.json is not accessible by Lamdba! So Configuration must be in environment variables or passed in at runtime

Static values:

  • addApi[String]: The path to your openAPI YAML - *this can be a relative or absolute path
  • specificationDir[String]: The path to the directory where you will place controllers that map to the operationId for each endpoints referenced in your YAML
  • dev[Boolean]: This indicates what are the middleware should run in developer mode. Dev mode will ~ Default false
    1. Give helpful error messages in your rest API as well as using the developer
    2. Log event will be sent to your terminal. Instead of the firetail SAAS platform.

Dynamic values:

  • overRideError[Function] (err): a callback to replace the generated firetail error with a custom error you can generate specific to your platform/interfaces
  • operations[Object]: an object, where the keys that will match with the operationIds and executed in the same way an Express route would be
  • decodedJwt[Function] (headers), Encodes the JWT token from the header. Returning the JWT as JSON
  • authCallbacks[Object] Each key in this object maps to a function, represents a security scheme that can be used in your end-points
  • customBodyDecoders[Object] Each key in this object maps to a function. This is used to parse an unknown content-type into JSON so the middleware can apply the rules outlined in YAML file.

Examples: Dynamic Configuration

overRideError

  overRideError:(firetailErr)=>{
    const { status, message } = firetailErr
    if(404 === status){
        return "Looks like your lost"
    }
    return "Something broke"
  }

operations

When an End-point match is found in the YMAL file + it contains a reference to an operationId a corresponding function should be included in the options.

Yaml file

paths:
  /mydata:
    get:
      operationId: app.dataLoader

Node file

  operations:{
    app:{
        dataLoader: (req,res)=>{
            //... end-point logic
        }
    }
  }

authCallbacks

For each security schema used, you will need to provide a matching named function in the security object under options

Yaml file

components:
  securitySchemes:
    jwt:
      type: http
      scheme: bearer
      bearerFormat: JWT

Node file

  authCallbacks:{
    jwt:(decodedJwtAsJSON)=>{
    const { authorization } = decodedJwtAsJSON
       //... run securitie logic
       // true if authenticated
       return false // true
    }
  }

customBodyDecoders

The below example, shows how you would use an XML validation with the xml2json module

Yaml file

paths:
  /my_pet:
    get:
      summary: read a pet
      responses:
        '200':
          content:
            application/xml:
              schema:
                $ref: '#/components/schemas/Pet'

Node file

  customBodyDecoders:{
    'application/xml': stringBody => {
        return parseXmlString.toJson(stringBody,{object:true})
    }
  }