export async function translateDocuments()

in src/contentScripts/functions.ts [116:140]


export async function translateDocuments(
  creds: TranslateClientConfig,
  SourceLanguageCode: string,
  TargetLanguageCode: string,
  docs: Documents
): Promise<Documents> {
  const client = new TranslateClient(creds);
  const responses = await sendDocumentsToTranslate(
    client,
    SourceLanguageCode,
    TargetLanguageCode,
    docs
  );

  if (responses.some(res => res.status === 'rejected')) {
    throw new Error('One or more parts of the document failed to translate.');
  }

  return responses.reduce((docs, response) => {
    if (response.status === 'fulfilled') {
      return docs.concat([response.value.TranslatedText ?? '']);
    }
    return docs;
  }, [] as Documents);
}