Skip to content

Commit

Permalink
feat(generate-proxy): update Proxy typing
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed Oct 17, 2021
1 parent 9ac995b commit 326c432
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 29 deletions.
4 changes: 2 additions & 2 deletions dist/main.d.ts
Expand Up @@ -118,11 +118,11 @@ declare type ResourceBasedType = StatementInterface & (OptionalPrincipalBlock |
interface ProxyOptions {
get?: {
allow?: boolean;
propertyMap?: Record<PropertyKey, PropertyKey>;
propertyMap?: Record<string, string>;
};
set?: {
allow?: boolean;
propertyMap?: Record<PropertyKey, PropertyKey>;
propertyMap?: Record<string, string>;
};
}

Expand Down
34 changes: 26 additions & 8 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.

34 changes: 26 additions & 8 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.

3 changes: 2 additions & 1 deletion src/ActionBasedPolicy.test.ts
Expand Up @@ -445,7 +445,7 @@ describe('ActionBasedPolicy Class', () => {
'Unauthorize to get firstName property'
);
const expectedError2 = new Error(
'Unauthorize to set Symbol(id) property'
"'set' on proxy: trap returned falsish for property 'Symbol(id)'"
);
const expectedError3 = new Error(
'Unauthorize to get otherValue property'
Expand All @@ -454,6 +454,7 @@ describe('ActionBasedPolicy Class', () => {
expect(proxy.lastName).toBe('Wick');
expect(() => proxy.firstName).toThrow(expectedError);
expect(() => (proxy[sym] = 2)).toThrow(expectedError2);
expect(proxy[sym]).toBe(1);
expect(() => proxy.otherValue).toThrow(expectedError3);
});
});
Expand Down
20 changes: 14 additions & 6 deletions src/ActionBasedPolicy.ts
Expand Up @@ -102,10 +102,14 @@ export class ActionBasedPolicy<T extends object> extends Policy<
const property = Reflect.has(propertyMapGet, prop)
? Reflect.get(propertyMapGet, prop)
: prop;
if (this.evaluate({ action: property })) {
return Reflect.get(target, 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 {
throw new Error(`Unauthorize to get ${String(prop)} property`);
return Reflect.get(target, prop);
}
}
}
Expand All @@ -116,10 +120,14 @@ export class ActionBasedPolicy<T extends object> extends Policy<
const property = Reflect.has(propertyMapSet, prop)
? Reflect.get(propertyMapSet, prop)
: prop;
if (this.evaluate({ action: property })) {
return Reflect.set(target, prop, value);
if (typeof prop === 'string') {
if (this.evaluate({ action: property })) {
return Reflect.set(target, prop, value);
} else {
throw new Error(`Unauthorize to set ${prop} property`);
}
} else {
throw new Error(`Unauthorize to set ${String(prop)} property`);
return false;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Expand Up @@ -130,11 +130,11 @@ type ResourceBasedType = StatementInterface &
interface ProxyOptions {
get?: {
allow?: boolean;
propertyMap?: Record<PropertyKey, PropertyKey>;
propertyMap?: Record<string, string>;
};
set?: {
allow?: boolean;
propertyMap?: Record<PropertyKey, PropertyKey>;
propertyMap?: Record<string, string>;
};
}

Expand Down

0 comments on commit 326c432

Please sign in to comment.