@stryke/trpc-next 0.5.48 → 0.5.50
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 +17 -0
- package/dist/_virtual/rolldown_runtime.cjs +29 -1
- package/dist/action-handler.cjs +20 -1
- package/dist/action-handler.mjs +18 -1
- package/dist/action-handler.mjs.map +1 -1
- package/dist/client.cjs +39 -1
- package/dist/client.mjs +37 -1
- package/dist/client.mjs.map +1 -1
- package/dist/env/src/ci-checks.cjs +13 -1
- package/dist/env/src/ci-checks.mjs +12 -1
- package/dist/env/src/ci-checks.mjs.map +1 -1
- package/dist/env/src/environment-checks.cjs +87 -1
- package/dist/env/src/environment-checks.mjs +87 -1
- package/dist/env/src/environment-checks.mjs.map +1 -1
- package/dist/index.cjs +30 -1
- package/dist/index.mjs +8 -1
- package/dist/path/src/is-type.cjs +28 -1
- package/dist/path/src/is-type.mjs +28 -1
- package/dist/path/src/is-type.mjs.map +1 -1
- package/dist/path/src/join-paths.cjs +106 -1
- package/dist/path/src/join-paths.mjs +106 -1
- package/dist/path/src/join-paths.mjs.map +1 -1
- package/dist/path/src/regex.cjs +12 -1
- package/dist/path/src/regex.mjs +8 -1
- package/dist/path/src/regex.mjs.map +1 -1
- package/dist/path/src/slash.cjs +15 -1
- package/dist/path/src/slash.mjs +14 -1
- package/dist/path/src/slash.mjs.map +1 -1
- package/dist/server.cjs +46 -1
- package/dist/server.mjs +33 -1
- package/dist/server.mjs.map +1 -1
- package/dist/shared.cjs +43 -1
- package/dist/shared.mjs +38 -1
- package/dist/shared.mjs.map +1 -1
- package/dist/shield/constructors.cjs +86 -1
- package/dist/shield/constructors.mjs +79 -1
- package/dist/shield/constructors.mjs.map +1 -1
- package/dist/shield/generator.cjs +28 -1
- package/dist/shield/generator.mjs +27 -1
- package/dist/shield/generator.mjs.map +1 -1
- package/dist/shield/index.cjs +12 -1
- package/dist/shield/index.mjs +4 -1
- package/dist/shield/rules.cjs +200 -1
- package/dist/shield/rules.mjs +191 -1
- package/dist/shield/rules.mjs.map +1 -1
- package/dist/shield/shield.cjs +31 -1
- package/dist/shield/shield.mjs +31 -1
- package/dist/shield/shield.mjs.map +1 -1
- package/dist/shield/utils.cjs +56 -1
- package/dist/shield/utils.mjs +51 -1
- package/dist/shield/utils.mjs.map +1 -1
- package/dist/shield/validation.cjs +59 -1
- package/dist/shield/validation.mjs +58 -1
- package/dist/shield/validation.mjs.map +1 -1
- package/dist/tanstack-query/client.cjs +42 -1
- package/dist/tanstack-query/client.mjs +41 -1
- package/dist/tanstack-query/client.mjs.map +1 -1
- package/dist/tanstack-query/server.cjs +54 -1
- package/dist/tanstack-query/server.mjs +51 -1
- package/dist/tanstack-query/server.mjs.map +1 -1
- package/package.json +2 -2
|
@@ -1 +1,86 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_shield_rules = require('./rules.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/shield/constructors.ts
|
|
4
|
+
/**
|
|
5
|
+
* Wraps a function into a Rule class. This way we can identify rules
|
|
6
|
+
* once we start generating middleware from our ruleTree.
|
|
7
|
+
*
|
|
8
|
+
* ```
|
|
9
|
+
* // 1.
|
|
10
|
+
* const auth = rule()(async (ctx, type, path, input, rawInput, options) => {
|
|
11
|
+
* return true
|
|
12
|
+
* })
|
|
13
|
+
*
|
|
14
|
+
* // 2.
|
|
15
|
+
* const auth = rule('name')(async (ctx, type, path, input, rawInput, options) => {
|
|
16
|
+
* return true
|
|
17
|
+
* })
|
|
18
|
+
*
|
|
19
|
+
* // 3.
|
|
20
|
+
* const auth = rule({
|
|
21
|
+
* name: 'name',
|
|
22
|
+
* })(async (ctx, type, path, input, rawInput, options) => {
|
|
23
|
+
* return true
|
|
24
|
+
* })
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
const rule = (name, options) => (func) => {
|
|
28
|
+
if (typeof name === "object") {
|
|
29
|
+
options = name;
|
|
30
|
+
name = Math.random().toString();
|
|
31
|
+
} else if (typeof name === "string") options = options ?? {};
|
|
32
|
+
else {
|
|
33
|
+
name = Math.random().toString();
|
|
34
|
+
options = {};
|
|
35
|
+
}
|
|
36
|
+
return new require_shield_rules.Rule(name, func, {});
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Logical operator and serves as a wrapper for and operation.
|
|
40
|
+
*/
|
|
41
|
+
const and = (...rules) => {
|
|
42
|
+
return new require_shield_rules.RuleAnd(rules);
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Logical operator and serves as a wrapper for and operation.
|
|
46
|
+
*/
|
|
47
|
+
const chain = (...rules) => {
|
|
48
|
+
return new require_shield_rules.RuleChain(rules);
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Logical operator and serves as a wrapper for and operation.
|
|
52
|
+
*/
|
|
53
|
+
const race = (...rules) => {
|
|
54
|
+
return new require_shield_rules.RuleRace(rules);
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Logical operator or serves as a wrapper for or operation.
|
|
58
|
+
*/
|
|
59
|
+
const or = (...rules) => {
|
|
60
|
+
return new require_shield_rules.RuleOr(rules);
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Logical operator not serves as a wrapper for not operation.
|
|
64
|
+
*/
|
|
65
|
+
const not = (rule$1, error) => {
|
|
66
|
+
if (typeof error === "string") return new require_shield_rules.RuleNot(rule$1, new Error(error));
|
|
67
|
+
return new require_shield_rules.RuleNot(rule$1, error);
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Allow queries.
|
|
71
|
+
*/
|
|
72
|
+
const allow = new require_shield_rules.RuleTrue();
|
|
73
|
+
/**
|
|
74
|
+
* Deny queries.
|
|
75
|
+
*/
|
|
76
|
+
const deny = new require_shield_rules.RuleFalse();
|
|
77
|
+
|
|
78
|
+
//#endregion
|
|
79
|
+
exports.allow = allow;
|
|
80
|
+
exports.and = and;
|
|
81
|
+
exports.chain = chain;
|
|
82
|
+
exports.deny = deny;
|
|
83
|
+
exports.not = not;
|
|
84
|
+
exports.or = or;
|
|
85
|
+
exports.race = race;
|
|
86
|
+
exports.rule = rule;
|
|
@@ -1,2 +1,80 @@
|
|
|
1
|
-
import{Rule
|
|
1
|
+
import { Rule, RuleAnd, RuleChain, RuleFalse, RuleNot, RuleOr, RuleRace, RuleTrue } from "./rules.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/shield/constructors.ts
|
|
4
|
+
/**
|
|
5
|
+
* Wraps a function into a Rule class. This way we can identify rules
|
|
6
|
+
* once we start generating middleware from our ruleTree.
|
|
7
|
+
*
|
|
8
|
+
* ```
|
|
9
|
+
* // 1.
|
|
10
|
+
* const auth = rule()(async (ctx, type, path, input, rawInput, options) => {
|
|
11
|
+
* return true
|
|
12
|
+
* })
|
|
13
|
+
*
|
|
14
|
+
* // 2.
|
|
15
|
+
* const auth = rule('name')(async (ctx, type, path, input, rawInput, options) => {
|
|
16
|
+
* return true
|
|
17
|
+
* })
|
|
18
|
+
*
|
|
19
|
+
* // 3.
|
|
20
|
+
* const auth = rule({
|
|
21
|
+
* name: 'name',
|
|
22
|
+
* })(async (ctx, type, path, input, rawInput, options) => {
|
|
23
|
+
* return true
|
|
24
|
+
* })
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
const rule = (name, options) => (func) => {
|
|
28
|
+
if (typeof name === "object") {
|
|
29
|
+
options = name;
|
|
30
|
+
name = Math.random().toString();
|
|
31
|
+
} else if (typeof name === "string") options = options ?? {};
|
|
32
|
+
else {
|
|
33
|
+
name = Math.random().toString();
|
|
34
|
+
options = {};
|
|
35
|
+
}
|
|
36
|
+
return new Rule(name, func, {});
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Logical operator and serves as a wrapper for and operation.
|
|
40
|
+
*/
|
|
41
|
+
const and = (...rules) => {
|
|
42
|
+
return new RuleAnd(rules);
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Logical operator and serves as a wrapper for and operation.
|
|
46
|
+
*/
|
|
47
|
+
const chain = (...rules) => {
|
|
48
|
+
return new RuleChain(rules);
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Logical operator and serves as a wrapper for and operation.
|
|
52
|
+
*/
|
|
53
|
+
const race = (...rules) => {
|
|
54
|
+
return new RuleRace(rules);
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Logical operator or serves as a wrapper for or operation.
|
|
58
|
+
*/
|
|
59
|
+
const or = (...rules) => {
|
|
60
|
+
return new RuleOr(rules);
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Logical operator not serves as a wrapper for not operation.
|
|
64
|
+
*/
|
|
65
|
+
const not = (rule$1, error) => {
|
|
66
|
+
if (typeof error === "string") return new RuleNot(rule$1, new Error(error));
|
|
67
|
+
return new RuleNot(rule$1, error);
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Allow queries.
|
|
71
|
+
*/
|
|
72
|
+
const allow = new RuleTrue();
|
|
73
|
+
/**
|
|
74
|
+
* Deny queries.
|
|
75
|
+
*/
|
|
76
|
+
const deny = new RuleFalse();
|
|
77
|
+
|
|
78
|
+
//#endregion
|
|
79
|
+
export { allow, and, chain, deny, not, or, race, rule };
|
|
2
80
|
//# sourceMappingURL=constructors.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constructors.mjs","names":["rule"],"sources":["../../src/shield/constructors.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport {\n Rule,\n RuleAnd,\n RuleChain,\n RuleFalse,\n RuleNot,\n RuleOr,\n RuleRace,\n RuleTrue\n} from \"./rules\";\nimport type {\n RuleConstructorOptionsInterface,\n RuleFunctionInterface,\n ShieldRule\n} from \"./types\";\n\n/**\n * Wraps a function into a Rule class. This way we can identify rules\n * once we start generating middleware from our ruleTree.\n *\n * ```\n * // 1.\n * const auth = rule()(async (ctx, type, path, input, rawInput, options) => {\n * return true\n * })\n *\n * // 2.\n * const auth = rule('name')(async (ctx, type, path, input, rawInput, options) => {\n * return true\n * })\n *\n * // 3.\n * const auth = rule({\n * name: 'name',\n * })(async (ctx, type, path, input, rawInput, options) => {\n * return true\n * })\n * ```\n */\nexport const rule =\n <TContext extends Record<string, any>>(\n name?: string,\n options?: RuleConstructorOptionsInterface\n ) =>\n (func: RuleFunctionInterface<TContext>): Rule<TContext> => {\n if (typeof name === \"object\") {\n options = name;\n name = Math.random().toString();\n } else if (typeof name === \"string\") {\n options = options ?? {};\n } else {\n name = Math.random().toString();\n options = {};\n }\n\n // @ts-ignore\n return new Rule(name, func, {});\n };\n\n/**\n * Logical operator and serves as a wrapper for and operation.\n */\nexport const and = <TContext extends Record<string, any>>(\n ...rules: ShieldRule<TContext>[]\n): RuleAnd<TContext> => {\n return new RuleAnd(rules);\n};\n\n/**\n * Logical operator and serves as a wrapper for and operation.\n */\nexport const chain = <TContext extends Record<string, any>>(\n ...rules: ShieldRule<TContext>[]\n): RuleChain<TContext> => {\n return new RuleChain(rules);\n};\n\n/**\n * Logical operator and serves as a wrapper for and operation.\n */\nexport const race = <TContext extends Record<string, any>>(\n ...rules: ShieldRule<TContext>[]\n): RuleRace<TContext> => {\n return new RuleRace(rules);\n};\n\n/**\n * Logical operator or serves as a wrapper for or operation.\n */\nexport const or = <TContext extends Record<string, any>>(\n ...rules: ShieldRule<TContext>[]\n): RuleOr<TContext> => {\n return new RuleOr(rules);\n};\n\n/**\n * Logical operator not serves as a wrapper for not operation.\n */\nexport const not = <TContext extends Record<string, any>>(\n rule: ShieldRule<TContext>,\n error?: string | Error\n): RuleNot<TContext> => {\n if (typeof error === \"string\") return new RuleNot(rule, new Error(error));\n return new RuleNot(rule, error);\n};\n\n/**\n * Allow queries.\n */\nexport const allow = new RuleTrue();\n\n/**\n * Deny queries.\n */\nexport const deny = new RuleFalse();\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"constructors.mjs","names":["rule"],"sources":["../../src/shield/constructors.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport {\n Rule,\n RuleAnd,\n RuleChain,\n RuleFalse,\n RuleNot,\n RuleOr,\n RuleRace,\n RuleTrue\n} from \"./rules\";\nimport type {\n RuleConstructorOptionsInterface,\n RuleFunctionInterface,\n ShieldRule\n} from \"./types\";\n\n/**\n * Wraps a function into a Rule class. This way we can identify rules\n * once we start generating middleware from our ruleTree.\n *\n * ```\n * // 1.\n * const auth = rule()(async (ctx, type, path, input, rawInput, options) => {\n * return true\n * })\n *\n * // 2.\n * const auth = rule('name')(async (ctx, type, path, input, rawInput, options) => {\n * return true\n * })\n *\n * // 3.\n * const auth = rule({\n * name: 'name',\n * })(async (ctx, type, path, input, rawInput, options) => {\n * return true\n * })\n * ```\n */\nexport const rule =\n <TContext extends Record<string, any>>(\n name?: string,\n options?: RuleConstructorOptionsInterface\n ) =>\n (func: RuleFunctionInterface<TContext>): Rule<TContext> => {\n if (typeof name === \"object\") {\n options = name;\n name = Math.random().toString();\n } else if (typeof name === \"string\") {\n options = options ?? {};\n } else {\n name = Math.random().toString();\n options = {};\n }\n\n // @ts-ignore\n return new Rule(name, func, {});\n };\n\n/**\n * Logical operator and serves as a wrapper for and operation.\n */\nexport const and = <TContext extends Record<string, any>>(\n ...rules: ShieldRule<TContext>[]\n): RuleAnd<TContext> => {\n return new RuleAnd(rules);\n};\n\n/**\n * Logical operator and serves as a wrapper for and operation.\n */\nexport const chain = <TContext extends Record<string, any>>(\n ...rules: ShieldRule<TContext>[]\n): RuleChain<TContext> => {\n return new RuleChain(rules);\n};\n\n/**\n * Logical operator and serves as a wrapper for and operation.\n */\nexport const race = <TContext extends Record<string, any>>(\n ...rules: ShieldRule<TContext>[]\n): RuleRace<TContext> => {\n return new RuleRace(rules);\n};\n\n/**\n * Logical operator or serves as a wrapper for or operation.\n */\nexport const or = <TContext extends Record<string, any>>(\n ...rules: ShieldRule<TContext>[]\n): RuleOr<TContext> => {\n return new RuleOr(rules);\n};\n\n/**\n * Logical operator not serves as a wrapper for not operation.\n */\nexport const not = <TContext extends Record<string, any>>(\n rule: ShieldRule<TContext>,\n error?: string | Error\n): RuleNot<TContext> => {\n if (typeof error === \"string\") return new RuleNot(rule, new Error(error));\n return new RuleNot(rule, error);\n};\n\n/**\n * Allow queries.\n */\nexport const allow = new RuleTrue();\n\n/**\n * Deny queries.\n */\nexport const deny = new RuleFalse();\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAyDA,MAAa,QAET,MACA,aAED,SAA0D;AACzD,KAAI,OAAO,SAAS,UAAU;AAC5B,YAAU;AACV,SAAO,KAAK,QAAQ,CAAC,UAAU;YACtB,OAAO,SAAS,SACzB,WAAU,WAAW,EAAE;MAClB;AACL,SAAO,KAAK,QAAQ,CAAC,UAAU;AAC/B,YAAU,EAAE;;AAId,QAAO,IAAI,KAAK,MAAM,MAAM,EAAE,CAAC;;;;;AAMnC,MAAa,OACX,GAAG,UACmB;AACtB,QAAO,IAAI,QAAQ,MAAM;;;;;AAM3B,MAAa,SACX,GAAG,UACqB;AACxB,QAAO,IAAI,UAAU,MAAM;;;;;AAM7B,MAAa,QACX,GAAG,UACoB;AACvB,QAAO,IAAI,SAAS,MAAM;;;;;AAM5B,MAAa,MACX,GAAG,UACkB;AACrB,QAAO,IAAI,OAAO,MAAM;;;;;AAM1B,MAAa,OACX,QACA,UACsB;AACtB,KAAI,OAAO,UAAU,SAAU,QAAO,IAAI,QAAQA,QAAM,IAAI,MAAM,MAAM,CAAC;AACzE,QAAO,IAAI,QAAQA,QAAM,MAAM;;;;;AAMjC,MAAa,QAAQ,IAAI,UAAU;;;;AAKnC,MAAa,OAAO,IAAI,WAAW"}
|
|
@@ -1 +1,28 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
//#region src/shield/generator.ts
|
|
3
|
+
/**
|
|
4
|
+
* Generates middleware from given rules.
|
|
5
|
+
*/
|
|
6
|
+
function generateMiddlewareFromRuleTree(ruleTree, options) {
|
|
7
|
+
return async ({ next, ctx, type, path, input, rawInput }) => {
|
|
8
|
+
const opWithPath = path.split(".");
|
|
9
|
+
const opName = opWithPath[opWithPath.length - 1];
|
|
10
|
+
const keys = Object.keys(ruleTree);
|
|
11
|
+
let rule;
|
|
12
|
+
if (keys.includes("query") || keys.includes("mutation")) rule = ruleTree?.[type]?.[opName];
|
|
13
|
+
else {
|
|
14
|
+
const tree = ruleTree[opWithPath[0]];
|
|
15
|
+
if (tree?.[type]?.[opName]) rule = tree?.[type]?.[opName];
|
|
16
|
+
}
|
|
17
|
+
rule = rule ?? options.fallbackRule;
|
|
18
|
+
if (rule) return rule?.resolve(ctx, type, path, input, rawInput, options).then(async (result) => {
|
|
19
|
+
if (result instanceof Error) throw result;
|
|
20
|
+
if (!result) throw options.fallbackError;
|
|
21
|
+
return next();
|
|
22
|
+
});
|
|
23
|
+
return next();
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
exports.generateMiddlewareFromRuleTree = generateMiddlewareFromRuleTree;
|
|
@@ -1,2 +1,28 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/shield/generator.ts
|
|
2
|
+
/**
|
|
3
|
+
* Generates middleware from given rules.
|
|
4
|
+
*/
|
|
5
|
+
function generateMiddlewareFromRuleTree(ruleTree, options) {
|
|
6
|
+
return async ({ next, ctx, type, path, input, rawInput }) => {
|
|
7
|
+
const opWithPath = path.split(".");
|
|
8
|
+
const opName = opWithPath[opWithPath.length - 1];
|
|
9
|
+
const keys = Object.keys(ruleTree);
|
|
10
|
+
let rule;
|
|
11
|
+
if (keys.includes("query") || keys.includes("mutation")) rule = ruleTree?.[type]?.[opName];
|
|
12
|
+
else {
|
|
13
|
+
const tree = ruleTree[opWithPath[0]];
|
|
14
|
+
if (tree?.[type]?.[opName]) rule = tree?.[type]?.[opName];
|
|
15
|
+
}
|
|
16
|
+
rule = rule ?? options.fallbackRule;
|
|
17
|
+
if (rule) return rule?.resolve(ctx, type, path, input, rawInput, options).then(async (result) => {
|
|
18
|
+
if (result instanceof Error) throw result;
|
|
19
|
+
if (!result) throw options.fallbackError;
|
|
20
|
+
return next();
|
|
21
|
+
});
|
|
22
|
+
return next();
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
export { generateMiddlewareFromRuleTree };
|
|
2
28
|
//# sourceMappingURL=generator.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generator.mjs","names":["opWithPath: Array<string>","opName: string","rule: ShieldRule<TContext> | undefined"],"sources":["../../src/shield/generator.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type { IRules, OptionsInterface, ShieldRule } from \"./types\";\n\n/**\n * Generates middleware from given rules.\n */\nexport function generateMiddlewareFromRuleTree<\n TContext extends Record<string, unknown>\n>(ruleTree: IRules<TContext>, options: OptionsInterface<TContext>) {\n return async ({\n next,\n ctx,\n type,\n path,\n input,\n rawInput\n }: {\n next: () => Promise<any>;\n ctx: TContext;\n type: string;\n path: string;\n input: { [name: string]: any };\n rawInput: unknown;\n }) => {\n const opWithPath: Array<string> = path.split(\".\");\n const opName: string = opWithPath[opWithPath.length - 1]!;\n const keys = Object.keys(ruleTree);\n let rule: ShieldRule<TContext> | undefined;\n if (keys.includes(\"query\") || keys.includes(\"mutation\")) {\n // @ts-ignore\n rule = ruleTree?.[type]?.[opName];\n } else {\n const namespace = opWithPath[0];\n\n const tree = (ruleTree as Record<string, any>)[namespace!];\n if (tree?.[type]?.[opName]) {\n rule = tree?.[type]?.[opName];\n }\n }\n rule = rule ?? options.fallbackRule;\n\n if (rule) {\n return rule\n ?.resolve(ctx, type, path, input, rawInput, options)\n .then(async result => {\n if (result instanceof Error) {\n throw result;\n }\n if (!result) {\n throw options.fallbackError;\n }\n\n return next();\n });\n }\n\n return next();\n };\n}\n"],"mappings":"AAuBA,SAAgB,
|
|
1
|
+
{"version":3,"file":"generator.mjs","names":["opWithPath: Array<string>","opName: string","rule: ShieldRule<TContext> | undefined"],"sources":["../../src/shield/generator.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type { IRules, OptionsInterface, ShieldRule } from \"./types\";\n\n/**\n * Generates middleware from given rules.\n */\nexport function generateMiddlewareFromRuleTree<\n TContext extends Record<string, unknown>\n>(ruleTree: IRules<TContext>, options: OptionsInterface<TContext>) {\n return async ({\n next,\n ctx,\n type,\n path,\n input,\n rawInput\n }: {\n next: () => Promise<any>;\n ctx: TContext;\n type: string;\n path: string;\n input: { [name: string]: any };\n rawInput: unknown;\n }) => {\n const opWithPath: Array<string> = path.split(\".\");\n const opName: string = opWithPath[opWithPath.length - 1]!;\n const keys = Object.keys(ruleTree);\n let rule: ShieldRule<TContext> | undefined;\n if (keys.includes(\"query\") || keys.includes(\"mutation\")) {\n // @ts-ignore\n rule = ruleTree?.[type]?.[opName];\n } else {\n const namespace = opWithPath[0];\n\n const tree = (ruleTree as Record<string, any>)[namespace!];\n if (tree?.[type]?.[opName]) {\n rule = tree?.[type]?.[opName];\n }\n }\n rule = rule ?? options.fallbackRule;\n\n if (rule) {\n return rule\n ?.resolve(ctx, type, path, input, rawInput, options)\n .then(async result => {\n if (result instanceof Error) {\n throw result;\n }\n if (!result) {\n throw options.fallbackError;\n }\n\n return next();\n });\n }\n\n return next();\n };\n}\n"],"mappings":";;;;AAuBA,SAAgB,+BAEd,UAA4B,SAAqC;AACjE,QAAO,OAAO,EACZ,MACA,KACA,MACA,MACA,OACA,eAQI;EACJ,MAAMA,aAA4B,KAAK,MAAM,IAAI;EACjD,MAAMC,SAAiB,WAAW,WAAW,SAAS;EACtD,MAAM,OAAO,OAAO,KAAK,SAAS;EAClC,IAAIC;AACJ,MAAI,KAAK,SAAS,QAAQ,IAAI,KAAK,SAAS,WAAW,CAErD,QAAO,WAAW,QAAQ;OACrB;GAGL,MAAM,OAAQ,SAFI,WAAW;AAG7B,OAAI,OAAO,QAAQ,QACjB,QAAO,OAAO,QAAQ;;AAG1B,SAAO,QAAQ,QAAQ;AAEvB,MAAI,KACF,QAAO,MACH,QAAQ,KAAK,MAAM,MAAM,OAAO,UAAU,QAAQ,CACnD,KAAK,OAAM,WAAU;AACpB,OAAI,kBAAkB,MACpB,OAAM;AAER,OAAI,CAAC,OACH,OAAM,QAAQ;AAGhB,UAAO,MAAM;IACb;AAGN,SAAO,MAAM"}
|
package/dist/shield/index.cjs
CHANGED
|
@@ -1 +1,12 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_shield_constructors = require('./constructors.cjs');
|
|
2
|
+
const require_shield_shield = require('./shield.cjs');
|
|
3
|
+
|
|
4
|
+
exports.allow = require_shield_constructors.allow;
|
|
5
|
+
exports.and = require_shield_constructors.and;
|
|
6
|
+
exports.chain = require_shield_constructors.chain;
|
|
7
|
+
exports.deny = require_shield_constructors.deny;
|
|
8
|
+
exports.not = require_shield_constructors.not;
|
|
9
|
+
exports.or = require_shield_constructors.or;
|
|
10
|
+
exports.race = require_shield_constructors.race;
|
|
11
|
+
exports.rule = require_shield_constructors.rule;
|
|
12
|
+
exports.shield = require_shield_shield.shield;
|
package/dist/shield/index.mjs
CHANGED
|
@@ -1 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { allow, and, chain, deny, not, or, race, rule } from "./constructors.mjs";
|
|
2
|
+
import { shield } from "./shield.mjs";
|
|
3
|
+
|
|
4
|
+
export { allow, and, chain, deny, not, or, race, rule, shield };
|
package/dist/shield/rules.cjs
CHANGED
|
@@ -1 +1,200 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
//#region src/shield/rules.ts
|
|
3
|
+
var Rule = class {
|
|
4
|
+
name;
|
|
5
|
+
func;
|
|
6
|
+
constructor(name, func) {
|
|
7
|
+
this.name = name;
|
|
8
|
+
this.func = func;
|
|
9
|
+
}
|
|
10
|
+
async resolve(ctx, type, path, input, rawInput, options) {
|
|
11
|
+
try {
|
|
12
|
+
const res = await this.executeRule(ctx, type, path, input, rawInput, options);
|
|
13
|
+
if (res instanceof Error) return res;
|
|
14
|
+
else if (typeof res === "string") return new Error(res);
|
|
15
|
+
else if (res === true) return true;
|
|
16
|
+
else return false;
|
|
17
|
+
} catch (err) {
|
|
18
|
+
if (options.debug) throw err;
|
|
19
|
+
else return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
*
|
|
24
|
+
* Compares a given rule with the current one
|
|
25
|
+
* and checks whether their functions are equal.
|
|
26
|
+
*
|
|
27
|
+
*/
|
|
28
|
+
equals(rule) {
|
|
29
|
+
return this.func === rule.func;
|
|
30
|
+
}
|
|
31
|
+
executeRule(ctx, type, path, input, rawInput, options) {
|
|
32
|
+
return this.func(ctx, type, path, input, rawInput, options);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var LogicRule = class extends Rule {
|
|
36
|
+
rules;
|
|
37
|
+
constructor(rules) {
|
|
38
|
+
super("LogicRule");
|
|
39
|
+
this.rules = rules;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* By default logic rule resolves to false.
|
|
43
|
+
*/
|
|
44
|
+
async resolve(_ctx, _type, _path, _input, _rawInput, _options) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Evaluates all the rules.
|
|
49
|
+
*/
|
|
50
|
+
async evaluate(ctx, type, path, input, rawInput, options) {
|
|
51
|
+
const tasks = this.getRules().map(async (rule) => rule.resolve(ctx, type, path, input, rawInput, options));
|
|
52
|
+
return Promise.all(tasks);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Returns rules in a logic rule.
|
|
56
|
+
*/
|
|
57
|
+
getRules() {
|
|
58
|
+
return this.rules;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
var RuleOr = class extends LogicRule {
|
|
62
|
+
constructor(rules) {
|
|
63
|
+
super(rules);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Makes sure that at least one of them has evaluated to true.
|
|
67
|
+
*/
|
|
68
|
+
async resolve(ctx, type, path, input, rawInput, options) {
|
|
69
|
+
const result = await this.evaluate(ctx, type, path, input, rawInput, options);
|
|
70
|
+
if (result.every((res) => res !== true)) return result.find((res) => res instanceof Error) ?? false;
|
|
71
|
+
else return true;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
var RuleAnd = class extends LogicRule {
|
|
75
|
+
constructor(rules) {
|
|
76
|
+
super(rules);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Makes sure that all of them have resolved to true.
|
|
80
|
+
*/
|
|
81
|
+
async resolve(ctx, type, path, input, rawInput, options) {
|
|
82
|
+
const result = await this.evaluate(ctx, type, path, input, rawInput, options);
|
|
83
|
+
if (result.some((res) => res !== true)) return result.find((res) => res instanceof Error) ?? false;
|
|
84
|
+
else return true;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
var RuleChain = class extends LogicRule {
|
|
88
|
+
constructor(rules) {
|
|
89
|
+
super(rules);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Makes sure that all of them have resolved to true.
|
|
93
|
+
*/
|
|
94
|
+
async resolve(ctx, type, path, input, rawInput, options) {
|
|
95
|
+
const result = await this.evaluate(ctx, type, path, input, rawInput, options);
|
|
96
|
+
if (result.some((res) => res !== true)) return result.find((res) => res instanceof Error) ?? false;
|
|
97
|
+
else return true;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Evaluates all the rules.
|
|
101
|
+
*/
|
|
102
|
+
async evaluate(ctx, type, path, input, rawInput, options) {
|
|
103
|
+
return iterate(this.getRules());
|
|
104
|
+
async function iterate([rule, ...otherRules]) {
|
|
105
|
+
if (rule === void 0) return [];
|
|
106
|
+
return rule.resolve(ctx, type, path, input, rawInput, options).then(async (res) => {
|
|
107
|
+
if (res !== true) return [res];
|
|
108
|
+
else return iterate(otherRules).then((ress) => ress.concat(res));
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
var RuleRace = class extends LogicRule {
|
|
114
|
+
constructor(rules) {
|
|
115
|
+
super(rules);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Makes sure that at least one of them resolved to true.
|
|
119
|
+
*/
|
|
120
|
+
async resolve(ctx, type, path, input, rawInput, options) {
|
|
121
|
+
const result = await this.evaluate(ctx, type, path, input, rawInput, options);
|
|
122
|
+
if (result.includes(true)) return true;
|
|
123
|
+
else return result.find((res) => res instanceof Error) ?? false;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Evaluates all the rules.
|
|
127
|
+
*/
|
|
128
|
+
async evaluate(ctx, type, path, input, rawInput, options) {
|
|
129
|
+
return iterate(this.getRules());
|
|
130
|
+
async function iterate([rule, ...otherRules]) {
|
|
131
|
+
if (rule === void 0) return [];
|
|
132
|
+
return rule.resolve(ctx, type, path, input, rawInput, options).then(async (res) => {
|
|
133
|
+
if (res === true) return [res];
|
|
134
|
+
else return iterate(otherRules).then((ress) => ress.concat(res));
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
var RuleNot = class extends LogicRule {
|
|
140
|
+
error;
|
|
141
|
+
name = "RuleNot";
|
|
142
|
+
equals;
|
|
143
|
+
constructor(rule, error) {
|
|
144
|
+
super([rule]);
|
|
145
|
+
this.error = error;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Negates the result.
|
|
149
|
+
*/
|
|
150
|
+
async resolve(ctx, type, path, input, rawInput, options) {
|
|
151
|
+
const [res] = await this.evaluate(ctx, type, path, input, rawInput, options);
|
|
152
|
+
if (res instanceof Error) return true;
|
|
153
|
+
else if (res !== true) return true;
|
|
154
|
+
else {
|
|
155
|
+
if (this.error) return this.error;
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
var RuleTrue = class extends LogicRule {
|
|
161
|
+
name = "RuleTrue";
|
|
162
|
+
equals;
|
|
163
|
+
constructor() {
|
|
164
|
+
super([]);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
*
|
|
168
|
+
* Always true.
|
|
169
|
+
*
|
|
170
|
+
*/
|
|
171
|
+
async resolve() {
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
var RuleFalse = class extends LogicRule {
|
|
176
|
+
name = "RuleTrue";
|
|
177
|
+
equals;
|
|
178
|
+
constructor() {
|
|
179
|
+
super([]);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
*
|
|
183
|
+
* Always false.
|
|
184
|
+
*
|
|
185
|
+
*/
|
|
186
|
+
async resolve() {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
//#endregion
|
|
192
|
+
exports.LogicRule = LogicRule;
|
|
193
|
+
exports.Rule = Rule;
|
|
194
|
+
exports.RuleAnd = RuleAnd;
|
|
195
|
+
exports.RuleChain = RuleChain;
|
|
196
|
+
exports.RuleFalse = RuleFalse;
|
|
197
|
+
exports.RuleNot = RuleNot;
|
|
198
|
+
exports.RuleOr = RuleOr;
|
|
199
|
+
exports.RuleRace = RuleRace;
|
|
200
|
+
exports.RuleTrue = RuleTrue;
|