Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimon committed Nov 1, 2024
1 parent 4cbf1a7 commit 6fb5fd3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/express/listEndpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,14 @@ const parseStack = function (stack, basePath, endpoints) {
return endpoints;
};

const getEndpoints = function (app) {
const getEndpoints = function (app, basePath) {
const endpoints = parseEndpoints(app);
return endpoints.flatMap((route) =>
route.methods
.filter((method) => !["HEAD", "OPTIONS"].includes(method.toUpperCase()))
.map((method) => ({
method,
path: route.path,
path: basePath + route.path,
})),
);
};
Expand Down
13 changes: 9 additions & 4 deletions src/express/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Express, NextFunction, Request, Response, Router } from "express";
import type { Express, NextFunction, Request, Response } from "express";
import { Router } from "express";
import type { ILayer } from "express-serve-static-core";
import { performance } from "perf_hooks";

Expand All @@ -20,12 +21,15 @@ declare module "express" {
}
}

export const useApitally = (app: Express | Router, config: ApitallyConfig) => {
export const useApitally = (
app: Express | Router,
config: ApitallyConfig & { basePath?: string },
) => {
const client = new ApitallyClient(config);
const middleware = getMiddleware(app, client);
app.use(middleware);
setTimeout(() => {
client.setStartupData(getAppInfo(app, config.appVersion));
client.setStartupData(getAppInfo(app, config.basePath, config.appVersion));
}, 1000);
};

Expand Down Expand Up @@ -229,6 +233,7 @@ const subsetJoiMessage = (message: string, key: string) => {

const getAppInfo = (
app: Express | Router,
basePath?: string,
appVersion?: string,
): StartupData => {
const versions: Array<[string, string]> = [
Expand All @@ -246,7 +251,7 @@ const getAppInfo = (
versions.push(["app", appVersion]);
}
return {
paths: listEndpoints(app),
paths: listEndpoints(app, basePath || ""),
versions: Object.fromEntries(versions),
client: "js:express",
};
Expand Down
6 changes: 4 additions & 2 deletions tests/express/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ describe("Middleware for Express router", () => {
expect(
requests.some(
(r) =>
r.method === "GET" && r.path === "/hello" && r.status_code === 200,
r.method === "GET" &&
r.path === "/api/hello" &&
r.status_code === 200,
),
).toBe(true);
});
Expand All @@ -163,7 +165,7 @@ describe("Middleware for Express router", () => {
expect(client.startupData?.paths).toEqual([
{
method: "GET",
path: "/hello",
path: "/api/hello",
},
]);
});
Expand Down
1 change: 1 addition & 0 deletions tests/express/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export const getAppWithMiddlewareOnRouter = () => {
clientId: CLIENT_ID,
env: ENV,
appVersion: "1.2.3",
basePath: "/api",
});

router.get("/hello", (req, res) => {
Expand Down

0 comments on commit 6fb5fd3

Please sign in to comment.