/**
 * Fetch 适配器
 * 
 * 基于原生 fetch API 的适配器实现
 */ /**
 * 创建 Fetch 适配器
 */ export async function fetchAdapter() {
    if (typeof fetch === 'undefined') {
        throw new Error('fetch API 不可用');
    }
    return {
        name: 'fetch',
        capabilities: {
            supportsAbort: true,
            supportsTimeout: true,
            supportsProgress: false,
            supportsInterceptors: false
        },
        async request (options) {
            const { url, method = 'GET', headers = {}, query, body, timeout, signal, credentials } = options;
            // 构建完整 URL
            let fullUrl = url;
            if (query) {
                const searchParams = new URLSearchParams();
                for (const [key, value] of Object.entries(query)){
                    if (value !== null && value !== undefined) {
                        searchParams.append(key, String(value));
                    }
                }
                const queryString = searchParams.toString();
                if (queryString) {
                    fullUrl += (fullUrl.includes('?') ? '&' : '?') + queryString;
                }
            }
            // 构建请求头
            const requestHeaders = {};
            for (const [key, value] of Object.entries(headers)){
                requestHeaders[key] = String(value);
            }
            // 构建请求体
            let requestBody;
            if (body !== undefined && body !== null) {
                if (typeof body === 'string' || body instanceof FormData || body instanceof URLSearchParams || body instanceof Blob || body instanceof ArrayBuffer) {
                    requestBody = body;
                } else {
                    // 对象转换为 JSON
                    requestHeaders['Content-Type'] = requestHeaders['Content-Type'] || 'application/json';
                    requestBody = JSON.stringify(body);
                }
            }
            // 创建中止控制器（如果支持超时）
            let abortController;
            let timeoutId;
            if (timeout && timeout > 0) {
                abortController = new AbortController();
                timeoutId = setTimeout(()=>{
                    abortController.abort();
                }, timeout);
            }
            // 合并中止信号
            const abortSignal = signal || abortController?.signal;
            try {
                // 执行请求
                const fetchResponse = await fetch(fullUrl, {
                    method,
                    headers: requestHeaders,
                    body: requestBody,
                    signal: abortSignal,
                    credentials: credentials || 'same-origin'
                });
                // 解析响应数据
                let data;
                const responseType = options.responseType || 'json';
                switch(responseType){
                    case 'text':
                        data = await fetchResponse.text();
                        break;
                    case 'blob':
                        data = await fetchResponse.blob();
                        break;
                    case 'arrayBuffer':
                        data = await fetchResponse.arrayBuffer();
                        break;
                    case 'formData':
                        data = await fetchResponse.formData();
                        break;
                    case 'json':
                    default:
                        try {
                            data = await fetchResponse.json();
                        } catch  {
                            // 如果 JSON 解析失败，返回空对象
                            data = {};
                        }
                        break;
                }
                // 构建响应头对象
                const responseHeaders = {};
                fetchResponse.headers.forEach((value, key)=>{
                    responseHeaders[key] = value;
                });
                // 检查 HTTP 状态码
                if (!fetchResponse.ok) {
                    throw new Error(`HTTP ${fetchResponse.status} ${fetchResponse.statusText}`);
                }
                // 返回响应对象
                return {
                    data,
                    status: fetchResponse.status,
                    statusText: fetchResponse.statusText,
                    headers: responseHeaders,
                    raw: fetchResponse
                };
            } catch (error) {
                // 清理超时定时器
                if (timeoutId) {
                    clearTimeout(timeoutId);
                }
                // 重新抛出错误
                throw error;
            }
        }
    };
}

//# sourceMappingURL=fetchAdapter.js.map