UNPKG

4.24 kBJavaScriptView Raw
1"use strict";
2// Copyright 2019 Google LLC
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.
15Object.defineProperty(exports, "__esModule", { value: true });
16exports.DefaultTransporter = void 0;
17const gaxios_1 = require("gaxios");
18const options_1 = require("./options");
19// eslint-disable-next-line @typescript-eslint/no-var-requires
20const pkg = require('../../package.json');
21const PRODUCT_NAME = 'google-api-nodejs-client';
22class DefaultTransporter {
23 /**
24 * Configures request options before making a request.
25 * @param opts GaxiosOptions options.
26 * @return Configured options.
27 */
28 configure(opts = {}) {
29 opts.headers = opts.headers || {};
30 if (typeof window === 'undefined') {
31 // set transporter user agent if not in browser
32 const uaValue = opts.headers['User-Agent'];
33 if (!uaValue) {
34 opts.headers['User-Agent'] = DefaultTransporter.USER_AGENT;
35 }
36 else if (!uaValue.includes(`${PRODUCT_NAME}/`)) {
37 opts.headers['User-Agent'] = `${uaValue} ${DefaultTransporter.USER_AGENT}`;
38 }
39 // track google-auth-library-nodejs version:
40 const authVersion = `auth/${pkg.version}`;
41 if (opts.headers['x-goog-api-client'] &&
42 !opts.headers['x-goog-api-client'].includes(authVersion)) {
43 opts.headers['x-goog-api-client'] = `${opts.headers['x-goog-api-client']} ${authVersion}`;
44 }
45 else if (!opts.headers['x-goog-api-client']) {
46 const nodeVersion = process.version.replace(/^v/, '');
47 opts.headers['x-goog-api-client'] = `gl-node/${nodeVersion} ${authVersion}`;
48 }
49 }
50 return opts;
51 }
52 request(opts, callback) {
53 // ensure the user isn't passing in request-style options
54 opts = this.configure(opts);
55 try {
56 options_1.validate(opts);
57 }
58 catch (e) {
59 if (callback) {
60 return callback(e);
61 }
62 else {
63 throw e;
64 }
65 }
66 if (callback) {
67 gaxios_1.request(opts).then(r => {
68 callback(null, r);
69 }, e => {
70 callback(this.processError(e));
71 });
72 }
73 else {
74 return gaxios_1.request(opts).catch(e => {
75 throw this.processError(e);
76 });
77 }
78 }
79 /**
80 * Changes the error to include details from the body.
81 */
82 processError(e) {
83 const res = e.response;
84 const err = e;
85 const body = res ? res.data : null;
86 if (res && body && body.error && res.status !== 200) {
87 if (typeof body.error === 'string') {
88 err.message = body.error;
89 err.code = res.status.toString();
90 }
91 else if (Array.isArray(body.error.errors)) {
92 err.message = body.error.errors
93 .map((err2) => err2.message)
94 .join('\n');
95 err.code = body.error.code;
96 err.errors = body.error.errors;
97 }
98 else {
99 err.message = body.error.message;
100 err.code = body.error.code || res.status;
101 }
102 }
103 else if (res && res.status >= 400) {
104 // Consider all 4xx and 5xx responses errors.
105 err.message = body;
106 err.code = res.status.toString();
107 }
108 return err;
109 }
110}
111exports.DefaultTransporter = DefaultTransporter;
112/**
113 * Default user agent.
114 */
115DefaultTransporter.USER_AGENT = `${PRODUCT_NAME}/${pkg.version}`;
116//# sourceMappingURL=transporters.js.map
\No newline at end of file