This repository was archived by the owner on Sep 2, 2022. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -28,24 +28,28 @@ npm install @apilytics/core
28283 . Set your api key and create a middleware which measures the execution time and sends the metrics:
2929* A good practice is to securely store the API key as an environment variable.
3030You 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 ` .*
31+ and make the middleware be disabled if the key is ` undefined ` .*
3232
3333` my-apilytics-middleware.js ` :
3434``` javascript
3535import { milliSecondTimer , sendApilyticsMetrics } from ' @apilytics/core' ;
3636
37- const myApilyticsMiddleware = (/* ... */ ) => {
38- const timer = milliSecondTimer ();
39-
40- // Call/await the request handler that the middleware wraps here.
37+ const myApilyticsMiddleware = async (req , handler ) => {
38+ const apiKey = process .env .APILYTICS_API_KEY ;
39+ if (! apiKey) {
40+ return await handler (req);
41+ }
4142
43+ const timer = milliSecondTimer ();
44+ const res = await handler (req);
4245 sendApilyticsMetrics ({
43- apiKey: process . env . APILYTICS_API_KEY ,
46+ apiKey,
4447 path: req .path ,
4548 method: req .method ,
4649 statusCode: res .statusCode ,
4750 timeMillis: timer (),
4851 });
52+ return res;
4953};
5054```
5155
You can’t perform that action at this time.
0 commit comments