1.5.0 • Published 2 years ago

@apilytics/core v1.5.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@apilytics/core

npm ci codecov typescript code style: prettier node versions license

Apilytics is a service that lets you analyze operational, performance and security metrics from your APIs easily.

Make sure to check out our out-of-the-box middleware packages first:

Installation

  1. Sign up and get your API key from https://apilytics.io - we offer a completely free trial with no credit card required!

  2. Install this package:

yarn add @apilytics/core
# OR
npm install @apilytics/core
  1. Set your api key and create a middleware which measures the execution time and sends the metrics:\ A good practice is to securely store the API key as an environment variable. You can leave the env variable unset in e.g. development and test environments, and make the middleware be disabled if the key is undefined.

my-apilytics-middleware.js:

import { milliSecondTimer, sendApilyticsMetrics } from '@apilytics/core';

const myApilyticsMiddleware = async (req, handler) => {
  const apiKey = process.env.APILYTICS_API_KEY;
  if (!apiKey) {
    return handler(req);
  }

  const timer = milliSecondTimer();
  const res = await handler(req);

  sendApilyticsMetrics({
    apiKey,
    path: req.path,
    query: req.queryString,
    method: req.method,
    statusCode: res.statusCode,
    requestSize: req.bodyBytes.length,
    responseSize: res.bodyBytes.length,
    userAgent: req.headers['user-agent'],
    ip: req.headers['x-forwarded-for']?.split(',')[0].trim(),
    timeMillis: timer(),
  });
  return res;
};

Frequently Asked Questions

Does the middleware slow down my backend?

  • No. The middleware does all of its requests to the Apilytics API in the background, by using promises without awaiting them, so it will not slow down your normal request handling.

What 3rd party dependencies does @apilytics/core have?

  • None :)

What Node.js versions does the package work with?