Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

constraints will be overrided when multiple router created #254

Closed
climba03003 opened this issue Apr 22, 2022 · 0 comments · Fixed by #256
Closed

constraints will be overrided when multiple router created #254

climba03003 opened this issue Apr 22, 2022 · 0 comments · Fixed by #256

Comments

@climba03003
Copy link
Contributor

climba03003 commented Apr 22, 2022

The current code override the HandlerStorage.prototype.constrainer to get the constrainter in router.
It will be override when two store created first before any route trying to lookup.

const findMyWay = require("find-my-way");

const constraint = {
  name: "secret",
  storage: function () {
    let secrets = {};
    return {
      get: (secret) => {
        return secrets[secret] || null;
      },
      set: (secret, store) => {
        secrets[secret] = store;
      },
      del: (secret) => {
        delete secrets[secret];
      },
      empty: () => {
        secrets = {};
      },
    };
  },
  deriveConstraint: (req, ctx) => {
    return req.headers["x-secret"];
  },
  validate() {
    return true;
  },
};

let router1 = findMyWay({ constraints: { secret: constraint } });

// it override the constraints of router1
const router2 = findMyWay({ constraints: undefined });

// it can registered constraints
router1.on("GET", "/", { constraints: { secret: "alpha" } }, () => {});
// it cannot find the constraints secret
router1.find("GET", "/", { constraints: { secret: "alpha" } });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant