1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | Object.defineProperty(exports, "__esModule", { value: true });
|
15 | exports.Discovery = void 0;
|
16 | const fs = require("fs");
|
17 | const google_auth_library_1 = require("google-auth-library");
|
18 | const resolve = require("url");
|
19 | const util = require("util");
|
20 | const apirequest_1 = require("./apirequest");
|
21 | const endpoint_1 = require("./endpoint");
|
22 | const readFile = util.promisify(fs.readFile);
|
23 | class Discovery {
|
24 | |
25 |
|
26 |
|
27 |
|
28 |
|
29 | constructor(options) {
|
30 | this.transporter = new google_auth_library_1.DefaultTransporter();
|
31 | this.options = options || {};
|
32 | }
|
33 | |
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | makeEndpoint(schema) {
|
40 | return (options) => {
|
41 | const ep = new endpoint_1.Endpoint(options);
|
42 | ep.applySchema(ep, schema, schema, ep);
|
43 | return ep;
|
44 | };
|
45 | }
|
46 | |
47 |
|
48 |
|
49 | log(...args) {
|
50 | if (this.options && this.options.debug) {
|
51 | console.log(...args);
|
52 | }
|
53 | }
|
54 | |
55 |
|
56 |
|
57 |
|
58 | async discoverAllAPIs(discoveryUrl) {
|
59 | const headers = this.options.includePrivate
|
60 | ? {}
|
61 | : { 'X-User-Ip': '0.0.0.0' };
|
62 | const res = await this.transporter.request({
|
63 | url: discoveryUrl,
|
64 | headers,
|
65 | });
|
66 | const items = res.data.items;
|
67 | const apis = await Promise.all(items.map(async (api) => {
|
68 | const endpointCreator = await this.discoverAPI(api.discoveryRestUrl);
|
69 | return { api, endpointCreator };
|
70 | }));
|
71 | const versionIndex = {};
|
72 |
|
73 | const apisIndex = {};
|
74 | for (const set of apis) {
|
75 | if (!apisIndex[set.api.name]) {
|
76 | versionIndex[set.api.name] = {};
|
77 | apisIndex[set.api.name] = (options) => {
|
78 | const type = typeof options;
|
79 | let version;
|
80 | if (type === 'string') {
|
81 | version = options;
|
82 | options = {};
|
83 | }
|
84 | else if (type === 'object') {
|
85 | version = options.version;
|
86 | delete options.version;
|
87 | }
|
88 | else {
|
89 | throw new Error('Argument error: Accepts only string or object');
|
90 | }
|
91 | try {
|
92 | const ep = set.endpointCreator(options, this);
|
93 | return Object.freeze(ep); // create new & freeze
|
94 | }
|
95 | catch (e) {
|
96 | throw new Error(util.format('Unable to load endpoint %s("%s"): %s', set.api.name, version, e.message));
|
97 | }
|
98 | };
|
99 | }
|
100 | versionIndex[set.api.name][set.api.version] = set.endpointCreator;
|
101 | }
|
102 | return apisIndex;
|
103 | }
|
104 | /**
|
105 | * Generate API file given discovery URL
|
106 | *
|
107 | * @param apiDiscoveryUrl URL or filename of discovery doc for API
|
108 | * @returns A promise that resolves with a function that creates the endpoint
|
109 | */
|
110 | async discoverAPI(apiDiscoveryUrl) {
|
111 | if (typeof apiDiscoveryUrl === 'string') {
|
112 | const parts = resolve.parse(apiDiscoveryUrl);
|
113 | if (apiDiscoveryUrl && !parts.protocol) {
|
114 | this.log('Reading from file ' + apiDiscoveryUrl);
|
115 | const file = await readFile(apiDiscoveryUrl, { encoding: 'utf8' });
|
116 | return this.makeEndpoint(JSON.parse(file));
|
117 | }
|
118 | else {
|
119 | this.log('Requesting ' + apiDiscoveryUrl);
|
120 | const res = await this.transporter.request({
|
121 | url: apiDiscoveryUrl,
|
122 | });
|
123 | return this.makeEndpoint(res.data);
|
124 | }
|
125 | }
|
126 | else {
|
127 | const options = apiDiscoveryUrl;
|
128 | this.log('Requesting ' + options.url);
|
129 | const url = options.url;
|
130 | delete options.url;
|
131 | const parameters = {
|
132 | options: { url, method: 'GET' },
|
133 | requiredParams: [],
|
134 | pathParams: [],
|
135 | params: options,
|
136 | context: { google: { _options: {} }, _options: {} },
|
137 | };
|
138 | const res = await (0, apirequest_1.createAPIRequest)(parameters);
|
139 | return this.makeEndpoint(res.data);
|
140 | }
|
141 | }
|
142 | }
|
143 | exports.Discovery = Discovery;
|
144 |
|
\ | No newline at end of file |