Skip to content

Latest commit

History

29 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

@nationdex/api

A ForgeScript extension that runs a fast HTTP server powered by uWebSockets.js. It lets you create API routes that run ForgeScript code.

discordLicense: GPL-3.0


Table of Contents


Installation

Run this in your project root:

npm install github:nationdex/api
---
## Getting Started
Add the `API` extension to your ForgeClient:
```tsimport { ForgeClient } from "@tryforge/forgescript";import { API } from "@nationdex/api";const api = new API({ path: "dist/routes", port: 3000});const client = new ForgeClient({ events: ["clientReady"], extensions: [api]});client.login("YOUR_BOT_TOKEN");

When your client starts, the extension loads all route files inside the ./routes folder and starts the server on port 3000.


Configuration

Extension Options

Pass these to new API(options):

OptionTypeDefaultDescription
portnumber | string3000Port to run the server on.
pathstringundefinedFolder containing route files (relative to current working directory).
throttleboolean | ThrottleOptionsfalseEnable rate limiting or configure throttle settings.
cacheIPnumber | string10000Size of the LRU cache for converted IP addresses.
allowedAllowedOptions{}Global IP, host, and header restrictions.
ssluWS.AppOptions{}SSL certificate and key paths if running HTTPS.

Throttle Options

When setting throttle: { ... }:

OptionTypeDefaultDescription
rateWindowMsnumber60000Time window for regular requests in milliseconds.
rateMaxRequestsnumber60Max requests allowed in the rateWindowMs.
burstWindowMsnumber1000Short time window to catch bursts.
burstMaxRequestsnumber3Max requests allowed in the burstWindowMs.
blockDurationnumber600000How long an IP stays blocked (burst blocks double this).
cleanupIntervalMsnumber180000How often expired tracking data is cleaned up.
maxTrackedIPsnumber1500Max tracked IPs stored in memory.
logBlocksbooleanfalseLog a warning when an IP gets blocked.

Access Control Options

Can be set globally on API options or per route:

{hosts: ["example.com","api.example.com"],ips: {white: ["127.0.0.1","::1"],black: ["192.168.1.100"]},headers: {"authorization": "Bearer secret-token"}}

Route Options

Each route object accepts:

FieldTypeRequiredDescription
urlstringYesPath to match, like "/hello" or "/user/:id".
methodstringYesHTTP method (GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD, TRACE, CONNECT, ANY).
codestringYesForgeScript code string to run for the route.
allowedboolean | RouteAllowedOptionsNoSet false to disable access checks, or supply route-specific allowed rules (with optional merge: true).
throttlebooleanNoSet false to skip rate limiting on this specific route.

License

This project is licensed under the GPL-3.0 License.

About

API extension for Project Nationdex 2.0

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages