INNER CODE UNIT · TypeScript

serializeBodyForFetch

stoplightio/prism · packages/http/src/forwarder/index.ts:99

function serializeBodyForFetch(input: IHttpRequest, logger: Logger): E.Either<Error, string | undefined> {
  const upperMethod = input.method.toUpperCase();
  if (['GET', 'HEAD'].includes(upperMethod) && ![null, undefined].includes(input.body as any)) {
    logger.warn(`Upstream ${upperMethod} call to ${input.url.path} has request body`);
    return E.left(ProblemJsonError.fromTemplate(PROXY_UNSUPPORTED_REQUEST_BODY));
  }

  return serializeBody(input.body);
}

export function serializeBody(body: unknown): E.Either<Error, string | undefined> {
  if (typeof body === 'string') {
    return E.right(body);
  }

  if (body) return pipe(J.stringify(body), E.mapLeft(E.toError));

  return E.right(undefined);

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…