@saltify/milky-node-sdk 0.1.2 → 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/LICENSE +4 -113
- package/README.md +3 -3
- package/dist/index.d.mts +108 -0
- package/dist/index.mjs +93 -0
- package/package.json +21 -17
- package/dist/index.d.ts +0 -107
- package/dist/index.js +0 -106
package/LICENSE
CHANGED
|
@@ -1,116 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
Copyright 2025 SaltifyDev
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
4
|
|
|
5
|
-
The
|
|
6
|
-
exclusive Copyright and Related Rights (defined below) upon the creator and
|
|
7
|
-
subsequent owner(s) (each and all, an "owner") of an original work of
|
|
8
|
-
authorship and/or a database (each, a "Work").
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
9
6
|
|
|
10
|
-
|
|
11
|
-
purpose of contributing to a commons of creative, cultural and scientific
|
|
12
|
-
works ("Commons") that the public can reliably and without fear of later
|
|
13
|
-
claims of infringement build upon, modify, incorporate in other works, reuse
|
|
14
|
-
and redistribute as freely as possible in any form whatsoever and for any
|
|
15
|
-
purposes, including without limitation commercial purposes. These owners may
|
|
16
|
-
contribute to the Commons to promote the ideal of a free culture and the
|
|
17
|
-
further production of creative, cultural and scientific works, or to gain
|
|
18
|
-
reputation or greater distribution for their Work in part through the use and
|
|
19
|
-
efforts of others.
|
|
20
|
-
|
|
21
|
-
For these and/or other purposes and motivations, and without any expectation
|
|
22
|
-
of additional consideration or compensation, the person associating CC0 with a
|
|
23
|
-
Work (the "Affirmer"), to the extent that he or she is an owner of Copyright
|
|
24
|
-
and Related Rights in the Work, voluntarily elects to apply CC0 to the Work
|
|
25
|
-
and publicly distribute the Work under its terms, with knowledge of his or her
|
|
26
|
-
Copyright and Related Rights in the Work and the meaning and intended legal
|
|
27
|
-
effect of CC0 on those rights.
|
|
28
|
-
|
|
29
|
-
1. Copyright and Related Rights. A Work made available under CC0 may be
|
|
30
|
-
protected by copyright and related or neighboring rights ("Copyright and
|
|
31
|
-
Related Rights"). Copyright and Related Rights include, but are not limited
|
|
32
|
-
to, the following:
|
|
33
|
-
|
|
34
|
-
i. the right to reproduce, adapt, distribute, perform, display, communicate,
|
|
35
|
-
and translate a Work;
|
|
36
|
-
|
|
37
|
-
ii. moral rights retained by the original author(s) and/or performer(s);
|
|
38
|
-
|
|
39
|
-
iii. publicity and privacy rights pertaining to a person's image or likeness
|
|
40
|
-
depicted in a Work;
|
|
41
|
-
|
|
42
|
-
iv. rights protecting against unfair competition in regards to a Work,
|
|
43
|
-
subject to the limitations in paragraph 4(a), below;
|
|
44
|
-
|
|
45
|
-
v. rights protecting the extraction, dissemination, use and reuse of data in
|
|
46
|
-
a Work;
|
|
47
|
-
|
|
48
|
-
vi. database rights (such as those arising under Directive 96/9/EC of the
|
|
49
|
-
European Parliament and of the Council of 11 March 1996 on the legal
|
|
50
|
-
protection of databases, and under any national implementation thereof,
|
|
51
|
-
including any amended or successor version of such directive); and
|
|
52
|
-
|
|
53
|
-
vii. other similar, equivalent or corresponding rights throughout the world
|
|
54
|
-
based on applicable law or treaty, and any national implementations thereof.
|
|
55
|
-
|
|
56
|
-
2. Waiver. To the greatest extent permitted by, but not in contravention of,
|
|
57
|
-
applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
|
|
58
|
-
unconditionally waives, abandons, and surrenders all of Affirmer's Copyright
|
|
59
|
-
and Related Rights and associated claims and causes of action, whether now
|
|
60
|
-
known or unknown (including existing as well as future claims and causes of
|
|
61
|
-
action), in the Work (i) in all territories worldwide, (ii) for the maximum
|
|
62
|
-
duration provided by applicable law or treaty (including future time
|
|
63
|
-
extensions), (iii) in any current or future medium and for any number of
|
|
64
|
-
copies, and (iv) for any purpose whatsoever, including without limitation
|
|
65
|
-
commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes
|
|
66
|
-
the Waiver for the benefit of each member of the public at large and to the
|
|
67
|
-
detriment of Affirmer's heirs and successors, fully intending that such Waiver
|
|
68
|
-
shall not be subject to revocation, rescission, cancellation, termination, or
|
|
69
|
-
any other legal or equitable action to disrupt the quiet enjoyment of the Work
|
|
70
|
-
by the public as contemplated by Affirmer's express Statement of Purpose.
|
|
71
|
-
|
|
72
|
-
3. Public License Fallback. Should any part of the Waiver for any reason be
|
|
73
|
-
judged legally invalid or ineffective under applicable law, then the Waiver
|
|
74
|
-
shall be preserved to the maximum extent permitted taking into account
|
|
75
|
-
Affirmer's express Statement of Purpose. In addition, to the extent the Waiver
|
|
76
|
-
is so judged Affirmer hereby grants to each affected person a royalty-free,
|
|
77
|
-
non transferable, non sublicensable, non exclusive, irrevocable and
|
|
78
|
-
unconditional license to exercise Affirmer's Copyright and Related Rights in
|
|
79
|
-
the Work (i) in all territories worldwide, (ii) for the maximum duration
|
|
80
|
-
provided by applicable law or treaty (including future time extensions), (iii)
|
|
81
|
-
in any current or future medium and for any number of copies, and (iv) for any
|
|
82
|
-
purpose whatsoever, including without limitation commercial, advertising or
|
|
83
|
-
promotional purposes (the "License"). The License shall be deemed effective as
|
|
84
|
-
of the date CC0 was applied by Affirmer to the Work. Should any part of the
|
|
85
|
-
License for any reason be judged legally invalid or ineffective under
|
|
86
|
-
applicable law, such partial invalidity or ineffectiveness shall not
|
|
87
|
-
invalidate the remainder of the License, and in such case Affirmer hereby
|
|
88
|
-
affirms that he or she will not (i) exercise any of his or her remaining
|
|
89
|
-
Copyright and Related Rights in the Work or (ii) assert any associated claims
|
|
90
|
-
and causes of action with respect to the Work, in either case contrary to
|
|
91
|
-
Affirmer's express Statement of Purpose.
|
|
92
|
-
|
|
93
|
-
4. Limitations and Disclaimers.
|
|
94
|
-
|
|
95
|
-
a. No trademark or patent rights held by Affirmer are waived, abandoned,
|
|
96
|
-
surrendered, licensed or otherwise affected by this document.
|
|
97
|
-
|
|
98
|
-
b. Affirmer offers the Work as-is and makes no representations or warranties
|
|
99
|
-
of any kind concerning the Work, express, implied, statutory or otherwise,
|
|
100
|
-
including without limitation warranties of title, merchantability, fitness
|
|
101
|
-
for a particular purpose, non infringement, or the absence of latent or
|
|
102
|
-
other defects, accuracy, or the present or absence of errors, whether or not
|
|
103
|
-
discoverable, all to the greatest extent permissible under applicable law.
|
|
104
|
-
|
|
105
|
-
c. Affirmer disclaims responsibility for clearing rights of other persons
|
|
106
|
-
that may apply to the Work or any use thereof, including without limitation
|
|
107
|
-
any person's Copyright and Related Rights in the Work. Further, Affirmer
|
|
108
|
-
disclaims responsibility for obtaining any necessary consents, permissions
|
|
109
|
-
or other rights required for any use of the Work.
|
|
110
|
-
|
|
111
|
-
d. Affirmer understands and acknowledges that Creative Commons is not a
|
|
112
|
-
party to this document and has no duty or obligation with respect to this
|
|
113
|
-
CC0 or use of the Work.
|
|
114
|
-
|
|
115
|
-
For more information, please see
|
|
116
|
-
<http://creativecommons.org/publicdomain/zero/1.0/>
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
这是 Milky 协议的 Node.js SDK,提供 TypeScript 支持。
|
|
4
4
|
|
|
5
|
-
源码中使用了 Node.js 的 WebSocket API,因此需要 Node
|
|
5
|
+
源码中使用了 Node.js 的 WebSocket API,因此需要 Node 版本高于或等于 22。如果必须在低于 22 的版本中使用,请在启动时加入 `--experimental-websocket` 这一 Flag。
|
|
6
6
|
|
|
7
7
|
## 初始化
|
|
8
8
|
|
|
@@ -16,7 +16,7 @@ const client = new MilkyClient('localhost', 3000, '/', 'your-access-token');
|
|
|
16
16
|
|
|
17
17
|
```typescript
|
|
18
18
|
const response = await client.callApi('get_group_info', {
|
|
19
|
-
group_id: 123456789
|
|
19
|
+
group_id: 123456789,
|
|
20
20
|
});
|
|
21
21
|
```
|
|
22
22
|
|
|
@@ -26,4 +26,4 @@ const response = await client.callApi('get_group_info', {
|
|
|
26
26
|
client.on('message_receive', (event) => {
|
|
27
27
|
console.log('Received event:', event);
|
|
28
28
|
});
|
|
29
|
-
```
|
|
29
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import * as types from "@saltify/milky-types";
|
|
2
|
+
import { Event } from "@saltify/milky-types";
|
|
3
|
+
import z from "zod";
|
|
4
|
+
|
|
5
|
+
//#region src/api.g.d.ts
|
|
6
|
+
interface ApiCollection {
|
|
7
|
+
get_login_info: () => types.GetLoginInfoOutput;
|
|
8
|
+
get_impl_info: () => types.GetImplInfoOutput;
|
|
9
|
+
get_user_profile: (input: z.input<typeof types.GetUserProfileInput>) => types.GetUserProfileOutput;
|
|
10
|
+
get_friend_list: (input: z.input<typeof types.GetFriendListInput>) => types.GetFriendListOutput;
|
|
11
|
+
get_friend_info: (input: z.input<typeof types.GetFriendInfoInput>) => types.GetFriendInfoOutput;
|
|
12
|
+
get_group_list: (input: z.input<typeof types.GetGroupListInput>) => types.GetGroupListOutput;
|
|
13
|
+
get_group_info: (input: z.input<typeof types.GetGroupInfoInput>) => types.GetGroupInfoOutput;
|
|
14
|
+
get_group_member_list: (input: z.input<typeof types.GetGroupMemberListInput>) => types.GetGroupMemberListOutput;
|
|
15
|
+
get_group_member_info: (input: z.input<typeof types.GetGroupMemberInfoInput>) => types.GetGroupMemberInfoOutput;
|
|
16
|
+
get_cookies: (input: z.input<typeof types.GetCookiesInput>) => types.GetCookiesOutput;
|
|
17
|
+
get_csrf_token: () => types.GetCSRFTokenOutput;
|
|
18
|
+
send_private_message: (input: z.input<typeof types.SendPrivateMessageInput>) => types.SendPrivateMessageOutput;
|
|
19
|
+
send_group_message: (input: z.input<typeof types.SendGroupMessageInput>) => types.SendGroupMessageOutput;
|
|
20
|
+
recall_private_message: (input: z.input<typeof types.RecallPrivateMessageInput>) => void;
|
|
21
|
+
recall_group_message: (input: z.input<typeof types.RecallGroupMessageInput>) => void;
|
|
22
|
+
get_message: (input: z.input<typeof types.GetMessageInput>) => types.GetMessageOutput;
|
|
23
|
+
get_history_messages: (input: z.input<typeof types.GetHistoryMessagesInput>) => types.GetHistoryMessagesOutput;
|
|
24
|
+
get_resource_temp_url: (input: z.input<typeof types.GetResourceTempUrlInput>) => types.GetResourceTempUrlOutput;
|
|
25
|
+
get_forwarded_messages: (input: z.input<typeof types.GetForwardedMessagesInput>) => types.GetForwardedMessagesOutput;
|
|
26
|
+
mark_message_as_read: (input: z.input<typeof types.MarkMessageAsReadInput>) => void;
|
|
27
|
+
send_friend_nudge: (input: z.input<typeof types.SendFriendNudgeInput>) => void;
|
|
28
|
+
send_profile_like: (input: z.input<typeof types.SendProfileLikeInput>) => void;
|
|
29
|
+
get_friend_requests: (input: z.input<typeof types.GetFriendRequestsInput>) => types.GetFriendRequestsOutput;
|
|
30
|
+
accept_friend_request: (input: z.input<typeof types.AcceptFriendRequestInput>) => void;
|
|
31
|
+
reject_friend_request: (input: z.input<typeof types.RejectFriendRequestInput>) => void;
|
|
32
|
+
set_group_name: (input: z.input<typeof types.SetGroupNameInput>) => void;
|
|
33
|
+
set_group_avatar: (input: z.input<typeof types.SetGroupAvatarInput>) => void;
|
|
34
|
+
set_group_member_card: (input: z.input<typeof types.SetGroupMemberCardInput>) => void;
|
|
35
|
+
set_group_member_special_title: (input: z.input<typeof types.SetGroupMemberSpecialTitleInput>) => void;
|
|
36
|
+
set_group_member_admin: (input: z.input<typeof types.SetGroupMemberAdminInput>) => void;
|
|
37
|
+
set_group_member_mute: (input: z.input<typeof types.SetGroupMemberMuteInput>) => void;
|
|
38
|
+
set_group_whole_mute: (input: z.input<typeof types.SetGroupWholeMuteInput>) => void;
|
|
39
|
+
kick_group_member: (input: z.input<typeof types.KickGroupMemberInput>) => void;
|
|
40
|
+
get_group_announcements: (input: z.input<typeof types.GetGroupAnnouncementsInput>) => types.GetGroupAnnouncementsOutput;
|
|
41
|
+
send_group_announcement: (input: z.input<typeof types.SendGroupAnnouncementInput>) => void;
|
|
42
|
+
delete_group_announcement: (input: z.input<typeof types.DeleteGroupAnnouncementInput>) => void;
|
|
43
|
+
get_group_essence_messages: (input: z.input<typeof types.GetGroupEssenceMessagesInput>) => types.GetGroupEssenceMessagesOutput;
|
|
44
|
+
set_group_essence_message: (input: z.input<typeof types.SetGroupEssenceMessageInput>) => void;
|
|
45
|
+
quit_group: (input: z.input<typeof types.QuitGroupInput>) => void;
|
|
46
|
+
send_group_message_reaction: (input: z.input<typeof types.SendGroupMessageReactionInput>) => void;
|
|
47
|
+
send_group_nudge: (input: z.input<typeof types.SendGroupNudgeInput>) => void;
|
|
48
|
+
get_group_notifications: (input: z.input<typeof types.GetGroupNotificationsInput>) => types.GetGroupNotificationsOutput;
|
|
49
|
+
accept_group_request: (input: z.input<typeof types.AcceptGroupRequestInput>) => void;
|
|
50
|
+
reject_group_request: (input: z.input<typeof types.RejectGroupRequestInput>) => void;
|
|
51
|
+
accept_group_invitation: (input: z.input<typeof types.AcceptGroupInvitationInput>) => void;
|
|
52
|
+
reject_group_invitation: (input: z.input<typeof types.RejectGroupInvitationInput>) => void;
|
|
53
|
+
upload_private_file: (input: z.input<typeof types.UploadPrivateFileInput>) => types.UploadPrivateFileOutput;
|
|
54
|
+
upload_group_file: (input: z.input<typeof types.UploadGroupFileInput>) => types.UploadGroupFileOutput;
|
|
55
|
+
get_private_file_download_url: (input: z.input<typeof types.GetPrivateFileDownloadUrlInput>) => types.GetPrivateFileDownloadUrlOutput;
|
|
56
|
+
get_group_file_download_url: (input: z.input<typeof types.GetGroupFileDownloadUrlInput>) => types.GetGroupFileDownloadUrlOutput;
|
|
57
|
+
get_group_files: (input: z.input<typeof types.GetGroupFilesInput>) => types.GetGroupFilesOutput;
|
|
58
|
+
move_group_file: (input: z.input<typeof types.MoveGroupFileInput>) => void;
|
|
59
|
+
rename_group_file: (input: z.input<typeof types.RenameGroupFileInput>) => void;
|
|
60
|
+
delete_group_file: (input: z.input<typeof types.DeleteGroupFileInput>) => void;
|
|
61
|
+
create_group_folder: (input: z.input<typeof types.CreateGroupFolderInput>) => types.CreateGroupFolderOutput;
|
|
62
|
+
rename_group_folder: (input: z.input<typeof types.RenameGroupFolderInput>) => void;
|
|
63
|
+
delete_group_folder: (input: z.input<typeof types.DeleteGroupFolderInput>) => void;
|
|
64
|
+
}
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region src/event.d.ts
|
|
67
|
+
type EventCollection = { [K in Event['event_type']]: Extract<Event, {
|
|
68
|
+
event_type: K;
|
|
69
|
+
}> };
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region src/index.d.ts
|
|
72
|
+
declare class MilkyClient {
|
|
73
|
+
private readonly eventEmitter;
|
|
74
|
+
private readonly httpApiUrl;
|
|
75
|
+
private readonly eventUrl;
|
|
76
|
+
private readonly fetchHeader;
|
|
77
|
+
private disposeCore?;
|
|
78
|
+
/**
|
|
79
|
+
* @param authority The authority of the Milky API (value of `new URL('https://example.com:443/some-path').host`)
|
|
80
|
+
* @param basePath The base path for the Milky API
|
|
81
|
+
* @param accessToken The access token for authentication (optional)
|
|
82
|
+
* @param useTLS Whether to use HTTPS and WSS (default: false)
|
|
83
|
+
* @param useSSE Whether to use Server-Sent Events for event streaming (default: false)
|
|
84
|
+
*/
|
|
85
|
+
constructor(authority: string, basePath?: `/${string}/` | '/', accessToken?: string, useTLS?: boolean, useSSE?: boolean);
|
|
86
|
+
/**
|
|
87
|
+
* Call a Milky API method.
|
|
88
|
+
* @param method The API method to call
|
|
89
|
+
* @param input The input parameters for the API method.
|
|
90
|
+
* If the API does not require any input, you can leave it empty.
|
|
91
|
+
*/
|
|
92
|
+
callApi<K$1 extends keyof ApiCollection>(method: K$1, ...input: Parameters<ApiCollection[K$1]>): Promise<ReturnType<ApiCollection[K$1]>>;
|
|
93
|
+
/**
|
|
94
|
+
* Listen for a Milky event.
|
|
95
|
+
* @param eventType The event type to listen for
|
|
96
|
+
* @param listener The listener function to call when the event is emitted
|
|
97
|
+
*/
|
|
98
|
+
onEvent<K$1 extends keyof EventCollection>(eventType: K$1, listener: (data: EventCollection[K$1]) => void): Promise<void>;
|
|
99
|
+
private createSSE;
|
|
100
|
+
private createWebsocket;
|
|
101
|
+
/**
|
|
102
|
+
* Release the WebSocket / Server Sent Event connection.
|
|
103
|
+
*/
|
|
104
|
+
dispose(): void;
|
|
105
|
+
[Symbol.dispose]: () => void;
|
|
106
|
+
}
|
|
107
|
+
//#endregion
|
|
108
|
+
export { MilkyClient };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import EventEmitter from "node:events";
|
|
2
|
+
import { createEventSource } from "eventsource-client";
|
|
3
|
+
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
const combineUrl = (base, path) => {
|
|
6
|
+
return `${base.endsWith("/") ? base.slice(0, -1) : base}/${path.startsWith("/") ? path.slice(1) : path}`;
|
|
7
|
+
};
|
|
8
|
+
var MilkyClient = class {
|
|
9
|
+
eventEmitter;
|
|
10
|
+
httpApiUrl;
|
|
11
|
+
eventUrl;
|
|
12
|
+
fetchHeader;
|
|
13
|
+
disposeCore;
|
|
14
|
+
/**
|
|
15
|
+
* @param authority The authority of the Milky API (value of `new URL('https://example.com:443/some-path').host`)
|
|
16
|
+
* @param basePath The base path for the Milky API
|
|
17
|
+
* @param accessToken The access token for authentication (optional)
|
|
18
|
+
* @param useTLS Whether to use HTTPS and WSS (default: false)
|
|
19
|
+
* @param useSSE Whether to use Server-Sent Events for event streaming (default: false)
|
|
20
|
+
*/
|
|
21
|
+
constructor(authority, basePath, accessToken, useTLS, useSSE) {
|
|
22
|
+
const httpUrlBase = `${useTLS ? "https" : "http"}://${`${authority}${basePath}`}`;
|
|
23
|
+
this.fetchHeader = {};
|
|
24
|
+
if (accessToken) this.fetchHeader["Authorization"] = `Bearer ${accessToken}`;
|
|
25
|
+
this.eventEmitter = new EventEmitter();
|
|
26
|
+
this.httpApiUrl = combineUrl(httpUrlBase, "api");
|
|
27
|
+
this.eventUrl = combineUrl(httpUrlBase, "event");
|
|
28
|
+
if (!useSSE) this.createWebsocket();
|
|
29
|
+
else this.createSSE();
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Call a Milky API method.
|
|
33
|
+
* @param method The API method to call
|
|
34
|
+
* @param input The input parameters for the API method.
|
|
35
|
+
* If the API does not require any input, you can leave it empty.
|
|
36
|
+
*/
|
|
37
|
+
async callApi(method, ...input) {
|
|
38
|
+
const callResult = await (await fetch(combineUrl(this.httpApiUrl, method), {
|
|
39
|
+
method: "POST",
|
|
40
|
+
headers: {
|
|
41
|
+
Accept: "application/json",
|
|
42
|
+
"Content-Type": "application/json",
|
|
43
|
+
...this.fetchHeader
|
|
44
|
+
},
|
|
45
|
+
body: JSON.stringify(input[0] ?? {})
|
|
46
|
+
})).json();
|
|
47
|
+
if (callResult.status === "failed") throw new Error(`call api ${method} failed with retcode ${callResult.retcode}: ${callResult.message}`);
|
|
48
|
+
return callResult.data;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Listen for a Milky event.
|
|
52
|
+
* @param eventType The event type to listen for
|
|
53
|
+
* @param listener The listener function to call when the event is emitted
|
|
54
|
+
*/
|
|
55
|
+
async onEvent(eventType, listener) {
|
|
56
|
+
this.eventEmitter.on(eventType, listener);
|
|
57
|
+
}
|
|
58
|
+
createSSE() {
|
|
59
|
+
const sse = createEventSource({
|
|
60
|
+
url: this.eventUrl,
|
|
61
|
+
headers: {
|
|
62
|
+
Accept: "text/event-stream",
|
|
63
|
+
...this.fetchHeader
|
|
64
|
+
},
|
|
65
|
+
onMessage: ({ event, data }) => {
|
|
66
|
+
if (event !== "milky_event") return;
|
|
67
|
+
const parsedData = JSON.parse(data);
|
|
68
|
+
this.eventEmitter.emit(parsedData.event_type, parsedData);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
sse.connect();
|
|
72
|
+
this.disposeCore = sse.close.bind(sse);
|
|
73
|
+
}
|
|
74
|
+
createWebsocket() {
|
|
75
|
+
if (!globalThis.WebSocket) throw new Error("WebSocket is not supported in this environment.");
|
|
76
|
+
const ws = new WebSocket(this.eventUrl, { headers: { ...this.fetchHeader } });
|
|
77
|
+
ws.addEventListener("message", (event) => {
|
|
78
|
+
const data = JSON.parse(event.data);
|
|
79
|
+
this.eventEmitter.emit(data.event_type, data);
|
|
80
|
+
});
|
|
81
|
+
this.disposeCore = ws.close.bind(ws);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Release the WebSocket / Server Sent Event connection.
|
|
85
|
+
*/
|
|
86
|
+
dispose() {
|
|
87
|
+
this.disposeCore?.();
|
|
88
|
+
}
|
|
89
|
+
[Symbol.dispose] = this.dispose;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
//#endregion
|
|
93
|
+
export { MilkyClient };
|
package/package.json
CHANGED
|
@@ -1,30 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saltify/milky-node-sdk",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
5
|
-
"description": "
|
|
6
|
-
"main": "dist/index.
|
|
7
|
-
"typings": "dist/index.d.
|
|
8
|
-
"files": [
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "",
|
|
6
|
+
"main": "dist/index.mjs",
|
|
7
|
+
"typings": "dist/index.d.mts",
|
|
8
|
+
"files": ["dist"],
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "npm run generate-api && tsdown",
|
|
11
|
+
"generate-api": "tsx ./scripts/generate-api.ts",
|
|
12
|
+
"prepack": "npm run build",
|
|
13
|
+
"prepare": "npm run generate-api"
|
|
14
|
+
},
|
|
11
15
|
"keywords": [],
|
|
12
16
|
"author": "SaltifyDev",
|
|
13
17
|
"repository": {
|
|
14
18
|
"type": "git",
|
|
15
|
-
"url": "git+https://github.com/SaltifyDev/milky.git"
|
|
16
|
-
},
|
|
17
|
-
"license": "CC0-1.0",
|
|
18
|
-
"devDependencies": {
|
|
19
|
-
"@types/node": "^22.17.2"
|
|
19
|
+
"url": "git+https://github.com/SaltifyDev/milky-node-sdk.git"
|
|
20
20
|
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"packageManager": "pnpm@10.26.1",
|
|
21
23
|
"dependencies": {
|
|
22
24
|
"@saltify/milky-types": "^1.0.0",
|
|
23
25
|
"eventsource-client": "^1.2.0",
|
|
24
|
-
"zod": "^4.1
|
|
26
|
+
"zod": "^4.2.1"
|
|
25
27
|
},
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/node": "^22.19.3",
|
|
30
|
+
"tsdown": "^0.18.2",
|
|
31
|
+
"tsx": "^4.21.0",
|
|
32
|
+
"typescript": "^5.9.3"
|
|
29
33
|
}
|
|
30
|
-
}
|
|
34
|
+
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import * as types from '@saltify/milky-types';
|
|
2
|
-
import { Event } from '@saltify/milky-types';
|
|
3
|
-
import z from 'zod';
|
|
4
|
-
|
|
5
|
-
type ApiCollection = {
|
|
6
|
-
get_login_info: () => types.GetLoginInfoOutput;
|
|
7
|
-
get_impl_info: () => types.GetImplInfoOutput;
|
|
8
|
-
get_user_profile: (input: z.input<typeof types.GetUserProfileInput>) => types.GetUserProfileOutput;
|
|
9
|
-
get_friend_list: (input: z.input<typeof types.GetFriendListInput>) => types.GetFriendListOutput;
|
|
10
|
-
get_friend_info: (input: z.input<typeof types.GetFriendInfoInput>) => types.GetFriendInfoOutput;
|
|
11
|
-
get_group_list: (input: z.input<typeof types.GetGroupListInput>) => types.GetGroupListOutput;
|
|
12
|
-
get_group_info: (input: z.input<typeof types.GetGroupInfoInput>) => types.GetGroupInfoOutput;
|
|
13
|
-
get_group_member_list: (input: z.input<typeof types.GetGroupMemberListInput>) => types.GetGroupMemberListOutput;
|
|
14
|
-
get_group_member_info: (input: z.input<typeof types.GetGroupMemberInfoInput>) => types.GetGroupMemberInfoOutput;
|
|
15
|
-
get_cookies: (input: z.input<typeof types.GetCookiesInput>) => types.GetCookiesOutput;
|
|
16
|
-
get_csrf_token: () => types.GetCSRFTokenOutput;
|
|
17
|
-
send_private_message: (input: z.input<typeof types.SendPrivateMessageInput>) => types.SendPrivateMessageOutput;
|
|
18
|
-
send_group_message: (input: z.input<typeof types.SendGroupMessageInput>) => types.SendGroupMessageOutput;
|
|
19
|
-
recall_private_message: (input: z.input<typeof types.RecallPrivateMessageInput>) => void;
|
|
20
|
-
recall_group_message: (input: z.input<typeof types.RecallGroupMessageInput>) => void;
|
|
21
|
-
get_message: (input: z.input<typeof types.GetMessageInput>) => types.GetMessageOutput;
|
|
22
|
-
get_history_messages: (input: z.input<typeof types.GetHistoryMessagesInput>) => types.GetHistoryMessagesOutput;
|
|
23
|
-
get_resource_temp_url: (input: z.input<typeof types.GetResourceTempUrlInput>) => types.GetResourceTempUrlOutput;
|
|
24
|
-
get_forwarded_messages: (input: z.input<typeof types.GetForwardedMessagesInput>) => types.GetForwardedMessagesOutput;
|
|
25
|
-
mark_message_as_read: (input: z.input<typeof types.MarkMessageAsReadInput>) => void;
|
|
26
|
-
send_friend_nudge: (input: z.input<typeof types.SendFriendNudgeInput>) => void;
|
|
27
|
-
send_profile_like: (input: z.input<typeof types.SendProfileLikeInput>) => void;
|
|
28
|
-
get_friend_requests: (input: z.input<typeof types.GetFriendRequestsInput>) => types.GetFriendRequestsOutput;
|
|
29
|
-
accept_friend_request: (input: z.input<typeof types.AcceptFriendRequestInput>) => void;
|
|
30
|
-
reject_friend_request: (input: z.input<typeof types.RejectFriendRequestInput>) => void;
|
|
31
|
-
set_group_name: (input: z.input<typeof types.SetGroupNameInput>) => void;
|
|
32
|
-
set_group_avatar: (input: z.input<typeof types.SetGroupAvatarInput>) => void;
|
|
33
|
-
set_group_member_card: (input: z.input<typeof types.SetGroupMemberCardInput>) => void;
|
|
34
|
-
set_group_member_special_title: (input: z.input<typeof types.SetGroupMemberSpecialTitleInput>) => void;
|
|
35
|
-
set_group_member_admin: (input: z.input<typeof types.SetGroupMemberAdminInput>) => void;
|
|
36
|
-
set_group_member_mute: (input: z.input<typeof types.SetGroupMemberMuteInput>) => void;
|
|
37
|
-
set_group_whole_mute: (input: z.input<typeof types.SetGroupWholeMuteInput>) => void;
|
|
38
|
-
kick_group_member: (input: z.input<typeof types.KickGroupMemberInput>) => void;
|
|
39
|
-
get_group_announcements: (input: z.input<typeof types.GetGroupAnnouncementsInput>) => types.GetGroupAnnouncementsOutput;
|
|
40
|
-
send_group_announcement: (input: z.input<typeof types.SendGroupAnnouncementInput>) => void;
|
|
41
|
-
delete_group_announcement: (input: z.input<typeof types.DeleteGroupAnnouncementInput>) => void;
|
|
42
|
-
get_group_essence_messages: (input: z.input<typeof types.GetGroupEssenceMessagesInput>) => types.GetGroupEssenceMessagesOutput;
|
|
43
|
-
set_group_essence_message: (input: z.input<typeof types.SetGroupEssenceMessageInput>) => void;
|
|
44
|
-
quit_group: (input: z.input<typeof types.QuitGroupInput>) => void;
|
|
45
|
-
send_group_message_reaction: (input: z.input<typeof types.SendGroupMessageReactionInput>) => void;
|
|
46
|
-
send_group_nudge: (input: z.input<typeof types.SendGroupNudgeInput>) => void;
|
|
47
|
-
get_group_notifications: (input: z.input<typeof types.GetGroupNotificationsInput>) => types.GetGroupNotificationsOutput;
|
|
48
|
-
accept_group_request: (input: z.input<typeof types.AcceptGroupRequestInput>) => void;
|
|
49
|
-
reject_group_request: (input: z.input<typeof types.RejectGroupRequestInput>) => void;
|
|
50
|
-
accept_group_invitation: (input: z.input<typeof types.AcceptGroupInvitationInput>) => void;
|
|
51
|
-
reject_group_invitation: (input: z.input<typeof types.RejectGroupInvitationInput>) => void;
|
|
52
|
-
upload_private_file: (input: z.input<typeof types.UploadPrivateFileInput>) => types.UploadPrivateFileOutput;
|
|
53
|
-
upload_group_file: (input: z.input<typeof types.UploadGroupFileInput>) => types.UploadGroupFileOutput;
|
|
54
|
-
get_private_file_download_url: (input: z.input<typeof types.GetPrivateFileDownloadUrlInput>) => types.GetPrivateFileDownloadUrlOutput;
|
|
55
|
-
get_group_file_download_url: (input: z.input<typeof types.GetGroupFileDownloadUrlInput>) => types.GetGroupFileDownloadUrlOutput;
|
|
56
|
-
get_group_files: (input: z.input<typeof types.GetGroupFilesInput>) => types.GetGroupFilesOutput;
|
|
57
|
-
move_group_file: (input: z.input<typeof types.MoveGroupFileInput>) => void;
|
|
58
|
-
rename_group_file: (input: z.input<typeof types.RenameGroupFileInput>) => void;
|
|
59
|
-
delete_group_file: (input: z.input<typeof types.DeleteGroupFileInput>) => void;
|
|
60
|
-
create_group_folder: (input: z.input<typeof types.CreateGroupFolderInput>) => types.CreateGroupFolderOutput;
|
|
61
|
-
rename_group_folder: (input: z.input<typeof types.RenameGroupFolderInput>) => void;
|
|
62
|
-
delete_group_folder: (input: z.input<typeof types.DeleteGroupFolderInput>) => void;
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
type EventCollection = {
|
|
66
|
-
[K in Event['event_type']]: Extract<Event, {
|
|
67
|
-
event_type: K;
|
|
68
|
-
}>;
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
declare class MilkyClient {
|
|
72
|
-
private readonly eventEmitter;
|
|
73
|
-
private readonly httpApiUrl;
|
|
74
|
-
private readonly eventUrl;
|
|
75
|
-
private readonly fetchHeader;
|
|
76
|
-
private disposeCore?;
|
|
77
|
-
/**
|
|
78
|
-
* @param authority The authority of the Milky API (value of `new URL('https://example.com:443/some-path').host`)
|
|
79
|
-
* @param basePath The base path for the Milky API
|
|
80
|
-
* @param accessToken The access token for authentication (optional)
|
|
81
|
-
* @param useTLS Whether to use HTTPS and WSS (default: false)
|
|
82
|
-
* @param useSSE Whether to use Server-Sent Events for event streaming (default: false)
|
|
83
|
-
*/
|
|
84
|
-
constructor(authority: string, basePath?: `/${string}/` | '/', accessToken?: string, useTLS?: boolean, useSSE?: boolean);
|
|
85
|
-
/**
|
|
86
|
-
* Call a Milky API method.
|
|
87
|
-
* @param method The API method to call
|
|
88
|
-
* @param input The input parameters for the API method.
|
|
89
|
-
* If the API does not require any input, you can leave it empty.
|
|
90
|
-
*/
|
|
91
|
-
callApi<K extends keyof ApiCollection>(method: K, ...input: Parameters<ApiCollection[K]>): Promise<ReturnType<ApiCollection[K]>>;
|
|
92
|
-
/**
|
|
93
|
-
* Listen for a Milky event.
|
|
94
|
-
* @param eventType The event type to listen for
|
|
95
|
-
* @param listener The listener function to call when the event is emitted
|
|
96
|
-
*/
|
|
97
|
-
onEvent<K extends keyof EventCollection>(eventType: K, listener: (data: EventCollection[K]) => void): Promise<void>;
|
|
98
|
-
private createSSE;
|
|
99
|
-
private createWebsocket;
|
|
100
|
-
/**
|
|
101
|
-
* Release the WebSocket / Server Sent Event connection.
|
|
102
|
-
*/
|
|
103
|
-
dispose(): void;
|
|
104
|
-
[Symbol.dispose]: () => void;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export { MilkyClient };
|
package/dist/index.js
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
import EventEmitter from "events";
|
|
3
|
-
import { createEventSource } from "eventsource-client";
|
|
4
|
-
var combineUrl = (base, path) => {
|
|
5
|
-
const baseUrl = base.endsWith("/") ? base.slice(0, -1) : base;
|
|
6
|
-
const pathUrl = path.startsWith("/") ? path.slice(1) : path;
|
|
7
|
-
return `${baseUrl}/${pathUrl}`;
|
|
8
|
-
};
|
|
9
|
-
var MilkyClient = class {
|
|
10
|
-
eventEmitter;
|
|
11
|
-
httpApiUrl;
|
|
12
|
-
eventUrl;
|
|
13
|
-
fetchHeader;
|
|
14
|
-
disposeCore;
|
|
15
|
-
/**
|
|
16
|
-
* @param authority The authority of the Milky API (value of `new URL('https://example.com:443/some-path').host`)
|
|
17
|
-
* @param basePath The base path for the Milky API
|
|
18
|
-
* @param accessToken The access token for authentication (optional)
|
|
19
|
-
* @param useTLS Whether to use HTTPS and WSS (default: false)
|
|
20
|
-
* @param useSSE Whether to use Server-Sent Events for event streaming (default: false)
|
|
21
|
-
*/
|
|
22
|
-
constructor(authority, basePath, accessToken, useTLS, useSSE) {
|
|
23
|
-
const httpProtocol = useTLS ? "https" : "http";
|
|
24
|
-
const urlFragment = `${authority}${basePath}`;
|
|
25
|
-
const httpUrlBase = `${httpProtocol}://${urlFragment}`;
|
|
26
|
-
this.fetchHeader = {};
|
|
27
|
-
if (accessToken) this.fetchHeader["Authorization"] = `Bearer ${accessToken}`;
|
|
28
|
-
this.eventEmitter = new EventEmitter();
|
|
29
|
-
this.httpApiUrl = combineUrl(httpUrlBase, "api");
|
|
30
|
-
this.eventUrl = combineUrl(httpUrlBase, "event");
|
|
31
|
-
if (!useSSE) {
|
|
32
|
-
this.createWebsocket();
|
|
33
|
-
} else {
|
|
34
|
-
this.createSSE();
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Call a Milky API method.
|
|
39
|
-
* @param method The API method to call
|
|
40
|
-
* @param input The input parameters for the API method.
|
|
41
|
-
* If the API does not require any input, you can leave it empty.
|
|
42
|
-
*/
|
|
43
|
-
async callApi(method, ...input) {
|
|
44
|
-
const response = await fetch(combineUrl(this.httpApiUrl, method), {
|
|
45
|
-
method: "POST",
|
|
46
|
-
headers: {
|
|
47
|
-
Accept: "application/json",
|
|
48
|
-
"Content-Type": "application/json",
|
|
49
|
-
...this.fetchHeader
|
|
50
|
-
},
|
|
51
|
-
body: JSON.stringify(input[0] ?? {})
|
|
52
|
-
});
|
|
53
|
-
const callResult = await response.json();
|
|
54
|
-
if (callResult.status === "failed") {
|
|
55
|
-
throw new Error(`call api ${method} failed with retcode ${callResult.retcode}: ${callResult.message}`);
|
|
56
|
-
}
|
|
57
|
-
return callResult.data;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Listen for a Milky event.
|
|
61
|
-
* @param eventType The event type to listen for
|
|
62
|
-
* @param listener The listener function to call when the event is emitted
|
|
63
|
-
*/
|
|
64
|
-
async onEvent(eventType, listener) {
|
|
65
|
-
this.eventEmitter.on(eventType, listener);
|
|
66
|
-
}
|
|
67
|
-
createSSE() {
|
|
68
|
-
const sse = createEventSource({
|
|
69
|
-
url: this.eventUrl,
|
|
70
|
-
headers: {
|
|
71
|
-
Accept: "text/event-stream",
|
|
72
|
-
...this.fetchHeader
|
|
73
|
-
},
|
|
74
|
-
onMessage: ({ event, data }) => {
|
|
75
|
-
if (event !== "milky_event") return;
|
|
76
|
-
const parsedData = JSON.parse(data);
|
|
77
|
-
this.eventEmitter.emit(parsedData.event_type, parsedData);
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
sse.connect();
|
|
81
|
-
this.disposeCore = sse.close.bind(sse);
|
|
82
|
-
}
|
|
83
|
-
createWebsocket() {
|
|
84
|
-
if (!globalThis.WebSocket) throw new Error("WebSocket is not supported in this environment.");
|
|
85
|
-
const ws = new WebSocket(this.eventUrl, {
|
|
86
|
-
headers: {
|
|
87
|
-
...this.fetchHeader
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
ws.addEventListener("message", (event) => {
|
|
91
|
-
const data = JSON.parse(event.data);
|
|
92
|
-
this.eventEmitter.emit(data.event_type, data);
|
|
93
|
-
});
|
|
94
|
-
this.disposeCore = ws.close.bind(ws);
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Release the WebSocket / Server Sent Event connection.
|
|
98
|
-
*/
|
|
99
|
-
dispose() {
|
|
100
|
-
this.disposeCore?.();
|
|
101
|
-
}
|
|
102
|
-
[Symbol.dispose] = this.dispose;
|
|
103
|
-
};
|
|
104
|
-
export {
|
|
105
|
-
MilkyClient
|
|
106
|
-
};
|