mirror of
https://github.com/lazuscripts/utility.git
synced 2024-11-16 03:14:21 +00:00
Create api.moon
A basic helper for writing JSON-based APIs
This commit is contained in:
parent
e918bbe5f1
commit
c6fcbdf566
29
api.moon
Normal file
29
api.moon
Normal 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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user