cluster0:http-publish

v0.0.1Published last month

zcfs:http-publish Build Status

This package add the ability to add HTTP server publish to your project. It's a server-side package only.

DEPRECATING: Use https://atmospherejs.com/simple/rest instead

Usage

HTTP.publish creates a http crud restpoint for a collection - only one cursor is allowed pr. publish

Security

CRUD+L - Create Read Update Delete + List are common rest point operations.

All CUD methods are the exact same as the ddp methods handlers - This means that Meteor.allow and Meteor.deny are setting the access rules for both ddp and http collection methods.

All R+L methods are limited to the publish function.

Fully mounted

If handed a collection and a publish function the HTTP.publish will mount on follow urls and methods:

  • GET - /api/list - all published data
  • POST - /api/list - insert a document into collection
  • GET - /api/list/:id - find one published document
  • PUT - /api/list/:id - update a document
  • DELETE - /api/list/:id - remove a document
1  myCollection = new Meteor.Collection('list');
2
3  // Add access points for `GET`, `POST`, `PUT`, `DELETE`
4  HTTP.publish({collection: myCollection}, function(data) {
5    // this.userId, this.query, this.params
6    return myCollection.find({});
7  });

Publish view only

If handed a mount name and a publish function the HTTP.publish will mount:

  • GET - /mylist - all published data
1  myCollection = new Meteor.Collection('list');
2
3  // Add access points for `GET`
4  HTTP.publish({name: 'mylist'}, function(data) {
5    // this.userId, this.query, this.params
6    return myCollection.find({});
7  });

Create Update Delete only

If handed a collection only the HTTP.publish will mount:

  • POST - /api/list - insert a document into collection
  • PUT - /api/list/:id - update a document
  • DELETE - /api/list/:id - remove a document
1  myCollection = new Meteor.Collection('list');
2
3  // Add access points for `POST`, `PUT`, `DELETE`
4  HTTP.publish({collection: myCollection});

Publish scope

The publish scope contains different kinds of inputs. We can also get user details if logged in.

  • this.userId The user whos id and token was used to run this method, if set/found
  • this.query - query params ?token=1 -> { token: 1 }
  • this.params - Set params /foo/:name/test/:id -> { name: '', id: '' }

Passing data via header

From the client:

1  HTTP.get('/api/list', {
2    data: { foo: 'bar' }
3  }, function(err, result) {
4    console.log('Content in parsed json: ');
5    console.log(result.data);
6  });

HTTP Server method:

1  HTTP.publish({collection: myCollection}, function(data) {
2    // data === { foo: 'bar' }
3  });

Authentication

For details on authentication of http calls please read the Authentication part in HTTP.methods package

The publish will have the this.userId set if an authenticated user is making the request.

Format handlers

The query parametre format is used to set different output formats. The buildin format is json (EJSON since we are on Meteor)

Example: (json is buildin)

1  // Format the output into json
2  HTTP.publishFormats({
3    'json': function(result) {
4      // Set the method scope content type to json
5      this.setContentType('application/json');
6      // Return EJSON string
7      return EJSON.stringify(result);
8    }
9  });

GET url: /api/list?format=json

1    HTTP.get('/api/list', {
2      params: {
3        format: 'json'
4      }
5    }, function(err, result) {
6      console.log('Back from update');
7      if (err) {
8        console.log('Got error');
9      }
10      console.log('Got json back: ' + result.content);
11    });

Unpublish

For api integrity theres added an HTTP.unpublish method that takes a collection or name of mount point to remove.

API Documentation

Here