form-param.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. var _ = require('../../lodash'), Property = require('./property').Property, PropertyBase = require('./property-base').PropertyBase, FormParam;
  3. /**
  4. * @typedef FormParam.definition
  5. * @property {String} key The name ("key") of the form data parameter.
  6. * @property {String} value The value of the parameter.
  7. */
  8. _.inherit((
  9. /**
  10. * Represents a Form Data parameter, which can exist in request body.
  11. *
  12. * @constructor
  13. * @param {FormParam.definition} options Pass the initial definition of the form data parameter.
  14. */
  15. FormParam = function PostmanFormParam(options) {
  16. FormParam.super_.apply(this, arguments);
  17. // @todo avoid using _.get
  18. this.key = _.get(options, 'key') || '';
  19. this.value = _.get(options, 'value') || '';
  20. this.type = _.get(options, 'type');
  21. this.src = _.get(options, 'src');
  22. this.contentType = _.get(options, 'contentType');
  23. }), Property);
  24. _.assign(FormParam.prototype, /** @lends FormParam.prototype */ {
  25. /**
  26. * Converts the FormParameter to a single param string.
  27. *
  28. * @returns {String}
  29. */
  30. toString: function () {
  31. return this.key + '=' + this.value;
  32. },
  33. /**
  34. * Returns the value of the form parameter (if any).
  35. *
  36. * @returns {*|string}
  37. */
  38. valueOf: function () {
  39. return this.value; // can be multiple types, so just return whatever we have instead of being too clever
  40. },
  41. /**
  42. * Convert the form-param to JSON compatible plain object.
  43. *
  44. * @returns {Object}
  45. */
  46. toJSON: function () {
  47. var obj = PropertyBase.toJSON(this);
  48. // remove value from file param because it is non-serializable ReadStream
  49. if (obj.type === 'file') {
  50. _.unset(obj, 'value');
  51. }
  52. return obj;
  53. }
  54. });
  55. _.assign(FormParam, /** @lends FormParam */ {
  56. /**
  57. * Defines the name of this property for internal use.
  58. * @private
  59. * @readOnly
  60. * @type {String}
  61. */
  62. _postman_propertyName: 'FormParam',
  63. /**
  64. * Declare the list index key, so that property lists of form parameters work correctly
  65. *
  66. * @type {String}
  67. */
  68. _postman_propertyIndexKey: 'key',
  69. /**
  70. * Form params can have multiple values, so set this to true.
  71. *
  72. * @type {Boolean}
  73. */
  74. _postman_propertyAllowsMultipleValues: true,
  75. /**
  76. * Parse a form data string into an array of objects, where each object contains a key and a value.
  77. * @todo implement this, not implemented yet.
  78. * @param formdata {String}
  79. * @returns {Array}
  80. */
  81. parse: _.noop
  82. });
  83. module.exports = {
  84. FormParam: FormParam
  85. };