@@ -148,7 +148,12 @@ function isRecord(value: unknown): value is Record<string, unknown> {
148148type HttpInstanceLike = {
149149 request : ( options ?: Record < string , unknown > ) => Promise < unknown > ;
150150 get : ( url : string , options ?: Record < string , unknown > ) => Promise < unknown > ;
151+ delete : ( url : string , options ?: Record < string , unknown > ) => Promise < unknown > ;
152+ head : ( url : string , options ?: Record < string , unknown > ) => Promise < unknown > ;
153+ options : ( url : string , options ?: Record < string , unknown > ) => Promise < unknown > ;
151154 post : ( url : string , body ?: unknown , options ?: Record < string , unknown > ) => Promise < unknown > ;
155+ put : ( url : string , body ?: unknown , options ?: Record < string , unknown > ) => Promise < unknown > ;
156+ patch : ( url : string , body ?: unknown , options ?: Record < string , unknown > ) => Promise < unknown > ;
152157} ;
153158
154159function requireHttpInstance ( value : unknown ) : HttpInstanceLike {
@@ -158,11 +163,7 @@ function requireHttpInstance(value: unknown): HttpInstanceLike {
158163 typeof value . get === "function" &&
159164 typeof value . post === "function"
160165 ) {
161- return {
162- request : value . request as HttpInstanceLike [ "request" ] ,
163- get : value . get as HttpInstanceLike [ "get" ] ,
164- post : value . post as HttpInstanceLike [ "post" ] ,
165- } ;
166+ return value as HttpInstanceLike ;
166167 }
167168 throw new Error ( "expected Feishu HTTP instance" ) ;
168169}
@@ -199,7 +200,7 @@ function firstWsClientOptions(): {
199200beforeAll ( async ( ) => {
200201 vi . doMock ( "@larksuiteoapi/node-sdk" , ( ) => ( {
201202 AppType : { SelfBuild : "self" } ,
202- Domain : { Feishu : "https://open.feishu.cn" , Lark : "https://open.larksuite.com" } ,
203+ Domain : { Feishu : 0 , Lark : 1 } ,
203204 LoggerLevel : { info : "info" } ,
204205 Client : clientCtorMock ,
205206 WSClient : wsClientCtorMock ,
@@ -277,6 +278,148 @@ describe("Feishu default User-Agent interceptor", () => {
277278 } ) ;
278279} ) ;
279280
281+ describe ( "Feishu custom HTTPS API domains" , ( ) => {
282+ const sdkOrigin = "https://open.feishu.cn" ;
283+ const customDomain = "https://private.feishu.test:8443/reverse-proxy/" ;
284+
285+ function createCustomDomainHttpInstance ( accountId : string ) : HttpInstanceLike {
286+ createFeishuClient ( {
287+ appId : `app_${ accountId } ` ,
288+ appSecret : "local-test-placeholder" , // pragma: allowlist secret
289+ accountId,
290+ domain : customDomain ,
291+ } ) ;
292+ expect ( readCallOptions ( clientCtorMock ) . domain ) . toBe ( 0 ) ;
293+ return requireHttpInstance ( readCallOptions ( clientCtorMock ) . httpInstance ) ;
294+ }
295+
296+ it . each ( [ "get" , "delete" , "head" , "options" ] as const ) (
297+ "routes SDK %s requests through the configured HTTPS origin, port, and path" ,
298+ async ( method ) => {
299+ const httpInstance = createCustomDomainHttpInstance ( `custom-domain-${ method } ` ) ;
300+ const requestUrl = `${ sdkOrigin } /open-apis/im/v1/messages/om_proof?locale=en` ;
301+
302+ await httpInstance [ method ] ( requestUrl , { headers : { "X-Proof" : "true" } } ) ;
303+
304+ expect ( mockBaseHttpInstance [ method ] ) . toHaveBeenCalledWith (
305+ "https://private.feishu.test:8443/reverse-proxy/open-apis/im/v1/messages/om_proof?locale=en" ,
306+ { timeout : FEISHU_HTTP_TIMEOUT_MS , headers : { "X-Proof" : "true" } } ,
307+ ) ;
308+ } ,
309+ ) ;
310+
311+ it . each ( [ "post" , "put" , "patch" ] as const ) (
312+ "routes SDK %s requests without changing their body or request options" ,
313+ async ( method ) => {
314+ const httpInstance = createCustomDomainHttpInstance ( `custom-domain-${ method } ` ) ;
315+ const body = { content : "preserve this body" } ;
316+
317+ await httpInstance [ method ] ( `${ sdkOrigin } /open-apis/im/v1/messages` , body , {
318+ headers : { "X-Proof" : "true" } ,
319+ } ) ;
320+
321+ expect ( mockBaseHttpInstance [ method ] ) . toHaveBeenCalledWith (
322+ "https://private.feishu.test:8443/reverse-proxy/open-apis/im/v1/messages" ,
323+ body ,
324+ { timeout : FEISHU_HTTP_TIMEOUT_MS , headers : { "X-Proof" : "true" } } ,
325+ ) ;
326+ } ,
327+ ) ;
328+
329+ it ( "routes request options after preserving Feishu SDK multipart normalization" , async ( ) => {
330+ const httpInstance = createCustomDomainHttpInstance ( "custom-domain-upload" ) ;
331+
332+ await httpInstance . request ( {
333+ url : `${ sdkOrigin } /open-apis/im/v1/files?folder=root` ,
334+ method : "POST" ,
335+ headers : { "Content-Type" : "multipart/form-data" } ,
336+ data : { file_type : "stream" , file_name : "proof.txt" , file : Buffer . from ( "proof" ) } ,
337+ } ) ;
338+
339+ const delegated = readCallOptions ( mockBaseHttpInstance . request ) ;
340+ expect ( delegated . url ) . toBe (
341+ "https://private.feishu.test:8443/reverse-proxy/open-apis/im/v1/files?folder=root" ,
342+ ) ;
343+ expect ( delegated . timeout ) . toBe ( FEISHU_HTTP_TIMEOUT_MS ) ;
344+ expect ( delegated . data ) . toBeInstanceOf ( FormData ) ;
345+ expect ( ( delegated . data as FormData ) . get ( "file_type" ) ) . toBe ( "stream" ) ;
346+ } ) ;
347+
348+ it . each ( [
349+ "https://open.feishu.cn.evil.test/open-apis/im/v1/messages" ,
350+ "https://open.feishu.cn:444/open-apis/im/v1/messages" ,
351+ "https://unrelated.example/open-apis/im/v1/messages" ,
352+ ] ) ( "does not rewrite an unrelated request origin: %s" , async ( url ) => {
353+ const httpInstance = createCustomDomainHttpInstance ( `custom-domain-external-${ url . length } ` ) ;
354+
355+ await httpInstance . get ( url ) ;
356+
357+ expect ( mockBaseHttpInstance . get ) . toHaveBeenCalledWith ( url , {
358+ timeout : FEISHU_HTTP_TIMEOUT_MS ,
359+ } ) ;
360+ } ) ;
361+
362+ it ( "keeps independent account transport origins isolated" , async ( ) => {
363+ const first = createCustomDomainHttpInstance ( "custom-domain-first-account" ) ;
364+ createFeishuClient ( {
365+ appId : "app_second_account" ,
366+ appSecret : "local-test-placeholder" , // pragma: allowlist secret
367+ accountId : "custom-domain-second-account" ,
368+ domain : "https://another.feishu.test:9443/tenant" ,
369+ } ) ;
370+ const second = requireHttpInstance ( readCallOptions ( clientCtorMock ) . httpInstance ) ;
371+
372+ await first . get ( `${ sdkOrigin } /open-apis/im/v1/chats/oc_first` ) ;
373+ await second . get ( `${ sdkOrigin } /open-apis/im/v1/chats/oc_second` ) ;
374+
375+ expect ( mockBaseHttpInstance . get . mock . calls . map ( ( call ) => call [ 0 ] ) ) . toEqual ( [
376+ "https://private.feishu.test:8443/reverse-proxy/open-apis/im/v1/chats/oc_first" ,
377+ "https://another.feishu.test:9443/tenant/open-apis/im/v1/chats/oc_second" ,
378+ ] ) ;
379+ } ) ;
380+
381+ it ( "routes WebSocket endpoint discovery through the same configured origin" , async ( ) => {
382+ await createFeishuWSClient ( {
383+ ...baseAccount ,
384+ accountId : "custom-domain-websocket" ,
385+ domain : customDomain ,
386+ } ) ;
387+
388+ const options = readCallOptions ( wsClientCtorMock ) ;
389+ expect ( options . domain ) . toBe ( 0 ) ;
390+ const httpInstance = requireHttpInstance ( options . httpInstance ) ;
391+ await httpInstance . request ( { url : `${ sdkOrigin } /callback/ws/endpoint` , method : "post" } ) ;
392+
393+ expect ( mockBaseHttpInstance . request ) . toHaveBeenCalledWith ( {
394+ url : "https://private.feishu.test:8443/reverse-proxy/callback/ws/endpoint" ,
395+ method : "post" ,
396+ timeout : FEISHU_HTTP_TIMEOUT_MS ,
397+ } ) ;
398+ } ) ;
399+
400+ it . each ( [
401+ [ "feishu" , 0 , "https://open.feishu.cn" ] ,
402+ [ "lark" , 1 , "https://open.larksuite.com" ] ,
403+ ] as const ) (
404+ "preserves the existing %s SDK domain and direct requests" ,
405+ async ( domain , sdkDomain , url ) => {
406+ createFeishuClient ( {
407+ appId : `app_${ domain } ` ,
408+ appSecret : "local-test-placeholder" , // pragma: allowlist secret
409+ accountId : `official-domain-${ domain } ` ,
410+ domain,
411+ } ) ;
412+
413+ const options = readCallOptions ( clientCtorMock ) ;
414+ expect ( options . domain ) . toBe ( sdkDomain ) ;
415+ await requireHttpInstance ( options . httpInstance ) . get ( `${ url } /open-apis/im/v1/messages` ) ;
416+ expect ( mockBaseHttpInstance . get ) . toHaveBeenCalledWith ( `${ url } /open-apis/im/v1/messages` , {
417+ timeout : FEISHU_HTTP_TIMEOUT_MS ,
418+ } ) ;
419+ } ,
420+ ) ;
421+ } ) ;
422+
280423describe ( "createFeishuClient HTTP timeout" , ( ) => {
281424 const readLastClientHttpInstance = ( ) : HttpInstanceLike =>
282425 requireHttpInstance ( readCallOptions ( clientCtorMock ) . httpInstance ) ;
0 commit comments