Skip to content

Commit

Permalink
fix: types with typeof Log
Browse files Browse the repository at this point in the history
  • Loading branch information
commenthol committed Oct 7, 2023
1 parent 4658f77 commit a488b64
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 65 deletions.
36 changes: 23 additions & 13 deletions src/browserLogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { Log } from './node.js'
import { adjustLevel, toNumLevel, fromNumLevel, INFO } from './utils.js'

// https://css-tricks.com/snippets/html/base64-encode-of-1x1px-transparent-gif/
const gif = Buffer.from('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', 'base64')
const gif = Buffer.from(
'R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7',
'base64'
)

/**
* @param {string} url
Expand Down Expand Up @@ -49,31 +52,38 @@ function parseJson (str) {
}

/**
* @typedef {object} MwLogOption
* @property {number} [maxSize=100] max number of different name loggers
* @property {boolean} [logAll=false] log everything even strings
* @property {boolean} [levelNumbers] log levels as numbers
* @property {Log} [Log] different extended Log class, e.g. LogEcs
* @typedef {{
* maxSize?: number
* logAll?: boolean
* levelNumbers?: boolean
* Log?: typeof Log
* }} MwLogOption
* - [maxSize=100] max number of different name loggers
* - [logAll=false] log everything even strings
* - [levelNumbers=false] log levels as numbers
* - [Log=Log] allows overwrite of Log class
*/

/**
* connect middleware which logs browser based logs on server side;
* sends a transparent gif as response
* @param {MwLogOption} [opts]
* @param {MwLogOption} [options]
* @return {function} connect middleware
*/
export function browserLogs (opts = {}) {
opts = Object.assign({ maxSize: 100, logAll: false, levelNumbers: false }, opts)
export function browserLogs (options) {
const opts = {
maxSize: 100,
logAll: false,
levelNumbers: false,
...(options || {})
}

const LogCls = opts?.Log || Log
// @ts-expect-error
const log = opts.logAll ? new LogCls('debug-level:browser') : undefined
const loggers = new Loggers(opts)

return function _browserLogs (req, res) {
const query = req.query
? req.query
: parseQuery(req.url)
const query = req.query ? req.query : parseQuery(req.url)

res.setHeader('Cache-Control', 'no-store, no-cache')
res.write(gif)
Expand Down
13 changes: 6 additions & 7 deletions src/httpLogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ const serializers = {
* @typedef {import('node:http').IncomingMessage} IncomingMessage
* @typedef {import('node:http').ServerResponse} ServerResponse
* @typedef {import('./node.js').LogOptions} LogOptions
*
*//**
* @typedef {object} ExtIncomingMessageId
* @property {string} [id] request Id
*
*//**
* @typedef {IncomingMessage & ExtIncomingMessageId} IncomingMessageWithId
*
*//**
* @typedef {object} ExtLogOptionsHttpLog
* @property {() => string} [customGenerateRequestId]
*
* @typedef {LogOptions & ExtLogOptionsHttpLog} LogOptionsHttpLog
*//**
* @typedef {LogOptions & ExtLogOptionsHttpLog & {Log: typeof Log}} LogOptionsHttpLog
*/

/**
* @param {string} [namespace='debug-level:http']
* @param {LogOptionsHttpLog & {Log: Log}} [opts]
* @param {LogOptionsHttpLog} [opts]
* @returns {(req: IncomingMessageWithId, res: ServerResponse, next: Function) => void} connect middleware
*/
export function httpLogs (namespace, opts) {
Expand All @@ -45,7 +45,6 @@ export function httpLogs (namespace, opts) {
...(options.serializers || {})
}

// @ts-expect-error
const log = new options.Log(namespace || 'debug-level:http', options)
const generateId = options.customGenerateRequestId || generateRequestId

Expand Down
7 changes: 5 additions & 2 deletions src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ import { Log } from './node.js'

const loggers = {}

/**
* @typedef {import('./node.js').LogOptions & {Log: typeof Log}} LogOptions
*/

/**
* @param {string} namespace
* @param {import('./node.js').LogOptions & {Log: Log}} [opts]
* @param {LogOptions} [opts]
*/
export function logger (namespace, opts) {
const { Log: optsLog, ..._opts } = opts || {}
const LogCls = optsLog || Log

let log = loggers[namespace]
if (!log) {
// @ts-expect-error
log = loggers[namespace] = new LogCls(namespace, _opts)
}
return log
Expand Down
46 changes: 23 additions & 23 deletions types/browserLogs.d.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
/**
* @typedef {object} MwLogOption
* @property {number} [maxSize=100] max number of different name loggers
* @property {boolean} [logAll=false] log everything even strings
* @property {boolean} [levelNumbers] log levels as numbers
* @property {Log} [Log] different extended Log class, e.g. LogEcs
* @typedef {{
* maxSize?: number
* logAll?: boolean
* levelNumbers?: boolean
* Log?: typeof Log
* }} MwLogOption
* - [maxSize=100] max number of different name loggers
* - [logAll=false] log everything even strings
* - [levelNumbers=false] log levels as numbers
* - [Log=Log] allows overwrite of Log class
*/
/**
* connect middleware which logs browser based logs on server side;
* sends a transparent gif as response
* @param {MwLogOption} [opts]
* @param {MwLogOption} [options]
* @return {function} connect middleware
*/
export function browserLogs(opts?: MwLogOption | undefined): Function;
export function browserLogs(options?: MwLogOption | undefined): Function;
/**
* - [maxSize=100] max number of different name loggers
* - [logAll=false] log everything even strings
* - [levelNumbers=false] log levels as numbers
* - [Log=Log] allows overwrite of Log class
*/
export type MwLogOption = {
/**
* max number of different name loggers
*/
maxSize?: number | undefined;
/**
* log everything even strings
*/
logAll?: boolean | undefined;
/**
* log levels as numbers
*/
levelNumbers?: boolean | undefined;
/**
* different extended Log class, e.g. LogEcs
*/
Log?: Log | undefined;
maxSize?: number;
logAll?: boolean;
levelNumbers?: boolean;
Log?: typeof Log;
};
import { Log } from "./node.js";
33 changes: 17 additions & 16 deletions types/httpLogs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@
* @typedef {import('node:http').IncomingMessage} IncomingMessage
* @typedef {import('node:http').ServerResponse} ServerResponse
* @typedef {import('./node.js').LogOptions} LogOptions
*
* @typedef {object} ExtIncomingMessageId
* @property {string} [id] request Id
*
* @typedef {IncomingMessage & ExtIncomingMessageId} IncomingMessageWithId
*
* @typedef {object} ExtLogOptionsHttpLog
* @property {() => string} [customGenerateRequestId]
*
* @typedef {LogOptions & ExtLogOptionsHttpLog} LogOptionsHttpLog
*/
*/ /**
* @typedef {object} ExtIncomingMessageId
* @property {string} [id] request Id
*/ /**
* @typedef {IncomingMessage & ExtIncomingMessageId} IncomingMessageWithId
*/ /**
* @typedef {object} ExtLogOptionsHttpLog
* @property {() => string} [customGenerateRequestId]
*/ /**
* @typedef {LogOptions & ExtLogOptionsHttpLog & {Log: typeof Log}} LogOptionsHttpLog
*/
/**
* @param {string} [namespace='debug-level:http']
* @param {LogOptionsHttpLog & {Log: Log}} [opts]
* @param {LogOptionsHttpLog} [opts]
* @returns {(req: IncomingMessageWithId, res: ServerResponse, next: Function) => void} connect middleware
*/
export function httpLogs(namespace?: string | undefined, opts?: (import("./Format.js").FormatOption & import("./LogBase.js").ExtLogBaseOptions & import("./node.js").ExtLogOptions & ExtLogOptionsHttpLog & {
Log: Log;
}) | undefined): (req: IncomingMessageWithId, res: ServerResponse, next: Function) => void;
export function httpLogs(namespace?: string | undefined, opts?: LogOptionsHttpLog | undefined): (req: IncomingMessageWithId, res: ServerResponse, next: Function) => void;
export type IncomingMessage = import('node:http').IncomingMessage;
export type ServerResponse = import('node:http').ServerResponse;
export type LogOptions = import('./node.js').LogOptions;
Expand All @@ -34,4 +32,7 @@ export type IncomingMessageWithId = IncomingMessage & ExtIncomingMessageId;
export type ExtLogOptionsHttpLog = {
customGenerateRequestId?: (() => string) | undefined;
};
export type LogOptionsHttpLog = LogOptions & ExtLogOptionsHttpLog;
export type LogOptionsHttpLog = LogOptions & ExtLogOptionsHttpLog & {
Log: typeof Log;
};
import { Log } from "./node.js";
13 changes: 9 additions & 4 deletions types/logger.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/**
* @typedef {import('./node.js').LogOptions & {Log: typeof Log}} LogOptions
*/
/**
* @param {string} namespace
* @param {import('./node.js').LogOptions & {Log: Log}} [opts]
* @param {LogOptions} [opts]
*/
export function logger(namespace: string, opts?: (import("./Format.js").FormatOption & import("./LogBase.js").ExtLogBaseOptions & import("./node.js").ExtLogOptions & {
Log: Log;
}) | undefined): any;
export function logger(namespace: string, opts?: LogOptions | undefined): any;
export type LogOptions = import('./node.js').LogOptions & {
Log: typeof Log;
};
import { Log } from "./node.js";

0 comments on commit a488b64

Please sign in to comment.