@via-profit/ability 3.4.1 → 3.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,27 +0,0 @@
1
- import { AbilityExplain } from './AbilityExplain';
2
- import { ResourceObject } from './AbilityTypeGenerator';
3
- import AbilityPolicy from './AbilityPolicy';
4
- import AbilityPolicyEffect from './AbilityPolicyEffect';
5
- export declare class AbilityResult<Resource extends ResourceObject = Record<string, unknown>> {
6
- /**
7
- * Already checked policies (after call the policy.check())
8
- */
9
- readonly policies: readonly AbilityPolicy<Resource>[];
10
- constructor(policies: readonly AbilityPolicy<Resource>[]);
11
- /**
12
- * Returns a list of explanations for each policy involved in the ability evaluation.
13
- * Each item describes how a specific policy contributed to the final permission result.
14
- *
15
- * Useful for debugging, logging, or building UI tools that visualize permission logic.
16
- */
17
- explain(): readonly AbilityExplain[];
18
- getLastMatchedPolicy(): AbilityPolicy<Resource> | null;
19
- isAllowed(): boolean;
20
- isDenied(): boolean;
21
- /**
22
- * Get the last effect of the policy
23
- *
24
- * @returns {AbilityPolicyEffect | null}
25
- */
26
- getLastEffectOfMatchedPolicy(): AbilityPolicyEffect | null;
27
- }
@@ -1,88 +0,0 @@
1
- import AbilityMatch from './AbilityMatch';
2
- import { AbilityCondition, AbilityConditionCodeType } from './AbilityCondition';
3
- export type AbilityRuleConfig = {
4
- readonly id?: string | null;
5
- readonly name?: string | null;
6
- /**
7
- * Subject key path like a 'user.name'
8
- */
9
- readonly subject: string;
10
- /**
11
- * Resource key path like a 'user.name' or value
12
- */
13
- readonly resource: string | number | boolean | null | (string | number | boolean | null)[];
14
- readonly condition: AbilityConditionCodeType;
15
- };
16
- export type AbilityRuleConstructorProps = Omit<AbilityRuleConfig, 'condition'> & {
17
- readonly condition: AbilityCondition;
18
- };
19
- /**
20
- * Represents a rule that defines a condition to be checked against a subject and resource.
21
- */
22
- export declare class AbilityRule<Resources extends object = object, Environment = unknown> {
23
- /**
24
- * Subject key path like a 'user.name'
25
- */
26
- subject: string;
27
- /**
28
- * Resource key path like a 'user.name' or value
29
- */
30
- resource: AbilityRuleConfig['resource'];
31
- condition: AbilityCondition;
32
- name: string;
33
- id: string;
34
- state: AbilityMatch;
35
- /**
36
- * Creates an instance of AbilityRule.
37
- * @param {string} params.id - The unique identifier of the rule.
38
- * @param {string} params.name - The name of the rule.
39
- * @param {AbilityCondition} params.condition - The condition to evaluate.
40
- * @param {string} params.subject - The subject of the rule.
41
- * @param {string} params.resource - The resource to compare against.
42
- * @param params
43
- */
44
- constructor(params: AbilityRuleConstructorProps);
45
- private isPrimitive;
46
- private isNumber;
47
- private isString;
48
- private valueLen;
49
- private operatorHandlers;
50
- /**
51
- * Check if the rule is matched
52
- * @param resource - The resource to check
53
- * @param environment
54
- */
55
- check(resource: Resources | null, environment?: Environment): Promise<AbilityMatch>;
56
- /**
57
- * Extract values from the resourceData
58
- * @param resourceData - The resourceData to extract values from
59
- * @param environment - Environment data
60
- */
61
- extractValues(resourceData: Resources | null, environment?: Environment | null): [AbilityRuleConfig['resource'] | undefined, AbilityRuleConfig['resource'] | undefined];
62
- /**
63
- * Get the value of the object by dot notation
64
- * @param resource - The object to get the value from
65
- * @param desc - The dot notation string
66
- */
67
- getDotNotationValue<T = unknown>(resource: unknown, desc: string): T | undefined;
68
- toString(): string;
69
- copyWith(props: Partial<{
70
- id: string | null;
71
- name: string | null;
72
- subject: string;
73
- resource: AbilityRuleConfig['resource'];
74
- condition: AbilityCondition;
75
- }>): AbilityRule<Resources, Environment>;
76
- static equals<Resources extends object = object, Environment = unknown>(subject: string, resource: AbilityRuleConfig['resource']): AbilityRule<Resources, Environment>;
77
- static notEquals<Resources extends object = object, Environment = unknown>(subject: string, resource: AbilityRuleConfig['resource']): AbilityRule<Resources, Environment>;
78
- static contains<Resources extends object = object, Environment = unknown>(subject: string, resource: AbilityRuleConfig['resource']): AbilityRule<Resources, Environment>;
79
- static notContains<Resources extends object = object, Environment = unknown>(subject: string, resource: AbilityRuleConfig['resource']): AbilityRule<Resources, Environment>;
80
- static notIn<Resources extends object = object, Environment = unknown>(subject: string, resource: AbilityRuleConfig['resource']): AbilityRule<Resources, Environment>;
81
- static in<Resources extends object = object, Environment = unknown>(subject: string, resource: AbilityRuleConfig['resource']): AbilityRule<Resources, Environment>;
82
- static notEqual<Resources extends object = object, Environment = unknown>(subject: string, resource: AbilityRuleConfig['resource']): AbilityRule<Resources, Environment>;
83
- static lessThan<Resources extends object = object, Environment = unknown>(subject: string, resource: AbilityRuleConfig['resource']): AbilityRule<Resources, Environment>;
84
- static lessOrEqual<Resources extends object = object, Environment = unknown>(subject: string, resource: AbilityRuleConfig['resource']): AbilityRule<Resources, Environment>;
85
- static moreThan<Resources extends object = object, Environment = unknown>(subject: string, resource: AbilityRuleConfig['resource']): AbilityRule<Resources, Environment>;
86
- static moreOrEqual<Resources extends object = object, Environment = unknown>(subject: string, resource: AbilityRuleConfig['resource']): AbilityRule<Resources, Environment>;
87
- }
88
- export default AbilityRule;
@@ -1,51 +0,0 @@
1
- import AbilityRule, { AbilityRuleConfig } from './AbilityRule';
2
- import AbilityCompare, { AbilityCompareCodeType } from './AbilityCompare';
3
- import AbilityMatch from './AbilityMatch';
4
- import { ResourceObject } from './AbilityTypeGenerator';
5
- export type AbilityRuleSetConfig = {
6
- readonly id?: string | null;
7
- readonly name?: string | null;
8
- readonly compareMethod: AbilityCompareCodeType;
9
- readonly rules: readonly AbilityRuleConfig[];
10
- };
11
- export type AbilityRuleSetConstructorProps = {
12
- readonly id?: string | null;
13
- readonly name?: string | null;
14
- readonly compareMethod: AbilityCompare;
15
- };
16
- export declare class AbilityRuleSet<Resources extends ResourceObject = Record<string, unknown>, Environment = unknown> {
17
- state: AbilityMatch;
18
- /**
19
- * List of rules
20
- */
21
- rules: AbilityRule<Resources, Environment>[];
22
- /**
23
- * Rules compare method.\
24
- * For the «and» method the rule will be permitted if all\
25
- * rules will be returns «permit» status and for the «or» - if\
26
- * one of the rules returns as «permit»
27
- */
28
- compareMethod: AbilityCompare;
29
- /**
30
- * Group name
31
- */
32
- name: string;
33
- /**
34
- * Group ID
35
- */
36
- id: string;
37
- constructor(params: AbilityRuleSetConstructorProps);
38
- addRule(rule: AbilityRule<Resources, Environment>): this;
39
- addRules(rules: AbilityRule<Resources, Environment>[]): this;
40
- check(resources: Resources | null, environment?: Environment): Promise<AbilityMatch>;
41
- toString(): string;
42
- copyWith(props: Partial<{
43
- id: string | null;
44
- name: string | null;
45
- compareMethod: AbilityCompare;
46
- rules: AbilityRule<Resources, Environment>[];
47
- }>): AbilityRuleSet<Resources, Environment>;
48
- static and(rules: AbilityRule[]): AbilityRuleSet<Record<string, unknown>, unknown>;
49
- static or(rules: AbilityRule[]): AbilityRuleSet<Record<string, unknown>, unknown>;
50
- }
51
- export default AbilityRuleSet;
@@ -1,55 +0,0 @@
1
- import AbilityPolicy from './AbilityPolicy';
2
- export type Primitive = string | number | boolean | null | undefined;
3
- export type NestedDict<T = Primitive> = {
4
- [key: string]: NestedDict<T> | T;
5
- };
6
- export type ResourceObject = Record<string, unknown>;
7
- export type ResourcesMap = Record<string, ResourceObject>;
8
- export declare class AbilityTypeGenerator {
9
- readonly policies: readonly AbilityPolicy[];
10
- constructor(policies: readonly AbilityPolicy[]);
11
- /**
12
- * Generates TypeScript type definitions based on the provided policies.
13
- * @returns A generated type definitions.
14
- */
15
- generateTypeDefs(): string;
16
- /**
17
- * Determines TypeScript type based on the rule
18
- * @param rule - The rule to analyze
19
- * @returns TypeScript type as string
20
- */
21
- private determineTypeFromRule;
22
- /**
23
- * Gets TypeScript type for array values
24
- * @param resource - The resource value to analyze
25
- * @returns TypeScript array type as string
26
- */
27
- private getArrayType;
28
- /**
29
- * Gets primitive TypeScript type for a value
30
- * @param value - The value to analyze
31
- * @returns TypeScript primitive type as string
32
- */
33
- private getPrimitiveType;
34
- /**
35
- * Builds nested structure from flat paths
36
- * Example: 'user.profile.name' -> { user: { profile: { name: 'string' } } }
37
- * @param flatStructure - Flat structure with dot notation paths
38
- * @returns Nested object structure
39
- */
40
- private buildNestedStructure;
41
- /**
42
- * Formats type structure into a string
43
- * @param structure - Nested type structure
44
- * @returns Formatted TypeScript type definition string
45
- */
46
- private formatTypeDefinitions;
47
- /**
48
- * Recursively formats nested object
49
- * @param obj - Object to format
50
- * @param indent - Current indentation level
51
- * @returns Formatted string
52
- */
53
- private formatNestedObject;
54
- }
55
- export default AbilityTypeGenerator;
@@ -1,24 +0,0 @@
1
- import { AbilityDSLToken } from '../../parsers/dsl/AbilityDSLToken';
2
- export declare class AbilityDSLLexer {
3
- private readonly input;
4
- private pos;
5
- private tokens;
6
- private line;
7
- private column;
8
- private readonly keywords;
9
- constructor(input: string);
10
- tokenize(): AbilityDSLToken[];
11
- private readComment;
12
- private readString;
13
- private readNumber;
14
- private readSymbol;
15
- private readWord;
16
- private skipWhitespace;
17
- private isDigit;
18
- private isAlpha;
19
- private isSymbol;
20
- private isNewline;
21
- private peek;
22
- private advance;
23
- private isAtEnd;
24
- }
@@ -1,85 +0,0 @@
1
- import AbilityPolicy from '../../core/AbilityPolicy';
2
- import { ResourceObject } from '../../core/AbilityTypeGenerator';
3
- /**
4
- * Parser for the Ability DSL.
5
- *
6
- * Converts a DSL string into one or more AbilityPolicy instances.
7
- * The grammar follows the structure:
8
- *
9
- * <effect> <permission> if <group> [ <group> ... ]
10
- *
11
- * where <group> is either "all of:" or "any of:", followed by a colon,
12
- * and then a list of rules (one per line).
13
- *
14
- * Each rule is: <identifier> <operator> <value>
15
- *
16
- * Operators can be simple (equals, contains, in) or
17
- * composed (is null, is not null, greater than, less than or equal, etc.).
18
- */
19
- export declare class AbilityDSLParser<Resource extends ResourceObject = Record<string, unknown>, Environment = unknown> {
20
- private dsl;
21
- private tokens;
22
- private pos;
23
- private annotationBuffer;
24
- constructor(dsl: string);
25
- /**
26
- * Main entry point: tokenize the input and parse all policies.
27
- * @returns Array of AbilityPolicy instances.
28
- */
29
- parse(): readonly AbilityPolicy<Resource, Environment>[];
30
- /**
31
- * Parses a single policy from the current token position.
32
- *
33
- * Grammar:
34
- * policy = EFFECT PERMISSION IF (ALL | ANY) COLON ruleSets
35
- */
36
- private parsePolicy;
37
- /**
38
- * Parses a sequence of rule sets (groups) until a new policy starts or EOF.
39
- */
40
- private parseRuleSets;
41
- /**
42
- * Parses a single group, e.g. "all of:" or "any of:", and returns a RuleSet.
43
- */
44
- private parseGroup;
45
- /**
46
- * Parses a single rule: subject operator value
47
- */
48
- private parseRule;
49
- /**
50
- * Parses the comparison operator part of a rule.
51
- * Returns both the resulting AbilityCondition and the token type that was consumed.
52
- */
53
- private parseConditionOperator;
54
- /**
55
- * Helper to match and consume a specific word token (KEYWORD or IDENTIFIER).
56
- * @param word The exact string to look for.
57
- * @returns True if the next token has that value.
58
- */
59
- private matchWord;
60
- private matchSymbol;
61
- /**
62
- * Parses a resource value. Can be a string literal, number, boolean,
63
- * null, a path (identifier), or an array.
64
- */
65
- private parseValue;
66
- /**
67
- * Parses an array literal: [ <value>, <value>, ... ]
68
- * The opening bracket has already been consumed.
69
- */
70
- private parseArray;
71
- private consumeLeadingComments;
72
- private processCommentToken;
73
- private takeAnnotations;
74
- private syntaxError;
75
- private suggest;
76
- private levenshteinDistance;
77
- private consumeOneOf;
78
- private consume;
79
- private check;
80
- private isStartOfPolicy;
81
- private isStartOfGroup;
82
- private advance;
83
- private peek;
84
- private isAtEnd;
85
- }
@@ -1,13 +0,0 @@
1
- export declare class AbilityDSLSyntaxError extends Error {
2
- readonly line: number;
3
- readonly column: number;
4
- readonly context: string;
5
- readonly details: string;
6
- private readonly _formattedMessage;
7
- private readonly _originalStack?;
8
- constructor(line: number, column: number, context: string, // строка DSL + ^ + соседние строки
9
- details: string);
10
- private static supportsColor;
11
- private formatMessage;
12
- toString(): string;
13
- }
@@ -1,57 +0,0 @@
1
- import AbilityCode from '../../core/AbilityCode';
2
- export type TokenType = 'EFFECT' | 'IF' | 'PERMISSION' | 'IDENTIFIER' | 'COLON' | 'COMMA' | 'DOT' | 'LBRACKET' | 'RBRACKET' | 'ALL' | 'ANY' | 'OF' | 'EOF' | 'COMMENT' | 'EQ' | 'CONTAINS' | 'IN' | 'NOT_IN' | 'NOT_CONTAINS' | 'GT' | 'GTE' | 'LT' | 'LTE' | 'NULL' | 'EQ_NULL' | 'NOT_EQ_NULL' | 'NOT_EQ' | 'LEN_GT' | 'LEN_LT' | 'LEN_EQ' | 'ALWAYS' | 'NEVER' | 'STRING' | 'NUMBER' | 'BOOLEAN' | 'SYMBOL' | 'KEYWORD' | 'UNKNOWN';
3
- /**
4
- * Represents a single token produced by the Ability DSL lexer.
5
- * Each token carries a type (e.g., EFFECT, IDENTIFIER, STRING) and its raw string value.
6
- */
7
- export declare class AbilityDSLToken<Code extends TokenType = TokenType> extends AbilityCode<Code> {
8
- /** The literal text of the token as it appeared in the input (e.g., "permit", "user.roles", "admin"). */
9
- readonly value: string;
10
- /** The line number in DSL */
11
- readonly line: number;
12
- /** The column in dsl */
13
- readonly column: number;
14
- constructor(type: Code, value: string, line: number, column: number);
15
- /**
16
- * Returns a human-readable representation of the token, useful for debugging.
17
- * Example output: "AbilityDSLToken([EFFECT] permit"
18
- */
19
- toString(): string;
20
- static readonly EFFECT: TokenType;
21
- static readonly IF: TokenType;
22
- static readonly PERMISSION: TokenType;
23
- static readonly IDENTIFIER: TokenType;
24
- static readonly COLON: TokenType;
25
- static readonly COMMA: TokenType;
26
- static readonly DOT: TokenType;
27
- static readonly LBRACKET: TokenType;
28
- static readonly RBRACKET: TokenType;
29
- static readonly ALL: TokenType;
30
- static readonly ANY: TokenType;
31
- static readonly OF: TokenType;
32
- static readonly EOF: TokenType;
33
- static readonly COMMENT: TokenType;
34
- static readonly EQ: TokenType;
35
- static readonly CONTAINS: TokenType;
36
- static readonly IN: TokenType;
37
- static readonly NOT_IN: TokenType;
38
- static readonly NOT_CONTAINS: TokenType;
39
- static readonly GT: TokenType;
40
- static readonly GTE: TokenType;
41
- static readonly LT: TokenType;
42
- static readonly LTE: TokenType;
43
- static readonly NULL: TokenType;
44
- static readonly EQ_NULL: TokenType;
45
- static readonly NOT_EQ_NULL: TokenType;
46
- static readonly LEN_GT: TokenType;
47
- static readonly LEN_LT: TokenType;
48
- static readonly LEN_EQ: TokenType;
49
- static readonly NOT_EQ: TokenType;
50
- static readonly ALWAYS: TokenType;
51
- static readonly NEVER: TokenType;
52
- static readonly STRING: TokenType;
53
- static readonly NUMBER: TokenType;
54
- static readonly BOOLEAN: TokenType;
55
- static readonly SYMBOL: TokenType;
56
- static readonly KEYWORD: TokenType;
57
- }
@@ -1,22 +0,0 @@
1
- import { AbilityRule, AbilityRuleConfig } from '../../core/AbilityRule';
2
- import { AbilityRuleSet, AbilityRuleSetConfig } from '../../core/AbilityRuleSet';
3
- import { ResourceObject } from '../../core/AbilityTypeGenerator';
4
- import { AbilityPolicy, AbilityPolicyConfig } from '../../core/AbilityPolicy';
5
- export declare class AbilityJSONParser {
6
- /**
7
- * Parses an array of policy configurations into an array of AbilityPolicy instances.
8
- * @param configs - Array of policy configurations
9
- * @returns Array of AbilityPolicy instances
10
- */
11
- static parse<Resource extends ResourceObject, Environment = unknown>(configs: readonly AbilityPolicyConfig[]): AbilityPolicy<Resource, Environment>[];
12
- static parsePolicy<Resource extends ResourceObject = Record<string, unknown>, Environment = unknown>(config: AbilityPolicyConfig): AbilityPolicy<Resource, Environment>;
13
- static parseRule<Resources extends object, Environment = unknown>(config: AbilityRuleConfig): AbilityRule<Resources, Environment>;
14
- /**
15
- * Parse the config JSON format to Group class instance
16
- */
17
- static parseRuleSet<Resource extends ResourceObject = Record<string, unknown>, Environment = unknown>(config: AbilityRuleSetConfig): AbilityRuleSet<Resource, Environment>;
18
- static ruleToJSON(rule: AbilityRule): AbilityRuleConfig;
19
- static ruleSetToJSON(ruleSet: AbilityRuleSet): AbilityRuleSetConfig;
20
- static policyToJSON(policy: AbilityPolicy): AbilityPolicyConfig;
21
- static toJSON(policies: readonly AbilityPolicy[]): AbilityPolicyConfig[];
22
- }