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

Commit 2a2948f

Browse files
authored
Merge pull request #11 from apilytics/documentation
Thoroughly document all exported symbols with JSDoc
2 parents f21a14b + 1754916 commit 2a2948f

5 files changed

Lines changed: 80 additions & 6 deletions

File tree

packages/core/src/index.ts

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,64 @@ interface Params {
66
apiKey: string;
77
path: string;
88
method: string;
9-
statusCode: number | null;
109
timeMillis: number;
1110
query?: string;
11+
statusCode?: number | null;
1212
apilyticsIntegration?: string;
1313
integratedLibrary?: string;
1414
}
1515

16+
/**
17+
* Send API analytics data to Apilytics (https://apilytics.io).
18+
* Does the sending as fire-and-forget background task.
19+
*
20+
* @param params
21+
* @param params.apiKey - The API key for your Apilytics origin.
22+
* @param params.path - Path of the user's HTTP request, e.g. '/foo/bar/123'.
23+
* @param params.method - Method of the user's HTTP request, e.g. 'GET'.
24+
* @param params.timeMillis - The amount of time in milliseconds it took
25+
* to respond to the user's request.
26+
* @param params.query - Optional query string of the user's HTTP request
27+
* e.g. 'key=val&other=123'. An empty string and null are treated equally.
28+
* Can have an optional '?' at the start.
29+
* @param params.statusCode - Status code for the sent HTTP response.
30+
* Can be omitted (or null) if the middleware could not get the status code
31+
* for the response. E.g. if the inner request handling threw an exception.
32+
* @param params.apilyticsIntegration - Name of the Apilytics integration that's
33+
* calling this, e.g. 'apilytics-node-express'.
34+
* No need to pass this when calling from user code.
35+
* @param params.integratedLibrary - Name and version of the integration that
36+
* this is used in, e.g. 'express/4.17.2'.
37+
* No need to pass this when calling from user code.
38+
*
39+
* @example
40+
*
41+
* const timer = milliSecondTimer();
42+
* const res = await handler(req);
43+
* sendApilyticsMetrics({
44+
* apikey: "<your-api-key>",
45+
* path: req.path,
46+
* query: req.queryString,
47+
* method: req.method,
48+
* statusCode: res.statusCode,
49+
* timeMillis: timer(),
50+
* });
51+
*/
1652
export const sendApilyticsMetrics = ({
1753
apiKey,
1854
path,
19-
query,
2055
method,
21-
statusCode,
2256
timeMillis,
57+
query,
58+
statusCode,
2359
apilyticsIntegration,
2460
integratedLibrary,
2561
}: Params): void => {
2662
const data = JSON.stringify({
2763
path,
2864
query: query || undefined,
2965
method,
30-
statusCode,
66+
statusCode: statusCode ?? undefined,
3167
timeMillis,
3268
});
3369
let apilyticsVersion = `${
@@ -65,6 +101,13 @@ export const sendApilyticsMetrics = ({
65101
});
66102
};
67103

104+
/**
105+
* Times the amount of milliseconds something takes to execute.
106+
* The timer starts when this is initially called.
107+
*
108+
* @returns A function that can be called to stop the timer.
109+
* That function's return value is the elapsed time.
110+
*/
68111
export const milliSecondTimer = (): (() => number) => {
69112
const startTimeNs = process.hrtime.bigint();
70113

packages/express/src/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ import type { NextFunction, Request, RequestHandler, Response } from 'express';
55

66
const EXPRESS_VERSION = require('express/package.json').version;
77

8+
/**
9+
* Express middleware that sends API analytics data to Apilytics (https://apilytics.io).
10+
*
11+
* @param apiKey - The API key for your Apilytics origin.
12+
* @returns An Express middleware that can be passed to `app.use()`.
13+
*
14+
* @example
15+
*
16+
* const { apilyticsMiddleware } = require('@apilytics/express');
17+
* const express = require('express');
18+
*
19+
* const app = express();
20+
*
21+
* app.use(apilyticsMiddleware("your-api-key"));
22+
*/
823
export const apilyticsMiddleware = (
924
apiKey: string | undefined,
1025
): RequestHandler => {

packages/next/__tests__/index.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ describe('withApilytics()', () => {
174174
expect(data).toStrictEqual({
175175
path: '/error',
176176
method: 'GET',
177-
statusCode: null,
178177
timeMillis: expect.any(Number),
179178
});
180179
});

packages/next/src/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@ import type { NextApiHandler, NextApiRequest, NextApiResponse } from 'next';
55

66
const NEXT_VERSION = require('next/package.json').version;
77

8+
/**
9+
* Next.js middleware that sends API analytics data to Apilytics (https://apilytics.io).
10+
*
11+
* @param handler - Next.js API route handler that this middleware should apply to.
12+
* @param apiKey - The API key for your Apilytics origin.
13+
* @returns A new API route handler which wraps the one that was passed in.
14+
*
15+
* @example
16+
*
17+
* import { withApilytics } from '@apilytics/next';
18+
*
19+
* const handler = async (req, res) => {
20+
* // ...
21+
* };
22+
*
23+
* export default withApilytics(handler, "<your-api-key>");
24+
*/
825
export const withApilytics = <T>(
926
handler: NextApiHandler<T>,
1027
apiKey: string | undefined,

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"strict": true,
88
"declaration": true,
99
"sourceMap": true,
10-
"removeComments": true,
10+
"inlineSources": true,
1111
"esModuleInterop": true,
1212
"skipLibCheck": true,
1313
"allowSyntheticDefaultImports": true,

0 commit comments

Comments
 (0)