Skip to content
This repository was archived by the owner on Sep 2, 2022. It is now read-only.

Commit 7394ed2

Browse files
committed
Handle unset APILYTICS_API_KEY in README's "other frameworks" example
1 parent b065d0d commit 7394ed2

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

packages/core/README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,28 @@ npm install @apilytics/core
2828
3. 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.
3030
You 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
3535
import { 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

0 commit comments

Comments
 (0)