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

@jeje-devs/plume-server

v3.2.2

Published

Package for Web Api Node JS apps with Express in TypeScript

Downloads

4

Readme

PlumeServer

Table of contents

General info

PlumeServer is an easy to use tool to create a quick Web API using Node.js and Express.js

Example:

import { Controller, HttpGet, FromRoute, PlumeServer, Injectable, Result, ok } from '@jeje-devs/plume-server';

@Injectable()
export class PlanetService
{
    public getPlanets(name: string): Array<string>
    {
        return ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturne', 'Uranus', 'Neptune']
            .filter(x => x.toLowerCase().includes(name.toLowerCase()));
    }
}

@Controller('api/planets')
export class PlanetController
{
    public constructor(
        private readonly _planetService: PlanetService) { }

    @HttpGet(':name')
    public async getPlanets(@FromRoute('name') name: string): Promise<Result<Array<string>>>
    {
        return ok(this._planetService.getPlanets(name));
    }
}

const host = PlumeServer.createHost();
host.serve(8080);

Technologies

  • Node.js
  • Express.js
  • Typescript

Setup

npm install @jeje-devs/plume-server

Usage

Create a controller class using the decorator @Controller:

@Controller('api/route')
export class MyController

Create the different http methods using the decorators:

@HttpGet('hello')
public async sayHello(): Promise<Result<string>>
{
    return ok('Hello World!');
}

The available methods are

  • HttpGet
  • HttpPost
  • HttpPut
  • HttpPatch
  • HttpDelete

Query and Route params

To send query params, add parameters to the endpoint method with the decorator @FromQuery. For routes params, you need to use @FromRoute The types must be string, number or boolean

// Endpoint url: /api/controller/cars?model=208&brand=Peugeot
@HttpGet('cars')
public async getCars(@FromQuery('model') model: string, @FromQuery('brand') brand: string): Promise<Result<Array<Car>>>
{
    // ...
}

@HttpGet('cars/:id')
public async getCarById(@FromRoute('id') id: number): Promise<Result<Car>>
{
    // ...
}

The argument is the property name sent by the client.

Body params

For body params, you need to use the @FromBody

@HttpPost('cars')
public addCar(@FromBody() car: Car)
{
    // ...
}

Example

Here is an example of what you can achieve:

import { Controller, HttpGet, HttpPost, HttpPut, HttpDelete, FromBody, Injectable, Result, ok, noContent } from '@jeje-devs/plume-server';
import { Car } from 'src/models/car.model';
import { CarService } from 'src/services/car.service';

@Controller('api/cars')
export class CarController
{
    public constructor(
        private readonly _carService: CarService) { }

    @HttpGet()
    public async getCars(@FromQuery('name') name: string, @FromBody('brand') brand: string): Promise<Result<Array<Car>>>
    {
        const data = await this._carService.getCars(name, brand);
        return ok(data);
    }

    @HttpPost()
    public async createCar(@FromBody() car: Car): Promise<Result<void>>
    {
        await this._carService.createCar(car);
        return noContent();
    }

    @HttpPut(':id')
    public async updateCar(@FromRoute('id') id: number, @FromBody() car: Car): Promise<Result<void>>
    {
        await this._carService.updateCar(id, car);
        return noContent();
    }

    @HttpDelete(':id')
    public async deleteCar(@FromRoute('id') id: number): Promise<Result<void>>
    {
        await this._carService.deleteCar(id);
        return noContent();
    }
}

Dependency injection

You can add other classes and register them with the mandatory @Injectable decorator. Typically, every other service classes referenced by the @Controller classes need to be registered, otherwise the application will not run. You will need to add the dependencies in the class constructor.

import { Controller, HttpGet, Injectable, Result, ok } from '@jeje-devs/plume-server';

@Injectable()
export class ElementService
{
    public getElements(): Array<string>
    {
        return ['Fire', 'Water', 'Wind', 'Earth'];
    }
}

@Controller('api/elements')
export class ElementController
{
    public constructor(
        private readonly _elementService: ElementService) { }

    @HttpGet()
    public async getElements(): Promise<Result<Array<string>>>
    {
        const data = this._elementService.getElements();
        return ok(data);
    }
}

Starting server

To start the server you need to run the PlumeServer.createHost and host.serve methods:

import { Controller, PlumeServer } from '@jeje-devs/plume-server';

import 'src/controller/car.controller';

const port = 8080;

const singletonToInject = {
    value1: 'Foo',
    value2: 'Bar'
};

const host = PlumeServer.createHost();
host.onApiErrors = (err: any) => console.error(err);
host.registerInstance('customSingleton', singletonToInject);
host.serve(port).then(() => console.log('Server is running'));

Contributors

Links