async function makeRelayAddress()

in src/js/background/background.js [382:441]


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

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

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

  // 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),
  });

  if (newRelayAddressResponse.status === 402) {
    // FIXME: can this just return newRelayAddressResponse ?
    return { status: 402 };
  }

  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;
}