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

btrz-http-service

v1.29.0

Published

HTTP related utilities for Betterez APIs

Downloads

383

Readme

btrz-http-service

HTTP related utilities for Betterez APIs

Utilities:

Swagger request handler formatter

Generates a definition for swagger including a request handler and it's schema.

An example Handler Class must implement "getSpec()" and "handler(req, res)" methods, like:

class RequestHandler {
  getSpec () {
    return {
      "description" : "endpoint description",
      "path" : "/endpoint",
      "summary" : "endpoint summary",
      "method": "POST",
      "parameters": [
        this.swagger.paramTypes.body("items", "the items", "Schema", null, true),
      ],
      "produces" : ["application/json"],
      "type" : "Schema",
      "errorResponses" : [],
      "nickname" : "nick"
    };
  }
  handler (req, res) {
    .... request handler code ...
  }
}

So to generate the swagger request handler:

let swaggerRequestHandler = require("btrz-http-service").swaggerRequestHandler;
let handler = new RequestHandler();
let swaggerHandler = swaggerRequestHandler(handler);

Just like in Express with Connect, "multiple callback functions that behave just like middleware" can be given to swaggerRequestHandler, and they will be called one after the other. Middleware callbacks must have the following signature: function (req, res, next) The last argument of swaggerRequestHandler must always be a Handler instance implementing "getSpec()" and "handler(req, res)" methods. An example:

let swaggerRequestHandler = require("btrz-http-service").swaggerRequestHandler;
let handler = new RequestHandler();
let swaggerHandler = swaggerRequestHandler(passportAuthenticate, otherMiddleware, handler);

Success/Error handlers

Success handler expects data to be send with 200 OK status code.

Error handler expects an Error (or a ValidationError, see below). And responds with the error message and status code 400 (status code can be overriden, see below).

Sample usage:

class RequestHandler {
  getSpec () {
   .......
  }
  handler (req, res) {
   doSomethingAsync(req.body)
     .then(function () {
      return doSomethingElse();
    })
    .then(ResponseHandlers.success(res))
    .catch(ResponseHandlers.error(res));
 }
}

Notice that ResponseHandlers.success and ResponseHandlers.error both expect the response object, and return a function.

Notice that ResponseHandlers.success and ResponseHandlers.error must be added at the end of the promises chain, and only once.

Swagger schemas

Some common schemas to avoid duplication. We should grow these schemas when we find common patterns.

  bzDate (The schema for BzDate)
  errorResponse (The schema for the error responses)
  defaultPagingProps (The properties that are common to any list that supports paging)

Swagger Schema Validation

Validates a body against a handler schema (a schema like the one in getSpec() above). Returns an array of ValidationErrors with any schema mismatch.

let validateSwaggerSchema = require("btrz-http-service").validateSwaggerSchema;
validateSwaggerSchema(validatorFunction, handlerSpec, schemaModelsDefinitionJSON, requestObject)

ValidationError error type

Subclass of Error, supporting an error code, message, and HTTP status override. Usage:

throw new ValidationError(errorCode, message, statusCode);
throw new ValidationError("CODE", "readable message", 418)

The Error request handler will catch any error and send a 500 response with the message.

If it receives a ValidationError or an array of ValidationErrors, it will send a response with the message and status code 400.

To change the HTTP status code, throw a validation error like:

throw new ValidationError(errorCode, message, statusCode);

for example:

throw new ValidationError("NO_CART", "Cart not found", 404);

Paginated Response Builder

This utility builds a standard response for a paginated resource.

Inputs:

  • result: the list of resources in the current page, in the form {resourceName: list}
  • query: the query directly from the request (req.query)
  • totalRecords: the count of all the resources
  • pageSize: the page size from the config (config.pageSize)
  • baseUrl: the FULL url of the resource (domain/api/resource)

Output:

{
  resourceName: list, // the resourceName as given in the "result" input
  count, // the "totalRecords" input
  next, // the full URL to the next page, empty string if no next page
  previous // the full URL to the previous page, empty string if no previous page
}

Usage example in Handler:

const result = {customers: countedCustomers.list},
  url = `${this.config.fullDomain()}/accounts${this.getSpec().path}`;
return ResponseBuilder.buildResponse(result, req.query, countedCustomers.count, this.config.pageSize, url);

Register

For the new API model, call this method to register all handlers and models

const register = require("btrz-http-service").register;
const basePath = `${__dirname}/path/to/modules`;

register(basePath, {models: require("./models/"),
    authenticator,
    swagger,
    logger,
    simpleDao,
    config});

Arguments:

  • basePath: relative path to start searching for handlers
  • options:
    • models // default to {models: {}} if not passed
    • authenticator // an instance of Authenticator from "btrz-auth-api-key"
    • swagger // an instance of swagger from "btrz-swagger-express"
    • logger // an instance of Logger from "btrz-logger"
    • simpleDao // an instance of BtrzSimpleDao from "btrz-simple-dao"
    • config // a configuration object for the API

validationPatterns

A series of RegExp patterns to use on common Swagger validations.

Each key comes in two variations a RegExp and a String, each identified with the suffix.

_REGEXP _STRING

  DATE_MMDDYYYY_PATTERN: to validate dates in the mmddyyyy format
  DATE_YYYY_MM_DD_PATTERN: to validate dates in the yyyy-mm-dd format
  OBJECT_ID_PATTERN: to validate a Mongo ObjectID
  TIME_HHMM_PATTERN: to validate time in the HH:MM format
  UUID4_PATTERN: to validate uuid v4 ids