Skip to content

Commit

Permalink
Merge pull request #20 from Pixelicc/mowojang-js
Browse files Browse the repository at this point in the history
mowojang-js
  • Loading branch information
Pixelicc authored Jul 8, 2024
2 parents c76f13b + eb64e12 commit fe88867
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
cache: "pnpm"
- run: pnpm install
- run: pnpm install --frozen-lockfile
- run: pnpm build
- run: pnpm test
6 changes: 5 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
.github
.vscode
docs
src
types
test
dist/test
.prettierignore
.prettierrc
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Mowojang-API NPM Wrapper
# mowojang-js

![NPM Version](https://img.shields.io/npm/v/mowojang?label=NPM)
![NPM Downloads](https://img.shields.io/npm/dw/mowojang?label=Downloads)
![NPM Downloads](https://img.shields.io/npm/dm/mowojang?label=Downloads)
![NPM License](https://img.shields.io/npm/l/mowojang?label=License)

> [!IMPORTANT]
> This is not an official Wrapper for the [Mowojang-API](https://mowojang.matdoes.dev/)!
> This is a third party wrapper for the [Mowojang-API](https://mowojang.matdoes.dev/)!
## ✨ Quick Start

Expand All @@ -19,7 +19,7 @@ const username = await Mowojang.getUsername("14727fae-fbdc-4aff-848c-d2713eb9939
```

> [!TIP]
> If you plan on using this in a long-running process with frequent usage check out this guide on how to setup custom caching to prevent memory leaks.
> If you plan on using this in a long-running process with heavy usage check out this guide on how to setup custom caching to prevent memory leaks.
> Guide: [Custom-Caching](docs/custom-caching.md)
## 🔧 Functions / Supported Endpoints
Expand All @@ -28,12 +28,12 @@ const username = await Mowojang.getUsername("14727fae-fbdc-4aff-848c-d2713eb9939
- `getProfile()`: Retrieve full Player Data by UUID or Username
- `getSessions()`: Retrieve Player Session Data of multiple Players by UUID or Username
- `getSession()`: Retrieve Player Session Data by UUID or Username
- `getUUID()`: Converts Username to UUID
- `getUsername()`: Converts UUID to Username
- `getUUID()`: Converts a Player's Username to UUID
- `getUsername()`: Converts a Player's UUID to Username
- `getSkin()`: Returns the Skin the specified Player is currently wearing
- `getSkinBuffer()`: Skin-Data fetched via getSkin() as a Buffer
- `getCape()`: Returns the Cape the specified Player is currently wearing
- `getCapeBuffer()`: Cape-Data fetched via getCape() as a Buffer
- `getSkinBuffer()`: Returns Skin Image fetched via getSkin() as a Buffer
- `getCape()`: Returns the Cape the specified Player has currently selected
- `getCapeBuffer()`: Returns Cape Image fetched via getCape() as a Buffer

All above Functions accept an second Argument containing an optional config.
Cache options can also be passed there via the cache key. All available cache options can be found [here](https://axios-cache-interceptor.js.org/config/request-specifics#cache).
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mowojang",
"version": "0.2.2",
"description": "A simple JavaScript and TypeScript compatible API Wrapper for the Mowojang-API.",
"description": "A simple JavaScript and TypeScript compatible wrapper for the Mowojang-API.",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"type": "module",
Expand All @@ -13,7 +13,7 @@
},
"repository": {
"type": "git",
"url": "https://github.com/pixelicc/mowojang-api-wrapper.git"
"url": "https://github.com/pixelicc/mowojang-js.git"
},
"keywords": [
"Minecraft",
Expand Down
20 changes: 10 additions & 10 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class Client {
}

/**
* Returns an Array of Player Profiles
* Returns an Array of Player Profiles consisting of their Usernames and UUIDs
*
* Players considered "INVALID" are excluded from the results
*/
Expand Down Expand Up @@ -59,7 +59,7 @@ export default class Client {
}

/**
* Returns a Player Profile
* Returns a Player's Profile consisting of their Username and UUID
*
*/
public async getProfile(
Expand All @@ -75,7 +75,7 @@ export default class Client {
}

/**
* Simple Wrapper to only retrieve the UUID from the Player's Profile Data
* A Simple Wrapper to Retrieve Only the UUID of a Player's Profile
*
*/
public async getUUID(username: Username, config?: MowojangRequestConfig): Promise<null | Username> {
Expand All @@ -87,7 +87,7 @@ export default class Client {
}

/**
* Simple Wrapper to only retrieve the Username from the Player's Profile Data
* A Simple Wrapper to Retrieve Only the Username of a Player's Profile
*
*/
public async getUsername(UUID: UUID, config?: MowojangRequestConfig): Promise<null | UUID> {
Expand All @@ -99,7 +99,7 @@ export default class Client {
}

/**
* Returns an Array of Player Sessions
* Returns an Array of Player Sessions consisting of their Usernames, UUIDs, Skins and Capes
*
* Players considered "INVALID" are excluded from the results
*/
Expand Down Expand Up @@ -133,7 +133,7 @@ export default class Client {
}

/**
* Returns a Player Session
* Returns a Player's Sessions consisting of their Username, UUID, Skin and Cape
*
*/
public async getSession(
Expand Down Expand Up @@ -191,7 +191,7 @@ export default class Client {
}

/**
* Simple Wrapper to only retrieve the Skin Data from the Player's Session Data
* A Simple Wrapper to Retrieve Only the Skin from a Player's Session
*
*/
public async getSkin(player: Player, config?: MowojangRequestConfig): Promise<null | MowojangSkin> {
Expand All @@ -201,7 +201,7 @@ export default class Client {
}

/**
* Simple Wrapper to only retrieve the Player's Skin from the Player's Session Data loaded into a Buffer
* A Simple Wrapper to Retrieve Only the Player's Skin from their Session Data Loaded into a Buffer
*
*/
public async getSkinBuffer(player: Player, config?: MowojangRequestConfig): Promise<null | Buffer> {
Expand All @@ -217,7 +217,7 @@ export default class Client {
}

/**
* Simple Wrapper to only retrieve the Cape Data from the Player's Session Data
* A Simple Wrapper to Retrieve Only the Cape Data from a Player's Session
*
*/
public async getCape(player: Player, config?: MowojangRequestConfig): Promise<null | MowojangCape> {
Expand All @@ -227,7 +227,7 @@ export default class Client {
}

/**
* Simple Wrapper to only retrieve the Player's Cape from the Player's Session Data loaded into a Buffer
* A Simple Wrapper to Retrieve Only the Player's Cape from their Session Data Loaded into a Buffer
*
*/
public async getCapeBuffer(player: Player, config?: MowojangRequestConfig): Promise<null | Buffer> {
Expand Down
13 changes: 11 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { UUID } from "../types/index.js";

export const undashUUID = (UUID: string): UUID => UUID.replace(/-/g, "").toLowerCase();
export const dashUUID = (UUID: string): UUID =>
/**
* Removes dashes from a UUID string
*
*/
export const undashUUID = (UUID: UUID): UUID => UUID.replace(/-/g, "").toLowerCase();

/**
* Adds dashes to a UUID string if not already dashed
*
*/
export const dashUUID = (UUID: UUID): UUID =>
undashUUID(UUID).replace(/(.{8})(.{4})(.{4})(.{4})(.{12})/, "$1-$2-$3-$4-$5");
11 changes: 11 additions & 0 deletions src/validators.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
import { Player } from "../types/index.js";
import { undashUUID } from "./utils.js";

/**
* Validates a UUIDv4 string
*
*/
export const validateUUID = (UUID: string): boolean => {
if (typeof UUID !== "string") return false;
UUID = undashUUID(UUID);
if (UUID.length !== 32) return false;
return /[0-9a-f]{12}4[0-9a-f]{19}/.test(UUID);
};

/**
* Validates a Minecraft Username
*/
export const validateUsername = (username: string): boolean => {
if (typeof username !== "string") return false;
return /^[a-zA-Z0-9_]{1,16}$/.test(username);
};

/**
* Validates a Minecraft Player by either validating their Username or supplied UUID
*
*/
export const validatePlayer = (player: Player): boolean => validateUUID(player) || validateUsername(player);

export const validateArray = <T>(
Expand Down

0 comments on commit fe88867

Please sign in to comment.