UNPKG

@piggly/fastify-chassis

Version:

An ESM/CommonJS toolkit to help you to do common operations in your back-end applications with Fastify and NodeJS.

67 lines 2.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UnauthorizedAccessEvent = void 0; const event_bus_1 = require("@piggly/event-bus"); const ddd_toolkit_1 = require("@piggly/ddd-toolkit"); const index_js_1 = require("../utils/index.js"); /** * @file This event should be published when an unauthorized access occurs. * @since 7.0.0 * @copyright Piggly Lab 2025 * @author Caique Araujo <caique@piggly.com.br> */ class UnauthorizedAccessEvent extends event_bus_1.EventPayload { /** * Create a new instance of the event. * * You may issue this event when a request is unauthorized for * any reason such as: 401 or 403 HTTP responses. * * @param {FastifyRequest} request The request. * @memberof UnauthorizedAccessEvent * @since 7.0.0 * @author Caique Araujo <caique@piggly.com.br> */ constructor(request) { super('UNAUTHORIZED_ACCESS_EVENT', { ip: (0, index_js_1.getIp)(request), method: request.method, url: request.url, }); } /** * Publish the event. * * This event should be completed on a graceful shutdown or interruption. * But use carrefully, as it will heavily increase memory usage if you have a lot of * events to wait. * * If you don't use the EventBus.interface.cleanup you * may change flag removing "wait_onclean" to false when * publishing this event. * * It will be logged as warn when log is true. with parameters: * - event: UNAUTHORIZED_ACCESS_EVENT * - ip: ip * - method: method * - url: url * * If you enable FileLogStreamService on LoggerService, the message will be: * [date-time] [uuid] (warn) "UNAUTHORIZED_ACCESS_EVENT" - "ip" - "method" - "url" * * @param {FastifyRequest} request The request. The current IP will be collected from the request. * @param {EventOptions} options The options. * @memberof UnauthorizedAccessEvent * @since 7.0.0 * @author Caique Araujo <caique@piggly.com.br> */ static publish(request, options = { log: true, wait_onclean: false }) { const event = new UnauthorizedAccessEvent(request); if (options.log) { ddd_toolkit_1.LoggerService.softResolve().warn('UNAUTHORIZED_ACCESS_EVENT', event.data.ip, event.data.method, event.data.url); } event_bus_1.EventBus.instance.send(event, options.wait_onclean); } } exports.UnauthorizedAccessEvent = UnauthorizedAccessEvent; //# sourceMappingURL=UnauthorizedAccessEvent.js.map