import type http from "@ohos:net.http";
import type { Chain } from '../core/Chain';
import type { Interceptor } from './Interceptor';
/**
 * @ProjectName : HarmonyUtilSdk
 * @FileName : RedirectInterceptor
 * @Author : yic
 * @Time : 2024/6/21 11:20
 * @Description : 使用header中的location进行url重定向
 */
export class RedirectInterceptor implements Interceptor {
    async intercept(chain: Chain): Promise<http.HttpResponse> {
        const requestBody = chain.requestBody;
        const interceptorIndex = requestBody.interceptorIndex;
        let response: http.HttpResponse = await chain.process(chain);
        if (response.header['location']) {
            requestBody.interceptorIndex = interceptorIndex;
            requestBody.setUrl(response.header['location']);
            return await chain.process(chain);
        }
        else {
            return response;
        }
    }
}
