0.3.4 • Published 6 months ago

dc-api-core v0.3.4

Weekly downloads
41
License
MIT
Repository
-
Last release
6 months ago

Simple API core for your projects

NPM

Useful links

Documentation image

Dependencies


Structure

📙
 ├── ⚙️ controllers      Request controllers
 ├── 🗃️ models           Models for working with DB
 │   └── 📁 <driver>     Database driver name (Optional)
 │       └── 📜 <model>  Model name (js or json)
 ├── ️📃 config.json      Configuration file
 └── ⏱ startup.js       Script, that was started before strting API server

Installation

0) Run npm init or yarn init

1) Install package - npm i dc-api-core --save or yarn add dc-api-core

2) Run npm exec dc-api-core init or yarn dc-api-core init

3) Run npm run dc-init or yarn dc-init

4) Run npm start or yarn start

5) Done!

CLI

You can use dc-api-core command locally in package.json scripts.

Options:

  • No options - Just running your project.
  • init - Create example files and start scripts.
  • --dev - Running project in development mode.
  • --cfg <path> - Overrides config.json location. You can use both relative and absolute paths.

config.json

FieldDefaultDescription
dbOptionalObject
db[driverName]Code of database driver
db[driverName].nameRequiredDatabase name
db[driverName].portDefined by pluginDatabase port
db[driverName].userOptionalDatabase username
db[driverName].passand password
db[driverName].srvOptional for mongoBoolean, true - use srv
session.secretRequiredPrivate string for cookie
session.storeRequiredDatabase config name
session.ttl3d (3 days)Session lifetime in zeit/ms format
sslOptionalEnables HTTPS mode if filled
ssl.*OptionalAny μWS.SSLApp options field
ssl.keyRequiredLocal path to private key
ssl.certRequiredLocal path to certificate file
plugins[]Array of plugin packages names
originOrigin headerAccept requests only from this origin
port8081API listing port
ws_timeout60WebSocket request waiting timeout in seconds
ignore[]Excluded directories in development mode
isDevRead-onlytrue if using --dev argument
dev{}Config to merge if isDev is true
ttl0WebSocket TTL in seconds, 0 - disabled

Example:

{
    "port": "$env", // Equals value of process.env.PORT
    "db": {
        "mongo": {
            "host": "localhost",
            "name": "test-db"
        }
    },
    "plugins": ["dc-api-mongo"],
    "session": {
        "secret": "super secret string",
        "store": "mongo"
    },
    "ssl": {
        "cert": "/etc/letsencrypt/live/awesome.site/cert.pem",
        "key": "/etc/letsencrypt/live/awesome.site/privkey.pem"
    },
    "dev": {
        "port": 8081,
        "db": {
            "mongo": { "name": "test-dev-db" }
        }
    }
}

DB module

require('dc-api-core/DB'): {
    [string: code]: (config?: string, template?: Object) => DBDriver
}
  • code - Registered code of database driver. For example dc-api-mongo registers mongo.
  • config - Configuration name after dot in config.json. Ex. mongo('dev') points to db['mongo.dev'].
  • template - Object that overrides selected configuration.
  • DBDriver - Mongoose-like object (not always, defined by plugin)

Example:

const db = require('dc-api-core/DB').mongo();

Where mongo - your database driver name.

Example: If you're using MySQL, use mysql as database driver (don't forget to apply plugin first).

const db = require('dc-api-core/DB').mysql();

Plugins

For first, install plugin package via npm or yarn. After this add name of plugin package to plugins array in config.json.

Example config.json:

{
    // ...
    "plugins": ["dc-api-mongo"]
}

If you want create your own plugin, read plugin development documentation


Sessions

Functions

FunctionExampleDescription
this.session.<name>this.session.name = 'User'Set session data
this.session.save()await this.session.save()Save session data
this.session.destroy()await this.session.destroy()Clear all session data

Example

module.exports = class Controller {
    async test () {
        this.session.name = 'test';
        await this.session.save();
        this.send('saved');
    }
}

Request hooks

onLoad

Will be executed before calling action method in controller.

If the onLoad function returns false, the request will be rejected.

Example

module.exports = class Test {
    onLoad () {
        if (!this.session.user) return false;
    }
}

Working with config.json

Require

Require config module:

const config = require('dc-api-core/config');

Get data:

config.<your_param>

Example

const config = require('dc-api-core/config');

module.exports = class Test {
    index() {
     this.send(config.myParam);
    }
}

Routing

Register route in startup script:

// startup.js
const Router = require('dc-api-core/router');
Router.register('/testing/files/${id}/${file}.jpg', 'Test.getFile');

Now requests like /testing/files/some-id/secret_file.jpg will call getFile method of Test controller.

// controllers/Test.js
class Test {
    async getFile () {
        this.send(this.params);
        // Will send { "id": "some-id", "file": "secret_file" }
    }
}

module.exports = Test;

My TODOs

  • Support for glibc < 2.18
  • Typing (.d.ts) files
  • Automatical package publication when all tests are passed
0.3.6-1

8 months ago

0.3.6-2

6 months ago

0.3.6

9 months ago

0.3.5

10 months ago

0.3.5-1

9 months ago

0.3.4-3

1 year ago

0.3.4-2

1 year ago

0.3.4-1

1 year ago

0.3.4

1 year ago

0.3.3-1

1 year ago

0.3.3

2 years ago

0.3.2

2 years ago

0.4.0-test1

2 years ago

0.3.1-2

2 years ago

0.3.1-1

3 years ago

0.3.1

3 years ago

0.3.0-pre5

3 years ago

0.3.0-pre4

3 years ago

0.3.0-pre3

3 years ago

0.3.0-pre1

3 years ago

0.2.5-3

3 years ago

0.2.5-2

3 years ago

0.2.5-1

3 years ago

0.2.5

3 years ago

0.2.4-1

4 years ago

0.2.4-f1

4 years ago

0.2.4

4 years ago

0.2.3-9

4 years ago

0.2.3-8

4 years ago

0.2.3-7

4 years ago

0.2.3-6

4 years ago

0.2.3-5

4 years ago

0.2.3-4

4 years ago

0.2.3-3

4 years ago

0.2.3-2

4 years ago

0.2.3-1

4 years ago

0.2.3-p0

4 years ago

0.2.3

4 years ago

0.2.2-8

4 years ago

0.2.2-7

4 years ago

0.2.2-p6

4 years ago

0.2.2-6

4 years ago

0.2.2-5

5 years ago

0.2.2-4

5 years ago

0.2.2-2

5 years ago

0.2.2-1

5 years ago

0.2.2

5 years ago

0.2.1-4

5 years ago

0.2.1-3

5 years ago

0.2.1-2

5 years ago

0.2.1-1

5 years ago

0.2.1

5 years ago

0.2.0-4

5 years ago

0.2.0-2

5 years ago

0.2.0-1

5 years ago

0.2.0

5 years ago

0.1.9-3

5 years ago

0.1.9-2

5 years ago

0.1.9-1

5 years ago

0.1.9

5 years ago

0.1.8-6

5 years ago

0.1.8-5

5 years ago

0.1.8-4pre

5 years ago

0.1.8-4

5 years ago

0.1.8-3

5 years ago

0.1.8-2

5 years ago

0.1.8-1

5 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5-1

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3-1

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago