• Jump To … +
    Nodulator.ls ClientDB.ls index.ls Resource.ls Bus.ls Cache.ls ChangeWatcher.ls Debug.ls Wrappers.ls Nodulator.ls Resource.ls Schema.ls NModule.ls AccountResource.ls User.ls index.ls Nodulator-Angular.ls TaskDirective.ls index.ls TaskService.ls main.ls index.ls index.ls Task.ls index.ls index.ls index.ls index.ls index.ls DOM.ls View.ls index.ls Nodulator.ls Mongo.ls Mysql.ls SqlMem.ls index.ls Resource.ls Request.ls Route.ls index.ls
  • Request.ls

  • ¶
    require! \prelude-ls : {is-type, map}
    
    errors =
      not_found: 404
      forbidden: 403
      'Error on Delete': 500
    
    class Request
    
      instance: null
      sent: false
    
      ([@req, @res, @next]) ->
        @_Parse!
    
      Send: !->
        return if @sent
    
        if it is null
          @sent = true
          return @res.status(404).send "Cannot #{@method.toUpperCase!} #{@url}"
    
        if is-type 'Array' it
          it := map (-> if it.ToJSON? => it.ToJSON! else it), it
    
        if it.ToJSON? => @res.status(200).send it.ToJSON!
        else          => @res.status(200).send it
    
        @sent = true
    
      SendError: ->
        return if @sent
    
        status = it.code || errors[it.status] || 500
    
        it = that if it.message?
    
        @res.status(status).send err: it
        @sent = true
    
      SetInstance: !->
        if it._promise?
          it
            .Then ~>
              @req._instance = it
              @instance = it
              @next()
            .Catch ~> @SendError it
        else
          @req._instance = it
          @instance = it
          @next()
    
      _Parse: ->
        import @req
        @instance = @_instance if @_instance?
    
    Request.errors = errors
    module.exports = Request