@bettergi/utils 0.0.6 → 0.0.8

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,18 @@ try {
134
136
  state.lastUsedTime = Date.now(); // 自动同步保存到文件
135
137
  }
136
138
  ```
139
+
140
+ ### 网络请求
141
+
142
+ > 对网络请求的简易封装。
143
+
144
+ ```ts
145
+ import { getForBody, postForBody } from "@bettergi/utils";
146
+
147
+ // 发送 GET 请求获取响应体内容
148
+ // 提示:需要在 `manifest.json` 文件中配置 `http_allowed_urls`,并在 调度器 -> 修改通用配置 中启用
149
+ const body1 = await getForBody("https://example.com/", null, { "User-Agent": "BetterGI" });
150
+ const body2 = await postForBody("https://example.com/", null, { "User-Agent": "BetterGI" });
151
+ log.info(`GET 请求响应体内容${body1}`);
152
+ log.info(`POST 请求响应体内容${body2}`);
153
+ ```
package/dist/game.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import "@bettergi/types/csharp/BetterGenshinImpact/GameTask/Model/Area/Region";
1
2
  /**
2
3
  * 打开派蒙菜单
3
4
  */
package/dist/game.js CHANGED
@@ -1,3 +1,4 @@
1
+ import "@bettergi/types/csharp/BetterGenshinImpact/GameTask/Model/Area/Region";
1
2
  import { waitUntil } from "./flow";
2
3
  import { mouseSlide } from "./mouse";
3
4
  import { findTextInDirection, findTextWithinBounds, findTextWithinListView } from "./ocr";
package/dist/http.d.ts ADDED
@@ -0,0 +1,25 @@
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<string>;
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 requestForBody("POST", url, body, headers);
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/dist/ocr.d.ts CHANGED
@@ -1,11 +1,12 @@
1
- import { type Region } from "@bettergi/types/csharp/BetterGenshinImpact/GameTask/Model/Area/Region";
1
+ import "@bettergi/types/csharp/BetterGenshinImpact/GameTask/Model/Area/ImageRegion";
2
+ import "@bettergi/types/csharp/BetterGenshinImpact/GameTask/Model/Area/Region";
2
3
  type Direction = "north" | "north-east" | "east" | "south-east" | "south" | "south-west" | "west" | "north-west";
3
4
  /**
4
5
  * 在整个画面内搜索图片
5
6
  * @param path 图片路径
6
7
  * @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
7
8
  */
8
- export declare const findImage: (path: string) => Region | undefined;
9
+ export declare const findImage: (path: string) => globalThis.Region | undefined;
9
10
  /**
10
11
  * 在指定区域内搜索图片
11
12
  * @param path 图片路径
@@ -15,14 +16,14 @@ export declare const findImage: (path: string) => Region | undefined;
15
16
  * @param h 高度
16
17
  * @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
17
18
  */
18
- export declare const findImageWithinBounds: (path: string, x: number, y: number, w: number, h: number) => Region | undefined;
19
+ export declare const findImageWithinBounds: (path: string, x: number, y: number, w: number, h: number) => globalThis.Region | undefined;
19
20
  /**
20
21
  * 在指定方向上搜索图片
21
22
  * @param path 图片路径
22
23
  * @param direction 搜索方向
23
24
  * @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
24
25
  */
25
- export declare const findImageInDirection: (path: string, direction: Direction) => Region | undefined;
26
+ export declare const findImageInDirection: (path: string, direction: Direction) => globalThis.Region | undefined;
26
27
  /**
27
28
  * 在整个画面内搜索文本
28
29
  * @param text 待搜索文本
@@ -30,7 +31,7 @@ export declare const findImageInDirection: (path: string, direction: Direction)
30
31
  * @param ignoreCase 是否忽略大小写
31
32
  * @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
32
33
  */
33
- export declare const findText: (text: string, contains: boolean, ignoreCase: boolean) => Region | undefined;
34
+ export declare const findText: (text: string, contains: boolean, ignoreCase: boolean) => globalThis.Region | undefined;
34
35
  /**
35
36
  * 在指定区域内搜索文本
36
37
  * @param text 待搜索文本
@@ -43,7 +44,7 @@ export declare const findText: (text: string, contains: boolean, ignoreCase: boo
43
44
  * @param h 高度
44
45
  * @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
45
46
  */
46
- export declare const findTextWithinBounds: (text: string, contains: boolean, ignoreCase: boolean, x: number, y: number, w: number, h: number) => Region | undefined;
47
+ export declare const findTextWithinBounds: (text: string, contains: boolean, ignoreCase: boolean, x: number, y: number, w: number, h: number) => globalThis.Region | undefined;
47
48
  /**
48
49
  * 在指定方向上搜索文本
49
50
  * @param text 待搜索文本
@@ -52,7 +53,7 @@ export declare const findTextWithinBounds: (text: string, contains: boolean, ign
52
53
  * @param direction 搜索方向
53
54
  * @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
54
55
  */
55
- export declare const findTextInDirection: (text: string, contains: boolean, ignoreCase: boolean, direction: Direction) => Region | undefined;
56
+ export declare const findTextInDirection: (text: string, contains: boolean, ignoreCase: boolean, direction: Direction) => globalThis.Region | undefined;
56
57
  /**
57
58
  * 在列表视图中滚动搜索文本
58
59
  * @param text 待搜索文本
@@ -70,5 +71,5 @@ export declare const findTextWithinListView: (text: string, contains: boolean, i
70
71
  maxListItems: number;
71
72
  lineHeight: number;
72
73
  padding?: number;
73
- }, timeout?: number) => Promise<Region | undefined>;
74
+ }, timeout?: number) => Promise<globalThis.Region | undefined>;
74
75
  export {};
package/dist/ocr.js CHANGED
@@ -1,3 +1,5 @@
1
+ import "@bettergi/types/csharp/BetterGenshinImpact/GameTask/Model/Area/ImageRegion";
2
+ import "@bettergi/types/csharp/BetterGenshinImpact/GameTask/Model/Area/Region";
1
3
  import { waitUntil } from "./flow";
2
4
  import { mouseScrollDownLines } from "./mouse";
3
5
  const findFirst = (ir, ro, predicate) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bettergi/utils",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
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.12",
37
37
  "typescript": "5.6.3"
38
38
  }
39
39
  }