balda-js 0.0.51 → 0.0.53

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/lib/index.cjs CHANGED
@@ -477,6 +477,39 @@ flagDecorator.number = (options) => {
477
477
  return flagDecorator({ ...options, type: "number" });
478
478
  };
479
479
  var flag = flagDecorator;
480
+ var NativePath = class {
481
+ join(...paths) {
482
+ switch (runtime.type) {
483
+ case "node":
484
+ case "bun":
485
+ case "deno":
486
+ return path__default.default.join(...paths);
487
+ default:
488
+ throw new Error("Unsupported runtime");
489
+ }
490
+ }
491
+ extName(inputPath) {
492
+ switch (runtime.type) {
493
+ case "bun":
494
+ case "node":
495
+ case "deno":
496
+ return path__default.default.extname(inputPath);
497
+ default:
498
+ throw new Error("Unsupported runtime");
499
+ }
500
+ }
501
+ resolve(...paths) {
502
+ switch (runtime.type) {
503
+ case "bun":
504
+ case "node":
505
+ case "deno":
506
+ return path__default.default.resolve(...paths);
507
+ default:
508
+ throw new Error("Unsupported runtime");
509
+ }
510
+ }
511
+ };
512
+ var nativePath = new NativePath();
480
513
 
481
514
  // src/server/router/router.ts
482
515
  var Node = class {
@@ -725,45 +758,28 @@ var Router = class _Router {
725
758
  }
726
759
  };
727
760
  var router = new Router();
728
- var NativePath = class {
729
- join(...paths) {
730
- switch (runtime.type) {
731
- case "node":
732
- case "bun":
733
- case "deno":
734
- return path__default.default.join(...paths);
735
- default:
736
- throw new Error("Unsupported runtime");
737
- }
738
- }
739
- extName(inputPath) {
740
- switch (runtime.type) {
741
- case "bun":
742
- case "node":
743
- case "deno":
744
- return path__default.default.extname(inputPath);
745
- default:
746
- throw new Error("Unsupported runtime");
747
- }
748
- }
749
- resolve(...paths) {
750
- switch (runtime.type) {
751
- case "bun":
752
- case "node":
753
- case "deno":
754
- return path__default.default.resolve(...paths);
755
- default:
756
- throw new Error("Unsupported runtime");
757
- }
758
- }
759
- };
760
- var nativePath = new NativePath();
761
761
 
762
762
  // src/decorators/controller/controller.ts
763
+ var createPolicyMiddleware = (policies) => {
764
+ return async (req, res, next) => {
765
+ for (const policy of policies) {
766
+ const allowed = await policy.manager.canAccess(
767
+ policy.scope,
768
+ policy.handler,
769
+ req
770
+ );
771
+ if (!allowed) {
772
+ return res.unauthorized({ error: "Unauthorized" });
773
+ }
774
+ }
775
+ return next();
776
+ };
777
+ };
763
778
  var controller = (path2, swaggerOptions) => {
764
779
  return (target) => {
765
780
  const classMeta = MetadataStore.get(target.prototype, "__class__");
766
781
  const classMiddlewares = classMeta?.middlewares || [];
782
+ const classPolicies = classMeta?.policies || [];
767
783
  const metaMap = MetadataStore.getAll(target.prototype);
768
784
  const instance = new target();
769
785
  for (const [propertyKey, meta] of metaMap.entries()) {
@@ -772,7 +788,16 @@ var controller = (path2, swaggerOptions) => {
772
788
  }
773
789
  const handler = target.prototype[propertyKey].bind(instance);
774
790
  const fullPath = path2 ? nativePath.join(path2, meta.route.path) : meta.route.path;
775
- const allMiddlewares = [...classMiddlewares, ...meta.middlewares || []];
791
+ const allPolicies = [
792
+ ...classPolicies,
793
+ ...meta.policies || []
794
+ ];
795
+ const policyMiddleware = allPolicies.length > 0 ? [createPolicyMiddleware(allPolicies)] : [];
796
+ const allMiddlewares = [
797
+ ...classMiddlewares,
798
+ ...policyMiddleware,
799
+ ...meta.middlewares || []
800
+ ];
776
801
  router.addOrUpdate(
777
802
  meta.route.method,
778
803
  fullPath,
@@ -5426,12 +5451,55 @@ var Server = class {
5426
5451
  var BasePlugin = class {
5427
5452
  };
5428
5453
 
5454
+ // src/server/policy/policy_decorator.ts
5455
+ var createPolicyDecorator = (manager) => {
5456
+ return (scope, handler) => {
5457
+ return (target, propertyKey, descriptor) => {
5458
+ const policyMeta = { scope, handler, manager };
5459
+ if (typeof propertyKey === "undefined") {
5460
+ let meta2 = MetadataStore.get(target.prototype, "__class__");
5461
+ if (!meta2) {
5462
+ meta2 = { policies: [] };
5463
+ }
5464
+ if (!meta2.policies) {
5465
+ meta2.policies = [];
5466
+ }
5467
+ meta2.policies.push(policyMeta);
5468
+ MetadataStore.set(target.prototype, "__class__", meta2);
5469
+ return target;
5470
+ }
5471
+ let meta = MetadataStore.get(target, propertyKey);
5472
+ if (!meta) {
5473
+ meta = { policies: [] };
5474
+ }
5475
+ if (!meta.policies) {
5476
+ meta.policies = [];
5477
+ }
5478
+ meta.policies.push(policyMeta);
5479
+ MetadataStore.set(target, propertyKey, meta);
5480
+ return descriptor;
5481
+ };
5482
+ };
5483
+ };
5484
+
5429
5485
  // src/server/policy/policy_manager.ts
5430
5486
  var PolicyManager = class {
5431
5487
  providers;
5432
5488
  constructor(providers) {
5433
5489
  this.providers = providers;
5434
5490
  }
5491
+ /**
5492
+ * Creates a decorator for the policy manager with typed parameters
5493
+ */
5494
+ createDecorator() {
5495
+ return createPolicyDecorator(this);
5496
+ }
5497
+ /**
5498
+ * Checks if the user has access to the given scope and handler
5499
+ * @param scope - The scope to check access for
5500
+ * @param handler - The handler to check access for
5501
+ * @param args - The arguments to pass to the handler
5502
+ */
5435
5503
  canAccess(scope, handler, ...args2) {
5436
5504
  const provider = this.providers[scope];
5437
5505
  if (!provider) {
@@ -5464,6 +5532,7 @@ exports.commandRegistry = commandRegistry;
5464
5532
  exports.controller = controller;
5465
5533
  exports.cookie = cookie;
5466
5534
  exports.cors = cors;
5535
+ exports.createPolicyDecorator = createPolicyDecorator;
5467
5536
  exports.cron = cron;
5468
5537
  exports.defineLoggerConfig = defineLoggerConfig;
5469
5538
  exports.defineQueueConfiguration = defineQueueConfiguration;