20 lines
503 B
JavaScript
20 lines
503 B
JavaScript
class FetchResult {
|
|
constructor(code, msg, data = null) {
|
|
if (typeof code !== 'number') {
|
|
throw new Error('Code must be a number')
|
|
}
|
|
if (typeof msg !== 'string') {
|
|
throw new Error('Msg must be a string')
|
|
}
|
|
this.code = code
|
|
this.msg = msg
|
|
this.data = data !== undefined && data !== null ? data : null
|
|
}
|
|
|
|
static formatResult(res, code, msg, data = null) {
|
|
return res.status(code).json(new FetchResult(code, msg, data))
|
|
}
|
|
}
|
|
|
|
module.exports = FetchResult
|