1- /**
2- * Netease API 请求层
3- *
4- * 核心职责:根据加密方式(weapi / linuxapi / eapi / api / xeapi)构造 URL、headers、form body,
5- * 处理 cookie 合并、响应解密、状态码归一化
6- */
1+ /** 按客户端协议装配请求并统一处理响应 */
72
83import { randomBytes } from "node:crypto" ;
94import {
@@ -23,9 +18,13 @@ import { getAnonymousToken, getDeviceId } from "./device";
2318import { ensureXeapiKey , getXeapiSession , updateXeapiSession } from "./xeapi" ;
2419import { getAntiCheatTokenV3 } from "./checktoken" ;
2520import { fetchWithProxy } from "@main/utils/proxy" ;
21+ import { NeteaseRequestError , type RequestResponse } from "./response" ;
22+ import { requestWithWebClient } from "./webClient" ;
2623
2724/** 调用方传入的可选参数 */
2825export interface RequestOptions {
26+ /** 网页请求由公共层匹配 SDK 规则并复用会话 */
27+ client ?: "web" ;
2928 /** 加密方式;省略时依据路径默认规则,详见 createRequest */
3029 crypto ?: CryptoMode | "" ;
3130 /** 预注入 cookie,可为字符串或对象 */
@@ -43,25 +42,7 @@ export interface RequestOptions {
4342 checkToken ?: string | boolean ;
4443}
4544
46- /** 响应统一结构 */
47- export interface RequestResponse {
48- status : number ;
49- body : Record < string , unknown > ;
50- cookie : string [ ] ;
51- }
52-
53- /** 非 200 响应抛出的错误 */
54- export class NeteaseRequestError extends Error {
55- readonly response : RequestResponse ;
56- constructor ( response : RequestResponse ) {
57- const body = response . body as { code ?: number | string ; msg ?: string ; message ?: string } ;
58- const code = body ?. code ?? response . status ;
59- const msg = body ?. msg ?? body ?. message ?? "" ;
60- super ( msg ? `netease ${ code } : ${ msg } ` : `netease ${ code } ` ) ;
61- this . name = "NeteaseRequestError" ;
62- this . response = response ;
63- }
64- }
45+ export { NeteaseRequestError , type RequestResponse } from "./response" ;
6546
6647interface NeteaseBody {
6748 code ?: number | string ;
@@ -119,7 +100,7 @@ const processCookieObject = (
119100 appver : cookie . appver || os . appver ,
120101 } ;
121102
122- // 服务端下发条件为不带 NMTID 请求任意 eapi 接口
103+ /** 服务端下发条件为不带 NMTID 请求任意 eapi 接口 */
123104 if ( cookie . NMTID ) {
124105 processed . NMTID = cookie . NMTID ;
125106 } else if ( cachedNmtid ) {
@@ -147,15 +128,17 @@ const chooseUserAgent = (
147128
148129/**
149130 * 构造并发送请求
150- * @param uri 接口路径,例如 `/api/w/login`;weapi 会自动替换前缀为 `/weapi/`
151- * @param data 业务参数(不含 cookie/csrf,请求层会自动注入)
152- * @param options 加密方式 / cookie / 代理等
131+ * @param uri - 接口路径,例如 `/api/w/login`;weapi 会自动替换前缀为 `/weapi/`
132+ * @param data - 业务参数(不含 cookie/csrf,请求层会自动注入)
133+ * @param options - 加密方式 / cookie / 代理等
134+ * @returns 接口响应与会话 Cookie
153135 */
154136export const createRequest = async (
155137 uri : string ,
156138 data : Record < string , unknown > ,
157139 options : RequestOptions ,
158140) : Promise < RequestResponse > => {
141+ if ( options . client === "web" ) return requestWithWebClient ( uri , data , options ) ;
159142 const headers : Record < string , string > = { } ;
160143 const ip = options . realIP || options . ip || "" ;
161144 if ( ip ) {
@@ -166,7 +149,7 @@ export const createRequest = async (
166149 let crypto : CryptoMode | "" = options . crypto ?? "" ;
167150 if ( crypto === "" ) crypto = "eapi" ;
168151
169- // 归一化 cookie 到对象并做一次补全
152+ /** 归一化 cookie 到对象并做一次补全 */
170153 let cookie : Record < string , string > =
171154 typeof options . cookie === "string" ? cookieToJson ( options . cookie ) : options . cookie || { } ;
172155 cookie = processCookieObject ( cookie , crypto ) ;
@@ -327,7 +310,7 @@ export const createRequest = async (
327310 throw new NeteaseRequestError ( answer ) ;
328311 }
329312
330- // 收集 set-cookie 并提取服务端下发的 NMTID
313+ /** 收集 set-cookie 并提取服务端下发的 NMTID */
331314 const setCookie =
332315 ( res . headers as unknown as { getSetCookie ?: ( ) => string [ ] } ) . getSetCookie ?.( ) ??
333316 ( res . headers . get ( "set-cookie" ) ? [ res . headers . get ( "set-cookie" ) as string ] : [ ] ) ;
@@ -344,7 +327,7 @@ export const createRequest = async (
344327 answer . cookie = setCookie . map ( cleanCookie ) ;
345328 }
346329
347- // xeapi 会话密钥由响应头下发,缓存供后续请求复用
330+ /** xeapi 会话密钥由响应头下发,缓存供后续请求复用 */
348331 if ( isXeapi ) {
349332 const ssid = res . headers . get ( "x-encr-ssid" ) ;
350333 const sskey = res . headers . get ( "x-encr-sskey" ) ;
@@ -367,7 +350,7 @@ export const createRequest = async (
367350 parsed = JSON . parse ( text ) ;
368351 }
369352 if ( ! parsed || typeof parsed !== "object" || Array . isArray ( parsed ) ) {
370- throw new Error ( "网易云响应不是有效对象 " ) ;
353+ throw new Error ( "平台响应不是有效对象 " ) ;
371354 }
372355 answer . body = parsed ;
373356 if ( parsed ?. code !== undefined ) parsed . code = Number ( parsed . code ) ;
@@ -377,7 +360,7 @@ export const createRequest = async (
377360 }
378361 } catch {
379362 answer . status = res . status >= 400 ? res . status : 502 ;
380- answer . body = { code : answer . status , msg : "网易云响应解析失败 " } ;
363+ answer . body = { code : answer . status , msg : "平台响应解析失败 " } ;
381364 }
382365
383366 answer . status = answer . status > 100 && answer . status < 600 ? answer . status : 400 ;
0 commit comments