async function makeDomainAddress()

in src/js/background/background.js [313:378]


async function makeDomainAddress(address, block_list_emails, description = null) {
  const apiToken = await browser.storage.local.get("apiToken");

  const { RELAY_SITE_ORIGIN } = await browser.storage.local.get("RELAY_SITE_ORIGIN");
  
  if (!apiToken.apiToken) {
    browser.tabs.create({
      url: RELAY_SITE_ORIGIN,
    });
    return;
  }

  const { relayApiSource } = await browser.storage.local.get("relayApiSource");  
  const serverStoragePermission = await getServerStoragePref();
  const relayApiUrlRelayAddress = `${relayApiSource}/domainaddresses/`;

  let apiBody = {
    "enabled": true,
    "description": "",
    "block_list_emails": block_list_emails,
    "used_on": "",
    "address": address,
  };

  // Only send description/generated_for/used_on fields in the request if the user is opt'd into server storage
  if (description && serverStoragePermission) {
    apiBody.description = description;
    apiBody.generated_for = description;
    // The "," is appended here as this field is a comma-seperated list (but is a strict STRING type in the database). 
    // used_on lists all the different sites the add-on has populated a form field on for this mask
    // Because it contains multiple websites, we're using the CSV structure to explode/filter the string later
    apiBody.used_on = description + ",";
  }


  const headers = await createNewHeadersObject({auth: true});

  const newRelayAddressResponse = await fetch(relayApiUrlRelayAddress, {
    mode: "same-origin",
    method: "POST",
    headers: headers,
    body: JSON.stringify(apiBody),
  });

  // Error Code Context: 
  // 400: Word not allowed (See https://github.com/mozilla/fx-private-relay/blob/main/emails/badwords.text)
  // 402: Currently unknown. See FIXME in makeRelayAddress() function.
  // 409: Custom mask name already exists
  
  if ([402, 409, 400].includes(newRelayAddressResponse.status)) {
      return {status: newRelayAddressResponse.status};
  }

  let newRelayAddressJson = await newRelayAddressResponse.json();

  if (description) {
    newRelayAddressJson.description = description;
    // Store the domain in which the alias was generated, separate from the label
    newRelayAddressJson.generated_for = description;
  }

  // Save the new mask in local storage
  updateLocalStorageAddress(newRelayAddressJson);
 
  return newRelayAddressJson;
}