clashofclans.js 3.3.4 → 3.3.5-dev.227b0d5

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.
@@ -175,7 +175,7 @@ class PollingClient extends Client_1.Client {
175
175
  }
176
176
  }
177
177
  seasonEndHandler() {
178
- const end = this.util.getSeasonEndTime().getTime() - Date.now();
178
+ const end = this.util.getSeason().endTime.getTime() - Date.now();
179
179
  // Why this? setTimeout can be up to 24.8 days or 2147483647ms [(2^31 - 1) Max 32bit Integer]
180
180
  if (end > 24 * 60 * 60 * 1000) {
181
181
  setTimeout(this.seasonEndHandler.bind(this), 60 * 60 * 1000);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const Client_1 = require("./client/Client");
4
+ describe('root', () => {
5
+ const client = new Client_1.Client({ baseURL: process.env.BASE_URL });
6
+ it('should return a player', async () => {
7
+ const result = await client.getPlayer('#LUV2PG2Y9');
8
+ console.log({
9
+ name: result.name,
10
+ tag: result.tag,
11
+ troops: result.troops.map((troop) => troop.name),
12
+ spells: result.spells.map((spell) => spell.name),
13
+ heroes: result.heroes.map((hero) => hero.name)
14
+ });
15
+ expect(result).toHaveProperty('tag');
16
+ });
17
+ it('should return a clan', async () => {
18
+ const result = await client.getClan('#8QU8J9LP');
19
+ console.log({
20
+ name: result.name,
21
+ tag: result.tag
22
+ });
23
+ expect(result).toHaveProperty('tag');
24
+ });
25
+ });
@@ -33,11 +33,19 @@ export declare class Util extends null {
33
33
  static formatDate(date: string): Date;
34
34
  /** Returns a string containing a query string suitable for use in a URL. */
35
35
  static queryString(options?: SearchOptions | ClanSearchOptions): string;
36
+ private static getSeasonStart;
36
37
  private static getSeasonEnd;
37
38
  /** Get current trophy season Id. */
38
39
  static getSeasonId(): string;
39
- /** Get trophy season end timestamp. */
40
- static getSeasonEndTime(timestamp?: Date): Date;
40
+ /**
41
+ * Get the timestamp for the last Monday of the previous month.
42
+ * @param {Date} timestamp - The reference date. Defaults to the current date if not provided.
43
+ * @param {boolean} forward - Whether to forward to the next month if the returned date is in the past relative to the given timestamp. Defaults to true.
44
+ */
45
+ static getSeason(timestamp?: Date, forward?: boolean): {
46
+ endTime: Date;
47
+ startTime: Date;
48
+ };
41
49
  static allSettled<T>(values: Promise<T>[]): Promise<T[]>;
42
50
  static delay(ms: number): Promise<unknown>;
43
51
  /** Parse in-game army link into troops and spells count with respective Id's. */
package/dist/util/Util.js CHANGED
@@ -96,27 +96,43 @@ class Util extends null {
96
96
  const query = new URLSearchParams(Object.entries(options).filter(([key]) => params.includes(key))).toString();
97
97
  return query.length ? `?${query}` : query;
98
98
  }
99
- static getSeasonEnd(month, year, autoFix = true) {
100
- const now = new Date();
101
- now.setUTCFullYear(year);
102
- now.setUTCMonth(month, 0);
103
- now.setUTCHours(5, 0, 0, 0);
104
- const newDate = now.getUTCDay() === 0 ? now.getUTCDate() - 6 : now.getUTCDate() - (now.getUTCDay() - 1);
105
- now.setUTCDate(newDate);
106
- if (Date.now() >= now.getTime() && autoFix) {
107
- return this.getSeasonEnd(month + 1, year);
99
+ static getSeasonStart(inputDate) {
100
+ const lastMonthLastDay = new Date(Date.UTC(inputDate.getUTCFullYear(), inputDate.getUTCMonth(), 0));
101
+ const lastMonthLastMonday = new Date(lastMonthLastDay);
102
+ lastMonthLastMonday.setUTCDate(lastMonthLastMonday.getUTCDate() - ((lastMonthLastDay.getUTCDay() + 6) % 7));
103
+ lastMonthLastMonday.setUTCHours(5, 0, 0, 0);
104
+ return lastMonthLastMonday;
105
+ }
106
+ static getSeasonEnd(inputDate, forward = true) {
107
+ const lastDayOfMonth = new Date(Date.UTC(inputDate.getUTCFullYear(), inputDate.getUTCMonth() + 1, 0));
108
+ const lastMonday = new Date(lastDayOfMonth);
109
+ lastMonday.setUTCDate(lastMonday.getUTCDate() - ((lastDayOfMonth.getUTCDay() + 6) % 7));
110
+ lastMonday.setUTCHours(5, 0, 0, 0);
111
+ // check if the last Monday is in the past relative to the input date
112
+ if (lastMonday.getTime() < inputDate.getTime() && forward) {
113
+ // calculate the last Monday of the next month
114
+ const nextMonth = new Date(Date.UTC(inputDate.getUTCFullYear(), inputDate.getUTCMonth() + 1, 1));
115
+ const nextMonthLastDay = new Date(Date.UTC(nextMonth.getUTCFullYear(), nextMonth.getUTCMonth() + 1, 0));
116
+ const nextMonthLastMonday = new Date(nextMonthLastDay);
117
+ nextMonthLastMonday.setUTCDate(nextMonthLastMonday.getUTCDate() - ((nextMonthLastDay.getUTCDay() + 6) % 7));
118
+ nextMonthLastMonday.setUTCHours(5, 0, 0, 0);
119
+ return nextMonthLastMonday;
108
120
  }
109
- return now;
121
+ return lastMonday;
110
122
  }
111
123
  /** Get current trophy season Id. */
112
124
  static getSeasonId() {
113
- return this.getSeasonEndTime().toISOString().substring(0, 7);
125
+ return this.getSeasonEnd(new Date()).toISOString().substring(0, 7);
114
126
  }
115
- /** Get trophy season end timestamp. */
116
- static getSeasonEndTime(timestamp) {
117
- const autoFix = !(timestamp instanceof Date);
118
- const date = timestamp instanceof Date ? timestamp : new Date();
119
- return this.getSeasonEnd(date.getUTCMonth() + 1, date.getUTCFullYear(), autoFix);
127
+ /**
128
+ * Get the timestamp for the last Monday of the previous month.
129
+ * @param {Date} timestamp - The reference date. Defaults to the current date if not provided.
130
+ * @param {boolean} forward - Whether to forward to the next month if the returned date is in the past relative to the given timestamp. Defaults to true.
131
+ */
132
+ static getSeason(timestamp, forward = true) {
133
+ const endTime = this.getSeasonEnd(timestamp ?? new Date(), forward);
134
+ const startTime = this.getSeasonStart(endTime);
135
+ return { endTime, startTime };
120
136
  }
121
137
  static async allSettled(values) {
122
138
  return (await Promise.allSettled(values))
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const Util_1 = require("./util/Util");
4
+ describe('util', () => {
5
+ it('should always be UTC', () => {
6
+ expect(new Date().getTimezoneOffset()).toBe(0);
7
+ });
8
+ it('should forward to next month if date is in past', async () => {
9
+ const timestamp = new Date('2024-03-25T05:01');
10
+ const { endTime, startTime } = Util_1.Util.getSeason(timestamp);
11
+ const expectedEndTime = new Date('2024-04-29T05:00').toISOString();
12
+ const expectedStartTime = new Date('2024-03-25T05:00').toISOString();
13
+ expect(endTime.toISOString()).toBe(expectedEndTime);
14
+ expect(startTime.toISOString()).toBe(expectedStartTime);
15
+ });
16
+ it('should return the correct season against a date', async () => {
17
+ const timestamp = new Date('2024-03-16T05:01');
18
+ const { endTime, startTime } = Util_1.Util.getSeason(timestamp);
19
+ const expectedEndTime = new Date('2024-03-25T05:00').toISOString();
20
+ const expectedStartTime = new Date('2024-02-26T05:00').toISOString();
21
+ expect(endTime.toISOString()).toBe(expectedEndTime);
22
+ expect(startTime.toISOString()).toBe(expectedStartTime);
23
+ });
24
+ it('should forward to the next year at the end of the year', async () => {
25
+ const timestamp = new Date('2024-12-30T05:01');
26
+ const { endTime, startTime } = Util_1.Util.getSeason(timestamp);
27
+ const expectedEndTime = new Date('2025-01-27T05:00').toISOString();
28
+ const expectedStartTime = new Date('2024-12-30T05:00').toISOString();
29
+ expect(endTime.toISOString()).toBe(expectedEndTime);
30
+ expect(startTime.toISOString()).toBe(expectedStartTime);
31
+ });
32
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clashofclans.js",
3
- "version": "3.3.4",
3
+ "version": "3.3.5-dev.227b0d5",
4
4
  "description": "JavaScript library for interacting with the Clash of Clans API",
5
5
  "author": "https://clashofclans.js.org",
6
6
  "license": "MIT",
@@ -20,8 +20,10 @@
20
20
  "scripts": {
21
21
  "build": "tsc && gen-esm-wrapper dist/index.js dist/index.mjs",
22
22
  "prepare": "rimraf dist && npm run build",
23
- "test": "eslint --ext .ts --ignore-path .gitignore .",
24
- "lint": "eslint --fix --ext .ts --ignore-path .gitignore ."
23
+ "lint:test": "eslint --ext .ts --ignore-path .gitignore .",
24
+ "lint:fix": "eslint --fix --ext .ts --ignore-path .gitignore .",
25
+ "test": "TZ=UTC jest --setupFiles dotenv/config --config ./jest.config.json",
26
+ "test:watch": "npm run test -- --watch"
25
27
  },
26
28
  "files": [
27
29
  "dist"
@@ -49,9 +51,12 @@
49
51
  }
50
52
  },
51
53
  "dependencies": {
54
+ "jest": "^29.7.0",
52
55
  "undici": "^5.23.0"
53
56
  },
54
57
  "devDependencies": {
58
+ "@jest/globals": "^29.7.0",
59
+ "@types/jest": "^29.5.12",
55
60
  "@types/node": "^18.17.5",
56
61
  "@typescript-eslint/eslint-plugin": "^6.3.0",
57
62
  "@typescript-eslint/parser": "^6.3.0",
@@ -62,9 +67,10 @@
62
67
  "gen-esm-wrapper": "^1.1.3",
63
68
  "prettier": "^3.0.1",
64
69
  "rimraf": "^5.0.1",
70
+ "ts-jest": "^29.1.2",
65
71
  "typescript": "^5.1.6"
66
72
  },
67
73
  "engines": {
68
74
  "node": ">=16.x"
69
75
  }
70
- }
76
+ }
package/CHANGELOG.md DELETED
@@ -1,173 +0,0 @@
1
- # Changelog
2
-
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
-
5
- ## 3.1.3 (24-12-2023)
6
-
7
- ### Features
8
-
9
- - Town Hall 16 updates.
10
- - Removed deprecated builder base properties.
11
-
12
- ## 3.1.3 (13-08-2023)
13
-
14
- ### Features
15
-
16
- - Switched to `undici` from `node-fetch` for better performance.
17
-
18
- ## 3.1.0 (2023-07-28)
19
-
20
- ### Features
21
-
22
- - Builder base update and new troop levels.
23
-
24
- ## 3.0.2 (2023-01-15)
25
-
26
- ### Bug Fixes
27
-
28
- - Conflict with the same name of builder base and home base troops. (#123)
29
- - Fix the issue with the `Client#getLeagueWar()` method.
30
- - Typings and documentation for clan capital.
31
- - Fix the issue with the `Clan#clanCapital` property.
32
-
33
- ### Features
34
-
35
- - Added `Client#getCapitalRaidSeasons()` method.
36
- - Added `Client#getCapitalLeagues()` method.
37
- - Added `Client#getClanCapitalRanks()` method.
38
- - Added new Super Troops in raw.json file.
39
-
40
- ### Breaking Changes
41
-
42
- - Using PascalCase instead of SCREAMING_SNAKE_CASE ([#115](https://github.com/clashperk/clashofclans.js/pull/115))
43
- - `Client#events` and `EventManager` have been removed in favor of `PollingClient` ([#117](https://github.com/clashperk/clashofclans.js/pull/117), [#127](https://github.com/clashperk/clashofclans.js/pull/127))
44
-
45
- ## 2.8.0 (2022-07-22)
46
-
47
- ### Features
48
-
49
- - Better Throttler with JS generator function. ([#111](https://github.com/clashperk/clashofclans.js/pull/111))
50
- - Updated raw data from game files. ([#111](https://github.com/clashperk/clashofclans.js/pull/111))
51
- - New method Util#parseArmyLink has been added. ([#110](https://github.com/clashperk/clashofclans.js/pull/110))
52
-
53
- ## 2.7.0 (2022-05-22)
54
-
55
- ### Features
56
-
57
- - Some useful QOL methods have been added. ([#106](https://github.com/clashperk/clashofclans.js/pull/106))
58
-
59
- ## 2.6.1 (2022-02-03)
60
-
61
- ### Bug Fixes
62
-
63
- - New value and typings `notInWar` added for `ClanWarLeagueGroup#state` ([#101](https://github.com/clashperk/clashofclans.js/pull/101))
64
- - Throw error if `Util.formatTag` / `Util.parseTag` is called with invalid argument ([#102](https://github.com/clashperk/clashofclans.js/pull/101))
65
-
66
- ## 2.6.0 (2022-01-29)
67
-
68
- ## Features
69
-
70
- - Replaced Keyv with customizable cache store ([#99](https://github.com/clashperk/clashofclans.js/pull/99))
71
- - Guide for [Internal Caching](https://clashofclans.js.org/guide/internal-caching)
72
-
73
- ## 2.5.2 (2022-01-23)
74
-
75
- ### Bug Fixes
76
-
77
- - Fix `ClanWar#attacksPerMembers` property ([#97](https://github.com/clashperk/clashofclans.js/pull/97))
78
- - Bump `node-fetch` from 2.6.6 to 2.6.7 ([#96](https://github.com/clashperk/clashofclans.js/pull/96))
79
-
80
- ## 2.5.1 (2022-01-11)
81
-
82
- ### Bug Fixes
83
-
84
- - Typings for `ClanWarLeagueGroup#state` property. ([#94](https://github.com/clashperk/clashofclans.js/pull/94))
85
-
86
- ## 2.5.0 (2021-12-30)
87
-
88
- ### Bug Fixes
89
-
90
- - Fix caching issue with unnecessary/invalid query params. ([#91](https://github.com/clashperk/clashofclans.js/pull/91))
91
- - Added necessary methods to `RESTManager` class. ([#92](https://github.com/clashperk/clashofclans.js/pull/92))
92
-
93
- ## 2.4.0 (2021-12-28)
94
-
95
- ### Features
96
-
97
- - `ClanWar#getClanWarLeagueGroup`, `ClanWar#isCWL` and `ClanWar#isFriendly` are now available. ([#87](https://github.com/clashperk/clashofclans.js/pull/87))
98
- - `RESTOptions#rejectIfNotValid` added to perform `res.ok` operations over `RESTManager` methods. [Know more?](https://clashofclans.js.org/guide/access-raw-data#easy-access) ([#87](https://github.com/clashperk/clashofclans.js/pull/87))
99
- - `Icon#fileName` and `Icon#sizes` are now available in `Icon` class. ([#87](https://github.com/clashperk/clashofclans.js/pull/87))
100
- - `Badge#fileName` and `Badge#sizes` are now available in `Badge` class. ([#87](https://github.com/clashperk/clashofclans.js/pull/87))
101
-
102
- ### Deprecations
103
-
104
- - `ClanWarMember#previousBestOpponentAttack` has been deprecated. Use `ClanWarAttack#previousBestAttack` instead. ([#87](https://github.com/clashperk/clashofclans.js/pull/87))
105
-
106
- ## 2.3.0 (2021-12-17)
107
-
108
- ### Features
109
-
110
- - BigInt literals issue fixed. ([#84](https://github.com/clashperk/clashofclans.js/pull/84))
111
- - Some Utility methods renamed. ([#84](https://github.com/clashperk/clashofclans.js/pull/84))
112
- - `Util.encodeTag()` to `Util.encodeURI()`
113
- - `Util.encodeTagToId()` to `Util.encodeTag()`
114
- - `Util.decodeIdToTag()` to `Util.decodeTag()`
115
- - Added `dps`, `resourceType`, `trainingTime` and `regenerationTime` to the `Unit` class. ([#85](https://github.com/clashperk/clashofclans.js/pull/85))
116
-
117
- ## 2.2.0 (2021-12-16)
118
-
119
- ### Bug Fixes
120
-
121
- - Show units as per in-game orders. ([#82](https://github.com/clashperk/clashofclans.js/pull/82)) ([6e23d2f](https://github.com/clashperk/clashofclans.js/commit/95cf3001059fd3ede9262e249814178631660d5b))
122
- - Season end time utility method. ([#82](https://github.com/clashperk/clashofclans.js/pull/82)) ([6e23d2f](https://github.com/clashperk/clashofclans.js/commit/95cf3001059fd3ede9262e249814178631660d5b))
123
- - Updated raw files for new Troops. ([#82](https://github.com/clashperk/clashofclans.js/pull/82)) ([6e23d2f](https://github.com/clashperk/clashofclans.js/commit/95cf3001059fd3ede9262e249814178631660d5b))
124
-
125
- ### Features
126
-
127
- - Added `seasonal`, `boostable` and `isLoaded` property to `Unit` class. ([#82](https://github.com/clashperk/clashofclans.js/pull/82)) ([6e23d2f](https://github.com/clashperk/clashofclans.js/commit/95cf3001059fd3ede9262e249814178631660d5b))
128
-
129
- ## 2.1.0 (2021-12-06)
130
-
131
- ### Bug Fixes
132
-
133
- - Consistency of `ClanWar.attacksPerMember` property. ([#75](https://github.com/clashperk/clashofclans.js/pull/75)) ([6e23d2f](https://github.com/clashperk/clashofclans.js/commit/6e23d2fe0373f56268ffa55d5ac2807c9a2dc2fc))
134
-
135
- ### Features
136
-
137
- - More utility methods added to `Util` class. ([#76](https://github.com/clashperk/clashofclans.js/pull/76)) ([ff41115](https://github.com/clashperk/clashofclans.js/commit/ff4111530d6293ef1fc54aa916436130fc30a09c))
138
-
139
- - `Util.formatTag(tag: string): string`
140
- - `Util.formatDate(date: string): Date`
141
- - `Util.isValidTag(tag: string): boolean`
142
- - `Util.encodeTagToId(tag: string): string` (Removed on 2.3.0)
143
- - `Util.decodeIdToTag(id: string): string` (Removed on 2.3.0)
144
-
145
- - Support of async/await for custom events ([#79](https://github.com/clashperk/clashofclans.js/pull/79)) ([ff41115](https://github.com/clashperk/clashofclans.js/commit/a23db3786bcca44b8547c70f27773bdb1216f990))
146
-
147
- ## 2.0.2 (2021-11-30)
148
-
149
- ### Bug Fixes
150
-
151
- - Return `null` for `RankedPlayer.clan` if they are not in the clan. ([#73](https://github.com/clashperk/clashofclans.js/pull/73)) ([ba82327](https://github.com/clashperk/clashofclans.js/commit/ba8232740f4ca9af2bcc7971aca3574612ef25b6))
152
- - `OverrideOptions` added for `Client#getClans` and `RESTManager#getClans` ([#73](https://github.com/clashperk/clashofclans.js/pull/73)) ([ba82327](https://github.com/clashperk/clashofclans.js/commit/ba8232740f4ca9af2bcc7971aca3574612ef25b6))
153
- - `SeasonRankedPlayer` class for legend league ranking. ([#73](https://github.com/clashperk/clashofclans.js/pull/73)) ([ba82327](https://github.com/clashperk/clashofclans.js/commit/ba8232740f4ca9af2bcc7971aca3574612ef25b6))
154
-
155
- ## 2.0.1 (2021-11-27)
156
-
157
- ### Bug Fixes
158
-
159
- - IP retrieval method and Event Loop ([#70](https://github.com/clashperk/clashofclans.js/issues/70)) ([82b84ba](https://github.com/clashperk/clashofclans.js/commit/82b84ba5d96505c43b75e53aa07f547ef0b77778))
160
-
161
- ## 2.0.0 (2021-11-26)
162
-
163
- This new version is a complete TypeScript rewrite to convert everything from plain (literal JSON) objects to class (constructor) objects and support a lot more features.
164
-
165
- ### Features
166
-
167
- - HTTP Request Request Retries ([#26](https://github.com/clashperk/clashofclans.js/issues/26)) ([94585f3](https://github.com/clashperk/clashofclans.js/commit/94585f3a84a7175b2d07872f9eb9e42372b95e12))
168
- - Event Manager and Custom Events ([#37](https://github.com/clashperk/clashofclans.js/issues/37)) ([5027ae6](https://github.com/clashperk/clashofclans.js/commit/5027ae663a8e07175e17384c7e5706f4a1a7afb4))
169
- - Email Password Login ([#31](https://github.com/clashperk/clashofclans.js/issues/31)) ([4153cd3](https://github.com/clashperk/clashofclans.js/commit/4153cd37ea0e1c71550b9e892105b84d5a407e23))
170
- - Queue Throttler and Batch Throttler ([#34](https://github.com/clashperk/clashofclans.js/issues/34)) ([3a8f051](https://github.com/clashperk/clashofclans.js/commit/3a8f051552e93b98f89bc7d524acdecddf242718))
171
- - Override Request Options ([#36](https://github.com/clashperk/clashofclans.js/issues/36)) ([42d7fdd](https://github.com/clashperk/clashofclans.js/commit/42d7fdd36262cc46f23b731f8cffb9daea19d3b0))
172
- - Internal Caching Options ([#53](https://github.com/clashperk/clashofclans.js/issues/53)) ([984451d](https://github.com/clashperk/clashofclans.js/commit/30ea3240c11866008d0dae514468c0fdbb34ffd0))
173
- - Additional Properties for Player Units ([#65](https://github.com/clashperk/clashofclans.js/pull/65)) ([aa1696](https://github.com/clashperk/clashofclans.js/commit/aa1696243d96d4fed0250b4282c60522a6482343))