astrocoins 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # astrocoins
2
+
3
+ Node.js / TypeScript client for the AstroCoins API. A 1:1 port of the Python
4
+ `astrocoins` library — it talks to the same REST endpoints
5
+ (`GET /balance/:id`, `POST /add`, `POST /remove`, `POST /transfer`,
6
+ `GET|POST /blacklist`, `GET|DELETE /blacklist/:id`) and maps HTTP errors to
7
+ typed exceptions. No other logic.
8
+
9
+ ## Install (once published)
10
+
11
+ ```bash
12
+ npm install astrocoins
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```ts
18
+ import {
19
+ AstroCoinsClient,
20
+ InsufficientFundsError,
21
+ BlacklistedError,
22
+ } from "astrocoins";
23
+
24
+ const client = new AstroCoinsClient({
25
+ apiKey: "your-api-key", // or set ASTROCOINS_API_KEY env var
26
+ });
27
+
28
+ const balance = await client.getBalance(123456789);
29
+ console.log(balance);
30
+
31
+ await client.addCoins(123456789, 50, "daily reward");
32
+
33
+ try {
34
+ await client.removeCoins(123456789, 1000, "purchase");
35
+ } catch (err) {
36
+ if (err instanceof InsufficientFundsError) {
37
+ console.log(`Only has ${err.available}, needed ${err.requested}`);
38
+ }
39
+ }
40
+
41
+ // Transfer coins between two users, within this key's wallet space
42
+ const result = await client.transfer(123456789, 987654321, 25, "trade");
43
+ console.log(result.from_balance, result.to_balance);
44
+
45
+ // Per-key blacklist — blocks a user from send/receive through this key only
46
+ await client.blacklistUser(123456789, "chargeback abuse");
47
+ const blocked = await client.isBlacklisted(123456789);
48
+ await client.unblacklistUser(123456789);
49
+
50
+ try {
51
+ await client.addCoins(123456789, 50);
52
+ } catch (err) {
53
+ if (err instanceof BlacklistedError) {
54
+ console.log(`${err.userId} is blacklisted: ${err.reason}`);
55
+ }
56
+ }
57
+ ```
58
+
59
+ Works with CommonJS `require("astrocoins")` too, since it compiles to CommonJS.
@@ -0,0 +1,48 @@
1
+ export interface AstroCoinsClientOptions {
2
+ /** API key. Falls back to the ASTROCOINS_API_KEY env var if omitted. */
3
+ apiKey?: string;
4
+ /** Base URL for the API. Falls back to ASTROCOINS_BASE_URL, then a default. */
5
+ baseUrl?: string;
6
+ /** Request timeout in milliseconds. Default 8000. */
7
+ timeoutMs?: number;
8
+ }
9
+ export interface TransferResult {
10
+ from_user_id: number;
11
+ to_user_id: number;
12
+ amount: number;
13
+ from_balance: number;
14
+ to_balance: number;
15
+ }
16
+ export interface BlacklistEntry {
17
+ user_id: string;
18
+ reason: string;
19
+ added_at: number;
20
+ }
21
+ export declare class AstroCoinsClient {
22
+ readonly apiKey: string;
23
+ readonly baseUrl: string;
24
+ private readonly timeoutMs;
25
+ constructor(options?: AstroCoinsClientOptions);
26
+ private request;
27
+ private safeJson;
28
+ private safeText;
29
+ getBalance(userId: number): Promise<number>;
30
+ addCoins(userId: number, amount: number, reason?: string): Promise<number>;
31
+ removeCoins(userId: number, amount: number, reason?: string): Promise<number>;
32
+ /**
33
+ * Moves coins from one user to another within this key's wallet space.
34
+ * Throws BlacklistedError if either side is blacklisted for this key,
35
+ * InsufficientFundsError if the sender's balance isn't enough.
36
+ */
37
+ transfer(fromUserId: number, toUserId: number, amount: number, reason?: string): Promise<TransferResult>;
38
+ /**
39
+ * Blacklists a user within this key's wallet space only. They will no
40
+ * longer be able to send/receive coins through this key until removed
41
+ * with unblacklistUser.
42
+ */
43
+ blacklistUser(userId: number, reason?: string): Promise<void>;
44
+ unblacklistUser(userId: number): Promise<void>;
45
+ isBlacklisted(userId: number): Promise<boolean>;
46
+ /** Returns every blacklist entry for this key. */
47
+ listBlacklist(): Promise<BlacklistEntry[]>;
48
+ }
package/dist/client.js ADDED
@@ -0,0 +1,149 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AstroCoinsClient = void 0;
4
+ const errors_1 = require("./errors");
5
+ const ENV_API_KEY = "ASTROCOINS_API_KEY";
6
+ const ENV_BASE_URL = "ASTROCOINS_BASE_URL";
7
+ const DEFAULT_BASE_URL = "https://astrobot.adevs.site/api/coins";
8
+ class AstroCoinsClient {
9
+ constructor(options = {}) {
10
+ const apiKey = options.apiKey ??
11
+ (typeof process !== "undefined" ? process.env[ENV_API_KEY] : undefined);
12
+ if (!apiKey) {
13
+ throw new errors_1.ConfigurationError("An API key is required: pass { apiKey } or set the " +
14
+ `${ENV_API_KEY} environment variable. You can get a key ` +
15
+ "from an AstroCoins administrator.");
16
+ }
17
+ this.apiKey = apiKey;
18
+ const baseUrl = options.baseUrl ??
19
+ (typeof process !== "undefined" ? process.env[ENV_BASE_URL] : undefined) ??
20
+ DEFAULT_BASE_URL;
21
+ this.baseUrl = baseUrl.replace(/\/+$/, "");
22
+ this.timeoutMs = options.timeoutMs ?? 8000;
23
+ }
24
+ async request(method, path, body) {
25
+ const controller = new AbortController();
26
+ const timer = setTimeout(() => controller.abort(), this.timeoutMs);
27
+ let resp;
28
+ try {
29
+ resp = await fetch(`${this.baseUrl}${path}`, {
30
+ method,
31
+ headers: {
32
+ "X-API-Key": this.apiKey,
33
+ ...(body ? { "Content-Type": "application/json" } : {}),
34
+ },
35
+ body: body ? JSON.stringify(body) : undefined,
36
+ signal: controller.signal,
37
+ });
38
+ }
39
+ catch (exc) {
40
+ const reason = exc instanceof Error ? exc.message : String(exc);
41
+ throw new errors_1.APIError(0, `Could not connect to the AstroCoins server: ${reason}`);
42
+ }
43
+ finally {
44
+ clearTimeout(timer);
45
+ }
46
+ if (resp.status === 401) {
47
+ throw new errors_1.AuthenticationError("The API key is invalid or has been revoked.");
48
+ }
49
+ if (resp.status === 429) {
50
+ throw new errors_1.RateLimitError("Daily limit for this key has been exceeded.");
51
+ }
52
+ if (resp.status === 403) {
53
+ const errBody = await this.safeJson(resp);
54
+ const blockedId = body
55
+ ?.user_id ??
56
+ body?.from_user_id ??
57
+ body?.to_user_id;
58
+ throw new errors_1.BlacklistedError(blockedId, errBody.error ?? "");
59
+ }
60
+ if (resp.status === 400 && body && "amount" in body) {
61
+ const errBody = await this.safeJson(resp);
62
+ if ("current_balance" in errBody) {
63
+ const subjectId = body.user_id ?? body.from_user_id;
64
+ throw new errors_1.InsufficientFundsError(subjectId, body.amount ?? 0, errBody.current_balance ?? 0);
65
+ }
66
+ const detail = errBody.error ?? errBody.detail ?? (await this.safeText(resp));
67
+ throw new errors_1.APIError(resp.status, detail);
68
+ }
69
+ if (!resp.ok) {
70
+ const errBody = await this.safeJson(resp);
71
+ const detail = errBody.error ?? errBody.detail ?? (await this.safeText(resp));
72
+ throw new errors_1.APIError(resp.status, detail);
73
+ }
74
+ return (await this.safeJson(resp));
75
+ }
76
+ async safeJson(resp) {
77
+ try {
78
+ return (await resp.clone().json());
79
+ }
80
+ catch {
81
+ return {};
82
+ }
83
+ }
84
+ async safeText(resp) {
85
+ try {
86
+ return await resp.text();
87
+ }
88
+ catch {
89
+ return "";
90
+ }
91
+ }
92
+ async getBalance(userId) {
93
+ const data = await this.request("GET", `/balance/${Math.trunc(userId)}`);
94
+ return data.balance ?? 0;
95
+ }
96
+ async addCoins(userId, amount, reason = "") {
97
+ const data = await this.request("POST", "/add", {
98
+ user_id: Math.trunc(userId),
99
+ amount: Math.trunc(amount),
100
+ reason,
101
+ });
102
+ return data.balance ?? 0;
103
+ }
104
+ async removeCoins(userId, amount, reason = "") {
105
+ const data = await this.request("POST", "/remove", {
106
+ user_id: Math.trunc(userId),
107
+ amount: Math.trunc(amount),
108
+ reason,
109
+ });
110
+ return data.balance ?? 0;
111
+ }
112
+ /**
113
+ * Moves coins from one user to another within this key's wallet space.
114
+ * Throws BlacklistedError if either side is blacklisted for this key,
115
+ * InsufficientFundsError if the sender's balance isn't enough.
116
+ */
117
+ async transfer(fromUserId, toUserId, amount, reason = "") {
118
+ return this.request("POST", "/transfer", {
119
+ from_user_id: Math.trunc(fromUserId),
120
+ to_user_id: Math.trunc(toUserId),
121
+ amount: Math.trunc(amount),
122
+ reason,
123
+ });
124
+ }
125
+ /**
126
+ * Blacklists a user within this key's wallet space only. They will no
127
+ * longer be able to send/receive coins through this key until removed
128
+ * with unblacklistUser.
129
+ */
130
+ async blacklistUser(userId, reason = "") {
131
+ await this.request("POST", "/blacklist", {
132
+ user_id: Math.trunc(userId),
133
+ reason,
134
+ });
135
+ }
136
+ async unblacklistUser(userId) {
137
+ await this.request("DELETE", `/blacklist/${Math.trunc(userId)}`);
138
+ }
139
+ async isBlacklisted(userId) {
140
+ const data = await this.request("GET", `/blacklist/${Math.trunc(userId)}`);
141
+ return data.blacklisted ?? false;
142
+ }
143
+ /** Returns every blacklist entry for this key. */
144
+ async listBlacklist() {
145
+ return this.request("GET", "/blacklist");
146
+ }
147
+ }
148
+ exports.AstroCoinsClient = AstroCoinsClient;
149
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":";;;AAAA,qCAOkB;AAElB,MAAM,WAAW,GAAG,oBAAoB,CAAC;AACzC,MAAM,YAAY,GAAG,qBAAqB,CAAC;AAE3C,MAAM,gBAAgB,GAAG,uCAAuC,CAAC;AA6CjE,MAAa,gBAAgB;IAK3B,YAAY,UAAmC,EAAE;QAC/C,MAAM,MAAM,GACV,OAAO,CAAC,MAAM;YACd,CAAC,OAAO,OAAO,KAAK,WAAW,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;QAE1E,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,IAAI,2BAAkB,CAC1B,qDAAqD;gBACnD,GAAG,WAAW,2CAA2C;gBACzD,mCAAmC,CACtC,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QAErB,MAAM,OAAO,GACX,OAAO,CAAC,OAAO;YACf,CAAC,OAAO,OAAO,KAAK,WAAW,CAAC,CAAC,CAAC,OAAO,C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AM,CAAC,EAAE,CAAC,CAAC;IACnE,CAAC;IAED,KAAK,CAAC,aAAa,CAAC,MAAc;QAChC,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,OAAO,CAC7B,KAAK,EACL,cAAc,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CACnC,CAAC;QACF,OAAO,IAAI,CAAC,WAAW,IAAI,KAAK,CAAC;IACnC,CAAC;IAED,kDAAkD;IAClD,KAAK,CAAC,aAAa;QACjB,OAAO,IAAI,CAAC,OAAO,CAAmB,KAAK,EAAE,YAAY,CAAC,CAAC;IAC7D,CAAC;CACF;AArLD,4CAqLC"}
@@ -0,0 +1,28 @@
1
+ export declare class AstroCoinsError extends Error {
2
+ constructor(message: string);
3
+ }
4
+ export declare class ConfigurationError extends AstroCoinsError {
5
+ constructor(message: string);
6
+ }
7
+ export declare class AuthenticationError extends AstroCoinsError {
8
+ constructor(message: string);
9
+ }
10
+ export declare class InsufficientFundsError extends AstroCoinsError {
11
+ readonly userId: number;
12
+ readonly requested: number;
13
+ readonly available: number;
14
+ constructor(userId: number, requested: number, available: number);
15
+ }
16
+ export declare class RateLimitError extends AstroCoinsError {
17
+ constructor(message: string);
18
+ }
19
+ export declare class BlacklistedError extends AstroCoinsError {
20
+ readonly userId?: number;
21
+ readonly reason: string;
22
+ constructor(userId: number | undefined, reason?: string);
23
+ }
24
+ export declare class APIError extends AstroCoinsError {
25
+ readonly statusCode: number;
26
+ readonly detail: string;
27
+ constructor(statusCode: number, detail: string);
28
+ }
package/dist/errors.js ADDED
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.APIError = exports.BlacklistedError = exports.RateLimitError = exports.InsufficientFundsError = exports.AuthenticationError = exports.ConfigurationError = exports.AstroCoinsError = void 0;
4
+ class AstroCoinsError extends Error {
5
+ constructor(message) {
6
+ super(message);
7
+ this.name = "AstroCoinsError";
8
+ Object.setPrototypeOf(this, new.target.prototype);
9
+ }
10
+ }
11
+ exports.AstroCoinsError = AstroCoinsError;
12
+ class ConfigurationError extends AstroCoinsError {
13
+ constructor(message) {
14
+ super(message);
15
+ this.name = "ConfigurationError";
16
+ Object.setPrototypeOf(this, new.target.prototype);
17
+ }
18
+ }
19
+ exports.ConfigurationError = ConfigurationError;
20
+ class AuthenticationError extends AstroCoinsError {
21
+ constructor(message) {
22
+ super(message);
23
+ this.name = "AuthenticationError";
24
+ Object.setPrototypeOf(this, new.target.prototype);
25
+ }
26
+ }
27
+ exports.AuthenticationError = AuthenticationError;
28
+ class InsufficientFundsError extends AstroCoinsError {
29
+ constructor(userId, requested, available) {
30
+ super(`User ${userId} has ${available} coins, which is not enough to cover ${requested}.`);
31
+ this.name = "InsufficientFundsError";
32
+ this.userId = userId;
33
+ this.requested = requested;
34
+ this.available = available;
35
+ Object.setPrototypeOf(this, new.target.prototype);
36
+ }
37
+ }
38
+ exports.InsufficientFundsError = InsufficientFundsError;
39
+ class RateLimitError extends AstroCoinsError {
40
+ constructor(message) {
41
+ super(message);
42
+ this.name = "RateLimitError";
43
+ Object.setPrototypeOf(this, new.target.prototype);
44
+ }
45
+ }
46
+ exports.RateLimitError = RateLimitError;
47
+ class BlacklistedError extends AstroCoinsError {
48
+ constructor(userId, reason = "") {
49
+ let message = `User ${userId ?? "unknown"} is blacklisted for this API key.`;
50
+ if (reason)
51
+ message += ` Reason: ${reason}`;
52
+ super(message);
53
+ this.name = "BlacklistedError";
54
+ this.userId = userId;
55
+ this.reason = reason;
56
+ Object.setPrototypeOf(this, new.target.prototype);
57
+ }
58
+ }
59
+ exports.BlacklistedError = BlacklistedError;
60
+ class APIError extends AstroCoinsError {
61
+ constructor(statusCode, detail) {
62
+ super(`AstroCoins API error (${statusCode}): ${detail}`);
63
+ this.name = "APIError";
64
+ this.statusCode = statusCode;
65
+ this.detail = detail;
66
+ Object.setPrototypeOf(this, new.target.prototype);
67
+ }
68
+ }
69
+ exports.APIError = APIError;
70
+ //# sourceMappingURL=err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AuthenticationError, InsufficientFundsError, RateLimitError, APIError, BlacklistedError, } from "./errors";
package/dist/index.js ADDED
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BlacklistedError = exports.APIError = exports.RateLimitError = exports.InsufficientFundsError = exports.AuthenticationError = exports.ConfigurationError = exports.AstroCoinsError = exports.AstroCoinsClient = void 0;
4
+ var client_1 = require("./client");
5
+ Object.defineProperty(exports, "AstroCoinsClient", { enumerable: true, get: function () { return client_1.AstroCoinsClient; } });
6
+ var errors_1 = require("./errors");
7
+ Object.defineProperty(exports, "AstroCoinsError", { enumerable: true, get: function () { return errors_1.AstroCoinsError; } });
8
+ Object.defineProperty(exports, "ConfigurationError", { enumerable: true, get: function () { return errors_1.ConfigurationError; } });
9
+ Object.defineProperty(exports, "AuthenticationError", { enumerable: true, get: function () { return errors_1.AuthenticationError; } });
10
+ Object.defineProperty(exports, "InsufficientFundsError", { enumerable: true, get: function () { return errors_1.InsufficientFundsError; } });
11
+ Object.defineProperty(exports, "RateLimitError", { enumerable: true, get: function () { return errors_1.RateLimitError; } });
12
+ Object.defineProperty(exports, "APIError", { enumerable: true, get: function () { return errors_1.APIError; } });
13
+ Object.defineProperty(exports, "BlacklistedError", { enumerable: true, get: function () { return errors_1.BlacklistedError; } });
14
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mCAKkB;AAJhB,0GAAA,gBAAgB,OAAA;AAKlB,mCAQkB;AAPhB,yGAAA,eAAe,OAAA;AACf,4GAAA,kBAAkB,OAAA;AAClB,6GAAA,mBAAmB,OAAA;AACnB,gHAAA,sBAAsB,OAAA;AACtB,wGAAA,cAAc,OAAA;AACd,kGAAA,QAAQ,OAAA;AACR,0GAAA,gBAAgB,OAAA"}
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "astrocoins",
3
+ "version": "2.1.0",
4
+ "description": "Node.js/TypeScript client for the AstroCoins API",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "type": "commonjs",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsc",
13
+ "prepublishOnly": "npm run build"
14
+ },
15
+ "keywords": [
16
+ "astrocoins",
17
+ "api-client",
18
+ "discord"
19
+ ],
20
+ "license": "MIT",
21
+ "devDependencies": {
22
+ "typescript": "^5.5.4",
23
+ "@types/node": "^20.14.9"
24
+ },
25
+ "engines": {
26
+ "node": ">=18"
27
+ }
28
+ }