async function executeFunction()

in src/features/chat-page/chat-services/chat-api/chat-api-dynamic-extensions.ts [59:139]


async function executeFunction(props: {
  functionModel: ExtensionFunctionModel;
  extensionModel: ExtensionModel;
  args: any;
}) {
  try {
    const { functionModel, args, extensionModel } = props;

    // get the secure headers
    const headerPromise = extensionModel.headers.map(async (h) => {
      const headerValue = await FindSecureHeaderValue(h.id);

      if (headerValue.status === "OK") {
        return {
          id: h.id,
          key: h.key,
          value: headerValue.response,
        };
      }

      return {
        id: h.id,
        key: h.key,
        value: "***",
      };
    });

    const headerItems = await Promise.all(headerPromise);

    // we need to add the user id to the headers as this is expected by the function and does not have context of the user
    headerItems.push({
      id: "authorization",
      key: "authorization",
      value: await userHashedId(),
    });
    // map the headers to a dictionary
    const headers: { [key: string]: string } = headerItems.reduce(
      (acc: { [key: string]: string }, header) => {
        acc[header.key] = header.value;
        return acc;
      },
      {}
    );

    // replace the query parameters
    if (args.query) {
      for (const key in args.query) {
        const value = args.query[key];
        functionModel.endpoint = functionModel.endpoint.replace(
          `${key}`,
          value
        );
      }
    }

    const requestInit: RequestInit = {
      method: functionModel.endpointType,
      headers: headers,
      cache: "no-store",
    };

    if (args.body) {
      requestInit.body = JSON.stringify(args.body);
    }

    const response = await fetch(functionModel.endpoint, requestInit);

    if (!response.ok) {
      return `There was an error calling the api: ${response.statusText}`;
    }
    const result = await response.json();

    return {
      id: functionModel.id,
      result: result,
    };
  } catch (e) {
    console.error("🔴", e);
    return `There was an error calling the api: ${e}`;
  }
}