ms-vite-plugin 1.1.22 → 1.1.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -101,6 +101,41 @@ function appendQueryParams(url, query) {
101
101
  url.searchParams.set(key, String(value));
102
102
  }
103
103
  }
104
+ /**
105
+ * 构建 JSON HTTP 请求体
106
+ * @param body 工具调用传入的 body
107
+ * @returns 返回可直接用于 fetch 的请求体和头
108
+ * @example
109
+ * createHttpRequestBody({ actions: [] })
110
+ */
111
+ function createHttpRequestBody(body) {
112
+ if (body === undefined) {
113
+ return {};
114
+ }
115
+ const headers = {
116
+ "Content-Type": "application/json",
117
+ };
118
+ if (typeof body === "string") {
119
+ const trimmedBody = body.trim();
120
+ try {
121
+ JSON.parse(trimmedBody);
122
+ return {
123
+ headers,
124
+ requestBody: trimmedBody,
125
+ };
126
+ }
127
+ catch {
128
+ return {
129
+ headers,
130
+ requestBody: JSON.stringify(body),
131
+ };
132
+ }
133
+ }
134
+ return {
135
+ headers,
136
+ requestBody: JSON.stringify(body),
137
+ };
138
+ }
104
139
  /**
105
140
  * 读取响应体并按要求格式化
106
141
  * @param response fetch 响应对象
@@ -262,7 +297,7 @@ function registerHttpApiTools(server) {
262
297
  body: z
263
298
  .unknown()
264
299
  .optional()
265
- .describe("POST 请求体;对象会按 JSON 提交"),
300
+ .describe("POST 请求体;始终按 JSON 提交。推荐传 JSON 对象;JSON 字符串会按原 JSON 提交,普通字符串会作为 JSON 字符串值提交。"),
266
301
  responseFormat: z
267
302
  .enum(["json", "text"])
268
303
  .optional()
@@ -303,22 +338,11 @@ function registerHttpApiTools(server) {
303
338
  const controller = new AbortController();
304
339
  const timeout = setTimeout(() => controller.abort(), timeoutMs);
305
340
  try {
306
- const headers = {};
307
- let requestBody;
308
- if (body !== undefined) {
309
- if (typeof body === "string") {
310
- requestBody = body;
311
- headers["Content-Type"] = "text/plain; charset=utf-8";
312
- }
313
- else {
314
- requestBody = JSON.stringify(body);
315
- headers["Content-Type"] = "application/json";
316
- }
317
- }
341
+ const { headers, requestBody } = createHttpRequestBody(body);
318
342
  const response = await fetch(url.toString(), {
319
343
  method,
320
344
  signal: controller.signal,
321
- headers: Object.keys(headers).length > 0 ? headers : undefined,
345
+ headers,
322
346
  body: requestBody,
323
347
  });
324
348
  const responseText = await readHttpApiResponseText(response, responseFormat);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ms-vite-plugin",
3
- "version": "1.1.22",
3
+ "version": "1.1.23",
4
4
  "type": "commonjs",
5
5
  "license": "MIT",
6
6
  "publishConfig": {