the-api

    5.3.3 • Public • Published

    NPM version NPM downloads MIT License Build Status: Linux Build Status: Windows Coverage Status Requirements Status Bundle Size Issues Stars Node version Contributors

    the-api

    The API

    Create API asap. Includes the following:

    • log all actions to console with time, unique id, request path and time elapsed since the beginning of the request.

    • error handler with app name, version, error codeName, stack, error name and description for developers.

    • info endpoint: server currentTime, app name, version, uptime, requests count, endpoints

    • login route - register, login, make and refresh app token

    • check token, return unauthorized if token is invalid

    • handle white/black lists, count of requests

    • cache data in memory/redis

    • notes route - example of category/notes api

    Install

    npm i -S the-api

    or

    yarn add the-api

    Usage

    index.js

    const TheAPI = require('the-api');
    const api = new TheAPI();
    
    const { logs, errors, info, token, access, cache } = api.extensions;
    const { login, check, notes } = api.routes;
    
    info.endpointsToShow(login, check, notes);
    
    api.up([
              // select any extension you want to use
      logs,   // log all actions to console with time, unique id, request path and time elapsed since the beginning of the request
      errors, // error handler with app name, version, error codeName, stack, error name and description for developers
      info,   // GET /info endpoint: server currentTime, app name, version, uptime, requests count, endpoints
      login,  // login route - register, login, make and refresh app token
      check,  // check route - small example, just send {ok: 1}
      token,  // check token, return unauthorized if token is invalid
      access, // handle white/black list, count of requests
      cache,  // cache data in memory for 5 seconds
      notes,  // notes route - example of category/notes api
    ]);
    

    Change e-mail templates

    ...
    const { login } = api.routes;
    ...
    login.setTemplates({
      register: {
        subject: 'Complete your registration',
        text: 'Hello, use your code {{code}} to POST /register/check',
        html: 'Hello, use your <b>code {{code}}</b> to POST <b>/register/check</b>',
      },
    })
    ...
    api.up([
    ...
    

    examples

    • Get info
    curl -vvv localhost:8877/info
    
    • Create User
    curl -vvv -X POST -d '{"login":"aaa", "password":"bbb"}' --header "Content-Type: application/json" localhost:8877/register
    
    • Refresh Token
    curl -vvv -X POST -d '{"refresh":"36ae43be-ae49-46b9-ba8a-027dbcf64fa0"}' --header "Content-Type: application/json" localhost:8877/login
    
    • Get new Token
    curl -vvv -X POST -d '{"login":"aaa", "password":"bbb"}' --header "Content-Type: application/json" localhost:8877/login
    

    .env

    name description example default value
    PORT port to listen 8878 8877
    API_PREFIX api prefix /v1
    JWT_SECRET JWT secret phrase nruR3@_123dri!aS
    JWT_EXPIRES_IN expressed in seconds or a string describing a time span zeit/ms 60, 2d, 10h 1h

    Database

    DB options for knex

    name description example default value
    DB_CLIENT client postgres, mysql, sqlite3 sqlite3
    DB_HOST DB host db.server
    DB_PORT DB port
    DB_USER DB user login
    DB_PASSWORD DB password password
    DB_NAME database name the_api_db
    DB_FILENAME sqlite3 DB filename ./sqlite.db

    Login Module

    name description example default value
    LOGIN_CHECK_EMAIL send code to check registration true, false false
    LOGIN_CHECK_EMAIL_DELAY waiting for check registration code, minutes 30 60
    LOGIN_UNCONFIRMED_FORBID Don't send token for unconfirmed e-mail true, false false
    EMAIL_USER nodemailer email user login
    EMAIL_PASSWORD email password password
    EMAIL_HOST email server smtp.server
    EMAIL_PORT email port 587
    EMAIL_SECURE nodemailer secure option true, false
    EMAIL_TLS_REJECTUNAUTH nodemailer rejectUnauthorized option true, false

    Created by

    Dimitry Ivanov 2@ivanoff.org.ua # curl -A cv ivanoff.org.ua

    Keywords

    Install

    npm i the-api

    DownloadsWeekly Downloads

    12

    Version

    5.3.3

    License

    MIT

    Unpacked Size

    69.2 kB

    Total Files

    53

    Last publish

    Collaborators

    • ivanoff