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.DefaultTransporter = void 0;
|
17 | const gaxios_1 = require("gaxios");
|
18 | const options_1 = require("./options");
|
19 |
|
20 | const pkg = require('../../package.json');
|
21 | const PRODUCT_NAME = 'google-api-nodejs-client';
|
22 | class DefaultTransporter {
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 | configure(opts = {}) {
|
29 | opts.headers = opts.headers || {};
|
30 | if (typeof window === 'undefined') {
|
31 |
|
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 |
|
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 |
|
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 |
|
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 |
|
105 | err.message = body;
|
106 | err.code = res.status.toString();
|
107 | }
|
108 | return err;
|
109 | }
|
110 | }
|
111 | exports.DefaultTransporter = DefaultTransporter;
|
112 |
|
113 |
|
114 |
|
115 | DefaultTransporter.USER_AGENT = `${PRODUCT_NAME}/${pkg.version}`;
|
116 |
|
\ | No newline at end of file |