@@ -3,6 +3,7 @@ import { fetchWithSsrFGuard, type MSTeamsConfig } from "../runtime-api.js";
33import { GRAPH_ROOT } from "./attachments/shared.js" ;
44
55const GRAPH_BETA = "https://graph.microsoft.com/beta" ;
6+ const NULL_BODY_STATUSES = new Set ( [ 101 , 204 , 205 , 304 ] ) ;
67import { createMSTeamsTokenProvider , loadMSTeamsSdkWithAuth } from "./sdk.js" ;
78import { readAccessToken } from "./token-response.js" ;
89import { resolveDelegatedAccessToken , resolveMSTeamsCredentials } from "./token.js" ;
@@ -45,23 +46,39 @@ async function requestGraph(params: {
4546 errorPrefix ?: string ;
4647} ) : Promise < Response > {
4748 const hasBody = params . body !== undefined ;
48- const res = await fetch ( `${ params . root ?? GRAPH_ROOT } ${ params . path } ` , {
49- method : params . method ,
50- headers : {
51- "User-Agent" : buildUserAgent ( ) ,
52- Authorization : `Bearer ${ params . token } ` ,
53- ...( hasBody ? { "Content-Type" : "application/json" } : { } ) ,
54- ...params . headers ,
49+ const url = `${ params . root ?? GRAPH_ROOT } ${ params . path } ` ;
50+ const currentFetch = globalThis . fetch ;
51+ const { response, release } = await fetchWithSsrFGuard ( {
52+ url,
53+ fetchImpl : async ( input , guardedInit ) => await currentFetch ( input , guardedInit ) ,
54+ init : {
55+ method : params . method ,
56+ headers : {
57+ "User-Agent" : buildUserAgent ( ) ,
58+ Authorization : `Bearer ${ params . token } ` ,
59+ ...( hasBody ? { "Content-Type" : "application/json" } : { } ) ,
60+ ...params . headers ,
61+ } ,
62+ body : hasBody ? JSON . stringify ( params . body ) : undefined ,
5563 } ,
56- body : hasBody ? JSON . stringify ( params . body ) : undefined ,
64+ auditContext : "msteams.graph" ,
5765 } ) ;
58- if ( ! res . ok ) {
59- const text = await res . text ( ) . catch ( ( ) => "" ) ;
60- throw new Error (
61- `${ params . errorPrefix ?? "Graph" } ${ params . path } failed (${ res . status } ): ${ text || "unknown error" } ` ,
62- ) ;
66+ try {
67+ if ( ! response . ok ) {
68+ const text = await response . text ( ) . catch ( ( ) => "" ) ;
69+ throw new Error (
70+ `${ params . errorPrefix ?? "Graph" } ${ params . path } failed (${ response . status } ): ${ text || "unknown error" } ` ,
71+ ) ;
72+ }
73+ const body = NULL_BODY_STATUSES . has ( response . status ) ? null : await response . arrayBuffer ( ) ;
74+ return new Response ( body , {
75+ status : response . status ,
76+ statusText : response . statusText ,
77+ headers : new Headers ( response . headers ) ,
78+ } ) ;
79+ } finally {
80+ await release ( ) ;
6381 }
64- return res ;
6582}
6683
6784async function readOptionalGraphJson < T > ( res : Response ) : Promise < T > {
0 commit comments