{{#if operation.responses}}
  const responseSchema: {
    [key: string]: { response: string | boolean | object | null; contentTypes: string[] | null; };
  } = {{json operation.responses}};
  let responseStatus: number;
  let acceptedContentTypes: string[] | null;
  let responseJson: unknown;
  let responseText: string;
  let responseObject: Response;
{{/if}}
let responseContentType: string;

try {
  const [processedUrl, processedParams] = await config.preMiddleware(requestUrl, requestParams{{#if operation.header}} as RequestInit{{/if}})
  responseObject = await fetch(processedUrl, processedParams{{#if operation.header}} as RequestInit{{/if}});
  responseContentType = responseObject.headers.get('Content-Type') || 'application/json';

  responseText = await responseObject.text();

  if (responseContentType.startsWith('application/json') || responseContentType.startsWith('application/hal+json')) {
    responseJson = JSON.parse(responseText);
  }

  if (!responseObject.ok) {
    throw new ApiError<typeof responseJson>({
      message: 'Api error while fetching {{operation.name}}',
      status: responseObject.status,
      statusText: responseObject.statusText,
      response: responseJson,
      headers: responseObject.headers,
    });
  }
  {{#if operation.responses}}
    responseStatus = responseObject.status;
    acceptedContentTypes = responseSchema[responseStatus] && responseSchema[responseStatus].contentTypes;

    if (acceptedContentTypes
      && !acceptedContentTypes.includes(responseContentType)
      && (!responseContentType.startsWith('application/json')
        || !acceptedContentTypes.find(acceptedContentType => acceptedContentType.startsWith('application/json')))
    ) {
      throw new Error('Incorrect content type');
    }
  {{/if}}
} catch (error) {
  if (error instanceof ApiError) {
    throw error;
  } else {
    throw error as ClientCodeError;
  }
}

{{#if (and operation.responses options.validateResponse)}}
  function isResponse(obj: unknown): obj is {{capitalize operation.name}}Response {
    const isValid = ajv.validate(responseSchema[responseStatus.toString()].response as string | boolean | object, obj);

    return !!isValid;
  }

  if ((responseContentType.startsWith('application/json') || responseContentType.startsWith('application/hal+json'))
    && responseSchema
    && responseSchema[responseStatus.toString()]
    && responseSchema[responseStatus.toString()].response) {
      if (!isResponse(responseJson)) {
        throw new ResponseValidationError({
          message: 'Response schema validation error',
          method: '{{operation.name}}',
          errors: ajv.errors || [],
        });
      }

    return {
      json: responseJson,
      text: responseText,
      headers: responseObject.headers,
    };
  }

  return {
    json: null,
    text: responseText,
    headers: responseObject.headers,
  };
{{else}}
  return {
    json: responseJson as {{capitalize operation.name}}Response,
    text: responseText,
    headers: responseObject.headers,
  };
{{/if}}

