async function defaultParseResponse()

in core.ts [40:91]


async function defaultParseResponse<T>(
  props: APIResponseProps,
): Promise<WithRequestID<T>> {
  const { response } = props;
  if (props.options.stream) {
    debug(
      "response",
      response.status,
      response.url,
      response.headers,
      response.body,
    );

    // Note: there is an invariant here that isn't represented in the type system
    // that if you set `stream: true` the response type must also be `Stream<T>`

    if (props.options.__streamClass) {
      return props.options.__streamClass.fromSSEResponse(
        response,
        props.controller,
      ) as any;
    }

    return Stream.fromSSEResponse(response, props.controller) as any;
  }

  // fetch refuses to read the body when the status code is 204.
  if (response.status === 204) {
    return null as WithRequestID<T>;
  }

  if (props.options.__binaryResponse) {
    return response as unknown as WithRequestID<T>;
  }

  const contentType = response.headers.get("content-type");
  const isJSON = contentType?.includes("application/json") ||
    contentType?.includes("application/vnd.api+json");
  if (isJSON) {
    const json = await response.json();

    debug("response", response.status, response.url, response.headers, json);

    return _addRequestID(json, response);
  }

  const text = await response.text();
  debug("response", response.status, response.url, response.headers, text);

  // TODO handle blob, arraybuffer, other content types, etc.
  return text as unknown as WithRequestID<T>;
}