diff --git a/package.json b/package.json index bc0f575..7871a1f 100644 --- a/package.json +++ b/package.json @@ -2,20 +2,17 @@ "name": "amino.ts", "description": "Unofficial client for working with rest-api aminoapps", "version": "1.0.0", - "homepage": "https://github.com/osm1um/amino.ts/#readme", - "author": "osm1um", "main": "build/index.js", - "scripts": { "build": "tsc --project tsconfig.json", "start": "node .", "test": "node . --requests-debug" }, - "dependencies": { "@types/node": "^13.11.0", + "node-fetch": "^2.6.0", "sync-request": "^6.1.0", "typescript": "^3.8.3", "ws": "^7.2.3" diff --git a/src/components/APIEndpoint.ts b/src/components/APIEndpoint.ts new file mode 100644 index 0000000..352a142 --- /dev/null +++ b/src/components/APIEndpoint.ts @@ -0,0 +1,63 @@ +import { blog_type, thread_type, thread_sort } from "../components/community/community" +/* +*Static object that can build endpoint +*/ +export class APIEndpoint { + //[prefix] + static Prefix: string = "http://service.narvii.com/api/v1"; + // [auth] + static Login: string = APIEndpoint.Prefix + "/g/s/auth/login"; + static JoiniedCommunities: string = APIEndpoint.Prefix + `/g/s/community/joined` + + + // [COMPLILE ENDPOINTS] + static CompileGetBlog(blogId: string, communityId: number): string { + return APIEndpoint.Prefix + `/x${communityId}/s/blog/${blogId}`; + } + static CompileGetComent(comentId: string, blogId: string): string { + return APIEndpoint.Prefix + `/x${comentId}/s/blog/${blogId}/comment/${comentId}`; + } + static CompileGetComents(comentId: string, communityId: number, start: number, size: number): string { + return APIEndpoint.CompileGetBlog(comentId, communityId) + `/comment?start=${start}&size=${size}`; + } + + static CompileProfile(id: number, userId: string): string { + return APIEndpoint.Prefix + `/x${id}/s/user-profile/${userId}`; + } + static CompileGetOnlineMembers(communityId: number, start: number, size: number): string { + return APIEndpoint.Prefix + `/x${communityId}/s/live-layer?topic=ndtopic%3Ax${communityId}%3Aonline-members&start=${start}&size=${size}`; + } + static CompileGetBlogs(communityId: number, type: blog_type, start: number, size: number): string { + return APIEndpoint.Prefix + `/x${communityId}/s/feed/${type}?start=${start}&size=${size}`; + } + static CompileGetThreads(communityId: number, type: thread_type, sort: thread_sort, start: number, size: number): string { + return APIEndpoint.Prefix + `/x${communityId}/s/chat/thread?type=${type}&filterType=${sort}&start=${start}&size=${size}`; + } + static CompileGetCommunity(communityId: number): string { + return APIEndpoint.Prefix + `/g/s-x${communityId}/community/info`; + } + static CompileCreateThread(communityId: number): string { + return APIEndpoint.Prefix + `/x${communityId}/s/chat/thread`; + } + static CompileGetRecentBlogs(id: string, communityId: number, start: number, size: number): string { + return APIEndpoint.Prefix + `/x${communityId}/s/blog?type=user&q=${id}&start=${start}&size=${size}`; + } + static CompileGetMember(id: string, communityId: number): string { + return APIEndpoint.Prefix + `/x${communityId}/s/user-profile/${id}?action=visit`; + } + static CompileMessageWithId(messageId: string, threadId: any, communityId: number): string { + return APIEndpoint.CompileMessage(threadId, communityId) + `/${messageId}`; + } + static CompileMessage(threadId: any, communityId: number): string { + return APIEndpoint.Prefix + `/x${communityId}/s/chat/thread/${threadId}/message`; + } + static CompileGetMessageList(threadId: any, communityId: number, count: number): string { + return APIEndpoint.Prefix + `/x${communityId}/s/chat/thread/${threadId}/message?v=2&pagingType=t&size=${count}`; + } + static CompileThread(threadId: any, communityId: number): string { + return APIEndpoint.Prefix + `/x${communityId}/s/chat/thread/${threadId}`; + } + static CompileThreadWithMember(threadId: any, communityId: number, memberID: string): string { + return APIEndpoint.CompileThread(threadId, communityId) + `/member/${memberID}`; + } +} \ No newline at end of file diff --git a/src/components/blog/blog.ts b/src/components/blog/blog.ts index 3fc0069..1f4814a 100644 --- a/src/components/blog/blog.ts +++ b/src/components/blog/blog.ts @@ -3,9 +3,9 @@ import AminoClient, { IAminoStorage, AminoMember, AminoCommunity, - IAminoCommentStorage + AminoCommentStorage } from "./../../index" - +import {APIEndpoint} from "../APIEndpoint" /** * Class for working with blogs */ @@ -43,21 +43,22 @@ export class AminoBlog { * @param {number} [start] start position * @param {number} [size] count by start */ - public get_comments(start: number = 1, size: number = 10): IAminoCommentStorage { - let response = request("GET", `https://service.narvii.com/api/v1/x${this.community.id}/s/blog/${this.id}/comment?start=${start}&size=${size}`, { + public get_comments(start: number = 1, size: number = 10): AminoCommentStorage { + + let response = request("GET", APIEndpoint.CompileGetComents(this.id,this.community.id,start,size), { "headers": { "NDCAUTH": "sid=" + this.client.session } }); - return new IAminoCommentStorage(this.client, this.community, this, response.commentList); + return new AminoCommentStorage(this.client, this.community, this, response.commentList); } /** * Method for updating the structure, by re-requesting information from the server */ public refresh(): AminoBlog { - let response = request("GET", `https://service.narvii.com/api/v1/x${this.community.id}/s/blog/${this.id}`, { + let response = request("GET", APIEndpoint.CompileGetBlog(this.id,this.community.id), { "headers": { "NDCAUTH": "sid=" + this.client.session } @@ -95,9 +96,9 @@ export class AminoBlog { /** * Class for storing blog objects */ -export class IAminoBlogStorage extends IAminoStorage { +export class AminoBlogStorage extends IAminoStorage { constructor(client: AminoClient, community: AminoCommunity, array?: any) { - super(client, IAminoBlogStorage.prototype); + super(client, AminoBlogStorage.prototype); if (array) { let members: AminoMember[] = community.cache.members.get(); array.forEach(struct => { diff --git a/src/components/comment/comment.ts b/src/components/comment/comment.ts index d86e976..d1dc572 100644 --- a/src/components/comment/comment.ts +++ b/src/components/comment/comment.ts @@ -5,7 +5,7 @@ import AminoClient, { AminoCommunity, AminoBlog } from "./../../index" - +import {APIEndpoint} from "../APIEndpoint" /** * Class for working with comments */ @@ -39,7 +39,7 @@ export class AminoComment { * Method for updating the structure, by re-requesting information from the server */ public refresh(): AminoComment { - let response = request("GET", `https://service.narvii.com/api/v1/x${this.id}/s/blog/${this.blog.id}/comment/${this.id}`, { + let response = request("GET", APIEndpoint.CompileGetComent(this.id,this.blog.id), { "headers": { "NDCAUTH": "sid=" + this.client.session } @@ -66,9 +66,9 @@ export class AminoComment { /** * Class for storing comment objects */ -export class IAminoCommentStorage extends IAminoStorage { +export class AminoCommentStorage extends IAminoStorage { constructor(client: AminoClient, community: AminoCommunity, blog: AminoBlog, array?: any) { - super(client, IAminoCommentStorage.prototype); + super(client, AminoCommentStorage.prototype); if (array) { let members: AminoMember[] = community.cache.members.get(); array.forEach(struct => { diff --git a/src/components/community/community.ts b/src/components/community/community.ts index 260c6c0..a695cea 100644 --- a/src/components/community/community.ts +++ b/src/components/community/community.ts @@ -3,15 +3,16 @@ import AminoClient, { IAminoCache, IAminoStorage, AminoMember, - IAminoMemberStorage, + AminoMemberStorage, AminoThread, - IAminoThreadStorage, - IAminoBlogStorage + AminoThreadStorage, + AminoBlogStorage, + requestAsync } from "./../../index" - -declare type blog_type = ('featured-more' | 'featured' | 'blog-all'); -declare type thread_sort = ('recommended' | 'popular' | 'latest'); -declare type thread_type = ('joined-me' | 'public-all'); +import { APIEndpoint } from "../APIEndpoint" +export declare type blog_type = ('featured-more' | 'featured' | 'blog-all'); +export declare type thread_sort = ('recommended' | 'popular' | 'latest'); +export declare type thread_type = ('joined-me' | 'public-all'); /** * Class for working with communities @@ -61,7 +62,7 @@ export class AminoCommunity { * @param {string} [nickname] string */ public set_nickname(nickname: string) { - let response = request("POST", `https://service.narvii.com/api/v1/x${this.id}/s/user-profile/${this.me.id}`, { + let response = request("POST", APIEndpoint.CompileProfile(this.id,this.me.id), { "headers": { "NDCAUTH": "sid=" + this.client.session }, @@ -77,8 +78,8 @@ export class AminoCommunity { * Set account description * @param {string} [description] string */ - public set_description(description: string) { - let response = request("POST", `https://service.narvii.com/api/v1/x${this.id}/s/user-profile/${this.me.id}`, { + public async set_description(description: string) : Promise { + return await requestAsync("POST", APIEndpoint.CompileProfile(this.id,this.me.id), { "headers": { "NDCAUTH": "sid=" + this.client.session }, @@ -95,14 +96,14 @@ export class AminoCommunity { * @param {number} [start] pointer to the starting index to read the list * @param {number} [size] number of records to read */ - public get_online_members(start: number = 0, size: number = 10): IAminoMemberStorage { - let response = request("GET", `https://service.narvii.com/api/v1/x${this.id}/s/live-layer?topic=ndtopic%3Ax${this.id}%3Aonline-members&start=${start}&size=${size}`, { + public get_online_members(start: number = 0, size: number = 10): AminoMemberStorage { + let response = request("GET", APIEndpoint.CompileGetOnlineMembers(this.id,start,size), { "headers": { "NDCAUTH": "sid=" + this.client.session } }); - return new IAminoMemberStorage(this.client, this, response.userProfileList); + return new AminoMemberStorage(this.client, this, response.userProfileList); } /** @@ -111,14 +112,15 @@ export class AminoCommunity { * @param {number} [start] start position * @param {number} [size] number of blogs to read */ - public get_blogs(type: blog_type, start: number = 1, size: number = 10): IAminoBlogStorage { - let response = request("GET", `https://service.narvii.com/api/v1/x${this.id}/s/feed/${type}?start=${start}&size=${size}`, { + public get_blogs(type: blog_type, start: number = 1, size: number = 10): AminoBlogStorage { + + let response = request("GET", APIEndpoint.CompileGetBlogs(this.id,type,start,size), { "headers": { "NDCAUTH": "sid=" + this.client.session } }); - return new IAminoBlogStorage(this.client, this, response.blogList); + return new AminoBlogStorage(this.client, this, response.blogList); } /** @@ -128,22 +130,23 @@ export class AminoCommunity { * @param {number} [size] number of records to read * @param {thread_sort} [sort] sorting type */ - public get_threads(type: thread_type, sort: thread_sort = "latest", start: number = 1, size: number = 10): IAminoThreadStorage { - let response = request("GET", `https://service.narvii.com/api/v1/x${this.id}/s/chat/thread?type=${type}&filterType=${sort}&start=${start}&size=${size}`, { + public get_threads(type: thread_type, sort: thread_sort = "latest", start: number = 1, size: number = 10): AminoThreadStorage { + let response = request("GET", APIEndpoint.CompileGetThreads(this.id,type,sort,start,size), { "headers": { "NDCAUTH": "sid=" + this.client.session, "NDCDEVICEID": this.client.device } }); - return new IAminoThreadStorage(this.client, this, response.threadList) + return new AminoThreadStorage(this.client, this, response.threadList) } /** * Method for updating the structure, by re-requesting information from the server */ public refresh(): AminoCommunity { - let response = request("GET", `https://service.narvii.com/api/v1/g/s-x${this.id}/community/info`, { + + let response = request("GET", APIEndpoint.CompileGetCommunity(this.id), { "headers": { "NDCAUTH": "sid=" + this.client.session } @@ -181,10 +184,10 @@ export class AminoCommunity { /** * Class for storing community objects */ -export class IAminoCommunityStorage extends IAminoStorage { +export class AminoCommunityStorage extends IAminoStorage { constructor(client: AminoClient) { - super(client, IAminoCommunityStorage.prototype); - request("GET", `https://service.narvii.com/api/v1/g/s/community/joined`, { + super(client, AminoCommunityStorage.prototype); + request("GET", APIEndpoint.JoiniedCommunities, { "headers": { "NDCAUTH": "sid=" + this.client.session } @@ -197,7 +200,7 @@ export class IAminoCommunityStorage extends IAminoStorage { * Call methods to update in structure objects */ public refresh() { - request("GET", `https://service.narvii.com/api/v1/g/s/community/joined`, { + request("GET", APIEndpoint.JoiniedCommunities, { "headers": { "NDCAUTH": "sid=" + this.client.session } diff --git a/src/components/member/member.ts b/src/components/member/member.ts index 5854f74..e8acf05 100644 --- a/src/components/member/member.ts +++ b/src/components/member/member.ts @@ -3,9 +3,9 @@ import AminoClient, { IAminoStorage, AminoThread, AminoCommunity, - IAminoBlogStorage + AminoBlogStorage } from "./../../index" - +import { APIEndpoint } from "../APIEndpoint" /** * Class for working with members */ @@ -46,7 +46,7 @@ export class AminoMember { * @param {string} [initial_message] initial message for member */ public create_thread(initial_message: string): AminoThread { - let response = request("POST", `https://service.narvii.com/api/v1/x${this.community.id}/s/chat/thread`, { + let response = request("POST", APIEndpoint.CompileCreateThread(this.community.id), { "headers": { "NDCAUTH": "sid=" + this.client.session }, @@ -69,21 +69,22 @@ export class AminoMember { * @param {number} [start] start position * @param {number} [size] number of blogs to read */ - public get_recent_blogs(start: number = 0, size: number = 10): IAminoBlogStorage { - let response = request("GET", `https://service.narvii.com/api/v1/x${this.community.id}/s/blog?type=user&q=${this.id}&start=${start}&size=${size}`, { + public get_recent_blogs(start: number = 0, size: number = 10): AminoBlogStorage { + + let response = request("GET", APIEndpoint.CompileGetRecentBlogs(this.id,this.community.id,start,size), { "headers": { "NDCAUTH": "sid=" + this.client.session } }); - return new IAminoBlogStorage(this.client, this.community, response.blogList); + return new AminoBlogStorage(this.client, this.community, response.blogList); } /** * Method for updating the structure, by re-requesting information from the server */ public refresh(): AminoMember { - let response = request("GET", `https://service.narvii.com/api/v1/x${this.community.id}/s/user-profile/${this.id}?action=visit`, { + let response = request("GET", APIEndpoint.CompileGetMember(this.id,this.community.id), { "headers": { "NDCAUTH": "sid=" + this.client.session } @@ -118,9 +119,9 @@ export class AminoMember { /** * Class for storing members objects */ -export class IAminoMemberStorage extends IAminoStorage { +export class AminoMemberStorage extends IAminoStorage { constructor(client: AminoClient, community: AminoCommunity, array?: any) { - super(client, IAminoMemberStorage.prototype); + super(client, AminoMemberStorage.prototype); if (array) { let members: AminoMember[] = community.cache.members.get(); array.forEach(struct => { diff --git a/src/components/message/message.ts b/src/components/message/message.ts index 0738334..76a4deb 100644 --- a/src/components/message/message.ts +++ b/src/components/message/message.ts @@ -5,7 +5,7 @@ import AminoClient, { AminoThread, AminoCommunity } from "./../../index" - +import { APIEndpoint } from "../APIEndpoint" export enum message_type { COMMON = 0, INVITATION = 103, @@ -51,7 +51,7 @@ export class AminoMessage { * @param {string} [content] text to be sent */ public reply(content: string): AminoMessage { - let response = request("POST", `https://service.narvii.com/api/v1/x${this.community.id}/s/chat/thread/${this.thread.id}/message`, { + let response = request("POST", APIEndpoint.CompileMessage(this.thread.id,this.community.id), { "headers": { "NDCAUTH": "sid=" + this.client.session }, @@ -72,7 +72,7 @@ export class AminoMessage { * Method for calling the delete message procedure */ public delete(): void { - let response = request("DELETE", `https://service.narvii.com/api/v1/x${this.community.id}/s/chat/thread/${this.thread.id}/message/${this.id}`, { + let response = request("DELETE", APIEndpoint.CompileMessageWithId(this.id,this.thread.id,this.community.id), { "headers": { "NDCAUTH": "sid=" + this.client.session } @@ -83,7 +83,7 @@ export class AminoMessage { * Method for updating the structure, by re-requesting information from the server */ public refresh(): AminoMessage { - let response = request("GET", `https://service.narvii.com/api/v1/x${this.community.id}/s/chat/thread/${this.thread.id}/message/${this.id}`, { + let response = request("GET", APIEndpoint.CompileMessageWithId(this.id,this.thread.id,this.community.id), { "headers": { "NDCAUTH": "sid=" + this.client.session } @@ -120,9 +120,9 @@ export class AminoMessage { /** * Class for storing messages objects */ -export class IAminoMessageStorage extends IAminoStorage { +export class AminoMessageStorage extends IAminoStorage { constructor(client: AminoClient, community: AminoCommunity, array?: any) { - super(client, IAminoMessageStorage.prototype); + super(client, AminoMessageStorage.prototype); if (array) { let members: AminoMember[] = community.cache.members.get(); let threads: AminoThread[] = community.cache.threads.get(); diff --git a/src/components/thread/thread.ts b/src/components/thread/thread.ts index 65d4cb2..b99cc30 100644 --- a/src/components/thread/thread.ts +++ b/src/components/thread/thread.ts @@ -2,13 +2,12 @@ import AminoClient, { request, IAminoStorage, AminoMember, - IAminoMessageStorage, + AminoMessageStorage, AminoCommunity, - AminoMessage + AminoMessage, + APIEndpoint } from "./../../index" -import * as fs from "fs"; - declare type image_type = ('image/png' | 'image/jpg'); export enum thread_type { @@ -55,14 +54,15 @@ export class AminoThread { * Method for receiving thread messages * @param {number} [count] number of messages */ - public get_message_list(count: number = 10): IAminoMessageStorage { - let response = request("GET", `https://service.narvii.com/api/v1/x${this.community.id}/s/chat/thread/${this.id}/message?v=2&pagingType=t&size=${count}`, { + public get_message_list(count: number = 10): AminoMessageStorage { + + let response = request("GET", APIEndpoint.CompileGetMessageList(this.id,this.community.id,count), { "headers": { "NDCAUTH": "sid=" + this.client.session } }); - return new IAminoMessageStorage(this.client, this.community, response.messageList); + return new AminoMessageStorage(this.client, this.community, response.messageList); } /** @@ -71,7 +71,7 @@ export class AminoThread { * @param {string} [image] path to the image */ public send_message(content: string): AminoMessage { - let response = request("POST", `https://service.narvii.com/api/v1/x${this.community.id}/s/chat/thread/${this.id}/message`, { + let response = request("POST", APIEndpoint.CompileMessage(this.id,this.community.id), { "headers": { "NDCAUTH": "sid=" + this.client.session }, @@ -97,7 +97,7 @@ export class AminoThread { type: image_type, link: string }): AminoMessage { - let response = request("POST", `https://service.narvii.com/api/v1/x${this.community.id}/s/chat/thread/${this.id}/message`, { + let response = request("POST", APIEndpoint.CompileMessage(this.id,this.community.id), { "headers": { "NDCAUTH": "sid=" + this.client.session }, @@ -128,7 +128,7 @@ export class AminoThread { * @param {image_type} [type] image type */ public send_image(image: Buffer, type: image_type): AminoMessage { - let response = request("POST", `https://service.narvii.com/api/v1/x${this.community.id}/s/chat/thread/${this.id}/message`, { + let response = request("POST", APIEndpoint.CompileMessage(this.id,this.community.id), { "headers": { "NDCAUTH": "sid=" + this.client.session }, @@ -154,7 +154,7 @@ export class AminoThread { * @param {string} [audio] path to audio file */ public send_audio(audio: Buffer): AminoMessage { - let response = request("POST", `https://service.narvii.com/api/v1/x${this.community.id}/s/chat/thread/${this.id}/message`, { + let response = request("POST", APIEndpoint.CompileMessage(this.id,this.community.id), { "headers": { "NDCAUTH": "sid=" + this.client.session }, @@ -174,10 +174,10 @@ export class AminoThread { } /** - * Method for leaving from thread + * Method for join to thread */ public join(): void { - let response = request("POST", ` https://service.narvii.com/api/v1/x${this.community.id}/s/chat/thread/${this.id}/member/${this.community.me.id}`, { + let response = request("POST", APIEndpoint.CompileThreadWithMember(this.id,this.community.id,this.community.me.id), { "headers": { "NDCAUTH": "sid=" + this.client.session } @@ -188,7 +188,8 @@ export class AminoThread { * Method for leaving from thread */ public leave(): void { - let response = request("DELETE", ` https://service.narvii.com/api/v1/x${this.community.id}/s/chat/thread/${this.id}/member/${this.community.me.id}`, { + + let response = request("DELETE", APIEndpoint.CompileThreadWithMember(this.id,this.community.id,this.community.me.id), { "headers": { "NDCAUTH": "sid=" + this.client.session } @@ -199,7 +200,7 @@ export class AminoThread { * Method for updating the structure, by re-requesting information from the server */ public refresh(): AminoThread { - let response = request("GET", `https://service.narvii.com/api/v1/x${this.community.id}/s/chat/thread/${this.id}`, { + let response = request("GET", APIEndpoint.CompileThread(this.id,this.community.id), { "headers": { "NDCAUTH": "sid=" + this.client.session } @@ -233,9 +234,9 @@ export class AminoThread { /** * Class for storing thread objects */ -export class IAminoThreadStorage extends IAminoStorage { +export class AminoThreadStorage extends IAminoStorage { constructor(client: AminoClient, community: AminoCommunity, array?: any) { - super(client, IAminoThreadStorage.prototype); + super(client, AminoThreadStorage.prototype); if (array) { let threads: AminoThread[] = community.cache.threads.get(); let members: AminoMember[] = community.cache.members.get(); diff --git a/src/index.ts b/src/index.ts index 76fbf55..36ee65c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,45 +1,48 @@ -import { request } from "./request" - +import { request,requestAsync } from "./request" import IAminoCache from "./components/cache" import IAminoStorage from "./components/storage" import EventHandler, { event_type } from "./events/events" -import { AminoCommunity, IAminoCommunityStorage } from "./components/community/community" -import { AminoMember, IAminoMemberStorage } from "./components/member/member" -import { AminoThread, IAminoThreadStorage, thread_type } from "./components/thread/thread" -import { AminoMessage, IAminoMessageStorage, message_type } from "./components/message/message" -import { AminoBlog, IAminoBlogStorage } from "./components/blog/blog" -import { AminoComment, IAminoCommentStorage } from "./components/comment/comment" +import { AminoCommunity, AminoCommunityStorage as AminoCommunityStorage } from "./components/community/community" +import { AminoMember, AminoMemberStorage } from "./components/member/member" +import { AminoThread, AminoThreadStorage, thread_type } from "./components/thread/thread" +import { AminoMessage, AminoMessageStorage, message_type } from "./components/message/message" +import { AminoBlog, AminoBlogStorage } from "./components/blog/blog" +import { AminoComment, AminoCommentStorage } from "./components/comment/comment" +import { APIEndpoint } from "./components/APIEndpoint" export { request, + requestAsync, IAminoCache, IAminoStorage, - IAminoCommunityStorage, + AminoCommunityStorage, AminoCommunity, - IAminoMemberStorage, + AminoMemberStorage, AminoMember, - IAminoThreadStorage, + AminoThreadStorage, AminoThread, thread_type, - IAminoMessageStorage, + AminoMessageStorage, AminoMessage, message_type, - IAminoBlogStorage, + AminoBlogStorage, AminoBlog, - IAminoCommentStorage, - AminoComment + AminoCommentStorage, + AminoComment, + AminoClient, + APIEndpoint } export default class AminoClient { - public communities: IAminoCommunityStorage; + public communities: AminoCommunityStorage; public session: string; public device: string; - private event_handler: EventHandler; + private _eventHandler: EventHandler; /** * Initialization of the main client @@ -49,7 +52,7 @@ export default class AminoClient { */ constructor(login: string, password: string, device: string) { this.device = device; - this.session = request("POST", `https://service.narvii.com/api/v1/g/s/auth/login`, { + this.session = request("POST", APIEndpoint.Login, { "json": { "email": login, "secret": "0 " + password, @@ -59,13 +62,19 @@ export default class AminoClient { "timestamp": new Date().getTime() } }).sid; - this.communities = new IAminoCommunityStorage(this); + this.communities = new AminoCommunityStorage(this); } - + public on(event: event_type, callback: any) { - if (this.event_handler === undefined) { - this.event_handler = new EventHandler(this); + if (this._eventHandler === undefined) { + this._eventHandler = new EventHandler(this); + } + this._eventHandler.on(event, callback); + } + public onMessage(callback: any) { + if (this._eventHandler === undefined) { + this._eventHandler = new EventHandler(this); } - this.event_handler.on(event, callback); + this._eventHandler.on("message", callback); } }; diff --git a/src/request.ts b/src/request.ts index 853c946..475b808 100644 --- a/src/request.ts +++ b/src/request.ts @@ -1,12 +1,25 @@ import * as library from "sync-request" - +const fetch = require("node-fetch") const debug: boolean = (process.argv.includes("--requests-debug") || process.argv.includes("-rd")); - export function request(method: library.HttpVerb, url: string | URL, options?: library.Options): any { try { if (debug) console.log(`[REQUEST]: ${method} : ${url}`); return JSON.parse(library.default(method, url, options).getBody("utf8")); } catch (error) { + console.error(error) + throw new Error( + error.body + ); + } +} + +export async function requestAsync(method: library.HttpVerb, url: string | URL, options?: library.Options): Promise { + try { + if (debug) console.log(`[REQUEST]: ${method} : ${url}`); + options['method'] = method + return await fetch(url,options).then(i=>i.json()); + } catch (error) { + console.error(error) throw new Error( error.body );