Auto API Middleware
Do you want all / a subset of the methods on your backend classes/objects/models/some {}
to be fetch
-able or $.ajax
-able on the front without having to write explicit express routes?
Then add them to your router like so:
.all("/json(/:model)?(/:verb)?", require("auto-api-middleware")({source:Model}) )
Example
Say a Model
was passed in that looked like this:
var Model={
book:{
getPage(num){}
,getWordsByFrequency(isbn){}
,fetchCover(isbn){}
}
,scheduler:{
setAppointment(){}
,getEventsInRange(start,end){}
}
,user:{
get(key){}
,update(key,changes){}
,columns:{name:'',created:'',password:'',email:''}
}
}
That would create these JSON routes:
/json/book/getPage
/json/book/getWordsByFrequency
/json/book/fetchCover
/json/scheduler/setAppointment
/json/scheduler/getEventsInRange
/json/user/get
/json/user/update
/json/user/columns
All routes for functions with named inputs would require them as URL query parameters like so:
$.ajax({url:"/json/scheduler/getEventsInRange",data:{start:"Tuesday",end:"Friday"}})
fetch("/json/scheduler/getEventsInRange?start=Tuesday&end=Friday")
These flow types are understood for functions:
- (async) If a function signature has an input that is named in
callbackNames
(see options below), it will use that - (promise) If a function returns a promise, it will send the resolution
- (sync) If a function returns anything else, that value is sent
Now, maybe you:
- don't want user.update to be available
- or want to restrict it to only be in the scope of the session's user
- or want to only take methods containing certain words
The options below allow that.
Options
.all("/json(/:model)?(/:verb)?", require("auto-api-middleware")({
source:{}
,beHelpful:false
,filter(key,val){
return !(key[0]=='_' || val.priv || val.private)
}
,methodMap(key,value){
return _.castArray(
(key.match(/^(get|gather|find|fetch)/gi) || !_.isFunction(value) ? "GET"
: key.match(/POST|GET|PUT|PATCH|DELETE/i)
) || "POST"
).first()
}
,before(req,res){}
,after(req,res,returned){}
,callbackNames:["done","next","then","cb","callback"]
}) )