UNPKG

4.47 kBTypeScriptView Raw
1/*!
2 * Copyright 2022 Google LLC. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { AuthClient, GoogleAuth, GoogleAuthOptions } from 'google-auth-library';
17import * as r from 'teeny-request';
18import { Interceptor } from './service-object.js';
19import { BodyResponseCallback, DecorateRequestOptions, MakeAuthenticatedRequest, PackageJson } from './util.js';
20export declare const DEFAULT_PROJECT_ID_TOKEN = "{{projectId}}";
21export interface StreamRequestOptions extends DecorateRequestOptions {
22 shouldReturnStream: true;
23}
24export interface ServiceConfig {
25 /**
26 * The base URL to make API requests to.
27 */
28 baseUrl: string;
29 /**
30 * The API Endpoint to use when connecting to the service.
31 * Example: storage.googleapis.com
32 */
33 apiEndpoint: string;
34 /**
35 * The scopes required for the request.
36 */
37 scopes: string[];
38 projectIdRequired?: boolean;
39 packageJson: PackageJson;
40 /**
41 * Reuse an existing `AuthClient` or `GoogleAuth` client instead of creating a new one.
42 */
43 authClient?: AuthClient | GoogleAuth;
44 /**
45 * Set to true if the endpoint is a custom URL
46 */
47 customEndpoint?: boolean;
48}
49export interface ServiceOptions extends Omit<GoogleAuthOptions, 'authClient'> {
50 authClient?: AuthClient | GoogleAuth;
51 interceptors_?: Interceptor[];
52 email?: string;
53 token?: string;
54 timeout?: number;
55 userAgent?: string;
56 useAuthWithCustomEndpoint?: boolean;
57}
58export declare class Service {
59 baseUrl: string;
60 private globalInterceptors;
61 interceptors: Interceptor[];
62 private packageJson;
63 projectId: string;
64 private projectIdRequired;
65 providedUserAgent?: string;
66 makeAuthenticatedRequest: MakeAuthenticatedRequest;
67 authClient: GoogleAuth<AuthClient>;
68 apiEndpoint: string;
69 timeout?: number;
70 universeDomain: string;
71 customEndpoint: boolean;
72 /**
73 * Service is a base class, meant to be inherited from by a "service," like
74 * BigQuery or Storage.
75 *
76 * This handles making authenticated requests by exposing a `makeReq_`
77 * function.
78 *
79 * @constructor
80 * @alias module:common/service
81 *
82 * @param {object} config - Configuration object.
83 * @param {string} config.baseUrl - The base URL to make API requests to.
84 * @param {string[]} config.scopes - The scopes required for the request.
85 * @param {object=} options - [Configuration object](#/docs).
86 */
87 constructor(config: ServiceConfig, options?: ServiceOptions);
88 /**
89 * Return the user's custom request interceptors.
90 */
91 getRequestInterceptors(): Function[];
92 /**
93 * Get and update the Service's project ID.
94 *
95 * @param {function} callback - The callback function.
96 */
97 getProjectId(): Promise<string>;
98 getProjectId(callback: (err: Error | null, projectId?: string) => void): void;
99 protected getProjectIdAsync(): Promise<string>;
100 /**
101 * Make an authenticated API request.
102 *
103 * @private
104 *
105 * @param {object} reqOpts - Request options that are passed to `request`.
106 * @param {string} reqOpts.uri - A URI relative to the baseUrl.
107 * @param {function} callback - The callback function passed to `request`.
108 */
109 private request_;
110 /**
111 * Make an authenticated API request.
112 *
113 * @param {object} reqOpts - Request options that are passed to `request`.
114 * @param {string} reqOpts.uri - A URI relative to the baseUrl.
115 * @param {function} callback - The callback function passed to `request`.
116 */
117 request(reqOpts: DecorateRequestOptions, callback: BodyResponseCallback): void;
118 /**
119 * Make an authenticated API request.
120 *
121 * @param {object} reqOpts - Request options that are passed to `request`.
122 * @param {string} reqOpts.uri - A URI relative to the baseUrl.
123 */
124 requestStream(reqOpts: DecorateRequestOptions): r.Request;
125}