json-machete 0.1.0 → 0.2.0-alpha-72c3f2bb5.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/dereferenceObject.d.ts +4 -2
- package/index.js +34 -6
- package/index.mjs +35 -9
- package/package.json +1 -1
package/dereferenceObject.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export declare const resolvePath: (path: string, root: any) => any;
|
|
2
|
-
export declare function
|
|
2
|
+
export declare function getAbsolutePath(path: string, cwd: string): string;
|
|
3
|
+
export declare function getCwd(path: string): string;
|
|
4
|
+
export declare function dereferenceObject<T extends object, TRoot = T>(obj: T, { cwd, externalFileCache, refMap, root, fetch, headers, }?: {
|
|
3
5
|
cwd?: string;
|
|
4
6
|
externalFileCache?: Map<string, any>;
|
|
5
7
|
refMap?: Map<string, any>;
|
|
6
8
|
root?: TRoot;
|
|
7
9
|
fetch?: WindowOrWorkerGlobalScope['fetch'];
|
|
8
|
-
|
|
10
|
+
headers?: Record<string, string>;
|
|
9
11
|
}): Promise<T>;
|
package/index.js
CHANGED
|
@@ -10,6 +10,7 @@ const path = require('path');
|
|
|
10
10
|
const util = require('util');
|
|
11
11
|
const urlJoin = _interopDefault(require('url-join'));
|
|
12
12
|
const crossUndiciFetch = require('cross-undici-fetch');
|
|
13
|
+
const fs = require('fs');
|
|
13
14
|
|
|
14
15
|
(function (OnCircularReference) {
|
|
15
16
|
OnCircularReference["WARN"] = "WARN";
|
|
@@ -299,7 +300,14 @@ async function compareJSONSchemas(oldSchema, newSchema) {
|
|
|
299
300
|
const reservedTypeNames = ['Query', 'Mutation', 'Subscription'];
|
|
300
301
|
function deduplicateJSONSchema(schema, seenMap = new Map()) {
|
|
301
302
|
if (typeof schema === 'object' && schema != null) {
|
|
303
|
+
const titleReserved = schema.title;
|
|
304
|
+
if (titleReserved) {
|
|
305
|
+
schema.title = undefined;
|
|
306
|
+
}
|
|
302
307
|
const stringified = util.inspect(schema, undefined, 3);
|
|
308
|
+
if (titleReserved) {
|
|
309
|
+
schema.title = titleReserved;
|
|
310
|
+
}
|
|
303
311
|
const seen = seenMap.get(stringified);
|
|
304
312
|
if (seen) {
|
|
305
313
|
return seen;
|
|
@@ -374,7 +382,7 @@ async function healJSONSchema(schema) {
|
|
|
374
382
|
if (reservedTypeNames.includes(pathBasedName)) {
|
|
375
383
|
pathBasedName += '_';
|
|
376
384
|
}
|
|
377
|
-
subSchema.title = subSchema.title || pathBasedName
|
|
385
|
+
subSchema.title = subSchema.title || pathBasedName;
|
|
378
386
|
}
|
|
379
387
|
// Try to find the type
|
|
380
388
|
if (!subSchema.type) {
|
|
@@ -412,6 +420,7 @@ async function healJSONSchema(schema) {
|
|
|
412
420
|
});
|
|
413
421
|
}
|
|
414
422
|
|
|
423
|
+
const { readFile } = fs.promises;
|
|
415
424
|
const resolvePath = (path, root) => {
|
|
416
425
|
return jsonPtr.JsonPointer.get(root, path);
|
|
417
426
|
};
|
|
@@ -453,8 +462,11 @@ function getAbsolutePath(path$1, cwd) {
|
|
|
453
462
|
}
|
|
454
463
|
return path.join(cwd, path$1);
|
|
455
464
|
}
|
|
465
|
+
function getCwd(path$1) {
|
|
466
|
+
return isURL(path$1) ? getCwdForUrl(path$1) : path.dirname(path$1);
|
|
467
|
+
}
|
|
456
468
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
457
|
-
async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache = new Map(), refMap = new Map(), root = obj, fetch = crossUndiciFetch.fetch,
|
|
469
|
+
async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache = new Map(), refMap = new Map(), root = obj, fetch = crossUndiciFetch.fetch, headers, } = {}) {
|
|
458
470
|
if (typeof obj === 'object') {
|
|
459
471
|
if (isRefObject(obj)) {
|
|
460
472
|
const $ref = obj.$ref;
|
|
@@ -465,12 +477,14 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
|
|
|
465
477
|
const [externalRelativeFilePath, refPath] = $ref.split('#');
|
|
466
478
|
if (externalRelativeFilePath) {
|
|
467
479
|
const externalFilePath = getAbsolutePath(externalRelativeFilePath, cwd);
|
|
468
|
-
const newCwd =
|
|
480
|
+
const newCwd = getCwd(externalFilePath);
|
|
469
481
|
let externalFile = externalFileCache.get(externalFilePath);
|
|
470
482
|
if (!externalFile) {
|
|
471
483
|
externalFile = isURL(externalFilePath)
|
|
472
|
-
? await fetch(externalFilePath
|
|
473
|
-
|
|
484
|
+
? await fetch(externalFilePath, {
|
|
485
|
+
headers,
|
|
486
|
+
}).then(res => res.json())
|
|
487
|
+
: await JSON.parse(await readFile(externalFilePath, 'utf-8'));
|
|
474
488
|
externalFile = await healJSONSchema(externalFile);
|
|
475
489
|
externalFileCache.set(externalFilePath, externalFile);
|
|
476
490
|
}
|
|
@@ -505,10 +519,14 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
|
|
|
505
519
|
},
|
|
506
520
|
}),
|
|
507
521
|
fetch,
|
|
522
|
+
headers,
|
|
508
523
|
});
|
|
509
524
|
}
|
|
510
525
|
else {
|
|
511
526
|
const result = resolvePath(refPath, root);
|
|
527
|
+
if (!result.title && obj.title) {
|
|
528
|
+
result.title = obj.title;
|
|
529
|
+
}
|
|
512
530
|
refMap.set($ref, result);
|
|
513
531
|
return dereferenceObject(result, {
|
|
514
532
|
cwd,
|
|
@@ -516,6 +534,7 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
|
|
|
516
534
|
refMap,
|
|
517
535
|
root,
|
|
518
536
|
fetch,
|
|
537
|
+
headers,
|
|
519
538
|
});
|
|
520
539
|
}
|
|
521
540
|
}
|
|
@@ -523,7 +542,14 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
|
|
|
523
542
|
else {
|
|
524
543
|
await Promise.all(Object.entries(obj).map(async ([key, val]) => {
|
|
525
544
|
if (typeof val === 'object') {
|
|
526
|
-
obj[key] = await dereferenceObject(val, {
|
|
545
|
+
obj[key] = await dereferenceObject(val, {
|
|
546
|
+
cwd,
|
|
547
|
+
externalFileCache,
|
|
548
|
+
refMap,
|
|
549
|
+
root,
|
|
550
|
+
fetch,
|
|
551
|
+
headers,
|
|
552
|
+
});
|
|
527
553
|
}
|
|
528
554
|
}));
|
|
529
555
|
}
|
|
@@ -571,6 +597,8 @@ async function referenceJSONSchema(schema) {
|
|
|
571
597
|
exports.FIRST_VISITED_PATH = FIRST_VISITED_PATH;
|
|
572
598
|
exports.compareJSONSchemas = compareJSONSchemas;
|
|
573
599
|
exports.dereferenceObject = dereferenceObject;
|
|
600
|
+
exports.getAbsolutePath = getAbsolutePath;
|
|
601
|
+
exports.getCwd = getCwd;
|
|
574
602
|
exports.healJSONSchema = healJSONSchema;
|
|
575
603
|
exports.referenceJSONSchema = referenceJSONSchema;
|
|
576
604
|
exports.resolvePath = resolvePath;
|
package/index.mjs
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { JsonPointer } from 'json-ptr';
|
|
2
|
-
import { AggregateError
|
|
3
|
-
import {
|
|
2
|
+
import { AggregateError } from '@graphql-mesh/utils';
|
|
3
|
+
import { isAbsolute, join, dirname } from 'path';
|
|
4
4
|
import { inspect } from 'util';
|
|
5
5
|
import urlJoin from 'url-join';
|
|
6
6
|
import { fetch } from 'cross-undici-fetch';
|
|
7
|
+
import { promises } from 'fs';
|
|
7
8
|
|
|
8
9
|
var OnCircularReference;
|
|
9
10
|
(function (OnCircularReference) {
|
|
@@ -294,7 +295,14 @@ async function compareJSONSchemas(oldSchema, newSchema) {
|
|
|
294
295
|
const reservedTypeNames = ['Query', 'Mutation', 'Subscription'];
|
|
295
296
|
function deduplicateJSONSchema(schema, seenMap = new Map()) {
|
|
296
297
|
if (typeof schema === 'object' && schema != null) {
|
|
298
|
+
const titleReserved = schema.title;
|
|
299
|
+
if (titleReserved) {
|
|
300
|
+
schema.title = undefined;
|
|
301
|
+
}
|
|
297
302
|
const stringified = inspect(schema, undefined, 3);
|
|
303
|
+
if (titleReserved) {
|
|
304
|
+
schema.title = titleReserved;
|
|
305
|
+
}
|
|
298
306
|
const seen = seenMap.get(stringified);
|
|
299
307
|
if (seen) {
|
|
300
308
|
return seen;
|
|
@@ -369,7 +377,7 @@ async function healJSONSchema(schema) {
|
|
|
369
377
|
if (reservedTypeNames.includes(pathBasedName)) {
|
|
370
378
|
pathBasedName += '_';
|
|
371
379
|
}
|
|
372
|
-
subSchema.title = subSchema.title || pathBasedName
|
|
380
|
+
subSchema.title = subSchema.title || pathBasedName;
|
|
373
381
|
}
|
|
374
382
|
// Try to find the type
|
|
375
383
|
if (!subSchema.type) {
|
|
@@ -407,6 +415,7 @@ async function healJSONSchema(schema) {
|
|
|
407
415
|
});
|
|
408
416
|
}
|
|
409
417
|
|
|
418
|
+
const { readFile } = promises;
|
|
410
419
|
const resolvePath = (path, root) => {
|
|
411
420
|
return JsonPointer.get(root, path);
|
|
412
421
|
};
|
|
@@ -448,8 +457,11 @@ function getAbsolutePath(path, cwd) {
|
|
|
448
457
|
}
|
|
449
458
|
return join(cwd, path);
|
|
450
459
|
}
|
|
460
|
+
function getCwd(path) {
|
|
461
|
+
return isURL(path) ? getCwdForUrl(path) : dirname(path);
|
|
462
|
+
}
|
|
451
463
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
452
|
-
async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache = new Map(), refMap = new Map(), root = obj, fetch: fetch$1 = fetch,
|
|
464
|
+
async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache = new Map(), refMap = new Map(), root = obj, fetch: fetch$1 = fetch, headers, } = {}) {
|
|
453
465
|
if (typeof obj === 'object') {
|
|
454
466
|
if (isRefObject(obj)) {
|
|
455
467
|
const $ref = obj.$ref;
|
|
@@ -460,12 +472,14 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
|
|
|
460
472
|
const [externalRelativeFilePath, refPath] = $ref.split('#');
|
|
461
473
|
if (externalRelativeFilePath) {
|
|
462
474
|
const externalFilePath = getAbsolutePath(externalRelativeFilePath, cwd);
|
|
463
|
-
const newCwd =
|
|
475
|
+
const newCwd = getCwd(externalFilePath);
|
|
464
476
|
let externalFile = externalFileCache.get(externalFilePath);
|
|
465
477
|
if (!externalFile) {
|
|
466
478
|
externalFile = isURL(externalFilePath)
|
|
467
|
-
? await fetch$1(externalFilePath
|
|
468
|
-
|
|
479
|
+
? await fetch$1(externalFilePath, {
|
|
480
|
+
headers,
|
|
481
|
+
}).then(res => res.json())
|
|
482
|
+
: await JSON.parse(await readFile(externalFilePath, 'utf-8'));
|
|
469
483
|
externalFile = await healJSONSchema(externalFile);
|
|
470
484
|
externalFileCache.set(externalFilePath, externalFile);
|
|
471
485
|
}
|
|
@@ -500,10 +514,14 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
|
|
|
500
514
|
},
|
|
501
515
|
}),
|
|
502
516
|
fetch: fetch$1,
|
|
517
|
+
headers,
|
|
503
518
|
});
|
|
504
519
|
}
|
|
505
520
|
else {
|
|
506
521
|
const result = resolvePath(refPath, root);
|
|
522
|
+
if (!result.title && obj.title) {
|
|
523
|
+
result.title = obj.title;
|
|
524
|
+
}
|
|
507
525
|
refMap.set($ref, result);
|
|
508
526
|
return dereferenceObject(result, {
|
|
509
527
|
cwd,
|
|
@@ -511,6 +529,7 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
|
|
|
511
529
|
refMap,
|
|
512
530
|
root,
|
|
513
531
|
fetch: fetch$1,
|
|
532
|
+
headers,
|
|
514
533
|
});
|
|
515
534
|
}
|
|
516
535
|
}
|
|
@@ -518,7 +537,14 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
|
|
|
518
537
|
else {
|
|
519
538
|
await Promise.all(Object.entries(obj).map(async ([key, val]) => {
|
|
520
539
|
if (typeof val === 'object') {
|
|
521
|
-
obj[key] = await dereferenceObject(val, {
|
|
540
|
+
obj[key] = await dereferenceObject(val, {
|
|
541
|
+
cwd,
|
|
542
|
+
externalFileCache,
|
|
543
|
+
refMap,
|
|
544
|
+
root,
|
|
545
|
+
fetch: fetch$1,
|
|
546
|
+
headers,
|
|
547
|
+
});
|
|
522
548
|
}
|
|
523
549
|
}));
|
|
524
550
|
}
|
|
@@ -563,4 +589,4 @@ async function referenceJSONSchema(schema) {
|
|
|
563
589
|
return finalSchema;
|
|
564
590
|
}
|
|
565
591
|
|
|
566
|
-
export { FIRST_VISITED_PATH, OnCircularReference, compareJSONSchemas, dereferenceObject, healJSONSchema, referenceJSONSchema, resolvePath, visitJSONSchema };
|
|
592
|
+
export { FIRST_VISITED_PATH, OnCircularReference, compareJSONSchemas, dereferenceObject, getAbsolutePath, getCwd, healJSONSchema, referenceJSONSchema, resolvePath, visitJSONSchema };
|