|
|
4 ani în urmă | |
|---|---|---|
| .. | ||
| packages | 4 ani în urmă | |
| .editorconfig | 4 ani în urmă | |
| .gitignore | 4 ani în urmă | |
| .jshintrc | 4 ani în urmă | |
| .travis.yml | 4 ani în urmă | |
| LICENSE.md | 4 ani în urmă | |
| README.md | 4 ani în urmă | |
| api.md | 4 ani în urmă | |
| http.publish.client.api.js | 4 ani în urmă | |
| http.publish.server.api.js | 4 ani în urmă | |
| http.publish.tests.client.js | 4 ani în urmă | |
| http.publish.tests.server.js | 4 ani în urmă | |
| internal.api.md | 4 ani în urmă | |
| package.js | 4 ani în urmă | |

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
HTTP.publish creates a http crud restpoint for a collection - only one cursor is allowed pr. publish
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.
If handed a collection and a publish function the HTTP.publish will mount on follow urls and methods:
GET - /api/list - all published dataPOST - /api/list - insert a document into collectionGET - /api/list/:id - find one published documentPUT - /api/list/:id - update a documentDELETE - /api/list/:id - remove a document
myCollection = new Meteor.Collection('list');
// Add access points for `GET`, `POST`, `PUT`, `DELETE`
HTTP.publish({collection: myCollection}, function(data) {
// this.userId, this.query, this.params
return myCollection.find({});
});
If handed a mount name and a publish function the HTTP.publish will mount:
GET - /mylist - all published data
myCollection = new Meteor.Collection('list');
// Add access points for `GET`
HTTP.publish({name: 'mylist'}, function(data) {
// this.userId, this.query, this.params
return myCollection.find({});
});
If handed a collection only the HTTP.publish will mount:
POST - /api/list - insert a document into collectionPUT - /api/list/:id - update a documentDELETE - /api/list/:id - remove a document
myCollection = new Meteor.Collection('list');
// Add access points for `POST`, `PUT`, `DELETE`
HTTP.publish({collection: myCollection});
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/foundthis.query - query params ?token=1 -> { token: 1 }this.params - Set params /foo/:name/test/:id -> { name: '', id: '' }From the client:
HTTP.get('/api/list', {
data: { foo: 'bar' }
}, function(err, result) {
console.log('Content in parsed json: ');
console.log(result.data);
});
HTTP Server method:
HTTP.publish({collection: myCollection}, function(data) {
// data === { foo: 'bar' }
});
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.
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)
// Format the output into json
HTTP.publishFormats({
'json': function(result) {
// Set the method scope content type to json
this.setContentType('application/json');
// Return EJSON string
return EJSON.stringify(result);
}
});
GET url: /api/list?format=json
HTTP.get('/api/list', {
params: {
format: 'json'
}
}, function(err, result) {
console.log('Back from update');
if (err) {
console.log('Got error');
}
console.log('Got json back: ' + result.content);
});
For api integrity theres added an HTTP.unpublish method that takes a collection or name of mount point to remove.