1.2.0 • Published 2 years ago

rest-docs v1.2.0

Weekly downloads
76
License
MIT
Repository
github
Last release
2 years ago

REST-Docs

RESTful HTTP client library + Docs to test your API REST. Supports for PostgreSQL, MySQL, MariaDB, MSSQL and SQLite3.

API

Table of contents

Install

npm i rest-docs --save

Install Database Library

  • MSSQL
npm i mssql --save
  • MySQL and MariaDB
npm i mysql --save
  • PostgreSQL
npm i pg --save
  • SQLite3
npm i sqlite3 --save

Usage

// server.js
const rest_docs = require('rest-docs');
var rest = new rest_docs();

rest.startServer({
  ip: '127.0.0.1', //<-- YOUR_SERVER_IP
  port: '8080', //<-- YOUR_SERVER_PORT
  compression: 'gzip' //<-- YOUR_COMPRESSION_STRATEGY
})

rest.startDBServer('mysql', {
  host: 'localhost', //<-- YOUR_DATABASE_HOST
  port: 3306, //<-- YOUR_DATABASE_PORT
  user: 'root', //<-- YOUR_DATABASE_USER
  password: '', //<-- YOUR_DATABASE_PASSWORD
  database: 'medic', //<-- YOUR_DATABASE_NAME
  timezone: '+00:00' //<-- YOUR_DATABASE_TIMEZONE
});

const api_config = {
  base: '/api',
  pages: {
    docs: true, //<-- Expose PAGE: /{{base}}/docs
    monitor: true //<-- Expose PAGE: /{{base}}/monitor
  },
  routes: {
    tb: [
      {      
        table: 'doctors', //<-- YOUR_TABLE_NAME
        event: 'DOCTOR', //<-- YOUR_EVENT_NAME 
        methods: ['GET', 'POST', 'PUT', 'DELETE'], //<-- YOUR_METHODS
        //Used only by methods 'POST' and 'PUT'
        columns: [
            {name: 'id', primary: true},
            {name: 'name'},
            {name: 'specialty'},
            {name: 'address'},
            {name: 'photo'}
        ]
      }
    ]  
  }
}

rest.buildRoutes(api_config)

Run

node server.js
# PAGES: {
#   api: 'http://127.0.0.1:8080/api',
#   docs: 'http://127.0.0.1:8080/api/docs',
#   monitor: 'http://127.0.0.1:8080/api/monitor'    
# }
# App listening at http://127.0.0.1:8080

Usage with .env file

npm i dotenv --save
// .env
NODE_ENV=development

IP=localhost
PORT=8000
COMPRESSION=gzip

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_USER=root
DB_PASSWORD=
DB_DATABASE=medic

API_KEY=65856b68541470a4ad95e7fe8b6bbb40
// server.js
const rest_docs = require('rest-docs');
var rest = new rest_docs();

rest.startServer()
rest.startDBServer();

const api_config = {
  base: '/api',
  table: {
    created_date: 'created',
    modified_date: 'modified',
    active: 'deleted',
    active_operator: '=',
    active_value: 0,
    delete_value: 1
  },
  routes: {
    tb: [
      {      
        table: 'doctors', //<-- YOUR_TABLE_NAME
        event: 'DOCTOR', //<-- YOUR_EVENT_NAME 
        methods: ['GET', 'POST', 'PUT', 'DELETE'], //<-- YOUR_METHODS
        //Used only by methods 'POST' and 'PUT'
        columns: [
            {name: 'id', primary: true},
            {name: 'name'},
            {name: 'specialty'},
            {name: 'address'},
            {name: 'photo'}
        ]
      }
    ]
  }  
}

rest.buildRoutes(api_config)

Run

node server.js
# PAGES: {
#   api: 'http://localhost:8000/api',
#   docs: 'http://localhost:8000/api/docs',
#   monitor: 'http://localhost:8000/api/monitor'    
# }
# App listening at http://localhost:8000

Result

  • GET /api/docs

API

Test API

  • GET /api/doctors

API

  • GET /api/doctors/:id

API

  • POST /api/doctors

API

  • PUT /api/doctors/:id

API

  • DELETE /api/doctors/:id

API

Pages

  • Docs /api/docs

DOCS

  • Monitor /api/monitor

MONITOR

Methods

  • startServer(SERVER_CONFIG)

The SERVER_CONFIG represents the connection to the server.

ConstantDefaultDescription
ip'localhost'Server ip
port8000Server port
compression''Compression strategy

Example:

// SERVER_CONFIG
{
  ip: {YOUR_SERVER_IP},
  port: {YOUR_SERVER_PORT},
  compression: {YOUR_COMPRESSION_STRATEGY}
}
  • startDBServer(CLIENT, CONNECTION_CONFIG)

The CLIENT parameter is required and determines which client adapter will be used with the library. By default: myslq.

DatabaseCLIENTAdditional command to install the appropriate database library
MariaDBmyslq$ npm i mysql --save
MSSQLmsslq$ npm i mssql --save
MySQLmyslq$ npm i mysql --save
PostgreSQLpg$ npm i pg --save
SQLite3sqlite$ npm i sqlite3 --save

The CONNECTION_CONFIG represents the connection parameters to the database.

ConstantDefaultDescription
host'localhost'Database host name
user'root'Database user name
password''Database password
database'database'Database name
timezone'+00:00'Database timezone

Example:

// CONNECTION_CONFIG
{
  host: {YOUR_DATABASE_HOST},
  user: {YOUR_DATABASE_USER},
  password: {YOUR_DATABASE_PASSWORD},
  database: {YOUR_DATABASE_NAME},
  timezone: {YOUR_DATABASE_TIMEZONE}
}
  • buildRoutes(API_CONFIG)

The API_CONFIG represents the API configuration.

ConstantDescription
baseMain path of the API
pagesPages of the API
tableMain configuration for all tables
routesAll API routes

Example:

// API_CONFIG
{
  base: '/api',
  pages: PAGE_CONFIG,
  table: TABLE_CONFIG,
  routes: ROUTE_CONFIG     
}

The PAGE_CONFIG represents the global configuration of the pages.

ConstantDefaultDescription
docstrueIndicates if the docs page is visible
monitortrueIndicates if the monitor page is visible

Example:

// PAGE_CONFIG
{      
  docs: true,  
  monitor: true
}

The TABLE_CONFIG represents the global configuration of table.

ConstantDefaultDescription
created_date'created'Column name that indicates the update date
modified_date'modified'Column name that indicates the update date
active'deleted'Column name indicating active status
active_operator'='Operator that is applied on the active comparison query
active_value0Value that is applied on the active comparison query
delete_value1Value that is applied in the soft delete query

Example:

// TABLE_CONFIG
{
  created_date: 'created',
  modified_date: 'modified',
  active: 'deleted',
  active_operator: '=',
  active_value: 0,
  delete_value: 1
}

The ROUTE_CONFIG represents the route groups.

ConstantDefaultDescription
tb[]Group for tables or views
fn[]Group for functions
sp[]Group for stored procedures

Example:

// ROUTE_CONFIG
{      
  tb: [
    TB_CONFIG,
    ...
  ],  
  fn: [
    FN_CONFIG,
    ...
  ],
  sp: [
    SP_CONFIG,
    ...
  ]
}

The TB_CONFIG represents the table or view configuration.

ConstantDefaultDescription
table'table'Table name
viewnullView name
event'TABLE'Event name(For socket.io event. => 'TABLE_INSERTED', 'TABLE_UPDATED', 'TABLE_DELETED')
methods'GET', 'POST', 'PUT', 'DELETE'List of methods to implement'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'SEARCH', 'SEARCH_COLUMN', 'POST_BATCH', 'PUT_BATCH', 'PATCH_BATCH', 'DELETE_BATCH'
columns[]List of columns(Used only by methods 'POST', 'PUT', 'PATCH', 'POST_BATCH', 'PUT_BATCH' and 'PATCH_BATCH')

Example:

// TB_CONFIG
{      
  table: 'table',  
  view: null,
  event: 'TABLE',  
  methods: ['GET', 'POST', 'PUT', 'DELETE'],  
  columns: [
    COLUMN_CONFIG,
    ...
  ]
}

The COLUMN_CONFIG represents the column of table.

ConstantDefaultDescription
name''Column name
primaryfalseDefines if column is primary key
hiddenfalseDefines if the column data will be sent in the response

Example:

// COLUMN_CONFIG
{
  name: 'id', 
  primary: true,
  hidden: true
}

The FN_CONFIG represents the function configuration.

ConstantDefaultDescription
function'function'Function name
params[]List of params

Example:

// FN_CONFIG
{      
  function: 'function',  
  params: [
    PARAM_CONFIG,
    ...
  ]
}

The SP_CONFIG represents the stored procedure configuration.

ConstantDefaultDescription
procedure'procedure'Stored procedure name
params[]List of params

Example:

// SP_CONFIG
{      
  procedure: 'procedure',  
  params: [
    PARAM_CONFIG,
    ...
  ]
}

The PARAM_CONFIG represents the param of function or stored procedure.

ConstantDefaultDescription
name''Param name

Example:

// PARAM_CONFIG
{
  name: 'n'
}

Examples

Migrations

Author

@victor-valencia.

License

Licensed under the MIT license.

1.2.0

2 years ago

1.1.0

2 years ago

1.0.6

2 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.5.6

3 years ago

0.5.5

3 years ago

0.5.4

3 years ago

0.5.3

3 years ago

0.5.2

3 years ago

0.5.1

3 years ago

0.5.0

3 years ago

0.4.5

3 years ago

0.4.4

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago

0.1.1

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago