@alkocats/http-ts
A Typescript HTTP server wrapper with express.
Last updated 10 months ago by alkocats .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @alkocats/http-ts 
SYNC missed versions from official npm registry.

http-ts

BuildStatus Maintainability Test Coverage npm version MIT License Known Vulnerabilities dependencies Status devDependencies Status Greenkeeper badge

Installation

npm install @alkocats/http-ts

Usage

Example

All relevant imports for a minimal setup:

import { Repository, Controller, HTTPGet, HTTPServer, HTTPForbiddenError, HTTP_STATUS } from '@alkocats/http-ts';
import { Request, Response } from 'express';

The user interface for the user repository:

interface User {
    name: string;
    password: string;
}

The user repository for the stored data the controller uses:

class UserRepository extends Repository<User[]> {
    public async getAsyncData(): Promise<User[]> {
        return this.data;
    }
}

The user controller, which handles the user requests, equipped with different GET-methods:

class UserController extends Controller<UserRepository> {
    /**
     * Define a GET-method for the url /users.
     */
    @HTTPGet('/users')
    public getUsers(): User[] {
        return this.repository.getData();
    }

    /**
     * Define a asynchronous GET-method for the url /async-users.
     */
    @HTTPGet('/async-users')
    public async getAsyncUsers(): Promise<User[]> {
        return await this.repository.getAsyncData();
    }

    /**
     * Define a asynchronous GET-method for the url /async-users-with-http-response and a custom http code
     */
    @HTTPGet ('/async-users-with-http-response')
    public async getAsyncUsersWithHTTPResponse(): Promise<HTTPResponse> {
        const data = await this.repository.getAsyncData();

        return new HTTPResponse(data, HTTP_STATUS.CODE_202_ACCEPTED);
    }

    /**
     * Define a GET-method for the url /faulty-users which throws a HTTP error.
     * None HTTP errors are automatically transformed to HTTP 500 error.
     */
    @HTTPGet('/faulty-method')
    public faultyMethod(): HTTPResponse {
        throw new HTTPForbiddenError();

        return null;
    }
}

Bringing it all together:

const userRepository = new UserRepository([{
    name: 'admin',
    password: 'the-cake-is-a-lie'
}]);
const userController = new UserController(userRepository);

const httpServer = new HTTPServer();
httpServer.registerController(userController);
httpServer.start();

Basic JWT / bcrypt Authentication

All relevant imports for a minimal setup:

import * as bcrypt from 'bcrypt';
import { JWTAuthenticator, Repository, HTTPServer, Controller, HTTPGet, Authenticated } from './authenticator';

The user interface for the user repository:

interface User {
    email: string;
    password: string;
}

The data interface for the data repository:

interface Data {
    foo: number;
    bar: string;
}

The data repository for the stored data the controller uses:

class DataRepository extends Repository<Data[]> {
    public getSecretData(): Data[] {
        return [{
            foo: 0,
            bar: 'top secret'
        }];
    }
}

const dataRepository = new DataRepository([{
    foo: 1,
    bar: 'no secret data'
}]);

The data controller, which handles all data requests.

class DataController extends Controller<DataRepository> {
    /**
     * A unauthaenticated get method which can be called by everyone
     */
    @HTTPGet('/data')
    public getDataUnauthenticated() {
        return this.repository.getData();
    }

    /**
     * An authenticated get method, which can only be called by authenticated
     * users who deliver a valid bearer token.
     */
    @Authenticated()
    @HTTPGet('/data-authenticated')
    public getDataAuthenticated(): Data[] {
        return this.repository.getSecretData();
    }
}

const dataController = new DataController(dataRepository);

Bringing it all together:

async function main() {
    // To login with a valid password, the password needs to be hashed with bcrypt
    const hashedPassword = await bcrypt.hash('the-cake-is-a-lie', 10);

    // This repository is used by the JWTAuthenticator and contains all valid logins.
    const userRepository = new Repository<User[]>([{
        email: 'alkocats.info@gmail.com',
        password: hashedPassword
    }]);

    // The secret for JWTAuthenticator to use for encryption / decryption.
    const secret = 'some-secret';

    /**
     * The path '/auth' defines, where to make a post with email / password
     * as a json object to obtain a valid bearer token.
     * The token then can be used to access @Authenticated methods of the
     * registered controllers.
     */
    const authenticator = new JWTAuthenticator<User>('/auth', {
        // Defines the repository with all the valid logins
        repository: userRepository,
        // Defines, which field of the repository data is used for identification
        identificationKey: 'email',
        // Defines, in which field of the repository data the hashed password is stored
        passwordKey: 'password',
        // Defines the expiration time of generated tokens (still valid after restart of server)
        expiresIn: 86400,
        // Defines the secret JWT uses to encrypt / decrypt the generated tokens.
        secret: secret
    });

    const httpServer = new HTTPServer(80, authenticator);

    httpServer.registerController(dataController);
    httpServer.start();
}

main();

After everything is implemented, a post to http://localhost/auth with the json data

{
    "email": "alkocats.info@gmail.com",
    "password": "the-cake-is-a-lie"
}

generates a bearer token, wich can be used in future requests to access @Authenticated methods of the registered controllers.

Alternatively a custom Authenticater can be created by creating a class which extends Authenticator.

API

Supported HTTP methods

@HTTPGet(path: string)

@HTTPPut(path: string)

@HTTPPost(path: string)

@HTTPDelete(path: string)

@HTTPPatch(path: string)

@HTTPHead(path: string)

@HTTPOptions(path: string)

@HTTPTrace(path: string)

@HTTPConnect(path: string)

Contributing

Feel free to create branches or pull requests.

Current Tags

  • 0.1.1                                ...           latest (10 months ago)

25 Versions

  • 0.1.1                                ...           10 months ago
  • 0.1.0                                ...           10 months ago
  • 0.0.27                                ...           a year ago
  • 0.0.26                                ...           a year ago
  • 0.0.25                                ...           a year ago
  • 0.0.24                                ...           a year ago
  • 0.0.22                                ...           a year ago
  • 0.0.21                                ...           a year ago
  • 0.0.20                                ...           a year ago
  • 0.0.19                                ...           a year ago
  • 0.0.18                                ...           a year ago
  • 0.0.17                                ...           a year ago
  • 0.0.16                                ...           a year ago
  • 0.0.15                                ...           a year ago
  • 0.0.14                                ...           a year ago
  • 0.0.13                                ...           a year ago
  • 0.0.12                                ...           a year ago
  • 0.0.11                                ...           a year ago
  • 0.0.10                                ...           a year ago
  • 0.0.9                                ...           a year ago
  • 0.0.6                                ...           a year ago
  • 0.0.5                                ...           a year ago
  • 0.0.3                                ...           a year ago
  • 0.0.4                                ...           a year ago
  • 0.0.2                                ...           a year ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 25
Last Month 75
Dependencies (11)
Dev Dependencies (18)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |