Skip to content
This repository was archived by the owner on Oct 27, 2024. It is now read-only.

Commit aee8be3

Browse files
committed
add achievements and challenges
1 parent 9d9d5a1 commit aee8be3

File tree

7 files changed

+124
-16
lines changed

7 files changed

+124
-16
lines changed

README.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,40 @@
22

33
A library to make requests to the Hypixel API, but with TypeScript to help you avoid mistakes. This library does not modify or process any data returned, that's up to you.
44

5-
I made this primarily for [skyblock-api](https://github.com/skyblockstats/skyblock-api), so information for other games may be limited. Feel free to contribute if you want these, though!
5+
I aim to cover the entire Hypixel API, and the tests automatically check our types with the Hypixel API every day so new fields can be added quickly.
66

7-
If you find missing/incorrect typings or JSDocs, please also contribute a fix.
7+
## Usage
8+
9+
Make a request:
10+
```ts
11+
import hypixel from 'typed-hypixel-api'
12+
13+
const res = await hypixel.request(
14+
'player',
15+
{
16+
key: 'your-api-key',
17+
uuid: '6536bfed869548fd83a1ecd24cf2a0fd'
18+
}
19+
)
20+
21+
if (res.data.success)
22+
console.log(res.data.player.playername)
23+
```
24+
25+
Typed wrapper function (for example to retry on errors or handle ratelimits):
26+
```ts
27+
export let sendApiRequest = async<P extends keyof hypixel.Requests>(path: P, options: hypixel.Requests[P]['options']): Promise<hypixel.Requests[P]['response']['data']> => {
28+
let response: hypixel.Requests[P]['response']
29+
try {
30+
response = await hypixel.request(
31+
path,
32+
options
33+
)
34+
} catch (e) {
35+
await sleep(1000)
36+
return await sendApiRequest(path, options)
37+
}
38+
39+
return response.data
40+
}
41+
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typed-hypixel-api",
3-
"version": "0.4.1",
3+
"version": "0.4.2",
44
"main": "build/index.js",
55
"types": "build/index.d.js",
66
"repository": "https://github.com/skyblockstats/typed-hypixel-api.git",

src/index.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
import { fetch, Headers, RequestInit } from 'undici'
22
export * from './responses/index'
33
import {
4+
// errors
45
InvalidApiKeyResponse,
56
MalformedUuidResponse,
67
MissingFieldResponse,
78
ThrottleResponse,
89

9-
ApiKeyInformationResponse,
10-
FriendsResponse,
11-
OnlineStatusResponse,
12-
PlayerDataResponse,
13-
RecentGamesResponse,
14-
GuildResponse,
15-
10+
// resources
1611
SkyBlockCollectionsResponse,
1712
SkyBlockElectionResponse,
13+
SkyBlockSkillsResponse,
1814
SkyBlockItemsResponse,
19-
SkyBlockProfileResponse,
15+
AchievementsResponse,
16+
ChallengesResponse,
17+
18+
// everything else
19+
ApiKeyInformationResponse,
20+
GamesInformationResponse,
2021
SkyBlockProfilesResponse,
21-
SkyBlockSkillsResponse,
22+
SkyBlockProfileResponse,
2223
RankedSkywarsResponse,
23-
GamesInformationResponse,
24+
OnlineStatusResponse,
25+
RecentGamesResponse,
26+
PlayerDataResponse,
27+
FriendsResponse,
28+
GuildResponse,
2429
} from './responses/index'
2530

2631

@@ -71,6 +76,14 @@ export interface Requests {
7176
options: {}
7277
response: Response<GamesInformationResponse>
7378
}
79+
'resources/achievements': {
80+
options: {}
81+
response: Response<AchievementsResponse>
82+
}
83+
'resources/challenges': {
84+
options: {}
85+
response: Response<ChallengesResponse>
86+
}
7487
'skyblock/profiles': {
7588
options: {
7689
uuid: string
@@ -189,3 +202,4 @@ export const request = async<P extends keyof Requests>(path: P, options: Request
189202
headers: headers as any
190203
}
191204
}
205+

src/responses/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ export * from './resources/skyblock/collections'
22
export * from './resources/skyblock/election'
33
export * from './resources/skyblock/skills'
44
export * from './resources/skyblock/items'
5+
export * from './resources/achievements'
6+
export * from './resources/challenges'
57
export * from './resources/games'
68

79
export * from './skyblock/_profile_member'
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export interface AchievementsResponse {
2+
success: true
3+
lastUpdated: number
4+
challenges: {
5+
[key: string]: {
6+
id: string
7+
name: string
8+
rewards: {
9+
type: 'MultipliedExperienceReward'
10+
amount: number
11+
}[]
12+
}
13+
}
14+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
export interface ChallengesResponse {
2+
success: true
3+
lastUpdated: number
4+
/**
5+
* The list of all Hypixel achievements. The keys are lowercase game/category IDs, for example `christmas2017`, `gingerbread`, `skyblock`.
6+
*/
7+
achievements: {
8+
/** The lowercase database names, for example `christmas2017`, `gingerbread`, `skyblock`. */
9+
[key: string]: {
10+
/** Achievements that can only be unlocked once. */
11+
one_time: {
12+
[key: string]: {
13+
points: number
14+
/** The name of the achievement. */
15+
name: string
16+
/** The description of the achievement. */
17+
description: string
18+
/** The percentage of people who've played the game who have unlocked the achievement. This is a decimal between 0 and 100. */
19+
gamePercentUnlocked: number
20+
/** The percentage of people who've unlocked the achievement. This is a decimal between 0 and 100. */
21+
globalPercentUnlocked: number
22+
}
23+
}
24+
/** Achievements that have different tiers to them. */
25+
tiered: {
26+
[key: string]: {
27+
/** The name of the achievement. */
28+
name: string
29+
/** The description of the achievement. */
30+
description: string
31+
tiers: {
32+
tier: number
33+
points: number
34+
amount: number
35+
}[]
36+
}
37+
}
38+
total_points: number
39+
total_legacy_points: number
40+
}
41+
}
42+
}

test/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ async function testData(typeName, data) {
2727
console.error(err.stdout)
2828
throw new Error(err)
2929
}
30-
console.log('Passed', typeName)
30+
console.log('\x1b[32m%s\x1b[0m', '✓', 'Passed', typeName)
3131
}
3232

33-
console.log('ok doing tsc');
33+
console.log('Running tests. This will take a few moments, be patient!');
3434

3535
(async () => {
3636
await exec('cd test')
@@ -69,6 +69,8 @@ console.log('ok doing tsc');
6969
}, true))
7070

7171
await testData('GamesInformationResponse', await request('resources/games', {}, true))
72+
await testData('ChallengesResponse', await request('resources/challenges', {}, true))
73+
await testData('AchievementsResponse', await request('resources/achievements', {}, true))
7274

73-
console.log('Everything passed :)')
75+
console.log('\x1b[32m%s\x1b[0m', '✓ Everything passed.', 'Good job! :)')
7476
})()

0 commit comments

Comments
 (0)