1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ServiceObject = void 0;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | const promisify_1 = require("@google-cloud/promisify");
|
20 | const events_1 = require("events");
|
21 | const util_js_1 = require("./util.js");
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | class ServiceObject extends events_1.EventEmitter {
|
35 | |
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | constructor(config) {
|
54 | super();
|
55 | this.metadata = {};
|
56 | this.baseUrl = config.baseUrl;
|
57 | this.parent = config.parent;
|
58 | this.id = config.id;
|
59 | this.createMethod = config.createMethod;
|
60 | this.methods = config.methods || {};
|
61 | this.interceptors = [];
|
62 | this.projectId = config.projectId;
|
63 | if (config.methods) {
|
64 |
|
65 |
|
66 |
|
67 | Object.getOwnPropertyNames(ServiceObject.prototype)
|
68 | .filter(methodName => {
|
69 | return (
|
70 |
|
71 |
|
72 | !/^request/.test(methodName) &&
|
73 | !/^getRequestInterceptors/.test(methodName) &&
|
74 |
|
75 |
|
76 |
|
77 | this[methodName] ===
|
78 |
|
79 | ServiceObject.prototype[methodName] &&
|
80 |
|
81 | !config.methods[methodName]);
|
82 | })
|
83 | .forEach(methodName => {
|
84 |
|
85 | this[methodName] = undefined;
|
86 | });
|
87 | }
|
88 | }
|
89 | create(optionsOrCallback, callback) {
|
90 |
|
91 | const self = this;
|
92 | const args = [this.id];
|
93 | if (typeof optionsOrCallback === 'function') {
|
94 | callback = optionsOrCallback;
|
95 | }
|
96 | if (typeof optionsOrCallback === 'object') {
|
97 | args.push(optionsOrCallback);
|
98 | }
|
99 |
|
100 |
|
101 |
|
102 | function onCreate(...args) {
|
103 | const [err, instance] = args;
|
104 | if (!err) {
|
105 | self.metadata = instance.metadata;
|
106 | if (self.id && instance.metadata) {
|
107 | self.id = instance.metadata.id;
|
108 | }
|
109 | args[1] = self;
|
110 | }
|
111 | callback(...args);
|
112 | }
|
113 | args.push(onCreate);
|
114 |
|
115 | this.createMethod.apply(null, args);
|
116 | }
|
117 | delete(optionsOrCallback, cb) {
|
118 | var _a;
|
119 | const [options, callback] = util_js_1.util.maybeOptionsOrCallback(optionsOrCallback, cb);
|
120 | const ignoreNotFound = options.ignoreNotFound;
|
121 | delete options.ignoreNotFound;
|
122 | const methodConfig = (typeof this.methods.delete === 'object' && this.methods.delete) || {};
|
123 | const reqOpts = {
|
124 | method: 'DELETE',
|
125 | uri: '',
|
126 | ...methodConfig.reqOpts,
|
127 | qs: {
|
128 | ...(_a = methodConfig.reqOpts) === null || _a === void 0 ? void 0 : _a.qs,
|
129 | ...options,
|
130 | },
|
131 | };
|
132 |
|
133 |
|
134 | ServiceObject.prototype.request.call(this, reqOpts, (err, body, res) => {
|
135 | if (err) {
|
136 | if (err.code === 404 && ignoreNotFound) {
|
137 | err = null;
|
138 | }
|
139 | }
|
140 | callback(err, res);
|
141 | });
|
142 | }
|
143 | exists(optionsOrCallback, cb) {
|
144 | const [options, callback] = util_js_1.util.maybeOptionsOrCallback(optionsOrCallback, cb);
|
145 | this.get(options, err => {
|
146 | if (err) {
|
147 | if (err.code === 404) {
|
148 | callback(null, false);
|
149 | }
|
150 | else {
|
151 | callback(err);
|
152 | }
|
153 | return;
|
154 | }
|
155 | callback(null, true);
|
156 | });
|
157 | }
|
158 | get(optionsOrCallback, cb) {
|
159 |
|
160 | const self = this;
|
161 | const [opts, callback] = util_js_1.util.maybeOptionsOrCallback(optionsOrCallback, cb);
|
162 | const options = Object.assign({}, opts);
|
163 | const autoCreate = options.autoCreate && typeof this.create === 'function';
|
164 | delete options.autoCreate;
|
165 | function onCreate(err, instance, apiResponse) {
|
166 | if (err) {
|
167 | if (err.code === 409) {
|
168 | self.get(options, callback);
|
169 | return;
|
170 | }
|
171 | callback(err, null, apiResponse);
|
172 | return;
|
173 | }
|
174 | callback(null, instance, apiResponse);
|
175 | }
|
176 | this.getMetadata(options, (err, metadata) => {
|
177 | if (err) {
|
178 | if (err.code === 404 && autoCreate) {
|
179 | const args = [];
|
180 | if (Object.keys(options).length > 0) {
|
181 | args.push(options);
|
182 | }
|
183 | args.push(onCreate);
|
184 | self.create(...args);
|
185 | return;
|
186 | }
|
187 | callback(err, null, metadata);
|
188 | return;
|
189 | }
|
190 | callback(null, self, metadata);
|
191 | });
|
192 | }
|
193 | getMetadata(optionsOrCallback, cb) {
|
194 | var _a;
|
195 | const [options, callback] = util_js_1.util.maybeOptionsOrCallback(optionsOrCallback, cb);
|
196 | const methodConfig = (typeof this.methods.getMetadata === 'object' &&
|
197 | this.methods.getMetadata) ||
|
198 | {};
|
199 | const reqOpts = {
|
200 | uri: '',
|
201 | ...methodConfig.reqOpts,
|
202 | qs: {
|
203 | ...(_a = methodConfig.reqOpts) === null || _a === void 0 ? void 0 : _a.qs,
|
204 | ...options,
|
205 | },
|
206 | };
|
207 |
|
208 |
|
209 | ServiceObject.prototype.request.call(this, reqOpts, (err, body, res) => {
|
210 | this.metadata = body;
|
211 | callback(err, this.metadata, res);
|
212 | });
|
213 | }
|
214 | |
215 |
|
216 |
|
217 | getRequestInterceptors() {
|
218 |
|
219 | const localInterceptors = this.interceptors
|
220 | .filter(interceptor => typeof interceptor.request === 'function')
|
221 | .map(interceptor => interceptor.request);
|
222 | return this.parent.getRequestInterceptors().concat(localInterceptors);
|
223 | }
|
224 | setMetadata(metadata, optionsOrCallback, cb) {
|
225 | var _a, _b;
|
226 | const [options, callback] = util_js_1.util.maybeOptionsOrCallback(optionsOrCallback, cb);
|
227 | const methodConfig = (typeof this.methods.setMetadata === 'object' &&
|
228 | this.methods.setMetadata) ||
|
229 | {};
|
230 | const reqOpts = {
|
231 | method: 'PATCH',
|
232 | uri: '',
|
233 | ...methodConfig.reqOpts,
|
234 | json: {
|
235 | ...(_a = methodConfig.reqOpts) === null || _a === void 0 ? void 0 : _a.json,
|
236 | ...metadata,
|
237 | },
|
238 | qs: {
|
239 | ...(_b = methodConfig.reqOpts) === null || _b === void 0 ? void 0 : _b.qs,
|
240 | ...options,
|
241 | },
|
242 | };
|
243 |
|
244 |
|
245 | ServiceObject.prototype.request.call(this, reqOpts, (err, body, res) => {
|
246 | this.metadata = body;
|
247 | callback(err, this.metadata, res);
|
248 | });
|
249 | }
|
250 | request_(reqOpts, callback) {
|
251 | reqOpts = { ...reqOpts };
|
252 | if (this.projectId) {
|
253 | reqOpts.projectId = this.projectId;
|
254 | }
|
255 | const isAbsoluteUrl = reqOpts.uri.indexOf('http') === 0;
|
256 | const uriComponents = [this.baseUrl, this.id || '', reqOpts.uri];
|
257 | if (isAbsoluteUrl) {
|
258 | uriComponents.splice(0, uriComponents.indexOf(reqOpts.uri));
|
259 | }
|
260 | reqOpts.uri = uriComponents
|
261 | .filter(x => x.trim())
|
262 | .map(uriComponent => {
|
263 | const trimSlashesRegex = /^\/*|\/*$/g;
|
264 | return uriComponent.replace(trimSlashesRegex, '');
|
265 | })
|
266 | .join('/');
|
267 | const childInterceptors = Array.isArray(reqOpts.interceptors_)
|
268 | ? reqOpts.interceptors_
|
269 | : [];
|
270 | const localInterceptors = [].slice.call(this.interceptors);
|
271 | reqOpts.interceptors_ = childInterceptors.concat(localInterceptors);
|
272 | if (reqOpts.shouldReturnStream) {
|
273 | return this.parent.requestStream(reqOpts);
|
274 | }
|
275 | this.parent.request(reqOpts, callback);
|
276 | }
|
277 | request(reqOpts, callback) {
|
278 | this.request_(reqOpts, callback);
|
279 | }
|
280 | |
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 | requestStream(reqOpts) {
|
287 | const opts = { ...reqOpts, shouldReturnStream: true };
|
288 | return this.request_(opts);
|
289 | }
|
290 | }
|
291 | exports.ServiceObject = ServiceObject;
|
292 | (0, promisify_1.promisifyAll)(ServiceObject, { exclude: ['getRequestInterceptors'] });
|