Skip to content
This repository was archived by the owner on Jul 14, 2022. It is now read-only.

Commit 250cbc0

Browse files
author
Ethan Davis
committed
Original files. Subject to change.
1 parent 797370a commit 250cbc0

File tree

11 files changed

+248
-2
lines changed

11 files changed

+248
-2
lines changed

.gitignore

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
.eslintrc.json
2+
.DS_Store
3+
*.tgz
4+
5+
logs
6+
*.log
7+
npm-debug.log*
8+
yarn-debug.log*
9+
yarn-error.log*
10+
11+
# Only dev-dependencies
12+
package-lock.json
13+
14+
# Env information / sensitive data
15+
.env
16+
pids
17+
*.pid
18+
*.seed
19+
*.pid.lock
20+
sftp-config.json
21+
22+
# Don't include dep directories.
23+
node_modules/
24+
jspm_packages/
25+
26+
# NPM cache
27+
.npm
28+
29+
# Node REPL history
30+
.node_repl_history
31+
32+
*.tmlanguage.cache
33+
*.tmPreferences.cache
34+
*.stTheme.cache
35+
36+
*.sublime-workspace
37+
38+
# Sublime Package Control files
39+
Package Control.last-run
40+
Package Control.ca-list
41+
Package Control.ca-bundle
42+
Package Control.system-ca-bundle
43+
Package Control.cache/
44+
Package Control.ca-certs/
45+
Package Control.merged-ca-bundle
46+
Package Control.user-ca-bundle
47+
oscrypto-ca-bundle.crt
48+
bh_unicode_properties.cache

.npmignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# NPM, Yarn, Travis ignores
2+
3+
.eslintrc.json
4+
logs
5+
*.log
6+
npm-debug.log*
7+
yarn-debug.log*
8+
yarn-error.log*
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
.npm
14+
*.tgz
15+
.travis.yml
16+
17+
# Core resource ignores
18+
19+
docs/
20+
media/

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: node_js
2+
node_js:
3+
- "node"
4+
- "8"
5+
- "7"
6+
- "6"

LICENSE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
License information: https://github.com/omnent/omnent-licenses/blob/master/licenses/OMv2-FO.txt
2+
3+
Copyright 2017 Ethan Davis
4+
5+
Permission is hereby granted, for free, to any person obtaining a copy of this software and associated documentation files (collectively "the Software" or "THE SOFTWARE"), to (without restriction) use, modify, copy, publish, execute, distribute, sublicense, and sell the Software and to allow people who gain access to the Software to do the same as long as this entire license and the above copyright notice are included in all copies or portions of the Software.
6+
7+
NOTWITHSTANDING ANYTHING IN CONTRADICTION, THE AUTHORS AND COPYRIGHT HOLDERS OF THE SOFTWARE SHALL UNDER NO CIRCUMSTANCES BE HELD LIABLE FOR ANY DAMAGES, CLAIM, OR OTHER LIABILITY RELATED TO, IN CONNECTION WITH, OR CAUSED BY USE OR DISTRIBUTION OF THE SOFTWARE. THE SOFTWARE IS PROVIDED "AS-IS" AND IS NOT PROVIDED WITH WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, AND ANY WARRANTY ARISING OUR OF PRIOR COURSE OF DEALING AND USAGE OF TRADE.

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
1-
# hypixel-api
2-
A light Hypixel Public API client for Node
1+
<p align="center" style="text-align: center;"><img src="https://api.hypixel.net/assets/images/logo.png" width="300" alt="Hypixel logo"/></p>
2+
3+
---
4+
5+
> A light Hypixel Public API client for Node
6+
7+
[Full documentation](https://ethanent.github.io/hypixel-api/) | [GitHub](https://github.com/ethanent/hypixel-api) | [NPM](https://www.npmjs.com/package/hypixel-api)
8+
9+
## Installation
10+
11+
```shell
12+
npm install --save hypixel-api
13+
```

class/Client.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const path = require('path')
2+
const pp = require('phin').promisified
3+
4+
const Player = require(path.join(__dirname, 'Player.js'))
5+
6+
/**
7+
* Main Client class. API keys are used to create client instances.
8+
*/
9+
class Client {
10+
constructor (key) {
11+
this.key = key
12+
}
13+
14+
/**
15+
* Get a Player instance for a player.
16+
* @param {string} [targetType=uuid] - Target type. 'name' or 'uuid'
17+
* @param {string} identifier - Identifier for the target. (Either a name or UUID, based on targetType.)
18+
*/
19+
async getPlayer(p1, p2) {
20+
const targetType = (p2 ? p1 : 'uuid')
21+
const identifier = (p2 ? p2 : p1)
22+
23+
if (targetType === 'name') {
24+
let playerResolution = await pp({
25+
'url': 'https://api.mojang.com/profiles/minecraft',
26+
'method': 'POST',
27+
'data': JSON.stringify([identifier])
28+
})
29+
30+
if (playerResolution.statusCode === 200) {
31+
let body
32+
try {
33+
body = JSON.parse(playerResolution.body)
34+
35+
if (!Array.isArray(body)) throw 'Not array'
36+
}
37+
catch (err) {
38+
throw new Error('Invalid response recieved from Mojang UUID resolution endpoint.')
39+
}
40+
41+
if (body.length > 0) {
42+
return new Player(body[0].id, this.key)
43+
}
44+
else {
45+
throw new Error('Player does not exist.')
46+
}
47+
}
48+
else {
49+
throw new Error('Unexpected status code from Mojang UUID resolution endpoint.')
50+
}
51+
}
52+
else {
53+
return new Player(identifier, this.key)
54+
}
55+
}
56+
}
57+
58+
module.exports = Client

class/Player.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const path = require('path')
2+
const p = require('phin')
3+
4+
/**
5+
* Player class, used to represent players.
6+
*/
7+
class Player {
8+
constructor (uuid, key) {
9+
this.uuid = uuid
10+
this.key = key
11+
}
12+
13+
/**
14+
* Get the player's name using Mojang's API
15+
*/
16+
get name() {
17+
p({
18+
'url': 'https://api.mojang.com/users/profiles/minecraft/' + encodeURIComponent(this.uuid),
19+
'method': 'GET'
20+
}, (err, res) => {
21+
if (err) throw err
22+
23+
if (res.statusCode === 200) {
24+
return JSON.parse(res.body).name
25+
}
26+
else {
27+
throw new Error('Mojang name resolution endpoint failed to respond as expected.')
28+
}
29+
})
30+
}
31+
32+
async getData() {
33+
p({
34+
'url': 'https://api.hypixel.net/player?uuid=' + encodeURIComponent(this.uuid) + '&key=' + encodeURIComponent(this.key),
35+
'method': 'GET'
36+
}, (err, res) => {
37+
if (err) throw err
38+
39+
if (res.statusCode === 200) {
40+
return JSON.parse(res.body.toString()).player
41+
}
42+
else {
43+
throw new Error('API endpoint gave unexpected response.')
44+
}
45+
})
46+
}
47+
}
48+
49+
module.exports = Player

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const path = require('path')
2+
3+
module.exports = require(path.join(__dirname, 'class', 'Client.js'))

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "hypixel-api",
3+
"version": "1.0.0",
4+
"description": "A light Hypixel Public API client for Node",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "node test.js"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/ethanent/hypixel-api.git"
12+
},
13+
"keywords": [
14+
"hypixel",
15+
"hypixel-api",
16+
"api-client",
17+
"client",
18+
"api",
19+
"minecraft"
20+
],
21+
"author": "Ethan Davis",
22+
"license": "SEE LICENSE IN LICENSE.md",
23+
"bugs": {
24+
"url": "https://github.com/ethanent/hypixel-api/issues"
25+
},
26+
"homepage": "https://github.com/ethanent/hypixel-api#readme",
27+
"dependencies": {
28+
"phin": "^2.4.18"
29+
}
30+
}

test.js

Whitespace-only changes.

0 commit comments

Comments
 (0)