This repository has been archived by the owner on Dec 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ListItemRouter.ts
108 lines (100 loc) · 3.63 KB
/
ListItemRouter.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { APIListItem, APIListItemPayload, Routes } from 'guilded-api-typings';
import { BaseRouter } from './BaseRouter';
/**
* The list item router for the Guilded REST API.
* @example new ListItemRouter(rest);
*/
export class ListItemRouter extends BaseRouter {
/**
* Fetch a list item from Guilded.
* @param channelId The ID of the channel the list item belongs to.
* @param listItemId The ID of the list item to fetch.
* @returns The fetched list item.
* @example listItems.fetch('abc', 'abc');
*/
fetch(channelId: string, listItemId: string): Promise<APIListItem>;
/**
* Fetch list items from Guilded.
* @param channelId The ID of the channel the list items belong to.
* @returns The fetched list items.
* @example listItems.fetch('abc');
*/
fetch(channelId: string): Promise<APIListItem[]>;
/** @ignore */
fetch(channelId: string, listItemId?: string) {
if (listItemId) return this.fetchSingle(channelId, listItemId);
return this.fetchMany(channelId);
}
/** @ignore */
private async fetchSingle(channelId: string, listItemId: string) {
const { listItem } = await this.rest.get<{ listItem: APIListItem }>(
Routes.listItem(channelId, listItemId),
);
return listItem;
}
/** @ignore */
private async fetchMany(channelId: string) {
const { listItems } = await this.rest.get<{ listItems: APIListItem[] }>(
Routes.listItems(channelId),
);
return listItems;
}
/**
* Create a list item on Guilded.
* @param channelId The ID of the channel the list item belongs to.
* @param message The message of the list item.
* @param note The note of the list item.
* @returns The created list item.
* @example listItems.create('abc', 'Random item');
*/
async create(channelId: string, message: string, note?: string) {
const { listItem } = await this.rest.post<{ listItem: APIListItem }, APIListItemPayload>(
Routes.listItems(channelId),
{ message, note: note ? { content: note } : undefined },
);
return listItem;
}
/**
* Edit a list item on Guilded.
* @param channelId The ID of the channel the list item belongs to.
* @param listItemId The ID of the list item to edit.
* @param message The message of theb list item.
* @param note The note of the list item.
* @returns The edited list item.
* @example listItems.edit('abc', 'abc', 'Random item');
*/
async edit(channelId: string, listItemId: string, message: string, note?: string) {
const { listItem } = await this.rest.put<{ listItem: APIListItem }, APIListItemPayload>(
Routes.listItem(channelId, listItemId),
{ message, note: note ? { content: note } : undefined },
);
return listItem;
}
/**
* Delete a list item from Guilded.
* @param channelId The ID of the channel the list item belongs to.
* @param listItemId The ID of the list item to delete.
* @example listItems.delete('abc', 'abc');
*/
delete(channelId: string, listItemId: string) {
return this.rest.post<void>(Routes.listItem(channelId, listItemId));
}
/**
* Complete a list item on Guilded.
* @param channelId The ID of the channel the list item belongs to.
* @param listItemId The ID of the list item to complete.
* @example listItems.complete('abc', 'abc');
*/
complete(channelId: string, listItemId: string) {
return this.rest.put<void>(Routes.listItemComplete(channelId, listItemId));
}
/**
* Uncomplete a list item on Guilded.
* @param channelId The ID of the channel the list item belongs to.
* @param listItemId The ID of the list item to uncomplete.
* @example listItems.uncomplete('abc', 'abc');
*/
uncomplete(channelId: string, listItemId: string) {
return this.rest.delete<void>(Routes.listItemComplete(channelId, listItemId));
}
}