json-machete 0.2.0-alpha-e2ff01c55.0 → 0.2.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,11 +1,10 @@
1
1
  export declare const resolvePath: (path: string, root: any) => any;
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, }?: {
2
+ export declare function dereferenceObject<T extends object, TRoot = T>(obj: T, { cwd, externalFileCache, refMap, root, fetch, importFn, headers, }?: {
5
3
  cwd?: string;
6
4
  externalFileCache?: Map<string, any>;
7
5
  refMap?: Map<string, any>;
8
6
  root?: TRoot;
9
7
  fetch?: WindowOrWorkerGlobalScope['fetch'];
8
+ importFn?: (moduleId: string) => Promise<any>;
10
9
  headers?: Record<string, string>;
11
10
  }): Promise<T>;
package/index.js CHANGED
@@ -299,14 +299,7 @@ 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
- }
306
302
  const stringified = util.inspect(schema, undefined, 3);
307
- if (titleReserved) {
308
- schema.title = titleReserved;
309
- }
310
303
  const seen = seenMap.get(stringified);
311
304
  if (seen) {
312
305
  return seen;
@@ -401,7 +394,7 @@ async function healJSONSchema(schema) {
401
394
  }
402
395
  }
403
396
  // Properties only exist in objects
404
- if (subSchema.properties || subSchema.patternProperties || 'additionalProperties' in subSchema) {
397
+ if (subSchema.properties) {
405
398
  subSchema.type = 'object';
406
399
  }
407
400
  // Items only exist in arrays
@@ -460,11 +453,8 @@ function getAbsolutePath(path$1, cwd) {
460
453
  }
461
454
  return path.join(cwd, path$1);
462
455
  }
463
- function getCwd(path$1) {
464
- return isURL(path$1) ? getCwdForUrl(path$1) : path.dirname(path$1);
465
- }
466
456
  // eslint-disable-next-line @typescript-eslint/ban-types
467
- async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache = new Map(), refMap = new Map(), root = obj, fetch = crossUndiciFetch.fetch, headers, } = {}) {
457
+ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache = new Map(), refMap = new Map(), root = obj, fetch = crossUndiciFetch.fetch, importFn = utils.defaultImportFn, headers, } = {}) {
468
458
  if (typeof obj === 'object') {
469
459
  if (isRefObject(obj)) {
470
460
  const $ref = obj.$ref;
@@ -475,17 +465,14 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
475
465
  const [externalRelativeFilePath, refPath] = $ref.split('#');
476
466
  if (externalRelativeFilePath) {
477
467
  const externalFilePath = getAbsolutePath(externalRelativeFilePath, cwd);
478
- const newCwd = getCwd(externalFilePath);
468
+ const newCwd = isURL(externalFilePath) ? getCwdForUrl(externalFilePath) : path.dirname(externalFilePath);
479
469
  let externalFile = externalFileCache.get(externalFilePath);
480
470
  if (!externalFile) {
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
- });
471
+ externalFile = isURL(externalFilePath)
472
+ ? await fetch(externalFilePath, {
473
+ headers,
474
+ }).then(res => res.json())
475
+ : await importFn(externalFilePath);
489
476
  externalFile = await healJSONSchema(externalFile);
490
477
  externalFileCache.set(externalFilePath, externalFile);
491
478
  }
@@ -525,9 +512,6 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
525
512
  }
526
513
  else {
527
514
  const result = resolvePath(refPath, root);
528
- if (!result.title && obj.title) {
529
- result.title = obj.title;
530
- }
531
515
  refMap.set($ref, result);
532
516
  return dereferenceObject(result, {
533
517
  cwd,
@@ -543,14 +527,7 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
543
527
  else {
544
528
  await Promise.all(Object.entries(obj).map(async ([key, val]) => {
545
529
  if (typeof val === 'object') {
546
- obj[key] = await dereferenceObject(val, {
547
- cwd,
548
- externalFileCache,
549
- refMap,
550
- root,
551
- fetch,
552
- headers,
553
- });
530
+ obj[key] = await dereferenceObject(val, { cwd, externalFileCache, refMap, root, fetch, headers });
554
531
  }
555
532
  }));
556
533
  }
@@ -598,8 +575,6 @@ async function referenceJSONSchema(schema) {
598
575
  exports.FIRST_VISITED_PATH = FIRST_VISITED_PATH;
599
576
  exports.compareJSONSchemas = compareJSONSchemas;
600
577
  exports.dereferenceObject = dereferenceObject;
601
- exports.getAbsolutePath = getAbsolutePath;
602
- exports.getCwd = getCwd;
603
578
  exports.healJSONSchema = healJSONSchema;
604
579
  exports.referenceJSONSchema = referenceJSONSchema;
605
580
  exports.resolvePath = resolvePath;
package/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { JsonPointer } from 'json-ptr';
2
- import { AggregateError, readFileOrUrl } from '@graphql-mesh/utils';
3
- import { isAbsolute, join, dirname } from 'path';
2
+ import { AggregateError, defaultImportFn } from '@graphql-mesh/utils';
3
+ import { dirname, isAbsolute, join } from 'path';
4
4
  import { inspect } from 'util';
5
5
  import urlJoin from 'url-join';
6
6
  import { fetch } from 'cross-undici-fetch';
@@ -294,14 +294,7 @@ 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
- }
301
297
  const stringified = inspect(schema, undefined, 3);
302
- if (titleReserved) {
303
- schema.title = titleReserved;
304
- }
305
298
  const seen = seenMap.get(stringified);
306
299
  if (seen) {
307
300
  return seen;
@@ -396,7 +389,7 @@ async function healJSONSchema(schema) {
396
389
  }
397
390
  }
398
391
  // Properties only exist in objects
399
- if (subSchema.properties || subSchema.patternProperties || 'additionalProperties' in subSchema) {
392
+ if (subSchema.properties) {
400
393
  subSchema.type = 'object';
401
394
  }
402
395
  // Items only exist in arrays
@@ -455,11 +448,8 @@ function getAbsolutePath(path, cwd) {
455
448
  }
456
449
  return join(cwd, path);
457
450
  }
458
- function getCwd(path) {
459
- return isURL(path) ? getCwdForUrl(path) : dirname(path);
460
- }
461
451
  // eslint-disable-next-line @typescript-eslint/ban-types
462
- async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache = new Map(), refMap = new Map(), root = obj, fetch: fetch$1 = fetch, headers, } = {}) {
452
+ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache = new Map(), refMap = new Map(), root = obj, fetch: fetch$1 = fetch, importFn = defaultImportFn, headers, } = {}) {
463
453
  if (typeof obj === 'object') {
464
454
  if (isRefObject(obj)) {
465
455
  const $ref = obj.$ref;
@@ -470,17 +460,14 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
470
460
  const [externalRelativeFilePath, refPath] = $ref.split('#');
471
461
  if (externalRelativeFilePath) {
472
462
  const externalFilePath = getAbsolutePath(externalRelativeFilePath, cwd);
473
- const newCwd = getCwd(externalFilePath);
463
+ const newCwd = isURL(externalFilePath) ? getCwdForUrl(externalFilePath) : dirname(externalFilePath);
474
464
  let externalFile = externalFileCache.get(externalFilePath);
475
465
  if (!externalFile) {
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
- });
466
+ externalFile = isURL(externalFilePath)
467
+ ? await fetch$1(externalFilePath, {
468
+ headers,
469
+ }).then(res => res.json())
470
+ : await importFn(externalFilePath);
484
471
  externalFile = await healJSONSchema(externalFile);
485
472
  externalFileCache.set(externalFilePath, externalFile);
486
473
  }
@@ -520,9 +507,6 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
520
507
  }
521
508
  else {
522
509
  const result = resolvePath(refPath, root);
523
- if (!result.title && obj.title) {
524
- result.title = obj.title;
525
- }
526
510
  refMap.set($ref, result);
527
511
  return dereferenceObject(result, {
528
512
  cwd,
@@ -538,14 +522,7 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
538
522
  else {
539
523
  await Promise.all(Object.entries(obj).map(async ([key, val]) => {
540
524
  if (typeof val === 'object') {
541
- obj[key] = await dereferenceObject(val, {
542
- cwd,
543
- externalFileCache,
544
- refMap,
545
- root,
546
- fetch: fetch$1,
547
- headers,
548
- });
525
+ obj[key] = await dereferenceObject(val, { cwd, externalFileCache, refMap, root, fetch: fetch$1, headers });
549
526
  }
550
527
  }));
551
528
  }
@@ -590,4 +567,4 @@ async function referenceJSONSchema(schema) {
590
567
  return finalSchema;
591
568
  }
592
569
 
593
- export { FIRST_VISITED_PATH, OnCircularReference, compareJSONSchemas, dereferenceObject, getAbsolutePath, getCwd, healJSONSchema, referenceJSONSchema, resolvePath, visitJSONSchema };
570
+ export { FIRST_VISITED_PATH, OnCircularReference, compareJSONSchemas, dereferenceObject, healJSONSchema, referenceJSONSchema, resolvePath, visitJSONSchema };
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "json-machete",
3
- "version": "0.2.0-alpha-e2ff01c55.0",
3
+ "version": "0.2.0",
4
4
  "sideEffects": false,
5
5
  "dependencies": {
6
- "@graphql-mesh/utils": "0.22.1-alpha-e2ff01c55.0",
6
+ "@graphql-mesh/utils": "0.22.1",
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",