An extremely simple user-authentication service.

helpers.moon 821B

123456789101112131415161718192021222324252627282930313233343536
  1. import json_params, capture_errors, yield_error, respond_to from require "lapis.application"
  2. import insert from table
  3. import max from math
  4. api = (fn) =>
  5. json_params capture_errors {
  6. =>
  7. result = fn(@)
  8. return json: result
  9. on_error: =>
  10. status = 400 -- most likely a bad request
  11. errors = {}
  12. for err in *@errors
  13. if "table" == type err
  14. status = max status, err[1] -- the worst error will have a higher status number
  15. insert errors, err[2]
  16. else
  17. insert errors, err
  18. return(:status, json: { success: false, :errors })
  19. }
  20. abort = (status, message) ->
  21. if message
  22. yield_error {status, message}
  23. else
  24. yield_error status
  25. assert_model = (result, err) ->
  26. abort 500, err if err
  27. return result
  28. {
  29. :api
  30. :abort
  31. :assert_model
  32. }