Socket
Socket
Sign inDemoInstall

eobject

Package Overview
Dependencies
0
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    eobject

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


Version published
Weekly downloads
2
Maintainers
1
Install size
6.75 kB
Created
Weekly downloads
 

Readme

Source

eobject

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

Compatible with all data types, including functions, 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
The above is based on the example.

A note on functions:

Functions do work in eobject. Functions, if they have parameters, will pull them from the query string (?firstName=John&lastName=Doe) and send them matched to the parameter names of the function.
Order of the query strings does not matter, but the name of the field/key must exactly match the name of the parameter in the function.
Example:
/users/add?email=example@example.com - Will Work
/users/add?Email=example@example.com - Will Not Work
/users/add?mail=example@example.com - Will Not Work

Getting Started

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

Docs

eobject.setup( object , settings (optional) )

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

Usage Example
const eobject = require('eobject');

var object = {
  properties: {
    a: "yes",
    b: "no"
  }
}
var settings = {
  debug: true
}

eobject.setup(object,debug)

eobject.generator( req , res , next )

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

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

settings

The settings object currently only takes the debug property.

Usage Example
var settings = {
  debug: true
}

eobject.setup(object,debug)

Example

const express = require('express');
const app = express();

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

const eobject = require('eobject');
eobject.setup(object);
app.use('/api',eobject.generator)

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

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

Keywords

FAQs

Last updated on 12 Dec 2021

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc