@@ -34,26 +34,16 @@ export async function createContext({ req }: { req: any }) {
3434
3535// 3. 创建并配置 Fastify 服务器
3636const server = fastify ( { maxParamLength : 5000 } ) ;
37- server . register ( cors , {
37+
38+ // CORS配置 - 使用可靠的配置
39+ const corsOptions = {
3840 origin : config . getCorsOrigins ( ) ,
41+ methods : 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS' ,
42+ allowedHeaders : [ 'Origin' , 'X-Requested-With' , 'Content-Type' , 'Accept' , 'Authorization' ] ,
3943 credentials : true ,
40- methods : [ 'GET' , 'POST' , 'PUT' , 'DELETE' , 'OPTIONS' ] ,
41- allowedHeaders : [ 'Content-Type' , 'Authorization' , 'X-Requested-With' ] ,
42- exposedHeaders : [ 'Content-Type' , 'Authorization' ] ,
43- maxAge : 86400
44- } ) ;
44+ } ;
4545
46- // 添加OPTIONS请求处理器
47- server . addHook ( 'onRequest' , async ( request , reply ) => {
48- if ( request . method === 'OPTIONS' ) {
49- reply . header ( 'Access-Control-Allow-Origin' , request . headers . origin || '*' ) ;
50- reply . header ( 'Access-Control-Allow-Methods' , 'GET, POST, PUT, DELETE, OPTIONS' ) ;
51- reply . header ( 'Access-Control-Allow-Headers' , 'Content-Type, Authorization, X-Requested-With' ) ;
52- reply . header ( 'Access-Control-Allow-Credentials' , 'true' ) ;
53- reply . header ( 'Access-Control-Max-Age' , '86400' ) ;
54- reply . send ( ) ;
55- }
56- } ) ;
46+ server . register ( cors , corsOptions ) ;
5747
5848server . register ( fastifyTRPCPlugin , {
5949 prefix : '/api/trpc' ,
0 commit comments