Skip to content

Commit

Permalink
fix(types): adding this types in public functions in classes
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed Oct 30, 2020
1 parent d278045 commit f182fd9
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 109 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# iam-policies

> [![NPM](https://img.shields.io/npm/v/iam-policies.svg)](https://www.npmjs.com/package/iam-policies) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) [![Build Status](https://travis-ci.com/roggervalf/iam-policies.svg?branch=master)](https://travis-ci.com/github/roggervalf/iam-policies) [![NPM downloads](https://img.shields.io/npm/dm/iam-policies)](https://www.npmjs.com/package/iam-policies) [![Coverage Status](https://coveralls.io/repos/github/roggervalf/iam-policies/badge.svg?branch=master)](https://coveralls.io/github/roggervalf/iam-policies?branch=master) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
> [![NPM](https://img.shields.io/npm/v/iam-policies.svg)](https://www.npmjs.com/package/iam-policies) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier) [![Build Status](https://travis-ci.com/roggervalf/iam-policies.svg?branch=master)](https://travis-ci.com/github/roggervalf/iam-policies) [![NPM downloads](https://img.shields.io/npm/dm/iam-policies)](https://www.npmjs.com/package/iam-policies) [![Coverage Status](https://coveralls.io/repos/github/roggervalf/iam-policies/badge.svg?branch=master)](https://coveralls.io/github/roggervalf/iam-policies?branch=master) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
## About

Expand Down
48 changes: 24 additions & 24 deletions dist/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,16 @@ declare class Statement {
protected readonly condition?: ConditionBlock;
effect: EffectBlock;
constructor({ sid, effect, condition }: StatementInterface);
matchConditions({ context, conditionResolver }: MatchConditionInterface): boolean;
matchConditions(this: Statement, { context, conditionResolver }: MatchConditionInterface): boolean;
}

declare class ActionBased extends Statement {
private action?;
private notAction?;
private statement;
constructor(action: ActionBasedType);
getStatement(): ActionBasedType;
matches({ action, context, conditionResolver }: MatchActionBasedInterface): boolean;
getStatement(this: ActionBased): ActionBasedType;
matches(this: ActionBased, { action, context, conditionResolver }: MatchActionBasedInterface): boolean;
private checkAndAssignActions;
private matchActions;
private matchNotActions;
Expand All @@ -158,8 +158,8 @@ declare class IdentityBased extends Statement {
private notAction?;
private statement;
constructor(identity: IdentityBasedType);
getStatement(): IdentityBasedType;
matches({ action, resource, context, conditionResolver }: MatchIdentityBasedInterface): boolean;
getStatement(this: IdentityBased): IdentityBasedType;
matches(this: IdentityBased, { action, resource, context, conditionResolver }: MatchIdentityBasedInterface): boolean;
private checkAndAssignActions;
private checkAndAssignResources;
private matchActions;
Expand All @@ -179,8 +179,8 @@ declare class ResourceBased extends Statement {
private hasPrincipals;
private hasResources;
constructor(identity: ResourceBasedType);
getStatement(): ResourceBasedType;
matches({ principal, action, resource, principalType, context, conditionResolver }: MatchResourceBasedInterface): boolean;
getStatement(this: ResourceBased): ResourceBasedType;
matches(this: ResourceBased, { principal, action, resource, principalType, context, conditionResolver }: MatchResourceBasedInterface): boolean;
private matchPrincipalAndNotPrincipal;
private matchResourceAndNotResource;
private checkAndAssignActions;
Expand All @@ -198,10 +198,10 @@ declare class Policy {
protected context?: Context;
protected conditionResolver?: ConditionResolver;
constructor({ context, conditionResolver }: MatchConditionInterface);
setContext(context: Context): void;
getContext(): Context | undefined;
setConditionResolver(conditionResolver: ConditionResolver): void;
getConditionResolver(): ConditionResolver | undefined;
setContext(this: Policy, context: Context): void;
getContext(this: Policy): Context | undefined;
setConditionResolver(this: Policy, conditionResolver: ConditionResolver): void;
getConditionResolver(this: Policy): ConditionResolver | undefined;
}

interface ActionBasedPolicyInterface {
Expand All @@ -214,11 +214,11 @@ declare class ActionBasedPolicy extends Policy {
private allowStatements;
private statements;
constructor({ statements, conditionResolver, context }: ActionBasedPolicyInterface);
getStatements(): ActionBasedType[];
evaluate({ action, context }: EvaluateActionBasedInterface): boolean;
can({ action, context }: EvaluateActionBasedInterface): boolean;
cannot({ action, context }: EvaluateActionBasedInterface): boolean;
generateProxy<T, U extends keyof T>(obj: unknown, options?: ProxyOptions): T | undefined;
getStatements(this: ActionBasedPolicy): ActionBasedType[];
evaluate(this: ActionBasedPolicy, { action, context }: EvaluateActionBasedInterface): boolean;
can(this: ActionBasedPolicy, { action, context }: EvaluateActionBasedInterface): boolean;
cannot(this: ActionBasedPolicy, { action, context }: EvaluateActionBasedInterface): boolean;
generateProxy<T, U extends keyof T>(this: ActionBasedPolicy, obj: unknown, options?: ProxyOptions): T | undefined;
}

interface IdentityBasedPolicyInterface {
Expand All @@ -231,10 +231,10 @@ declare class IdentityBasedPolicy extends Policy {
private allowStatements;
private statements;
constructor({ statements, conditionResolver, context }: IdentityBasedPolicyInterface);
getStatements(): IdentityBasedType[];
evaluate({ action, resource, context }: EvaluateIdentityBasedInterface): boolean;
can({ action, resource, context }: EvaluateIdentityBasedInterface): boolean;
cannot({ action, resource, context }: EvaluateIdentityBasedInterface): boolean;
getStatements(this: IdentityBasedPolicy): IdentityBasedType[];
evaluate(this: IdentityBasedPolicy, { action, resource, context }: EvaluateIdentityBasedInterface): boolean;
can(this: IdentityBasedPolicy, { action, resource, context }: EvaluateIdentityBasedInterface): boolean;
cannot(this: IdentityBasedPolicy, { action, resource, context }: EvaluateIdentityBasedInterface): boolean;
}

interface ResourceBasedPolicyInterface {
Expand All @@ -247,10 +247,10 @@ declare class ResourceBasedPolicy extends Policy {
private allowStatements;
private statements;
constructor({ statements, conditionResolver, context }: ResourceBasedPolicyInterface);
getStatements(): ResourceBasedType[];
evaluate({ principal, action, resource, principalType, context }: EvaluateResourceBasedInterface): boolean;
can({ principal, action, resource, principalType, context }: EvaluateResourceBasedInterface): boolean;
cannot({ principal, action, resource, principalType, context }: EvaluateResourceBasedInterface): boolean;
getStatements(this: ResourceBasedPolicy): ResourceBasedType[];
evaluate(this: ResourceBasedPolicy, { principal, action, resource, principalType, context }: EvaluateResourceBasedInterface): boolean;
can(this: ResourceBasedPolicy, { principal, action, resource, principalType, context }: EvaluateResourceBasedInterface): boolean;
cannot(this: ResourceBasedPolicy, { principal, action, resource, principalType, context }: EvaluateResourceBasedInterface): boolean;
}

export { ActionBased, ActionBasedPolicy, ActionBasedPolicyInterface, IdentityBased, IdentityBasedPolicy, ResourceBased, ResourceBasedPolicy, Statement, applyContext, baseGet, castPath, getValueFromPath };
10 changes: 5 additions & 5 deletions dist/main.es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions dist/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 14 additions & 4 deletions src/ActionBasedPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,22 @@ export class ActionBasedPolicy extends Policy {
);
}

getStatements(): ActionBasedType[] {
getStatements(this: ActionBasedPolicy): ActionBasedType[] {
return this.statements;
}

evaluate({ action, context }: EvaluateActionBasedInterface): boolean {
evaluate(
this: ActionBasedPolicy,
{ action, context }: EvaluateActionBasedInterface
): boolean {
const args = { action, context };
return !this.cannot(args) && this.can(args);
}

can({ action, context }: EvaluateActionBasedInterface): boolean {
can(
this: ActionBasedPolicy,
{ action, context }: EvaluateActionBasedInterface
): boolean {
return this.allowStatements.some((s) =>
s.matches({
action,
Expand All @@ -56,7 +62,10 @@ export class ActionBasedPolicy extends Policy {
);
}

cannot({ action, context }: EvaluateActionBasedInterface): boolean {
cannot(
this: ActionBasedPolicy,
{ action, context }: EvaluateActionBasedInterface
): boolean {
return this.denyStatements.some((s) =>
s.matches({
action,
Expand All @@ -67,6 +76,7 @@ export class ActionBasedPolicy extends Policy {
}

generateProxy<T, U extends keyof T>(
this: ActionBasedPolicy,
obj: unknown,
options: ProxyOptions = {}
): T | undefined {
Expand Down
11 changes: 5 additions & 6 deletions src/ActionBasedStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ class ActionBased extends Statement {
this.statement = { ...action, sid: this.sid };
}

getStatement(): ActionBasedType {
getStatement(this: ActionBased): ActionBasedType {
return this.statement;
}

matches({
action,
context,
conditionResolver
}: MatchActionBasedInterface): boolean {
matches(
this: ActionBased,
{ action, context, conditionResolver }: MatchActionBasedInterface
): boolean {
return (
this.matchActions(action, context) &&
this.matchNotActions(action, context) &&
Expand Down
25 changes: 13 additions & 12 deletions src/IdentityBasedPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,22 @@ export class IdentityBasedPolicy extends Policy {
);
}

getStatements(): IdentityBasedType[] {
getStatements(this: IdentityBasedPolicy): IdentityBasedType[] {
return this.statements;
}

evaluate({
action,
resource,
context
}: EvaluateIdentityBasedInterface): boolean {
evaluate(
this: IdentityBasedPolicy,
{ action, resource, context }: EvaluateIdentityBasedInterface
): boolean {
const args = { action, resource, context };
return !this.cannot(args) && this.can(args);
}

can({ action, resource, context }: EvaluateIdentityBasedInterface): boolean {
can(
this: IdentityBasedPolicy,
{ action, resource, context }: EvaluateIdentityBasedInterface
): boolean {
return this.allowStatements.some((s) =>
s.matches({
action,
Expand All @@ -60,11 +62,10 @@ export class IdentityBasedPolicy extends Policy {
);
}

cannot({
action,
resource,
context
}: EvaluateIdentityBasedInterface): boolean {
cannot(
this: IdentityBasedPolicy,
{ action, resource, context }: EvaluateIdentityBasedInterface
): boolean {
return this.denyStatements.some((s) =>
s.matches({
action,
Expand Down
17 changes: 10 additions & 7 deletions src/IdentityBasedStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ class IdentityBased extends Statement {
this.statement = { ...identity, sid: this.sid };
}

getStatement(): IdentityBasedType {
getStatement(this: IdentityBased): IdentityBasedType {
return this.statement;
}

matches({
action,
resource,
context,
conditionResolver
}: MatchIdentityBasedInterface): boolean {
matches(
this: IdentityBased,
{
action,
resource,
context,
conditionResolver
}: MatchIdentityBasedInterface
): boolean {
return (
this.matchActions(action, context) &&
this.matchNotActions(action, context) &&
Expand Down
12 changes: 6 additions & 6 deletions src/Matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export class Matcher {
});
}

match(this: Matcher, str: string): boolean {
if (this.empty) return str === '';

return this.set.some((pattern) => this.matchOne(str, pattern));
}

private braceExpand(): string[] {
const pattern = this.pattern;
if (!pattern.match(/{.*}/)) {
Expand Down Expand Up @@ -81,12 +87,6 @@ export class Matcher {
return expansions;
}

match(str: string): boolean {
if (this.empty) return str === '';

return this.set.some((pattern) => this.matchOne(str, pattern));
}

private matchOne(str: string, pattern: string | RegExp): boolean {
if (typeof pattern === 'string') {
return str === pattern;
Expand Down
11 changes: 7 additions & 4 deletions src/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ class Policy {
this.conditionResolver = conditionResolver;
}

setContext(context: Context): void {
setContext(this: Policy, context: Context): void {
this.context = context;
}

getContext(): Context | undefined {
getContext(this: Policy): Context | undefined {
return this.context;
}

setConditionResolver(conditionResolver: ConditionResolver): void {
setConditionResolver(
this: Policy,
conditionResolver: ConditionResolver
): void {
this.conditionResolver = conditionResolver;
}

getConditionResolver(): ConditionResolver | undefined {
getConditionResolver(this: Policy): ConditionResolver | undefined {
return this.conditionResolver;
}
}
Expand Down

0 comments on commit f182fd9

Please sign in to comment.