match.js 818 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * Expose `Layer`.
  3. */
  4. module.exports = Match;
  5. function Match(layer, path, params) {
  6. this.layer = layer;
  7. this.params = {};
  8. this.path = path || '';
  9. if (!params) {
  10. return this;
  11. }
  12. var keys = layer.keys;
  13. var n = 0;
  14. var prop;
  15. var key;
  16. var val;
  17. for (var i = 0; i < params.length; i++) {
  18. key = keys[i];
  19. val = decode_param(params[i]);
  20. prop = key
  21. ? key.name
  22. : n++;
  23. this.params[prop] = val;
  24. }
  25. return this;
  26. };
  27. /**
  28. * Decode param value.
  29. *
  30. * @param {string} val
  31. * @return {string}
  32. * @api private
  33. */
  34. function decode_param(val){
  35. if (typeof val !== 'string') {
  36. return val;
  37. }
  38. try {
  39. return decodeURIComponent(val);
  40. } catch (e) {
  41. var err = new TypeError("Failed to decode param '" + val + "'");
  42. err.status = 400;
  43. throw err;
  44. }
  45. }