Skip to content

Commit

Permalink
field options for getUserProfile
Browse files Browse the repository at this point in the history
  • Loading branch information
wtflink authored and chentsulin committed Jan 29, 2019
1 parent 9a2494e commit 30cf2b6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
13 changes: 6 additions & 7 deletions packages/messaging-api-messenger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2014,26 +2014,25 @@ The API will respond with the time the broadcast is scheduled for, and one of th

### User Profile API - [Official Docs](https://developers.facebook.com/docs/messenger-platform/user-profile)

## `getUserProfile(userId)`
## `getUserProfile(userId, options)`

Retrieving a Person's Profile.

| Param | Type | Description |
| ------ | -------- | ------------------------------------- |
| userId | `String` | Page-scoped user ID of the recipient. |
| Param | Type | Description |
| -------------- | --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| userId | `String` | Page-scoped user ID of the recipient. |
| options.fields | `Array<String>` | Value must be among `id`, `name`, `first_name`, `last_name`, `profile_pic`, `locale`, `timezone`, `gender`, default with `id`, `name`, `first_name`, `last_name`, `profile_pic`, |

Example:

```js
client.getUserProfile(USER_ID).then(user => {
console.log(user);
// {
// id: '5566'
// first_name: 'Johnathan',
// last_name: 'Jackson',
// profile_pic: 'https://example.com/pic.png',
// locale: 'en_US',
// timezone: 8,
// gender: 'male',
// }
});
```
Expand Down
11 changes: 9 additions & 2 deletions packages/messaging-api-messenger/src/MessengerClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,17 @@ export default class MessengerClient {
*/
getUserProfile(
userId: string,
{ access_token: customAccessToken }: { access_token?: string } = {}
{
access_token: customAccessToken,
fields = ['id', 'name', 'first_name', 'last_name', 'profile_pic'],
}: { access_token?: string, fields?: Array<string> } = {}
): Promise<User> {
return this._axios
.get(`/${userId}?access_token=${customAccessToken || this._accessToken}`)
.get(
`/${userId}?fields=${fields.join(
','
)}&access_token=${customAccessToken || this._accessToken}`
)
.then(res => res.data, handleError);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,30 @@ describe('#getMessagingFeatureReview', () => {

describe('user profile', () => {
describe('#getUserProfile', () => {
it('should response user profile', async () => {
it('should get user profile with default fields', async () => {
const { client, mock } = createMock();
const reply = {
id: '1',
first_name: 'Kevin',
last_name: 'Durant',
profile_pic: 'https://example.com/pic.png',
};

mock
.onGet(
`/1?fields=id,name,first_name,last_name,profile_pic&access_token=${ACCESS_TOKEN}`
)
.reply(200, reply);

const res = await client.getUserProfile('1');

expect(res).toEqual(reply);
});

it('should get user profile with given fields', async () => {
const { client, mock } = createMock();
const reply = {
id: '1',
first_name: 'Kevin',
last_name: 'Durant',
profile_pic: 'https://example.com/pic.png',
Expand All @@ -277,9 +298,24 @@ describe('user profile', () => {
gender: 'male',
};

mock.onGet(`/1?access_token=${ACCESS_TOKEN}`).reply(200, reply);
mock
.onGet(
`/1?fields=id,name,first_name,last_name,profile_pic,locale,timezone,gender&access_token=${ACCESS_TOKEN}`
)
.reply(200, reply);

const res = await client.getUserProfile('1');
const res = await client.getUserProfile('1', {
fields: [
'id',
'name',
'first_name',
'last_name',
'profile_pic',
'locale',
'timezone',
'gender',
],
});

expect(res).toEqual(reply);
});
Expand Down

0 comments on commit 30cf2b6

Please sign in to comment.