{"version":3,"file":"shield.mjs","names":[],"sources":["../../src/shield/shield.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 { MiddlewareFunction } from \"@trpc/server/unstable-core-do-not-import\";\nimport { allow } from \"./constructors\";\nimport { generateMiddlewareFromRuleTree } from \"./generator\";\nimport type {\n  IFallbackErrorType,\n  IRules,\n  OptionsConstructorInterface,\n  OptionsInterface,\n  ShieldRule\n} from \"./types\";\nimport { withDefault } from \"./utils\";\nimport { ValidationError, validateRuleTree } from \"./validation\";\n\n/**\n * Makes sure all of defined rules are in accord with the options\n * shield can process.\n */\nfunction normalizeOptions<TContext extends Record<string, any>>(\n  options: OptionsConstructorInterface<TContext>\n): OptionsInterface<TContext> {\n  if (typeof options.fallbackError === \"string\") {\n    options.fallbackError = new Error(options.fallbackError);\n  }\n\n  return {\n    debug: options.debug ?? false,\n    allowExternalErrors: withDefault(false)(options.allowExternalErrors),\n    fallbackRule: withDefault<ShieldRule<TContext>>(\n      allow as ShieldRule<TContext>\n    )(options.fallbackRule),\n    fallbackError: withDefault<IFallbackErrorType<TContext>>(\n      new Error(\"Authorization error\")\n    )(options.fallbackError)\n  };\n}\n\n/**\n * Validates rules and generates middleware from defined rule tree.\n */\nexport function shield<\n  TContext extends Record<string, any>,\n  TMeta extends object = object\n>(\n  ruleTree: IRules<TContext>,\n  options: OptionsConstructorInterface<TContext> = {}\n): MiddlewareFunction<TContext, TMeta, TContext, TContext, unknown> {\n  const normalizedOptions = normalizeOptions<TContext>(options);\n  const ruleTreeValidity = validateRuleTree<TContext>(ruleTree);\n\n  if (ruleTreeValidity.status === \"ok\") {\n    return generateMiddlewareFromRuleTree<TContext>(\n      ruleTree,\n      normalizedOptions\n    ) as any;\n  } else {\n    throw new ValidationError(ruleTreeValidity.message);\n  }\n}\n"],"mappings":";;;;;;;;;;AAmCA,SAAS,iBACP,SAC4B;CAC5B,IAAI,OAAO,QAAQ,kBAAkB,UACnC,QAAQ,gBAAgB,IAAI,MAAM,QAAQ,aAAa;CAGzD,OAAO;EACL,OAAO,QAAQ,SAAS;EACxB,qBAAqB,YAAY,KAAK,CAAC,CAAC,QAAQ,mBAAmB;EACnE,cAAc,YACZ,KACF,CAAC,CAAC,QAAQ,YAAY;EACtB,eAAe,4BACb,IAAI,MAAM,qBAAqB,CACjC,CAAC,CAAC,QAAQ,aAAa;CACzB;AACF;;;;AAKA,SAAgB,OAId,UACA,UAAiD,CAAC,GACgB;CAClE,MAAM,oBAAoB,iBAA2B,OAAO;CAC5D,MAAM,mBAAmB,iBAA2B,QAAQ;CAE5D,IAAI,iBAAiB,WAAW,MAC9B,OAAO,+BACL,UACA,iBACF;MAEA,MAAM,IAAI,gBAAgB,iBAAiB,OAAO;AAEtD"}