From c6fcbdf56691af45f0ce71e71416c9045ea25b53 Mon Sep 17 00:00:00 2001 From: Paul Liverman III Date: Thu, 26 Apr 2018 13:18:48 -0700 Subject: [PATCH] Create api.moon A basic helper for writing JSON-based APIs --- api.moon | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 api.moon diff --git a/api.moon b/api.moon new file mode 100644 index 0000000..ebbee40 --- /dev/null +++ b/api.moon @@ -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 +}