Skip to content

Commit

Permalink
feat(types): add missing typescript definitions for reporters (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr committed May 7, 2020
1 parent 9a82d5e commit 4a08ef0
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/index.js
@@ -1,5 +1,6 @@
export { default as Consola } from './consola'
export { default as Types } from './types'
export { LogLevel } from './logLevels'
export { isLogObj } from './utils'
export { assignGlobalConsola } from './utils/global'

Expand Down
10 changes: 10 additions & 0 deletions src/logLevels.js
@@ -0,0 +1,10 @@
export const LogLevel = {}
LogLevel[LogLevel.Fatal = 0] = 'Fatal'
LogLevel[LogLevel.Error = 0] = 'Error'
LogLevel[LogLevel.Warn = 1] = 'Warn'
LogLevel[LogLevel.Log = 2] = 'Log'
LogLevel[LogLevel.Info = 3] = 'Info'
LogLevel[LogLevel.Success = 3] = 'Success'
LogLevel[LogLevel.Debug = 4] = 'Debug'
LogLevel[LogLevel.Trace = 5] = 'Trace'
LogLevel[LogLevel.Silent = Infinity] = 'Silent'
24 changes: 13 additions & 11 deletions src/types.js
@@ -1,44 +1,46 @@
import { LogLevel } from './logLevels'

export default {
// Level 0
fatal: {
level: 0
level: LogLevel.Fatal
},
error: {
level: 0
level: LogLevel.Error
},
// Level 1
warn: {
level: 1
level: LogLevel.Warn
},
// Level 2
log: {
level: 2
level: LogLevel.Log
},
// Level 3
info: {
level: 3
level: LogLevel.Info
},
success: {
level: 3
level: LogLevel.Success
},
// Level 4
debug: {
level: 4
level: LogLevel.Debug
},
// Level 5
trace: {
level: 5
level: LogLevel.Trace
},
// Silent
silent: {
level: Infinity
level: LogLevel.Silent
},

// Legacy
ready: {
level: 3
level: LogLevel.Info
},
start: {
level: 3
level: LogLevel.Info
}
}
74 changes: 68 additions & 6 deletions types/consola.d.ts
@@ -1,10 +1,25 @@
import { InspectOptions } from 'util';

export enum LogLevel {
Fatal= 0,
Error= 0,
Warn= 1,
Log= 2,
Info= 3,
Success= 3,
Debug= 4,
Trace= 5,
Silent= Infinity,
}

export interface ConsolaLogObject {
level?: number,
level?: LogLevel,
tag?: string,
type?: string,
message?: string,
additional?: string | string[],
args?: any[],
date?: Date,
}

type ConsolaMock = (...args: any) => void
Expand All @@ -13,8 +28,8 @@ type ConsolaMockFn = (type: string, defaults: ConsolaLogObject) => ConsolaMock

export interface ConsolaReporterArgs {
async: boolean,
stdout: any,
stderr: any,
stdout: NodeJS.WritableStream,
stderr: NodeJS.WritableStream,
}

export interface ConsolaReporter {
Expand All @@ -24,7 +39,7 @@ export interface ConsolaReporter {
export interface ConsolaOptions {
reporters?: ConsolaReporter[],
types?: { [type: string]: ConsolaLogObject },
level?: number,
level?: LogLevel,
defaults?: ConsolaLogObject,
async?: boolean,
stdout?: any,
Expand All @@ -36,7 +51,7 @@ export interface ConsolaOptions {
export declare class Consola {
constructor(options: ConsolaOptions)

level: number
level: LogLevel
readonly stdout: any
readonly stderr: any

Expand Down Expand Up @@ -87,8 +102,55 @@ export declare class Consola {
mock(mockFn: ConsolaMockFn): any
}

export interface BasicReporterOptions {
dateFormat?: string;
formatOptions?: InspectOptions;
}

export declare class BasicReporter implements ConsolaReporter {
protected options: BasicReporterOptions;

constructor(options?: BasicReporterOptions);

public log(logObj: ConsolaLogObject, args: ConsolaReporterArgs): void;

protected formatStack(stack: string): string;
protected formatArgs(args: any[]): string;
protected formatDate(date: Date): string;
protected filterAndJoin(arr: Array<string | undefined>): string;
protected formatLogObj(logObj: ConsolaLogObject): string;
}

export interface FancyReporterOptions extends BasicReporterOptions{
secondaryColor?: string;
}

export declare class FancyReporter extends BasicReporter {
constructor(options?: FancyReporterOptions);

protected formatType(logObj: ConsolaLogObject): void;
}

export type BrowserReporterOptions = {};

export declare class BrowserReporter implements ConsolaReporter {
log: (logObj: ConsolaLogObject, args: ConsolaReporterArgs) => void
public log(logObj: ConsolaLogObject, args: ConsolaReporterArgs): void;
}

export type JSONReporterOptions = {
stream?: NodeJS.WritableStream;
};

export declare class JSONReporter implements ConsolaReporter {
constructor(options?: JSONReporterOptions);
public log(logObj: ConsolaLogObject, args: ConsolaReporterArgs): void;
}

export type Winston = any;

export declare class WinstonReporter implements ConsolaReporter {
constructor(logger?: Winston);
public log(logObj: ConsolaLogObject, args: ConsolaReporterArgs): void;
}

declare const consolaGlobalInstance: Consola;
Expand Down

0 comments on commit 4a08ef0

Please sign in to comment.