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

smally

v0.2.14

Published

Small nodejs server-side framework based on event system.

Downloads

10

Readme

smally

Small server framework.

Small nodejs server-side framework based on event system.

Install

npm install smally@latest

Conception

The Common concept is application consists of several level services that provides API's transport and business logic. Services interact with each other through registered methods and events.

Simple application

import { of, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import * as express from 'express';
import { App, Service, Method, Request } from 'smally';

@Service({ alias: 'HttpServer' })
class HttpServerService {
  constructor(private req: Request) {
    const ex = express();
    ex.post('/rest/users', (req: express.Request, res: express.Response, next: express.NextFunction) => {
      this.req.request({
        method: 'getUsers',
        service: 'Admin',
      }).subscribe(users => res.json(users), err => next(err));
    });
  }
}

@Service({ alias: 'Admin' })
class AdminService {
  @Method({ alias: 'getUsers' })
  private getUsers(): Observable<any[]> {
    return of([
     { id: '1', fio: 'Ivanov' },
     { id: '2', fio: 'Petrov' },
    ]);
  }
}

@Service({ alias: 'Miracle' })
class MiracleService {
  constructor(private req: Request) {
  }
  
  @Method('getFirstUserId')
  private getUserByName(fio: string): Observable<string> {
    return this.req.request<any[]>({
      method: 'getUsers',
      service: 'Admin',
    }).pipe(map(users => users.find(user => user.fio === fio)?.id));
  }
}

const app = new App({ name: 'awesome2', services: [AdminService, MiracleService] });

Services

Services is the high level elements of application. The ones should be marked with a class decorator @Service.

@Service()
class AdminService {
...
}

By default, service name equal his constructor's name: (new AdminService).constructor.name. If you want set certain service name you should define alias:

@Service({ alias: 'Admin' })
class AdminService {
...
}

The name uses to request methods from one service to another.

Services have own API's methods that can be called from other services:

@Service({ alias: 'Admin' })
class AdminService {
  @Method()
  private getUsers(): Observable<any[]> {
    return of([
      { id: '1', fio: 'Ivanov' },
      { id: '2', fio: 'Petrov' },
    ]);
  }
}

You can also pass alias for the method @Method({ alias: 'getAllUsers' }) .

Systems Providers

System providers ensure common interaction between the core and application's elements.

ServiceManager

All services in one place and ready to use. This system provider registers services and its methods into own repo.

import { ServiceManager, Service } from 'smally';
import { of } from 'rxjs';

@Service({ alias: 'Admin' })
class AdminService {
  constructor(private sm: ServiceManager) {
    const w = this.sm.getService('Admin'); // internal wrapper instance of AdminService 
    // register custom method1
    w.addMethod('method1', () => of(this.multiMethod(1)));
    // register custom method2
    w.addMethod('method2', () => of(this.multiMethod(2)));
  }
  
  private multiMethod(n: number): number {
    return n * n;
  }
}

Request

Request provider uses ServiceManager repo in order to call service's methods. It aims to determinate existsing service and method and to call it.

import { Service, Method } from 'smally';
import { of, Observable } from 'rxjs';

@Service({ alias: 'HttpServer' })
class HttpServerService {
  constructor(private req: Request) {
    const ex = express();
    ex.post('/rest/users', (req: express.Request, res: express.Response, next: express.NextFunction) => {
      // call method getUsers from Admin
      this.req.request({
        method: 'getUsers',
        service: 'Admin',
      }).subscribe(users => res.json(users), err => next(err));
    });
  }
}

@Service({ alias: 'Admin' })
class AdminService {
  @Method({ alias: 'getUsers' })
  private getUsers(): Observable<any[]> {
    return of([
     { id: '1', fio: 'Ivanov' },
     { id: '2', fio: 'Petrov' },
    ]);
  }
}

Events

Important part of services interaction is event system. All elements could emit its own events that either can be processed inside of service or outside of it.

import { Service, Method, Events } from 'smally';
import { of, Observable } from 'rxjs';

@Service({ 
  alias: 'HttpServer',
  providers: [{ provide: Events, useFactory: root => new Events('HttpServer', root) }],
 })
class HttpServerService {
  constructor(private events: Events) {
    this.events.emit('start');
  }
}

@Service({ 
  alias: 'Admin',
  providers: [{ provide: Events, useFactory: root => new Events('Admin', root) }],
 })
class AdminService {
  constructor(private events: Events) {
    this.events.on('HttpServer', 'start', () => {
      // I know that server was started
    });
  }
}

DataCenter

Data Center allows to share any data between services safely.

Injector

The injection systems based on @Service and @Injectable decorators. Injector provider builds tree of dependencies and instantiate services and providers.