Files
standardnotes-app-web/packages/snjs/lib/Services/Challenge/ChallengeResponse.ts
Karol Sójko b2faa815e9 feat: add sending user requests to process (#1908)
* feat: add sending user requests to process

* fix(snjs): yarn lock

* fix(snjs): imports

* fix: specs
2022-11-02 11:33:02 +01:00

34 lines
953 B
TypeScript

import { isNullOrUndefined } from '@standardnotes/utils'
import {
Challenge,
ChallengeResponseInterface,
ChallengeValidation,
ChallengeValue,
ChallengeArtifacts,
} from '@standardnotes/services'
export class ChallengeResponse implements ChallengeResponseInterface {
constructor(
public readonly challenge: Challenge,
public readonly values: ChallengeValue[],
public readonly artifacts?: ChallengeArtifacts,
) {
Object.freeze(this)
}
getValueForType(type: ChallengeValidation): ChallengeValue {
const value = this.values.find((value) => value.prompt.validation === type)
if (isNullOrUndefined(value)) {
throw Error('Could not find value for validation type ' + type)
}
return value
}
getDefaultValue(): ChallengeValue {
if (this.values.length > 1) {
throw Error('Attempting to retrieve default response value when more than one value exists')
}
return this.values[0]
}
}