Skip to content

Commit 0fc39d0

Browse files
authored
feat(user): add getUserFriendsRequests function to retrieve friends requests (#228)
1 parent 5d80716 commit 0fc39d0

7 files changed

Lines changed: 167 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ Click the function names to open their complete docs on the docs site.
113113
- [`getProfileFromAccountId()`](https://psn-api.achievements.app/api-docs/users#getprofilefromaccountid) - Get a user's profile from the `accountId`.
114114
- [`getUserFriendsAccountIds()`](https://psn-api.achievements.app/api-docs/users#getuserfriendsaccountids) - Get a list
115115
of `accountId` values present on a target account's friends list.
116+
- [`getUserFriendsRequests()`](https://psn-api.achievements.app/api-docs/users#getuserfriendsrequests) - Get a list
117+
of `accountId` values corresponding to received friend requests for the account the client is logged into.
116118
- [`getBasicPresence()`](https://psn-api.achievements.app/api-docs/users#getbasicpresence) - Get a user's basic presence
117119
information.
118120
- [`getUserRegion()`](https://psn-api.achievements.app/api-docs/users#getuserregion) - Get a user's region information based on their username.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface GetUserFriendsRequestsResponse {
2+
/** A list of `accountId` values corresponding to accounts that have sent friend requests to the user. */
3+
receivedRequests: string[];
4+
5+
/** The total number of friend requests the user has received. */
6+
totalItemCount: number;
7+
}

src/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export * from "./authorization-payload.model";
44
export * from "./basic-presence-response.model";
55
export * from "./call-valid-headers.model";
66
export * from "./get-user-friends-account-ids-response.model";
7+
export * from "./get-user-friends-requests-response.model";
78
export * from "./membership.model";
89
export * from "./profile-from-account-id-response.model";
910
export * from "./profile-from-user-name-response.model";
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import nock from "nock";
2+
3+
import type {
4+
AuthorizationPayload,
5+
GetUserFriendsRequestsResponse
6+
} from "../models";
7+
import { getUserFriendsRequests } from "./getUserFriendsRequests";
8+
import { USER_BASE_URL } from "./USER_BASE_URL";
9+
10+
describe("Function: getUserFriendsRequests", () => {
11+
afterEach(() => {
12+
nock.cleanAll();
13+
});
14+
15+
it("is defined #sanity", () => {
16+
// ASSERT
17+
expect(getUserFriendsRequests).toBeDefined();
18+
});
19+
20+
it("retrieves the received friend requests for the authenticated user", async () => {
21+
// ARRANGE
22+
const mockAuthorization: AuthorizationPayload = {
23+
accessToken: "mockAccessToken"
24+
};
25+
26+
const mockResponse: GetUserFriendsRequestsResponse = {
27+
receivedRequests: ["2984038888603282554", "8403439712302084350"],
28+
totalItemCount: 2
29+
};
30+
31+
const baseUrlObj = new URL(USER_BASE_URL);
32+
const baseUrl = `${baseUrlObj.protocol}//${baseUrlObj.host}`;
33+
const basePath = baseUrlObj.pathname;
34+
35+
nock(baseUrl)
36+
.get(`${basePath}/me/friends/receivedRequests`)
37+
.query(true)
38+
.reply(200, mockResponse);
39+
40+
// ACT
41+
const response = await getUserFriendsRequests(mockAuthorization);
42+
43+
// ASSERT
44+
expect(response).toEqual(mockResponse);
45+
});
46+
47+
it("throws an error if we receive a response containing an `error` object", async () => {
48+
// ARRANGE
49+
const mockAuthorization: AuthorizationPayload = {
50+
accessToken: "mockAccessToken"
51+
};
52+
53+
const mockResponse = {
54+
error: {
55+
referenceId: "d71bd8ff-5f63-11ec-87da-d5dfd3bc6e67",
56+
code: 2_281_604,
57+
message: "Not Found"
58+
}
59+
};
60+
61+
nock("https://m.np.playstation.com")
62+
.get("/api/userProfile/v1/internal/users/me/friends/receivedRequests")
63+
.query(true)
64+
.reply(200, mockResponse);
65+
66+
// ASSERT
67+
await expect(
68+
getUserFriendsRequests(mockAuthorization)
69+
).rejects.toThrow();
70+
});
71+
});

src/user/getUserFriendsRequests.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import type {
2+
AllCallOptions,
3+
AuthorizationPayload,
4+
GetUserFriendsRequestsResponse
5+
} from "../models";
6+
import { buildRequestUrl } from "../utils/buildRequestUrl";
7+
import { call } from "../utils/call";
8+
import { USER_BASE_URL } from "./USER_BASE_URL";
9+
10+
type GetUserFriendsRequestsOptions = Pick<AllCallOptions, "limit" | "offset">;
11+
12+
/**
13+
* A call to this function will retrieve the list of received friend requests `accountId` values
14+
* for the account the client is logged into.
15+
*
16+
* @param authorization An object containing your access token, typically retrieved with `exchangeAccessCodeForAuthTokens()`.
17+
* @param options Optional parameters for pagination (limit and offset).
18+
*/
19+
export const getUserFriendsRequests = async (
20+
authorization: AuthorizationPayload,
21+
options?: Partial<GetUserFriendsRequestsOptions>
22+
): Promise<GetUserFriendsRequestsResponse> => {
23+
const url = buildRequestUrl(
24+
USER_BASE_URL,
25+
"/:accountId/friends/receivedRequests",
26+
options,
27+
{
28+
accountId: "me" // 'me' is used to refer to the authenticated user's account
29+
}
30+
);
31+
32+
const response = await call<GetUserFriendsRequestsResponse>(
33+
{ url },
34+
authorization
35+
);
36+
37+
// If you are unable to access the user's friend requests, an error will be thrown.
38+
if ((response as any)?.error) {
39+
throw new Error((response as any)?.error?.message ?? "Unexpected Error");
40+
}
41+
42+
return response;
43+
};

src/user/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ export * from "./getProfileFromAccountId";
44
export * from "./getProfileFromUserName";
55
export * from "./getProfileShareableLink";
66
export * from "./getUserFriendsAccountIds";
7+
export * from "./getUserFriendsRequests";
78
export * from "./getUserPlayedGames";
89
export * from "./getUserRegion";

website/docs/api-docs/users.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,48 @@ These are the possible values that can be in the `options` object (the third par
217217

218218
---
219219

220+
## getUserFriendsRequests
221+
222+
A call to this function will retrieve the list of received friend requests (as `accountId` values) for the account the client is logged into.
223+
224+
### Examples
225+
226+
#### Get received friend requests
227+
228+
```ts
229+
import { getUserFriendsRequests } from "psn-api";
230+
231+
const response = await getUserFriendsRequests(authorization);
232+
```
233+
234+
### Returns
235+
236+
| Name | Type | Description |
237+
| :----------------- | :--------- | :------------------------------------------------------------------------------- |
238+
| `receivedRequests` | `string[]` | The `accountId` values of the users who have sent friend requests to your account. |
239+
| `totalItemCount` | `number` | The total number of friend requests received. |
240+
241+
### Parameters
242+
243+
| Name | Type | Description |
244+
| :-------------- | :-------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------- |
245+
| `authorization` | [`AuthorizationPayload`](/api-docs/data-models/authorization-payload) | An object that must contain an `accessToken`. See [this page](/authentication/authenticating-manually) for how to get one. |
246+
247+
### Options
248+
249+
These are the possible values that can be in the `options` object (the second parameter of the function).
250+
251+
| Name | Type | Description |
252+
| :------- | :------- | :--------------------------------------------------- |
253+
| `limit` | `number` | Limit the number of friend requests returned. |
254+
| `offset` | `number` | Return friend request data from this result onwards. |
255+
256+
### Source
257+
258+
[user/getUserFriendsRequests.ts](https://github.com/achievements-app/psn-api/blob/main/src/user/getUserFriendsRequests.ts)
259+
260+
---
261+
220262
## getBasicPresence
221263

222264
A call to this function will retrieve the presence of the accountId being requested. If the user cannot be found (either

0 commit comments

Comments
 (0)