@tinyhttp/logger
Advanced tools
+2
-11
| { | ||
| "name": "@tinyhttp/logger", | ||
| "version": "1.3.2", | ||
| "version": "1.3.3", | ||
| "type": "module", | ||
@@ -14,13 +14,4 @@ "description": "Minimal and flexible HTTP logger.", | ||
| }, | ||
| "main": "./dist/index.cjs", | ||
| "types": "./dist/index.d.ts", | ||
| "module": "./dist/index.js", | ||
| "exports": { | ||
| ".": { | ||
| "import": "./dist/index.js", | ||
| "require": "./dist/index.cjs" | ||
| }, | ||
| "./package.json": "./package.json", | ||
| "./": "./" | ||
| }, | ||
| "exports": "./dist/index.js", | ||
| "keywords": [ | ||
@@ -27,0 +18,0 @@ "tinyhttp", |
| import { ServerResponse as Response, IncomingMessage as Request } from 'http'; | ||
| export interface LoggerOptions { | ||
| methods?: string[]; | ||
| output?: { | ||
| color: boolean; | ||
| callback: (string: string) => void; | ||
| }; | ||
| timestamp?: boolean | { | ||
| format?: string; | ||
| }; | ||
| emoji?: boolean; | ||
| ip?: boolean; | ||
| } | ||
| export declare const logger: (options?: LoggerOptions) => (req: Request, res: Response, next?: () => void) => void; |
| import { magenta, bold, red, cyan } from 'colorette'; | ||
| import statusEmoji from 'http-status-emojis'; | ||
| import dayjs from 'dayjs'; | ||
| import { METHODS } from 'http'; | ||
| const compileArgs = (args, req, res, options = {}, status, msg) => { | ||
| var _a, _b; | ||
| const { method } = req; | ||
| const { statusCode } = res; | ||
| const url = req.originalUrl || req.url; | ||
| const methods = (_a = options.methods) !== null && _a !== void 0 ? _a : METHODS; | ||
| const timestamp = (_b = options.timestamp) !== null && _b !== void 0 ? _b : false; | ||
| const emojiEnabled = options.emoji; | ||
| if (methods.includes(method) && timestamp) { | ||
| args.push(`${dayjs() | ||
| .format(typeof timestamp !== 'boolean' && timestamp.format ? timestamp.format : 'HH:mm:ss') | ||
| .toString()} - `); | ||
| } | ||
| if (options.ip) | ||
| args.push(req.ip); | ||
| if (emojiEnabled) | ||
| args.push(statusEmoji[statusCode]); | ||
| args.push(method); | ||
| args.push(status || res.statusCode); | ||
| args.push(msg || res.statusMessage); | ||
| args.push(url); | ||
| }; | ||
| const logger = (options = {}) => { | ||
| var _a, _b; | ||
| const methods = (_a = options.methods) !== null && _a !== void 0 ? _a : METHODS; | ||
| const output = (_b = options.output) !== null && _b !== void 0 ? _b : { callback: console.log, color: true }; | ||
| return (req, res, next) => { | ||
| res.on('finish', () => { | ||
| const args = []; | ||
| if (methods.includes(req.method)) { | ||
| const s = res.statusCode.toString(); | ||
| if (!output.color) { | ||
| compileArgs(args, req, res, options); | ||
| const m = args.join(' '); | ||
| output.callback(m); | ||
| } | ||
| else { | ||
| switch (s[0]) { | ||
| case '2': | ||
| compileArgs(args, req, res, options, cyan(bold(s)), cyan(res.statusMessage)); | ||
| output.callback(args.join(' ')); | ||
| break; | ||
| case '4': | ||
| compileArgs(args, req, res, options, red(bold(s)), red(res.statusMessage)); | ||
| output.callback(args.join(' ')); | ||
| break; | ||
| case '5': | ||
| compileArgs(args, req, res, options, magenta(bold(s)), magenta(res.statusMessage)); | ||
| output.callback(args.join(' ')); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| next === null || next === void 0 ? void 0 : next(); | ||
| }; | ||
| }; | ||
| export { logger }; |
Empty package
Supply chain riskPackage does not contain any code. It may be removed, is name squatting, or the result of a faulty package publish.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
1
-66.67%5301
-37.85%3
-40%0
-100%