clashofclans.js 2.4.0-dev.cedc2ac → 2.5.0-dev.597dedf

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/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## 2.5.0 (2021-12-30)
6
+
7
+ ### Bug Fixes
8
+
9
+ - Fix caching issue with unnecessary/invalid query params. ([#91](https://github.com/clashperk/clashofclans.js/pull/91))
10
+ - Added necessary methods to `RESTManager` class. ([#92](https://github.com/clashperk/clashofclans.js/pull/92))
11
+
5
12
  ## 2.4.0 (2021-12-28)
6
13
 
7
14
  ### Features
@@ -1,10 +1,24 @@
1
1
  import { RequestHandler } from './RequestHandler';
2
- import { APIClan, APIClanList, APIClanMemberList, APIClanRankingList, APIClanVersusRankingList, APIClanWar, APIClanWarLeagueGroup, APIClanWarLog, APIGoldPassSeason, APILabelList, APILeague, APILeagueList, APILeagueSeasonList, APILocation, APILocationList, APIPlayer, APIPlayerRankingList, APIPlayerSeasonRankingList, APIPlayerVersusRankingList, APIVerifyToken, APIWarLeague, APIWarLeagueList, SearchOptions, ClanSearchOptions, RESTOptions, OverrideOptions } from '../types';
2
+ import { Util } from '../util/Util';
3
+ import { APIClan, APIClanList, APIClanMemberList, APIClanRankingList, APIClanVersusRankingList, APIClanWar, APIClanWarLeagueGroup, APIClanWarLog, APIGoldPassSeason, APILabelList, APILeague, APILeagueList, APILeagueSeasonList, APILocation, APILocationList, APIPlayer, APIPlayerRankingList, APIPlayerSeasonRankingList, APIPlayerVersusRankingList, APIVerifyToken, APIWarLeague, APIWarLeagueList, SearchOptions, ClanSearchOptions, RESTOptions, OverrideOptions, LoginOptions } from '../types';
3
4
  /** Represents a REST Manager of the client. */
4
5
  export declare class RESTManager {
5
6
  /** Request Handler for the RESTManager. */
6
7
  readonly handler: RequestHandler;
7
8
  constructor(options?: RESTOptions);
9
+ /** Contains various general-purpose utility methods. */
10
+ get util(): typeof Util;
11
+ /**
12
+ * Initialize the client to create keys.
13
+ * @example
14
+ * ```
15
+ * const rest = new RESTManager();
16
+ * rest.login({ email: 'developer@email.com', password: '***' });
17
+ * ```
18
+ */
19
+ login(options: LoginOptions): Promise<string[]>;
20
+ /** Set Clash of Clans API keys. */
21
+ setKeys(keys: string[]): this;
8
22
  /** Search all clans by name and/or filtering the results using various criteria. */
9
23
  getClans(query: ClanSearchOptions, options?: OverrideOptions): Promise<import("../types").Response<APIClanList>>;
10
24
  /** Get info about a clan. */
@@ -8,9 +8,29 @@ class RESTManager {
8
8
  constructor(options) {
9
9
  this.handler = new RequestHandler_1.RequestHandler(options);
10
10
  }
11
+ /** Contains various general-purpose utility methods. */
12
+ get util() {
13
+ return Util_1.Util;
14
+ }
15
+ /**
16
+ * Initialize the client to create keys.
17
+ * @example
18
+ * ```
19
+ * const rest = new RESTManager();
20
+ * rest.login({ email: 'developer@email.com', password: '***' });
21
+ * ```
22
+ */
23
+ login(options) {
24
+ return this.handler.init(options);
25
+ }
26
+ /** Set Clash of Clans API keys. */
27
+ setKeys(keys) {
28
+ this.handler.setKeys(keys);
29
+ return this;
30
+ }
11
31
  /** Search all clans by name and/or filtering the results using various criteria. */
12
32
  getClans(query, options) {
13
- return this.handler.request(`/clans?${Util_1.Util.queryString(query)}`, options);
33
+ return this.handler.request(`/clans${Util_1.Util.queryString(query)}`, options);
14
34
  }
15
35
  /** Get info about a clan. */
16
36
  getClan(clanTag, options) {
@@ -19,12 +39,12 @@ class RESTManager {
19
39
  /** Get list of clan members. */
20
40
  getClanMembers(clanTag, options) {
21
41
  const query = Util_1.Util.queryString(options);
22
- return this.handler.request(`/clans/${Util_1.Util.encodeURI(clanTag)}/members?${query}`, options);
42
+ return this.handler.request(`/clans/${Util_1.Util.encodeURI(clanTag)}/members${query}`, options);
23
43
  }
24
44
  /** Get clan war log. */
25
45
  getClanWarLog(clanTag, options) {
26
46
  const query = Util_1.Util.queryString(options);
27
- return this.handler.request(`/clans/${Util_1.Util.encodeURI(clanTag)}/warlog?${query}`, options);
47
+ return this.handler.request(`/clans/${Util_1.Util.encodeURI(clanTag)}/warlog${query}`, options);
28
48
  }
29
49
  /** Get info about currently running war in the clan. */
30
50
  getCurrentWar(clanTag, options) {
@@ -50,7 +70,7 @@ class RESTManager {
50
70
  /** Get list of Leagues. */
51
71
  getLeagues(options) {
52
72
  const query = Util_1.Util.queryString(options);
53
- return this.handler.request(`/leagues?${query}`, options);
73
+ return this.handler.request(`/leagues${query}`, options);
54
74
  }
55
75
  /** Get a League info. */
56
76
  getLeague(leagueId, options) {
@@ -59,17 +79,17 @@ class RESTManager {
59
79
  /** Get Legend League season Ids. */
60
80
  getLeagueSeasons(leagueId, options) {
61
81
  const query = Util_1.Util.queryString(options);
62
- return this.handler.request(`/leagues/${leagueId}/seasons?${query}`, options);
82
+ return this.handler.request(`/leagues/${leagueId}/seasons${query}`, options);
63
83
  }
64
84
  /** Get Legend League season rankings by season Id. */
65
85
  getSeasonRankings(leagueId, seasonId, options) {
66
86
  const query = Util_1.Util.queryString(options);
67
- return this.handler.request(`/leagues/${leagueId}/seasons/${seasonId}?${query}`, options);
87
+ return this.handler.request(`/leagues/${leagueId}/seasons/${seasonId}${query}`, options);
68
88
  }
69
89
  /** Get list of Clan War Leagues. */
70
90
  getWarLeagues(options) {
71
91
  const query = Util_1.Util.queryString(options);
72
- return this.handler.request(`/warleagues?${query}`, options);
92
+ return this.handler.request(`/warleagues${query}`, options);
73
93
  }
74
94
  /** Get info about a Clan War League. */
75
95
  getWarLeague(leagueId, options) {
@@ -78,7 +98,7 @@ class RESTManager {
78
98
  /** Get list of Locations. */
79
99
  getLocations(options) {
80
100
  const query = Util_1.Util.queryString(options);
81
- return this.handler.request(`/locations?${query}`, options);
101
+ return this.handler.request(`/locations${query}`, options);
82
102
  }
83
103
  /** Get info about a Location. */
84
104
  getLocation(locationId, options) {
@@ -87,32 +107,32 @@ class RESTManager {
87
107
  /** Get clan rankings for a specific location. */
88
108
  getClanRanks(locationId, options) {
89
109
  const query = Util_1.Util.queryString(options);
90
- return this.handler.request(`/locations/${locationId}/rankings/clans?${query}`, options);
110
+ return this.handler.request(`/locations/${locationId}/rankings/clans${query}`, options);
91
111
  }
92
112
  /** Get player rankings for a specific location. */
93
113
  getPlayerRanks(locationId, options) {
94
114
  const query = Util_1.Util.queryString(options);
95
- return this.handler.request(`/locations/${locationId}/rankings/players?${query}`, options);
115
+ return this.handler.request(`/locations/${locationId}/rankings/players${query}`, options);
96
116
  }
97
117
  /** Get clan versus rankings for a specific location. */
98
118
  getVersusClanRanks(locationId, options) {
99
119
  const query = Util_1.Util.queryString(options);
100
- return this.handler.request(`/locations/${locationId}/rankings/clans-versus?${query}`, options);
120
+ return this.handler.request(`/locations/${locationId}/rankings/clans-versus${query}`, options);
101
121
  }
102
122
  /** Get player versus rankings for a specific location. */
103
123
  getVersusPlayerRanks(locationId, options) {
104
124
  const query = Util_1.Util.queryString(options);
105
- return this.handler.request(`/locations/${locationId}/rankings/players-versus?${query}`, options);
125
+ return this.handler.request(`/locations/${locationId}/rankings/players-versus${query}`, options);
106
126
  }
107
127
  /** Get list of clan labels. */
108
128
  getClanLabels(options) {
109
129
  const query = Util_1.Util.queryString(options);
110
- return this.handler.request(`/labels/clans?${query}`, options);
130
+ return this.handler.request(`/labels/clans${query}`, options);
111
131
  }
112
132
  /** Get list of player labels. */
113
133
  getPlayerLabels(options) {
114
134
  const query = Util_1.Util.queryString(options);
115
- return this.handler.request(`/labels/players?${query}`, options);
135
+ return this.handler.request(`/labels/players${query}`, options);
116
136
  }
117
137
  /** Get info about gold pass season. */
118
138
  getGoldPassSeason(options) {
@@ -88,8 +88,8 @@ class RequestHandler {
88
88
  if (!res?.ok && this.rejectIfNotValid) {
89
89
  throw new HTTPError_1.HTTPError(data, res?.status ?? 504, path, maxAge, options.method);
90
90
  }
91
- if (this.cached && maxAge > 0 && options.cache !== false) {
92
- await this.cached.set(path, { data, ttl: Date.now() + maxAge, status: res?.status ?? 504 }, maxAge);
91
+ if (this.cached && maxAge > 0 && options.cache !== false && res?.ok) {
92
+ await this.cached.set(path, { data, ttl: Date.now() + maxAge, status: res.status }, maxAge);
93
93
  }
94
94
  return { data, maxAge, status: res?.status ?? 504, path, ok: res?.status === 200 };
95
95
  }
@@ -1,3 +1,4 @@
1
+ import { ClanSearchOptions, SearchOptions } from '../types';
1
2
  /** Contains various general-purpose utility methods. */
2
3
  export declare class Util extends null {
3
4
  /**
@@ -30,7 +31,7 @@ export declare class Util extends null {
30
31
  /** Converts API Date to JavaScript Date. */
31
32
  static formatDate(date: string): Date;
32
33
  /** Returns a string containing a query string suitable for use in a URL. */
33
- static queryString(options?: {}): string;
34
+ static queryString(options?: SearchOptions | ClanSearchOptions): string;
34
35
  private static getSeasonEnd;
35
36
  /** Get current trophy season Id. */
36
37
  static getSeasonId(): string;
package/dist/util/Util.js CHANGED
@@ -2,6 +2,19 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Util = void 0;
4
4
  const TAG_CHARACTERS = '0289PYLQGRJCUV';
5
+ const params = [
6
+ 'name',
7
+ 'minMembers',
8
+ 'maxMembers',
9
+ 'minClanPoints',
10
+ 'minClanLevel',
11
+ 'warFrequency',
12
+ 'locationId',
13
+ 'labelIds',
14
+ 'limit',
15
+ 'after',
16
+ 'before'
17
+ ];
5
18
  /** Contains various general-purpose utility methods. */
6
19
  class Util extends null {
7
20
  /**
@@ -62,10 +75,8 @@ class Util extends null {
62
75
  }
63
76
  /** Returns a string containing a query string suitable for use in a URL. */
64
77
  static queryString(options = {}) {
65
- const query = new URLSearchParams(options);
66
- for (const key of ['cache', 'force', 'retryLimit', 'ignoreRateLimit', 'restRequestTimeout'])
67
- query.delete(key);
68
- return query.toString();
78
+ const query = new URLSearchParams(Object.entries(options).filter(([key]) => params.includes(key))).toString();
79
+ return query.length ? `?${query}` : query;
69
80
  }
70
81
  static getSeasonEnd(month, year, autoFix = true) {
71
82
  const now = new Date();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clashofclans.js",
3
- "version": "2.4.0-dev.cedc2ac",
3
+ "version": "2.5.0-dev.597dedf",
4
4
  "description": "JavaScript library for interacting with the Clash of Clans API",
5
5
  "author": "SUVAJIT <suvajit.me@gmail.com>",
6
6
  "license": "MIT",
@@ -22,7 +22,9 @@
22
22
  "keywords": [
23
23
  "clash-of-clans-api",
24
24
  "clash-of-clans",
25
- "clash-api"
25
+ "clash-api",
26
+ "supercell",
27
+ "coc"
26
28
  ],
27
29
  "bugs": {
28
30
  "url": "https://github.com/clashperk/clashofclans.js/issues"