npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ttt-net-logger

v2.0.4

Published

Utilities which expose classes to decorate any Network API and do logging

Downloads

20

Readme

ttt-net-logger

The library provides a set of tools that will allow you to start logging requests made with the client. The library provides decorators for the basic API (Fetch API), but if necessary you can implement your own decorator.

You should create your own implementation of the Logger class because of the flexibility.

Getting Started

npm install ttt-net-logger

Usage

session.ts

import { SessionUuid, ISession } from 'ttt-net-logger'

export class UrOwnSessionUuid extends SessionUuid {
  protected generateSession(): ISession {
    // Your own implementation
  }

  protected saveSession(): void {
    // Your own implementation
  }
}

logger.ts

import { Logger, IBaseLog, IClientParams } from 'ttt-net-logger'
import { UrOwnSessionUuid } from './session'

class UrOwnParticular extends Logger {
    declare protected session: UrOwnSessionUuid

    constructor( params?: IClientParams ) {
      super()
      this.session = new UrOwnSessionUuid( this.params.sessionVar )
    }

    async sendLog( log: IBaseLog ): Promise<void> {
    // Allows you to send a request that will not be logged
    await super.silentFetch( 'localhost:5500/logger', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify( crmLog ),
    } )

  }
}

Config

Parameters that are passed to the construct Logger.

type IClientParams = {
    enableConsole?: boolean;
    ignore?: RegExp[];
    sessionVar?: string;
}

| Property | Description | | ---------- | -------------------------------------------------------------------------------------------- | | enableConsole | Allows you to turn on/off the output of the console log[^console_log] | | ignore | List of regular expressions for URLs that do not need to be logged | | sessionVar | Name of variable in sessionStorage |

API

IBaseLog

export interface IBaseLog {
  request: IRequest;
  response: IResponse;
  userAgent: string;
}

interface IRequest {
  method: string;
  url: string;
  mode?: RequestMode;
  headers?: Record<string, string>;
  body?: any;
}

interface IResponse {
  headers: Record<string, string>;
  body?: any;
  type: ResponseType;
  statusText: string;
  status: number;
}

IClientParams

type IClientParams = {
    project?: string | undefined;
    enableConsole?: boolean | undefined;
    ignore?: string[] | undefined;
    sessionVar?: string | undefined;
}

SessionUuid && ISession

export interface ISession {
  uuid: string;
}

SessionUuid

The class is used to generate the current session.

Interface:

protected sessionVar: string;

The name of the variable in Session Storage. Used to avoid collisions with other variables.


protected session: ISession;

This active session.


constructor( sessionVar: string );

get uuid(): string;

Getting the current uuid. By default, it is implemented as a singleton - if there is no session in Sesion Storage, it creates a new one, otherwise takes the value from storage.


protected generateSession(): ISession;

New session generation function.


protected saveSession(): void

Session saver. By default saves in Session Storage.

####NetworkAPIDecorator

abstract class NetworkAPIDecorator

The class is used as a basis for creating decorators for various IPAs. It is the decorator who tells the logger when and what to log.

For example, see [FetchDecorator] (./.../lib/core/decorators/FetchDecorator.ts)

Interface:

set logger(logger: Logger)

Binds a copy of the logger to the decorator.


protected sendLog( log: IBaseLog | null ): void

A method for sending a log to a server.


abstract decorate(): void;
abstract undecorate(): void;

The methods allow to realize the process of integration of the decorator and its removal.

Logger

abstract class Logger

The class of logger. Is an abstract class, accordingly it is necessary to write an implementation on the basis of it.

Interface:

protected session: SessionUuid

Session controller. Allows you to redefine the controller with your own.


protected only params: IInnerParams

Parameters that were passed when logger was initialized

constructor( params: IClientParams )

set decorator( decorator: NetworkAPIDecorator )

Allows you to change the current active decorator


protected silentFetch( input: RequestInfo | URL, init?: RequestInit | undefined )

Allows you to query a server without logging.


abstract sendLog( log: IBaseLog ): void

The abstract method you need to implement to send the log to the server.

[^console_log]: Format - HH:MM:SS:MS METHOD URL