Skip to content

Commit 875f561

Browse files
fix(people): resolve type errors (#585)
Resolves TypeScript errors in the `people/quickstart/quickstart.gs` file by: - Adding JSDoc annotations for `Name`, `Person`, `Connection` and `EmailAddress` types. - Adding a check to ensure the `People` advanced service is enabled. - Removing an unhelpful try/catch block. - Improving the logic to safely access `displayName`. * docs: Add JSDoc links to People API definitions and clarify partial type definitions. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: Justin Poehnelt <jpoehnelt@google.com>
1 parent 83b1049 commit 875f561

File tree

1 file changed

+48
-21
lines changed

1 file changed

+48
-21
lines changed

people/quickstart/quickstart.gs

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,58 @@
1414
* limitations under the License.
1515
*/
1616
// [START people_quickstart]
17+
/**
18+
* @typedef {Object} EmailAddress
19+
* @see https://developers.google.com/people/api/rest/v1/people#Person
20+
* @property {string} value
21+
* Note: This is a partial definition.
22+
*/
23+
24+
/**
25+
* @typedef {Object} Name
26+
* @see https://developers.google.com/people/api/rest/v1/people#Person
27+
* @property {string} displayName
28+
* Note: This is a partial definition.
29+
*/
30+
31+
/**
32+
* @typedef {Object} Person
33+
* @see https://developers.google.com/people/api/rest/v1/people#Person
34+
* @property {Name[]} names
35+
* @property {EmailAddress[]} [emailAddresses]
36+
* Note: This is a partial definition.
37+
*/
38+
39+
/**
40+
* @typedef {Object} Connection
41+
* @see https://developers.google.com/people/api/rest/v1/people.connections/list
42+
* @property {Person[]} connections
43+
* Note: This is a partial definition.
44+
*/
45+
1746
/**
1847
* Print the display name if available for 10 connections.
1948
*/
2049
function listConnectionNames() {
21-
try {
22-
/**
23-
* List the 10 connections/contacts of user
24-
* @see https://developers.google.com/people/api/rest/v1/people.connections/list
25-
*/
26-
const connections = People.People.Connections.list('people/me', {
27-
pageSize: 10,
28-
personFields: 'names,emailAddresses'
29-
// use other query parameter here if needed.
30-
});
31-
connections.connections.forEach((person) => {
32-
// if contacts/connections is available, print the name of person.
33-
if (person.names && person.names.length === 0) {
34-
console.log('No display name found for connection.');
35-
return;
36-
}
37-
console.log(person.names[0].displayName);
38-
});
39-
} catch (err) {
40-
// TODO (developer) - Handle exception from People API
41-
console.log('Failed with error %s', err.message);
50+
// Use the People API to list the connections of the logged in user.
51+
// See: https://developers.google.com/people/api/rest/v1/people.connections/list
52+
if (!People || !People.People || !People.People.Connections) {
53+
throw new Error('People service not enabled.');
4254
}
55+
const connections = People.People.Connections.list('people/me', {
56+
pageSize: 10,
57+
personFields: 'names,emailAddresses',
58+
});
59+
if (!connections.connections) {
60+
console.log('No connections found.');
61+
return;
62+
}
63+
connections.connections.forEach((person) => {
64+
if (person.names && person.names.length > 0 && person.names[0].displayName) {
65+
console.log(person.names[0].displayName);
66+
} else {
67+
console.log('No display name found for connection.');
68+
}
69+
});
4370
}
4471
// [END people_quickstart]

0 commit comments

Comments
 (0)