apitally
Version:
Simple API monitoring & analytics for REST APIs built with Express, Fastify, NestJS, AdonisJS, Hono, H3, Elysia, Hapi, and Koa.
104 lines • 2.95 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
import { AsyncLocalStorage } from "node:async_hooks";
import { ApitallyClient } from "../common/client.js";
import { getPackageVersion } from "../common/packageVersions.js";
import { patchConsole, patchPinoLogger } from "../loggers/index.js";
const _ApitallyProvider = class _ApitallyProvider {
app;
constructor(app) {
this.app = app;
}
register() {
this.app.container.singleton("apitallyClient", () => {
const config = this.app.config.get("apitally");
return new ApitallyClient(config);
});
this.app.container.singleton("apitallyLogsContext", () => {
return new AsyncLocalStorage();
});
}
async start() {
const client = await this.app.container.make("apitallyClient");
if (client.requestLogger.config.captureLogs) {
const logsContext = await this.app.container.make("apitallyLogsContext");
const logger = await this.app.container.make("logger");
patchPinoLogger(logger.pino, logsContext);
patchConsole(logsContext);
}
}
async ready() {
const client = await this.app.container.make("apitallyClient");
const router = await this.app.container.make("router");
const paths = listRoutes(router);
const versions = getVersions(this.app.config.get("apitally.appVersion"));
const startupData = {
paths,
versions,
client: "js:adonisjs"
};
client.setStartupData(startupData);
client.startSync();
}
async shutdown() {
const client = await this.app.container.make("apitallyClient");
await client.handleShutdown();
}
};
__name(_ApitallyProvider, "ApitallyProvider");
let ApitallyProvider = _ApitallyProvider;
function listRoutes(router) {
const routes = router.toJSON();
const paths = [];
for (const domain in routes) {
for (const route of routes[domain]) {
for (const method of route.methods) {
if (![
"HEAD",
"OPTIONS"
].includes(method.toUpperCase())) {
paths.push({
method: method.toUpperCase(),
path: route.pattern
});
}
}
}
}
return paths;
}
__name(listRoutes, "listRoutes");
function getVersions(appVersion) {
const versions = [
[
"nodejs",
process.version.replace(/^v/, "")
]
];
const adonisJsVersion = getPackageVersion("@adonisjs/core");
const apitallyVersion = getPackageVersion("../..");
if (adonisJsVersion) {
versions.push([
"adonisjs",
adonisJsVersion
]);
}
if (apitallyVersion) {
versions.push([
"apitally",
apitallyVersion
]);
}
if (appVersion) {
versions.push([
"app",
appVersion
]);
}
return Object.fromEntries(versions);
}
__name(getVersions, "getVersions");
export {
ApitallyProvider as default
};
//# sourceMappingURL=provider.js.map