@retroachievements/api 2.3.0 → 2.5.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/README.md +8 -0
- package/dist/api.cjs +1 -1
- package/dist/api.cjs.map +1 -1
- package/dist/api.modern.js +1 -1
- package/dist/api.modern.js.map +1 -1
- package/dist/api.module.js +1 -1
- package/dist/api.module.js.map +1 -1
- package/dist/api.umd.js +1 -1
- package/dist/api.umd.js.map +1 -1
- package/dist/comment/getComments.d.ts +58 -0
- package/dist/comment/getComments.test.d.ts +1 -0
- package/dist/comment/index.d.ts +2 -0
- package/dist/comment/models/comment-entity.model.d.ts +6 -0
- package/dist/comment/models/comment-response.model.d.ts +6 -0
- package/dist/comment/models/get-comments-response.model.d.ts +12 -0
- package/dist/comment/models/index.d.ts +3 -0
- package/dist/index.d.ts +2 -0
- package/dist/leaderboard/getLeaderboardEntries.d.ts +46 -0
- package/dist/leaderboard/getLeaderboardEntries.test.d.ts +1 -0
- package/dist/leaderboard/index.d.ts +2 -0
- package/dist/leaderboard/models/get-leaderboard-entries-response.model.d.ts +12 -0
- package/dist/leaderboard/models/index.d.ts +2 -0
- package/dist/leaderboard/models/leaderboard-entries.model.d.ts +12 -0
- package/package.json +1 -1
- package/src/comment/getComments.test.ts +277 -0
- package/src/comment/getComments.ts +107 -0
- package/src/comment/index.ts +2 -0
- package/src/comment/models/comment-entity.model.ts +7 -0
- package/src/comment/models/comment-response.model.ts +7 -0
- package/src/comment/models/get-comments-response.model.ts +13 -0
- package/src/comment/models/index.ts +3 -0
- package/src/index.ts +2 -0
- package/src/leaderboard/getLeaderboardEntries.test.ts +73 -0
- package/src/leaderboard/getLeaderboardEntries.ts +75 -0
- package/src/leaderboard/index.ts +2 -0
- package/src/leaderboard/models/get-leaderboard-entries-response.model.ts +12 -0
- package/src/leaderboard/models/index.ts +2 -0
- package/src/leaderboard/models/leaderboard-entries.model.ts +12 -0
package/dist/index.d.ts
CHANGED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { ID } from "../utils/internal";
|
|
2
|
+
import type { AuthObject } from "../utils/public";
|
|
3
|
+
import type { LeaderboardEntries } from "./models";
|
|
4
|
+
/**
|
|
5
|
+
* A call to this endpoint will retrieve a given leaderboard's entries, targeted by its ID.
|
|
6
|
+
*
|
|
7
|
+
* @param authorization An object containing your username and webApiKey.
|
|
8
|
+
* This can be constructed with `buildAuthorization()`.
|
|
9
|
+
*
|
|
10
|
+
* @param payload.leaderboardId The target leaderboard ID.
|
|
11
|
+
*
|
|
12
|
+
* @param payload.offset Defaults to 0. The number of entries to skip.
|
|
13
|
+
*
|
|
14
|
+
* @param payload.count Defaults to 100, has a max of 500.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```
|
|
18
|
+
* const leaderboardEntries = await getLeaderboardEntries(
|
|
19
|
+
* authorization,
|
|
20
|
+
* { leaderboardId: 14402 }
|
|
21
|
+
* );
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @returns An object containing a leaderboard's entries.
|
|
25
|
+
* ```json
|
|
26
|
+
* {
|
|
27
|
+
* "count": 100,
|
|
28
|
+
* "total": 1287,
|
|
29
|
+
* "results": [
|
|
30
|
+
* {
|
|
31
|
+
* "rank": 1,
|
|
32
|
+
* "user": "vani11a",
|
|
33
|
+
* "ulid": "00003EMFWR7XB8SDPEHB3K56ZQ",
|
|
34
|
+
* "score": 390490,
|
|
35
|
+
* "formattedScore": "390,490",
|
|
36
|
+
* "dateSubmitted": "2024-07-25T15:51:00+00:00"
|
|
37
|
+
* }
|
|
38
|
+
* ]
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare const getLeaderboardEntries: (authorization: AuthObject, payload: {
|
|
43
|
+
leaderboardId: ID;
|
|
44
|
+
offset?: number;
|
|
45
|
+
count?: number;
|
|
46
|
+
}) => Promise<LeaderboardEntries>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/* eslint-disable sonarjs/no-duplicate-string */
|
|
2
|
+
|
|
3
|
+
import { http, HttpResponse } from "msw";
|
|
4
|
+
import { setupServer } from "msw/node";
|
|
5
|
+
|
|
6
|
+
import { apiBaseUrl } from "../utils/internal";
|
|
7
|
+
import { buildAuthorization } from "../utils/public";
|
|
8
|
+
import { getComments } from "./getComments";
|
|
9
|
+
import type { CommentsResponse, GetCommentsResponse } from "./models";
|
|
10
|
+
|
|
11
|
+
const server = setupServer();
|
|
12
|
+
|
|
13
|
+
describe("Function: getComments", () => {
|
|
14
|
+
// MSW Setup
|
|
15
|
+
beforeAll(() => server.listen());
|
|
16
|
+
afterEach(() => server.resetHandlers());
|
|
17
|
+
afterAll(() => server.close());
|
|
18
|
+
|
|
19
|
+
it("is defined #sanity", () => {
|
|
20
|
+
// ASSERT
|
|
21
|
+
expect(getComments).toBeDefined();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("retrieves the comments on a user's wall", async () => {
|
|
25
|
+
// ARRANGE
|
|
26
|
+
const authorization = buildAuthorization({
|
|
27
|
+
username: "mockUserName",
|
|
28
|
+
webApiKey: "mockWebApiKey",
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const mockResponse: GetCommentsResponse = {
|
|
32
|
+
Count: 2,
|
|
33
|
+
Total: 2,
|
|
34
|
+
Results: [
|
|
35
|
+
{
|
|
36
|
+
User: "PlayTester",
|
|
37
|
+
Submitted: "2024-07-31T11:22:23.000000Z",
|
|
38
|
+
CommentText: "Comment 1",
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
User: "PlayTester",
|
|
42
|
+
Submitted: "2024-07-31T11:22:23.000000Z",
|
|
43
|
+
CommentText: "Comment 2",
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
server.use(
|
|
49
|
+
http.get(`${apiBaseUrl}/API_GetComments.php`, () =>
|
|
50
|
+
HttpResponse.json(mockResponse)
|
|
51
|
+
)
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
// ACT
|
|
55
|
+
const response = await getComments(authorization, {
|
|
56
|
+
identifier: "xelnia",
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// ASSERT
|
|
60
|
+
const expectedResponse: CommentsResponse = {
|
|
61
|
+
count: 2,
|
|
62
|
+
total: 2,
|
|
63
|
+
results: [
|
|
64
|
+
{
|
|
65
|
+
user: "PlayTester",
|
|
66
|
+
submitted: "2024-07-31T11:22:23.000000Z",
|
|
67
|
+
commentText: "Comment 1",
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
user: "PlayTester",
|
|
71
|
+
submitted: "2024-07-31T11:22:23.000000Z",
|
|
72
|
+
commentText: "Comment 2",
|
|
73
|
+
},
|
|
74
|
+
],
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
expect(response).toEqual(expectedResponse);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("retrieves the comments on an game", async () => {
|
|
81
|
+
// ARRANGE
|
|
82
|
+
const authorization = buildAuthorization({
|
|
83
|
+
username: "mockUserName",
|
|
84
|
+
webApiKey: "mockWebApiKey",
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const mockResponse: GetCommentsResponse = {
|
|
88
|
+
Count: 2,
|
|
89
|
+
Total: 2,
|
|
90
|
+
Results: [
|
|
91
|
+
{
|
|
92
|
+
User: "PlayTester",
|
|
93
|
+
Submitted: "2024-07-31T11:22:23.000000Z",
|
|
94
|
+
CommentText: "Comment 1",
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
User: "PlayTester",
|
|
98
|
+
Submitted: "2024-07-31T11:22:23.000000Z",
|
|
99
|
+
CommentText: "Comment 2",
|
|
100
|
+
},
|
|
101
|
+
],
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
server.use(
|
|
105
|
+
http.get(`${apiBaseUrl}/API_GetComments.php`, () =>
|
|
106
|
+
HttpResponse.json(mockResponse)
|
|
107
|
+
)
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
// ACT
|
|
111
|
+
const response = await getComments(authorization, {
|
|
112
|
+
identifier: 321_865,
|
|
113
|
+
kind: "game",
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// ASSERT
|
|
117
|
+
const expectedResponse: CommentsResponse = {
|
|
118
|
+
count: 2,
|
|
119
|
+
total: 2,
|
|
120
|
+
results: [
|
|
121
|
+
{
|
|
122
|
+
user: "PlayTester",
|
|
123
|
+
submitted: "2024-07-31T11:22:23.000000Z",
|
|
124
|
+
commentText: "Comment 1",
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
user: "PlayTester",
|
|
128
|
+
submitted: "2024-07-31T11:22:23.000000Z",
|
|
129
|
+
commentText: "Comment 2",
|
|
130
|
+
},
|
|
131
|
+
],
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
expect(response).toEqual(expectedResponse);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("retrieves the comments on an achievement, respecting count", async () => {
|
|
138
|
+
// ARRANGE
|
|
139
|
+
const authorization = buildAuthorization({
|
|
140
|
+
username: "mockUserName",
|
|
141
|
+
webApiKey: "mockWebApiKey",
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const mockResponse: GetCommentsResponse = {
|
|
145
|
+
Count: 2,
|
|
146
|
+
Total: 4,
|
|
147
|
+
Results: [
|
|
148
|
+
{
|
|
149
|
+
User: "PlayTester",
|
|
150
|
+
Submitted: "2024-07-31T11:22:23.000000Z",
|
|
151
|
+
CommentText: "Comment 1",
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
User: "PlayTester",
|
|
155
|
+
Submitted: "2024-07-31T11:22:23.000000Z",
|
|
156
|
+
CommentText: "Comment 2",
|
|
157
|
+
},
|
|
158
|
+
],
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
server.use(
|
|
162
|
+
http.get(`${apiBaseUrl}/API_GetComments.php`, () =>
|
|
163
|
+
HttpResponse.json(mockResponse)
|
|
164
|
+
)
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
// ACT
|
|
168
|
+
const response = await getComments(authorization, {
|
|
169
|
+
identifier: 321_865,
|
|
170
|
+
count: 2,
|
|
171
|
+
kind: "achievement",
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
// ASSERT
|
|
175
|
+
const expectedResponse: CommentsResponse = {
|
|
176
|
+
count: 2,
|
|
177
|
+
total: 4,
|
|
178
|
+
results: [
|
|
179
|
+
{
|
|
180
|
+
user: "PlayTester",
|
|
181
|
+
submitted: "2024-07-31T11:22:23.000000Z",
|
|
182
|
+
commentText: "Comment 1",
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
user: "PlayTester",
|
|
186
|
+
submitted: "2024-07-31T11:22:23.000000Z",
|
|
187
|
+
commentText: "Comment 2",
|
|
188
|
+
},
|
|
189
|
+
],
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
expect(response).toEqual(expectedResponse);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("retrieves the comments on an game, respecting offset", async () => {
|
|
196
|
+
// ARRANGE
|
|
197
|
+
const authorization = buildAuthorization({
|
|
198
|
+
username: "mockUserName",
|
|
199
|
+
webApiKey: "mockWebApiKey",
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const mockResponse: GetCommentsResponse = {
|
|
203
|
+
Count: 1,
|
|
204
|
+
Total: 2,
|
|
205
|
+
Results: [
|
|
206
|
+
{
|
|
207
|
+
User: "PlayTester",
|
|
208
|
+
Submitted: "2024-07-31T11:22:23.000000Z",
|
|
209
|
+
CommentText: "Comment 2",
|
|
210
|
+
},
|
|
211
|
+
],
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
server.use(
|
|
215
|
+
http.get(`${apiBaseUrl}/API_GetComments.php`, () =>
|
|
216
|
+
HttpResponse.json(mockResponse)
|
|
217
|
+
)
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
// ACT
|
|
221
|
+
const response = await getComments(authorization, {
|
|
222
|
+
identifier: 321_865,
|
|
223
|
+
offset: 1,
|
|
224
|
+
kind: "game",
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
// ASSERT
|
|
228
|
+
const expectedResponse: CommentsResponse = {
|
|
229
|
+
count: 1,
|
|
230
|
+
total: 2,
|
|
231
|
+
results: [
|
|
232
|
+
{
|
|
233
|
+
user: "PlayTester",
|
|
234
|
+
submitted: "2024-07-31T11:22:23.000000Z",
|
|
235
|
+
commentText: "Comment 2",
|
|
236
|
+
},
|
|
237
|
+
],
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
expect(response).toEqual(expectedResponse);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it("warns the developer when they don't specify kind for achievements/games", async () => {
|
|
244
|
+
// ARRANGE
|
|
245
|
+
const authorization = buildAuthorization({
|
|
246
|
+
username: "mockUserName",
|
|
247
|
+
webApiKey: "mockWebApiKey",
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
const mockResponse: GetCommentsResponse = {
|
|
251
|
+
Count: 1,
|
|
252
|
+
Total: 2,
|
|
253
|
+
Results: [
|
|
254
|
+
{
|
|
255
|
+
User: "PlayTester",
|
|
256
|
+
Submitted: "2024-07-31T11:22:23.000000Z",
|
|
257
|
+
CommentText: "Comment 2",
|
|
258
|
+
},
|
|
259
|
+
],
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
server.use(
|
|
263
|
+
http.get(`${apiBaseUrl}/API_GetComments.php`, () =>
|
|
264
|
+
HttpResponse.json(mockResponse)
|
|
265
|
+
)
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
// ACT
|
|
269
|
+
const response = getComments(authorization, {
|
|
270
|
+
identifier: 321_865,
|
|
271
|
+
offset: 1,
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
// ASSERT
|
|
275
|
+
await expect(response).rejects.toThrow(TypeError);
|
|
276
|
+
});
|
|
277
|
+
});
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { ID } from "../utils/internal";
|
|
2
|
+
import {
|
|
3
|
+
apiBaseUrl,
|
|
4
|
+
buildRequestUrl,
|
|
5
|
+
call,
|
|
6
|
+
serializeProperties,
|
|
7
|
+
} from "../utils/internal";
|
|
8
|
+
import type { AuthObject } from "../utils/public";
|
|
9
|
+
import type { CommentsResponse, GetCommentsResponse } from "./models";
|
|
10
|
+
|
|
11
|
+
const kindMap: Record<"game" | "achievement" | "user", number> = {
|
|
12
|
+
game: 1,
|
|
13
|
+
achievement: 2,
|
|
14
|
+
user: 3,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A call to this function will retrieve a list of comments on a particular target.
|
|
19
|
+
*
|
|
20
|
+
* @param authorization An object containing your username and webApiKey.
|
|
21
|
+
* This can be constructed with `buildAuthorization()`.
|
|
22
|
+
*
|
|
23
|
+
* @param payload.identifier The identifier to retrieve. For user walls, this will
|
|
24
|
+
* be a string (the username), and for game and achievement walls, this will be a
|
|
25
|
+
* the ID of the object in question.
|
|
26
|
+
*
|
|
27
|
+
* @param payload.kind What kind of identifier was used. This can be "game",
|
|
28
|
+
* "achievement", or "user".
|
|
29
|
+
*
|
|
30
|
+
* @param payload.offset Defaults to 0. The number of entries to skip.
|
|
31
|
+
*
|
|
32
|
+
* @param payload.count Defaults to 50, has a max of 500.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```
|
|
36
|
+
* // Retrieving game/achievement comments
|
|
37
|
+
* const gameWallComments = await getComments(
|
|
38
|
+
* authorization,
|
|
39
|
+
* { identifier: 20294, kind: 'game', count: 4, offset: 0 },
|
|
40
|
+
* );
|
|
41
|
+
*
|
|
42
|
+
* // Retrieving comments on a user's wall
|
|
43
|
+
* const userWallComments = await getComments(
|
|
44
|
+
* authorization,
|
|
45
|
+
* { identifier: "xelnia", count: 4, offset: 0 },
|
|
46
|
+
* );
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* @returns An object containing the amount of comments retrieved,
|
|
50
|
+
* the total comments, and an array of the comments themselves.
|
|
51
|
+
* ```
|
|
52
|
+
* {
|
|
53
|
+
* count: 4,
|
|
54
|
+
* total: 4,
|
|
55
|
+
* results: [
|
|
56
|
+
* {
|
|
57
|
+
* user: "PlayTester",
|
|
58
|
+
* submitted: "2024-07-31T11:22:23.000000Z",
|
|
59
|
+
* commentText: "Comment 1"
|
|
60
|
+
* },
|
|
61
|
+
* // ...
|
|
62
|
+
* ]
|
|
63
|
+
* }
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export const getComments = async (
|
|
67
|
+
authorization: AuthObject,
|
|
68
|
+
payload: {
|
|
69
|
+
identifier: ID;
|
|
70
|
+
kind?: "game" | "achievement" | "user";
|
|
71
|
+
offset?: number;
|
|
72
|
+
count?: number;
|
|
73
|
+
}
|
|
74
|
+
): Promise<CommentsResponse> => {
|
|
75
|
+
const { identifier, kind, offset, count } = payload;
|
|
76
|
+
|
|
77
|
+
const queryParams: Record<string, number | string> = { i: identifier };
|
|
78
|
+
|
|
79
|
+
if (kind) {
|
|
80
|
+
queryParams.t = kindMap[kind];
|
|
81
|
+
} else if (typeof identifier === "number") {
|
|
82
|
+
throw new TypeError(
|
|
83
|
+
"'kind' must be specified when looking up an achievement or game."
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (offset) {
|
|
88
|
+
queryParams.o = offset;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (count) {
|
|
92
|
+
queryParams.c = count;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const url = buildRequestUrl(
|
|
96
|
+
apiBaseUrl,
|
|
97
|
+
"/API_GetComments.php",
|
|
98
|
+
authorization,
|
|
99
|
+
queryParams
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
const rawResponse = await call<GetCommentsResponse>({ url });
|
|
103
|
+
|
|
104
|
+
return serializeProperties(rawResponse, {
|
|
105
|
+
shouldCastToNumbers: ["Count", "Total"],
|
|
106
|
+
});
|
|
107
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
interface RawCommentsResponseEntity {
|
|
2
|
+
Count: number;
|
|
3
|
+
Total: number;
|
|
4
|
+
Results: RawComment[];
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
interface RawComment {
|
|
8
|
+
User: string;
|
|
9
|
+
Submitted: string;
|
|
10
|
+
CommentText: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type GetCommentsResponse = RawCommentsResponseEntity;
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// This file is the single public-facing API of the entire library.
|
|
2
2
|
|
|
3
3
|
export * from "./achievement";
|
|
4
|
+
export * from "./comment";
|
|
4
5
|
export * from "./console";
|
|
5
6
|
export * from "./feed";
|
|
6
7
|
export * from "./game";
|
|
8
|
+
export * from "./leaderboard";
|
|
7
9
|
export * from "./ticket";
|
|
8
10
|
export * from "./user";
|
|
9
11
|
export * from "./utils/public";
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/* eslint-disable sonarjs/no-duplicate-string */
|
|
2
|
+
|
|
3
|
+
import { http, HttpResponse } from "msw";
|
|
4
|
+
import { setupServer } from "msw/node";
|
|
5
|
+
|
|
6
|
+
import { apiBaseUrl } from "../utils/internal";
|
|
7
|
+
import { buildAuthorization } from "../utils/public";
|
|
8
|
+
import { getLeaderboardEntries } from "./getLeaderboardEntries";
|
|
9
|
+
import type { GetLeaderboardEntriesResponse } from "./models";
|
|
10
|
+
|
|
11
|
+
const server = setupServer();
|
|
12
|
+
|
|
13
|
+
describe("Function: getLeaderboardEntries", () => {
|
|
14
|
+
// MSW Setup
|
|
15
|
+
beforeAll(() => server.listen());
|
|
16
|
+
afterEach(() => server.resetHandlers());
|
|
17
|
+
afterAll(() => server.close());
|
|
18
|
+
|
|
19
|
+
it("is defined #sanity", () => {
|
|
20
|
+
// ASSERT
|
|
21
|
+
expect(getLeaderboardEntries).toBeDefined();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("given a leaderboard ID, retrieves the entries in that leaderboard", async () => {
|
|
25
|
+
// ARRANGE
|
|
26
|
+
const authorization = buildAuthorization({
|
|
27
|
+
username: "mockUserName",
|
|
28
|
+
webApiKey: "mockWebApiKey",
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const mockResponse: GetLeaderboardEntriesResponse = {
|
|
32
|
+
Count: 100,
|
|
33
|
+
Total: 1287,
|
|
34
|
+
Results: [
|
|
35
|
+
{
|
|
36
|
+
Rank: 1,
|
|
37
|
+
User: "vani11a",
|
|
38
|
+
ULID: "00003EMFWR7XB8SDPEHB3K56ZQ",
|
|
39
|
+
Score: 390_490,
|
|
40
|
+
FormattedScore: "390,490",
|
|
41
|
+
DateSubmitted: "2024-07-25T15:51:00+00:00",
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
server.use(
|
|
47
|
+
http.get(`${apiBaseUrl}/API_GetLeaderboardEntries.php`, () =>
|
|
48
|
+
HttpResponse.json(mockResponse)
|
|
49
|
+
)
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
// ACT
|
|
53
|
+
const response = await getLeaderboardEntries(authorization, {
|
|
54
|
+
leaderboardId: 104_370,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// ASSERT
|
|
58
|
+
expect(response).toEqual({
|
|
59
|
+
count: 100,
|
|
60
|
+
total: 1287,
|
|
61
|
+
results: [
|
|
62
|
+
{
|
|
63
|
+
rank: 1,
|
|
64
|
+
user: "vani11a",
|
|
65
|
+
ulid: "00003EMFWR7XB8SDPEHB3K56ZQ",
|
|
66
|
+
score: 390_490,
|
|
67
|
+
formattedScore: "390,490",
|
|
68
|
+
dateSubmitted: "2024-07-25T15:51:00+00:00",
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { ID } from "../utils/internal";
|
|
2
|
+
import {
|
|
3
|
+
apiBaseUrl,
|
|
4
|
+
buildRequestUrl,
|
|
5
|
+
call,
|
|
6
|
+
serializeProperties,
|
|
7
|
+
} from "../utils/internal";
|
|
8
|
+
import type { AuthObject } from "../utils/public";
|
|
9
|
+
import type {
|
|
10
|
+
GetLeaderboardEntriesResponse,
|
|
11
|
+
LeaderboardEntries,
|
|
12
|
+
} from "./models";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A call to this endpoint will retrieve a given leaderboard's entries, targeted by its ID.
|
|
16
|
+
*
|
|
17
|
+
* @param authorization An object containing your username and webApiKey.
|
|
18
|
+
* This can be constructed with `buildAuthorization()`.
|
|
19
|
+
*
|
|
20
|
+
* @param payload.leaderboardId The target leaderboard ID.
|
|
21
|
+
*
|
|
22
|
+
* @param payload.offset Defaults to 0. The number of entries to skip.
|
|
23
|
+
*
|
|
24
|
+
* @param payload.count Defaults to 100, has a max of 500.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```
|
|
28
|
+
* const leaderboardEntries = await getLeaderboardEntries(
|
|
29
|
+
* authorization,
|
|
30
|
+
* { leaderboardId: 14402 }
|
|
31
|
+
* );
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @returns An object containing a leaderboard's entries.
|
|
35
|
+
* ```json
|
|
36
|
+
* {
|
|
37
|
+
* "count": 100,
|
|
38
|
+
* "total": 1287,
|
|
39
|
+
* "results": [
|
|
40
|
+
* {
|
|
41
|
+
* "rank": 1,
|
|
42
|
+
* "user": "vani11a",
|
|
43
|
+
* "ulid": "00003EMFWR7XB8SDPEHB3K56ZQ",
|
|
44
|
+
* "score": 390490,
|
|
45
|
+
* "formattedScore": "390,490",
|
|
46
|
+
* "dateSubmitted": "2024-07-25T15:51:00+00:00"
|
|
47
|
+
* }
|
|
48
|
+
* ]
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export const getLeaderboardEntries = async (
|
|
53
|
+
authorization: AuthObject,
|
|
54
|
+
payload: { leaderboardId: ID; offset?: number; count?: number }
|
|
55
|
+
): Promise<LeaderboardEntries> => {
|
|
56
|
+
const queryParams: Record<string, any> = {};
|
|
57
|
+
queryParams.i = payload.leaderboardId;
|
|
58
|
+
if (payload?.offset) {
|
|
59
|
+
queryParams.o = payload.offset;
|
|
60
|
+
}
|
|
61
|
+
if (payload?.count) {
|
|
62
|
+
queryParams.c = payload.count;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const url = buildRequestUrl(
|
|
66
|
+
apiBaseUrl,
|
|
67
|
+
"/API_GetLeaderboardEntries.php",
|
|
68
|
+
authorization,
|
|
69
|
+
queryParams
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const rawResponse = await call<GetLeaderboardEntriesResponse>({ url });
|
|
73
|
+
|
|
74
|
+
return serializeProperties(rawResponse);
|
|
75
|
+
};
|