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

ts-service

v0.0.10

Published

[![Build Status](https://travis-ci.org/start-plus/ts-service.svg?branch=master)](https://travis-ci.org/start-plus/ts-service) [![codecov](https://codecov.io/gh/start-plus/ts-service/branch/master/graph/badge.svg)](https://codecov.io/gh/start-plus/ts-servi

Downloads

17

Readme

ts-service

Build Status codecov

ts-service is a TypeScript library for validation and logging.
It depends on joi (validator) and bunyan (logger)

Installation

npm i ts-service

Features

  • Input logging (input parameters):
myService: ENTER methodName: {param1: 'foo', param2: 'bar'}
  • Output logging (sync and async):
myService:  EXIT methodName: {result: 'foobar', anotherProp: 'bar'}
  • Error logging with input parameters (see example below).
  • Input validation and normalization (example: string type "2" to number type 2).
add(
    @schema(Joi.number().required())
    a: number,
  ) {
    // `typeof a` will be always 'number'
    // even if we pass number string value e.g '2'
    // if the input is invalid (null, object, array etc) then an error will be thrown in runtime
  }
  • Validation with inline annotation.
sendEmail(
  @schema(Joi.string().email().required()
  email: string
) {
  ...
}
  • Validation with class annotation.
@schema(Joi.object().keys({....}))
class SendEmailValues {
  ...
}

sendEmail(values: SendEmailValues) {
  ...
}

Example usage (inline annotation)

file services/CalcService.ts

import * as Joi from 'joi';
import { service, validate, schema } from 'ts-service';

@service
class CalcService {
  @validate
  add(
    @schema(Joi.number().required())
    a: number,
    @schema(Joi.number().required())
    b: number,
  ) {
    return a + b;
  }
}

// create your service
export const calcService = new CalcService();

use service

import {calcService} from './services/CalcService';


calcService.add(1, 3); // returns 4
calcService.add('5' as any, '6' as any); // returns 11, input parameters are converted to number types
calcService.add('1' as any, { foo: 'bar' } as any); // logs and throws an error
// NOTE: you shouldn't use casting `as any` in your code. It's used only for a demonstration purpose.
// The service is expected to be called with unknown input (for example: req.body).

Alt text

See example under examples/example1.ts. Run it using npm run example1.

Async example usage (class annotation)

file services/UserService.ts

import * as Joi from 'joi';
import { service, validate, schema } from 'ts-service';

@schema(
  Joi.object().keys({
    name: Joi.string()
      .required()
      .alphanum(),
    email: Joi.string()
      .required()
      .email(),
    password: Joi.string()
      .required()
      .min(5),
  }),
)
class CreateUserValues {
  name: string;
  email: string;
  password: string;
}

@service
class UserService {
  @validate
  async createUser(values: CreateUserValues) {
    const id = 1;
    return id;
  }
}

// create your service
export const userService = new UserService();

use service

import {userService} from './services/UserService';

await userService.createUser({
  name: 'john',
  email: '[email protected]',
  password: 'secret',
}); // ok
await userService.createUser({
  name: 'john',
  email: 'invalid email',
  password: 'secret',
}); // throws an error

Alt text

See example under examples/example2.ts. Run it using npm run example2.

Removing security information

By default properties password, token, accessToken are removed from logging.
Additionally you can annotated method with @removeOutput to remove the method result.
Example:

file services/SecurityService.ts

import * as Joi from 'joi';
import { service, validate, schema, removeOutput } from 'ts-service';

@service
class SecurityService {
  @validate
  @removeOutput
  hashPassword(
    @schema(Joi.string().required())
    password: string,
  ) {
    return 'ba817ef716'; // hash password here
  }
}

// create your service
export const securityService = new SecurityService();

use service

import {securityService} from './services/SecurityService';

securityService.hashPassword('secret-password');

Alt text

See example under examples/example3.ts. Run it using npm run example3.

Configuration

import {configure} from 'ts-service';

configure({
  removeFields: string[], // the array of fields not won't be logged to the console, default: ['password', 'token', 'accessToken'],
  debug: boolean,         // the flag if ENTER and EXIT logging is enabled, (errors are always enabled), default: true
  depth: number,          // the object depth level when serializing, default: 4           
  maxArrayLength: number, // the maximum number of elements to include when formatting an array, default: 30  
})

You must configure it, before creating any service.

Special properties

if the parameter name is req it's assumed that the object is an express request.
Only properties are logged: method, url, headers, remoteAddress, remotePort.

if the parameter name is res it's assumed that the object is an express response.
Only properties are logged: statusCode, header.

MIT License