From 3cf4d36c95a710d2a0ccb4e590e3cb356679540e Mon Sep 17 00:00:00 2001 From: Pascal Perrenoud Date: Mon, 20 May 2024 01:06:07 +0200 Subject: [PATCH] Implement WebResult --- .gitignore | 6 ++++++ index.test.ts | 26 ++++++++++++++++++++++++++ index.ts | 29 +++++++++++++++++++++++++++++ package.json | 25 +++++++++++++++++++++++++ tsconfig.json | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 118 insertions(+) create mode 100644 .gitignore create mode 100644 index.test.ts create mode 100644 index.ts create mode 100644 package.json create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f39fcb6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.idea/ +._* +.DS_Store +bun.lockb +package-lock.json +node_modules/ diff --git a/index.test.ts b/index.test.ts new file mode 100644 index 0000000..561ec87 --- /dev/null +++ b/index.test.ts @@ -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(value) + + const handler = (code: number) => { + expect(code).toBe(value) + return 'OK' + } + expect(res.as_response(handler)).toBe('OK') +}) diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..fa2a114 --- /dev/null +++ b/index.ts @@ -0,0 +1,29 @@ +import {Result, State} from 'result' +export {State} from 'result' + +// @ts-expect-error +export class WebResult extends Result { + public constructor( + _data: T | number, + _state: State, + ) { + super(_data, _state) + } + + public static ok(data: T) : WebResult { + return new WebResult(data, State.OK) + } + public static error(code: number) : WebResult { + return new WebResult(code, State.ERROR) + } + + public as_response(error: (code: number) => V) : T | V { + if (this.state() === State.OK) { + // @ts-expect-error + return this._data + } + + // @ts-expect-error + return error(this._data) + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..8d2b1a9 --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "webresult", + "description": "Extension of Result with a web specific error type", + "version": "1.0.0", + + "author": "Pascal Perrenoud ", + + "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" + } +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..7e4818b --- /dev/null +++ b/tsconfig.json @@ -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" + ] +}