Skip to content

Commit

Permalink
feat(generate-proxy): update Proxy types (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed Oct 17, 2021
1 parent fc6534e commit 236627a
Show file tree
Hide file tree
Showing 11 changed files with 2,773 additions and 3,066 deletions.
1 change: 0 additions & 1 deletion .prettierrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ module.exports = {
printWidth: 80,
semi: true,
bracketSpacing: true,
jsxBracketSameLine: false,
endOfLine: 'lf'
};
2 changes: 1 addition & 1 deletion dist/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ declare class ActionBasedPolicy<T extends object> extends Policy<T, ActionBasedT
evaluate(this: ActionBasedPolicy<T>, { action, context }: EvaluateActionBasedInterface<T>): boolean;
can(this: ActionBasedPolicy<T>, { action, context }: EvaluateActionBasedInterface<T>): boolean;
cannot(this: ActionBasedPolicy<T>, { action, context }: EvaluateActionBasedInterface<T>): boolean;
generateProxy<U extends object, W extends keyof U>(this: ActionBasedPolicy<T>, obj: U, options?: ProxyOptions): U;
generateProxy<U extends object>(this: ActionBasedPolicy<T>, obj: U, options?: ProxyOptions): U;
}

interface IdentityBasedPolicyInterface<T extends object> {
Expand Down
31 changes: 20 additions & 11 deletions dist/main.es.js

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

2 changes: 1 addition & 1 deletion dist/main.es.js.map

Large diffs are not rendered by default.

31 changes: 20 additions & 11 deletions dist/main.js

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

2 changes: 1 addition & 1 deletion dist/main.js.map

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@
"@babel/core": "7.7.2",
"@babel/preset-env": "7.7.1",
"@babel/preset-typescript": "7.7.2",
"@commitlint/cli": "11.0.0",
"@commitlint/config-conventional": "11.0.0",
"@commitlint/travis-cli": "11.0.0",
"@commitlint/cli": "8.3.5",
"@commitlint/config-conventional": "8.3.4",
"@commitlint/travis-cli": "10.0.0",
"@rollup/plugin-babel": "5.0.0",
"@rollup/plugin-commonjs": "16.0.0",
"@rollup/plugin-node-resolve": "10.0.0",
Expand All @@ -87,15 +87,15 @@
"lint-staged": "10.5.4",
"markdownlint": "0.18.0",
"npm-run-all": "4.1.5",
"prettier": "2.1.2",
"pretty-quick": "3.1.0",
"prettier": "2.4.1",
"pretty-quick": "3.1.1",
"rimraf": "3.0.0",
"rollup": "2.33.3",
"rollup-plugin-dts": "1.4.13",
"rollup-plugin-peer-deps-external": "2.2.4",
"rollup-plugin-typescript2": "0.25.3",
"semantic-release": "17.3.9",
"tslib": "1.10.0",
"tslib": "1.14.1",
"typescript": "4.0.3"
},
"files": [
Expand Down
14 changes: 10 additions & 4 deletions src/ActionBasedPolicy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ describe('ActionBasedPolicy Class', () => {
}
]
});
const sym: string = (Symbol('id') as unknown) as string;
const sym: string = Symbol('id') as unknown as string;
const user = {
firstName: 'John',
lastName: 'Wick',
Expand All @@ -444,12 +444,18 @@ describe('ActionBasedPolicy Class', () => {
const expectedError = new Error(
'Unauthorize to get firstName property'
);
const expectedError2 = new Error(
"'set' on proxy: trap returned falsish for property 'Symbol(id)'"
);
const expectedError3 = new Error(
'Unauthorize to get otherValue property'
);

expect(proxy.lastName).toBe('Wick');
expect(() => (proxy[sym] = 2)).not.toThrow();
expect(proxy[sym]).toBe(1);
expect(proxy.otherValue).toBe(undefined);
expect(() => proxy.firstName).toThrow(expectedError);
expect(() => (proxy[sym] = 2)).toThrow(expectedError2);
expect(proxy[sym]).toBe(1);
expect(() => proxy.otherValue).toThrow(expectedError3);
});
});

Expand Down
34 changes: 21 additions & 13 deletions src/ActionBasedPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class ActionBasedPolicy<T extends object> extends Policy<
);
}

generateProxy<U extends object, W extends keyof U>(
generateProxy<U extends object>(
this: ActionBasedPolicy<T>,
obj: U,
options: ProxyOptions = {}
Expand All @@ -98,29 +98,37 @@ export class ActionBasedPolicy<T extends object> extends Policy<
const handler = {
...(allowGet
? {
get: (target: U, prop: W): any => {
if (prop in target) {
if (typeof prop === 'string') {
const property = propertyMapGet[prop] || prop;
if (this.evaluate({ action: property })) return target[prop];
get: (target: U, prop: string | symbol): any => {
const property = Reflect.has(propertyMapGet, prop)
? Reflect.get(propertyMapGet, prop)
: prop;
if (typeof prop === 'string') {
if (this.evaluate({ action: property })) {
return Reflect.get(target, prop);
} else {
throw new Error(`Unauthorize to get ${prop} property`);
}
} else {
return Reflect.get(target, prop);
}
return target[prop];
}
}
: {}),
...(allowSet
? {
set: (target: U, prop: W, value: any): boolean => {
set: (target: U, prop: string | symbol, value: any): boolean => {
const property = Reflect.has(propertyMapSet, prop)
? Reflect.get(propertyMapSet, prop)
: prop;
if (typeof prop === 'string') {
const property = propertyMapSet[prop] || prop;
if (this.evaluate({ action: property })) {
target[prop] = value;
return true;
} else throw new Error(`Unauthorize to set ${prop} property`);
return Reflect.set(target, prop, value);
} else {
throw new Error(`Unauthorize to set ${prop} property`);
}
} else {
return false;
}
return true;
}
}
: {})
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export interface MatchConditionInterface<T extends object> {
}

export interface MatchConditionResolverInterface<T extends object> {
context: T
context: T;
conditionResolver?: ConditionResolver;
path: string;
condition: string;
Expand Down

0 comments on commit 236627a

Please sign in to comment.