Skip to content

Commit

Permalink
Update variable naming for ConditionContext to clarify that they are …
Browse files Browse the repository at this point in the history
…related to context parameters.
  • Loading branch information
derekpierre committed Aug 16, 2024
1 parent 81d9006 commit e872fff
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
35 changes: 18 additions & 17 deletions packages/taco/src/conditions/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ export const RESERVED_CONTEXT_PARAMS = [
];

export class ConditionContext {
public requestedParameters: Set<string>;
private customParameters: Record<string, CustomContextParam> = {};
public requestedContextParameters: Set<string>;
private customContextParameters: Record<string, CustomContextParam> = {};
private authProviders: Record<string, AuthProvider> = {};

constructor(condition: Condition) {
const condProps = condition.toObj();
ConditionContext.validateCoreConditions(condProps);
this.requestedParameters =
this.requestedContextParameters =
ConditionContext.findContextParameters(condProps);
}

Expand All @@ -73,27 +73,28 @@ export class ConditionContext {
) {
// Ok, so at this point we should have all the parameters we need
// If we don't, we have a problem and we should throw
const missingParameters = Array.from(this.requestedParameters).filter(
(key) => parameters[key] === undefined,
);
const missingParameters = Array.from(
this.requestedContextParameters,
).filter((key) => parameters[key] === undefined);
if (missingParameters.length > 0) {
throw new Error(ERR_MISSING_CONTEXT_PARAMS(missingParameters));
}
}

private async fillContextParameters(
requestedParameters: Set<string>,
requestedContextParameters: Set<string>,
): Promise<Record<string, ContextParam>> {
const parameters =
await this.fillAuthContextParameters(requestedParameters);
for (const key in this.customParameters) {
parameters[key] = this.customParameters[key];
const parameters = await this.fillAuthContextParameters(
requestedContextParameters,
);
for (const key in this.customContextParameters) {
parameters[key] = this.customContextParameters[key];
}
return parameters;
}

private validateAuthProviders(): void {
for (const param of this.requestedParameters) {
for (const param of this.requestedContextParameters) {
// If it's not a user address parameter, we can skip
if (!USER_ADDRESS_PARAMS.includes(param)) {
continue;
Expand Down Expand Up @@ -131,7 +132,7 @@ export class ConditionContext {
throw new Error(ERR_RESERVED_PARAM(customParam));
}

if (!this.requestedParameters.has(customParam)) {
if (!this.requestedContextParameters.has(customParam)) {
throw new Error(ERR_UNKNOWN_CUSTOM_CONTEXT_PARAM(customParam));
}
}
Expand Down Expand Up @@ -182,11 +183,11 @@ export class ConditionContext {
}

public addCustomContextParameterValues(
customParameters: Record<string, CustomContextParam>,
customContextParameters: Record<string, CustomContextParam>,
) {
Object.keys(customParameters).forEach((key) => {
Object.keys(customContextParameters).forEach((key) => {
this.validateCustomContextParameter(key);
this.customParameters[key] = customParameters[key];
this.customContextParameters[key] = customContextParameters[key];
});
}

Expand Down Expand Up @@ -221,7 +222,7 @@ export class ConditionContext {
> => {
this.validateAuthProviders();
const parameters = await this.fillContextParameters(
this.requestedParameters,
this.requestedContextParameters,
);
this.validateNoMissingContextParameters(parameters);
return parameters;
Expand Down
14 changes: 9 additions & 5 deletions packages/taco/test/conditions/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ describe('context', () => {
describe('custom parameters', () => {
it('detects when a custom parameter is requested', () => {
const conditionContext = new ConditionContext(contractCondition);
expect(conditionContext.requestedParameters).toContain(customParamKey);
expect(conditionContext.requestedContextParameters).toContain(
customParamKey,
);
});

it('serializes bytes as hex strings', async () => {
Expand Down Expand Up @@ -232,10 +234,12 @@ describe('context', () => {
};

it('handles both custom and auth context parameters', () => {
const requestedParams = new ConditionContext(contractCondition)
.requestedParameters;
expect(requestedParams).not.toContain(USER_ADDRESS_PARAM_DEFAULT);
expect(requestedParams).toContain(customParamKey);
const requestedContextParams = new ConditionContext(contractCondition)
.requestedContextParameters;
expect(requestedContextParams).not.toContain(
USER_ADDRESS_PARAM_DEFAULT,
);
expect(requestedContextParams).toContain(customParamKey);
});

it('rejects on a missing custom parameter ', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/taco/test/taco.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe('taco', () => {
expect(getFinalizedRitualSpy).toHaveBeenCalled();

const conditionContext = ConditionContext.fromMessageKit(messageKit);
const requestedParameters = conditionContext.requestedParameters;
const requestedParameters = conditionContext.requestedContextParameters;
expect(requestedParameters).toEqual(
new Set([customParamKey, USER_ADDRESS_PARAM_DEFAULT]),
);
Expand Down

0 comments on commit e872fff

Please sign in to comment.