UNPKG

@google-cloud/storage-control

Version:
187 lines (186 loc) 9.05 kB
"use strict"; // Copyright 2026 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. Object.defineProperty(exports, "__esModule", { value: true }); exports.LICENSE = exports.FILES = exports.CLIENTS = void 0; exports.buildOutput = buildOutput; const prettier = require("prettier"); const ast_utils_1 = require("./ast_utils"); exports.CLIENTS = ['StorageControlInternal', 'StorageClient']; exports.FILES = [ '../v2/storage_control_client.ts', '../v2/storage_client.ts', ]; exports.LICENSE = ` // Copyright 2026 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // ** This file is automatically generated ** // ** All changes to this file may be overwritten. ** `; /** * This function creates the import statements for the uber client. * * @param {string[]} clients - A list of client names. * @returns {string} The import statements. */ function makeImports(clients) { let imports = `import {protos} from './'; import {StorageControlClient as StorageControlInternal} from './v2/storage_control_client'; import {StorageClient} from './v2/storage_client';`; const staticImports = ` import type * as gax from "google-gax"; import {Callback, CallOptions, ClientOptions, PaginationCallback, LROperation, Descriptors} from "google-gax"; import {Transform} from 'stream'; `; imports = imports.concat(`${staticImports}\n\n`); return imports; } /** * This function builds the option types for the uber client. * * @param {string[]} clients - A list of client names. * @returns {string} The option types. */ function buildOptionTypes(clients) { let output = ''; const docstring = `/** * Options passed to the underlying client. * * @param {object} [options] - The configuration object. * The options accepted by the constructor are described in detail * in [this document](https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#creating-the-client-instance). * The common options are: * @param {object} [options.credentials] - Credentials object. * @param {string} [options.credentials.client_email] * @param {string} [options.credentials.private_key] * @param {string} [options.email] - Account email address. Required when * using a .pem or .p12 keyFilename. * @param {string} [options.keyFilename] - Full path to the a .json, .pem, or * .p12 key downloaded from the Google Developers Console. If you provide * a path to a JSON file, the projectId option below is not necessary. * NOTE: .pem and .p12 require you to specify options.email as well. * @param {number} [options.port] - The port on which to connect to * the remote host. * @param {string} [options.projectId] - The project ID from the Google * Developer's Console, e.g. 'grape-spaceship-123'. We will also check * the environment variable GCLOUD_PROJECT for your project ID. If your * app is running in an environment which supports * {@link https://cloud.google.com/docs/authentication/application-default-credentials Application Default Credentials}, * your project ID will be detected automatically. * @param {string} [options.apiEndpoint] - The domain name of the * API remote host. * @param {gax.ClientConfig} [options.clientConfig] - Client configuration override. * Follows the structure of {@link gapicConfig}. * @param {boolean} [options.fallback] - Use HTTP/1.1 REST mode. * For more information, please check the * {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}. * @param {gax} [gaxInstance]: loaded instance of \`google-gax\`. Useful if you * need to avoid loading the default gRPC version and want to use the fallback * HTTP implementation. Load only fallback version and pass it to the constructor: * \`\`\` * const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC * const client = new StorageControlClient({fallback: true}, gax); * \`\`\` */\n`; output = output.concat(docstring); let storageControlOptionsType = 'export interface StorageControlClientOptions extends ClientOptions {\n'; storageControlOptionsType = storageControlOptionsType.concat('gaxInstance?: typeof gax | typeof gax.fallback;\n'); for (const client in clients) { let variableDecl = ''; const clientName = (0, ast_utils_1.parseClientName)(clients[client]); variableDecl = variableDecl.concat(`\t ${clientName}?: ${clients[client]};\n`); storageControlOptionsType = storageControlOptionsType.concat(variableDecl); } storageControlOptionsType = storageControlOptionsType.concat('}\n\n'); output = output.concat(storageControlOptionsType); return output; } /** * This function builds the client constructor for the uber client. * * @param {string[]} clients - A list of client names. * @returns {string} The client constructor. */ function buildClientConstructor(clients) { let variableDecl = ''; const comment = `\t/** * @param {object} [StorageControlClientOptions] - Enables user to instantiate clients separately and use those as the subclients. * To have sub-clients with different options, instantiate each client separately. */`; let constructorInitializers = `\tconstructor(options?: StorageControlClientOptions){ options = options || {};\n`; let clientCounter = 0; for (const client in clients) { const clientName = (0, ast_utils_1.parseClientName)(clients[client]); variableDecl = variableDecl.concat(`\t${clientName}: ${clients[client]};\n`); constructorInitializers = constructorInitializers.concat(`\t\tthis.${clientName} = options?.${clientName} ?? new ${clients[client]}(options);\n`); // add statement about auth only after the first subClient if (clientCounter === 0) { constructorInitializers = constructorInitializers.concat(`\n\t\t// utilize whatever auth was created with the first client for the rest of the clients // this will either be what the user passed into options.auth, or whatever was // initialized by default in gax. We reuse this auth rather than instantiating a default ourselves // so that we do not have to keep this code in sync with gax options.auth = this.${clientName}.auth;\n\n`); clientCounter++; } } const properties = (0, ast_utils_1.getPropertyDeclarations)(exports.FILES[0]); properties.forEach((property, key) => { constructorInitializers = constructorInitializers.concat(`\t\tthis.${key} = this.storageControlInternal.${key};\n`); }); constructorInitializers = constructorInitializers.concat('\t}'); let output = 'export class StorageControlClient{\n'; properties.forEach(property => { variableDecl = variableDecl.concat(property); }); output = output.concat(variableDecl, '\n', comment, '\n', constructorInitializers); return output; } /** * This function builds the output for the uber client. * It first adds the components that don't come from underlying files * (imports, exported types, docstring w/ client constructor), then * traverses the file inputs and adds the functions from them. * * @returns {Promise<string>} The formatted output. */ async function buildOutput() { console.log('Regenerating storage_control_client.ts'); let output = ''; output = exports.LICENSE.concat(makeImports(exports.CLIENTS)); output = output.concat(buildOptionTypes(exports.CLIENTS)); output = output.concat(buildClientConstructor(exports.CLIENTS)); output = output.concat((0, ast_utils_1.astHelper)(exports.FILES, exports.CLIENTS)); output = output.concat('}\n'); return prettier.format(output, { parser: 'typescript', trailingComma: 'all', singleQuote: true, bracketSpacing: false, }); } //# sourceMappingURL=uber_client_builder.js.map