1.1.7 • Published 6 months ago

the-framework v1.1.7

Weekly downloads
17
License
ISC
Repository
github
Last release
6 months ago

The Framework

The Framework is an extremely light weight API building framework for NodeJS. It has very few dependencies, doesn't use Express (or any other framework), and is very fast 🔥.

Installing

npm install the-framework --save

Quick Setup

Place the following code in your main app file

const theFramework = require("the-framework");
const PORT = process.env.PORT;

theFramework.startServer({
    authenticationMethod: async (req, token) => {
        // Optional method to check user token and return either a user object or null (if you cannot authenticate your user)
        if (token) {
            // Code to check token
        }

        return null;
    },
    apiDirectory: "/api", // Directory you will store your route files in
    userTokenHeader: "x-user-token", // Header you will use for your user tokens
    port: PORT
});

And then create .js route files under the /api directory (or whatever you specified above in apiDirectory).

Any route files you create under this directory will be automatically added to the framework.

Route file structure

Route files are just standard NodeJS files, and look something like:

const theFramework = require("the-framework");

theFramework.get("/hello", [
    {id: "name", type: theFramework.STRING, required: true, description: "Your name"}
], {
    description: "Says hello",
    authRequired: false
}, async (params, user) => {
    // Params is an object of processed parameters
    // user is the logged in user, if there is one
    return {message: "Hello " + params.name}
});

Supported Parameter types

TypeDescription
STRINGString
INTEGERInteger, e.g. 4
FLOATFloating point number, e.g. 2.5
ENUMEnum options, e.g. Male, Female - you must also provide a validValues array in the parameter config
DATEDate, defaults to YYYY-MM-DD format, unless you pass a date format to the dateFormat parameter
BOOLEANBoolean, e.g. true or false
IMAGEImage file upload
FILEFile upload
OBJECTJSON Object
UUIDValid UUID String

Throwing exceptions

If you throw exceptions in your code, by default a 501 server error will be returned.

Throwing an object with a status and a message will allow you to throw a different error code.

e.g.

{
    status: 123,
    message: "Error message here"
}

Returning non JSON Content Types

It is possible to return non JSON content by specifying content_type and content in your return object, e.g.

{
    content_type: "text/csv",
    content: "...csv content..."
}

You can also set content_disposition if you want, for example, to force a download of the file.

Uploading files and images

If you specify that a parameter is a FILE or IMAGE, the parameter object will look something like:

{
    path: // Where the file has been saved (usually tmp dir),
    mimetype: // the mimetype of the uploaded filename
    original_filename: // the original filename of the file
}

Note: you can optionally specify a file size limit in the main config object.

E.g. for 10MB limit:

fileSizeLimit: 10000000

Disable docs and JSON route listing

By default, if you hit /docs.json you will get back a json list of all of your route. To disable this, pass disableListing: true in your server config.

Similarly,by default, if you hit /docs you will get back some nicely formatted auto generated API docs. To disable this, pass disableDocs: true in your server config.

1.1.7

6 months ago

1.1.6

6 months ago

1.1.5

11 months ago

1.1.4

1 year ago

1.1.1

2 years ago

1.1.3

1 year ago

1.1.2

2 years ago

1.1.0

2 years ago

1.0.32

2 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.29

4 years ago

1.0.28

4 years ago

1.0.27

4 years ago

1.0.26

4 years ago

1.0.25

4 years ago

1.0.24

4 years ago

1.0.23

4 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago