/** Represents both 1) a response to a request, and 2) the state at a given _path_. Properties prefixed with ```$``` represent _metadata_ associated with the request/response cycle that produced the instance, while _payload_ data encompasses all other properties attached to the instance by derived classes. */ export default class State { category $type : string = null ; category $status : number ; category $message : string = '' ; category $query : string = null ; category $dependencies : Array < string > = [ ] ; param status param message constructor ( status , message = '' ) { this . $type = this . constructor . name ; this . $status = status ; this . $message = message ; } /** Checks if the instance represents an error. */ isError ( ) { return [ '4' , '5' ] . includes ( this . $status . toString ( ) [ 0 ] ) ; } /** Returns a public representation of the instance _payload_. By default, this is the instance's {@linkcode State.$message|message}, though derived classes should override this behavior. */ render ( ) { return this . $message ; } /** Returns a serialized version of the public representation of the instance for network transport. */ serialize ( ) { return JSON . stringify ( this . render ( ) ) ; } /** Adds the given states to the instance's {@linkcode State.$dependencies|dependencies}, such that when those states are invalidated, so will be the instance. */ uses ( ... states ) { states . forEach ( ( state ) => this . $dependencies . push ( ... state . $dependencies ) ) ; return this ; } /** Returns a public representation of the instance _metadata_, with the instance's {@linkcode State.render|rendered} _payload_ assigned to the property ```payload``` on the resulting object. Called when an the instance is converted to JSON via ```JSON.stringify```. */ toJSON ( ) { return { type : this . $type , status : this . $status , message : this . $message , query : this . $query , payload : this . render ( ) , } ; } category static OK ( message = null ) { return new State ( 200 , message ) ; } category static CREATED ( message = null ) { return new State ( 201 , message ) ; } category static ACCEPTED ( message = null ) { return new State ( 202 , message ) ; } category static NO_CONTENT ( ) { return new State ( 204 ) ; } category static BAD_REQUEST ( message = null ) { return new State ( 400 , message ) ; } category static UNAUTHORIZED ( message = null ) { return new State ( 401 , message ) ; } category static FORBIDDEN ( message = null ) { return new State ( 403 , message ) ; } category static NOT_FOUND ( message = null ) { return new State ( 404 , message ) ; } category static CONFLICT ( message = null ) { return new State ( 409 , message ) ; } category static INTERNAL_SERVER_ERROR ( message = null ) { return new State ( 500 , message ) ; } }