mirror of
https://github.com/Wessel/kirbe.git
synced 2026-07-27 02:20:55 +02:00
Upload main code
This commit is contained in:
50
model/KirbeResponse.js
Normal file
50
model/KirbeResponse.js
Normal file
@@ -0,0 +1,50 @@
|
||||
module.exports = class KirbeResponse {
|
||||
constructor( res ) {
|
||||
this.coreRes = res;
|
||||
this.headers = {};
|
||||
|
||||
this.statusCode = 200;
|
||||
this.statusMessage = null;
|
||||
|
||||
this.data = Buffer.alloc( 0 );
|
||||
}
|
||||
|
||||
body( body ) {
|
||||
if ( typeof body === 'object' && !Buffer.isBuffer( body ) ) {
|
||||
if ( !this.headers[ 'content-type' ] ) this.headers[ 'content-type' ] = 'application/json';
|
||||
|
||||
this.data = JSON.stringify( body );
|
||||
} else this.data = body;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
header( a, b ) {
|
||||
if ( typeof a === 'object' ) Object.keys( a ).forEach( ( v ) => this.headers[ v.toLowerCase() ] = a[ v ] );
|
||||
else this.headers[ a.toLowerCase() ] = b;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
status( code, message ) {
|
||||
this.statusCode = code;
|
||||
this.statusMessage = message;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
writeHead( status, headers ) {
|
||||
if ( status ) this.status( status );
|
||||
if ( headers ) this.header( headers );
|
||||
}
|
||||
|
||||
end( data ) {
|
||||
if ( data ) this.body( data );
|
||||
|
||||
if (this.statusMessage) this.coreRes.writeHead( this.statusCode, this.statusMessage, this.headers );
|
||||
else this.coreRes.writeHead( this.statusCode, this.headers );
|
||||
this.coreRes.end( this.data );
|
||||
|
||||
return this;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user