json-machete 0.1.0 → 0.1.1-alpha-016c0bc68.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 +37 -8
- package/index.mjs +38 -11
- package/package.json +2 -2
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
|
@@ -299,7 +299,14 @@ async function compareJSONSchemas(oldSchema, newSchema) {
|
|
|
299
299
|
const reservedTypeNames = ['Query', 'Mutation', 'Subscription'];
|
|
300
300
|
function deduplicateJSONSchema(schema, seenMap = new Map()) {
|
|
301
301
|
if (typeof schema === 'object' && schema != null) {
|
|
302
|
+
const titleReserved = schema.title;
|
|
303
|
+
if (titleReserved) {
|
|
304
|
+
schema.title = undefined;
|
|
305
|
+
}
|
|
302
306
|
const stringified = util.inspect(schema, undefined, 3);
|
|
307
|
+
if (titleReserved) {
|
|
308
|
+
schema.title = titleReserved;
|
|
309
|
+
}
|
|
303
310
|
const seen = seenMap.get(stringified);
|
|
304
311
|
if (seen) {
|
|
305
312
|
return seen;
|
|
@@ -374,7 +381,7 @@ async function healJSONSchema(schema) {
|
|
|
374
381
|
if (reservedTypeNames.includes(pathBasedName)) {
|
|
375
382
|
pathBasedName += '_';
|
|
376
383
|
}
|
|
377
|
-
subSchema.title = subSchema.title || pathBasedName
|
|
384
|
+
subSchema.title = subSchema.title || pathBasedName;
|
|
378
385
|
}
|
|
379
386
|
// Try to find the type
|
|
380
387
|
if (!subSchema.type) {
|
|
@@ -394,7 +401,7 @@ async function healJSONSchema(schema) {
|
|
|
394
401
|
}
|
|
395
402
|
}
|
|
396
403
|
// Properties only exist in objects
|
|
397
|
-
if (subSchema.properties) {
|
|
404
|
+
if (subSchema.properties || subSchema.patternProperties || 'additionalProperties' in subSchema) {
|
|
398
405
|
subSchema.type = 'object';
|
|
399
406
|
}
|
|
400
407
|
// Items only exist in arrays
|
|
@@ -453,8 +460,11 @@ function getAbsolutePath(path$1, cwd) {
|
|
|
453
460
|
}
|
|
454
461
|
return path.join(cwd, path$1);
|
|
455
462
|
}
|
|
463
|
+
function getCwd(path$1) {
|
|
464
|
+
return isURL(path$1) ? getCwdForUrl(path$1) : path.dirname(path$1);
|
|
465
|
+
}
|
|
456
466
|
// 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,
|
|
467
|
+
async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache = new Map(), refMap = new Map(), root = obj, fetch = crossUndiciFetch.fetch, headers, } = {}) {
|
|
458
468
|
if (typeof obj === 'object') {
|
|
459
469
|
if (isRefObject(obj)) {
|
|
460
470
|
const $ref = obj.$ref;
|
|
@@ -465,12 +475,17 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
|
|
|
465
475
|
const [externalRelativeFilePath, refPath] = $ref.split('#');
|
|
466
476
|
if (externalRelativeFilePath) {
|
|
467
477
|
const externalFilePath = getAbsolutePath(externalRelativeFilePath, cwd);
|
|
468
|
-
const newCwd =
|
|
478
|
+
const newCwd = getCwd(externalFilePath);
|
|
469
479
|
let externalFile = externalFileCache.get(externalFilePath);
|
|
470
480
|
if (!externalFile) {
|
|
471
|
-
externalFile =
|
|
472
|
-
|
|
473
|
-
|
|
481
|
+
externalFile = await utils.readFileOrUrl(externalFilePath, {
|
|
482
|
+
fetch,
|
|
483
|
+
headers,
|
|
484
|
+
cwd,
|
|
485
|
+
fallbackFormat: 'json',
|
|
486
|
+
}).catch(() => {
|
|
487
|
+
throw new Error(`Unable to load ${externalRelativeFilePath} from ${cwd}`);
|
|
488
|
+
});
|
|
474
489
|
externalFile = await healJSONSchema(externalFile);
|
|
475
490
|
externalFileCache.set(externalFilePath, externalFile);
|
|
476
491
|
}
|
|
@@ -505,10 +520,14 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
|
|
|
505
520
|
},
|
|
506
521
|
}),
|
|
507
522
|
fetch,
|
|
523
|
+
headers,
|
|
508
524
|
});
|
|
509
525
|
}
|
|
510
526
|
else {
|
|
511
527
|
const result = resolvePath(refPath, root);
|
|
528
|
+
if (!result.title && obj.title) {
|
|
529
|
+
result.title = obj.title;
|
|
530
|
+
}
|
|
512
531
|
refMap.set($ref, result);
|
|
513
532
|
return dereferenceObject(result, {
|
|
514
533
|
cwd,
|
|
@@ -516,6 +535,7 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
|
|
|
516
535
|
refMap,
|
|
517
536
|
root,
|
|
518
537
|
fetch,
|
|
538
|
+
headers,
|
|
519
539
|
});
|
|
520
540
|
}
|
|
521
541
|
}
|
|
@@ -523,7 +543,14 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
|
|
|
523
543
|
else {
|
|
524
544
|
await Promise.all(Object.entries(obj).map(async ([key, val]) => {
|
|
525
545
|
if (typeof val === 'object') {
|
|
526
|
-
obj[key] = await dereferenceObject(val, {
|
|
546
|
+
obj[key] = await dereferenceObject(val, {
|
|
547
|
+
cwd,
|
|
548
|
+
externalFileCache,
|
|
549
|
+
refMap,
|
|
550
|
+
root,
|
|
551
|
+
fetch,
|
|
552
|
+
headers,
|
|
553
|
+
});
|
|
527
554
|
}
|
|
528
555
|
}));
|
|
529
556
|
}
|
|
@@ -571,6 +598,8 @@ async function referenceJSONSchema(schema) {
|
|
|
571
598
|
exports.FIRST_VISITED_PATH = FIRST_VISITED_PATH;
|
|
572
599
|
exports.compareJSONSchemas = compareJSONSchemas;
|
|
573
600
|
exports.dereferenceObject = dereferenceObject;
|
|
601
|
+
exports.getAbsolutePath = getAbsolutePath;
|
|
602
|
+
exports.getCwd = getCwd;
|
|
574
603
|
exports.healJSONSchema = healJSONSchema;
|
|
575
604
|
exports.referenceJSONSchema = referenceJSONSchema;
|
|
576
605
|
exports.resolvePath = resolvePath;
|
package/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { JsonPointer } from 'json-ptr';
|
|
2
|
-
import { AggregateError,
|
|
3
|
-
import {
|
|
2
|
+
import { AggregateError, readFileOrUrl } 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';
|
|
@@ -294,7 +294,14 @@ async function compareJSONSchemas(oldSchema, newSchema) {
|
|
|
294
294
|
const reservedTypeNames = ['Query', 'Mutation', 'Subscription'];
|
|
295
295
|
function deduplicateJSONSchema(schema, seenMap = new Map()) {
|
|
296
296
|
if (typeof schema === 'object' && schema != null) {
|
|
297
|
+
const titleReserved = schema.title;
|
|
298
|
+
if (titleReserved) {
|
|
299
|
+
schema.title = undefined;
|
|
300
|
+
}
|
|
297
301
|
const stringified = inspect(schema, undefined, 3);
|
|
302
|
+
if (titleReserved) {
|
|
303
|
+
schema.title = titleReserved;
|
|
304
|
+
}
|
|
298
305
|
const seen = seenMap.get(stringified);
|
|
299
306
|
if (seen) {
|
|
300
307
|
return seen;
|
|
@@ -369,7 +376,7 @@ async function healJSONSchema(schema) {
|
|
|
369
376
|
if (reservedTypeNames.includes(pathBasedName)) {
|
|
370
377
|
pathBasedName += '_';
|
|
371
378
|
}
|
|
372
|
-
subSchema.title = subSchema.title || pathBasedName
|
|
379
|
+
subSchema.title = subSchema.title || pathBasedName;
|
|
373
380
|
}
|
|
374
381
|
// Try to find the type
|
|
375
382
|
if (!subSchema.type) {
|
|
@@ -389,7 +396,7 @@ async function healJSONSchema(schema) {
|
|
|
389
396
|
}
|
|
390
397
|
}
|
|
391
398
|
// Properties only exist in objects
|
|
392
|
-
if (subSchema.properties) {
|
|
399
|
+
if (subSchema.properties || subSchema.patternProperties || 'additionalProperties' in subSchema) {
|
|
393
400
|
subSchema.type = 'object';
|
|
394
401
|
}
|
|
395
402
|
// Items only exist in arrays
|
|
@@ -448,8 +455,11 @@ function getAbsolutePath(path, cwd) {
|
|
|
448
455
|
}
|
|
449
456
|
return join(cwd, path);
|
|
450
457
|
}
|
|
458
|
+
function getCwd(path) {
|
|
459
|
+
return isURL(path) ? getCwdForUrl(path) : dirname(path);
|
|
460
|
+
}
|
|
451
461
|
// 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,
|
|
462
|
+
async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache = new Map(), refMap = new Map(), root = obj, fetch: fetch$1 = fetch, headers, } = {}) {
|
|
453
463
|
if (typeof obj === 'object') {
|
|
454
464
|
if (isRefObject(obj)) {
|
|
455
465
|
const $ref = obj.$ref;
|
|
@@ -460,12 +470,17 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
|
|
|
460
470
|
const [externalRelativeFilePath, refPath] = $ref.split('#');
|
|
461
471
|
if (externalRelativeFilePath) {
|
|
462
472
|
const externalFilePath = getAbsolutePath(externalRelativeFilePath, cwd);
|
|
463
|
-
const newCwd =
|
|
473
|
+
const newCwd = getCwd(externalFilePath);
|
|
464
474
|
let externalFile = externalFileCache.get(externalFilePath);
|
|
465
475
|
if (!externalFile) {
|
|
466
|
-
externalFile =
|
|
467
|
-
|
|
468
|
-
|
|
476
|
+
externalFile = await readFileOrUrl(externalFilePath, {
|
|
477
|
+
fetch: fetch$1,
|
|
478
|
+
headers,
|
|
479
|
+
cwd,
|
|
480
|
+
fallbackFormat: 'json',
|
|
481
|
+
}).catch(() => {
|
|
482
|
+
throw new Error(`Unable to load ${externalRelativeFilePath} from ${cwd}`);
|
|
483
|
+
});
|
|
469
484
|
externalFile = await healJSONSchema(externalFile);
|
|
470
485
|
externalFileCache.set(externalFilePath, externalFile);
|
|
471
486
|
}
|
|
@@ -500,10 +515,14 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
|
|
|
500
515
|
},
|
|
501
516
|
}),
|
|
502
517
|
fetch: fetch$1,
|
|
518
|
+
headers,
|
|
503
519
|
});
|
|
504
520
|
}
|
|
505
521
|
else {
|
|
506
522
|
const result = resolvePath(refPath, root);
|
|
523
|
+
if (!result.title && obj.title) {
|
|
524
|
+
result.title = obj.title;
|
|
525
|
+
}
|
|
507
526
|
refMap.set($ref, result);
|
|
508
527
|
return dereferenceObject(result, {
|
|
509
528
|
cwd,
|
|
@@ -511,6 +530,7 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
|
|
|
511
530
|
refMap,
|
|
512
531
|
root,
|
|
513
532
|
fetch: fetch$1,
|
|
533
|
+
headers,
|
|
514
534
|
});
|
|
515
535
|
}
|
|
516
536
|
}
|
|
@@ -518,7 +538,14 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
|
|
|
518
538
|
else {
|
|
519
539
|
await Promise.all(Object.entries(obj).map(async ([key, val]) => {
|
|
520
540
|
if (typeof val === 'object') {
|
|
521
|
-
obj[key] = await dereferenceObject(val, {
|
|
541
|
+
obj[key] = await dereferenceObject(val, {
|
|
542
|
+
cwd,
|
|
543
|
+
externalFileCache,
|
|
544
|
+
refMap,
|
|
545
|
+
root,
|
|
546
|
+
fetch: fetch$1,
|
|
547
|
+
headers,
|
|
548
|
+
});
|
|
522
549
|
}
|
|
523
550
|
}));
|
|
524
551
|
}
|
|
@@ -563,4 +590,4 @@ async function referenceJSONSchema(schema) {
|
|
|
563
590
|
return finalSchema;
|
|
564
591
|
}
|
|
565
592
|
|
|
566
|
-
export { FIRST_VISITED_PATH, OnCircularReference, compareJSONSchemas, dereferenceObject, healJSONSchema, referenceJSONSchema, resolvePath, visitJSONSchema };
|
|
593
|
+
export { FIRST_VISITED_PATH, OnCircularReference, compareJSONSchemas, dereferenceObject, getAbsolutePath, getCwd, healJSONSchema, referenceJSONSchema, resolvePath, visitJSONSchema };
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-machete",
|
|
3
|
-
"version": "0.1.0",
|
|
3
|
+
"version": "0.1.1-alpha-016c0bc68.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"@graphql-mesh/utils": "0.22.0",
|
|
6
|
+
"@graphql-mesh/utils": "0.22.1-alpha-016c0bc68.0",
|
|
7
7
|
"@json-schema-tools/meta-schema": "1.6.19",
|
|
8
8
|
"cross-undici-fetch": "0.0.26",
|
|
9
9
|
"json-ptr": "2.2.0",
|