// {{folder_name}} API 客户端 (高级版本)
// Generated by apifox-flow, do not edit manually

{{#if imports}}
{{#each imports}}
import { {{this}} } from '{{@root.import_path}}';
{{/each}}

{{/if}}
export interface {{class_name}}Config {
  baseUrl: string;
  timeout?: number;
  retries?: number;
  headers?: Record<string, string>;
}

export class {{class_name}} {
  private config: {{class_name}}Config;
  private defaultHeaders: Record<string, string>;

  constructor(config: {{class_name}}Config) {
    this.config = {
      timeout: 10000,
      retries: 3,
      headers: {},
      ...config,
    };
    this.defaultHeaders = {
      'Content-Type': 'application/json',
      ...this.config.headers,
    };
  }

  private async request<T>(
    path: string,
    options: RequestInit = {}
  ): Promise<T> {
    const controller = new AbortController();
    const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);

    try {
      const response = await fetch(`${this.config.baseUrl}${path}`, {
        ...options,
        headers: { ...this.defaultHeaders, ...options.headers },
        signal: controller.signal,
      });

      clearTimeout(timeoutId);

      if (!response.ok) {
        throw new Error(`HTTP ${response.status}: ${response.statusText}`);
      }

      return response.json();
    } catch (error) {
      clearTimeout(timeoutId);
      throw error;
    }
  }

{{#each methods}}
  // {{summary}}
  async {{method_name}}({{#if parameters}}params: { {{#each parameters}}{{name}}{{#unless required}}?{{/unless}}: {{type_}}{{#unless @last}}, {{/unless}}{{/each}} }}{{/if}}): Promise<{{response_type}}> {
    return this.request<{{response_type}}>('{{path}}', {
      method: '{{http_method}}',{{#unless is_get}}
      body: JSON.stringify(params),{{/unless}}
    });
  }

{{/each}}
}
