import { ItemAddResult , List } from '@pnp/sp' ; export type ListLoggerEntryLevel = 'Info' | 'Warning' | 'Error' ; export class IListLoggerEntry { webUrl ? : string ; scope ? : string ; functionName ? : string ; message : string ; level : ListLoggerEntryLevel ; } export class IListLoggerMemberMap { webUrl ? : string ; scope ? : string ; functionName ? : string ; message ? : string ; level ? : string ; } export default new ( class ListLogger { private _list : any ; private _memberMap : IListLoggerMemberMap ; private _webUrl : string = '' ; private _scope : string = '' ; param { any } list param { IListLoggerMemberMap } memberMap param { string } webUrl param { string } scope public init ( list , memberMap , webUrl ? , scope ? ) { this . _list = list ; this . _memberMap = memberMap ; this . _webUrl = webUrl ; this . _scope = scope ; } param { IListLoggerEntry } entry public log ( entry ) { const spItem = this . getSpItem ( entry ) ; return ( this . _list as List ) . items . add ( spItem ) ; } param { string } message param { ListLoggerEntryLevel } level param { string } functionName public write ( message , level = 'Info' , functionName ? ) { return this . log ( { message , level , functionName , webUrl : this . _webUrl , scope : this . _scope } ) ; } param { IListLoggerEntry } entry private getSpItem ( entry ) { let item = { } ; if ( this . _webUrl && this . _memberMap . webUrl ) { item [ this . _memberMap . webUrl ] = this . _webUrl ; } if ( this . _scope && this . _memberMap . scope ) { item [ this . _memberMap . scope ] = this . _scope ; } item = Object . keys ( this . _memberMap ) . reduce ( ( _item , key ) => { const fieldName = this . _memberMap [ key ] ; _item [ fieldName ] = entry [ key ] ; return _item ; } , item ) ; return item ; } } ) ( ) ;