Source: catalogItem/subsets.js

  1. import { BricklinkRequest, RequestParams } from '../request';
  2. import { CatalogItem } from './catalogItem';
  3. /**
  4. * Represents a subset entry.
  5. */
  6. export class SubsetEntry {
  7. /**
  8. * Create an instance of a subset entry
  9. * @param {object} [data] The API response data
  10. * @param {object} [data.item] Item information
  11. * @param {number} [data.color_id] The color id of the entry item.
  12. * @param {number} [data.quantity] The number of items in the subset entry.
  13. * @param {number} [data.extra_quantity] The number of extra items included in the subset.
  14. * @param {boolean} [data.is_alternate] Indicator that the item is an alternate.
  15. */
  16. constructor(data) {
  17. data = data || {};
  18. /** @type {CatalogItem} */
  19. this.item = data.item ? new CatalogItem(data.item) : new CatalogItem();
  20. /** @type {number} */
  21. this.color_id = data.color_id || 0;
  22. /** @type {number} */
  23. this.quantity = data.quantity || 0;
  24. /** @type {number} */
  25. this.extra_quantity = data.extra_quantity || 0;
  26. /** @type {boolean} */
  27. this.is_alternate = data.is_alternate || false;
  28. }
  29. }
  30. /**
  31. * Represents a subset item. A subset item can have more than one matching entry due to alternate parts.
  32. */
  33. export class Subset {
  34. /**
  35. * Create an instance of a subset item.
  36. * @param {object} [data] The response data from API request.
  37. * @param {number} [data.match_no] The number of matching entries or 0 if there is no matching of alternative item.
  38. * @param {object} [data.entries] The specific entries for the subset item.
  39. */
  40. constructor(data) {
  41. data = data || {};
  42. /** @type {number} */
  43. this.match_no = data.match_no || 0;
  44. const entries = data.entries || [];
  45. /** @type {SubsetEntry[]} */
  46. this.entries = entries.map((e) => new SubsetEntry(e));
  47. }
  48. /**
  49. * Method to get a subset of a catalog item.
  50. *
  51. * Usage:
  52. *
  53. * ```
  54. * var req = Subset.get(ItemType.Set, '6020-1', {break_minifigs: true});
  55. * client.send(req).then(subset => console.log(subset));
  56. * ```
  57. *
  58. * @param {string} itemType Catalog item type
  59. * @param {string} itemNumber Catalog item number
  60. * @param {object} options Options that conform to {@link SubsetOptions}.
  61. */
  62. static get(itemType, itemNumber, options) {
  63. const method = BricklinkRequest.GET;
  64. options = options ? new SubsetOptions(options) : new SubsetOptions();
  65. const uri = `/items/${itemType}/${itemNumber}/subsets`;
  66. return new BricklinkRequest(method, uri, options, (data) => {
  67. return data.map((e) => new Subset(e));
  68. });
  69. }
  70. }
  71. /**
  72. * Options that can be used when make a request for a subset.
  73. */
  74. export class SubsetOptions extends RequestParams {
  75. /**
  76. * Make an instance of SubsetOptions.
  77. * @param {object} [data] Raw options object.
  78. * @param {number} [data.color_id] The color of the item(This value is valid only for {@link ItemType}.Part.
  79. * @param {boolean} [data.box] Indicates whether the set includes the original box
  80. * @param {boolean} [data.instruction] Indicates whether the set includes the original instruction
  81. * @param {boolean} [data.break_minifigs] Indicates whether the result breaks down minifigs as parts
  82. * @param {boolean} [data.break_subsets] Indicates whether the result breaks down sets in set
  83. */
  84. constructor(data) {
  85. super();
  86. data = data || {};
  87. /** @type {number|null} */
  88. this.color_id = data.color_id || null;
  89. /** @type {boolean|null} */
  90. this.box = data.box || null;
  91. /** @type {boolean|null} */
  92. this.instruction = data.instruction || null;
  93. /** @type {boolean|null} */
  94. this.break_minifigs = data.break_minifigs || null;
  95. /** @type {boolean|null} */
  96. this.break_subsets = data.break_subsets || null;
  97. }
  98. }