-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
782 lines (690 loc) · 22.7 KB
/
index.js
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
/**
* @copyright Copyright 2016-2021 Kevin Locke <kevin@kevinlocke.name>
* @license MIT
* @module procore-docs-to-openapi
*/
import assert from 'node:assert';
import { join as joinUrlPath } from 'node:path/posix';
import { debuglog, isDeepStrictEqual } from 'node:util';
// https://github.com/import-js/eslint-plugin-import/issues/1810
// eslint-disable-next-line import/no-unresolved
import { camelCase } from 'camel-case';
import escapeStringRegexp from 'escape-string-regexp';
import groupNameToUrlPath from './lib/group-name-to-url-path.js';
import toJsonPointer from './lib/to-json-pointer.js';
const debug = debuglog('procore-docs-to-openapi');
const indentIncrement = 2;
const docsUrlSymbol = Symbol('docsUrl');
const versionNameSymbol = Symbol('versionName');
const versionToolsSymbol = Symbol('versionTools');
/** OpenAPI Specification version of documents produced by this module.
*
* @private
*/
const openapiVersion = '3.1.0';
function visit(transformer, method, propName, propValue) {
transformer.transformPath.push(propName);
let handlingException = false;
try {
return method.call(transformer, propValue);
} catch (err) {
handlingException = true;
if (!Object.hasOwn(err, 'transformPath')) {
err.transformPath = [...transformer.transformPath];
err.message +=
` (while transforming ${toJsonPointer(err.transformPath)})`;
}
throw err;
} finally {
const popProp = transformer.transformPath.pop();
// Avoid clobbering an exception which is already propagating
if (!handlingException) {
assert.strictEqual(popProp, propName);
}
}
}
export default class ProcoreApiDocToOpenApiTransformer {
constructor(options = {}) {
if (!options || typeof options !== 'object') {
throw new TypeError('options must be an object or undefined');
}
this.options = options;
/** Property names traversed in current transformation.
*
* @type {!Array<string>}
*/
this.transformPath = [];
/** URL of the Procore API docs for the current version object.
*
* @private
* @type {string=}
*/
this[docsUrlSymbol] = undefined;
/** Name of the current Procore API version object.
*
* @private
* @type {string=}
*/
this[versionNameSymbol] = undefined;
/** Tools categorization for the current Procore API version.
*
* @private
* @type {Array<string>=}
*/
this[versionToolsSymbol] = undefined;
}
/** Logs a warning about the transformation.
* May be overridden.
* Arguments are treated like console.log.
*
* @param {string|*} message Message with zero or more substitution strings,
* or first value to log.
* @param {*} values Additional values to log. Applied to substitution
* string in message, if one matches, otherwise appended.
*/
warn(message, ...values) {
// Note: debug.enabled defined on Node.js v14.9.0 and later
if (debug.enabled !== false) {
debug(message, ...values, 'at', toJsonPointer(this.transformPath));
}
}
/** Transforms a path_params or query_params object to an OpenAPI Parameter
* Object.
*
* @param {!object} param Item from path_params or query_params array.
* @returns {!object} OpenAPI Parameter Object.
*/
transformParam(param) {
const {
description,
enum: enumValues,
type,
} = param;
// Procore docs add enum: [] to non-enumerated parameters.
// Sanity check enumValues before use.
let checkedEnum;
if (enumValues !== undefined && enumValues !== null) {
if (!Array.isArray(enumValues)) {
this.warn('Unexpected non-Array enum:', enumValues);
} else if (enumValues.length > 0) {
checkedEnum = enumValues;
}
}
return {
description: description || undefined,
type,
enum: checkedEnum,
};
}
/** Transforms path_params or query_params to an array of OpenAPI Parameter
* Objects.
*
* @param {!Array<!object>} params path_params or query_params array.
* @param {string} paramsIn in property of the returned Parameter Objects.
* @returns {!Array<!object>} Array of OpenAPI Parameter Objects.
*/
transformParams(params, paramsIn) {
const oasParams = [];
let prevSchema;
for (const [i, param] of Object.entries(params)) {
const {
description,
enum: enumValues,
name,
required,
type,
...unrecognized
} = param;
const unrecognizedProps = Object.keys(unrecognized);
if (unrecognizedProps.length > 0) {
this.warn(
'Unrecognized properties on %O:',
`${paramsIn}_param`,
unrecognizedProps,
);
}
if (type === 'object') {
// Without indentation, how do we know which of the following params
// are properties?
throw new Error('Unsupported parameter with type object');
}
const schema = visit(this, this.transformParam, i, param);
if (prevSchema && prevSchema.type === 'array') {
if (prevSchema.items) {
throw new Error(`${name} conflicts with existing array item schema`);
}
prevSchema.items = schema;
} else {
oasParams.push({
name,
in: paramsIn,
description: schema.description,
required,
schema,
});
delete schema.description;
}
prevSchema = schema;
}
return oasParams;
}
/** Transforms path_params array to an array of OpenAPI Parameter Objects.
*
* @param {!Array<!object>} pathParams path_params array.
* @returns {!Array<!object>} Array of OpenAPI Parameter Objects.
*/
transformPathParams(pathParams) {
return this.transformParams(pathParams, 'path');
}
/** Transforms query_params array to an array of OpenAPI Parameter Objects.
*
* @param {!Array<!object>} queryParams query_params array.
* @returns {!Array<!object>} Array of OpenAPI Parameter Objects.
*/
transformQueryParams(queryParams) {
return this.transformParams(queryParams, 'query');
}
/** Transforms body_params to JSON Schema.
*
* @param {!Array<!object>} params body_params array.
* @returns {!object} JSON Schema.
*/
transformBodyParams(params) {
// The Procore docs represent schema hierarchy for body using indentation.
// Keep track of most recent schema for each indent depth, so child schema
// can be matched to parent.
const schemaForDepth = [{ type: 'object' }];
for (const [i, param] of Object.entries(params)) {
const {
description,
direct_child_of_object: directChildOfObject,
enum: enumValues,
indentation,
name,
required,
type,
...unrecognized
} = param;
const unrecognizedProps = Object.keys(unrecognized);
if (unrecognizedProps.length > 0) {
this.warn('Unrecognized properties on body_param:', unrecognizedProps);
}
if (directChildOfObject !== undefined && directChildOfObject !== true) {
this.warn('Unrecognized direct_child_of_object:', directChildOfObject);
}
if (indentation < indentIncrement
|| indentation % indentIncrement !== 0) {
throw new Error(
`indentation ${indentation} is not a multiple of ${indentIncrement}`,
);
}
const depth = indentation / indentIncrement;
// Procore docs omits array parameter for array of array
// Check for missing parent with grandparent array missing item type
if (depth > 2 && depth - 1 === schemaForDepth.length) {
const maybeGrandparent = schemaForDepth[depth - 2];
if (maybeGrandparent.type === 'array' && !maybeGrandparent.items) {
const parentArray = { type: 'array' };
maybeGrandparent.items = parentArray;
schemaForDepth[depth - 1] = parentArray;
}
}
const parentSchema = schemaForDepth[depth - 1];
if (!parentSchema) {
throw new Error(
`param ${name} has no parent at indent ${depth * indentIncrement}`,
);
}
const schema = visit(this, this.transformParam, i, param);
const parentType = parentSchema.type;
if (parentType === 'array' && !name) {
// Parameter is array item
if (parentSchema.items) {
throw new Error(
`item schema for array with existing item schema: ${
JSON.stringify(parentSchema.items)}`,
);
}
parentSchema.items = schema;
} else {
let parentObjectSchema;
if (parentType === 'object') {
// Parameter is property of parent object
parentObjectSchema = parentSchema;
} else if (parentType === 'array') {
// Parameter is property of parent array item
parentObjectSchema = parentSchema.items;
if (!parentObjectSchema) {
parentObjectSchema = { type: 'object' };
parentSchema.items = parentObjectSchema;
} else if (parentObjectSchema.type !== 'object') {
throw new Error(`property ${name} for array of non-object items`);
}
} else {
throw new Error(
`param ${name} is child of non-object/array type ${parentType}`,
);
}
if (!name) {
throw new Error('missing name for child param of object');
}
// Procore docs represent variables in param names as %{var}
// e.g. "custom_field_%{custom_field_definition_id}"
const variableParts = name.split(/(%\{[^}]+\})/g).filter(Boolean);
if (variableParts.length === 1 && !variableParts[0].startsWith('%{')) {
// No variables in name, add to properties
let parentProperties = parentObjectSchema.properties;
if (!parentProperties) {
parentProperties = {};
parentObjectSchema.properties = parentProperties;
}
if (Object.hasOwn(parentProperties, name)) {
throw new Error(`duplicate property ${name} for parameter`);
}
parentProperties[name] = schema;
} else {
// Variables in name, add converted pattern to patternProperties
// Note: patterns are not implicitly anchored. Need ^$.
// https://github.com/json-schema-org/json-schema-spec/issues/897
let pattern = '^';
for (const variablePart of variableParts) {
if (variablePart.startsWith('%{')) {
if (variablePart.endsWith('_id}')) {
pattern += '([0-9]+)';
} else {
pattern += '(.*)';
}
} else {
pattern += escapeStringRegexp(variablePart);
}
}
pattern += '$';
let { patternProperties } = parentObjectSchema;
if (!patternProperties) {
patternProperties = {};
parentObjectSchema.patternProperties = patternProperties;
}
if (Object.hasOwn(patternProperties, pattern)) {
throw new Error(
`duplicate patternProperty ${pattern} for parameter ${name}`,
);
}
patternProperties[pattern] = schema;
}
if (required) {
const parentRequired = parentObjectSchema.required;
if (!parentRequired) {
parentObjectSchema.required = [name];
} else {
parentObjectSchema.required.push(name);
}
}
}
schemaForDepth.splice(depth, schemaForDepth.length - depth, schema);
}
return schemaForDepth[0];
}
/** Transforms schema properties to JSON Schema properties.
*
* @param {!Array<!object>} properties schema properties array.
* @returns {!object} JSON Schema properties.
*/
transformSchemaProperties(properties) {
if (properties.length === 0) {
return undefined;
}
const propertiesByName = {};
for (const [i, property] of Object.entries(properties)) {
const { field } = property;
if (!field || typeof field !== 'string') {
throw new Error(
`Invalid field '${field}' in schema properties`,
);
}
if (Object.hasOwn(propertiesByName, field)) {
throw new Error(
`Duplicate field '${field}' in schema properties`,
);
}
propertiesByName[field] = visit(
this,
this.transformSchema,
i,
property,
);
}
return propertiesByName;
}
/** Transforms a schema to a JSON Schema.
*
* @param {!object} schema schema object.
* @returns {!object} JSON Schema.
*/
transformSchema(schema) {
const {
field,
...newSchema
} = schema;
const {
items,
properties,
additionalProperties,
} = schema;
if (items !== undefined) {
newSchema.items = visit(this, this.transformSchema, 'items', items);
}
if (properties !== undefined) {
newSchema.properties = visit(
this,
this.transformSchemaProperties,
'properties',
properties,
);
}
if (typeof additionalProperties === 'object') {
newSchema.additionalProperties = visit(
this,
this.transformSchema,
'additionalProperties',
additionalProperties,
);
}
return newSchema;
}
/** Transforms a response object to an OpenAPI Response Object.
*
* @param {!object} response responses array item.
* @returns {!object} OpenAPI Response Object.
*/
transformResponse(response) {
const {
description,
status,
schema,
...unrecognized
} = response;
const unrecognizedProps = Object.keys(unrecognized);
if (unrecognizedProps.length > 0) {
this.warn('Unrecognized properties on response:', unrecognizedProps);
}
if (schema.field) {
throw new Error('field on top-level schema');
}
return {
description: description || undefined,
content: {
'application/json': {
schema: visit(this, this.transformSchema, 'schema', schema),
},
},
};
}
/** Transforms a responses array to an OpenAPI Responses Object.
*
* @param {!Array<!object>} responses responses array.
* @returns {!object} OpenAPI Responses Object.
*/
transformResponses(responses) {
const responseByStatus = {};
for (const [i, response] of Object.entries(responses)) {
const { status } = response;
if (typeof status !== 'string' || !/^[2-5][0-9][0-9]$/.test(status)) {
this.warn('Invalid status:', status);
}
if (Object.hasOwn(responseByStatus, status)) {
throw new Error(`Multiple responses for status ${status}`);
}
responseByStatus[status] =
visit(this, this.transformResponse, i, response);
}
return responseByStatus;
}
/** Transforms an endpoint object to a path, method name, and OpenAPI
* Operation Object.
*
* @param {!object} endpoint endpoint object.
* @returns {!{
* path: string,
* method: string,
* operation: !object
* }} Path, method name, and OpenAPI Operation Object.
*/
transformEndpoint(endpoint) {
const {
base_path: basePath,
beta_programs: betaPrograms,
body_example: example,
body_params: bodyParams,
changelog,
deprecated_at: deprecatedAt,
description,
group,
internal_only: internalOnly,
path,
path_params: pathParams,
query_params: queryParams,
responses,
support_level: supportLevel,
summary,
tools,
verb: method,
...unrecognized
} = endpoint;
const unrecognizedProps = Object.keys(unrecognized);
if (unrecognizedProps.length > 0) {
this.warn('Unrecognized properties on endpoint:', unrecognizedProps);
}
const parameters = [
...visit(this, this.transformPathParams, 'path_params', pathParams),
...visit(this, this.transformQueryParams, 'query_params', queryParams),
];
// Warn if name used for docsUrl differs.
// Currently occurs for several endpoints with "Drawings" group.
const versionName = this[versionNameSymbol];
if (versionName && group !== versionName) {
this.warn(
'endpoint.group (%O) differs from ancestor version.name (%O).'
+ ' Using version.name for externalDocs.url.',
group,
versionName,
);
}
if (basePath[0] !== '/') {
this.warn('base_path (%s) is not absolute', basePath);
}
let docsUrl = this[docsUrlSymbol];
if (docsUrl && summary) {
docsUrl += `#${groupNameToUrlPath(summary)}`;
}
let combinedSummary = '';
if (internalOnly) {
combinedSummary += '(Internal Only)';
}
if (supportLevel !== 'production') {
combinedSummary +=
`(${supportLevel[0].toUpperCase() + supportLevel.slice(1)}) `;
}
combinedSummary += summary;
let combinedDescription = description || '';
if (betaPrograms && betaPrograms.length > 0) {
combinedDescription += `\nPart of Beta Program: ${betaPrograms}`;
}
if (changelog) {
const expectEndpoint = `${method.toUpperCase()} ${path}`;
combinedDescription += '\n#### Changelog\n\n'
+ '| Date | Change |\n'
+ '| ---------- | ------ |\n';
for (const {
breaking: clBreaking,
datestamp: clDatestamp,
description: clDescription,
endpoint: clEndpoint,
summary: clSummary,
// FIXME: What is the meaning of support_level?
// For example, some changelogs in weather-logs.json have
// support_level: "alpha" for endpoints which are "production".
// Maybe changelog item was added when endpoint was "alpha"?
// support_level: clSupportLevel,
type: clType,
// TODO: versions
} of changelog) {
if (clEndpoint !== expectEndpoint) {
this.warn(
'Expected changelog entry to have endpoint %O, got %O',
expectEndpoint,
clEndpoint,
);
}
// eslint-disable-next-line prefer-template
combinedDescription += `| ${clDatestamp} | `
+ `(${clType[0].toUpperCase() + clType.slice(1)})`
+ (clBreaking ? '(BREAKING)' : '')
+ ` **${clSummary.replace(/\.?$/, '.')}**`
+ ` ${clDescription}`
+ ' |\n';
}
}
const versionTools = this[versionToolsSymbol];
const tags = tools || versionTools;
if (tools && versionTools && !isDeepStrictEqual(tools, versionTools)) {
this.warn(
'endpoint.tools (%O) differs from ancestor version.tools (%O).'
+ ' Using endpoint.tools for tags.',
tools,
versionTools,
);
}
return {
path: joinUrlPath(basePath, path),
method,
operation: {
operationId: camelCase(summary),
summary: combinedSummary || undefined,
description: combinedDescription || undefined,
externalDocs: docsUrl ? { url: docsUrl } : undefined,
tags,
deprecated: deprecatedAt ? true : undefined,
parameters: parameters.length > 0 ? parameters : undefined,
requestBody: bodyParams.length === 0 ? undefined : {
required: true,
content: {
'application/json': {
schema: visit(
this,
this.transformBodyParams,
'body_params',
bodyParams,
),
example: example || undefined,
},
},
},
responses: visit(this, this.transformResponses, 'responses', responses),
},
};
}
/** Transforms an endpoints array to an OpenAPI Paths Object.
*
* @param {!Array<!object>} endpoints endpoints array.
* @returns {!object} OpenAPI Paths Object.
*/
transformEndpoints(endpoints) {
const paths = {};
for (const [i, endpoint] of Object.entries(endpoints)) {
if (!this.options.endpointFilter
|| this.options.endpointFilter(endpoint)) {
const { path, method, operation } =
visit(this, this.transformEndpoint, i, endpoint);
const pathItem = paths[path];
if (!pathItem) {
paths[path] = { [method]: operation };
} else if (!pathItem[method]) {
pathItem[method] = operation;
} else {
throw new Error(
`Method ${method} appears multiple times for ${path}`,
);
}
}
}
return paths;
}
/** Transforms a version object to an OpenAPI document.
*
* @param {!object} version version object.
* @returns {!object} OpenAPI document.
*/
transformVersion(version) {
const {
api_version: apiVersion,
endpoints,
name,
product_category: productCategory,
resource_version: resourceVersion,
tools,
// Checked on individual endpoints. Ignored on version object.
highest_support_level: highestSupportLevel,
internal_only: internalOnly,
beta_programs: betaPrograms,
...unrecognized
} = version;
const unrecognizedProps = Object.keys(unrecognized);
if (unrecognizedProps.length > 0) {
this.warn('Unrecognized properties on version:', unrecognizedProps);
}
this[docsUrlSymbol] = `https://developers.procore.com/reference/rest/v1/${
groupNameToUrlPath(name)}`;
this[versionNameSymbol] = name;
this[versionToolsSymbol] = tools;
try {
return {
openapi: openapiVersion,
paths: visit(this, this.transformEndpoints, 'endpoints', endpoints),
};
} finally {
this[docsUrlSymbol] = undefined;
this[versionNameSymbol] = undefined;
this[versionToolsSymbol] = undefined;
}
}
/** Transforms a versions array to an OpenAPI document.
*
* @param {!Array<!object>} versions versions array.
* @returns {!object} OpenAPI document.
*/
transformVersions(versions) {
if (!Array.isArray(versions)) {
throw new TypeError('versions must be an Array');
}
if (versions.length > 1) {
this.warn(
'Found %d versions. Ignoring all but the last.',
versions.length,
);
}
const last = versions.length - 1;
return visit(this, this.transformVersion, String(last), versions[last]);
}
/** Transforms the root object to an OpenAPI document.
*
* @param {!object} doc Procore REST API document root object.
* @returns {!object} OpenAPI document.
*/
transformApiDoc(doc) {
if (!doc || typeof doc !== 'object') {
throw new TypeError('doc must be an object');
}
const {
versions,
// api_version and resource_version are handled in each version
api_version: apiVersion,
resource_version_list: resourceVersionList,
...unrecognized
} = doc;
const unrecognizedProps = Object.keys(unrecognized);
if (unrecognizedProps.length > 0) {
this.warn('Unrecognized properties on doc:', unrecognizedProps);
}
return visit(this, this.transformVersions, 'versions', versions);
}
}