Code Monkey home page Code Monkey logo

restle's Introduction

Restle

Build Status npm version

Restle is a lightweight (and unstable) JSON API engine compatible with Ember Data. This library wraps Express and Mongoose. This is alpha software and not recommended for use in production code.

Not all features in the JSON API specification are implemented yet: fields, voluntary inclusion, and some other small things. I'm trying to match the test suite as close as possible with the JSON API spec. Reach out on GitHub for feature requests and any bugs you encounter, thanks!

Getting started

$ npm install restle --save
// API: http://localhost:1337/api/
import Restle from 'restle';

const restle = new Restle({
  port: 1337,
  database: 'mongodb://...',
  namespace: '/api',
  cors: ['http://localhost:4200', 'https://example.com']
});

// define schemas
const userSchema = {
  name: { attr: 'string' },
  birthday: { attr: 'date' },
  isMarried: { attr: 'boolean' },
  articles: { hasMany: 'article' },
  company: { belongsTo: 'company' },
};

const articleSchema = {
  title: { attr: 'string' },
  body: { attr: 'string' },
  createdOn: { attr: 'date' },
};

const companySchema = {
  name: { attr: 'string' },
  employees: { hasMany: 'user' },
};

// register schemas
restle.register('user', userSchema);
restle.register('article', articleSchema);
restle.register('company', companySchema);

// check out some events
restle.on('ready', () => {
  console.log('Database has connected!');
});

// verify the user with a JSON Web Token
// all events except ready have express `req`, `res`, and `next` arguments
import jwt from 'jsonwebtoken';

restle.on('before', (req, res, next) => {
  console.log('Intercept all requests to your API.');
  const token = req.get('authorization');
  const secret = new Buffer(process.env.SECRET_KEY, 'base64');

  jwt.verify(token, secret, (err, decoded) => {
    // life ain't so good
    if (err) {
      return res.status(403).json({
        isVerified: false,
        error: err
      });
    }

    // life is good
    next();
  });
});

// add a company to a user if there isn't one
restle.on('user.create', (req, res, next) => {
  console.log('Fires before creating a user.');

  // Mongoose object
  const company = restle.model('company');

  if (!req.body.data.relationships.company) {
    company.find({}, (err, companies) => {
      if (err) {
        return res.sendStatus(500);
      }

      req.body.data.relationships.company = {
        type: 'company',
        id: companies[0]._id;
      };

      next();
    });  
  }
});

Complete list of events for the user model:

  • user.find
  • user.create
  • user.findOne
  • user.update
  • user.delete
  • user.findRelationship
  • user.appendRelationship
  • user.updateRelationship
  • user.deleteRelationship
// request
GET /users HTTP/1.1
Accept: application/vnd.api+json

// response
HTTP/1.1 200 OK
Content-Type: application/vnd.api+json

{
  "data": [{
    "type": "user",
    "id": "1",
    "attributes": {
      "name": "Bob"
    },
    "links": {
      "self": "http://localhost:1337/api/users/1"
    },
    "relationships": {
      "company": {
        "links": {
          "self": "http://localhost:1337/api/articles/1/relationships/company",
          "related": "http://localhost:1337/api/articles/1/company"
        },
        "data": { "type": "company", "id": "9" }
      },
      "articles": {
        "links": {
          "self": "http://localhost:1337/api/users/1/relationships/articles",
          "related": "http://localhost:1337/api/users/1/articles"
        },
        "data": [
          { "type": "article", "id": "5" },
          { "type": "article", "id": "12" }
        ]
      }
    }
  }],
  "included": [{
    "type": "company",
    "id": "9",
    "attributes": {
      "name:" "Apple"
    },
    "links": {
      "self": "http://localhost:1337/api/companies/9"
    }
  }, {
    "type": "article",
    "id": "5",
    "attributes": {
      "title": "Awesome title",
      "body": "Awesome text"
    },
    "links": {
      "self": "http://localhost:1337/api/articles/5"
    }
  }, {
    "type": "article",
    "id": "12",
    "attributes": {
      "title": "Another title",
      "body": "Another text"
    },
    "links": {
      "self": "http://localhost:1337/api/articles/12"
    }
  }]
}

restle's People

Contributors

dylnslck avatar jebukowski avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.