Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eobject

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eobject

Generate Express.js routes easily and automatically from a object.

  • 0.3.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

eobject

eobject is a package designed to quickly and easily create a API from a JavaScript object.

Compatible with all data types valid in JavaScript objects, eobject automatically generates Express.js routes.

JSONWeb
A JSON Objectexample.com/info - {name:"Test",version: "1.0.0"}
example.com/info/name - Test
example.com/info/version - 1.0.0
example.com/users - {}
example.com/users/add - nothing
example.com/users/add?email=johndoe@example.com - johndoe@example.com

A note on functions

Functions do work in eobject. It is one of the primary use cases for eobject. Functions that have arguments map both to URL query strings, as well as the body of the function.

[Learn more about the usage of functions in eobject in the dedicated section] (#functions)

Getting Started

  • Install the package: npm install eobject
  • Start with the setup function to provide the object: eobject.setup(*object*,*settings (optional)*);
  • Use the generator function in app.use();.
    To use on the root: app.use(eobject.generator);
    To use on a directory: app.use('/*path*',eobject.generator);
  • Then, it should be accessible. Ex: *object*.info.name should corespond to /info/name

See the full working example for more guidance.

Docs

eobject.setup( object , ?settings )

The setup function configures eobject with the object that it should generate routes from. It accepts a JavaScript object and a optional settings object.

Example
eobject.setup({test:"test"})

var object = {test: "test"}
var settings = {debug: true}
eobject.setup(object,settings)

eobject.generator( req , res , next )

Important: The generator function is a Express.js middleware function and shouldn't be used directory.
To only be used within: app.use(eobject.generator) or app.use('*PATH*',eobject.generator)

Example
app.use('/api',eobject.generator)

app.use(eobject.generator)

Settings Documentation

Default Settings Object
{
  "debug": false,
  "secureonly": false,
  "rootpagedisplay": true,
  "ifnotfoundsendtonext": true,
  "ifemptyobjectstillsend": true,
  "acceptqueryfunctionarguments": true,
  "acceptbodyfunctionarguments": true,
  "validtypes": {
    "function": true,
    "object": true,
    "string": true,
    "number": true
  }
}
Settings Options

The following table specifies each settings option:

NameDescription
debugOption for debug mode. It will show the process behind each and every request.
secureonlyOption for allowing secure connections only. Only HTTPS connections will be allowed and anything else will be rejected by a 403 Forbidden response.
rootpagedisplayOption to display the root 'welcome' screen.
ifnotfoundsendtonextOption to not send any unresolved requests beyond eobject.
ifemptyobjectstillsendOption to still send any empty objects (empty object: {}) back to the client.
acceptqueryfunctionargumentsOption to disable accepting function arguments from URL query strings.
acceptbodyfunctionargumentsOption to enable accepting function arguments from the request body. (Read enabling function argument matching from body)
validtypesOptions to independently disable any specific data type from being used in eobject.

Functions

Although all valid data types that work in JavaScript objects do work in eobject, functions are their own special data type which requires more work in order to be used properly.

For functions to effectively operate as an API, there must be methods of inputting function arguments from the client side. eobject has two ways of acomplishing this at this time:

  • URL Query Strings
  • Request Body

Pulling arguments from the query string of the requested URL

eobject by default pulls argument data from the query string of the URL that is requested via key/value combinations of the query string, parsed by the native URL constructor. You can disable pulling arguments from query strings via the acceptqueryfunctionarguments option in the [settings] (#settings-options).

The order of the query strings does not matter, but the name of the key must exactly match the name of the argument in the function, including the case.
Example:
If the argument name is firstName:
/users/add?firstName=example@example.com - Will Work
/users/add?FirstName=example@example.com - Will Not Work
/users/add?firstname=example@example.com - Will Not Work

Pulling arguments from the request body

eobject also has the option of pulling argument data from a JSON-formatted body of a HTTP request. This functionality can be enabled via the acceptbodyfunctionarguments option in the [settings] (#settings-options).

Although having a body for other types of requests is theoretically valid, eobject only accepts common practice request bodies in PUT, POST and PATCH requests.

Express also requires that the body-parser package be used to parse the request body. The full working example includes a implementation of this. It requires the following lines of code be included before the app.use(express.generator(*object*)) line and after app initalization of Express:

const bodyParser = require("body-parser");
app.use(bodyParser.json())

Example

Full working example:

const eobject = require('eobject');
const express = require('express');
const bodyParser = require("body-parser");
const app = express();

var object = {
  info: {
    name: "Test",
    version: '1.0.0'
  },
  users: {
    add: async function(firstName,lastName,email) {
      return {
        name: firstName + " " + lastName,
        email: email
      }
    }
  }
}

var settings = {
  "acceptbodyfunctionarguments": true,
  "secureonly": true
}

app.use(bodyParser.json())

eobject.setup(object,settings);
app.use('/',eobject.generator)

app.get('*',(req,res) => {
  res.send("test")
})

app.listen(3000, () => {
  console.log('server started');
});

Keywords

FAQs

Package last updated on 27 Mar 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc