import { InvalidVerifyResultError , InvalidVerifyResultJSONError } from '../errors' ; import { VerifyResultJSON } from './types' ; export enum VerifyResultModel { Succeed , AlreadyExists , OutOfMemory , UnableToVerify , Invalid , Expired , InsufficientFunds , PolicyFail , Unknown } export interface VerifyResultModelExtended { readonly verifyResult ? : VerifyResultModel ; readonly failureReason ? : string ; } export const hasVerifyResult = ( reason , flag ) : boolean => ( reason & flag ) === flag ; export const isVerifyResult = ( value ) : value is VerifyResultModel => VerifyResultModel [ value ] !== undefined ; export const assertVerifyResult = ( value ) : VerifyResultModel => { if ( ! isVerifyResult ( value ) ) { throw new InvalidVerifyResultError ( value ) ; } return value ; } ; export const toVerifyResultJSON = ( reason ) : VerifyResultJSON => assertVerifyResultJSON ( VerifyResultModel [ reason ] ) ; export const isVerifyResultJSON = ( reason ) : reason is VerifyResultJSON => VerifyResultModel [ reason as any ] !== undefined ; export const assertVerifyResultJSON = ( reason ) : VerifyResultJSON => { if ( ! isVerifyResultJSON ( reason ) ) { throw new InvalidVerifyResultJSONError ( reason ) ; } return reason ; } ; export const toVerifyResult = ( reason ) : VerifyResultModel => VerifyResultModel [ reason ] ;