Skip to content

Commit f88f202

Browse files
committed
fix: no typescript errors, more esm modernization, top-level await, no local tokens
1 parent db23f5e commit f88f202

File tree

247 files changed

+1726
-2725
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

247 files changed

+1726
-2725
lines changed

adminSDK/directory/index.js

Lines changed: 12 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -13,99 +13,45 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
/* eslint-disable camelcase */
16+
1717
// [START admin_sdk_directory_quickstart]
18-
import fs from 'fs/promises';
19-
import path from 'path';
20-
import process from 'process';
18+
19+
import path from 'node:path';
20+
import process from 'node:process';
2121
import {authenticate} from '@google-cloud/local-auth';
2222
import {google} from 'googleapis';
2323

24-
// If modifying these scopes, delete token.json.
2524
const SCOPES = ['https://www.googleapis.com/auth/admin.directory.user'];
26-
// The file token.json stores the user's access and refresh tokens, and is
27-
// created automatically when the authorization flow completes for the first
28-
// time.
29-
const TOKEN_PATH = path.join(process.cwd(), 'token.json');
3025
const CREDENTIALS_PATH = path.join(process.cwd(), 'credentials.json');
3126

3227
/**
33-
* Reads previously authorized credentials from the save file.
34-
*
35-
* @return {Promise<import("google-auth-library").AuthClient|null>}
36-
*/
37-
async function loadSavedCredentialsIfExist() {
38-
try {
39-
const content = await fs.readFile(TOKEN_PATH);
40-
const credentials = JSON.parse(content);
41-
return google.auth.fromJSON(credentials);
42-
} catch (err) {
43-
return null;
44-
}
45-
}
46-
47-
/**
48-
* Serializes credentials to a file compatible with GoogleAuth.fromJSON.
49-
*
50-
* @param {import("google-auth-library").AuthClient} client
51-
* @return {Promise<void>}
52-
*/
53-
async function saveCredentials(client) {
54-
const content = await fs.readFile(CREDENTIALS_PATH);
55-
const keys = JSON.parse(content);
56-
const key = keys.installed || keys.web;
57-
const payload = JSON.stringify({
58-
type: 'authorized_user',
59-
client_id: key.client_id,
60-
client_secret: key.client_secret,
61-
refresh_token: client.credentials.refresh_token,
62-
});
63-
await fs.writeFile(TOKEN_PATH, payload);
64-
}
65-
66-
/**
67-
* Load or request or authorization to call APIs.
68-
*
28+
* Lists the first 10 users in the domain.
6929
*/
70-
async function authorize() {
71-
let client = await loadSavedCredentialsIfExist();
72-
if (client) {
73-
return client;
74-
}
75-
client = await authenticate({
30+
async function listUsers() {
31+
const auth = await authenticate({
7632
scopes: SCOPES,
7733
keyfilePath: CREDENTIALS_PATH,
7834
});
79-
if (client.credentials) {
80-
await saveCredentials(client);
81-
}
82-
return client;
83-
}
8435

85-
/**
86-
* Lists the first 10 users in the domain.
87-
*
88-
* @param {import("google-auth-library").AuthClient} auth An authorized OAuth2 client.
89-
*/
90-
async function listUsers(auth) {
9136
const service = google.admin({version: 'directory_v1', auth});
92-
const res = await service.users.list({
37+
const result = await service.users.list({
9338
customer: 'my_customer',
9439
maxResults: 10,
9540
orderBy: 'email',
9641
});
9742

98-
const users = res.data.users;
43+
const users = result.data.users;
9944
if (!users || users.length === 0) {
10045
console.log('No users found.');
10146
return;
10247
}
10348

10449
console.log('Users:');
10550
users.forEach((user) => {
106-
console.log(`${user.primaryEmail} (${user.name.fullName})`);
51+
console.log(`${user.primaryEmail} (${user.name?.fullName})`);
10752
});
10853
}
10954

110-
authorize().then(listUsers).catch(console.error);
55+
await listUsers();
56+
11157
// [END admin_sdk_directory_quickstart]

adminSDK/directory/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"extends": "../../tsconfig.base.json",
33
"include": ["**/*.js"]
4-
}
4+
}

adminSDK/reports/index.js

Lines changed: 11 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,88 +13,32 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
/* eslint-disable camelcase */
16+
1717
// [START admin_sdk_reports_quickstart]
18-
import fs from 'fs/promises';
19-
import path from 'path';
20-
import process from 'process';
18+
19+
import path from 'node:path';
20+
import process from 'node:process';
2121
import {authenticate} from '@google-cloud/local-auth';
2222
import {google} from 'googleapis';
2323

24-
// If modifying these scopes, delete token.json.
2524
const SCOPES = ['https://www.googleapis.com/auth/admin.reports.audit.readonly'];
26-
// The file token.json stores the user's access and refresh tokens, and is
27-
// created automatically when the authorization flow completes for the first
28-
// time.
29-
const TOKEN_PATH = path.join(process.cwd(), 'token.json');
3025
const CREDENTIALS_PATH = path.join(process.cwd(), 'credentials.json');
3126

3227
/**
33-
* Reads previously authorized credentials from the save file.
34-
*
35-
* @return {Promise<import("google-auth-library").AuthClient|null>}
36-
*/
37-
async function loadSavedCredentialsIfExist() {
38-
try {
39-
const content = await fs.readFile(TOKEN_PATH);
40-
const credentials = JSON.parse(content);
41-
return google.auth.fromJSON(credentials);
42-
} catch (err) {
43-
return null;
44-
}
45-
}
46-
47-
/**
48-
* Serializes credentials to a file compatible with GoogleAuth.fromJSON.
49-
*
50-
* @param {import("google-auth-library").AuthClient} client
51-
* @return {Promise<void>}
52-
*/
53-
async function saveCredentials(client) {
54-
const content = await fs.readFile(CREDENTIALS_PATH);
55-
const keys = JSON.parse(content);
56-
const key = keys.installed || keys.web;
57-
const payload = JSON.stringify({
58-
type: 'authorized_user',
59-
client_id: key.client_id,
60-
client_secret: key.client_secret,
61-
refresh_token: client.credentials.refresh_token,
62-
});
63-
await fs.writeFile(TOKEN_PATH, payload);
64-
}
65-
66-
/**
67-
* Load or request or authorization to call APIs.
68-
*
28+
* Lists the last 10 login events for the domain.
6929
*/
70-
async function authorize() {
71-
let client = await loadSavedCredentialsIfExist();
72-
if (client) {
73-
return client;
74-
}
75-
client = await authenticate({
30+
async function listLoginEvents() {
31+
const auth = await authenticate({
7632
scopes: SCOPES,
7733
keyfilePath: CREDENTIALS_PATH,
7834
});
79-
if (client.credentials) {
80-
await saveCredentials(client);
81-
}
82-
return client;
83-
}
84-
85-
/**
86-
* Lists the last 10 login events for the domain.
87-
*
88-
* @param {import("google-auth-library").AuthClient} auth An authorized OAuth2 client.
89-
*/
90-
async function listLoginEvents(auth) {
9135
const service = google.admin({version: 'reports_v1', auth});
92-
const res = await service.activities.list({
36+
const result = await service.activities.list({
9337
userKey: 'all',
9438
applicationName: 'login',
9539
maxResults: 10,
9640
});
97-
const activities = res.data.items;
41+
const activities = result.data.items;
9842
if (!activities || activities.length === 0) {
9943
console.log('No logins found.');
10044
return;
@@ -103,10 +47,10 @@ async function listLoginEvents(auth) {
10347
console.log('Logins:');
10448
activities.forEach((activity) => {
10549
console.log(
106-
`${activity.id.time}: ${activity.actor.email} (${activity.events[0].name})`,
50+
`${activity.id?.time}: ${activity.actor?.email} (${activity.events?.[0]?.name})`,
10751
);
10852
});
10953
}
11054

111-
authorize().then(listLoginEvents).catch(console.error);
55+
await listLoginEvents();
11256
// [END admin_sdk_reports_quickstart]

adminSDK/reports/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"extends": "../../tsconfig.base.json",
33
"include": ["**/*.js"]
4-
}
4+
}

adminSDK/reseller/index.js

Lines changed: 13 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -13,95 +13,42 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
/* eslint-disable camelcase */
16+
1717
// [START admin_sdk_reseller_quickstart]
18-
import fs from 'fs/promises';
19-
import path from 'path';
20-
import process from 'process';
18+
19+
import path from 'node:path';
20+
import process from 'node:process';
2121
import {authenticate} from '@google-cloud/local-auth';
2222
import {google} from 'googleapis';
2323

24-
// If modifying these scopes, delete token.json.
2524
const SCOPES = ['https://www.googleapis.com/auth/apps.order'];
26-
// The file token.json stores the user's access and refresh tokens, and is
27-
// created automatically when the authorization flow completes for the first
28-
// time.
29-
const TOKEN_PATH = path.join(process.cwd(), 'token.json');
3025
const CREDENTIALS_PATH = path.join(process.cwd(), 'credentials.json');
3126

3227
/**
33-
* Reads previously authorized credentials from the save file.
34-
*
35-
* @return {Promise<import("google-auth-library").AuthClient|null>}
36-
*/
37-
async function loadSavedCredentialsIfExist() {
38-
try {
39-
const content = await fs.readFile(TOKEN_PATH);
40-
const credentials = JSON.parse(content);
41-
return google.auth.fromJSON(credentials);
42-
} catch (err) {
43-
return null;
44-
}
45-
}
46-
47-
/**
48-
* Serializes credentials to a file compatible with GoogleAuth.fromJSON.
49-
*
50-
* @param {import("google-auth-library").AuthClient} client
51-
* @return {Promise<void>}
52-
*/
53-
async function saveCredentials(client) {
54-
const content = await fs.readFile(CREDENTIALS_PATH);
55-
const keys = JSON.parse(content);
56-
const key = keys.installed || keys.web;
57-
const payload = JSON.stringify({
58-
type: 'authorized_user',
59-
client_id: key.client_id,
60-
client_secret: key.client_secret,
61-
refresh_token: client.credentials.refresh_token,
62-
});
63-
await fs.writeFile(TOKEN_PATH, payload);
64-
}
65-
66-
/**
67-
* Load or request or authorization to call APIs.
68-
*
28+
* Lists the first 10 subscriptions you manage.
6929
*/
70-
async function authorize() {
71-
let client = await loadSavedCredentialsIfExist();
72-
if (client) {
73-
return client;
74-
}
75-
client = await authenticate({
30+
async function listSubscriptions() {
31+
const auth = await authenticate({
7632
scopes: SCOPES,
7733
keyfilePath: CREDENTIALS_PATH,
7834
});
79-
if (client.credentials) {
80-
await saveCredentials(client);
81-
}
82-
return client;
83-
}
8435

85-
/**
86-
* Lists the first 10 subscriptions you manage.
87-
*
88-
* @param {import("google-auth-library").AuthClient} auth An authorized OAuth2 client.
89-
*/
90-
async function listSubscriptions(auth) {
9136
const service = google.reseller({version: 'v1', auth});
92-
const res = await service.subscriptions.list({
37+
const result = await service.subscriptions.list({
9338
maxResults: 10,
9439
});
95-
const subscriptions = res.data.subscriptions;
40+
const subscriptions = result.data.subscriptions;
9641
if (!subscriptions || subscriptions.length === 0) {
9742
console.log('No subscriptions found.');
9843
return;
9944
}
10045

10146
console.log('Subscriptions:');
10247
subscriptions.forEach(({customerId, skuId, plan}) => {
103-
console.log(`${customerId} (${skuId}, ${plan.planName})`);
48+
console.log(`${customerId} (${skuId}, ${plan?.planName})`);
10449
});
10550
}
106-
authorize().then(listSubscriptions).catch(console.error);
51+
52+
await listSubscriptions();
53+
10754
// [END admin_sdk_reseller_quickstart]

adminSDK/reseller/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"extends": "../../tsconfig.base.json",
33
"include": ["**/*.js"]
4-
}
4+
}

0 commit comments

Comments
 (0)