@tinacms/graphql 1.4.0 → 1.4.2

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/dist/index.es.js CHANGED
@@ -2220,11 +2220,11 @@ Visit https://tina.io/docs/errors/ui-not-supported/ for more information
2220
2220
  switch (field.type) {
2221
2221
  case "boolean":
2222
2222
  case "datetime":
2223
- case "image":
2224
2223
  case "number":
2225
2224
  if (field.list) {
2226
2225
  console.warn(listWarningMsg);
2227
2226
  }
2227
+ case "image":
2228
2228
  case "string":
2229
2229
  return astBuilder.FieldDefinition({
2230
2230
  name: field.name,
@@ -2491,7 +2491,7 @@ var validateField = async (field) => {
2491
2491
  // package.json
2492
2492
  var package_default = {
2493
2493
  name: "@tinacms/graphql",
2494
- version: "1.4.0",
2494
+ version: "1.4.2",
2495
2495
  main: "dist/index.js",
2496
2496
  module: "dist/index.es.js",
2497
2497
  typings: "dist/index.d.ts",
@@ -4497,6 +4497,10 @@ var parseFile = (content, format, yupSchema, markdownParseConfig) => {
4497
4497
  var replaceNameOverrides = (template, obj) => {
4498
4498
  if (template.list) {
4499
4499
  return obj.map((item) => {
4500
+ if (isBlockField(template) && template.templateKey) {
4501
+ item._template = item[template.templateKey];
4502
+ delete item[template.templateKey];
4503
+ }
4500
4504
  return _replaceNameOverrides(
4501
4505
  getTemplateForData(template, item).fields,
4502
4506
  item
@@ -4506,6 +4510,9 @@ var replaceNameOverrides = (template, obj) => {
4506
4510
  return _replaceNameOverrides(getTemplateForData(template, obj).fields, obj);
4507
4511
  }
4508
4512
  };
4513
+ function isBlockField(field) {
4514
+ return field && field.type === "object" && field.templates?.length > 0;
4515
+ }
4509
4516
  var _replaceNameOverrides = (fields, obj) => {
4510
4517
  const output = {};
4511
4518
  Object.keys(obj).forEach((key) => {
@@ -4516,36 +4523,33 @@ var _replaceNameOverrides = (fields, obj) => {
4516
4523
  });
4517
4524
  return output;
4518
4525
  };
4519
- var getTemplateKey = (field) => {
4520
- const DEFAULT_TEMPLATE_KEY = "_template";
4521
- if (field.templates?.length) {
4522
- const templateField = field.templates[0]?.fields?.find(
4523
- (field2) => field2.name === DEFAULT_TEMPLATE_KEY
4524
- );
4525
- return templateField?.alias || DEFAULT_TEMPLATE_KEY;
4526
- }
4527
- return DEFAULT_TEMPLATE_KEY;
4528
- };
4529
4526
  var getTemplateForData = (field, data) => {
4530
4527
  if (field.templates?.length) {
4531
- const templateKey = getTemplateKey(field);
4528
+ const templateKey = "_template";
4532
4529
  if (data[templateKey]) {
4533
4530
  return field.templates.find(
4534
4531
  (template) => template.name === data[templateKey]
4535
4532
  );
4536
4533
  }
4534
+ throw new Error(
4535
+ `Missing required key "${templateKey}" on field ${field.name}`
4536
+ );
4537
4537
  } else {
4538
4538
  return field;
4539
4539
  }
4540
- throw new Error("No template found for field " + field.name);
4541
4540
  };
4542
4541
  var applyNameOverrides = (template, obj) => {
4543
4542
  if (template.list) {
4544
4543
  return obj.map((item) => {
4545
- return _applyNameOverrides(
4544
+ const result = _applyNameOverrides(
4546
4545
  getTemplateForData(template, item).fields,
4547
4546
  item
4548
4547
  );
4548
+ if (isBlockField(template) && template.templateKey) {
4549
+ result[template.templateKey] = result._template;
4550
+ delete result._template;
4551
+ }
4552
+ return result;
4549
4553
  });
4550
4554
  } else {
4551
4555
  return _applyNameOverrides(getTemplateForData(template, obj).fields, obj);
@@ -5372,11 +5376,15 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
5372
5376
  frontmatterFormat: collection?.frontmatterFormat
5373
5377
  }
5374
5378
  );
5379
+ const template = getTemplateForFile(templateInfo, data);
5380
+ if (!template) {
5381
+ console.warn(
5382
+ `Document: ${filepath} has an ambiguous template, skipping from indexing`
5383
+ );
5384
+ return;
5385
+ }
5375
5386
  const normalizedPath = normalizePath(filepath);
5376
- const aliasedData = templateInfo ? replaceNameOverrides(
5377
- getTemplateForFile(templateInfo, data),
5378
- data
5379
- ) : data;
5387
+ const aliasedData = templateInfo ? replaceNameOverrides(template, data) : data;
5380
5388
  await enqueueOps([
5381
5389
  ...makeIndexOpsForDocument(
5382
5390
  normalizedPath,
@@ -5443,13 +5451,17 @@ var getTemplateForFile = (templateInfo, data) => {
5443
5451
  }
5444
5452
  if (templateInfo.type === "union") {
5445
5453
  if (hasOwnProperty(data, "_template")) {
5446
- return templateInfo.templates.find(
5454
+ const template = templateInfo.templates.find(
5447
5455
  (t) => lastItem(t.namespace) === data._template
5448
5456
  );
5457
+ if (!template) {
5458
+ throw new Error(
5459
+ `Unable to find template "${data._template}". Possible templates are: ${templateInfo.templates.map((template2) => `"${template2.name}"`).join(", ")}.`
5460
+ );
5461
+ }
5462
+ return template;
5449
5463
  } else {
5450
- throw new Error(
5451
- `Expected _template to be provided for document in an ambiguous collection`
5452
- );
5464
+ return void 0;
5453
5465
  }
5454
5466
  }
5455
5467
  throw new Error(`Unable to determine template`);
@@ -5460,14 +5472,15 @@ import { ManyLevelGuest } from "many-level";
5460
5472
  import { pipeline } from "readable-stream";
5461
5473
  import { connect } from "net";
5462
5474
  var TinaLevelClient = class extends ManyLevelGuest {
5463
- constructor() {
5464
- super(...arguments);
5475
+ constructor(port) {
5476
+ super();
5465
5477
  this._connected = false;
5478
+ this.port = port || 9e3;
5466
5479
  }
5467
5480
  openConnection() {
5468
5481
  if (this._connected)
5469
5482
  return;
5470
- const socket = connect(9e3);
5483
+ const socket = connect(this.port);
5471
5484
  pipeline(socket, this.createRpcStream(), socket, () => {
5472
5485
  this._connected = false;
5473
5486
  });
package/dist/index.js CHANGED
@@ -2269,11 +2269,11 @@ Visit https://tina.io/docs/errors/ui-not-supported/ for more information
2269
2269
  switch (field.type) {
2270
2270
  case "boolean":
2271
2271
  case "datetime":
2272
- case "image":
2273
2272
  case "number":
2274
2273
  if (field.list) {
2275
2274
  console.warn(listWarningMsg);
2276
2275
  }
2276
+ case "image":
2277
2277
  case "string":
2278
2278
  return astBuilder.FieldDefinition({
2279
2279
  name: field.name,
@@ -2542,7 +2542,7 @@ var validateField = async (field) => {
2542
2542
  // package.json
2543
2543
  var package_default = {
2544
2544
  name: "@tinacms/graphql",
2545
- version: "1.4.0",
2545
+ version: "1.4.2",
2546
2546
  main: "dist/index.js",
2547
2547
  module: "dist/index.es.js",
2548
2548
  typings: "dist/index.d.ts",
@@ -4548,6 +4548,10 @@ var parseFile = (content, format, yupSchema, markdownParseConfig) => {
4548
4548
  var replaceNameOverrides = (template, obj) => {
4549
4549
  if (template.list) {
4550
4550
  return obj.map((item) => {
4551
+ if (isBlockField(template) && template.templateKey) {
4552
+ item._template = item[template.templateKey];
4553
+ delete item[template.templateKey];
4554
+ }
4551
4555
  return _replaceNameOverrides(
4552
4556
  getTemplateForData(template, item).fields,
4553
4557
  item
@@ -4557,6 +4561,10 @@ var replaceNameOverrides = (template, obj) => {
4557
4561
  return _replaceNameOverrides(getTemplateForData(template, obj).fields, obj);
4558
4562
  }
4559
4563
  };
4564
+ function isBlockField(field) {
4565
+ var _a;
4566
+ return field && field.type === "object" && ((_a = field.templates) == null ? void 0 : _a.length) > 0;
4567
+ }
4560
4568
  var _replaceNameOverrides = (fields, obj) => {
4561
4569
  const output = {};
4562
4570
  Object.keys(obj).forEach((key) => {
@@ -4567,38 +4575,34 @@ var _replaceNameOverrides = (fields, obj) => {
4567
4575
  });
4568
4576
  return output;
4569
4577
  };
4570
- var getTemplateKey = (field) => {
4571
- var _a, _b, _c;
4572
- const DEFAULT_TEMPLATE_KEY = "_template";
4573
- if ((_a = field.templates) == null ? void 0 : _a.length) {
4574
- const templateField = (_c = (_b = field.templates[0]) == null ? void 0 : _b.fields) == null ? void 0 : _c.find(
4575
- (field2) => field2.name === DEFAULT_TEMPLATE_KEY
4576
- );
4577
- return (templateField == null ? void 0 : templateField.alias) || DEFAULT_TEMPLATE_KEY;
4578
- }
4579
- return DEFAULT_TEMPLATE_KEY;
4580
- };
4581
4578
  var getTemplateForData = (field, data) => {
4582
4579
  var _a;
4583
4580
  if ((_a = field.templates) == null ? void 0 : _a.length) {
4584
- const templateKey = getTemplateKey(field);
4581
+ const templateKey = "_template";
4585
4582
  if (data[templateKey]) {
4586
4583
  return field.templates.find(
4587
4584
  (template) => template.name === data[templateKey]
4588
4585
  );
4589
4586
  }
4587
+ throw new Error(
4588
+ `Missing required key "${templateKey}" on field ${field.name}`
4589
+ );
4590
4590
  } else {
4591
4591
  return field;
4592
4592
  }
4593
- throw new Error("No template found for field " + field.name);
4594
4593
  };
4595
4594
  var applyNameOverrides = (template, obj) => {
4596
4595
  if (template.list) {
4597
4596
  return obj.map((item) => {
4598
- return _applyNameOverrides(
4597
+ const result = _applyNameOverrides(
4599
4598
  getTemplateForData(template, item).fields,
4600
4599
  item
4601
4600
  );
4601
+ if (isBlockField(template) && template.templateKey) {
4602
+ result[template.templateKey] = result._template;
4603
+ delete result._template;
4604
+ }
4605
+ return result;
4602
4606
  });
4603
4607
  } else {
4604
4608
  return _applyNameOverrides(getTemplateForData(template, obj).fields, obj);
@@ -5430,11 +5434,15 @@ var _indexContent = async (database, level, documentPaths, enqueueOps, collectio
5430
5434
  frontmatterFormat: collection == null ? void 0 : collection.frontmatterFormat
5431
5435
  }
5432
5436
  );
5437
+ const template = getTemplateForFile(templateInfo, data);
5438
+ if (!template) {
5439
+ console.warn(
5440
+ `Document: ${filepath} has an ambiguous template, skipping from indexing`
5441
+ );
5442
+ return;
5443
+ }
5433
5444
  const normalizedPath = (0, import_schema_tools3.normalizePath)(filepath);
5434
- const aliasedData = templateInfo ? replaceNameOverrides(
5435
- getTemplateForFile(templateInfo, data),
5436
- data
5437
- ) : data;
5445
+ const aliasedData = templateInfo ? replaceNameOverrides(template, data) : data;
5438
5446
  await enqueueOps([
5439
5447
  ...makeIndexOpsForDocument(
5440
5448
  normalizedPath,
@@ -5501,13 +5509,17 @@ var getTemplateForFile = (templateInfo, data) => {
5501
5509
  }
5502
5510
  if (templateInfo.type === "union") {
5503
5511
  if (hasOwnProperty(data, "_template")) {
5504
- return templateInfo.templates.find(
5512
+ const template = templateInfo.templates.find(
5505
5513
  (t) => lastItem(t.namespace) === data._template
5506
5514
  );
5515
+ if (!template) {
5516
+ throw new Error(
5517
+ `Unable to find template "${data._template}". Possible templates are: ${templateInfo.templates.map((template2) => `"${template2.name}"`).join(", ")}.`
5518
+ );
5519
+ }
5520
+ return template;
5507
5521
  } else {
5508
- throw new Error(
5509
- `Expected _template to be provided for document in an ambiguous collection`
5510
- );
5522
+ return void 0;
5511
5523
  }
5512
5524
  }
5513
5525
  throw new Error(`Unable to determine template`);
@@ -5518,14 +5530,15 @@ var import_many_level = require("many-level");
5518
5530
  var import_readable_stream = require("readable-stream");
5519
5531
  var import_net = require("net");
5520
5532
  var TinaLevelClient = class extends import_many_level.ManyLevelGuest {
5521
- constructor() {
5522
- super(...arguments);
5533
+ constructor(port) {
5534
+ super();
5523
5535
  this._connected = false;
5536
+ this.port = port || 9e3;
5524
5537
  }
5525
5538
  openConnection() {
5526
5539
  if (this._connected)
5527
5540
  return;
5528
- const socket = (0, import_net.connect)(9e3);
5541
+ const socket = (0, import_net.connect)(this.port);
5529
5542
  (0, import_readable_stream.pipeline)(socket, this.createRpcStream(), socket, () => {
5530
5543
  this._connected = false;
5531
5544
  });
@@ -3,7 +3,9 @@
3
3
  */
4
4
  import { ManyLevelGuest } from 'many-level';
5
5
  export declare class TinaLevelClient extends ManyLevelGuest<string, Record<string, any>> {
6
+ private port;
6
7
  private _connected;
8
+ constructor(port?: number);
7
9
  openConnection(): void;
8
10
  }
9
11
  export interface Bridge {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinacms/graphql",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.es.js",
6
6
  "typings": "dist/index.d.ts",
@@ -25,8 +25,8 @@
25
25
  "dependencies": {
26
26
  "@graphql-tools/relay-operation-optimizer": "^6.4.1",
27
27
  "@iarna/toml": "^2.2.5",
28
- "@tinacms/mdx": "1.3.5",
29
- "@tinacms/schema-tools": "1.4.0",
28
+ "@tinacms/mdx": "1.3.7",
29
+ "@tinacms/schema-tools": "1.4.2",
30
30
  "abstract-level": "^1.0.3",
31
31
  "body-parser": "^1.19.0",
32
32
  "cors": "^2.8.5",
@@ -83,7 +83,7 @@
83
83
  "directory": "packages/tina-graphql"
84
84
  },
85
85
  "devDependencies": {
86
- "@tinacms/schema-tools": "1.4.0",
86
+ "@tinacms/schema-tools": "1.4.2",
87
87
  "@tinacms/scripts": "1.1.0",
88
88
  "@types/cors": "^2.8.7",
89
89
  "@types/estree": "^0.0.50",