Implement WebResult
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
.idea/
|
||||
._*
|
||||
.DS_Store
|
||||
bun.lockb
|
||||
package-lock.json
|
||||
node_modules/
|
||||
@@ -0,0 +1,26 @@
|
||||
import {expect, test} from 'bun:test'
|
||||
|
||||
import {WebResult} from './index'
|
||||
|
||||
test('base case Ok', () => {
|
||||
const value = 'test'
|
||||
|
||||
const res = WebResult.ok(value)
|
||||
|
||||
const handler = (code: number) => {
|
||||
throw "Shouldn't happen"
|
||||
}
|
||||
expect(res.as_response(handler)).toBe(value)
|
||||
})
|
||||
|
||||
test('base case Error', () => {
|
||||
const value = 12
|
||||
|
||||
const res = WebResult.error<never>(value)
|
||||
|
||||
const handler = (code: number) => {
|
||||
expect(code).toBe(value)
|
||||
return 'OK'
|
||||
}
|
||||
expect(res.as_response(handler)).toBe('OK')
|
||||
})
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "webresult",
|
||||
"description": "Extension of Result with a web specific error type",
|
||||
"version": "1.0.0",
|
||||
|
||||
"author": "Pascal Perrenoud <pascal@pband.ch>",
|
||||
|
||||
"module": "index.ts",
|
||||
"type": "module",
|
||||
"files": ["index.ts"],
|
||||
|
||||
"scripts": {
|
||||
"test": "bun test"
|
||||
},
|
||||
|
||||
"dependencies": {
|
||||
"result": "git+https://git.pband.ch/typescript/result.git"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bun": "^1.1.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"typescript": "^5.0.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
// Enable latest features
|
||||
"lib": ["ESNext"],
|
||||
"target": "ESNext",
|
||||
"module": "ESNext",
|
||||
"moduleDetection": "force",
|
||||
"allowJs": true,
|
||||
"checkJs": true,
|
||||
|
||||
// Bundler mode
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
"noEmit": true,
|
||||
|
||||
// Best practices
|
||||
"strict": true,
|
||||
"skipLibCheck": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
|
||||
// Some stricter flags (disabled by default)
|
||||
"noUnusedLocals": false,
|
||||
"noUnusedParameters": false,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"noPropertyAccessFromIndexSignature": false
|
||||
},
|
||||
|
||||
"include": [
|
||||
"index.ts"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user