Skip to content

Commit 61d6fbd

Browse files
fix(people): resolve type errors in quickstart
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`.
1 parent 275a497 commit 61d6fbd

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

people/quickstart/quickstart.gs

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,51 @@
1414
* limitations under the License.
1515
*/
1616
// [START people_quickstart]
17+
/**
18+
* @typedef {Object} EmailAddress
19+
* @property {string} value
20+
*/
21+
22+
/**
23+
* @typedef {Object} Name
24+
* @property {string} displayName
25+
*/
26+
27+
/**
28+
* @typedef {Object} Person
29+
* @property {Name[]} names
30+
* @property {EmailAddress[]} [emailAddresses]
31+
*/
32+
33+
/**
34+
* @typedef {Object} Connection
35+
* @property {Person[]} connections
36+
*/
37+
1738
/**
1839
* Print the display name if available for 10 connections.
1940
*/
2041
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);
42+
// Poll the People API to list the connections of the logged in user.
43+
// See: https://developers.google.com/people/api/rest/v1/people.connections/list
44+
if (!People || !People.People || !People.People.Connections) {
45+
// See: https://developers.google.com/apps-script/guides/services/advanced#enable_advanced_services
46+
throw new Error('People service not enabled.');
4247
}
48+
const connections = People.People.Connections.list('people/me', {
49+
pageSize: 10,
50+
personFields: 'names,emailAddresses',
51+
});
52+
if (!connections.connections) {
53+
console.log('No connections found.');
54+
return;
55+
}
56+
connections.connections.forEach((person) => {
57+
if (person.names && person.names.length > 0 && person.names[0].displayName) {
58+
console.log(person.names[0].displayName);
59+
} else {
60+
console.log('No display name found for connection.');
61+
}
62+
});
4363
}
4464
// [END people_quickstart]

0 commit comments

Comments
 (0)