88[ ![ node versions] ( https://img.shields.io/node/v/@apilytics/core )] ( #what-nodejs-versions-does-the-package-work-with )
99[ ![ license] ( https://img.shields.io/npm/l/@apilytics/core )] ( https://github.com/apilytics/apilytics-node/blob/master/packages/core/LICENSE )
1010
11+ Apilytics is a service that lets you analyze operational, performance and security metrics from your APIs without infrastructure-level logging.
12+
1113Make sure to check out our out-of-the-box middleware packages first:
1214
13- - [ ** Express** ] ( ../express/README.md#installation )
15+ - [ ** Express** ( ` @apilytics/express ` ) ] ( ../express/README.md#installation )
1416
15- - [ ** Next.js** ] ( ../next/README.md#installation )
17+ - [ ** Next.js** ( ` @apilytics/next ` ) ] ( ../next/README.md#installation )
1618
1719## Installation
1820
@@ -28,24 +30,28 @@ npm install @apilytics/core
28303 . Set your api key and create a middleware which measures the execution time and sends the metrics:
2931* A good practice is to securely store the API key as an environment variable.
3032You can leave the env variable unset in e.g. development and test environments,
31- the middleware will be automatically disabled if the key is ` undefined ` .*
33+ and make the middleware be disabled if the key is ` undefined ` .*
3234
3335` my-apilytics-middleware.js ` :
3436``` javascript
3537import { milliSecondTimer , sendApilyticsMetrics } from ' @apilytics/core' ;
3638
37- const myApilyticsMiddleware = (/* ... */ ) => {
38- const timer = milliSecondTimer ();
39-
40- // Call/await the request handler that the middleware wraps here.
39+ const myApilyticsMiddleware = async (req , handler ) => {
40+ const apiKey = process .env .APILYTICS_API_KEY ;
41+ if (! apiKey) {
42+ return await handler (req);
43+ }
4144
45+ const timer = milliSecondTimer ();
46+ const res = await handler (req);
4247 sendApilyticsMetrics ({
43- apiKey: process . env . APILYTICS_API_KEY ,
48+ apiKey,
4449 path: req .path ,
4550 method: req .method ,
4651 statusCode: res .statusCode ,
4752 timeMillis: timer (),
4853 });
54+ return res;
4955};
5056```
5157
0 commit comments