Skip to content

Commit

Permalink
add TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
jakjus committed Aug 8, 2022
1 parent 3e9038f commit 7e5253a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
63 changes: 61 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@ npm install haxball.js
#### 💻 Module Usage Example

```js
// room.js

const HaxballJS = require("haxball.js");

HaxballJS.then((HBInit) => {
// All is same as Haxball Headless Host Documentation
// Same as in Haxball Headless Host Documentation
const room = HBInit({
roomName: "Haxball.JS",
maxPlayers: 16,
public: true,
noPlayer: true,
token: "TOKEN", // Required
token: "YOUR_TOKEN_HERE", // Required
});

room.setDefaultStadium("Big");
Expand All @@ -54,6 +56,61 @@ HaxballJS.then((HBInit) => {
});
```

#### 💻 (Optional) TypeScript
From v2.1.0, the package has basic typings included. Typings are automatically imported alongside `haxball.js` package.

Install TypeScript and ts-node in your project:
```bash
npm install typescript --save-dev
npm install ts-node --save-dev
```

Name the file `room.ts` instead of `room.js` and use example room code from the previous section.

You may run the server with `ts-node room.ts` instead of `node room.js`.

#### 💻 (Optional) Modularize Room Script
You can build full room scripts as NPM packages, that export `roomBuilder` function. These functions can be easily imported by another packages (launchers, remote orchestrators, etc.).

```ts
// super-futsal-room/index.ts

import { Headless } from "haxball.js"

// Every user installing the package will have to
// implement this interface in order to run it.
interface RoomArgs {
token: string
timeLimit: number
}

const roomBuilder = (HBInit: Headless, args: RoomArgs) => {
let room = HBInit({
roomName: "Hello TypeScript!",
token: args.token
})

room.setTimeLimit(args.timeLimit)
}

export default roomBuilder;
```

```ts
// room-launcher/index.ts

// Local path as example, but may refer to installed npm package
import roomBuilder from "../super-futsal-room";
import HaxballJS from "haxball.js";

HaxballJS.then((HBInit => roomBuilder(HBInit, {
// Interface for config arguments is provided by the room script author
// Not defining them results in TypeScript error
token: "YOUR_TOKEN_HERE",
timeLimit: 3
})))
```

---

<h2 id="technologies">🚀 Technologies</h2>
Expand Down Expand Up @@ -83,6 +140,7 @@ HaxballJS.then((HBInit) => {
- [x] Promise based
- [x] Synchronous
- [x] Performant
- [x] Strongly Typed

[Back To The Top](#title)

Expand Down Expand Up @@ -114,6 +172,7 @@ HaxballJS.then((HBInit) => {
<p>

<a href="https://github.com/mertushka"><img width="60" src="https://avatars1.githubusercontent.com/u/34413473?v=4"/>
<a href="https://github.com/jakjus"><img width="60" src="https://avatars.githubusercontent.com/u/43467994?v=4"/>

</p>

Expand Down
17 changes: 15 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "haxball.js",
"version": "2.0.2",
"version": "2.1.0",
"description": "A powerful library for interacting with the Haxball Headless API",
"main": "src/index.js",
"types": "types/index.d.ts",
"scripts": {
"test": "mocha --recursive --exit"
},
Expand All @@ -24,6 +25,7 @@
},
"devDependencies": {
"@mapbox/node-pre-gyp": "^1.0.9",
"@types/haxball-headless-browser": "^0.1.0",
"mocha": "^9.2.2"
},
"repository": {
Expand Down
6 changes: 6 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import "haxball-headless-browser";

export declare type Headless = (RoomConfig: RoomConfigObject) => RoomObject;

declare const HaxballJS: Promise<Headless>
export default HaxballJS

0 comments on commit 7e5253a

Please sign in to comment.