pinia-logger
Version:
Lightweight logger for Pinia
105 lines (104 loc) • 3.17 kB
TypeScript
import type { PiniaPluginContext, StoreActions, StoreGeneric, _ActionsTree, _StoreOnActionListenerContext } from "pinia";
type KeyOfStoreActions<Store> = keyof StoreActions<Store>;
interface Logger extends Partial<Pick<Console, "groupCollapsed" | "group" | "groupEnd">> {
log(message: string, color?: string, payload?: unknown): void;
group(message: string, color?: string, payload?: unknown): void;
groupCollapsed(message: string, color?: string, payload?: unknown): void;
groupEnd(): void;
}
export interface PiniaLoggerOptions {
/**
* @default true
*/
logErrors?: boolean;
/**
* @default false
* @description Disable the logger
*/
disabled?: boolean;
/**
* @default true
* @description Expand the console group
*/
expanded?: boolean;
/**
* @default false
* @description Show the duration of the action in the console
* @example "action [store] actionName @ 12:00:00:000"
* @example "action [store] actionName failed after 100ms @ 12:00:00:000"
*/
showDuration?: boolean;
/**
* @default true
* @description show the time of the action in the console
* @example "action [store] actionName @ 12:00:00:000"
*/
showTime?: boolean;
/**
* @default true
* @description Show the store name in the console
*/
showStoreName?: boolean;
/**
* @default true
* @description Show the pineapple Emoji in the console
*/
showPineapple?: boolean;
/**
* @default
* ```ts
* () => true
* ```
* @description Filter actions to log
* @example
* ```ts
* (action) => action.name !== "incrementCounter"
* ```
*/
filter?: (action: PiniaActionListenerContext) => boolean;
/**
* @default undefined
* @description List of actions to log
* @description If defined, only the actions in this list will be logged
* @description If undefined, all actions will be logged
*/
actions?: KeyOfStoreActions<StoreGeneric>[];
/**
* @default console
* @description Define custom log function
* @description If undefined, console will be used
*/
logger?: Logger;
}
export type PiniaActionListenerContext = _StoreOnActionListenerContext<StoreGeneric, string, _ActionsTree>;
declare module "pinia" {
interface DefineStoreOptionsBase<S, Store> {
/**
* Customize logger options for individual Pinia stores.
*
* @example
* ```ts
* defineStore('id', {
* actions: { getApi() {}},
* logger: false
* })
* ```
* @example
*
* ```ts
* defineStore('id', {
* actions: { getApi() {}},
* logger: {
* disabled: true,
* expanded: false
* }
* })
* ```
*/
logger?: boolean | (PiniaLoggerOptions & {
actions?: KeyOfStoreActions<Store>[];
});
}
}
export declare const PiniaLogger: (config?: PiniaLoggerOptions) => (ctx: PiniaPluginContext) => void;
export default PiniaLogger;