UNPKG

apitally

Version:

Simple API monitoring & analytics for REST APIs built with Express, Fastify, NestJS, AdonisJS, Hono, H3, Elysia, Hapi, and Koa.

105 lines 4.51 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var nestjs_exports = {}; __export(nestjs_exports, { setConsumer: () => setConsumer, useApitally: () => useApitally }); module.exports = __toCommonJS(nestjs_exports); var import_common = require("@nestjs/common"); var import_rxjs = require("rxjs"); var _a; function _ts_decorate(decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; } __name(_ts_decorate, "_ts_decorate"); function _ts_metadata(k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); } __name(_ts_metadata, "_ts_metadata"); let setConsumerFn = null; async function useApitally(app, config) { const httpAdapter = app.getHttpAdapter(); const instance = httpAdapter.getInstance(); const platform = httpAdapter.constructor.name.toLowerCase().includes("fastify") ? "fastify" : "express"; if (platform === "express") { const { useApitally: useApitally2, setConsumer: setConsumer2 } = await import("../express/index.js"); setConsumerFn = setConsumer2; useApitally2(instance, config); } else if (platform === "fastify") { const { apitallyPlugin, setConsumer: setConsumer2 } = await import("../fastify/index.js"); setConsumerFn = setConsumer2; await instance.register(apitallyPlugin, config); } app.useGlobalInterceptors(new ApitallyInterceptor(platform)); } __name(useApitally, "useApitally"); function setConsumer(request, consumer) { if (setConsumerFn) { setConsumerFn(request, consumer); } } __name(setConsumer, "setConsumer"); let ApitallyInterceptor = (_a = class { platform; constructor(platform) { this.platform = platform; } intercept(context, next) { return next.handle().pipe((0, import_rxjs.catchError)((exception) => { if (context.getType() === "http") { const ctx = context.switchToHttp(); const res = ctx.getResponse(); if (this.platform === "express" && res.locals) { res.locals.serverError = exception; } else if (this.platform === "fastify" && (!exception.statusCode || exception.statusCode === 500)) { res.serverError = exception; } } return (0, import_rxjs.throwError)(() => exception); })); } }, __name(_a, "ApitallyInterceptor"), _a); ApitallyInterceptor = _ts_decorate([ (0, import_common.Injectable)(), _ts_metadata("design:type", Function), _ts_metadata("design:paramtypes", [ String ]) ], ApitallyInterceptor); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { setConsumer, useApitally }); //# sourceMappingURL=index.cjs.map