-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
75 lines (67 loc) · 2.51 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { GroupMeConfig,
GroupMeCreateGroup,
GroupMeGroup,
GroupMeGroupResponse_Single,
GroupMeGroupResponse_Multiple,
GroupMeMessage,
GroupMeMessageResponse_Multiple
} from "./objects/GroupMeInterfaces";
import fetch from "node-fetch";
function _buildUrl(baseURL: string, endPoint: string, accessToken:string): string{
return `${baseURL}${endPoint}?token=${accessToken}`;
}
class GroupMe{
apiKey: string;
baseUrl: string = "https://api.groupme.com/v3";
constructor(config: GroupMeConfig) {
this.apiKey = config.apiKey;
}
async GetGroups():Promise<GroupMeGroup[]>
{
let full_url: string = _buildUrl(this.baseUrl, "/groups", this.apiKey);
let res = await fetch(full_url)
const data: GroupMeGroupResponse_Multiple = await res.json();
if (res.ok){
try {
const Groups: GroupMeGroup[] = data.response;
return Groups;
} catch (error) {
return Promise.reject(error)
}
}
return Promise.reject(new Error("Unable to Get Groups"))
}
async CreateGroup(group: GroupMeCreateGroup): Promise<GroupMeGroup> {
let full_url: string = _buildUrl(this.baseUrl, "/groups", this.apiKey);
const res = await fetch(full_url, {
method: "POST",
body: JSON.stringify(group),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
});
if (!res.ok){
let error = await res.json();
return Promise.reject(`Failed to Create Group: ${error.meta.errors}`)
}
let g: GroupMeGroupResponse_Single= await res.json();
return g.response;
}
async DeleteGroup(groupId: number): Promise<boolean> {
let full_url = _buildUrl(this.baseUrl, `/groups/${groupId}/destroy`, this.apiKey)
const res = await fetch(full_url, {
method: "POST"
});
return res.ok;
}
async GetMessagesByGroupId(group_id: Number): Promise<GroupMeMessage[]> {
let full_url = _buildUrl(this.baseUrl, `/groups/${group_id}/messages`, this.apiKey)
const res = await fetch(full_url);
let fullResp: GroupMeMessageResponse_Multiple = await res.json();
return fullResp.response.messages;
}
async GetMessages(group: GroupMeGroup): Promise<GroupMeMessage[]> {
return await this.GetMessagesByGroupId(parseInt(group.id));
}
}