1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | exports.Service = exports.DEFAULT_PROJECT_ID_TOKEN = void 0;
|
17 |
|
18 |
|
19 |
|
20 | const arrify = require("arrify");
|
21 | const extend = require("extend");
|
22 | const util_1 = require("./util");
|
23 | exports.DEFAULT_PROJECT_ID_TOKEN = '{{projectId}}';
|
24 | class Service {
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | constructor(config, options = {}) {
|
41 | this.baseUrl = config.baseUrl;
|
42 | this.apiEndpoint = config.apiEndpoint;
|
43 | this.timeout = options.timeout;
|
44 | this.globalInterceptors = arrify(options.interceptors_);
|
45 | this.interceptors = [];
|
46 | this.packageJson = config.packageJson;
|
47 | this.projectId = options.projectId || exports.DEFAULT_PROJECT_ID_TOKEN;
|
48 | this.projectIdRequired = config.projectIdRequired !== false;
|
49 | this.providedUserAgent = options.userAgent;
|
50 | const reqCfg = extend({}, config, {
|
51 | projectIdRequired: this.projectIdRequired,
|
52 | projectId: this.projectId,
|
53 | authClient: options.authClient,
|
54 | credentials: options.credentials,
|
55 | keyFile: options.keyFilename,
|
56 | email: options.email,
|
57 | token: options.token,
|
58 | });
|
59 | this.makeAuthenticatedRequest =
|
60 | util_1.util.makeAuthenticatedRequestFactory(reqCfg);
|
61 | this.authClient = this.makeAuthenticatedRequest.authClient;
|
62 | this.getCredentials = this.makeAuthenticatedRequest.getCredentials;
|
63 | const isCloudFunctionEnv = !!process.env.FUNCTION_NAME;
|
64 | if (isCloudFunctionEnv) {
|
65 | this.interceptors.push({
|
66 | request(reqOpts) {
|
67 | reqOpts.forever = false;
|
68 | return reqOpts;
|
69 | },
|
70 | });
|
71 | }
|
72 | }
|
73 | |
74 |
|
75 |
|
76 | getRequestInterceptors() {
|
77 |
|
78 | return [].slice
|
79 | .call(this.globalInterceptors)
|
80 | .concat(this.interceptors)
|
81 | .filter(interceptor => typeof interceptor.request === 'function')
|
82 | .map(interceptor => interceptor.request);
|
83 | }
|
84 | getProjectId(callback) {
|
85 | if (!callback) {
|
86 | return this.getProjectIdAsync();
|
87 | }
|
88 | this.getProjectIdAsync().then(p => callback(null, p), callback);
|
89 | }
|
90 | async getProjectIdAsync() {
|
91 | const projectId = await this.authClient.getProjectId();
|
92 | if (this.projectId === exports.DEFAULT_PROJECT_ID_TOKEN && projectId) {
|
93 | this.projectId = projectId;
|
94 | }
|
95 | return this.projectId;
|
96 | }
|
97 | request_(reqOpts, callback) {
|
98 | reqOpts = extend(true, {}, reqOpts, { timeout: this.timeout });
|
99 | const isAbsoluteUrl = reqOpts.uri.indexOf('http') === 0;
|
100 | const uriComponents = [this.baseUrl];
|
101 | if (this.projectIdRequired) {
|
102 | if (reqOpts.projectId) {
|
103 | uriComponents.push('projects');
|
104 | uriComponents.push(reqOpts.projectId);
|
105 | }
|
106 | else {
|
107 | uriComponents.push('projects');
|
108 | uriComponents.push(this.projectId);
|
109 | }
|
110 | }
|
111 | uriComponents.push(reqOpts.uri);
|
112 | if (isAbsoluteUrl) {
|
113 | uriComponents.splice(0, uriComponents.indexOf(reqOpts.uri));
|
114 | }
|
115 | reqOpts.uri = uriComponents
|
116 | .map(uriComponent => {
|
117 | const trimSlashesRegex = /^\/*|\/*$/g;
|
118 | return uriComponent.replace(trimSlashesRegex, '');
|
119 | })
|
120 | .join('/')
|
121 |
|
122 |
|
123 |
|
124 | .replace(/\/:/g, ':');
|
125 | const requestInterceptors = this.getRequestInterceptors();
|
126 | arrify(reqOpts.interceptors_).forEach(interceptor => {
|
127 | if (typeof interceptor.request === 'function') {
|
128 | requestInterceptors.push(interceptor.request);
|
129 | }
|
130 | });
|
131 | requestInterceptors.forEach(requestInterceptor => {
|
132 | reqOpts = requestInterceptor(reqOpts);
|
133 | });
|
134 | delete reqOpts.interceptors_;
|
135 | const pkg = this.packageJson;
|
136 | let userAgent = util_1.util.getUserAgentFromPackageJson(pkg);
|
137 | if (this.providedUserAgent) {
|
138 | userAgent = `${this.providedUserAgent} ${userAgent}`;
|
139 | }
|
140 | reqOpts.headers = extend({}, reqOpts.headers, {
|
141 | 'User-Agent': userAgent,
|
142 | 'x-goog-api-client': `gl-node/${process.versions.node} gccl/${pkg.version}`,
|
143 | });
|
144 | if (reqOpts.shouldReturnStream) {
|
145 | return this.makeAuthenticatedRequest(reqOpts);
|
146 | }
|
147 | else {
|
148 | this.makeAuthenticatedRequest(reqOpts, callback);
|
149 | }
|
150 | }
|
151 | |
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | request(reqOpts, callback) {
|
159 | Service.prototype.request_.call(this, reqOpts, callback);
|
160 | }
|
161 | |
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | requestStream(reqOpts) {
|
168 | const opts = extend(true, reqOpts, { shouldReturnStream: true });
|
169 | return Service.prototype.request_.call(this, opts);
|
170 | }
|
171 | }
|
172 | exports.Service = Service;
|
173 |
|
\ | No newline at end of file |