Skip to content

Commit

Permalink
Merge pull request #484 from Maxim-Mazurok/update-apis
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim-Mazurok authored Apr 28, 2021
2 parents 20e19f5 + 432a3ad commit 023c000
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 4 deletions.
106 changes: 106 additions & 0 deletions bin/apis-sync-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import {getProxySettings} from 'get-proxy-settings';
import _ from 'lodash';
import {request} from '../src/utils';
import fs from 'fs';
import {excludedApis} from '../src/app';
type DirectoryList = gapi.client.discovery.DirectoryList;

const prefix = '@maxim_mazurok/gapi.client.';
const pathToConfig =
'/home/maxim/google-api-typings-generator/bin/auto-publish/config.ts';
const pathToLocalAllowedPackageJsonDependencies =
'/home/maxim/DefinitelyTyped-tools/packages/definitions-parser/allowedPackageJsonDependencies.txt';

const getProxy = async () => {
const proxy = await getProxySettings();
return proxy ? proxy.https || proxy.http : undefined;
};

const updateSupportedApis = (discoveryTypes: string[]) => {
const newConfig = [
'export const supportedApis = [',
...discoveryTypes.map(x => ` '${x}',`),
'];',
'',
];
fs.writeFileSync(pathToConfig, newConfig.join('\n'));
};

const updateLocalAllowedPackageJsonDependencies = (
discoveryTypes: string[]
) => {
const localAllowedPackageJsonDependencies = fs
.readFileSync(pathToLocalAllowedPackageJsonDependencies, {
encoding: 'utf-8',
})
.split('\n');

const firstIndex = localAllowedPackageJsonDependencies.findIndex(x =>
x.startsWith(prefix)
);

const newLocalAllowedPackageJsonDependencies = localAllowedPackageJsonDependencies.filter(
x => !x.startsWith(prefix)
);

newLocalAllowedPackageJsonDependencies.splice(
firstIndex,
0,
...discoveryTypes.map(x => `${prefix}${x}`)
);
fs.writeFileSync(
pathToLocalAllowedPackageJsonDependencies,
newLocalAllowedPackageJsonDependencies.join('\n')
);
};

const listDiscoveryTypes = async () => {
const list = await request<DirectoryList>(
'https://www.googleapis.com/discovery/v1/apis',
await getProxy()
);

return _.uniq(
list.items
?.filter(x => x.name !== undefined)
.map(x => (x.name || '').toLocaleLowerCase())
.filter(x => !excludedApis.includes(x))
).sort();
};

const listAllowedPackageJsonDependencies = async () => {
const list = await request<string>(
'https://raw.githubusercontent.com/microsoft/DefinitelyTyped-tools/master/packages/definitions-parser/allowedPackageJsonDependencies.txt',
await getProxy(),
'text'
);
return _.uniq(
list
.split('\n')
.filter(x => x.startsWith(prefix))
.map(x => x.replace(prefix, '').toLocaleLowerCase())
.filter(x => !excludedApis.includes(x))
).sort();
};

(async () => {
const discoveryTypes = await listDiscoveryTypes();
const allowedPackageJsonDependencies = await listAllowedPackageJsonDependencies();

const discoveryTypesNotPresentInAllowedPackageJsonDependencies = discoveryTypes.filter(
x => !allowedPackageJsonDependencies.includes(x)
);

console.log({discoveryTypesNotPresentInAllowedPackageJsonDependencies});

if (discoveryTypesNotPresentInAllowedPackageJsonDependencies.length !== 0) {
updateLocalAllowedPackageJsonDependencies(discoveryTypes);
updateSupportedApis(discoveryTypes);
// todo: open PR
}

const allowedPackageJsonDependenciesNotPresentInDiscoveryTypes = allowedPackageJsonDependencies.filter(
x => !discoveryTypes.includes(x)
);
console.log({allowedPackageJsonDependenciesNotPresentInDiscoveryTypes});
})();
17 changes: 17 additions & 0 deletions bin/auto-publish/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const supportedApis = [
'androidmanagement',
'androidpublisher',
'apigateway',
'apikeys',
'appengine',
'area120tables',
'artifactregistry',
Expand All @@ -35,12 +36,15 @@ export const supportedApis = [
'books',
'calendar',
'chat',
'chromemanagement',
'chromepolicy',
'chromeuxreport',
'civicinfo',
'classroom',
'cloudasset',
'cloudbilling',
'cloudbuild',
'cloudchannel',
'clouddebugger',
'clouderrorreporting',
'cloudfunctions',
Expand Down Expand Up @@ -76,12 +80,14 @@ export const supportedApis = [
'dlp',
'dns',
'docs',
'documentai',
'domains',
'domainsrdap',
'doubleclickbidmanager',
'doubleclicksearch',
'drive',
'driveactivity',
'essentialcontacts',
'eventarc',
'factchecktools',
'fcm',
Expand All @@ -92,13 +98,15 @@ export const supportedApis = [
'firebasehosting',
'firebaseml',
'firebaserules',
'firebasestorage',
'firestore',
'fitness',
'games',
'gamesconfiguration',
'gameservices',
'gamesmanagement',
'genomics',
'gkehub',
'gmail',
'gmailpostmastertools',
'groupsmigration',
Expand All @@ -121,17 +129,24 @@ export const supportedApis = [
'managedidentities',
'manufacturers',
'memcache',
'metastore',
'ml',
'monitoring',
'mybusinessaccountmanagement',
'mybusinesslodging',
'networkconnectivity',
'networkmanagement',
'notebooks',
'oauth2',
'ondemandscanning',
'orgpolicy',
'osconfig',
'oslogin',
'pagespeedonline',
'people',
'playablelocations',
'playcustomapp',
'policysimulator',
'policytroubleshooter',
'poly',
'privateca',
Expand All @@ -144,6 +159,7 @@ export const supportedApis = [
'redis',
'remotebuildexecution',
'reseller',
'retail',
'run',
'runtimeconfig',
'safebrowsing',
Expand Down Expand Up @@ -186,6 +202,7 @@ export const supportedApis = [
'vision',
'webfonts',
'webmasters',
'webrisk',
'websecurityscanner',
'workflowexecutions',
'workflows',
Expand Down
10 changes: 6 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,16 @@ export async function getBannedTypes(): Promise<string[]> {
return options.length || options[0].length ? options.map(x => x[0]) : [];
}

export async function request<T extends object>(
export async function request<T extends object | string>(
url: string,
proxy: ProxySetting | undefined
proxy: ProxySetting | undefined,
responseType: 'json' | 'text' = 'json'
): Promise<T> {
const protocol = new URL(url).protocol as 'http:' | 'https:';
const agentProtocol = protocol === 'http:' ? Protocol.Http : Protocol.Https;
const agent =
agentProtocol === Protocol.Http ? HttpProxyAgent : HttpsProxyAgent;
return (await got(url, {
const response = got(url, {
...(proxy
? {
agent: {
Expand All @@ -100,7 +101,8 @@ export async function request<T extends object>(
},
}
: {}),
}).json()) as T;
});
return (await response[responseType]()) as T;
}

/**
Expand Down

0 comments on commit 023c000

Please sign in to comment.