@gamotech/game-sdk-nodejs 1.0.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/errors.js +9 -0
- package/index.js +126 -0
- package/package.json +18 -0
package/errors.js
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { ProviderSDKError } from "./errors.js";
|
|
2
|
+
|
|
3
|
+
const DEFAULT_API_BASE = "https://api.dpbossking.com/api/v1";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Node.js game server SDK — wallet operations (balance, debit, credit).
|
|
7
|
+
* operatorId is resolved from sessionToken server-side — do not pass it.
|
|
8
|
+
*/
|
|
9
|
+
export class ProviderGameServerSDK {
|
|
10
|
+
#apiBase;
|
|
11
|
+
#gameServerKey;
|
|
12
|
+
|
|
13
|
+
constructor(options = {}) {
|
|
14
|
+
this.#apiBase = (options.apiBaseUrl ?? DEFAULT_API_BASE).replace(/\/+$/, "");
|
|
15
|
+
this.#gameServerKey = options.gameServerKey ?? process.env.GAME_SERVER_API_KEY;
|
|
16
|
+
|
|
17
|
+
if (!this.#gameServerKey) {
|
|
18
|
+
throw new ProviderSDKError(
|
|
19
|
+
"gameServerKey is required — set GAME_SERVER_API_KEY on your game server"
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async getBalance({ sessionToken }) {
|
|
25
|
+
this.#requireFields({ sessionToken }, ["sessionToken"]);
|
|
26
|
+
return this.#walletCall("balance", { sessionToken });
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async getPlayerProfile({ sessionToken }) {
|
|
30
|
+
this.#requireFields({ sessionToken }, ["sessionToken"]);
|
|
31
|
+
return this.#walletCall("player-profile", { sessionToken });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async debit({ sessionToken, amount, transactionId, roundId, tableId, ...extra }) {
|
|
35
|
+
this.#requireFields(
|
|
36
|
+
{ sessionToken, amount, transactionId },
|
|
37
|
+
["sessionToken", "amount", "transactionId"]
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
return this.#walletCall("debit", {
|
|
41
|
+
sessionToken,
|
|
42
|
+
amount,
|
|
43
|
+
transactionId,
|
|
44
|
+
roundId,
|
|
45
|
+
tableId,
|
|
46
|
+
...extra,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async credit({ sessionToken, amount, transactionId, roundId, tableId, ...extra }) {
|
|
51
|
+
this.#requireFields(
|
|
52
|
+
{ sessionToken, amount, transactionId },
|
|
53
|
+
["sessionToken", "amount", "transactionId"]
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
return this.#walletCall("credit", {
|
|
57
|
+
sessionToken,
|
|
58
|
+
amount,
|
|
59
|
+
transactionId,
|
|
60
|
+
roundId,
|
|
61
|
+
tableId,
|
|
62
|
+
...extra,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async #walletCall(operation, body) {
|
|
67
|
+
const data = await this.#request(`/adapters/${operation}`, {
|
|
68
|
+
method: "POST",
|
|
69
|
+
body,
|
|
70
|
+
});
|
|
71
|
+
return data.data;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async #request(path, { method = "POST", body } = {}) {
|
|
75
|
+
const url = `${this.#apiBase}${path.startsWith("/") ? path : `/${path}`}`;
|
|
76
|
+
|
|
77
|
+
const headers = {
|
|
78
|
+
"Content-Type": "application/json",
|
|
79
|
+
"X-Game-Server-Key": this.#gameServerKey,
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
if (body?.sessionToken) {
|
|
83
|
+
headers.Authorization = `Bearer ${body.sessionToken}`;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
let response;
|
|
87
|
+
try {
|
|
88
|
+
response = await fetch(url, {
|
|
89
|
+
method,
|
|
90
|
+
headers,
|
|
91
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
92
|
+
});
|
|
93
|
+
} catch {
|
|
94
|
+
throw new ProviderSDKError("Network error — unable to reach Provider API");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
let data;
|
|
98
|
+
try {
|
|
99
|
+
data = await response.json();
|
|
100
|
+
} catch {
|
|
101
|
+
throw new ProviderSDKError("Invalid JSON response from Provider API", {
|
|
102
|
+
status: response.status,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (!response.ok || data.success === false) {
|
|
107
|
+
throw new ProviderSDKError(data.message || "Provider API request failed", {
|
|
108
|
+
status: response.status,
|
|
109
|
+
data,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return data;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
#requireFields(obj, fields) {
|
|
117
|
+
for (const field of fields) {
|
|
118
|
+
if (obj[field] === undefined || obj[field] === null || obj[field] === "") {
|
|
119
|
+
throw new ProviderSDKError(`${field} is required`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export { ProviderSDKError };
|
|
126
|
+
export default ProviderGameServerSDK;
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gamotech/game-sdk-nodejs",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Provider platform Node.js game server SDK — wallet balance, debit, credit",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./index.js"
|
|
9
|
+
},
|
|
10
|
+
"files": ["index.js", "errors.js"],
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=18"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
}
|
|
18
|
+
}
|