mirror of
				https://github.com/Yidadaa/ChatGPT-Next-Web.git
				synced 2025-11-04 08:26:12 +08:00 
			
		
		
		
	* chore: update path * fix: fix google auth logic * fix: not using header authorization for google api * chore: revert to allow stream
		
			
				
	
	
		
			51 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			51 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import { getClientConfig } from "../config/client";
 | 
						|
import { ApiPath, DEFAULT_API_HOST } from "../constant";
 | 
						|
 | 
						|
export function corsPath(path: string) {
 | 
						|
  const baseUrl = getClientConfig()?.isApp ? `${DEFAULT_API_HOST}` : "";
 | 
						|
 | 
						|
  if (!path.startsWith("/")) {
 | 
						|
    path = "/" + path;
 | 
						|
  }
 | 
						|
 | 
						|
  if (!path.endsWith("/")) {
 | 
						|
    path += "/";
 | 
						|
  }
 | 
						|
 | 
						|
  return `${baseUrl}${path}`;
 | 
						|
}
 | 
						|
 | 
						|
export function corsFetch(
 | 
						|
  url: string,
 | 
						|
  options: RequestInit & {
 | 
						|
    proxyUrl?: string;
 | 
						|
  },
 | 
						|
) {
 | 
						|
  if (!url.startsWith("http")) {
 | 
						|
    throw Error("[CORS Fetch] url must starts with http/https");
 | 
						|
  }
 | 
						|
 | 
						|
  let proxyUrl = options.proxyUrl ?? corsPath(ApiPath.Cors);
 | 
						|
  if (!proxyUrl.endsWith("/")) {
 | 
						|
    proxyUrl += "/";
 | 
						|
  }
 | 
						|
 | 
						|
  url = url.replace("://", "/");
 | 
						|
 | 
						|
  const corsOptions = {
 | 
						|
    ...options,
 | 
						|
    method: "POST",
 | 
						|
    headers: options.method
 | 
						|
      ? {
 | 
						|
          ...options.headers,
 | 
						|
          method: options.method,
 | 
						|
        }
 | 
						|
      : options.headers,
 | 
						|
  };
 | 
						|
 | 
						|
  const corsUrl = proxyUrl + url;
 | 
						|
  console.info("[CORS] target = ", corsUrl);
 | 
						|
 | 
						|
  return fetch(corsUrl, corsOptions);
 | 
						|
}
 |