{"version":3,"file":"generator.mjs","names":[],"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;CACjE,OAAO,OAAO,EACZ,MACA,KACA,MACA,MACA,OACA,eAQI;EACJ,MAAM,aAA4B,KAAK,MAAM,GAAG;EAChD,MAAM,SAAiB,WAAW,WAAW,SAAS;EACtD,MAAM,OAAO,OAAO,KAAK,QAAQ;EACjC,IAAI;EACJ,IAAI,KAAK,SAAS,OAAO,KAAK,KAAK,SAAS,UAAU,GAEpD,OAAO,WAAW,KAAK,GAAG;OACrB;GAGL,MAAM,OAAQ,SAFI,WAAW;GAG7B,IAAI,OAAO,KAAK,GAAG,SACjB,OAAO,OAAO,KAAK,GAAG;EAE1B;EACA,OAAO,QAAQ,QAAQ;EAEvB,IAAI,MACF,OAAO,MACH,QAAQ,KAAK,MAAM,MAAM,OAAO,UAAU,OAAO,CAAC,CACnD,KAAK,OAAM,WAAU;GACpB,IAAI,kBAAkB,OACpB,MAAM;GAER,IAAI,CAAC,QACH,MAAM,QAAQ;GAGhB,OAAO,KAAK;EACd,CAAC;EAGL,OAAO,KAAK;CACd;AACF"}