tornapi-typescript 2.0.0 → 2.0.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.
- package/README.md +23 -5
- package/dist/index.d.ts +3889 -5452
- package/dist/index.js +138 -0
- package/dist/index.ts +12174 -0
- package/dist/openapi.json +64 -55
- package/package.json +5 -2
package/dist/index.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
/**
|
|
4
|
+
* This file is generated by core-types-ts on behalf of typeconv, DO NOT EDIT.
|
|
5
|
+
* For more information, see:
|
|
6
|
+
* - {@link https://github.com/grantila/core-types-ts}
|
|
7
|
+
* - {@link https://github.com/grantila/typeconv}
|
|
8
|
+
*/
|
|
9
|
+
export var TornApiError;
|
|
10
|
+
(function (TornApiError) {
|
|
11
|
+
TornApiError[TornApiError["UNKNOWN_ERROR"] = 0] = "UNKNOWN_ERROR";
|
|
12
|
+
TornApiError[TornApiError["KEY_EMPTY"] = 1] = "KEY_EMPTY";
|
|
13
|
+
TornApiError[TornApiError["INCORRECT_KEY"] = 2] = "INCORRECT_KEY";
|
|
14
|
+
TornApiError[TornApiError["WRONG_TYPE"] = 3] = "WRONG_TYPE";
|
|
15
|
+
TornApiError[TornApiError["WRONG_FIELDS"] = 4] = "WRONG_FIELDS";
|
|
16
|
+
TornApiError[TornApiError["TOO_MANY_REQUESTS"] = 5] = "TOO_MANY_REQUESTS";
|
|
17
|
+
TornApiError[TornApiError["INCORRECT_ID"] = 6] = "INCORRECT_ID";
|
|
18
|
+
TornApiError[TornApiError["INCORRECT_RELATION"] = 7] = "INCORRECT_RELATION";
|
|
19
|
+
TornApiError[TornApiError["IP_BLOCK"] = 8] = "IP_BLOCK";
|
|
20
|
+
TornApiError[TornApiError["API_DISABLED"] = 9] = "API_DISABLED";
|
|
21
|
+
TornApiError[TornApiError["KEY_FEDERAL_JAIL"] = 10] = "KEY_FEDERAL_JAIL";
|
|
22
|
+
TornApiError[TornApiError["KEY_CHANGE_ERROR"] = 11] = "KEY_CHANGE_ERROR";
|
|
23
|
+
TornApiError[TornApiError["KEY_READ_ERROR"] = 12] = "KEY_READ_ERROR";
|
|
24
|
+
TornApiError[TornApiError["KEY_TEMPORARILY_DISABLED_TO_INACTIVITY"] = 13] = "KEY_TEMPORARILY_DISABLED_TO_INACTIVITY";
|
|
25
|
+
TornApiError[TornApiError["DAILY_READ_LIMIT_REACHED"] = 14] = "DAILY_READ_LIMIT_REACHED";
|
|
26
|
+
TornApiError[TornApiError["TEMPORARY_ERROR"] = 15] = "TEMPORARY_ERROR";
|
|
27
|
+
TornApiError[TornApiError["ACCESS_LEVEL_KEY_NOT_HIGH"] = 16] = "ACCESS_LEVEL_KEY_NOT_HIGH";
|
|
28
|
+
TornApiError[TornApiError["BACKEND_ERROR_OCCURRED"] = 17] = "BACKEND_ERROR_OCCURRED";
|
|
29
|
+
TornApiError[TornApiError["API_KEY_HAS_BEEN_PAUSED"] = 18] = "API_KEY_HAS_BEEN_PAUSED";
|
|
30
|
+
TornApiError[TornApiError["MUST_BE_MIGRATED_TO_CRIMES"] = 19] = "MUST_BE_MIGRATED_TO_CRIMES";
|
|
31
|
+
TornApiError[TornApiError["RACE_NOT_YET_FINISHED"] = 20] = "RACE_NOT_YET_FINISHED";
|
|
32
|
+
TornApiError[TornApiError["INCORRECT_CATEGORY"] = 21] = "INCORRECT_CATEGORY";
|
|
33
|
+
TornApiError[TornApiError["SELECTION_ONLY_AVAILABLE_API_V1"] = 22] = "SELECTION_ONLY_AVAILABLE_API_V1";
|
|
34
|
+
TornApiError[TornApiError["SELECTION_ONLY_AVAILABLE_API_V2"] = 23] = "SELECTION_ONLY_AVAILABLE_API_V2";
|
|
35
|
+
TornApiError[TornApiError["CLOSED_TEMPORARILY"] = 24] = "CLOSED_TEMPORARILY";
|
|
36
|
+
})(TornApiError || (TornApiError = {}));
|
|
37
|
+
export async function tornApiGetV1({ section, selections, id, params = {}, key, comment, cache, expiry, }) {
|
|
38
|
+
const cached = await cache?.get({
|
|
39
|
+
section,
|
|
40
|
+
selections,
|
|
41
|
+
id,
|
|
42
|
+
params,
|
|
43
|
+
key,
|
|
44
|
+
});
|
|
45
|
+
if (cached)
|
|
46
|
+
return cached;
|
|
47
|
+
const url = new URL(`https://api.torn.com/${section}/${id ?? ""}`);
|
|
48
|
+
selections?.length &&
|
|
49
|
+
url.searchParams.set("selections", selections.join(","));
|
|
50
|
+
url.searchParams.set("key", key);
|
|
51
|
+
if (comment)
|
|
52
|
+
url.searchParams.set("comment", comment);
|
|
53
|
+
Object.entries(params).forEach(([k, v]) => url.searchParams.set(k, v));
|
|
54
|
+
return await fetch(url)
|
|
55
|
+
.then((res) => res.json())
|
|
56
|
+
.then(addToCache)
|
|
57
|
+
.catch(handleError);
|
|
58
|
+
function handleError(e) {
|
|
59
|
+
console.error(e);
|
|
60
|
+
return { error: { code: -1, error: generateErrorString(e) } };
|
|
61
|
+
function generateErrorString(e) {
|
|
62
|
+
switch (typeof e) {
|
|
63
|
+
case "string":
|
|
64
|
+
return e;
|
|
65
|
+
case "object": {
|
|
66
|
+
if (e instanceof Error)
|
|
67
|
+
return e.message;
|
|
68
|
+
return JSON.stringify(e);
|
|
69
|
+
}
|
|
70
|
+
default:
|
|
71
|
+
return e.toString();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function addToCache(response) {
|
|
76
|
+
if ("error" in response)
|
|
77
|
+
return response;
|
|
78
|
+
cache?.set({
|
|
79
|
+
section,
|
|
80
|
+
selections,
|
|
81
|
+
id,
|
|
82
|
+
params,
|
|
83
|
+
key,
|
|
84
|
+
}, response, expiry ?? Date.now() + 30_000);
|
|
85
|
+
return response;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
export async function tornApiGetV2({ section, selections, id, params = {}, key, comment, cache, expiry, }) {
|
|
89
|
+
const cached = await cache?.get({
|
|
90
|
+
section,
|
|
91
|
+
selections,
|
|
92
|
+
id,
|
|
93
|
+
params,
|
|
94
|
+
key,
|
|
95
|
+
});
|
|
96
|
+
if (cached)
|
|
97
|
+
return cached;
|
|
98
|
+
const url = new URL(`https://api.torn.com/v2/${section}/${id ?? ""}`);
|
|
99
|
+
selections?.length &&
|
|
100
|
+
url.searchParams.set("selections", selections.join(","));
|
|
101
|
+
url.searchParams.set("key", key);
|
|
102
|
+
if (comment)
|
|
103
|
+
url.searchParams.set("comment", comment);
|
|
104
|
+
Object.entries(params).forEach(([k, v]) => url.searchParams.set(k, v));
|
|
105
|
+
return await fetch(url)
|
|
106
|
+
.then((res) => res.json())
|
|
107
|
+
.then(addToCache)
|
|
108
|
+
.catch(handleError);
|
|
109
|
+
function handleError(e) {
|
|
110
|
+
console.error(e);
|
|
111
|
+
return { error: { code: -1, error: generateErrorString(e) } };
|
|
112
|
+
function generateErrorString(e) {
|
|
113
|
+
switch (typeof e) {
|
|
114
|
+
case "string":
|
|
115
|
+
return e;
|
|
116
|
+
case "object": {
|
|
117
|
+
if (e instanceof Error)
|
|
118
|
+
return e.message;
|
|
119
|
+
return JSON.stringify(e);
|
|
120
|
+
}
|
|
121
|
+
default:
|
|
122
|
+
return e.toString();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function addToCache(response) {
|
|
127
|
+
if ("error" in response)
|
|
128
|
+
return response;
|
|
129
|
+
cache?.set({
|
|
130
|
+
section,
|
|
131
|
+
selections,
|
|
132
|
+
id,
|
|
133
|
+
params,
|
|
134
|
+
key,
|
|
135
|
+
}, response, expiry ?? Date.now() + 30_000);
|
|
136
|
+
return response;
|
|
137
|
+
}
|
|
138
|
+
}
|