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,45 @@
|
|
|
1
|
+
import hash from 'object-hash';
|
|
2
|
+
import { middleware } from 'graphql-middleware';
|
|
3
|
+
import { ValidationError, validateRuleTree } from './validation.js';
|
|
4
|
+
import { generateMiddlewareGeneratorFromRuleTree } from './generator.js';
|
|
5
|
+
import { allow } from './constructors.js';
|
|
6
|
+
import { withDefault } from './utils.js';
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* @param options
|
|
10
|
+
*
|
|
11
|
+
* Makes sure all of defined rules are in accord with the options
|
|
12
|
+
* shield can process.
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
function normalizeOptions(options) {
|
|
16
|
+
if (typeof options.fallbackError === 'string') {
|
|
17
|
+
options.fallbackError = new Error(options.fallbackError);
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
debug: options.debug !== undefined ? options.debug : false,
|
|
21
|
+
allowExternalErrors: withDefault(false)(options.allowExternalErrors),
|
|
22
|
+
fallbackRule: withDefault(allow)(options.fallbackRule),
|
|
23
|
+
fallbackError: withDefault(new Error('Not Authorised!'))(options.fallbackError),
|
|
24
|
+
hashFunction: withDefault(hash)(options.hashFunction),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
*
|
|
29
|
+
* @param ruleTree
|
|
30
|
+
* @param options
|
|
31
|
+
*
|
|
32
|
+
* Validates rules and generates middleware from defined rule tree.
|
|
33
|
+
*
|
|
34
|
+
*/
|
|
35
|
+
export function shield(ruleTree, options = {}) {
|
|
36
|
+
const normalizedOptions = normalizeOptions(options);
|
|
37
|
+
const ruleTreeValidity = validateRuleTree(ruleTree);
|
|
38
|
+
if (ruleTreeValidity.status === 'ok') {
|
|
39
|
+
const generatorFunction = generateMiddlewareGeneratorFromRuleTree(ruleTree, normalizedOptions);
|
|
40
|
+
return middleware(generatorFunction);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
throw new ValidationError(ruleTreeValidity.message);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { Rule, LogicRule } from './rules.js';
|
|
2
|
+
/**
|
|
3
|
+
*
|
|
4
|
+
* @param x
|
|
5
|
+
*
|
|
6
|
+
* Makes sure that a certain field is a rule.
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
export function isRule(x) {
|
|
10
|
+
return (x instanceof Rule || (x && x.constructor && x.constructor.name === 'Rule'));
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* @param x
|
|
15
|
+
*
|
|
16
|
+
* Makes sure that a certain field is a logic rule.
|
|
17
|
+
*
|
|
18
|
+
*/
|
|
19
|
+
export function isLogicRule(x) {
|
|
20
|
+
return (x instanceof LogicRule ||
|
|
21
|
+
(x &&
|
|
22
|
+
x.constructor &&
|
|
23
|
+
(x.constructor.name === 'RuleOr' ||
|
|
24
|
+
x.constructor.name === 'RuleAnd' ||
|
|
25
|
+
x.constructor.name === 'RuleChain' ||
|
|
26
|
+
x.constructor.name === 'RuleRace' ||
|
|
27
|
+
x.constructor.name === 'RuleNot' ||
|
|
28
|
+
x.constructor.name === 'RuleTrue' ||
|
|
29
|
+
x.constructor.name === 'RuleFalse')));
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
*
|
|
33
|
+
* @param x
|
|
34
|
+
*
|
|
35
|
+
* Makes sure that a certain field is a rule or a logic rule.
|
|
36
|
+
*
|
|
37
|
+
*/
|
|
38
|
+
export function isRuleFunction(x) {
|
|
39
|
+
return isRule(x) || isLogicRule(x);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
*
|
|
43
|
+
* @param x
|
|
44
|
+
*
|
|
45
|
+
* Determines whether a certain field is rule field map or not.
|
|
46
|
+
*
|
|
47
|
+
*/
|
|
48
|
+
export function isRuleFieldMap(x) {
|
|
49
|
+
return (typeof x === 'object' &&
|
|
50
|
+
Object.values(x).every((rule) => isRuleFunction(rule)));
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
*
|
|
54
|
+
* @param obj
|
|
55
|
+
* @param func
|
|
56
|
+
*
|
|
57
|
+
* Flattens object of particular type by checking if the leaf
|
|
58
|
+
* evaluates to true from particular function.
|
|
59
|
+
*
|
|
60
|
+
*/
|
|
61
|
+
export function flattenObjectOf(obj, f) {
|
|
62
|
+
const values = Object.keys(obj).reduce((acc, key) => {
|
|
63
|
+
const val = obj[key];
|
|
64
|
+
if (f(val)) {
|
|
65
|
+
return [...acc, val];
|
|
66
|
+
}
|
|
67
|
+
else if (typeof val === 'object' && !f(val)) {
|
|
68
|
+
return [...acc, ...flattenObjectOf(val, f)];
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
return acc;
|
|
72
|
+
}
|
|
73
|
+
}, []);
|
|
74
|
+
return values;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
*
|
|
78
|
+
* Returns fallback is provided value is undefined
|
|
79
|
+
*
|
|
80
|
+
* @param fallback
|
|
81
|
+
*/
|
|
82
|
+
export function withDefault(fallback) {
|
|
83
|
+
return (value) => {
|
|
84
|
+
if (value === undefined)
|
|
85
|
+
return fallback;
|
|
86
|
+
return value;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { isRuleFunction, flattenObjectOf, isLogicRule } from './utils.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 function validateRuleTree(ruleTree) {
|
|
12
|
+
const rules = extractRules(ruleTree);
|
|
13
|
+
const valid = rules.reduce(({ map, duplicates }, rule) => {
|
|
14
|
+
if (!map.has(rule.name)) {
|
|
15
|
+
return { map: map.set(rule.name, rule), duplicates };
|
|
16
|
+
}
|
|
17
|
+
else if (!map.get(rule.name).equals(rule) &&
|
|
18
|
+
!duplicates.includes(rule.name)) {
|
|
19
|
+
return {
|
|
20
|
+
map: map.set(rule.name, rule),
|
|
21
|
+
duplicates: [...duplicates, rule.name],
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
return { map, duplicates };
|
|
26
|
+
}
|
|
27
|
+
}, { map: new Map(), duplicates: [] });
|
|
28
|
+
if (valid.duplicates.length === 0) {
|
|
29
|
+
return { status: 'ok' };
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
const duplicates = valid.duplicates.join(', ');
|
|
33
|
+
return {
|
|
34
|
+
status: 'err',
|
|
35
|
+
message: `There seem to be multiple definitions of these rules: ${duplicates}`,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/* Helper functions */
|
|
39
|
+
/**
|
|
40
|
+
*
|
|
41
|
+
* @param ruleTree
|
|
42
|
+
*
|
|
43
|
+
* Extracts rules from rule tree.
|
|
44
|
+
*
|
|
45
|
+
*/
|
|
46
|
+
function extractRules(ruleTree) {
|
|
47
|
+
const resolvers = flattenObjectOf(ruleTree, isRuleFunction);
|
|
48
|
+
const rules = resolvers.reduce((rules, rule) => {
|
|
49
|
+
if (isLogicRule(rule)) {
|
|
50
|
+
return [...rules, ...extractLogicRules(rule)];
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
return [...rules, rule];
|
|
54
|
+
}
|
|
55
|
+
}, []);
|
|
56
|
+
return rules;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
*
|
|
60
|
+
* Recursively extracts Rules from LogicRule
|
|
61
|
+
*
|
|
62
|
+
* @param rule
|
|
63
|
+
*/
|
|
64
|
+
function extractLogicRules(rule) {
|
|
65
|
+
return rule.getRules().reduce((acc, shieldRule) => {
|
|
66
|
+
if (isLogicRule(shieldRule)) {
|
|
67
|
+
return [...acc, ...extractLogicRules(shieldRule)];
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
return [...acc, shieldRule];
|
|
71
|
+
}
|
|
72
|
+
}, []);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
export class ValidationError extends Error {
|
|
76
|
+
constructor(message) {
|
|
77
|
+
super(message);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@szofon/graphql-shield-node23",
|
|
3
|
+
"version": "7.6.5",
|
|
4
|
+
"description": "GraphQL Server permissions as another layer of abstraction!",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"peerDependencies": {
|
|
7
|
+
"graphql": "^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0",
|
|
8
|
+
"graphql-middleware": "^2.0.0 || ^3.0.0 || ^4.0.0 || ^6.0.0"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@types/yup": "0.29.13",
|
|
12
|
+
"object-hash": "^3.0.0",
|
|
13
|
+
"tslib": "^2.4.0",
|
|
14
|
+
"yup": "^0.32.0"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/maticzav/graphql-shield.git"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/maticzav/graphql-shield",
|
|
21
|
+
"author": "Matic Zavadlal <matic.zavadlal@gmail.com>",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"main": "cjs/index.js",
|
|
24
|
+
"module": "esm/index.js",
|
|
25
|
+
"typings": "typings/index.d.ts",
|
|
26
|
+
"typescript": {
|
|
27
|
+
"definition": "typings/index.d.ts"
|
|
28
|
+
},
|
|
29
|
+
"type": "module",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"require": {
|
|
33
|
+
"types": "./typings/index.d.cts",
|
|
34
|
+
"default": "./cjs/index.js"
|
|
35
|
+
},
|
|
36
|
+
"import": {
|
|
37
|
+
"types": "./typings/index.d.ts",
|
|
38
|
+
"default": "./esm/index.js"
|
|
39
|
+
},
|
|
40
|
+
"default": {
|
|
41
|
+
"types": "./typings/index.d.ts",
|
|
42
|
+
"default": "./esm/index.js"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"./package.json": "./package.json"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import * as Yup from 'yup';
|
|
2
|
+
import { IRuleFunction, IRuleConstructorOptions, ShieldRule, IShieldContext } from './types.cjs';
|
|
3
|
+
import { Rule, RuleAnd, RuleOr, RuleNot, RuleTrue, RuleFalse, InputRule, RuleChain, RuleRace } from './rules.cjs';
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* @param name
|
|
7
|
+
* @param options
|
|
8
|
+
*
|
|
9
|
+
* Wraps a function into a Rule class. This way we can identify rules
|
|
10
|
+
* once we start generating middleware from our ruleTree.
|
|
11
|
+
*
|
|
12
|
+
* 1.
|
|
13
|
+
* const auth = rule()(async (parent, args, ctx, info) => {
|
|
14
|
+
* return true
|
|
15
|
+
* })
|
|
16
|
+
*
|
|
17
|
+
* 2.
|
|
18
|
+
* const auth = rule('name')(async (parent, args, ctx, info) => {
|
|
19
|
+
* return true
|
|
20
|
+
* })
|
|
21
|
+
*
|
|
22
|
+
* 3.
|
|
23
|
+
* const auth = rule({
|
|
24
|
+
* name: 'name',
|
|
25
|
+
* fragment: 'string',
|
|
26
|
+
* cache: 'cache',
|
|
27
|
+
* })(async (parent, args, ctx, info) => {
|
|
28
|
+
* return true
|
|
29
|
+
* })
|
|
30
|
+
*
|
|
31
|
+
*/
|
|
32
|
+
export declare const rule: (name?: string | IRuleConstructorOptions, options?: IRuleConstructorOptions) => (func: IRuleFunction) => Rule;
|
|
33
|
+
/**
|
|
34
|
+
*
|
|
35
|
+
* Constructs a new InputRule based on the schema.
|
|
36
|
+
*
|
|
37
|
+
* @param schema
|
|
38
|
+
*/
|
|
39
|
+
export declare const inputRule: <T>(name?: string) => (schema: (yup: typeof Yup, ctx: IShieldContext) => Yup.BaseSchema<T, import("yup/lib/types.js").AnyObject, any>, options?: import("yup/lib/types.js").ValidateOptions<import("yup/lib/types.js").AnyObject> | undefined) => InputRule<T>;
|
|
40
|
+
/**
|
|
41
|
+
*
|
|
42
|
+
* @param rules
|
|
43
|
+
*
|
|
44
|
+
* Logical operator and serves as a wrapper for and operation.
|
|
45
|
+
*
|
|
46
|
+
*/
|
|
47
|
+
export declare const and: (...rules: ShieldRule[]) => RuleAnd;
|
|
48
|
+
/**
|
|
49
|
+
*
|
|
50
|
+
* @param rules
|
|
51
|
+
*
|
|
52
|
+
* Logical operator and serves as a wrapper for and operation.
|
|
53
|
+
*
|
|
54
|
+
*/
|
|
55
|
+
export declare const chain: (...rules: ShieldRule[]) => RuleChain;
|
|
56
|
+
/**
|
|
57
|
+
*
|
|
58
|
+
* @param rules
|
|
59
|
+
*
|
|
60
|
+
* Logical operator and serves as a wrapper for and operation.
|
|
61
|
+
*
|
|
62
|
+
*/
|
|
63
|
+
export declare const race: (...rules: ShieldRule[]) => RuleRace;
|
|
64
|
+
/**
|
|
65
|
+
*
|
|
66
|
+
* @param rules
|
|
67
|
+
*
|
|
68
|
+
* Logical operator or serves as a wrapper for or operation.
|
|
69
|
+
*
|
|
70
|
+
*/
|
|
71
|
+
export declare const or: (...rules: ShieldRule[]) => RuleOr;
|
|
72
|
+
/**
|
|
73
|
+
*
|
|
74
|
+
* @param rule
|
|
75
|
+
*
|
|
76
|
+
* Logical operator not serves as a wrapper for not operation.
|
|
77
|
+
*
|
|
78
|
+
*/
|
|
79
|
+
export declare const not: (rule: ShieldRule, error?: string | Error) => RuleNot;
|
|
80
|
+
/**
|
|
81
|
+
*
|
|
82
|
+
* Allow queries.
|
|
83
|
+
*
|
|
84
|
+
*/
|
|
85
|
+
export declare const allow: RuleTrue;
|
|
86
|
+
/**
|
|
87
|
+
*
|
|
88
|
+
* Deny queries.
|
|
89
|
+
*
|
|
90
|
+
*/
|
|
91
|
+
export declare const deny: RuleFalse;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import * as Yup from 'yup';
|
|
2
|
+
import { IRuleFunction, IRuleConstructorOptions, ShieldRule, IShieldContext } from './types.js';
|
|
3
|
+
import { Rule, RuleAnd, RuleOr, RuleNot, RuleTrue, RuleFalse, InputRule, RuleChain, RuleRace } from './rules.js';
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* @param name
|
|
7
|
+
* @param options
|
|
8
|
+
*
|
|
9
|
+
* Wraps a function into a Rule class. This way we can identify rules
|
|
10
|
+
* once we start generating middleware from our ruleTree.
|
|
11
|
+
*
|
|
12
|
+
* 1.
|
|
13
|
+
* const auth = rule()(async (parent, args, ctx, info) => {
|
|
14
|
+
* return true
|
|
15
|
+
* })
|
|
16
|
+
*
|
|
17
|
+
* 2.
|
|
18
|
+
* const auth = rule('name')(async (parent, args, ctx, info) => {
|
|
19
|
+
* return true
|
|
20
|
+
* })
|
|
21
|
+
*
|
|
22
|
+
* 3.
|
|
23
|
+
* const auth = rule({
|
|
24
|
+
* name: 'name',
|
|
25
|
+
* fragment: 'string',
|
|
26
|
+
* cache: 'cache',
|
|
27
|
+
* })(async (parent, args, ctx, info) => {
|
|
28
|
+
* return true
|
|
29
|
+
* })
|
|
30
|
+
*
|
|
31
|
+
*/
|
|
32
|
+
export declare const rule: (name?: string | IRuleConstructorOptions, options?: IRuleConstructorOptions) => (func: IRuleFunction) => Rule;
|
|
33
|
+
/**
|
|
34
|
+
*
|
|
35
|
+
* Constructs a new InputRule based on the schema.
|
|
36
|
+
*
|
|
37
|
+
* @param schema
|
|
38
|
+
*/
|
|
39
|
+
export declare const inputRule: <T>(name?: string) => (schema: (yup: typeof Yup, ctx: IShieldContext) => Yup.BaseSchema<T, import("yup/lib/types.js").AnyObject, any>, options?: import("yup/lib/types.js").ValidateOptions<import("yup/lib/types.js").AnyObject> | undefined) => InputRule<T>;
|
|
40
|
+
/**
|
|
41
|
+
*
|
|
42
|
+
* @param rules
|
|
43
|
+
*
|
|
44
|
+
* Logical operator and serves as a wrapper for and operation.
|
|
45
|
+
*
|
|
46
|
+
*/
|
|
47
|
+
export declare const and: (...rules: ShieldRule[]) => RuleAnd;
|
|
48
|
+
/**
|
|
49
|
+
*
|
|
50
|
+
* @param rules
|
|
51
|
+
*
|
|
52
|
+
* Logical operator and serves as a wrapper for and operation.
|
|
53
|
+
*
|
|
54
|
+
*/
|
|
55
|
+
export declare const chain: (...rules: ShieldRule[]) => RuleChain;
|
|
56
|
+
/**
|
|
57
|
+
*
|
|
58
|
+
* @param rules
|
|
59
|
+
*
|
|
60
|
+
* Logical operator and serves as a wrapper for and operation.
|
|
61
|
+
*
|
|
62
|
+
*/
|
|
63
|
+
export declare const race: (...rules: ShieldRule[]) => RuleRace;
|
|
64
|
+
/**
|
|
65
|
+
*
|
|
66
|
+
* @param rules
|
|
67
|
+
*
|
|
68
|
+
* Logical operator or serves as a wrapper for or operation.
|
|
69
|
+
*
|
|
70
|
+
*/
|
|
71
|
+
export declare const or: (...rules: ShieldRule[]) => RuleOr;
|
|
72
|
+
/**
|
|
73
|
+
*
|
|
74
|
+
* @param rule
|
|
75
|
+
*
|
|
76
|
+
* Logical operator not serves as a wrapper for not operation.
|
|
77
|
+
*
|
|
78
|
+
*/
|
|
79
|
+
export declare const not: (rule: ShieldRule, error?: string | Error) => RuleNot;
|
|
80
|
+
/**
|
|
81
|
+
*
|
|
82
|
+
* Allow queries.
|
|
83
|
+
*
|
|
84
|
+
*/
|
|
85
|
+
export declare const allow: RuleTrue;
|
|
86
|
+
/**
|
|
87
|
+
*
|
|
88
|
+
* Deny queries.
|
|
89
|
+
*
|
|
90
|
+
*/
|
|
91
|
+
export declare const deny: RuleFalse;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { IMiddlewareGeneratorConstructor } from 'graphql-middleware';
|
|
2
|
+
import { IRules, IOptions } from './types.cjs';
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param ruleTree
|
|
6
|
+
* @param options
|
|
7
|
+
*
|
|
8
|
+
* Generates middleware from given rules.
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
export declare function generateMiddlewareGeneratorFromRuleTree<TSource = any, TContext = any, TArgs = any>(ruleTree: IRules, options: IOptions): IMiddlewareGeneratorConstructor<TSource, TContext, TArgs>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { IMiddlewareGeneratorConstructor } from 'graphql-middleware';
|
|
2
|
+
import { IRules, IOptions } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param ruleTree
|
|
6
|
+
* @param options
|
|
7
|
+
*
|
|
8
|
+
* Generates middleware from given rules.
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
export declare function generateMiddlewareGeneratorFromRuleTree<TSource = any, TContext = any, TArgs = any>(ruleTree: IRules, options: IOptions): IMiddlewareGeneratorConstructor<TSource, TContext, TArgs>;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import * as Yup from 'yup';
|
|
2
|
+
import { IRuleFunction, IRule, IFragment, IRuleConstructorOptions, ILogicRule, ShieldRule, IRuleResult, IOptions, IShieldContext } from './types.cjs';
|
|
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
|
+
}
|