@3-/aiapi 0.1.2

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.
Files changed (3) hide show
  1. package/README.md +43 -0
  2. package/lib.js +68 -0
  3. package/package.json +24 -0
package/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # @3-/aiapi
2
+
3
+ [test/main.coffee](./test/main.coffee) :
4
+
5
+ ```coffee
6
+ #!/usr/bin/env coffee
7
+
8
+ > @3-/aiapi
9
+ ```
10
+
11
+ output :
12
+
13
+ ```
14
+ ./out.txt
15
+ ```
16
+
17
+ ## About
18
+
19
+ This project is an open-source component of [i18n.site ⋅ Internationalization Solution](https://i18n.site).
20
+
21
+ * [i18 : MarkDown Command Line Translation Tool](https://i18n.site/i18)
22
+
23
+ The translation perfectly maintains the Markdown format.
24
+
25
+ It recognizes file changes and only translates the modified files.
26
+
27
+ The translated Markdown content is editable; if you modify the original text and translate it again, manually edited translations will not be overwritten (as long as the original text has not been changed).
28
+
29
+ * [i18n.site : MarkDown Multi-language Static Site Generator](https://i18n.site/i18n.site)
30
+
31
+ Optimized for a better reading experience
32
+
33
+ ## 关于
34
+
35
+ 本项目为 [i18n.site ⋅ 国际化解决方案](https://i18n.site) 的开源组件。
36
+
37
+ * [i18 : MarkDown命令行翻译工具](https://i18n.site/i18)
38
+
39
+ 翻译能够完美保持 Markdown 的格式。能识别文件的修改,仅翻译有变动的文件。
40
+
41
+ Markdown 翻译内容可编辑;如果你修改原文并再次机器翻译,手动修改过的翻译不会被覆盖(如果这段原文没有被修改)。
42
+
43
+ * [i18n.site : MarkDown多语言静态站点生成器](https://i18n.site/i18n.site) 为阅读体验而优化。
package/lib.js ADDED
@@ -0,0 +1,68 @@
1
+ var MAX_RETRY;
2
+
3
+ import req from '@3-/req/_req.js';
4
+
5
+ import sleep from '@3-/sleep';
6
+
7
+ MAX_RETRY = 9;
8
+
9
+ export default (prefix, key_li) => {
10
+ var curl, key_len, n;
11
+ n = 0;
12
+ key_len = key_li.length;
13
+ curl = async(url, opt) => {
14
+ var err, headers, key, r, retry, status;
15
+ if (opt) {
16
+ ({headers} = opt);
17
+ } else {
18
+ opt = {};
19
+ }
20
+ if (!headers) {
21
+ headers = opt.headers = {};
22
+ }
23
+ headers['Content-Type'] ??= 'application/json';
24
+ retry = 0;
25
+ while (true) {
26
+ key = key_li[n];
27
+ headers.Authorization = 'Bearer ' + key;
28
+ n = (n + 1) % key_len;
29
+ try {
30
+ r = (await req(prefix + url, opt));
31
+ ({status} = r);
32
+ if ([401, 403, 400].includes(status)) {
33
+ retry = MAX_RETRY;
34
+ console.error(prefix + ' api key ' + key + ' ' + r.status + ' ' + r.statusText);
35
+ console.error((await r.text()));
36
+ throw new Error(r);
37
+ }
38
+ if (status === 429) {
39
+ console.error(prefix + ' api key ' + key + ' TOO_MANY_REQUESTS, WAIT 9s');
40
+ try {
41
+ console.error((await r.text()));
42
+ } catch (error) {
43
+ err = error;
44
+ console.error(err);
45
+ }
46
+ await sleep(9e3);
47
+ }
48
+ return (await r.json());
49
+ } catch (error) {
50
+ err = error;
51
+ if (++retry < MAX_RETRY) {
52
+ console.error(retry, prefix + ' api key ' + key, ':', err);
53
+ await sleep(9e3);
54
+ }
55
+ throw err;
56
+ }
57
+ }
58
+ };
59
+ return {
60
+ GET: curl,
61
+ POST: (url, body, opt = {
62
+ method: 'POST'
63
+ }) => {
64
+ opt.body = JSON.stringify(body);
65
+ return curl(url, opt);
66
+ }
67
+ };
68
+ };
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@3-/aiapi",
3
+ "version": "0.1.2",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "git+https://atomgit.com/i18n/lib.git"
7
+ },
8
+ "homepage": "https://atomgit.com/i18n/lib/tree/dev/aiapi",
9
+ "author": "i18n.site@gmail.com",
10
+ "license": "Apache-2.0",
11
+ "exports": {
12
+ ".": "./lib.js",
13
+ "./*": "./*"
14
+ },
15
+ "files": [
16
+ "./*"
17
+ ],
18
+ "devDependencies": {},
19
+ "type": "module",
20
+ "dependencies": {
21
+ "@3-/req": "^0.0.2"
22
+ },
23
+ "scripts": {}
24
+ }