@antti5/ship-tool-common 1.1.2 → 1.1.3

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.
@@ -1,5 +1,6 @@
1
- import Semaphore from '../Semaphore.ts';
2
1
  import z from 'zod';
2
+ import Util from '../Util.ts';
3
+ import Semaphore from '../Semaphore.ts';
3
4
 
4
5
 
5
6
  /* Helper function to call any of the API endpoints */
@@ -26,8 +27,8 @@ const WgResponseSchema = z.object({
26
27
  data: z.union([z.looseObject({}), z.array(z.unknown())])
27
28
  });
28
29
 
29
- const callApi = async (url: string) => {
30
- const data = await (await fetch(url)).json() as unknown;
30
+ const callApi = async (url: string, options = {}) => {
31
+ const data = await Util.fetch(url, options);
31
32
  const response = WgResponseSchema.parse(data);
32
33
 
33
34
  if (response.status !== 'ok')
@@ -43,8 +44,12 @@ const EncyclopediaInfoSchema = z.object({
43
44
  game_version: z.string()
44
45
  });
45
46
 
46
- const encyclopediaInfo = async (apiId: string) => {
47
- const { data } = await callApi(`https://api.worldofwarships.eu/wows/encyclopedia/info/?application_id=${apiId}`);
47
+ const encyclopediaInfo = async (apiId: string, { debug = false } = {}) => {
48
+ const { data } = await callApi(
49
+ `https://api.worldofwarships.eu/wows/encyclopedia/info/?application_id=${apiId}`,
50
+ { debug }
51
+ );
52
+
48
53
  return EncyclopediaInfoSchema.parse(data);
49
54
  };
50
55
 
@@ -60,8 +65,11 @@ const EncyclopediaShipsSchema = z.record(
60
65
  })
61
66
  );
62
67
 
63
- const encyclopediaShips = async (apiId: string, { concurrency = 1 } = {}) => {
64
- const { meta, data } = await callApi(`https://api.worldofwarships.eu/wows/encyclopedia/ships/?application_id=${apiId}`);
68
+ const encyclopediaShips = async (apiId: string, { concurrency = 1, debug = false } = {}) => {
69
+ const { meta, data } = await callApi(
70
+ `https://api.worldofwarships.eu/wows/encyclopedia/ships/?application_id=${apiId}`,
71
+ { debug }
72
+ );
65
73
 
66
74
  if (!meta || !meta.page_total)
67
75
  throw new Error('Unexpected /encyclopedia/ships response');
@@ -95,8 +103,11 @@ const ClansListSchema = z.array(
95
103
  })
96
104
  );
97
105
 
98
- const clansList = async (apiId: string, region: string, clanTag: string) => {
99
- const { data } = await callApi(`https://api.worldofwarships.${region}/wows/clans/list/?application_id=${apiId}&search=${clanTag}`);
106
+ const clansList = async (apiId: string, region: string, clanTag: string, { debug = false } = {}) => {
107
+ const { data } = await callApi(
108
+ `https://api.worldofwarships.${region}/wows/clans/list/?application_id=${apiId}&search=${clanTag}`,
109
+ { debug }
110
+ );
100
111
  const response = ClansListSchema.parse(data);
101
112
  const clanInfo = response.find(({ tag }) => tag === clanTag);
102
113
 
@@ -128,8 +139,11 @@ const ClansInfoSchema = z.record(
128
139
  }))
129
140
  );
130
141
 
131
- const clansInfoMany = async (apiId: string, region: string, clanIds: number[]) => {
132
- const { data } = await callApi(`https://api.worldofwarships.${region}/wows/clans/info/?application_id=${apiId}&clan_id=${clanIds.join(',')}&extra=members`);
142
+ const clansInfoMany = async (apiId: string, region: string, clanIds: number[], { debug = false } = {}) => {
143
+ const { data } = await callApi(
144
+ `https://api.worldofwarships.${region}/wows/clans/info/?application_id=${apiId}&clan_id=${clanIds.join(',')}&extra=members`,
145
+ { debug }
146
+ );
133
147
  return ClansInfoSchema.parse(data);
134
148
  };
135
149
 
@@ -164,13 +178,16 @@ const AccountInfoSchema = z.record(
164
178
  })
165
179
  );
166
180
 
167
- const accountInfoMany = async (apiId: string, region: string, accountIds: number[]) => {
168
- const { data } = await callApi(`https://api.worldofwarships.${region}/wows/account/info/?application_id=${apiId}&account_id=${accountIds.join(',')}&extra=statistics.pvp_solo`);
181
+ const accountInfoMany = async (apiId: string, region: string, accountIds: number[], { debug = false } = {}) => {
182
+ const { data } = await callApi(
183
+ `https://api.worldofwarships.${region}/wows/account/info/?application_id=${apiId}&account_id=${accountIds.join(',')}&extra=statistics.pvp_solo`,
184
+ { debug }
185
+ );
169
186
  return AccountInfoSchema.parse(data);
170
187
  };
171
188
 
172
- const accountInfoOne = async (apiId: string, region: string, accountId: number) => {
173
- const response = await accountInfoMany(apiId, region, [accountId]);
189
+ const accountInfoOne = async (apiId: string, region: string, accountId: number, options = {}) => {
190
+ const response = await accountInfoMany(apiId, region, [accountId], options);
174
191
 
175
192
  if (!response[accountId])
176
193
  throw new Error('Account not found in /account/info response: ' + accountId);
@@ -193,8 +210,11 @@ const ShipsStatsSchema = z.record(
193
210
  )
194
211
  );
195
212
 
196
- const shipsStats = async (apiId: string, region: string, accountId: number) => {
197
- const { data } = await callApi(`https://api.worldofwarships.${region}/wows/ships/stats/?application_id=${apiId}&account_id=${accountId}`);
213
+ const shipsStats = async (apiId: string, region: string, accountId: number, { debug = false } = {}) => {
214
+ const { data } = await callApi(
215
+ `https://api.worldofwarships.${region}/wows/ships/stats/?application_id=${apiId}&account_id=${accountId}`,
216
+ { debug }
217
+ );
198
218
  const response = ShipsStatsSchema.parse(data);
199
219
 
200
220
  if (!response[accountId])
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antti5/ship-tool-common",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "Ship Tool common utils and library code",
5
5
  "repository": {
6
6
  "type": "git",