Create api.moon

A basic helper for writing JSON-based APIs
This commit is contained in:
Paul Liverman III 2018-04-26 13:18:48 -07:00 committed by GitHub
parent e918bbe5f1
commit c6fcbdf566
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

29
api.moon Normal file
View File

@ -0,0 +1,29 @@
import json_params, capture_errors, yield_error from require "lapis.application"
import insert from table
import max from math
request = (fn) ->
json_params capture_errors {
fn,
on_error: =>
status = 400
errors = {}
for err in *@errors
if "table" == type err
status = max status, err[1]
insert errors, err[2]
else
insert errors, err
return(:status, json: { :errors })
}
abort = (status, message) ->
if message
yield_error {status, message}
else
yield_error status
{
:request
:abort
}