Home

Awesome

json-schema-agent

Please note this library is not ready for production use.

JSON Hyper-Schema HTTP REST client. Together with json-schema-core and json-schema-hyper, this library provides a basic mechanism for correlating instances with schema over HTTP. It also provides validation of request and response messages (schema and targetSchema), if json-schema-valid is used.

Refer to the JSON Schema Core v4 and Hyper-Schema IETF Internet Draft specs for more info.

Installation

component:

$ component install ericgj/json-schema-agent

npm:

$ npm install json-schema-agent-component

Examples


// Simple GET

var agent = new Agent();
agent.get('http://some.uri/path', function(err,correlation){
  correlation.instance  // the JSON instance (parsed body of response)
  correlation.schema    // the schema
})


// POST using link attributes

var link = { href: 'http://example.com/thing',
             rel: 'create',
             mediaType: 'application/vnd.thing+json'
           }

agent.post(link, obj, fn);

// or automatically follow method defined in link

link.method = 'POST'
agent.follow(link, obj, fn);


// follow chain of links

agent.follow(link, function(err, correlation){
  var nextItem = correlation.rel('next');  // find link rel="next" in the schema
  agent.follow(nextItem, fn)
})


// fetch and dereference schema from link
// note schema is cached to the agent

agent.getSchema(link, function(err,schema){
  schema    // the parsed, dereferenced schema
})


// dereference raw schema object

agent.dereference(data, function(err,schema){
  schema    // the parsed, dereferenced schema
})


// Configuration

// set default base uri for resolving relative URIs in links
// in-browser, typically you'd want to set this to window.location.origin

agent.base('http://example.com/api'); 


// set underlying http client (class)
var httpClient = require('superagent');
Agent.service(httpClient);


API

Running tests

In browser:

  1. Unit tests:
  1. Functional tests:
$ node test/server.js

And browse to http://localhost:3000/functional.html

In Node-land:

  1. Unit tests:
$ npm test
  1. Functional tests:
$ node test/server.js &
$ mocha --ui bdd test/functional.js

Notes

Limitations

License

MIT