Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristina Matuleviciute committed Jun 1, 2016
1 parent 61ff93c commit 4010d45
Show file tree
Hide file tree
Showing 40 changed files with 486 additions and 533 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extra testing, or new features please get in touch. - Before contributing please

## License

Copyright Richard Rodger and other contributors 2013 - 2016, Licensed under [MIT](./LICENSE).
Copyright Richard Rodger and other contributors 2013 - 2016, Licensed under [MIT](./LICENSE.txt).



4 changes: 4 additions & 0 deletions api-server/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": ["seneca"]
}

165 changes: 75 additions & 90 deletions api-server/api.js
Original file line number Diff line number Diff line change
@@ -1,114 +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:*',
startware: verify_token,
map:{
star: {
alias:'/:id/star'
},
handle_star:{
PUT:true,
DELETE:true,
alias:'/:id/star'
}
seneca.act('role:web', {use: {
prefix: '/product',
pin: 'role:api,product:*',
startware: verify_token,
map: {
star: {
alias: '/:id/star'
},
handle_star: {
PUT: true,
DELETE: true,
alias: '/:id/star'
}
}})

done()
})


var internal_err = {
http$: {status:500},
why: 'Internal error.'
}

var bad_input = {
http$: {status:400},
why: 'Bad input.'
}


function verify_token(req,res,next) {
var token = req.headers['api-access-token']

}
}})

done()
})

var internal_err = {
http$: {status: 500},
why: 'Internal error.'
}

var bad_input = {
http$: {status: 400},
why: 'Bad input.'
}

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.")
res.end()
}


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,{
product: res.id,
star: res.star
})
if('1234' === token) return next()
res.writeHead(401, 'Access denied.')
res.end()
}

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, {
product: res.id,
star: res.star
})
}

function handle_star( args, done ) {
this.act(
})
}

function handle_star (args, done) {
this.act(
'role:product,cmd:star',
{
// product id
id:args.id,

// PUT means star, DELETE means unstar
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 id
id: args.id,
// PUT means star, DELETE means unstar
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
star: res.star
})
}
)
}
}
}
14 changes: 6 additions & 8 deletions api-server/app.js
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)

74 changes: 31 additions & 43 deletions api-server/client.js
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()
}))
}
8 changes: 7 additions & 1 deletion api-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
"express": "~4.13.4",
"lodash": "~4.13.1",
"request": "~2.72.0",
"seneca": "2.1.0",
"seneca": "0.6.2",
"seneca-entity": "0.1.1",
"seneca-jsonrest-api": "~0.3.1"
},
"devDependencies": {
"eslint-config-seneca": "2.x.x",
"eslint-plugin-standard": "1.x.x",
"eslint-plugin-hapi": "4.x.x"
}
}
40 changes: 17 additions & 23 deletions api-server/product_catalog.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
// PUBLIC DOMAIN
"use strict";
'use strict'

module.exports = function product_catalog( options ) {
module.exports = function product_catalog (options) {
var seneca = this

seneca.add('role:product,cmd:star', cmd_star)


function cmd_star( args, done ) {
function cmd_star (args, done) {
var seneca = this

seneca
.make$('product')
.load$(args.id,function( err, product ) {
if(err) return done(err);

if( product ) {
product.star = Math.max(0,(
(product.star||0) +
((args.star&&true) ? +1 : -1 )))

product.save$(function(err,product){
if(err) return done(err);

return done(null,{
ok: true,
id: product.id,
.load$(args.id, function (err, product) {
if(err) return done(err)
if(product) {
product.star = Math.max(0, (
(product.star || 0) +
((args.star && true) ? +1 : -1)))

product.save$(function (err, product) {
if(err) return done(err)

return done(null, {
ok: true,
id: product.id,
star: product.star
})
})
}
else return done(null,{ok:false})
else return done(null, {ok: false})
})
}

}
4 changes: 2 additions & 2 deletions api-server/seneca.options.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
'mem-store': { web: { dump:true } },
web: {debug: { service:true } },
'mem-store': {web: {dump: true}},
'web': {debug: {service: true}}
}
4 changes: 4 additions & 0 deletions data-entities/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": ["seneca"]
}

Loading

0 comments on commit 4010d45

Please sign in to comment.