json-machete 0.2.0-alpha-d952116c3.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
@@ -10,7 +10,6 @@ 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');
14
13
 
15
14
  (function (OnCircularReference) {
16
15
  OnCircularReference["WARN"] = "WARN";
@@ -300,14 +299,7 @@ async function compareJSONSchemas(oldSchema, newSchema) {
300
299
  const reservedTypeNames = ['Query', 'Mutation', 'Subscription'];
301
300
  function deduplicateJSONSchema(schema, seenMap = new Map()) {
302
301
  if (typeof schema === 'object' && schema != null) {
303
- const titleReserved = schema.title;
304
- if (titleReserved) {
305
- schema.title = undefined;
306
- }
307
302
  const stringified = util.inspect(schema, undefined, 3);
308
- if (titleReserved) {
309
- schema.title = titleReserved;
310
- }
311
303
  const seen = seenMap.get(stringified);
312
304
  if (seen) {
313
305
  return seen;
@@ -420,7 +412,6 @@ async function healJSONSchema(schema) {
420
412
  });
421
413
  }
422
414
 
423
- const { readFile } = fs.promises;
424
415
  const resolvePath = (path, root) => {
425
416
  return jsonPtr.JsonPointer.get(root, path);
426
417
  };
@@ -462,11 +453,8 @@ function getAbsolutePath(path$1, cwd) {
462
453
  }
463
454
  return path.join(cwd, path$1);
464
455
  }
465
- function getCwd(path$1) {
466
- return isURL(path$1) ? getCwdForUrl(path$1) : path.dirname(path$1);
467
- }
468
456
  // eslint-disable-next-line @typescript-eslint/ban-types
469
- 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, } = {}) {
470
458
  if (typeof obj === 'object') {
471
459
  if (isRefObject(obj)) {
472
460
  const $ref = obj.$ref;
@@ -477,14 +465,14 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
477
465
  const [externalRelativeFilePath, refPath] = $ref.split('#');
478
466
  if (externalRelativeFilePath) {
479
467
  const externalFilePath = getAbsolutePath(externalRelativeFilePath, cwd);
480
- const newCwd = getCwd(externalFilePath);
468
+ const newCwd = isURL(externalFilePath) ? getCwdForUrl(externalFilePath) : path.dirname(externalFilePath);
481
469
  let externalFile = externalFileCache.get(externalFilePath);
482
470
  if (!externalFile) {
483
471
  externalFile = isURL(externalFilePath)
484
472
  ? await fetch(externalFilePath, {
485
473
  headers,
486
474
  }).then(res => res.json())
487
- : await JSON.parse(await readFile(externalFilePath, 'utf-8'));
475
+ : await importFn(externalFilePath);
488
476
  externalFile = await healJSONSchema(externalFile);
489
477
  externalFileCache.set(externalFilePath, externalFile);
490
478
  }
@@ -524,9 +512,6 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
524
512
  }
525
513
  else {
526
514
  const result = resolvePath(refPath, root);
527
- if (!result.title && obj.title) {
528
- result.title = obj.title;
529
- }
530
515
  refMap.set($ref, result);
531
516
  return dereferenceObject(result, {
532
517
  cwd,
@@ -542,14 +527,7 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
542
527
  else {
543
528
  await Promise.all(Object.entries(obj).map(async ([key, val]) => {
544
529
  if (typeof val === 'object') {
545
- obj[key] = await dereferenceObject(val, {
546
- cwd,
547
- externalFileCache,
548
- refMap,
549
- root,
550
- fetch,
551
- headers,
552
- });
530
+ obj[key] = await dereferenceObject(val, { cwd, externalFileCache, refMap, root, fetch, headers });
553
531
  }
554
532
  }));
555
533
  }
@@ -597,8 +575,6 @@ async function referenceJSONSchema(schema) {
597
575
  exports.FIRST_VISITED_PATH = FIRST_VISITED_PATH;
598
576
  exports.compareJSONSchemas = compareJSONSchemas;
599
577
  exports.dereferenceObject = dereferenceObject;
600
- exports.getAbsolutePath = getAbsolutePath;
601
- exports.getCwd = getCwd;
602
578
  exports.healJSONSchema = healJSONSchema;
603
579
  exports.referenceJSONSchema = referenceJSONSchema;
604
580
  exports.resolvePath = resolvePath;
package/index.mjs CHANGED
@@ -1,10 +1,9 @@
1
1
  import { JsonPointer } from 'json-ptr';
2
- import { AggregateError } 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';
7
- import { promises } from 'fs';
8
7
 
9
8
  var OnCircularReference;
10
9
  (function (OnCircularReference) {
@@ -295,14 +294,7 @@ async function compareJSONSchemas(oldSchema, newSchema) {
295
294
  const reservedTypeNames = ['Query', 'Mutation', 'Subscription'];
296
295
  function deduplicateJSONSchema(schema, seenMap = new Map()) {
297
296
  if (typeof schema === 'object' && schema != null) {
298
- const titleReserved = schema.title;
299
- if (titleReserved) {
300
- schema.title = undefined;
301
- }
302
297
  const stringified = inspect(schema, undefined, 3);
303
- if (titleReserved) {
304
- schema.title = titleReserved;
305
- }
306
298
  const seen = seenMap.get(stringified);
307
299
  if (seen) {
308
300
  return seen;
@@ -415,7 +407,6 @@ async function healJSONSchema(schema) {
415
407
  });
416
408
  }
417
409
 
418
- const { readFile } = promises;
419
410
  const resolvePath = (path, root) => {
420
411
  return JsonPointer.get(root, path);
421
412
  };
@@ -457,11 +448,8 @@ function getAbsolutePath(path, cwd) {
457
448
  }
458
449
  return join(cwd, path);
459
450
  }
460
- function getCwd(path) {
461
- return isURL(path) ? getCwdForUrl(path) : dirname(path);
462
- }
463
451
  // eslint-disable-next-line @typescript-eslint/ban-types
464
- 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, } = {}) {
465
453
  if (typeof obj === 'object') {
466
454
  if (isRefObject(obj)) {
467
455
  const $ref = obj.$ref;
@@ -472,14 +460,14 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
472
460
  const [externalRelativeFilePath, refPath] = $ref.split('#');
473
461
  if (externalRelativeFilePath) {
474
462
  const externalFilePath = getAbsolutePath(externalRelativeFilePath, cwd);
475
- const newCwd = getCwd(externalFilePath);
463
+ const newCwd = isURL(externalFilePath) ? getCwdForUrl(externalFilePath) : dirname(externalFilePath);
476
464
  let externalFile = externalFileCache.get(externalFilePath);
477
465
  if (!externalFile) {
478
466
  externalFile = isURL(externalFilePath)
479
467
  ? await fetch$1(externalFilePath, {
480
468
  headers,
481
469
  }).then(res => res.json())
482
- : await JSON.parse(await readFile(externalFilePath, 'utf-8'));
470
+ : await importFn(externalFilePath);
483
471
  externalFile = await healJSONSchema(externalFile);
484
472
  externalFileCache.set(externalFilePath, externalFile);
485
473
  }
@@ -519,9 +507,6 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
519
507
  }
520
508
  else {
521
509
  const result = resolvePath(refPath, root);
522
- if (!result.title && obj.title) {
523
- result.title = obj.title;
524
- }
525
510
  refMap.set($ref, result);
526
511
  return dereferenceObject(result, {
527
512
  cwd,
@@ -537,14 +522,7 @@ async function dereferenceObject(obj, { cwd = process.cwd(), externalFileCache =
537
522
  else {
538
523
  await Promise.all(Object.entries(obj).map(async ([key, val]) => {
539
524
  if (typeof val === 'object') {
540
- obj[key] = await dereferenceObject(val, {
541
- cwd,
542
- externalFileCache,
543
- refMap,
544
- root,
545
- fetch: fetch$1,
546
- headers,
547
- });
525
+ obj[key] = await dereferenceObject(val, { cwd, externalFileCache, refMap, root, fetch: fetch$1, headers });
548
526
  }
549
527
  }));
550
528
  }
@@ -589,4 +567,4 @@ async function referenceJSONSchema(schema) {
589
567
  return finalSchema;
590
568
  }
591
569
 
592
- 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-d952116c3.0",
3
+ "version": "0.2.0",
4
4
  "sideEffects": false,
5
5
  "dependencies": {
6
- "@graphql-mesh/utils": "0.22.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",