json-machete 0.3.5 → 0.3.6-alpha-983cd38bc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +154 -149
- package/index.mjs +154 -149
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -7,10 +7,10 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau
|
|
|
7
7
|
const jsonPtr = require('json-ptr');
|
|
8
8
|
const utils = require('@graphql-mesh/utils');
|
|
9
9
|
const path = require('path');
|
|
10
|
-
const util = require('util');
|
|
11
|
-
const toJsonSchema = _interopDefault(require('to-json-schema'));
|
|
12
10
|
const urlJoin = _interopDefault(require('url-join'));
|
|
13
11
|
const crossUndiciFetch = require('cross-undici-fetch');
|
|
12
|
+
const util = require('util');
|
|
13
|
+
const toJsonSchema = _interopDefault(require('to-json-schema'));
|
|
14
14
|
|
|
15
15
|
(function (OnCircularReference) {
|
|
16
16
|
OnCircularReference["WARN"] = "WARN";
|
|
@@ -310,6 +310,154 @@ async function compareJSONSchemas(oldSchema, newSchema) {
|
|
|
310
310
|
}
|
|
311
311
|
}
|
|
312
312
|
|
|
313
|
+
const resolvePath = (path, root) => {
|
|
314
|
+
return jsonPtr.JsonPointer.get(root, path);
|
|
315
|
+
};
|
|
316
|
+
function isRefObject(obj) {
|
|
317
|
+
return typeof obj === 'object' && obj.$ref;
|
|
318
|
+
}
|
|
319
|
+
function isURL(str) {
|
|
320
|
+
return /^https?:\/\//.test(str);
|
|
321
|
+
}
|
|
322
|
+
const getAbsolute$Ref = (given$ref, baseFilePath) => {
|
|
323
|
+
const [givenExternalFileRelativePath, givenRefPath] = given$ref.split('#');
|
|
324
|
+
if (givenExternalFileRelativePath) {
|
|
325
|
+
const cwd = isURL(baseFilePath) ? getCwdForUrl(baseFilePath) : path.dirname(baseFilePath);
|
|
326
|
+
const givenExternalFilePath = getAbsolutePath(givenExternalFileRelativePath, cwd);
|
|
327
|
+
if (givenRefPath) {
|
|
328
|
+
return `${givenExternalFilePath}#${givenRefPath}`;
|
|
329
|
+
}
|
|
330
|
+
return givenExternalFilePath;
|
|
331
|
+
}
|
|
332
|
+
return `${baseFilePath}#${givenRefPath}`;
|
|
333
|
+
};
|
|
334
|
+
function getCwdForUrl(url) {
|
|
335
|
+
const urlParts = url.split('/');
|
|
336
|
+
urlParts.pop();
|
|
337
|
+
return urlParts.join('/');
|
|
338
|
+
}
|
|
339
|
+
function normalizeUrl(url) {
|
|
340
|
+
return new URL(url).toString();
|
|
341
|
+
}
|
|
342
|
+
function getAbsolutePath(path$1, cwd) {
|
|
343
|
+
if (isURL(path$1)) {
|
|
344
|
+
return path$1;
|
|
345
|
+
}
|
|
346
|
+
if (isURL(cwd)) {
|
|
347
|
+
return normalizeUrl(urlJoin(cwd, path$1));
|
|
348
|
+
}
|
|
349
|
+
if (path.isAbsolute(path$1)) {
|
|
350
|
+
return path$1;
|
|
351
|
+
}
|
|
352
|
+
return path.join(cwd, path$1);
|
|
353
|
+
}
|
|
354
|
+
function getCwd(path$1) {
|
|
355
|
+
return isURL(path$1) ? getCwdForUrl(path$1) : path.dirname(path$1);
|
|
356
|
+
}
|
|
357
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
358
|
+
async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache = new Map(), refMap = new Map(), root = obj, fetch = crossUndiciFetch.fetch, headers, } = {}) {
|
|
359
|
+
if (typeof obj === 'object') {
|
|
360
|
+
if (isRefObject(obj)) {
|
|
361
|
+
const $ref = obj.$ref;
|
|
362
|
+
if (refMap.has($ref)) {
|
|
363
|
+
return refMap.get($ref);
|
|
364
|
+
}
|
|
365
|
+
else {
|
|
366
|
+
const [externalRelativeFilePath, refPath] = $ref.split('#');
|
|
367
|
+
if (externalRelativeFilePath) {
|
|
368
|
+
const externalFilePath = getAbsolutePath(externalRelativeFilePath, cwd);
|
|
369
|
+
const newCwd = getCwd(externalFilePath);
|
|
370
|
+
let externalFile = externalFileCache.get(externalFilePath);
|
|
371
|
+
if (!externalFile) {
|
|
372
|
+
externalFile = await utils.readFileOrUrl(externalFilePath, {
|
|
373
|
+
fetch,
|
|
374
|
+
headers,
|
|
375
|
+
cwd,
|
|
376
|
+
}).catch(() => {
|
|
377
|
+
throw new Error(`Unable to load ${externalRelativeFilePath} from ${cwd}`);
|
|
378
|
+
});
|
|
379
|
+
externalFileCache.set(externalFilePath, externalFile);
|
|
380
|
+
}
|
|
381
|
+
const result = await dereferenceObject(refPath
|
|
382
|
+
? {
|
|
383
|
+
$ref: `#${refPath}`,
|
|
384
|
+
}
|
|
385
|
+
: externalFile, {
|
|
386
|
+
cwd: newCwd,
|
|
387
|
+
externalFileCache,
|
|
388
|
+
refMap: new Proxy(refMap, {
|
|
389
|
+
get: (originalRefMap, key) => {
|
|
390
|
+
switch (key) {
|
|
391
|
+
case 'has':
|
|
392
|
+
return (given$ref) => {
|
|
393
|
+
const original$Ref = getAbsolute$Ref(given$ref, externalFilePath);
|
|
394
|
+
return originalRefMap.has(original$Ref);
|
|
395
|
+
};
|
|
396
|
+
case 'get':
|
|
397
|
+
return (given$ref) => {
|
|
398
|
+
const original$Ref = getAbsolute$Ref(given$ref, externalFilePath);
|
|
399
|
+
return originalRefMap.get(original$Ref);
|
|
400
|
+
};
|
|
401
|
+
case 'set':
|
|
402
|
+
return (given$ref, val) => {
|
|
403
|
+
const original$Ref = getAbsolute$Ref(given$ref, externalFilePath);
|
|
404
|
+
return originalRefMap.set(original$Ref, val);
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
throw new Error('Not implemented ' + key.toString());
|
|
408
|
+
},
|
|
409
|
+
}),
|
|
410
|
+
fetch,
|
|
411
|
+
headers,
|
|
412
|
+
root: externalFile,
|
|
413
|
+
});
|
|
414
|
+
refMap.set($ref, result);
|
|
415
|
+
result.$resolvedRef = $ref;
|
|
416
|
+
if (obj.title) {
|
|
417
|
+
result.title = obj.title;
|
|
418
|
+
}
|
|
419
|
+
return result;
|
|
420
|
+
}
|
|
421
|
+
else {
|
|
422
|
+
const resolvedObj = resolvePath(refPath, root);
|
|
423
|
+
if (typeof resolvedObj === 'object' && !resolvedObj.$ref) {
|
|
424
|
+
refMap.set($ref, resolvedObj);
|
|
425
|
+
}
|
|
426
|
+
const result = await dereferenceObject(resolvedObj, {
|
|
427
|
+
cwd,
|
|
428
|
+
externalFileCache,
|
|
429
|
+
refMap,
|
|
430
|
+
root,
|
|
431
|
+
fetch,
|
|
432
|
+
headers,
|
|
433
|
+
});
|
|
434
|
+
if (obj.title) {
|
|
435
|
+
result.title = obj.title;
|
|
436
|
+
}
|
|
437
|
+
refMap.set($ref, result);
|
|
438
|
+
result.$resolvedRef = $ref;
|
|
439
|
+
return result;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
else {
|
|
444
|
+
await Promise.all(Object.entries(obj).map(async ([key, val]) => {
|
|
445
|
+
if (typeof val === 'object') {
|
|
446
|
+
obj[key] = await dereferenceObject(val, {
|
|
447
|
+
cwd,
|
|
448
|
+
externalFileCache,
|
|
449
|
+
refMap,
|
|
450
|
+
root,
|
|
451
|
+
fetch,
|
|
452
|
+
headers,
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
}));
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return obj;
|
|
459
|
+
}
|
|
460
|
+
|
|
313
461
|
const reservedTypeNames = ['Query', 'Mutation', 'Subscription'];
|
|
314
462
|
function deduplicateJSONSchema(schema, seenMap = new Map()) {
|
|
315
463
|
if (typeof schema === 'object' && schema != null) {
|
|
@@ -362,11 +510,12 @@ async function healJSONSchema(schema) {
|
|
|
362
510
|
delete subSchema.title;
|
|
363
511
|
}
|
|
364
512
|
if (!subSchema.title && !subSchema.$ref) {
|
|
513
|
+
const realPath = subSchema.$resolvedRef || path;
|
|
365
514
|
// Try to get definition name if missing
|
|
366
515
|
const splitByDefinitions = path.includes('/components/schemas/')
|
|
367
|
-
?
|
|
368
|
-
:
|
|
369
|
-
const maybeDefinitionBasedPath = splitByDefinitions.length > 1 ? splitByDefinitions[splitByDefinitions.length - 1] :
|
|
516
|
+
? realPath.split('/components/schemas/')
|
|
517
|
+
: realPath.split('/definitions/');
|
|
518
|
+
const maybeDefinitionBasedPath = splitByDefinitions.length > 1 ? splitByDefinitions[splitByDefinitions.length - 1] : realPath;
|
|
370
519
|
let pathBasedName = maybeDefinitionBasedPath
|
|
371
520
|
.split('/properties')
|
|
372
521
|
.join('')
|
|
@@ -459,150 +608,6 @@ async function healJSONSchema(schema) {
|
|
|
459
608
|
});
|
|
460
609
|
}
|
|
461
610
|
|
|
462
|
-
const resolvePath = (path, root) => {
|
|
463
|
-
return jsonPtr.JsonPointer.get(root, path);
|
|
464
|
-
};
|
|
465
|
-
function isRefObject(obj) {
|
|
466
|
-
return typeof obj === 'object' && obj.$ref;
|
|
467
|
-
}
|
|
468
|
-
function isURL(str) {
|
|
469
|
-
return /^https?:\/\//.test(str);
|
|
470
|
-
}
|
|
471
|
-
const getAbsolute$Ref = (given$ref, baseFilePath) => {
|
|
472
|
-
const [givenExternalFileRelativePath, givenRefPath] = given$ref.split('#');
|
|
473
|
-
if (givenExternalFileRelativePath) {
|
|
474
|
-
const cwd = isURL(baseFilePath) ? getCwdForUrl(baseFilePath) : path.dirname(baseFilePath);
|
|
475
|
-
const givenExternalFilePath = getAbsolutePath(givenExternalFileRelativePath, cwd);
|
|
476
|
-
if (givenRefPath) {
|
|
477
|
-
return `${givenExternalFilePath}#${givenRefPath}`;
|
|
478
|
-
}
|
|
479
|
-
return givenExternalFilePath;
|
|
480
|
-
}
|
|
481
|
-
return `${baseFilePath}#${givenRefPath}`;
|
|
482
|
-
};
|
|
483
|
-
function getCwdForUrl(url) {
|
|
484
|
-
const urlParts = url.split('/');
|
|
485
|
-
urlParts.pop();
|
|
486
|
-
return urlParts.join('/');
|
|
487
|
-
}
|
|
488
|
-
function normalizeUrl(url) {
|
|
489
|
-
return new URL(url).toString();
|
|
490
|
-
}
|
|
491
|
-
function getAbsolutePath(path$1, cwd) {
|
|
492
|
-
if (isURL(path$1)) {
|
|
493
|
-
return path$1;
|
|
494
|
-
}
|
|
495
|
-
if (isURL(cwd)) {
|
|
496
|
-
return normalizeUrl(urlJoin(cwd, path$1));
|
|
497
|
-
}
|
|
498
|
-
if (path.isAbsolute(path$1)) {
|
|
499
|
-
return path$1;
|
|
500
|
-
}
|
|
501
|
-
return path.join(cwd, path$1);
|
|
502
|
-
}
|
|
503
|
-
function getCwd(path$1) {
|
|
504
|
-
return isURL(path$1) ? getCwdForUrl(path$1) : path.dirname(path$1);
|
|
505
|
-
}
|
|
506
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
507
|
-
async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache = new Map(), refMap = new Map(), root = obj, fetch = crossUndiciFetch.fetch, headers, } = {}) {
|
|
508
|
-
if (typeof obj === 'object') {
|
|
509
|
-
if (isRefObject(obj)) {
|
|
510
|
-
const $ref = obj.$ref;
|
|
511
|
-
if (refMap.has($ref)) {
|
|
512
|
-
return refMap.get($ref);
|
|
513
|
-
}
|
|
514
|
-
else {
|
|
515
|
-
const [externalRelativeFilePath, refPath] = $ref.split('#');
|
|
516
|
-
if (externalRelativeFilePath) {
|
|
517
|
-
const externalFilePath = getAbsolutePath(externalRelativeFilePath, cwd);
|
|
518
|
-
const newCwd = getCwd(externalFilePath);
|
|
519
|
-
let externalFile = externalFileCache.get(externalFilePath);
|
|
520
|
-
if (!externalFile) {
|
|
521
|
-
externalFile = await utils.readFileOrUrl(externalFilePath, {
|
|
522
|
-
fetch,
|
|
523
|
-
headers,
|
|
524
|
-
cwd,
|
|
525
|
-
}).catch(() => {
|
|
526
|
-
throw new Error(`Unable to load ${externalRelativeFilePath} from ${cwd}`);
|
|
527
|
-
});
|
|
528
|
-
externalFile = await healJSONSchema(externalFile);
|
|
529
|
-
externalFileCache.set(externalFilePath, externalFile);
|
|
530
|
-
}
|
|
531
|
-
const result = await dereferenceObject(refPath
|
|
532
|
-
? {
|
|
533
|
-
$ref: `#${refPath}`,
|
|
534
|
-
}
|
|
535
|
-
: externalFile, {
|
|
536
|
-
cwd: newCwd,
|
|
537
|
-
externalFileCache,
|
|
538
|
-
refMap: new Proxy(refMap, {
|
|
539
|
-
get: (originalRefMap, key) => {
|
|
540
|
-
switch (key) {
|
|
541
|
-
case 'has':
|
|
542
|
-
return (given$ref) => {
|
|
543
|
-
const original$Ref = getAbsolute$Ref(given$ref, externalFilePath);
|
|
544
|
-
return originalRefMap.has(original$Ref);
|
|
545
|
-
};
|
|
546
|
-
case 'get':
|
|
547
|
-
return (given$ref) => {
|
|
548
|
-
const original$Ref = getAbsolute$Ref(given$ref, externalFilePath);
|
|
549
|
-
return originalRefMap.get(original$Ref);
|
|
550
|
-
};
|
|
551
|
-
case 'set':
|
|
552
|
-
return (given$ref, val) => {
|
|
553
|
-
const original$Ref = getAbsolute$Ref(given$ref, externalFilePath);
|
|
554
|
-
return originalRefMap.set(original$Ref, val);
|
|
555
|
-
};
|
|
556
|
-
}
|
|
557
|
-
throw new Error('Not implemented ' + key.toString());
|
|
558
|
-
},
|
|
559
|
-
}),
|
|
560
|
-
fetch,
|
|
561
|
-
headers,
|
|
562
|
-
root: externalFile,
|
|
563
|
-
});
|
|
564
|
-
refMap.set($ref, result);
|
|
565
|
-
return result;
|
|
566
|
-
}
|
|
567
|
-
else {
|
|
568
|
-
const resolvedObj = resolvePath(refPath, root);
|
|
569
|
-
if (typeof resolvedObj === 'object' && !resolvedObj.$ref) {
|
|
570
|
-
refMap.set($ref, resolvedObj);
|
|
571
|
-
}
|
|
572
|
-
const result = await dereferenceObject(resolvedObj, {
|
|
573
|
-
cwd,
|
|
574
|
-
externalFileCache,
|
|
575
|
-
refMap,
|
|
576
|
-
root,
|
|
577
|
-
fetch,
|
|
578
|
-
headers,
|
|
579
|
-
});
|
|
580
|
-
if (!result.title && obj.title) {
|
|
581
|
-
result.title = obj.title;
|
|
582
|
-
}
|
|
583
|
-
refMap.set($ref, result);
|
|
584
|
-
return result;
|
|
585
|
-
}
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
else {
|
|
589
|
-
await Promise.all(Object.entries(obj).map(async ([key, val]) => {
|
|
590
|
-
if (typeof val === 'object') {
|
|
591
|
-
obj[key] = await dereferenceObject(val, {
|
|
592
|
-
cwd,
|
|
593
|
-
externalFileCache,
|
|
594
|
-
refMap,
|
|
595
|
-
root,
|
|
596
|
-
fetch,
|
|
597
|
-
headers,
|
|
598
|
-
});
|
|
599
|
-
}
|
|
600
|
-
}));
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
return obj;
|
|
604
|
-
}
|
|
605
|
-
|
|
606
611
|
async function referenceJSONSchema(schema) {
|
|
607
612
|
const definitions = {};
|
|
608
613
|
const finalSchema = await visitJSONSchema(schema, subSchema => {
|
package/index.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { JsonPointer } from 'json-ptr';
|
|
2
2
|
import { AggregateError, readFileOrUrl } from '@graphql-mesh/utils';
|
|
3
3
|
import { isAbsolute, join, dirname } from 'path';
|
|
4
|
-
import { inspect } from 'util';
|
|
5
|
-
import toJsonSchema from 'to-json-schema';
|
|
6
4
|
import urlJoin from 'url-join';
|
|
7
5
|
import { fetch } from 'cross-undici-fetch';
|
|
6
|
+
import { inspect } from 'util';
|
|
7
|
+
import toJsonSchema from 'to-json-schema';
|
|
8
8
|
|
|
9
9
|
var OnCircularReference;
|
|
10
10
|
(function (OnCircularReference) {
|
|
@@ -305,6 +305,154 @@ async function compareJSONSchemas(oldSchema, newSchema) {
|
|
|
305
305
|
}
|
|
306
306
|
}
|
|
307
307
|
|
|
308
|
+
const resolvePath = (path, root) => {
|
|
309
|
+
return JsonPointer.get(root, path);
|
|
310
|
+
};
|
|
311
|
+
function isRefObject(obj) {
|
|
312
|
+
return typeof obj === 'object' && obj.$ref;
|
|
313
|
+
}
|
|
314
|
+
function isURL(str) {
|
|
315
|
+
return /^https?:\/\//.test(str);
|
|
316
|
+
}
|
|
317
|
+
const getAbsolute$Ref = (given$ref, baseFilePath) => {
|
|
318
|
+
const [givenExternalFileRelativePath, givenRefPath] = given$ref.split('#');
|
|
319
|
+
if (givenExternalFileRelativePath) {
|
|
320
|
+
const cwd = isURL(baseFilePath) ? getCwdForUrl(baseFilePath) : dirname(baseFilePath);
|
|
321
|
+
const givenExternalFilePath = getAbsolutePath(givenExternalFileRelativePath, cwd);
|
|
322
|
+
if (givenRefPath) {
|
|
323
|
+
return `${givenExternalFilePath}#${givenRefPath}`;
|
|
324
|
+
}
|
|
325
|
+
return givenExternalFilePath;
|
|
326
|
+
}
|
|
327
|
+
return `${baseFilePath}#${givenRefPath}`;
|
|
328
|
+
};
|
|
329
|
+
function getCwdForUrl(url) {
|
|
330
|
+
const urlParts = url.split('/');
|
|
331
|
+
urlParts.pop();
|
|
332
|
+
return urlParts.join('/');
|
|
333
|
+
}
|
|
334
|
+
function normalizeUrl(url) {
|
|
335
|
+
return new URL(url).toString();
|
|
336
|
+
}
|
|
337
|
+
function getAbsolutePath(path, cwd) {
|
|
338
|
+
if (isURL(path)) {
|
|
339
|
+
return path;
|
|
340
|
+
}
|
|
341
|
+
if (isURL(cwd)) {
|
|
342
|
+
return normalizeUrl(urlJoin(cwd, path));
|
|
343
|
+
}
|
|
344
|
+
if (isAbsolute(path)) {
|
|
345
|
+
return path;
|
|
346
|
+
}
|
|
347
|
+
return join(cwd, path);
|
|
348
|
+
}
|
|
349
|
+
function getCwd(path) {
|
|
350
|
+
return isURL(path) ? getCwdForUrl(path) : dirname(path);
|
|
351
|
+
}
|
|
352
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
353
|
+
async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache = new Map(), refMap = new Map(), root = obj, fetch: fetch$1 = fetch, headers, } = {}) {
|
|
354
|
+
if (typeof obj === 'object') {
|
|
355
|
+
if (isRefObject(obj)) {
|
|
356
|
+
const $ref = obj.$ref;
|
|
357
|
+
if (refMap.has($ref)) {
|
|
358
|
+
return refMap.get($ref);
|
|
359
|
+
}
|
|
360
|
+
else {
|
|
361
|
+
const [externalRelativeFilePath, refPath] = $ref.split('#');
|
|
362
|
+
if (externalRelativeFilePath) {
|
|
363
|
+
const externalFilePath = getAbsolutePath(externalRelativeFilePath, cwd);
|
|
364
|
+
const newCwd = getCwd(externalFilePath);
|
|
365
|
+
let externalFile = externalFileCache.get(externalFilePath);
|
|
366
|
+
if (!externalFile) {
|
|
367
|
+
externalFile = await readFileOrUrl(externalFilePath, {
|
|
368
|
+
fetch: fetch$1,
|
|
369
|
+
headers,
|
|
370
|
+
cwd,
|
|
371
|
+
}).catch(() => {
|
|
372
|
+
throw new Error(`Unable to load ${externalRelativeFilePath} from ${cwd}`);
|
|
373
|
+
});
|
|
374
|
+
externalFileCache.set(externalFilePath, externalFile);
|
|
375
|
+
}
|
|
376
|
+
const result = await dereferenceObject(refPath
|
|
377
|
+
? {
|
|
378
|
+
$ref: `#${refPath}`,
|
|
379
|
+
}
|
|
380
|
+
: externalFile, {
|
|
381
|
+
cwd: newCwd,
|
|
382
|
+
externalFileCache,
|
|
383
|
+
refMap: new Proxy(refMap, {
|
|
384
|
+
get: (originalRefMap, key) => {
|
|
385
|
+
switch (key) {
|
|
386
|
+
case 'has':
|
|
387
|
+
return (given$ref) => {
|
|
388
|
+
const original$Ref = getAbsolute$Ref(given$ref, externalFilePath);
|
|
389
|
+
return originalRefMap.has(original$Ref);
|
|
390
|
+
};
|
|
391
|
+
case 'get':
|
|
392
|
+
return (given$ref) => {
|
|
393
|
+
const original$Ref = getAbsolute$Ref(given$ref, externalFilePath);
|
|
394
|
+
return originalRefMap.get(original$Ref);
|
|
395
|
+
};
|
|
396
|
+
case 'set':
|
|
397
|
+
return (given$ref, val) => {
|
|
398
|
+
const original$Ref = getAbsolute$Ref(given$ref, externalFilePath);
|
|
399
|
+
return originalRefMap.set(original$Ref, val);
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
throw new Error('Not implemented ' + key.toString());
|
|
403
|
+
},
|
|
404
|
+
}),
|
|
405
|
+
fetch: fetch$1,
|
|
406
|
+
headers,
|
|
407
|
+
root: externalFile,
|
|
408
|
+
});
|
|
409
|
+
refMap.set($ref, result);
|
|
410
|
+
result.$resolvedRef = $ref;
|
|
411
|
+
if (obj.title) {
|
|
412
|
+
result.title = obj.title;
|
|
413
|
+
}
|
|
414
|
+
return result;
|
|
415
|
+
}
|
|
416
|
+
else {
|
|
417
|
+
const resolvedObj = resolvePath(refPath, root);
|
|
418
|
+
if (typeof resolvedObj === 'object' && !resolvedObj.$ref) {
|
|
419
|
+
refMap.set($ref, resolvedObj);
|
|
420
|
+
}
|
|
421
|
+
const result = await dereferenceObject(resolvedObj, {
|
|
422
|
+
cwd,
|
|
423
|
+
externalFileCache,
|
|
424
|
+
refMap,
|
|
425
|
+
root,
|
|
426
|
+
fetch: fetch$1,
|
|
427
|
+
headers,
|
|
428
|
+
});
|
|
429
|
+
if (obj.title) {
|
|
430
|
+
result.title = obj.title;
|
|
431
|
+
}
|
|
432
|
+
refMap.set($ref, result);
|
|
433
|
+
result.$resolvedRef = $ref;
|
|
434
|
+
return result;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
else {
|
|
439
|
+
await Promise.all(Object.entries(obj).map(async ([key, val]) => {
|
|
440
|
+
if (typeof val === 'object') {
|
|
441
|
+
obj[key] = await dereferenceObject(val, {
|
|
442
|
+
cwd,
|
|
443
|
+
externalFileCache,
|
|
444
|
+
refMap,
|
|
445
|
+
root,
|
|
446
|
+
fetch: fetch$1,
|
|
447
|
+
headers,
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
}));
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
return obj;
|
|
454
|
+
}
|
|
455
|
+
|
|
308
456
|
const reservedTypeNames = ['Query', 'Mutation', 'Subscription'];
|
|
309
457
|
function deduplicateJSONSchema(schema, seenMap = new Map()) {
|
|
310
458
|
if (typeof schema === 'object' && schema != null) {
|
|
@@ -357,11 +505,12 @@ async function healJSONSchema(schema) {
|
|
|
357
505
|
delete subSchema.title;
|
|
358
506
|
}
|
|
359
507
|
if (!subSchema.title && !subSchema.$ref) {
|
|
508
|
+
const realPath = subSchema.$resolvedRef || path;
|
|
360
509
|
// Try to get definition name if missing
|
|
361
510
|
const splitByDefinitions = path.includes('/components/schemas/')
|
|
362
|
-
?
|
|
363
|
-
:
|
|
364
|
-
const maybeDefinitionBasedPath = splitByDefinitions.length > 1 ? splitByDefinitions[splitByDefinitions.length - 1] :
|
|
511
|
+
? realPath.split('/components/schemas/')
|
|
512
|
+
: realPath.split('/definitions/');
|
|
513
|
+
const maybeDefinitionBasedPath = splitByDefinitions.length > 1 ? splitByDefinitions[splitByDefinitions.length - 1] : realPath;
|
|
365
514
|
let pathBasedName = maybeDefinitionBasedPath
|
|
366
515
|
.split('/properties')
|
|
367
516
|
.join('')
|
|
@@ -454,150 +603,6 @@ async function healJSONSchema(schema) {
|
|
|
454
603
|
});
|
|
455
604
|
}
|
|
456
605
|
|
|
457
|
-
const resolvePath = (path, root) => {
|
|
458
|
-
return JsonPointer.get(root, path);
|
|
459
|
-
};
|
|
460
|
-
function isRefObject(obj) {
|
|
461
|
-
return typeof obj === 'object' && obj.$ref;
|
|
462
|
-
}
|
|
463
|
-
function isURL(str) {
|
|
464
|
-
return /^https?:\/\//.test(str);
|
|
465
|
-
}
|
|
466
|
-
const getAbsolute$Ref = (given$ref, baseFilePath) => {
|
|
467
|
-
const [givenExternalFileRelativePath, givenRefPath] = given$ref.split('#');
|
|
468
|
-
if (givenExternalFileRelativePath) {
|
|
469
|
-
const cwd = isURL(baseFilePath) ? getCwdForUrl(baseFilePath) : dirname(baseFilePath);
|
|
470
|
-
const givenExternalFilePath = getAbsolutePath(givenExternalFileRelativePath, cwd);
|
|
471
|
-
if (givenRefPath) {
|
|
472
|
-
return `${givenExternalFilePath}#${givenRefPath}`;
|
|
473
|
-
}
|
|
474
|
-
return givenExternalFilePath;
|
|
475
|
-
}
|
|
476
|
-
return `${baseFilePath}#${givenRefPath}`;
|
|
477
|
-
};
|
|
478
|
-
function getCwdForUrl(url) {
|
|
479
|
-
const urlParts = url.split('/');
|
|
480
|
-
urlParts.pop();
|
|
481
|
-
return urlParts.join('/');
|
|
482
|
-
}
|
|
483
|
-
function normalizeUrl(url) {
|
|
484
|
-
return new URL(url).toString();
|
|
485
|
-
}
|
|
486
|
-
function getAbsolutePath(path, cwd) {
|
|
487
|
-
if (isURL(path)) {
|
|
488
|
-
return path;
|
|
489
|
-
}
|
|
490
|
-
if (isURL(cwd)) {
|
|
491
|
-
return normalizeUrl(urlJoin(cwd, path));
|
|
492
|
-
}
|
|
493
|
-
if (isAbsolute(path)) {
|
|
494
|
-
return path;
|
|
495
|
-
}
|
|
496
|
-
return join(cwd, path);
|
|
497
|
-
}
|
|
498
|
-
function getCwd(path) {
|
|
499
|
-
return isURL(path) ? getCwdForUrl(path) : dirname(path);
|
|
500
|
-
}
|
|
501
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
502
|
-
async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache = new Map(), refMap = new Map(), root = obj, fetch: fetch$1 = fetch, headers, } = {}) {
|
|
503
|
-
if (typeof obj === 'object') {
|
|
504
|
-
if (isRefObject(obj)) {
|
|
505
|
-
const $ref = obj.$ref;
|
|
506
|
-
if (refMap.has($ref)) {
|
|
507
|
-
return refMap.get($ref);
|
|
508
|
-
}
|
|
509
|
-
else {
|
|
510
|
-
const [externalRelativeFilePath, refPath] = $ref.split('#');
|
|
511
|
-
if (externalRelativeFilePath) {
|
|
512
|
-
const externalFilePath = getAbsolutePath(externalRelativeFilePath, cwd);
|
|
513
|
-
const newCwd = getCwd(externalFilePath);
|
|
514
|
-
let externalFile = externalFileCache.get(externalFilePath);
|
|
515
|
-
if (!externalFile) {
|
|
516
|
-
externalFile = await readFileOrUrl(externalFilePath, {
|
|
517
|
-
fetch: fetch$1,
|
|
518
|
-
headers,
|
|
519
|
-
cwd,
|
|
520
|
-
}).catch(() => {
|
|
521
|
-
throw new Error(`Unable to load ${externalRelativeFilePath} from ${cwd}`);
|
|
522
|
-
});
|
|
523
|
-
externalFile = await healJSONSchema(externalFile);
|
|
524
|
-
externalFileCache.set(externalFilePath, externalFile);
|
|
525
|
-
}
|
|
526
|
-
const result = await dereferenceObject(refPath
|
|
527
|
-
? {
|
|
528
|
-
$ref: `#${refPath}`,
|
|
529
|
-
}
|
|
530
|
-
: externalFile, {
|
|
531
|
-
cwd: newCwd,
|
|
532
|
-
externalFileCache,
|
|
533
|
-
refMap: new Proxy(refMap, {
|
|
534
|
-
get: (originalRefMap, key) => {
|
|
535
|
-
switch (key) {
|
|
536
|
-
case 'has':
|
|
537
|
-
return (given$ref) => {
|
|
538
|
-
const original$Ref = getAbsolute$Ref(given$ref, externalFilePath);
|
|
539
|
-
return originalRefMap.has(original$Ref);
|
|
540
|
-
};
|
|
541
|
-
case 'get':
|
|
542
|
-
return (given$ref) => {
|
|
543
|
-
const original$Ref = getAbsolute$Ref(given$ref, externalFilePath);
|
|
544
|
-
return originalRefMap.get(original$Ref);
|
|
545
|
-
};
|
|
546
|
-
case 'set':
|
|
547
|
-
return (given$ref, val) => {
|
|
548
|
-
const original$Ref = getAbsolute$Ref(given$ref, externalFilePath);
|
|
549
|
-
return originalRefMap.set(original$Ref, val);
|
|
550
|
-
};
|
|
551
|
-
}
|
|
552
|
-
throw new Error('Not implemented ' + key.toString());
|
|
553
|
-
},
|
|
554
|
-
}),
|
|
555
|
-
fetch: fetch$1,
|
|
556
|
-
headers,
|
|
557
|
-
root: externalFile,
|
|
558
|
-
});
|
|
559
|
-
refMap.set($ref, result);
|
|
560
|
-
return result;
|
|
561
|
-
}
|
|
562
|
-
else {
|
|
563
|
-
const resolvedObj = resolvePath(refPath, root);
|
|
564
|
-
if (typeof resolvedObj === 'object' && !resolvedObj.$ref) {
|
|
565
|
-
refMap.set($ref, resolvedObj);
|
|
566
|
-
}
|
|
567
|
-
const result = await dereferenceObject(resolvedObj, {
|
|
568
|
-
cwd,
|
|
569
|
-
externalFileCache,
|
|
570
|
-
refMap,
|
|
571
|
-
root,
|
|
572
|
-
fetch: fetch$1,
|
|
573
|
-
headers,
|
|
574
|
-
});
|
|
575
|
-
if (!result.title && obj.title) {
|
|
576
|
-
result.title = obj.title;
|
|
577
|
-
}
|
|
578
|
-
refMap.set($ref, result);
|
|
579
|
-
return result;
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
else {
|
|
584
|
-
await Promise.all(Object.entries(obj).map(async ([key, val]) => {
|
|
585
|
-
if (typeof val === 'object') {
|
|
586
|
-
obj[key] = await dereferenceObject(val, {
|
|
587
|
-
cwd,
|
|
588
|
-
externalFileCache,
|
|
589
|
-
refMap,
|
|
590
|
-
root,
|
|
591
|
-
fetch: fetch$1,
|
|
592
|
-
headers,
|
|
593
|
-
});
|
|
594
|
-
}
|
|
595
|
-
}));
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
return obj;
|
|
599
|
-
}
|
|
600
|
-
|
|
601
606
|
async function referenceJSONSchema(schema) {
|
|
602
607
|
const definitions = {};
|
|
603
608
|
const finalSchema = await visitJSONSchema(schema, subSchema => {
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-machete",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.6-alpha-983cd38bc.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@graphql-mesh/utils": "0.26.0",
|
|
7
7
|
"@json-schema-tools/meta-schema": "1.6.19",
|
|
8
|
-
"cross-undici-fetch": "0.1.
|
|
8
|
+
"cross-undici-fetch": "0.1.15",
|
|
9
9
|
"json-ptr": "3.0.1",
|
|
10
10
|
"to-json-schema": "0.2.5",
|
|
11
11
|
"url-join": "4.0.1"
|