@nucypher/taco
Version:
### [`nucypher/taco-web`](../../README.md)
49 lines • 1.56 kB
JavaScript
import { objectEquals } from '@nucypher/shared';
import { toJSON } from '../utils';
import { USER_ADDRESS_PARAMS } from './const';
export { baseConditionSchema } from './schemas/common';
const ERR_INVALID_CONDITION = (error) => `Invalid condition: ${JSON.stringify(error.issues)}`;
export class Condition {
schema;
value;
constructor(schema, value) {
this.schema = schema;
this.value = value;
const { data, error } = Condition.validate(schema, value);
if (error) {
throw new Error(ERR_INVALID_CONDITION(error));
}
this.value = data;
}
static validate(schema, value) {
const result = schema.safeParse(value);
if (result.success) {
return { data: result.data };
}
return { error: result.error };
}
// TODO: Fix this method and add a test for it
findParamWithAuthentication() {
const serialized = toJSON(this.value);
for (const param of USER_ADDRESS_PARAMS) {
if (serialized.includes(param)) {
return param;
}
}
return null;
}
requiresAuthentication() {
return Boolean(this.findParamWithAuthentication());
}
toObj() {
const { data, error } = Condition.validate(this.schema, this.value);
if (error) {
throw new Error(ERR_INVALID_CONDITION(error));
}
return data;
}
equals(other) {
return objectEquals(this.toObj(), other.toObj());
}
}
//# sourceMappingURL=condition.js.map