Skip to content

The open alternative to Salesforce, designed for AI.

2.6 KB
Raw Blame
import { isNumber, isObject } from '@sniptt/guards';

import { PDL_BASE_URL } from 'src/constants/pdl-base-url';
import { extractPdlErrorMessage } from 'src/logic-functions/utils/extract-pdl-error-message';
import { getPdlApiKey } from 'src/logic-functions/utils/get-pdl-api-key';
import { parsePdlItem } from 'src/logic-functions/utils/parse-pdl-item';
import { type PdlEnrichResult } from 'src/types/pdl-enrich-result';

export const postPdlBulkEnrich = async <TData>({
  path,
  requests,
}: {
  path: string;
  requests: Record<string, unknown>[];
}): Promise<PdlEnrichResult<TData>[]> => {
  if (requests.length === 0) {
    return [];
  }

  const apiKey = getPdlApiKey();

  const failAll = ({
    message,
    httpStatus,
  }: {
    message: string;
    httpStatus: number;
  }): PdlEnrichResult<TData>[] =>
    requests.map(() => ({ outcome: 'error', httpStatus, message }));

  let response: Response;
  try {
    response = await fetch(`${PDL_BASE_URL}${path}`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'X-Api-Key': apiKey,
      },
      body: JSON.stringify({ requests: requests.map((params) => ({ params })) }),
    });
  } catch (error) {
    const message = error instanceof Error ? error.message : String(error);

    return failAll({ message: `PDL request failed: ${message}`, httpStatus: 0 });
  }

  let json: unknown;
  try {
    json = await response.json();
  } catch {
    return failAll({
      message: `PDL returned a non-JSON response (HTTP ${response.status}).`,
      httpStatus: response.status,
    });
  }

  if (!response.ok) {
    const message = isObject(json)
      ? extractPdlErrorMessage({
          json: json as Record<string, unknown>,
          httpStatus: response.status,
        })
      : `PDL request failed (HTTP ${response.status}).`;

    return failAll({ message, httpStatus: response.status });
  }

  const responseItems = Array.isArray(json)
    ? json
    : isObject(json) && Array.isArray((json as Record<string, unknown>).responses)
      ? ((json as Record<string, unknown>).responses as unknown[])
      : [];

  if (responseItems.length !== requests.length) {
    return failAll({
      message: `People Data Labs returned ${responseItems.length} results for ${requests.length} requests (HTTP ${response.status}).`,
      httpStatus: response.status,
    });
  }

  return requests.map((requestParams, index) =>
    parsePdlItem<TData>({
      item: responseItems[index],
      requestedMinLikelihood: isNumber(requestParams.min_likelihood)
        ? requestParams.min_likelihood
        : undefined,
    }),
  );
};