ChemPare Documentation - v0.0.0
    Preparing search index...

    Class ProductBuilder<T>

    Builder class for constructing Product objects with a fluent interface. Implements the Builder pattern to handle complex product construction with optional fields and data validation.

    This is a utility class for building product data up over different requests. It is used to build the product data up over different requests, and then return a complete product object.

    const builder = new ProductBuilder<Product>('https://example.com');
    const product = await builder
    .setBasicInfo('Sodium Chloride', '/products/nacl', 'ChemSupplier')
    .setPricing(29.99, 'USD', '$')
    .setQuantity(500, 'g')
    .setDescription('99.9% pure NaCl')
    .setCAS('7647-14-5')
    .build();

    if (product) {
    console.log(product.title); // "Sodium Chloride"
    console.log(product.price); // 29.99
    console.log(product.uom); // "g"
    }

    Type Parameters

    Index

    Constructors

    • Creates a new ProductBuilder instance.

      Type Parameters

      Parameters

      • baseURL: string

        The base URL of the supplier's website, used for resolving relative URLs

      Returns ProductBuilder<T>

      const builder = new ProductBuilder('https://example.com');
      

    Methods

    • Sets the data for the product by merging the provided data object.

      Parameters

      • data: Partial<T>

        The data to merge into the product

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.setData({
      title: "Test Chemical",
      price: 29.99,
      quantity: 500,
      uom: "g"
      });
    • Sets the basic information for the product including title, URL, and supplier name.

      Parameters

      • title: string

        The display name/title of the product

      • url: string

        The URL where the product can be found (can be relative to baseURL)

      • supplier: string

        The name of the supplier/vendor

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.setBasicInfo(
      'Hydrochloric Acid',
      '/products/hcl-solution',
      'ChemSupplier'
      );
    • Sets the formula for the product.

      Parameters

      • Optionalformula: string

        The formula to set

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.setFormula('foobar K<sub>2</sub>Cr<sub>2</sub>O<sub>7</sub> baz');
      // sets this.product.formula to "K₂Cr₂O₇"
      builder.setFormula("H<sub>2</sub>SO<sub>4</sub>");
      // sets this.product.formula to "H₂SO ₄"
      builder.setFormula("Just some text");
      // sets this.product.formula to undefined
    • Sets the grade/purity level of the product. Only sets the grade if a non-empty string is provided.

      Parameters

      • grade: string

        The grade or purity level of the product

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.setGrade("ACS Grade");
      builder.setGrade("Reagent Grade");
    • Sets the price for the product. This is useful for if the price and currency are easier to add separately (eg: getting the currency code is done in a different request handler)

      Parameters

      • price: string | number

        The price to set

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.setPrice(123.34);
      
    • Sets the currency symbol for the product. This is useful for if the price and currency are easier to add separately (eg: getting the currency code is done in a different request handler). If no currency symbol is set, then it will be inferred from the currencyCode

      Parameters

      • sign: valueof

        The currency symbol to set

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.setCurrencySymbol('$');
      
    • Sets the currency code for the product. This is useful for if the price and currency are easier to add separately (eg: getting the currency code is done in a different request handler).

      Parameters

      • code: valueof

        The currency code to set

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.setCurrencyCode('USD');
      
    • Overload

      Sets the pricing information for the product including price and currency details when given a parsedPrice object

      Parameters

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.setPricing(parsePrice('$123.34'));
      // Sets this.product.price to 123.34
      // Sets this.product.currencyCode to 'USD'
      // Sets this.product.currencySymbol to '$'
    • Overload

      Sets the pricing information for the product including price and currency details when given a price

      Parameters

      • price: string

        Price in string format

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.setPricing('$123.34');
      // Sets this.product.price to 123.34
      // Sets this.product.currencyCode to 'USD'
      // Sets this.product.currencySymbol to '$'
    • Overload

      Sets the pricing information for the product including price and currency details when given a price

      Parameters

      • price: string | number

        Price in number format

      • currencyCode: string

        The ISO currency code (e.g., 'USD', 'EUR')

      • currencySymbol: string

        The currency symbol (e.g., '$', '€')

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.setPricing(123.34, 'USD', '$');
      // Sets this.product.price to 123.34
      // Sets this.product.currencyCode to 'USD'
      // Sets this.product.currencySymbol to '$'
    • Overload

      Sets the quantity information for the product.

      Parameters

      Returns ProductBuilder<T>

      The builder instance for method chaining

      // For 500 grams
      builder.setQuantity(parseQuantity('500g'));
      // Sets this.product.quantity to 500
      // Sets this.product.uom to 'g'
    • Overload

      Sets the quantity information for the product.

      Parameters

      • quantity: string

        Quantity in string format

      Returns ProductBuilder<T>

      The builder instance for method chaining

      // For 500 grams
      builder.setQuantity('500g');
      // Sets this.product.quantity to 500
      // Sets this.product.uom to 'g'
    • Overload

      Sets the quantity information for the product.

      Parameters

      • quantity: number

        Quantity in number format

      • uom: string

        The unit of measure (e.g., 'g', 'ml', 'kg')

      Returns ProductBuilder<T>

      The builder instance for method chaining

      // For 500 grams
      builder.setQuantity(500, 'g');
      // Sets this.product.quantity to 500
      // Sets this.product.uom to 'g'
    • Sets the unit of measure for the product.

      Parameters

      • uom: string

        The unit of measure for the product

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.setUOM('g');
      
    • Sets the country of the supplier.

      Parameters

      • country: string

        The country of the supplier

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.setSupplierCountry("US");
      
    • Sets the shipping scope of the supplier.

      Parameters

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.setSupplierShipping("worldwide");
      
    • Sets the payment methods accepted by the supplier.

      Parameters

      • paymentMethods: PaymentMethod[]

        The payment methods accepted by the supplier

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.setSupplierPaymentMethods(["visa", "mastercard"]);
      
    • Sets the product description.

      Parameters

      • description: string

        The detailed description of the product

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.setDescription(
      'High purity sodium chloride, 99.9% pure, suitable for laboratory use'
      );
    • Sets the CAS (Chemical Abstracts Service) registry number for the product. Validates the CAS number format before setting.

      Parameters

      • cas: string

        The CAS registry number in format "XXXXX-XX-X"

      Returns ProductBuilder<T>

      The builder instance for method chaining

      // For sodium chloride
      builder.setCAS('7647-14-5');
      // For invalid CAS number (will not set)
      builder.setCAS('invalid-cas');
    • Sets the ID for the product.

      Parameters

      • Optionalid: string | number

        The unique identifier for the product

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.setID(12345);
      
    • Sets the UUID for the product.

      Parameters

      • uuid: string

        The UUID string for the product

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.setUUID('550e8400-e29b-41d4-a716-446655440000');
      
    • Sets the SKU (Stock Keeping Unit) for the product.

      Parameters

      • sku: string

        The SKU string for the product

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.setSku('CHEM-NaCl-500G');
      
    • Sets the vendor for the product.

      Parameters

      • Optionalvendor: string

        The vendor name

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.setVendor('Vendor Name');
      
    • Tries to determine the availability of the product based on variable input.

      Parameters

      • Optionalavailability: string | boolean

        The availability of the product

      Returns Maybe<AVAILABILITY>

      The availability of the product

      // In stock
      builder.determineAvailability("instock");
      builder.determineAvailability(true);
      builder.determineAvailability("outofstock");
      builder.determineAvailability("unavailable");
      builder.determineAvailability(false);
      builder.determineAvailability("preorder");
      builder.determineAvailability("backorder");
      builder.determineAvailability("discontinued");
    • Sets the availability of the product.

      Parameters

      Returns ProductBuilder<T>

      The builder instance for method chaining

      // In stock
      builder.setAvailability("IN_STOCK");
      // Set as in stock
      builder.setAvailability(false);
      // Out of stock
      // etc
    • Sets the availability of the product.

      Parameters

      • availability: boolean

        The availability of the product

      Returns ProductBuilder<T>

      The builder instance for method chaining

      // In stock
      builder.setAvailability("IN_STOCK");
      // Set as in stock
      builder.setAvailability(false);
      // Out of stock
      // etc
    • Sets the availability of the product.

      Parameters

      • availability: string

        The availability of the product

      Returns ProductBuilder<T>

      The builder instance for method chaining

      // In stock
      builder.setAvailability("IN_STOCK");
      // Set as in stock
      builder.setAvailability(false);
      // Out of stock
      // etc
    • Just a place to hold the products original response object.

      Parameters

      • Optionaldata: Record<string, unknown>

        The raw data to add the raw data.

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.addRawData({
      title: 'Sodium Chloride',
      price: 29.99,
      });
    • Adds a single variant to the product.

      Parameters

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.addVariant({
      title: '500g Package',
      price: 49.99,
      quantity: 500,
      uom: 'g',
      sku: 'CHEM-500G'
      });
    • Adds multiple variants to the product at once.

      Parameters

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.addVariants([
      {
      title: '500g Package',
      price: 49.99,
      quantity: 500,
      uom: 'g'
      },
      {
      title: '1kg Package',
      price: 89.99,
      quantity: 1000,
      uom: 'g'
      }
      ]);
    • Sets the variants for the product. Slightly different from addVariants in that it will replace the existing variants with the new ones.

      Parameters

      Returns ProductBuilder<T>

      The builder instance for method chaining

      builder.setVariants([{ id: 1, title: '500g Package', price: 49.99, quantity: 500, uom: 'g' }]);
      
    • Get a specific property from the product.

      Parameters

      • key: keyof T

        The key of the property to get

      Returns T[keyof T] | Maybe<T[keyof T]>

      The value of the property

      const title = builder.get("title");
      console.log(title); // "Sodium Chloride"
    • Gets a specific variant from the product.

      Parameters

      • index: number

        The index of the variant to get

      Returns undefined | Variant

      The variant object

      const variant = builder.getVariant(0);
      console.log(variant); // { id: 1, title: '500g Package', price: 49.99, quantity: 500, uom: 'g' }
    • Converts a relative or partial URL to an absolute URL using the base URL.

      Parameters

      • path: string | URL

        The URL or path to convert

      Returns string

      The absolute URL as a string

      const url = this.href('/products/123');
      // Returns: 'https://example.com/products/123'
    • Builds and validates the final Product object. Performs the following steps:

      1. Validates minimum required properties
      2. Calculates USD price if in different currency
      3. Converts quantity to base units
      4. Converts relative URLs to absolute
      5. Processes and validates variants if present

      Returns Promise<Maybe<Product>>

      const product = await builder
      .setBasicInfo('Test Chemical', '/products/test', 'Supplier')
      .setPricing(29.99, 'USD', '$')
      .setQuantity(100, 'g')
      .addVariant({
      title: '500g Package',
      price: 49.99,
      quantity: 500,
      uom: 'g'
      })
      .build();
    • Returns the current state of the product being built. Useful for debugging or inspecting the build progress.

      Returns Partial<T>

      The current partial product object

      const partialProduct = builder
      .setBasicInfo('Test', '/test', 'Supplier')
      .dump();
      console.log(partialProduct);
    • Creates an array of ProductBuilder instances from cached product data. This is used to restore builders from cache storage.

      Type Parameters

      Parameters

      • baseURL: string

        The base URL of the supplier's website

      • data: unknown[]

        Array of cached product data (from .dump())

      Returns ProductBuilder<T>[]

      Array of ProductBuilder instances

      const cachedData = await chrome.storage.local.get('cached_products');
      const builders = ProductBuilder.createFromCache('https://example.com', cachedData);
      for (const builder of builders) {
      const product = await builder.build();
      console.log(product.title);
      }

    Properties

    product: Partial<T> = {}

    The partial product object being built

    rawData: Record<string, unknown> = {}

    The raw data of the product

    baseURL: string

    The base URL of the supplier's website

    logger: Logger

    The logger for the product builder