graphql-shield-node23 7.6.5
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.
- package/CHANGELOG.md +31 -0
- package/dist/cjs/constructors.js +134 -0
- package/dist/cjs/generator.js +205 -0
- package/dist/cjs/index.js +15 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/rules.js +402 -0
- package/dist/cjs/shield.js +52 -0
- package/dist/cjs/types.js +2 -0
- package/dist/cjs/utils.js +97 -0
- package/dist/cjs/validation.js +84 -0
- package/dist/esm/constructors.js +124 -0
- package/dist/esm/generator.js +201 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/rules.js +366 -0
- package/dist/esm/shield.js +45 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/utils.js +88 -0
- package/dist/esm/validation.js +79 -0
- package/dist/package.json +47 -0
- package/dist/typings/constructors.d.cts +91 -0
- package/dist/typings/constructors.d.ts +91 -0
- package/dist/typings/generator.d.cts +11 -0
- package/dist/typings/generator.d.ts +11 -0
- package/dist/typings/index.d.cts +3 -0
- package/dist/typings/index.d.ts +3 -0
- package/dist/typings/rules.d.cts +159 -0
- package/dist/typings/rules.d.ts +159 -0
- package/dist/typings/shield.d.cts +11 -0
- package/dist/typings/shield.d.ts +11 -0
- package/dist/typings/types.d.cts +64 -0
- package/dist/typings/types.d.ts +64 -0
- package/dist/typings/utils.d.cts +52 -0
- package/dist/typings/utils.d.ts +52 -0
- package/dist/typings/validation.d.cts +19 -0
- package/dist/typings/validation.d.ts +19 -0
- package/package.json +67 -0
- package/src/constructors.ts +157 -0
- package/src/generator.ts +294 -0
- package/src/index.ts +13 -0
- package/src/rules.ts +521 -0
- package/src/shield.ts +53 -0
- package/src/types.ts +94 -0
- package/src/utils.ts +101 -0
- package/src/validation.ts +90 -0
- package/tests/__snapshots__/input.test.ts.snap +7 -0
- package/tests/cache.test.ts +545 -0
- package/tests/constructors.test.ts +136 -0
- package/tests/fallback.test.ts +618 -0
- package/tests/fragments.test.ts +113 -0
- package/tests/generator.test.ts +356 -0
- package/tests/input.test.ts +63 -0
- package/tests/integration.test.ts +65 -0
- package/tests/logic.test.ts +530 -0
- package/tests/utils.test.ts +55 -0
- package/tests/validation.test.ts +139 -0
- package/tsconfig.json +10 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import * as Yup from 'yup';
|
|
2
|
+
import { IRuleFunction, IRule, IFragment, IRuleConstructorOptions, ILogicRule, ShieldRule, IRuleResult, IOptions, IShieldContext } from './types.js';
|
|
3
|
+
import { GraphQLResolveInfo } from 'graphql';
|
|
4
|
+
export declare class Rule implements IRule {
|
|
5
|
+
readonly name: string;
|
|
6
|
+
private cache;
|
|
7
|
+
private fragment?;
|
|
8
|
+
private func;
|
|
9
|
+
constructor(name: string, func: IRuleFunction, constructorOptions: IRuleConstructorOptions);
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
* @param parent
|
|
13
|
+
* @param args
|
|
14
|
+
* @param ctx
|
|
15
|
+
* @param info
|
|
16
|
+
*
|
|
17
|
+
* Resolves rule and writes to cache its result.
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
resolve(parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo, options: IOptions): Promise<IRuleResult>;
|
|
21
|
+
/**
|
|
22
|
+
*
|
|
23
|
+
* @param rule
|
|
24
|
+
*
|
|
25
|
+
* Compares a given rule with the current one
|
|
26
|
+
* and checks whether their functions are equal.
|
|
27
|
+
*
|
|
28
|
+
*/
|
|
29
|
+
equals(rule: Rule): boolean;
|
|
30
|
+
/**
|
|
31
|
+
*
|
|
32
|
+
* Extracts fragment from the rule.
|
|
33
|
+
*
|
|
34
|
+
*/
|
|
35
|
+
extractFragment(): IFragment | undefined;
|
|
36
|
+
/**
|
|
37
|
+
*
|
|
38
|
+
* @param options
|
|
39
|
+
*
|
|
40
|
+
* Sets default values for options.
|
|
41
|
+
*
|
|
42
|
+
*/
|
|
43
|
+
private normalizeOptions;
|
|
44
|
+
/**
|
|
45
|
+
*
|
|
46
|
+
* @param cache
|
|
47
|
+
*
|
|
48
|
+
* This ensures backward capability of shield.
|
|
49
|
+
*
|
|
50
|
+
*/
|
|
51
|
+
private normalizeCacheOption;
|
|
52
|
+
/**
|
|
53
|
+
* Executes a rule and writes to cache if needed.
|
|
54
|
+
*
|
|
55
|
+
* @param parent
|
|
56
|
+
* @param args
|
|
57
|
+
* @param ctx
|
|
58
|
+
* @param info
|
|
59
|
+
*/
|
|
60
|
+
private executeRule;
|
|
61
|
+
/**
|
|
62
|
+
* Writes or reads result from cache.
|
|
63
|
+
*
|
|
64
|
+
* @param key
|
|
65
|
+
*/
|
|
66
|
+
private writeToCache;
|
|
67
|
+
}
|
|
68
|
+
export declare class InputRule<T> extends Rule {
|
|
69
|
+
constructor(name: string, schema: (yup: typeof Yup, ctx: IShieldContext) => Yup.BaseSchema<T>, options?: Parameters<Yup.BaseSchema<T>['validate']>[1]);
|
|
70
|
+
}
|
|
71
|
+
export declare class LogicRule implements ILogicRule {
|
|
72
|
+
private rules;
|
|
73
|
+
constructor(rules: ShieldRule[]);
|
|
74
|
+
/**
|
|
75
|
+
* By default logic rule resolves to false.
|
|
76
|
+
*/
|
|
77
|
+
resolve(parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo, options: IOptions): Promise<IRuleResult>;
|
|
78
|
+
/**
|
|
79
|
+
* Evaluates all the rules.
|
|
80
|
+
*/
|
|
81
|
+
evaluate(parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo, options: IOptions): Promise<IRuleResult[]>;
|
|
82
|
+
/**
|
|
83
|
+
* Returns rules in a logic rule.
|
|
84
|
+
*/
|
|
85
|
+
getRules(): ShieldRule[];
|
|
86
|
+
/**
|
|
87
|
+
* Extracts fragments from the defined rules.
|
|
88
|
+
*/
|
|
89
|
+
extractFragments(): IFragment[];
|
|
90
|
+
}
|
|
91
|
+
export declare class RuleOr extends LogicRule {
|
|
92
|
+
constructor(rules: ShieldRule[]);
|
|
93
|
+
/**
|
|
94
|
+
* Makes sure that at least one of them has evaluated to true.
|
|
95
|
+
*/
|
|
96
|
+
resolve(parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo, options: IOptions): Promise<IRuleResult>;
|
|
97
|
+
}
|
|
98
|
+
export declare class RuleAnd extends LogicRule {
|
|
99
|
+
constructor(rules: ShieldRule[]);
|
|
100
|
+
/**
|
|
101
|
+
* Makes sure that all of them have resolved to true.
|
|
102
|
+
*/
|
|
103
|
+
resolve(parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo, options: IOptions): Promise<IRuleResult>;
|
|
104
|
+
}
|
|
105
|
+
export declare class RuleChain extends LogicRule {
|
|
106
|
+
constructor(rules: ShieldRule[]);
|
|
107
|
+
/**
|
|
108
|
+
* Makes sure that all of them have resolved to true.
|
|
109
|
+
*/
|
|
110
|
+
resolve(parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo, options: IOptions): Promise<IRuleResult>;
|
|
111
|
+
/**
|
|
112
|
+
* Evaluates all the rules.
|
|
113
|
+
*/
|
|
114
|
+
evaluate(parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo, options: IOptions): Promise<IRuleResult[]>;
|
|
115
|
+
}
|
|
116
|
+
export declare class RuleRace extends LogicRule {
|
|
117
|
+
constructor(rules: ShieldRule[]);
|
|
118
|
+
/**
|
|
119
|
+
* Makes sure that at least one of them resolved to true.
|
|
120
|
+
*/
|
|
121
|
+
resolve(parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo, options: IOptions): Promise<IRuleResult>;
|
|
122
|
+
/**
|
|
123
|
+
* Evaluates all the rules.
|
|
124
|
+
*/
|
|
125
|
+
evaluate(parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo, options: IOptions): Promise<IRuleResult[]>;
|
|
126
|
+
}
|
|
127
|
+
export declare class RuleNot extends LogicRule {
|
|
128
|
+
error?: Error;
|
|
129
|
+
constructor(rule: ShieldRule, error?: Error);
|
|
130
|
+
/**
|
|
131
|
+
*
|
|
132
|
+
* @param parent
|
|
133
|
+
* @param args
|
|
134
|
+
* @param ctx
|
|
135
|
+
* @param info
|
|
136
|
+
*
|
|
137
|
+
* Negates the result.
|
|
138
|
+
*
|
|
139
|
+
*/
|
|
140
|
+
resolve(parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo, options: IOptions): Promise<IRuleResult>;
|
|
141
|
+
}
|
|
142
|
+
export declare class RuleTrue extends LogicRule {
|
|
143
|
+
constructor();
|
|
144
|
+
/**
|
|
145
|
+
*
|
|
146
|
+
* Always true.
|
|
147
|
+
*
|
|
148
|
+
*/
|
|
149
|
+
resolve(): Promise<IRuleResult>;
|
|
150
|
+
}
|
|
151
|
+
export declare class RuleFalse extends LogicRule {
|
|
152
|
+
constructor();
|
|
153
|
+
/**
|
|
154
|
+
*
|
|
155
|
+
* Always false.
|
|
156
|
+
*
|
|
157
|
+
*/
|
|
158
|
+
resolve(): Promise<IRuleResult>;
|
|
159
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { IMiddlewareGenerator } from 'graphql-middleware';
|
|
2
|
+
import { IRules, IOptionsConstructor } from './types.cjs';
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param ruleTree
|
|
6
|
+
* @param options
|
|
7
|
+
*
|
|
8
|
+
* Validates rules and generates middleware from defined rule tree.
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
export declare function shield<TSource = any, TContext = any, TArgs = any>(ruleTree: IRules, options?: IOptionsConstructor): IMiddlewareGenerator<TSource, TContext, TArgs>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { IMiddlewareGenerator } from 'graphql-middleware';
|
|
2
|
+
import { IRules, IOptionsConstructor } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param ruleTree
|
|
6
|
+
* @param options
|
|
7
|
+
*
|
|
8
|
+
* Validates rules and generates middleware from defined rule tree.
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
export declare function shield<TSource = any, TContext = any, TArgs = any>(ruleTree: IRules, options?: IOptionsConstructor): IMiddlewareGenerator<TSource, TContext, TArgs>;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { GraphQLResolveInfo } from 'graphql';
|
|
2
|
+
import { IMiddlewareGenerator } from 'graphql-middleware';
|
|
3
|
+
export declare type ShieldRule = IRule | ILogicRule;
|
|
4
|
+
export interface IRule {
|
|
5
|
+
readonly name: string;
|
|
6
|
+
equals(rule: IRule): boolean;
|
|
7
|
+
extractFragment(): IFragment | undefined;
|
|
8
|
+
resolve(parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo, options: IOptions): Promise<IRuleResult>;
|
|
9
|
+
}
|
|
10
|
+
export interface IRuleOptions {
|
|
11
|
+
cache: ICache;
|
|
12
|
+
fragment?: IFragment;
|
|
13
|
+
}
|
|
14
|
+
export interface ILogicRule {
|
|
15
|
+
getRules(): ShieldRule[];
|
|
16
|
+
extractFragments(): IFragment[];
|
|
17
|
+
evaluate(parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo, options: IOptions): Promise<IRuleResult[]>;
|
|
18
|
+
resolve(parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo, options: IOptions): Promise<IRuleResult>;
|
|
19
|
+
}
|
|
20
|
+
export declare type IFragment = string;
|
|
21
|
+
export declare type ICache = 'strict' | 'contextual' | 'no_cache' | ICacheKeyFn;
|
|
22
|
+
export declare type ICacheKeyFn = (parent: any, args: any, ctx: any, info: GraphQLResolveInfo) => string;
|
|
23
|
+
export declare type IRuleResult = boolean | string | Error;
|
|
24
|
+
export declare type IRuleFunction = (parent: any, args: any, ctx: any, info: GraphQLResolveInfo) => IRuleResult | Promise<IRuleResult>;
|
|
25
|
+
export declare type ICacheContructorOptions = ICache | boolean;
|
|
26
|
+
export interface IRuleConstructorOptions {
|
|
27
|
+
cache?: ICacheContructorOptions;
|
|
28
|
+
fragment?: IFragment;
|
|
29
|
+
}
|
|
30
|
+
export interface IRuleTypeMap {
|
|
31
|
+
[key: string]: ShieldRule | IRuleFieldMap;
|
|
32
|
+
}
|
|
33
|
+
export interface IRuleFieldMap {
|
|
34
|
+
[key: string]: ShieldRule;
|
|
35
|
+
}
|
|
36
|
+
export declare type IRules = ShieldRule | IRuleTypeMap;
|
|
37
|
+
export declare type IHashFunction = (arg: {
|
|
38
|
+
parent: any;
|
|
39
|
+
args: any;
|
|
40
|
+
}) => string;
|
|
41
|
+
export declare type IFallbackErrorMapperType = (err: unknown, parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo) => Promise<Error> | Error;
|
|
42
|
+
export declare type IFallbackErrorType = Error | IFallbackErrorMapperType;
|
|
43
|
+
export interface IOptions {
|
|
44
|
+
debug: boolean;
|
|
45
|
+
allowExternalErrors: boolean;
|
|
46
|
+
fallbackRule: ShieldRule;
|
|
47
|
+
fallbackError?: IFallbackErrorType;
|
|
48
|
+
hashFunction: IHashFunction;
|
|
49
|
+
}
|
|
50
|
+
export interface IOptionsConstructor {
|
|
51
|
+
debug?: boolean;
|
|
52
|
+
allowExternalErrors?: boolean;
|
|
53
|
+
fallbackRule?: ShieldRule;
|
|
54
|
+
fallbackError?: string | IFallbackErrorType;
|
|
55
|
+
hashFunction?: IHashFunction;
|
|
56
|
+
}
|
|
57
|
+
export declare function shield<TSource = any, TContext = any, TArgs = any>(ruleTree: IRules, options: IOptions): IMiddlewareGenerator<TSource, TContext, TArgs>;
|
|
58
|
+
export interface IShieldContext {
|
|
59
|
+
_shield: {
|
|
60
|
+
cache: {
|
|
61
|
+
[key: string]: IRuleResult | Promise<IRuleResult>;
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { GraphQLResolveInfo } from 'graphql';
|
|
2
|
+
import { IMiddlewareGenerator } from 'graphql-middleware';
|
|
3
|
+
export declare type ShieldRule = IRule | ILogicRule;
|
|
4
|
+
export interface IRule {
|
|
5
|
+
readonly name: string;
|
|
6
|
+
equals(rule: IRule): boolean;
|
|
7
|
+
extractFragment(): IFragment | undefined;
|
|
8
|
+
resolve(parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo, options: IOptions): Promise<IRuleResult>;
|
|
9
|
+
}
|
|
10
|
+
export interface IRuleOptions {
|
|
11
|
+
cache: ICache;
|
|
12
|
+
fragment?: IFragment;
|
|
13
|
+
}
|
|
14
|
+
export interface ILogicRule {
|
|
15
|
+
getRules(): ShieldRule[];
|
|
16
|
+
extractFragments(): IFragment[];
|
|
17
|
+
evaluate(parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo, options: IOptions): Promise<IRuleResult[]>;
|
|
18
|
+
resolve(parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo, options: IOptions): Promise<IRuleResult>;
|
|
19
|
+
}
|
|
20
|
+
export declare type IFragment = string;
|
|
21
|
+
export declare type ICache = 'strict' | 'contextual' | 'no_cache' | ICacheKeyFn;
|
|
22
|
+
export declare type ICacheKeyFn = (parent: any, args: any, ctx: any, info: GraphQLResolveInfo) => string;
|
|
23
|
+
export declare type IRuleResult = boolean | string | Error;
|
|
24
|
+
export declare type IRuleFunction = (parent: any, args: any, ctx: any, info: GraphQLResolveInfo) => IRuleResult | Promise<IRuleResult>;
|
|
25
|
+
export declare type ICacheContructorOptions = ICache | boolean;
|
|
26
|
+
export interface IRuleConstructorOptions {
|
|
27
|
+
cache?: ICacheContructorOptions;
|
|
28
|
+
fragment?: IFragment;
|
|
29
|
+
}
|
|
30
|
+
export interface IRuleTypeMap {
|
|
31
|
+
[key: string]: ShieldRule | IRuleFieldMap;
|
|
32
|
+
}
|
|
33
|
+
export interface IRuleFieldMap {
|
|
34
|
+
[key: string]: ShieldRule;
|
|
35
|
+
}
|
|
36
|
+
export declare type IRules = ShieldRule | IRuleTypeMap;
|
|
37
|
+
export declare type IHashFunction = (arg: {
|
|
38
|
+
parent: any;
|
|
39
|
+
args: any;
|
|
40
|
+
}) => string;
|
|
41
|
+
export declare type IFallbackErrorMapperType = (err: unknown, parent: object, args: object, ctx: IShieldContext, info: GraphQLResolveInfo) => Promise<Error> | Error;
|
|
42
|
+
export declare type IFallbackErrorType = Error | IFallbackErrorMapperType;
|
|
43
|
+
export interface IOptions {
|
|
44
|
+
debug: boolean;
|
|
45
|
+
allowExternalErrors: boolean;
|
|
46
|
+
fallbackRule: ShieldRule;
|
|
47
|
+
fallbackError?: IFallbackErrorType;
|
|
48
|
+
hashFunction: IHashFunction;
|
|
49
|
+
}
|
|
50
|
+
export interface IOptionsConstructor {
|
|
51
|
+
debug?: boolean;
|
|
52
|
+
allowExternalErrors?: boolean;
|
|
53
|
+
fallbackRule?: ShieldRule;
|
|
54
|
+
fallbackError?: string | IFallbackErrorType;
|
|
55
|
+
hashFunction?: IHashFunction;
|
|
56
|
+
}
|
|
57
|
+
export declare function shield<TSource = any, TContext = any, TArgs = any>(ruleTree: IRules, options: IOptions): IMiddlewareGenerator<TSource, TContext, TArgs>;
|
|
58
|
+
export interface IShieldContext {
|
|
59
|
+
_shield: {
|
|
60
|
+
cache: {
|
|
61
|
+
[key: string]: IRuleResult | Promise<IRuleResult>;
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { ShieldRule, ILogicRule, IRuleFieldMap, IRule } from './types.cjs';
|
|
2
|
+
/**
|
|
3
|
+
*
|
|
4
|
+
* @param x
|
|
5
|
+
*
|
|
6
|
+
* Makes sure that a certain field is a rule.
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
export declare function isRule(x: any): x is IRule;
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
* @param x
|
|
13
|
+
*
|
|
14
|
+
* Makes sure that a certain field is a logic rule.
|
|
15
|
+
*
|
|
16
|
+
*/
|
|
17
|
+
export declare function isLogicRule(x: any): x is ILogicRule;
|
|
18
|
+
/**
|
|
19
|
+
*
|
|
20
|
+
* @param x
|
|
21
|
+
*
|
|
22
|
+
* Makes sure that a certain field is a rule or a logic rule.
|
|
23
|
+
*
|
|
24
|
+
*/
|
|
25
|
+
export declare function isRuleFunction(x: any): x is ShieldRule;
|
|
26
|
+
/**
|
|
27
|
+
*
|
|
28
|
+
* @param x
|
|
29
|
+
*
|
|
30
|
+
* Determines whether a certain field is rule field map or not.
|
|
31
|
+
*
|
|
32
|
+
*/
|
|
33
|
+
export declare function isRuleFieldMap(x: any): x is IRuleFieldMap;
|
|
34
|
+
/**
|
|
35
|
+
*
|
|
36
|
+
* @param obj
|
|
37
|
+
* @param func
|
|
38
|
+
*
|
|
39
|
+
* Flattens object of particular type by checking if the leaf
|
|
40
|
+
* evaluates to true from particular function.
|
|
41
|
+
*
|
|
42
|
+
*/
|
|
43
|
+
export declare function flattenObjectOf<T>(obj: {
|
|
44
|
+
[key: string]: any;
|
|
45
|
+
}, f: (x: any) => boolean): T[];
|
|
46
|
+
/**
|
|
47
|
+
*
|
|
48
|
+
* Returns fallback is provided value is undefined
|
|
49
|
+
*
|
|
50
|
+
* @param fallback
|
|
51
|
+
*/
|
|
52
|
+
export declare function withDefault<T>(fallback: T): (value: T | undefined) => T;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { ShieldRule, ILogicRule, IRuleFieldMap, IRule } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
*
|
|
4
|
+
* @param x
|
|
5
|
+
*
|
|
6
|
+
* Makes sure that a certain field is a rule.
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
export declare function isRule(x: any): x is IRule;
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
* @param x
|
|
13
|
+
*
|
|
14
|
+
* Makes sure that a certain field is a logic rule.
|
|
15
|
+
*
|
|
16
|
+
*/
|
|
17
|
+
export declare function isLogicRule(x: any): x is ILogicRule;
|
|
18
|
+
/**
|
|
19
|
+
*
|
|
20
|
+
* @param x
|
|
21
|
+
*
|
|
22
|
+
* Makes sure that a certain field is a rule or a logic rule.
|
|
23
|
+
*
|
|
24
|
+
*/
|
|
25
|
+
export declare function isRuleFunction(x: any): x is ShieldRule;
|
|
26
|
+
/**
|
|
27
|
+
*
|
|
28
|
+
* @param x
|
|
29
|
+
*
|
|
30
|
+
* Determines whether a certain field is rule field map or not.
|
|
31
|
+
*
|
|
32
|
+
*/
|
|
33
|
+
export declare function isRuleFieldMap(x: any): x is IRuleFieldMap;
|
|
34
|
+
/**
|
|
35
|
+
*
|
|
36
|
+
* @param obj
|
|
37
|
+
* @param func
|
|
38
|
+
*
|
|
39
|
+
* Flattens object of particular type by checking if the leaf
|
|
40
|
+
* evaluates to true from particular function.
|
|
41
|
+
*
|
|
42
|
+
*/
|
|
43
|
+
export declare function flattenObjectOf<T>(obj: {
|
|
44
|
+
[key: string]: any;
|
|
45
|
+
}, f: (x: any) => boolean): T[];
|
|
46
|
+
/**
|
|
47
|
+
*
|
|
48
|
+
* Returns fallback is provided value is undefined
|
|
49
|
+
*
|
|
50
|
+
* @param fallback
|
|
51
|
+
*/
|
|
52
|
+
export declare function withDefault<T>(fallback: T): (value: T | undefined) => T;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { IRules } from './types.cjs';
|
|
2
|
+
/**
|
|
3
|
+
*
|
|
4
|
+
* @param ruleTree
|
|
5
|
+
*
|
|
6
|
+
* Validates the rule tree declaration by checking references of rule
|
|
7
|
+
* functions. We deem rule tree valid if no two rules with the same name point
|
|
8
|
+
* to different rules.
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
export declare function validateRuleTree(ruleTree: IRules): {
|
|
12
|
+
status: 'ok';
|
|
13
|
+
} | {
|
|
14
|
+
status: 'err';
|
|
15
|
+
message: string;
|
|
16
|
+
};
|
|
17
|
+
export declare class ValidationError extends Error {
|
|
18
|
+
constructor(message: string);
|
|
19
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { IRules } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
*
|
|
4
|
+
* @param ruleTree
|
|
5
|
+
*
|
|
6
|
+
* Validates the rule tree declaration by checking references of rule
|
|
7
|
+
* functions. We deem rule tree valid if no two rules with the same name point
|
|
8
|
+
* to different rules.
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
export declare function validateRuleTree(ruleTree: IRules): {
|
|
12
|
+
status: 'ok';
|
|
13
|
+
} | {
|
|
14
|
+
status: 'err';
|
|
15
|
+
message: string;
|
|
16
|
+
};
|
|
17
|
+
export declare class ValidationError extends Error {
|
|
18
|
+
constructor(message: string);
|
|
19
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "graphql-shield-node23",
|
|
3
|
+
"description": "GraphQL Server permissions as another layer of abstraction!",
|
|
4
|
+
"version": "7.6.5",
|
|
5
|
+
"author": "Matic Zavadlal <matic.zavadlal@gmail.com>",
|
|
6
|
+
"homepage": "https://github.com/maticzav/graphql-shield",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/maticzav/graphql-shield.git"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"check": "tsc --noEmit"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@types/yup": "0.29.13",
|
|
16
|
+
"object-hash": "^3.0.0",
|
|
17
|
+
"tslib": "^2.4.0",
|
|
18
|
+
"yup": "^0.32.0"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@graphql-tools/schema": "8.5.1",
|
|
22
|
+
"@graphql-tools/utils": "8.9.0",
|
|
23
|
+
"@types/node": "18.6.3",
|
|
24
|
+
"@types/node-fetch": "^2.6.2",
|
|
25
|
+
"@types/object-hash": "2.2.1",
|
|
26
|
+
"@types/request-promise-native": "1.0.18",
|
|
27
|
+
"apollo-server": "3.10.0",
|
|
28
|
+
"graphql": "16.5.0",
|
|
29
|
+
"graphql-middleware": "6.1.33",
|
|
30
|
+
"graphql-shield-rules": "0.0.1",
|
|
31
|
+
"node-fetch": "^2.6.7"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"graphql": "^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0",
|
|
35
|
+
"graphql-middleware": "^2.0.0 || ^3.0.0 || ^4.0.0 || ^6.0.0"
|
|
36
|
+
},
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"main": "dist/cjs/index.js",
|
|
39
|
+
"module": "dist/esm/index.js",
|
|
40
|
+
"exports": {
|
|
41
|
+
".": {
|
|
42
|
+
"require": {
|
|
43
|
+
"types": "./dist/typings/index.d.cts",
|
|
44
|
+
"default": "./dist/cjs/index.js"
|
|
45
|
+
},
|
|
46
|
+
"import": {
|
|
47
|
+
"types": "./dist/typings/index.d.ts",
|
|
48
|
+
"default": "./dist/esm/index.js"
|
|
49
|
+
},
|
|
50
|
+
"default": {
|
|
51
|
+
"types": "./dist/typings/index.d.ts",
|
|
52
|
+
"default": "./dist/esm/index.js"
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"./package.json": "./package.json"
|
|
56
|
+
},
|
|
57
|
+
"typings": "dist/typings/index.d.ts",
|
|
58
|
+
"typescript": {
|
|
59
|
+
"definition": "dist/typings/index.d.ts"
|
|
60
|
+
},
|
|
61
|
+
"type": "module",
|
|
62
|
+
"sideEffects": false,
|
|
63
|
+
"publishConfig": {
|
|
64
|
+
"directory": "dist",
|
|
65
|
+
"access": "public"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import * as Yup from 'yup'
|
|
2
|
+
import {
|
|
3
|
+
IRuleFunction,
|
|
4
|
+
IRuleConstructorOptions,
|
|
5
|
+
ShieldRule,
|
|
6
|
+
IShieldContext,
|
|
7
|
+
} from './types.js'
|
|
8
|
+
import {
|
|
9
|
+
Rule,
|
|
10
|
+
RuleAnd,
|
|
11
|
+
RuleOr,
|
|
12
|
+
RuleNot,
|
|
13
|
+
RuleTrue,
|
|
14
|
+
RuleFalse,
|
|
15
|
+
InputRule,
|
|
16
|
+
RuleChain,
|
|
17
|
+
RuleRace,
|
|
18
|
+
} from './rules.js'
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
*
|
|
22
|
+
* @param name
|
|
23
|
+
* @param options
|
|
24
|
+
*
|
|
25
|
+
* Wraps a function into a Rule class. This way we can identify rules
|
|
26
|
+
* once we start generating middleware from our ruleTree.
|
|
27
|
+
*
|
|
28
|
+
* 1.
|
|
29
|
+
* const auth = rule()(async (parent, args, ctx, info) => {
|
|
30
|
+
* return true
|
|
31
|
+
* })
|
|
32
|
+
*
|
|
33
|
+
* 2.
|
|
34
|
+
* const auth = rule('name')(async (parent, args, ctx, info) => {
|
|
35
|
+
* return true
|
|
36
|
+
* })
|
|
37
|
+
*
|
|
38
|
+
* 3.
|
|
39
|
+
* const auth = rule({
|
|
40
|
+
* name: 'name',
|
|
41
|
+
* fragment: 'string',
|
|
42
|
+
* cache: 'cache',
|
|
43
|
+
* })(async (parent, args, ctx, info) => {
|
|
44
|
+
* return true
|
|
45
|
+
* })
|
|
46
|
+
*
|
|
47
|
+
*/
|
|
48
|
+
export const rule =
|
|
49
|
+
(
|
|
50
|
+
name?: string | IRuleConstructorOptions,
|
|
51
|
+
options?: IRuleConstructorOptions,
|
|
52
|
+
) =>
|
|
53
|
+
(func: IRuleFunction): Rule => {
|
|
54
|
+
if (typeof name === 'object') {
|
|
55
|
+
options = name
|
|
56
|
+
name = Math.random().toString()
|
|
57
|
+
} else if (typeof name === 'string') {
|
|
58
|
+
options = options || {}
|
|
59
|
+
} else {
|
|
60
|
+
name = Math.random().toString()
|
|
61
|
+
options = {}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return new Rule(name, func, {
|
|
65
|
+
fragment: options.fragment,
|
|
66
|
+
cache: options.cache,
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
*
|
|
72
|
+
* Constructs a new InputRule based on the schema.
|
|
73
|
+
*
|
|
74
|
+
* @param schema
|
|
75
|
+
*/
|
|
76
|
+
export const inputRule =
|
|
77
|
+
<T>(name?: string) =>
|
|
78
|
+
(
|
|
79
|
+
schema: (yup: typeof Yup, ctx: IShieldContext) => Yup.BaseSchema<T>,
|
|
80
|
+
options?: Parameters<Yup.BaseSchema<T>['validate']>[1],
|
|
81
|
+
) => {
|
|
82
|
+
if (typeof name === 'string') {
|
|
83
|
+
return new InputRule(name, schema, options)
|
|
84
|
+
} else {
|
|
85
|
+
return new InputRule(Math.random().toString(), schema, options)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
*
|
|
91
|
+
* @param rules
|
|
92
|
+
*
|
|
93
|
+
* Logical operator and serves as a wrapper for and operation.
|
|
94
|
+
*
|
|
95
|
+
*/
|
|
96
|
+
export const and = (...rules: ShieldRule[]): RuleAnd => {
|
|
97
|
+
return new RuleAnd(rules)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
*
|
|
102
|
+
* @param rules
|
|
103
|
+
*
|
|
104
|
+
* Logical operator and serves as a wrapper for and operation.
|
|
105
|
+
*
|
|
106
|
+
*/
|
|
107
|
+
export const chain = (...rules: ShieldRule[]): RuleChain => {
|
|
108
|
+
return new RuleChain(rules)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
*
|
|
113
|
+
* @param rules
|
|
114
|
+
*
|
|
115
|
+
* Logical operator and serves as a wrapper for and operation.
|
|
116
|
+
*
|
|
117
|
+
*/
|
|
118
|
+
export const race = (...rules: ShieldRule[]): RuleRace => {
|
|
119
|
+
return new RuleRace(rules)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
*
|
|
124
|
+
* @param rules
|
|
125
|
+
*
|
|
126
|
+
* Logical operator or serves as a wrapper for or operation.
|
|
127
|
+
*
|
|
128
|
+
*/
|
|
129
|
+
export const or = (...rules: ShieldRule[]): RuleOr => {
|
|
130
|
+
return new RuleOr(rules)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
*
|
|
135
|
+
* @param rule
|
|
136
|
+
*
|
|
137
|
+
* Logical operator not serves as a wrapper for not operation.
|
|
138
|
+
*
|
|
139
|
+
*/
|
|
140
|
+
export const not = (rule: ShieldRule, error?: string | Error): RuleNot => {
|
|
141
|
+
if (typeof error === 'string') return new RuleNot(rule, new Error(error))
|
|
142
|
+
return new RuleNot(rule, error)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
*
|
|
147
|
+
* Allow queries.
|
|
148
|
+
*
|
|
149
|
+
*/
|
|
150
|
+
export const allow = new RuleTrue()
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
*
|
|
154
|
+
* Deny queries.
|
|
155
|
+
*
|
|
156
|
+
*/
|
|
157
|
+
export const deny = new RuleFalse()
|