json-machete 0.2.0 → 0.2.1-alpha-044ef7368.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.
@@ -1,10 +1,11 @@
1
1
  export declare const resolvePath: (path: string, root: any) => any;
2
- export declare function dereferenceObject<T extends object, TRoot = T>(obj: T, { cwd, externalFileCache, refMap, root, fetch, importFn, headers, }?: {
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
- importFn?: (moduleId: string) => Promise<any>;
9
10
  headers?: Record<string, string>;
10
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;
@@ -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, importFn = utils.defaultImportFn, headers, } = {}) {
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,14 +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 = isURL(externalFilePath) ? getCwdForUrl(externalFilePath) : path.dirname(externalFilePath);
478
+ const newCwd = getCwd(externalFilePath);
469
479
  let externalFile = externalFileCache.get(externalFilePath);
470
480
  if (!externalFile) {
471
- externalFile = isURL(externalFilePath)
472
- ? await fetch(externalFilePath, {
473
- headers,
474
- }).then(res => res.json())
475
- : await importFn(externalFilePath);
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
+ });
476
489
  externalFile = await healJSONSchema(externalFile);
477
490
  externalFileCache.set(externalFilePath, externalFile);
478
491
  }
@@ -512,6 +525,9 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
512
525
  }
513
526
  else {
514
527
  const result = resolvePath(refPath, root);
528
+ if (!result.title && obj.title) {
529
+ result.title = obj.title;
530
+ }
515
531
  refMap.set($ref, result);
516
532
  return dereferenceObject(result, {
517
533
  cwd,
@@ -527,7 +543,14 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
527
543
  else {
528
544
  await Promise.all(Object.entries(obj).map(async ([key, val]) => {
529
545
  if (typeof val === 'object') {
530
- obj[key] = await dereferenceObject(val, { cwd, externalFileCache, refMap, root, fetch, headers });
546
+ obj[key] = await dereferenceObject(val, {
547
+ cwd,
548
+ externalFileCache,
549
+ refMap,
550
+ root,
551
+ fetch,
552
+ headers,
553
+ });
531
554
  }
532
555
  }));
533
556
  }
@@ -575,6 +598,8 @@ async function referenceJSONSchema(schema) {
575
598
  exports.FIRST_VISITED_PATH = FIRST_VISITED_PATH;
576
599
  exports.compareJSONSchemas = compareJSONSchemas;
577
600
  exports.dereferenceObject = dereferenceObject;
601
+ exports.getAbsolutePath = getAbsolutePath;
602
+ exports.getCwd = getCwd;
578
603
  exports.healJSONSchema = healJSONSchema;
579
604
  exports.referenceJSONSchema = referenceJSONSchema;
580
605
  exports.resolvePath = resolvePath;
package/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { JsonPointer } from 'json-ptr';
2
- import { AggregateError, defaultImportFn } from '@graphql-mesh/utils';
3
- import { dirname, isAbsolute, join } from 'path';
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;
@@ -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, importFn = defaultImportFn, headers, } = {}) {
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,14 +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 = isURL(externalFilePath) ? getCwdForUrl(externalFilePath) : dirname(externalFilePath);
473
+ const newCwd = getCwd(externalFilePath);
464
474
  let externalFile = externalFileCache.get(externalFilePath);
465
475
  if (!externalFile) {
466
- externalFile = isURL(externalFilePath)
467
- ? await fetch$1(externalFilePath, {
468
- headers,
469
- }).then(res => res.json())
470
- : await importFn(externalFilePath);
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
+ });
471
484
  externalFile = await healJSONSchema(externalFile);
472
485
  externalFileCache.set(externalFilePath, externalFile);
473
486
  }
@@ -507,6 +520,9 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
507
520
  }
508
521
  else {
509
522
  const result = resolvePath(refPath, root);
523
+ if (!result.title && obj.title) {
524
+ result.title = obj.title;
525
+ }
510
526
  refMap.set($ref, result);
511
527
  return dereferenceObject(result, {
512
528
  cwd,
@@ -522,7 +538,14 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
522
538
  else {
523
539
  await Promise.all(Object.entries(obj).map(async ([key, val]) => {
524
540
  if (typeof val === 'object') {
525
- obj[key] = await dereferenceObject(val, { cwd, externalFileCache, refMap, root, fetch: fetch$1, headers });
541
+ obj[key] = await dereferenceObject(val, {
542
+ cwd,
543
+ externalFileCache,
544
+ refMap,
545
+ root,
546
+ fetch: fetch$1,
547
+ headers,
548
+ });
526
549
  }
527
550
  }));
528
551
  }
@@ -567,4 +590,4 @@ async function referenceJSONSchema(schema) {
567
590
  return finalSchema;
568
591
  }
569
592
 
570
- 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.2.0",
3
+ "version": "0.2.1-alpha-044ef7368.0",
4
4
  "sideEffects": false,
5
5
  "dependencies": {
6
- "@graphql-mesh/utils": "0.22.1",
6
+ "@graphql-mesh/utils": "0.22.2-alpha-044ef7368.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",