mares-app-service
Version:
슬로그업 ddd 패턴의 application layer에서 사용되는 base class
83 lines (75 loc) • 1.42 kB
JavaScript
/**
* App Service Super Class
*/
class MaresAppService {
/**
* @constructor
* @param {string} appKey - app key
*/
constructor(appKey) {
this.appKey = appKey
}
/**
* send 204 response
* @returns {{status: number}}
*/
static sendNoContents() {
return {
status: 204
}
}
/**
* normalize return value for object
* @param {number} status - status code
* @param {Object} [obj] - returning object
* @param {Object} [others = null] - other returning object
* @returns {Object}
*/
static sendObject(status, obj, others = null) {
if (!obj) {
return {
status
}
}
let body = Object.assign({
row: obj
}, others || {})
return {
status,
body
}
}
/**
* normalize return error for mares error
* @param {MaresError} e - Mares Error
* @returns {Object}
*/
static sendError(e) {
let error = {}
if (e.getError) {
error = e.getError()
} else {
throw e
}
return error
}
/**
* normalize return value for array
* @param {number} status - status code
* @param {Object} array - returning array
* @param {number} count - returning count
* @param {Object} [others = null] - other returning object
* @returns {Object}
*/
static sendArray(status, array, count, others = null) {
let body = Object.assign({
rows: array,
count: count
}, others || {})
return {
status,
body
}
}
}
module.exports = MaresAppService