Skip to content

Commit

Permalink
fix(types): using object type in generateProxy
Browse files Browse the repository at this point in the history
using PropertyKey in Record types too
  • Loading branch information
roggervalf committed Nov 3, 2020
1 parent 0d70e43 commit 8f90ff1
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 55 deletions.
9 changes: 9 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@
],
"@typescript-eslint/no-explicit-any": "off",

"@typescript-eslint/ban-types": [
"error",
{
"extendDefaults": true,
"types": {
"object": false
}
}
],
// "@typescript-eslint/ban-ts-ignore": "off",

"no-console": "off",
Expand Down
8 changes: 4 additions & 4 deletions dist/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @param {Object} [object] The object to query keys on.
* @returns {Array} Returns the cast property path array.
*/
declare function castPath<T>(value: unknown, object: Record<string | symbol, unknown>): Array<T>;
declare function castPath<T>(value: unknown, object: Record<PropertyKey, unknown>): Array<T>;
/**
* The base implementation of `get` without support for default values.
*
Expand All @@ -15,7 +15,7 @@ declare function castPath<T>(value: unknown, object: Record<string | symbol, unk
* @param {Array|string} path The path of the property to get.
* @returns {*} Returns the resolved value.
*/
declare function baseGet<T>(object: Record<string, unknown>, path: Array<T> | string): any;
declare function baseGet<T>(object: Record<PropertyKey, unknown>, path: Array<T> | string): any;
/**
* Gets the value at `path` of `object`. If the resolved value is
* `undefined`, the `defaultValue` is returned in its place.
Expand All @@ -39,7 +39,7 @@ declare function baseGet<T>(object: Record<string, unknown>, path: Array<T> | st
* getValueFromPath(object, 'a.b.c', 'default')
* // => 'default'
*/
declare function getValueFromPath<T>(object: Record<string, unknown>, path: Array<T> | string, defaultValue?: unknown): any;
declare function getValueFromPath<T>(object: Record<PropertyKey, unknown>, path: Array<T> | string, defaultValue?: unknown): any;

declare type EffectBlock = 'allow' | 'deny';
declare type Patterns = string[] | string;
Expand Down Expand Up @@ -218,7 +218,7 @@ declare class ActionBasedPolicy extends Policy {
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;
generateProxy<T extends object, U extends keyof T>(this: ActionBasedPolicy, obj: T, options?: ProxyOptions): T;
}

interface IdentityBasedPolicyInterface {
Expand Down
10 changes: 3 additions & 7 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.

10 changes: 3 additions & 7 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.

24 changes: 4 additions & 20 deletions src/ActionBasedPolicy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ describe('ActionBasedPolicy Class', () => {
}
}
const user = new User('John', 'Wick');
const proxy = policy.generateProxy(user) as User;
const proxy = policy.generateProxy(user);
const getExpectedError = new Error(
'Unauthorize to get firstName property'
);
Expand Down Expand Up @@ -312,10 +312,7 @@ describe('ActionBasedPolicy Class', () => {
lastName: 'Wick',
[sym]: 1
};
const proxy = policy.generateProxy(user) as Record<
string | symbol,
unknown
>;
const proxy = policy.generateProxy(user);
const expectedError = new Error(
'Unauthorize to get firstName property'
);
Expand All @@ -326,19 +323,6 @@ describe('ActionBasedPolicy Class', () => {
expect(proxy.otherValue).toBe(undefined);
expect(() => proxy.firstName).toThrow(expectedError);
});

it('returns undefined', () => {
const policy = new ActionBasedPolicy({
statements: [
{
action: ['lastName']
}
]
});
const proxy = policy.generateProxy(8);

expect(proxy).toBe(undefined);
});
});

describe('when pass options', () => {
Expand All @@ -363,7 +347,7 @@ describe('ActionBasedPolicy Class', () => {
set: {
allow: false
}
}) as Record<string, unknown>;
});
const expectedError = new Error(
'Unauthorize to get firstName property'
);
Expand Down Expand Up @@ -393,7 +377,7 @@ describe('ActionBasedPolicy Class', () => {
get: {
allow: false
}
}) as Record<string, unknown>;
});
const expectedError = new Error(
'Unauthorize to set firstName property'
);
Expand Down
11 changes: 4 additions & 7 deletions src/ActionBasedPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ export class ActionBasedPolicy extends Policy {
);
}

generateProxy<T, U extends keyof T>(
generateProxy<T extends object, U extends keyof T>(
this: ActionBasedPolicy,
obj: unknown,
obj: T,
options: ProxyOptions = {}
): T | undefined {
): T {
const { get = {}, set = {} } = options;
const { allow: allowGet = true, propertyMap: propertyMapGet = {} } = get;
const { allow: allowSet = true, propertyMap: propertyMapSet = {} } = set;
Expand Down Expand Up @@ -114,9 +114,6 @@ export class ActionBasedPolicy extends Policy {
: {})
};

if (obj instanceof Object) {
return new Proxy(obj, handler) as T;
}
return undefined;
return new Proxy(obj, handler);
}
}
6 changes: 3 additions & 3 deletions src/utils/getValueFromPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { stringToPath } from './stringToPath';
*/
export function castPath<T>(
value: unknown,
object: Record<string | symbol, unknown>
object: Record<PropertyKey, unknown>
): Array<T> {
if (Array.isArray(value)) {
return value;
Expand All @@ -30,7 +30,7 @@ export function castPath<T>(
* @returns {*} Returns the resolved value.
*/
export function baseGet<T>(
object: Record<string, unknown>,
object: Record<PropertyKey, unknown>,
path: Array<T> | string
): any {
const newPath = castPath(path, object);
Expand Down Expand Up @@ -70,7 +70,7 @@ export function baseGet<T>(
* // => 'default'
*/
export function getValueFromPath<T>(
object: Record<string, unknown>,
object: Record<PropertyKey, unknown>,
path: Array<T> | string,
defaultValue?: unknown
): any {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/isKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isSymbol } from './isSymbol';

/** Used to match property names within property paths. */
const reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/;
const reIsPlainProp = /^\w*$/; //matches any word caracter (alphanumeric and underscore)
const reIsPlainProp = /^\w*$/; //matches any word character (alphanumeric and underscore)

/**
* Checks if `value` is a property name and not a property path.
Expand Down Expand Up @@ -37,7 +37,7 @@ const reIsPlainProp = /^\w*$/; //matches any word caracter (alphanumeric and und
*/
export function isKey(
value: unknown,
object?: Record<string | symbol, unknown>
object?: Record<PropertyKey, unknown>
): boolean {
const type = typeof value;
if (
Expand Down
6 changes: 3 additions & 3 deletions src/utils/toKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ const INFINITY = 1 / 0;
* toKey('abc')
* // => false
*/
export function toKey(value: any): string | symbol {
export function toKey(value: unknown): string | symbol {
if (typeof value === 'string' || isSymbol(value)) {
return value;
}
const result = `${value}`;
return result === '0' && 1 / value === -INFINITY ? '-0' : result;

return value === 0 && 1 / value === -INFINITY ? '-0' : `${value}`;
}

0 comments on commit 8f90ff1

Please sign in to comment.