crafter-kit 1.0.5 → 1.0.8
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/dist/index.d.mts +27 -5
- package/dist/index.d.ts +27 -5
- package/dist/index.js +85 -5
- package/dist/index.mjs +84 -5
- package/package.json +4 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
type ApiError = {
|
|
2
|
+
msg: string;
|
|
3
|
+
};
|
|
1
4
|
type ApiResponse<T> = {
|
|
2
5
|
data: T;
|
|
3
|
-
} |
|
|
4
|
-
error: string;
|
|
5
|
-
};
|
|
6
|
+
} | ApiError;
|
|
6
7
|
type CreateRoomResponse = ApiResponse<CreateRoomData>;
|
|
7
8
|
interface CreateRoomRequest {
|
|
8
9
|
filterChat?: Boolean;
|
|
@@ -10,7 +11,28 @@ interface CreateRoomRequest {
|
|
|
10
11
|
interface CreateRoomData {
|
|
11
12
|
roomId: string;
|
|
12
13
|
}
|
|
13
|
-
|
|
14
|
+
interface Message {
|
|
15
|
+
id: string;
|
|
16
|
+
senderID: string;
|
|
17
|
+
destinationId: string;
|
|
18
|
+
content: string;
|
|
19
|
+
messageType: "DIRECT" | "GROUP";
|
|
20
|
+
flagged: Boolean;
|
|
21
|
+
createdAt: Date;
|
|
22
|
+
}
|
|
23
|
+
type MessageCallback = (message: Message) => void;
|
|
24
|
+
type ConnectCallback = () => void;
|
|
25
|
+
declare class Chatter {
|
|
26
|
+
private onMessageCallbacks;
|
|
27
|
+
private socket;
|
|
28
|
+
private playerID;
|
|
29
|
+
constructor(playerID: string);
|
|
30
|
+
setMessageCallback(sourceID: string, callback: MessageCallback): void;
|
|
31
|
+
connect(): Promise<void>;
|
|
32
|
+
joinRoom(roomID: string, onMessage: MessageCallback): Promise<void>;
|
|
33
|
+
sendRoomMessage(roomID: string, messageContent: string): Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
declare function createRoom(data: CreateRoomRequest): Promise<string>;
|
|
14
36
|
declare function add(a: number, b: number): number;
|
|
15
37
|
|
|
16
|
-
export { type CreateRoomData, type CreateRoomRequest, type CreateRoomResponse, add, createRoom };
|
|
38
|
+
export { Chatter, type ConnectCallback, type CreateRoomData, type CreateRoomRequest, type CreateRoomResponse, type Message, type MessageCallback, add, createRoom };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
type ApiError = {
|
|
2
|
+
msg: string;
|
|
3
|
+
};
|
|
1
4
|
type ApiResponse<T> = {
|
|
2
5
|
data: T;
|
|
3
|
-
} |
|
|
4
|
-
error: string;
|
|
5
|
-
};
|
|
6
|
+
} | ApiError;
|
|
6
7
|
type CreateRoomResponse = ApiResponse<CreateRoomData>;
|
|
7
8
|
interface CreateRoomRequest {
|
|
8
9
|
filterChat?: Boolean;
|
|
@@ -10,7 +11,28 @@ interface CreateRoomRequest {
|
|
|
10
11
|
interface CreateRoomData {
|
|
11
12
|
roomId: string;
|
|
12
13
|
}
|
|
13
|
-
|
|
14
|
+
interface Message {
|
|
15
|
+
id: string;
|
|
16
|
+
senderID: string;
|
|
17
|
+
destinationId: string;
|
|
18
|
+
content: string;
|
|
19
|
+
messageType: "DIRECT" | "GROUP";
|
|
20
|
+
flagged: Boolean;
|
|
21
|
+
createdAt: Date;
|
|
22
|
+
}
|
|
23
|
+
type MessageCallback = (message: Message) => void;
|
|
24
|
+
type ConnectCallback = () => void;
|
|
25
|
+
declare class Chatter {
|
|
26
|
+
private onMessageCallbacks;
|
|
27
|
+
private socket;
|
|
28
|
+
private playerID;
|
|
29
|
+
constructor(playerID: string);
|
|
30
|
+
setMessageCallback(sourceID: string, callback: MessageCallback): void;
|
|
31
|
+
connect(): Promise<void>;
|
|
32
|
+
joinRoom(roomID: string, onMessage: MessageCallback): Promise<void>;
|
|
33
|
+
sendRoomMessage(roomID: string, messageContent: string): Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
declare function createRoom(data: CreateRoomRequest): Promise<string>;
|
|
14
36
|
declare function add(a: number, b: number): number;
|
|
15
37
|
|
|
16
|
-
export { type CreateRoomData, type CreateRoomRequest, type CreateRoomResponse, add, createRoom };
|
|
38
|
+
export { Chatter, type ConnectCallback, type CreateRoomData, type CreateRoomRequest, type CreateRoomResponse, type Message, type MessageCallback, add, createRoom };
|
package/dist/index.js
CHANGED
|
@@ -20,13 +20,88 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
Chatter: () => Chatter,
|
|
23
24
|
add: () => add,
|
|
24
25
|
createRoom: () => createRoom
|
|
25
26
|
});
|
|
26
27
|
module.exports = __toCommonJS(index_exports);
|
|
27
|
-
var
|
|
28
|
+
var import_socket = require("socket.io-client");
|
|
29
|
+
var SERVER_HTTP_URL = "http://localhost:3000";
|
|
30
|
+
var SERVER_WS_URL = "ws://localhost:3000";
|
|
31
|
+
function connectSocket(url) {
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
const socket = (0, import_socket.io)(url, {
|
|
34
|
+
reconnectionAttempts: 3,
|
|
35
|
+
reconnectionDelay: 5e3
|
|
36
|
+
});
|
|
37
|
+
socket.on("connect", () => resolve(socket));
|
|
38
|
+
socket.on(
|
|
39
|
+
"reconnect_failed",
|
|
40
|
+
(err) => reject(new Error(`Could not connect to Crafter: ${err}`))
|
|
41
|
+
);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
var Chatter = class {
|
|
45
|
+
constructor(playerID) {
|
|
46
|
+
this.onMessageCallbacks = /* @__PURE__ */ new Map();
|
|
47
|
+
this.playerID = playerID;
|
|
48
|
+
this.socket = null;
|
|
49
|
+
}
|
|
50
|
+
setMessageCallback(sourceID, callback) {
|
|
51
|
+
this.onMessageCallbacks.set(sourceID, callback);
|
|
52
|
+
}
|
|
53
|
+
async connect() {
|
|
54
|
+
this.socket = await connectSocket(SERVER_WS_URL);
|
|
55
|
+
this.socket.on("disconnect", () => {
|
|
56
|
+
this.socket = null;
|
|
57
|
+
});
|
|
58
|
+
this.socket.on("receiveMessage", (msg) => {
|
|
59
|
+
if (msg.messageType == "GROUP") {
|
|
60
|
+
const msgCallback = this.onMessageCallbacks.get(msg.destinationId);
|
|
61
|
+
if (msgCallback) msgCallback(msg);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
async joinRoom(roomID, onMessage) {
|
|
66
|
+
if (this.socket) {
|
|
67
|
+
this.onMessageCallbacks.set(roomID, onMessage);
|
|
68
|
+
return new Promise((resolve, reject) => {
|
|
69
|
+
this.socket.emit(
|
|
70
|
+
"joinRoom",
|
|
71
|
+
roomID,
|
|
72
|
+
(response) => {
|
|
73
|
+
if (response.success) resolve();
|
|
74
|
+
else reject(new Error(response.error || "Failed to join room"));
|
|
75
|
+
}
|
|
76
|
+
);
|
|
77
|
+
});
|
|
78
|
+
} else {
|
|
79
|
+
throw new Error("Chatter client does not have a valid socket connection");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
sendRoomMessage(roomID, messageContent) {
|
|
83
|
+
if (!this.socket || this.socket.disconnected) {
|
|
84
|
+
return Promise.reject(
|
|
85
|
+
new Error("Chatter client does not have a valid socket connection")
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
return new Promise((resolve, reject) => {
|
|
89
|
+
this.socket.emit(
|
|
90
|
+
"sendMessage",
|
|
91
|
+
roomID,
|
|
92
|
+
this.playerID,
|
|
93
|
+
messageContent,
|
|
94
|
+
(response) => {
|
|
95
|
+
if (response.success) resolve();
|
|
96
|
+
else
|
|
97
|
+
reject(new Error(response.error || "Failed to send room message"));
|
|
98
|
+
}
|
|
99
|
+
);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
};
|
|
28
103
|
async function createRoom(data) {
|
|
29
|
-
const response = await fetch(`${
|
|
104
|
+
const response = await fetch(`${SERVER_HTTP_URL}/api/chat/createRoom`, {
|
|
30
105
|
method: "POST",
|
|
31
106
|
headers: {
|
|
32
107
|
"Content-Type": "application/json"
|
|
@@ -34,16 +109,21 @@ async function createRoom(data) {
|
|
|
34
109
|
body: JSON.stringify(data)
|
|
35
110
|
});
|
|
36
111
|
if (!response.ok) {
|
|
37
|
-
const
|
|
38
|
-
throw new Error(`
|
|
112
|
+
const error = await response.json();
|
|
113
|
+
throw new Error(`Failed to create chat room: ${error.msg}`);
|
|
114
|
+
}
|
|
115
|
+
const responseData = await response.json();
|
|
116
|
+
if (!responseData || !responseData.roomId) {
|
|
117
|
+
throw new Error("Failed to create chat room: missing room ID");
|
|
39
118
|
}
|
|
40
|
-
return
|
|
119
|
+
return responseData.roomId;
|
|
41
120
|
}
|
|
42
121
|
function add(a, b) {
|
|
43
122
|
return a + b;
|
|
44
123
|
}
|
|
45
124
|
// Annotate the CommonJS export names for ESM import in node:
|
|
46
125
|
0 && (module.exports = {
|
|
126
|
+
Chatter,
|
|
47
127
|
add,
|
|
48
128
|
createRoom
|
|
49
129
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,81 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
|
|
2
|
+
import { io } from "socket.io-client";
|
|
3
|
+
var SERVER_HTTP_URL = "http://localhost:3000";
|
|
4
|
+
var SERVER_WS_URL = "ws://localhost:3000";
|
|
5
|
+
function connectSocket(url) {
|
|
6
|
+
return new Promise((resolve, reject) => {
|
|
7
|
+
const socket = io(url, {
|
|
8
|
+
reconnectionAttempts: 3,
|
|
9
|
+
reconnectionDelay: 5e3
|
|
10
|
+
});
|
|
11
|
+
socket.on("connect", () => resolve(socket));
|
|
12
|
+
socket.on(
|
|
13
|
+
"reconnect_failed",
|
|
14
|
+
(err) => reject(new Error(`Could not connect to Crafter: ${err}`))
|
|
15
|
+
);
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
var Chatter = class {
|
|
19
|
+
constructor(playerID) {
|
|
20
|
+
this.onMessageCallbacks = /* @__PURE__ */ new Map();
|
|
21
|
+
this.playerID = playerID;
|
|
22
|
+
this.socket = null;
|
|
23
|
+
}
|
|
24
|
+
setMessageCallback(sourceID, callback) {
|
|
25
|
+
this.onMessageCallbacks.set(sourceID, callback);
|
|
26
|
+
}
|
|
27
|
+
async connect() {
|
|
28
|
+
this.socket = await connectSocket(SERVER_WS_URL);
|
|
29
|
+
this.socket.on("disconnect", () => {
|
|
30
|
+
this.socket = null;
|
|
31
|
+
});
|
|
32
|
+
this.socket.on("receiveMessage", (msg) => {
|
|
33
|
+
if (msg.messageType == "GROUP") {
|
|
34
|
+
const msgCallback = this.onMessageCallbacks.get(msg.destinationId);
|
|
35
|
+
if (msgCallback) msgCallback(msg);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
async joinRoom(roomID, onMessage) {
|
|
40
|
+
if (this.socket) {
|
|
41
|
+
this.onMessageCallbacks.set(roomID, onMessage);
|
|
42
|
+
return new Promise((resolve, reject) => {
|
|
43
|
+
this.socket.emit(
|
|
44
|
+
"joinRoom",
|
|
45
|
+
roomID,
|
|
46
|
+
(response) => {
|
|
47
|
+
if (response.success) resolve();
|
|
48
|
+
else reject(new Error(response.error || "Failed to join room"));
|
|
49
|
+
}
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
} else {
|
|
53
|
+
throw new Error("Chatter client does not have a valid socket connection");
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
sendRoomMessage(roomID, messageContent) {
|
|
57
|
+
if (!this.socket || this.socket.disconnected) {
|
|
58
|
+
return Promise.reject(
|
|
59
|
+
new Error("Chatter client does not have a valid socket connection")
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
return new Promise((resolve, reject) => {
|
|
63
|
+
this.socket.emit(
|
|
64
|
+
"sendMessage",
|
|
65
|
+
roomID,
|
|
66
|
+
this.playerID,
|
|
67
|
+
messageContent,
|
|
68
|
+
(response) => {
|
|
69
|
+
if (response.success) resolve();
|
|
70
|
+
else
|
|
71
|
+
reject(new Error(response.error || "Failed to send room message"));
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
};
|
|
3
77
|
async function createRoom(data) {
|
|
4
|
-
const response = await fetch(`${
|
|
78
|
+
const response = await fetch(`${SERVER_HTTP_URL}/api/chat/createRoom`, {
|
|
5
79
|
method: "POST",
|
|
6
80
|
headers: {
|
|
7
81
|
"Content-Type": "application/json"
|
|
@@ -9,15 +83,20 @@ async function createRoom(data) {
|
|
|
9
83
|
body: JSON.stringify(data)
|
|
10
84
|
});
|
|
11
85
|
if (!response.ok) {
|
|
12
|
-
const
|
|
13
|
-
throw new Error(`
|
|
86
|
+
const error = await response.json();
|
|
87
|
+
throw new Error(`Failed to create chat room: ${error.msg}`);
|
|
88
|
+
}
|
|
89
|
+
const responseData = await response.json();
|
|
90
|
+
if (!responseData || !responseData.roomId) {
|
|
91
|
+
throw new Error("Failed to create chat room: missing room ID");
|
|
14
92
|
}
|
|
15
|
-
return
|
|
93
|
+
return responseData.roomId;
|
|
16
94
|
}
|
|
17
95
|
function add(a, b) {
|
|
18
96
|
return a + b;
|
|
19
97
|
}
|
|
20
98
|
export {
|
|
99
|
+
Chatter,
|
|
21
100
|
add,
|
|
22
101
|
createRoom
|
|
23
102
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "crafter-kit",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -22,5 +22,8 @@
|
|
|
22
22
|
"prettier": "^3.8.1",
|
|
23
23
|
"tsup": "^8.5.1",
|
|
24
24
|
"typescript": "^5.9.3"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"socket.io-client": "^4.8.3"
|
|
25
28
|
}
|
|
26
29
|
}
|