Skip to content

Commit

Permalink
feat(types): adding logger type for logger plugin (#1853)
Browse files Browse the repository at this point in the history
`createLogger` takes in logger option which by default is console but
can be overridden with own logger. Here we addd `Logger` interface
for `logger` options params which expects log method to
be implemented.

Co-authored-by: Thomas Truong <ttruong@factset.com>
  • Loading branch information
itomtom and Thomas Truong authored Oct 23, 2020
1 parent 6734ac5 commit cb3198d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 6 additions & 0 deletions types/logger.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Payload, Plugin } from "./index";

interface Logger extends Partial<Pick<Console, 'groupCollapsed' | 'group' | 'groupEnd'>> {
log(message: string, colour: string, action: any): void;
log(message: string): void;
}

export interface LoggerOption<S> {
collapsed?: boolean;
filter?: <P extends Payload>(mutation: P, stateBefore: S, stateAfter: S) => boolean;
Expand All @@ -9,6 +14,7 @@ export interface LoggerOption<S> {
actionTransformer?: <P extends Payload>(action: P) => any;
logMutations?: boolean;
logActions?: boolean;
logger?: Logger;
}

export default function createLogger<S>(option?: LoggerOption<S>): Plugin<S>;
9 changes: 8 additions & 1 deletion types/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,17 @@ namespace Plugins {
});
}

class MyLogger {
log(message: string) {
console.log(message);
}
}

const logger = Vuex.createLogger<{ value: number }>({
collapsed: true,
transformer: state => state.value,
mutationTransformer: (mutation: { type: string }) => mutation.type
mutationTransformer: (mutation: { type: string }) => mutation.type,
logger: new MyLogger()
});

const store = new Vuex.Store<{ value: number }>({
Expand Down

0 comments on commit cb3198d

Please # to comment.