-
Notifications
You must be signed in to change notification settings - Fork 83
updates #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KristinaMatuleviciute
wants to merge
3
commits into
rjrodger:master
Choose a base branch
from
KristinaMatuleviciute:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
updates #29
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "extends": ["seneca"] | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,116 +1,99 @@ | ||
| // PUBLIC DOMAIN | ||
| "use strict"; | ||
| 'use strict' | ||
|
|
||
| module.exports = function api( options ) { | ||
| module.exports = function api (options) { | ||
| var seneca = this | ||
|
|
||
| seneca.add('role:api,info:hello', hello) | ||
|
|
||
| seneca.add('role:api,product:star', get_star) | ||
| seneca.add('role:api,product:handle_star', handle_star) | ||
|
|
||
|
|
||
| seneca.add('init:api',function(args,done){ | ||
|
|
||
| seneca.add('init:api', function (args, done) { | ||
| // Order is significant here! | ||
|
|
||
| seneca.act('role:web',{use:{ | ||
| prefix:'/', | ||
| pin:'role:api,info:*', | ||
| map:{ | ||
| hello:true | ||
| seneca.act('role:web', {use: { | ||
| prefix: '/', | ||
| pin: 'role:api,info:*', | ||
| map: { | ||
| hello: true | ||
| } | ||
| }}) | ||
|
|
||
| seneca.use( | ||
| {name:'jsonrest-api',tag:'product'}, | ||
| {name: 'jsonrest-api', tag: 'product'}, | ||
| { | ||
| prefix: '/', | ||
| list: {embed:'list'}, | ||
| pin: { name:'product' }, | ||
| list: {embed: 'list'}, | ||
| pin: { name: 'product' }, | ||
| startware: verify_token, | ||
| allow_id: true | ||
| }) | ||
|
|
||
| seneca.act('role:web',{use:{ | ||
| prefix:'/product', | ||
| pin:'role:api,product:*', | ||
| seneca.act('role:web', {use: { | ||
| prefix: '/product', | ||
| pin: 'role:api,product:*', | ||
| startware: verify_token, | ||
| map:{ | ||
| star: { | ||
| alias:'/:id/star' | ||
| map: { | ||
| star: { | ||
| alias: '/:id/star' | ||
| }, | ||
| handle_star:{ | ||
| PUT:true, | ||
| DELETE:true, | ||
| alias:'/:id/star' | ||
| handle_star: { | ||
| PUT: true, | ||
| DELETE: true, | ||
| alias: '/:id/star' | ||
| } | ||
| } | ||
| }}) | ||
|
|
||
| done() | ||
| }) | ||
|
|
||
|
|
||
| var internal_err = { | ||
| http$: {status:500}, | ||
| why: 'Internal error.' | ||
| http$: {status: 500}, | ||
| why: 'Internal error.' | ||
| } | ||
|
|
||
| var bad_input = { | ||
| http$: {status:400}, | ||
| why: 'Bad input.' | ||
| http$: {status: 400}, | ||
| why: 'Bad input.' | ||
| } | ||
|
|
||
|
|
||
| function verify_token(req,res,next) { | ||
| function verify_token (req, res, next) { | ||
| var token = req.headers['api-access-token'] | ||
|
|
||
| // Hard coded token for this example!!! | ||
| if( '1234' == token ) return next(); | ||
|
|
||
| res.writeHead(401,"Access denied.") | ||
| // Hard coded token for this example!!! | ||
| if('1234' === token) return next() | ||
| res.writeHead(401, 'Access denied.') | ||
| res.end() | ||
| } | ||
|
|
||
|
|
||
| function hello( args, done ) { | ||
| done(null,{msg:'hello!'}) | ||
| function hello (args, done) { | ||
| done(null, {msg: 'hello!'}) | ||
| } | ||
|
|
||
| function get_star( args, done ) { | ||
| this.make$('product').load$(args.id,function(err,res){ | ||
| if(err) return done(null,internal_err); | ||
| if(!res) return done(null,bad_input); | ||
|
|
||
| return done(null,{ | ||
| function get_star (args, done) { | ||
| this.make$('product').load$(args.id, function (err, res) { | ||
| if(err) return done(null, internal_err) | ||
| if(!res) return done(null, bad_input) | ||
| return done(null, { | ||
| product: res.id, | ||
| star: res.star | ||
| star: res.star | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| function handle_star( args, done ) { | ||
| function handle_star (args, done) { | ||
| this.act( | ||
| 'role:product,cmd:star', | ||
| 'role:product,cmd:star', | ||
| { | ||
| // product id | ||
| id:args.id, | ||
|
|
||
| id: args.id, | ||
| // PUT means star, DELETE means unstar | ||
| star:('PUT'===args.req$.method) | ||
| star: ('PUT' === args.req$.method) | ||
| }, | ||
|
|
||
| function(err,res){ | ||
| if(err) return done(null,internal_err); | ||
| if(!res.ok) return done(null,bad_input) | ||
|
|
||
| return done(null,{ | ||
| product: res.id, | ||
| star: res.star | ||
| }) | ||
| } | ||
| ) | ||
| function (err, res) { | ||
| if(err) return done(null, internal_err) | ||
| if(!res.ok) return done(null, bad_input) | ||
| return done(null, { | ||
| product: res.id, | ||
| star: res.star | ||
| }) | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,14 @@ | ||
| // PUBLIC DOMAIN | ||
|
|
||
| var seneca = require('seneca')() | ||
| .use('./product_catalog') | ||
| .use('./api') | ||
| .ready(function(){ | ||
| .ready(function () { | ||
| this.make$('product') | ||
| .make$({id$:0,name:'Apple',price:99,star:0}).save$() | ||
| .make$({id$:1,name:'Orange',price:199,star:0}).save$() | ||
| .make$({id$: 0, name: 'Apple', price: 99, star: 0}).save$() | ||
| .make$({id$: 1, name: 'Orange', price: 199, star: 0}).save$() | ||
| }) | ||
|
|
||
| var app = require('express')() | ||
| .use( require('body-parser').json() ) | ||
| .use( seneca.export('web') ) | ||
| require('express')() | ||
| .use(require('body-parser').json()) | ||
| .use(seneca.export('web')) | ||
| .listen(3000) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,51 +1,39 @@ | ||
| var util = require('util') | ||
| var util = require ('util') | ||
|
|
||
| var request = require('request') | ||
| var _ = require('lodash') | ||
| var Request = require('request') | ||
| var _ = require('lodash') | ||
|
|
||
| var base = 'http://localhost:3000/' | ||
| var headers = {'api-access-token':'1234'} | ||
|
|
||
|
|
||
| ;print('get',{url:base+'hello',headers:headers},function(){ | ||
|
|
||
| ;print('get',{url:base+'product',headers:headers},function(){ | ||
| ;print('get',{url:base+'product/0',headers:headers},function(){ | ||
| ;print('get',{url:base+'product/1',headers:headers},function(){ | ||
| ;print('get',{url:base+'product/2',headers:headers},function(){ | ||
|
|
||
| ;print('post',{url:base+'product',headers:headers,json:{ | ||
| id$:2, name:'Pear', price:299 | ||
| }},function(){ | ||
|
|
||
| ;print('get',{url:base+'product/2',headers:headers},function(){ | ||
|
|
||
| ;print('put',{url:base+'product/2',headers:headers,json:{ | ||
| name:'Pear', price:Math.floor(300*Math.random()) | ||
| }},function(){ | ||
|
|
||
| ;print('get',{url:base+'product/2',headers:headers},function(){ | ||
|
|
||
| ;print('del',{url:base+'product/1',headers:headers},function(){ | ||
| ;print('get',{url:base+'product/1',headers:headers},function(){ | ||
|
|
||
| ;print('get',{url:base+'product/0/star',headers:headers},function(){ | ||
| ;print('put',{url:base+'product/0/star',headers:headers},function(){ | ||
| ;print('get',{url:base+'product/0/star',headers:headers},function(){ | ||
|
|
||
| })})})})})})})})})})})})})}) | ||
|
|
||
|
|
||
|
|
||
| // utility function to pretty print request results | ||
| function print() { | ||
| var headers = {'api-access-token': '1234'} | ||
|
|
||
|
|
||
| ;print('get', {url: base + 'hello', headers: headers}, function () { | ||
| ;print('get', {url: base + 'product', headers: headers}, function () { | ||
| ;print('get', {url: base + 'product/0', headers: headers}, function () { | ||
| ;print('get', {url: base + 'product/1', headers: headers}, function () { | ||
| ;print('get', {url: base + 'product/2', headers: headers}, function () { | ||
| ;print('post', {url: base + 'product', headers: headers, json: { | ||
| id$: 2, name: 'Pear', price: 299 | ||
| }}, function () { | ||
| ;print('get', {url: base + 'product/2', headers: headers}, function () { | ||
| ;print('put', {url: base + 'product/2', headers: headers, json: { | ||
| name: 'Pear', price: Math.floor(300 * Math.random()) | ||
| }}, function () { | ||
| ;print('get', {url: base + 'product/2', headers: headers}, function () { | ||
| ;print('del', {url: base + 'product/1', headers: headers}, function () { | ||
| ;print('get', {url: base + 'product/1', headers: headers}, function () { | ||
| ;print('get', {url: base + 'product/0/star', headers: headers}, function () { | ||
| ;print('put', {url: base + 'product/0/star', headers: headers}, function () { | ||
| ;print('get', {url: base + 'product/0/star', headers: headers}, function () { | ||
| }) }) }) }) }) }) }) }) }) }) }) }) }) }) | ||
|
|
||
| // utility function to pretty print request results | ||
| function print () { | ||
| var a = Array.prototype.slice.call(arguments) | ||
| var mstr = a[0] | ||
|
|
||
| var n = _.isFunction( a[a.length-1] ) ? a.pop() : null | ||
|
|
||
| request[mstr].apply(request, a.slice(1).concat(function(err,res,body){ | ||
| console.log(mstr,res.req.path,res.statusCode,err||body) | ||
| var n = _.isFunction(a[ a.length - 1 ]) ? a.pop() : null | ||
| Request[mstr].apply(Request, a.slice(1).concat(function (err, res, body) { | ||
| console.log(mstr, res.req.path, res.statusCode, err || body) | ||
| !err && n && n() | ||
| })) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
broken link