

























@@ -3,6 +3,7 @@ import { fetchWithSsrFGuard, type MSTeamsConfig } from "../runtime-api.js";
33import { GRAPH_ROOT } from "./attachments/shared.js";
4455const 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: {
4546errorPrefix?: string;
4647}): Promise<Response> {
4748const 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}
66836784async function readOptionalGraphJson<T>(res: Response): Promise<T> {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。