forked from googleapis/google-api-nodejs-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.ts
357 lines (330 loc) · 11.1 KB
/
generator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright 2014-2016, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {handleError, buildurl} from './generator_utils';
import * as async from 'async';
import * as swig from 'swig';
import * as path from 'path';
import * as mkdirp from 'mkdirp';
import * as fs from 'fs';
import * as url from 'url';
import * as util from 'util';
import { js_beautify } from 'js-beautify';
import { DefaultTransporter } from 'google-auth-library/lib/transporters';
import * as minimist from 'minimist';
const argv = minimist(process.argv.slice(2));
const args = argv._;
const DISCOVERY_URL = argv['discovery-url'] ? argv['discovery-url'] : (
args.length ? args[0] : 'https://www.googleapis.com/discovery/v1/apis/'
);
const FRAGMENT_URL = 'https://storage.googleapis.com/apisnippets-staging/public/';
const API_TEMPLATE = './templates/api-endpoint.ts';
const BEAUTIFY_OPTIONS = {
'indent_size': 2,
'indent_char': ' ',
'eol': '\n',
'indent_level': 0,
'indent_with_tabs': false,
'preserve_newlines': true,
'max_preserve_newlines': 2,
'jslint_happy': false,
'space_after_anon_function': true,
'brace_style': 'collapse',
'keep_array_indentation': false,
'keep_function_indentation': true,
'space_before_conditional': true,
'break_chained_methods': false,
'eval_code': false,
'unescape_strings': false,
'wrap_line_length': 0,
'wrap_attributes': 'auto',
'wrap_attributes_indent_size': 4,
'end_with_newline': true
};
const RESERVED_PARAMS = ['resource', 'media', 'auth'];
const templateContents = fs.readFileSync(API_TEMPLATE, { encoding: 'utf8' });
export class Generator {
private _transporter = new DefaultTransporter();
private _requestQueue;
/**
* A multi-line string is turned into one line.
*
* @private
* @param {string} str String to process
* @return {string} Single line string processed
*/
private oneLine (str: string) {
return str.replace(/\n/g, ' ');
}
/**
* Clean a string of comment tags.
*
* @private
* @param {string} str String to process
* @return {string} Single line string processed
*/
private cleanComments (str: string) {
// Convert /* into /x and */ into x/
return str.replace(/\*\//g, 'x/').replace(/\/\*/g, '/x');
}
/**
* Returns the list of names of APIS
*
* @private
* @param {object} items Object of api endpoints
* @return {array} Array of api names
*/
private getAPIs (items) {
const apis = [];
for (const i in items) {
apis.push(items[i].name);
}
return apis;
}
private getPathParams (params) {
const pathParams = [];
if (typeof params !== 'object') {
params = {};
}
Object.keys(params).forEach(key => {
if (params[key].location === 'path') {
pathParams.push(key);
}
});
return pathParams;
}
private getSafeParamName (param) {
if (RESERVED_PARAMS.indexOf(param) !== -1) {
return param + '_';
}
return param;
}
private _options: any;
private _state = {};
/**
* Generator for generating API endpoints
*
* @private
* @param {object} options Options for generation
* @this {Generator}
*/
constructor (options) {
this._options = options || {};
/**
* This API can generate thousands of concurrent HTTP requests.
* If left to happen while generating all APIs, things get very unstable.
* This makes sure we only ever have 10 concurrent network requests, and
* adds retry logic.
*/
this._requestQueue = async.queue((opts, callback) => {
async.retry(3, () => {
return this._transporter.request(opts, callback);
});
}, 10);
swig.setFilter('buildurl', buildurl);
swig.setFilter('getAPIs', this.getAPIs);
swig.setFilter('oneLine', this.oneLine);
swig.setFilter('cleanComments', this.cleanComments);
swig.setFilter('getPathParams', this.getPathParams);
swig.setFilter('getSafeParamName', this.getSafeParamName);
swig.setFilter('cleanPaths', (str) => {
return str.replace(/\/\*\//gi, '/x/').replace(/\/\*`/gi, '/x');
});
swig.setDefaults({ loader: swig.loaders.fs(path.join(__dirname, '..', 'templates')) });
}
/**
* Add a requests to the rate limited queue.
* @param opts Options to pass to the default transporter
* @param callback
*/
private makeRequest (opts, callback) {
this._requestQueue.push(opts, callback);
}
/**
* Log output of generator
* Works just like console.log
*/
private log (...args) {
if (this._options && this._options.debug) {
console.log.apply(this, arguments);
}
}
/**
* Write to the state log, which is used for debugging.
* @param id DiscoveryRestUrl of the endpoint to log
* @param message
*/
private logResult (id, message) {
if (!this._state[id]) {
this._state[id] = [];
}
this._state[id].push(message);
}
/**
* Generate all APIs and write to files.
*
* @param {function} callback Callback when all APIs have been generated
* @throws {Error} If there is an error generating any of the APIs
*/
generateAllAPIs (callback: Function) {
const headers = this._options.includePrivate ? {} : { 'X-User-Ip': '0.0.0.0' };
this.makeRequest({
uri: DISCOVERY_URL,
headers
}, (err, resp) => {
if (err) {
return handleError(err, callback);
}
const apis = resp.items;
const queue = async.queue((api: any, next) => {
this.log('Generating API for %s...', api.id);
this.logResult(api.discoveryRestUrl, 'Attempting first generateAPI call...');
async.retry(3, this.generateAPI.bind(this, api.discoveryRestUrl), (err, results) => {
if (err) {
this.logResult(api.discoveryRestUrl, `GenerateAPI call failed with error: ${err}, moving on.`);
console.error(`Failed to generate API: ${api.id}`);
console.log(api.id + "\n-----------\n" + (util as any).inspect(this._state[api.discoveryRestUrl], { maxArrayLength: null }) + '\n');
} else {
this.logResult(api.discoveryRestUrl, `GenerateAPI call success!`);
}
this._state[api.discoveryRestUrl].done = true;
next(err, results);
});
}, 3);
apis.forEach((api) => {
queue.push(api);
});
queue.drain = (err:Error) => {
console.log((util as any).inspect(this._state, { maxArrayLength: null }));
if (callback) callback(err);
};
});
}
/**
* Given a discovery doc, parse it and recursively iterate over the various embedded links.
* @param api
* @param schema
* @param path
* @param tasks
*/
private getFragmentsForSchema (apiDiscoveryUrl, schema, path, tasks) {
if (schema.methods) {
for (const methodName in schema.methods) {
const methodSchema = schema.methods[methodName];
methodSchema.sampleUrl = path + '.' + methodName + '.frag.json';
tasks.push((cb) => {
this.logResult(apiDiscoveryUrl, `Making fragment request...`);
this.logResult(apiDiscoveryUrl, methodSchema.sampleUrl);
this.makeRequest({
uri: methodSchema.sampleUrl
}, (err, response) => {
if (err) {
this.logResult(apiDiscoveryUrl, `Fragment request err: ${err}`);
return cb(err);
}
this.logResult(apiDiscoveryUrl, `Fragment request complete.`);
if (response && response.codeFragment && response.codeFragment['Node.js']) {
let fragment = response.codeFragment['Node.js'].fragment;
fragment = fragment.replace(/\/\*/gi, '/<');
fragment = fragment.replace(/\*\//gi, '>/');
fragment = fragment.replace(/`\*/gi, '`<');
fragment = fragment.replace(/\*`/gi, '>`');
const lines = fragment.split('\n');
lines.forEach((_line, i) => {
lines[i] = '*' + (_line ? ' ' + lines[i] : '');
});
fragment = lines.join('\n');
methodSchema.fragment = fragment;
}
cb();
});
});
}
}
if (schema.resources) {
for (const resourceName in schema.resources) {
this.getFragmentsForSchema(
apiDiscoveryUrl,
schema.resources[resourceName],
path + '.' + resourceName,
tasks
);
}
}
}
/**
* Generate API file given discovery URL
* @param {String} apiDiscoveryUri URL or filename of discovery doc for API
* @param {function} callback Callback when successful write of API
* @throws {Error} If there is an error generating the API.
*/
generateAPI (apiDiscoveryUrl, callback: Function) {
const _generate = (err: Error, resp) => {
this.logResult(apiDiscoveryUrl, `Discovery doc request complete.`);
if (err) {
handleError(err, callback);
return;
}
const tasks = [];
this.getFragmentsForSchema(
apiDiscoveryUrl,
resp,
FRAGMENT_URL + resp.name + '/' + resp.version + '/0/' + resp.name,
tasks
);
// e.g. apis/drive/v2.js
const exportFilename = path.join(__dirname, '../apis', resp.name, resp.version + '.ts');
let contents;
this.logResult(apiDiscoveryUrl, `Generating templates...`);
async.waterfall([
(cb) => {
this.logResult(apiDiscoveryUrl, `Step 1...`);
async.parallel(tasks, cb);
},
(results, cb) => {
this.logResult(apiDiscoveryUrl, `Step 2...`);
const result = swig.render(templateContents, { locals: resp });
contents = js_beautify(result, BEAUTIFY_OPTIONS);
mkdirp(path.dirname(exportFilename), cb);
},
(dir, cb) => {
this.logResult(apiDiscoveryUrl, `Step 3...`);
fs.writeFile(exportFilename, contents, { encoding: 'utf8' }, cb);
}
], (err) => {
if (err) {
handleError(err, callback);
return;
}
this.logResult(apiDiscoveryUrl, `Template generation complete.`);
callback(null, exportFilename);
});
};
const parts = url.parse(apiDiscoveryUrl);
if (apiDiscoveryUrl && !parts.protocol) {
this.log('Reading from file ' + apiDiscoveryUrl);
try {
return _generate(null, JSON.parse(fs.readFileSync(apiDiscoveryUrl, {
encoding: 'utf-8'
})));
} catch (err) {
return handleError(err, callback);
}
} else {
this.logResult(apiDiscoveryUrl, `Starting discovery doc request...`);
this.logResult(apiDiscoveryUrl, apiDiscoveryUrl);
this.makeRequest({
uri: apiDiscoveryUrl
}, _generate);
}
}
}