@bettergi/utils 0.0.6 → 0.0.7

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.
package/README.md CHANGED
@@ -10,6 +10,8 @@ npm install @bettergi/utils
10
10
 
11
11
  ### 游戏内操作
12
12
 
13
+ > 常见游戏内操作封装,省去手动实现的繁琐。
14
+
13
15
  ```ts
14
16
  import { openMenu, openMenuPage, openPaimonMenu, setTime } from "@bettergi/utils";
15
17
 
@@ -134,3 +136,16 @@ try {
134
136
  state.lastUsedTime = Date.now(); // 自动同步保存到文件
135
137
  }
136
138
  ```
139
+
140
+ ### 网络请求
141
+
142
+ > 对网络请求的简易封装。
143
+
144
+ ```ts
145
+ import { getForBody } from "@bettergi/utils";
146
+
147
+ // 发送 GET 请求获取响应体内容
148
+ // 提示:需要在 `manifest.json` 文件中配置 `http_allowed_urls`,并在 调度器 -> 修改通用配置 中启用
149
+ const body = await getForBody("https://example.com/", undefined, { "User-Agent": "BetterGI" });
150
+ log.info(`响应体内容${body}`);
151
+ ```
package/dist/http.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ /**
2
+ * 发送 HTTP 请求,获取响应体内容
3
+ * @param method 请求方法
4
+ * @param url 请求 URL
5
+ * @param body 请求体
6
+ * @param headers 请求头
7
+ * @returns 响应体内容
8
+ */
9
+ export declare const requestForBody: (method: Parameters<typeof http.request>[0], url: string, body?: string, headers?: Record<string, any>) => Promise<string>;
10
+ /**
11
+ * 发送 HTTP GET 请求,获取响应体内容
12
+ * @param url 请求 URL
13
+ * @param body 请求体
14
+ * @param headers 请求头
15
+ * @returns 响应体内容
16
+ */
17
+ export declare const getForBody: (url: string, body?: string, headers?: Record<string, any>) => Promise<string>;
18
+ /**
19
+ * 发送 HTTP POST 请求,获取响应体内容
20
+ * @param url 请求 URL
21
+ * @param body 请求体
22
+ * @param headers 请求头
23
+ * @returns 响应体内容
24
+ */
25
+ export declare const postForBody: (url: string, body?: string, headers?: Record<string, any>) => Promise<{
26
+ status_code: number;
27
+ headers: Record<string, string>;
28
+ body: string;
29
+ }>;
package/dist/http.js ADDED
@@ -0,0 +1,37 @@
1
+ /**
2
+ * 发送 HTTP 请求,获取响应体内容
3
+ * @param method 请求方法
4
+ * @param url 请求 URL
5
+ * @param body 请求体
6
+ * @param headers 请求头
7
+ * @returns 响应体内容
8
+ */
9
+ export const requestForBody = async (method, url, body, headers) => {
10
+ const resp = await http.request(method, url, body ?? "null", headers ? JSON.stringify(headers) : "null");
11
+ if (resp.status_code >= 200 && resp.status_code < 400) {
12
+ return resp.body;
13
+ }
14
+ else {
15
+ throw new Error(`HTTP request failed with status ${resp.status_code}`);
16
+ }
17
+ };
18
+ /**
19
+ * 发送 HTTP GET 请求,获取响应体内容
20
+ * @param url 请求 URL
21
+ * @param body 请求体
22
+ * @param headers 请求头
23
+ * @returns 响应体内容
24
+ */
25
+ export const getForBody = async (url, body, headers) => {
26
+ return requestForBody("GET", url, body, headers);
27
+ };
28
+ /**
29
+ * 发送 HTTP POST 请求,获取响应体内容
30
+ * @param url 请求 URL
31
+ * @param body 请求体
32
+ * @param headers 请求头
33
+ * @returns 响应体内容
34
+ */
35
+ export const postForBody = (url, body, headers) => {
36
+ return http.request("POST", url, body, headers ? JSON.stringify(headers) : undefined);
37
+ };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./flow";
2
2
  export * from "./game";
3
+ export * from "./http";
3
4
  export * from "./mouse";
4
5
  export * from "./ocr";
5
6
  export * from "./store";
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./flow";
2
2
  export * from "./game";
3
+ export * from "./http";
3
4
  export * from "./mouse";
4
5
  export * from "./ocr";
5
6
  export * from "./store";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bettergi/utils",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "Utils for BetterGI JavaScript Development",
5
5
  "type": "module",
6
6
  "author": "Bread Grocery<https://github.com/breadgrocery>",
@@ -33,7 +33,7 @@
33
33
  "build": "tsc"
34
34
  },
35
35
  "devDependencies": {
36
- "@bettergi/types": "^0.0.10",
36
+ "@bettergi/types": "^0.0.11",
37
37
  "typescript": "5.6.3"
38
38
  }
39
39
  }