wix-fetch get with basic authorization

To help you better post if the Console give Errors. :slight_smile:

Try out:

import wixSecretsBackend from 'wix-secrets-backend';
import { fetch } from 'wix-fetch';

async function getKey() {
  try {
    const secret = await wixSecretsBackend.getSecret("key");
    return `:${secret}`;
  } catch (error) {
    console.error(error);
    return '';
  }
}

async function getUser() {
  try {
    const secret = await wixSecretsBackend.getSecret("user");
    return secret;
  } catch (error) {
    console.error(error);
    return '';
  }
}

function encodeKeys(user, key) {
  const encodedSecret = Buffer.from(`${user}${key}`).toString('base64');
  return encodedSecret;
}

async function doFetch() {
  const user = await getUser();
  const key = await getKey();
  const encodedSecret = encodeKeys(user, key);
  const endpointUrl = "someurl";

  try {
    const httpResponse = await fetch(endpointUrl, {
      method: "get",
      headers: {
        Authorization: `Basic ${encodedSecret}`,
        "Content-Type": "application/json",
      },
    });
    if (httpResponse.ok) {
      return httpResponse.json();
    }
    console.log("httpResponse:", httpResponse);
    return Promise.reject("Fetch did not succeed");
  } catch (error) {
    console.log(error);
    return null;
  }
}