-
Notifications
You must be signed in to change notification settings - Fork 5
/
getTicketData.ts
287 lines (264 loc) · 7.49 KB
/
getTicketData.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import type { ID } from "../utils/internal";
import {
apiBaseUrl,
buildRequestUrl,
call,
serializeProperties,
} from "../utils/internal";
import type { AuthObject } from "../utils/public";
import type {
AchievementTicketStats,
GameTicketStats,
MostTicketedGames,
RecentTickets,
TicketEntity,
UserTicketStats,
} from "./models";
interface GetTicketDataAllPayloadValues {
ticketId?: string | number;
offset?: number;
count?: number;
isGettingMostTicketedGames?: true;
username?: string;
gameId?: string | number;
isGettingTicketsForUnofficialAchievements?: true;
shouldReturnTicketsList?: true;
achievementId?: string | number;
}
/**
* BEGIN: Function overload definitions
*/
/**
* A call to this function will retrieve ticket metadata information
* about a single achievement ticket, targeted by its ticket ID.
*
* @param authorization An object containing your username and webApiKey.
* This can be constructed with `buildAuthorization()`.
*
* @param payload.ticketId The ID of the ticket to get information about.
*
* @example
* ```
* const ticketData = await getTicketData(
* authorization,
* { ticketId: 12345 }
* );
* ```
*
* @returns An object containing metadata about a target ticket.
*/
export function getTicketData(
authorization: AuthObject,
payload: { ticketId: ID }
): Promise<TicketEntity>;
/**
* A call to this function will retrieve ticket metadata information
* about the latest opened achievement tickets on RetroAchievements.
*
* @param authorization An object containing your username and webApiKey.
* This can be constructed with `buildAuthorization()`.
*
* @param payload.count Optional. Defaults to 10. Max is 100.
* How many tickets to retrieve.
*
* @param payload.offset Optional. Defaults to 0.
* Number of tickets to skip. This can be used for pagination.
*
* @example
* ```
* const ticketData = await getTicketData(authorization);
* ```
*
* @returns A list of the most recently opened tickets on the site.
*/
export function getTicketData(
authorization: AuthObject,
payload?: Partial<{ offset: number; count: number }>
): Promise<RecentTickets>;
/**
* A call to this function will retrieve the games on the site with
* the highest count of opened achievement tickets.
*
* @param authorization An object containing your username and webApiKey.
* This can be constructed with `buildAuthorization()`.
*
* @param payload.count Optional. Defaults to 10. Max is 100.
* How many ticketed games to retrieve.
*
* @param payload.offset Optional. Defaults to 0.
* Number of games to skip. This can be used for pagination.
*
* @example
* ```
* const ticketData = await getTicketData(
* authorization,
* { isGettingMostTicketedGames: true }
* );
* ```
*
* @returns A list of the most recently opened tickets on the site.
*/
export function getTicketData(
authorization: AuthObject,
payload: { isGettingMostTicketedGames: true; offset?: number; count?: number }
): Promise<MostTicketedGames>;
/**
* A call to this function will retrieve an achievement developer's
* ticket stats, targeted by that developer's username.
*
* @param authorization An object containing your username and webApiKey.
* This can be constructed with `buildAuthorization()`.
*
* @param payload.username The developer's account username to retrieve
* ticket stats for.
*
* @example
* ```
* const ticketData = await getTicketData(
* authorization,
* { username: "xelnia" }
* );
* ```
*
* @returns An achievement developer's ticket stats.
*/
export function getTicketData(
authorization: AuthObject,
payload: { username: string }
): Promise<UserTicketStats>;
/**
* A call to this function will retrieve a game's ticket stats, targeted
* by the game's ID. If you are unsure of a game's ID, visit its page
* on the RetroAchievements website and copy the number at the end of the URL.
*
* @param authorization An object containing your username and webApiKey.
* This can be constructed with `buildAuthorization()`.
*
* @param payload.gameId The game ID to fetch ticket stats for.
*
* @param payload.isGettingTicketsForUnofficialAchievements Optional. Fetch stats
* for unofficial/non-core achievements that have tickets.
*
* @param payload.shouldReturnTicketsList Optional. If true, not only fetches a
* game's ticket stats, but also returns a list of tickets for the game.
*
* @example
* ```
* const ticketData = await getTicketData(
* authorization,
* { gameId: 14_402 }
* );
* ```
*
* @returns A game's ticket stats, potentially also including the ticket list.
*/
export function getTicketData(
authorization: AuthObject,
payload: {
gameId: ID;
isGettingTicketsForUnofficialAchievements?: true;
shouldReturnTicketsList?: true;
}
): Promise<GameTicketStats>;
/**
* A call to this function will retrieve the an achievement's
* ticket stats, targeted by the achievement's ID. If you are unsure
* of an achievement's ID, open its page on the RetroAchievements
* website and copy the number at the end of the URL.
*
* @param authorization An object containing your username and webApiKey.
* This can be constructed with `buildAuthorization()`.
*
* @param payload.achievementId The ID of the achievement to fetch ticket
* stats for.
*
* @example
* ```
* const ticketData = await getTicketData(
* authorization,
* { achievementId: 12345 }
* );
* ```
*
* @returns An achievement developer's ticket stats.
*/
export function getTicketData(
authorization: AuthObject,
payload: { achievementId: ID }
): Promise<AchievementTicketStats>;
/**
* END: Function overload definitions
*/
export async function getTicketData(
authorization: AuthObject,
payload: GetTicketDataAllPayloadValues = {}
) {
const queryParams = buildGetTicketDataQueryParams(payload);
const url = buildRequestUrl(
apiBaseUrl,
"/API_GetTicketData.php",
authorization,
queryParams
);
const rawResponse = await call({ url });
return serializeProperties(rawResponse, {
shouldCastToNumbers: [
"ID",
"AchievementID",
"Points",
"GameID",
"ReportType",
"ReportState",
"OpenTickets",
],
shouldMapToBooleans: ["Hardcore"],
});
}
const buildGetTicketDataQueryParams = (
payload: GetTicketDataAllPayloadValues
) => {
const {
ticketId,
isGettingMostTicketedGames,
username,
gameId,
isGettingTicketsForUnofficialAchievements,
shouldReturnTicketsList,
achievementId,
} = payload;
let queryParams: Record<string, string | number> = {};
if (ticketId !== undefined) {
queryParams["i"] = ticketId;
} else if (isGettingMostTicketedGames) {
queryParams["f"] = "1";
queryParams = applyPaginationQueryParams(queryParams, payload);
} else if (username) {
queryParams["u"] = username;
} else if (gameId) {
queryParams["g"] = gameId;
if (isGettingTicketsForUnofficialAchievements) {
queryParams["f"] = "5";
}
if (shouldReturnTicketsList) {
queryParams["d"] = "1";
}
} else if (achievementId) {
queryParams["a"] = achievementId;
} else {
queryParams = applyPaginationQueryParams(queryParams, payload);
}
return queryParams;
};
const applyPaginationQueryParams = (
currentParams: Record<string, string | number>,
payload: Partial<{ count: number; offset: number }>
) => {
const modifiedParams = { ...currentParams };
if (payload.count !== undefined) {
modifiedParams["c"] = payload.count;
}
if (payload.offset !== undefined) {
modifiedParams["o"] = payload.offset;
}
return modifiedParams;
};