{"version":3,"file":"constructors.mjs","names":[],"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;CACzD,IAAI,OAAO,SAAS,UAAU;EAC5B,UAAU;EACV,OAAO,KAAK,OAAO,CAAC,CAAC,SAAS;CAChC,OAAO,IAAI,OAAO,SAAS,UACzB,UAAU,WAAW,CAAC;MACjB;EACL,OAAO,KAAK,OAAO,CAAC,CAAC,SAAS;EAC9B,UAAU,CAAC;CACb;CAGA,OAAO,IAAI,KAAK,MAAM,MAAM,CAAC,CAAC;AAChC;;;;AAKF,MAAa,OACX,GAAG,UACmB;CACtB,OAAO,IAAI,QAAQ,KAAK;AAC1B;;;;AAKA,MAAa,SACX,GAAG,UACqB;CACxB,OAAO,IAAI,UAAU,KAAK;AAC5B;;;;AAKA,MAAa,QACX,GAAG,UACoB;CACvB,OAAO,IAAI,SAAS,KAAK;AAC3B;;;;AAKA,MAAa,MACX,GAAG,UACkB;CACrB,OAAO,IAAI,OAAO,KAAK;AACzB;;;;AAKA,MAAa,OACX,MACA,UACsB;CACtB,IAAI,OAAO,UAAU,UAAU,OAAO,IAAI,QAAQ,MAAM,IAAI,MAAM,KAAK,CAAC;CACxE,OAAO,IAAI,QAAQ,MAAM,KAAK;AAChC;;;;AAKA,MAAa,QAAQ,IAAI,SAAS;;;;AAKlC,MAAa,OAAO,IAAI,UAAU"}