Implement WebResult
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import {Result, State} from 'result'
|
||||
export {State} from 'result'
|
||||
|
||||
// @ts-expect-error
|
||||
export class WebResult<T> extends Result<T, number> {
|
||||
public constructor(
|
||||
_data: T | number,
|
||||
_state: State,
|
||||
) {
|
||||
super(_data, _state)
|
||||
}
|
||||
|
||||
public static ok<T, U>(data: T) : WebResult<T> {
|
||||
return new WebResult<T>(data, State.OK)
|
||||
}
|
||||
public static error<T>(code: number) : WebResult<T> {
|
||||
return new WebResult<T>(code, State.ERROR)
|
||||
}
|
||||
|
||||
public as_response<V>(error: (code: number) => V) : T | V {
|
||||
if (this.state() === State.OK) {
|
||||
// @ts-expect-error
|
||||
return this._data
|
||||
}
|
||||
|
||||
// @ts-expect-error
|
||||
return error(this._data)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user