Error response enhancements, CoreID auth client backend

This commit is contained in:
2022-03-29 01:14:46 -05:00
parent a039b1ff25
commit 8f08b94f74
31 changed files with 736 additions and 52 deletions

View File

@@ -1,5 +1,7 @@
import {Integer, isInteger} from './types'
import {ErrorWithContext} from '../error/ErrorWithContext'
import {JSONState} from './Rehydratable'
import {isJSON} from './data'
export class Safe {
protected thrower: (message: string, value: unknown) => never
@@ -22,10 +24,10 @@ export class Safe {
present(): this {
if ( !this.value && this.value !== 0 && this.value !== false ) {
return this
this.thrower('Missing value', this.value)
}
this.thrower('Missing value', this.value)
return this
}
integer(): Integer {
@@ -51,6 +53,23 @@ export class Safe {
return String(this.value)
}
json(): JSONState {
const str = this.string()
if ( !isJSON(str) ) {
this.thrower('Invalid JSON', str)
}
return JSON.parse(str)
}
or(other: unknown): Safe {
if ( !this.value && this.value !== 0 && this.value !== false ) {
return new Safe(other)
}
return this
}
in<T>(allowed: T[]): T {
if ( allowed.includes(this.value as any) ) {
return this.value as T