@contello/client 1.0.0 → 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020-2026 entwico GmbH
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/cli.js CHANGED
@@ -4,7 +4,7 @@
4
4
  var command = process.argv[2];
5
5
  switch (command) {
6
6
  case "generate":
7
- await import("./generate-OTWB5J56.js");
7
+ await import("./generate-KEYVGQHL.js");
8
8
  break;
9
9
  default:
10
10
  console.error(command ? `unknown command: ${command}` : "usage: contello-client <command>");
@@ -3,7 +3,7 @@
3
3
  // src/generate/index.ts
4
4
  import { mkdirSync, readFileSync, rmSync, writeFileSync } from "fs";
5
5
  import { glob } from "fs/promises";
6
- import { dirname, resolve as resolve2 } from "path";
6
+ import { dirname, relative, resolve as resolve2 } from "path";
7
7
  import { parse as parse2 } from "graphql";
8
8
 
9
9
  // src/generate/config.ts
@@ -556,6 +556,7 @@ function generateSchemaTypes(schema) {
556
556
  lines.push("");
557
557
  }
558
558
  const modelNames = /* @__PURE__ */ new Map();
559
+ const entityModelNames = /* @__PURE__ */ new Map();
559
560
  for (const unionName of ["ContelloEntity", "ContelloComponent"]) {
560
561
  const union = typeMap[unionName];
561
562
  if (union && isUnionType(union)) {
@@ -563,6 +564,9 @@ function generateSchemaTypes(schema) {
563
564
  const model = deriveModelName(member.name);
564
565
  if (model) {
565
566
  modelNames.set(member.name, model);
567
+ if (unionName === "ContelloEntity") {
568
+ entityModelNames.set(member.name, model);
569
+ }
566
570
  }
567
571
  }
568
572
  }
@@ -594,13 +598,11 @@ function generateSchemaTypes(schema) {
594
598
  if (unions.length > 0) {
595
599
  lines.push("");
596
600
  }
597
- const contelloEntityUnion = unions.find((u) => u.name === "ContelloEntity");
598
- if (contelloEntityUnion) {
599
- const typeNames = contelloEntityUnion.getTypes().map((t) => t.name);
600
- const modelNames2 = typeNames.map((n) => n.replace(/Entity$/, "")).map((n) => n.charAt(0).toLowerCase() + n.slice(1));
601
+ if (entityModelNames.size > 0) {
602
+ const sorted = [...entityModelNames.entries()].sort(([a], [b]) => a.localeCompare(b));
601
603
  lines.push("export const models = {");
602
- for (let i = 0; i < modelNames2.length; i++) {
603
- lines.push(` ${modelNames2[i]}: '${typeNames[i]}',`);
604
+ for (const [typeName, model] of sorted) {
605
+ lines.push(` ${model}: '${typeName}',`);
604
606
  }
605
607
  lines.push("} as const;");
606
608
  lines.push("");
@@ -657,6 +659,19 @@ function transformSelectionSet(schema, selectionSet, parentTypeName, fragments,
657
659
  const parentFields = parentType.getFields();
658
660
  const newSelections = [];
659
661
  let changed = false;
662
+ function recurse(node, typeName) {
663
+ if (!node.selectionSet) {
664
+ newSelections.push(node);
665
+ return;
666
+ }
667
+ const transformed = transformSelectionSet(schema, node.selectionSet, typeName, fragments, refsOnly);
668
+ if (transformed !== node.selectionSet) {
669
+ changed = true;
670
+ newSelections.push({ ...node, selectionSet: transformed });
671
+ } else {
672
+ newSelections.push(node);
673
+ }
674
+ }
660
675
  for (const selection of selectionSet.selections) {
661
676
  if (selection.kind === Kind3.FIELD) {
662
677
  const fieldName = selection.name.value;
@@ -673,49 +688,28 @@ function transformSelectionSet(schema, selectionSet, parentTypeName, fragments,
673
688
  continue;
674
689
  }
675
690
  changed = true;
676
- const flatRefField = {
677
- ...selection,
678
- selectionSet: FLAT_REF_SELECTIONS
679
- };
680
- newSelections.push(flatRefField);
691
+ newSelections.push({ ...selection, selectionSet: FLAT_REF_SELECTIONS });
681
692
  if (!refsOnly) {
682
- const flatSiblingField = {
693
+ newSelections.push({
683
694
  kind: Kind3.FIELD,
684
695
  name: { kind: Kind3.NAME, value: flatFieldName },
685
696
  selectionSet: addFlatRefSelections(selection.selectionSet)
686
- };
687
- newSelections.push(flatSiblingField);
688
- }
689
- } else if (selection.selectionSet && namedTypeName) {
690
- const transformed = transformSelectionSet(schema, selection.selectionSet, namedTypeName, fragments, refsOnly);
691
- if (transformed !== selection.selectionSet) {
692
- changed = true;
693
- newSelections.push({ ...selection, selectionSet: transformed });
694
- } else {
695
- newSelections.push(selection);
697
+ });
696
698
  }
699
+ } else if (namedTypeName) {
700
+ recurse(selection, namedTypeName);
697
701
  } else {
698
702
  newSelections.push(selection);
699
703
  }
700
704
  } else if (selection.kind === Kind3.INLINE_FRAGMENT) {
701
- if (selection.typeCondition && selection.selectionSet) {
702
- const typeName = selection.typeCondition.name.value;
703
- const type = schema.getType(typeName);
704
- if (type && isCompositeType2(type) && isObjectType3(type)) {
705
- const transformed = transformSelectionSet(schema, selection.selectionSet, typeName, fragments, refsOnly);
706
- if (transformed !== selection.selectionSet) {
707
- changed = true;
708
- newSelections.push({ ...selection, selectionSet: transformed });
709
- } else {
710
- newSelections.push(selection);
711
- }
712
- } else {
713
- newSelections.push(selection);
714
- }
705
+ const typeName = selection.typeCondition?.name.value;
706
+ const type = typeName ? schema.getType(typeName) : void 0;
707
+ if (typeName && type && isCompositeType2(type) && isObjectType3(type)) {
708
+ recurse(selection, typeName);
715
709
  } else {
716
710
  newSelections.push(selection);
717
711
  }
718
- } else if (selection.kind === Kind3.FRAGMENT_SPREAD) {
712
+ } else {
719
713
  newSelections.push(selection);
720
714
  }
721
715
  }
@@ -770,23 +764,33 @@ function transformFragment(schema, fragment, fragments) {
770
764
  }
771
765
 
772
766
  // src/generate/index.ts
767
+ var ctrlSeq = "\x1B[";
768
+ var styled = (s, pre, post) => `${ctrlSeq}${pre}${s}${ctrlSeq}${post}`;
769
+ var dim = (s) => styled(s, "2m", "22m");
770
+ var bold = (s) => styled(s, "1m", "22m");
771
+ var green = (s) => styled(s, "32m", "39m");
772
+ var yellow = (s) => styled(s, "33m", "39m");
773
+ var cyan = (s) => styled(s, "36m", "39m");
773
774
  async function main() {
774
775
  const cwd = process.cwd();
775
776
  const config = await loadConfig(cwd);
777
+ console.log("");
778
+ console.log(`${bold("@contello/client")} ${dim("generate")}`);
779
+ console.log("");
776
780
  for (const project of config.projects) {
777
- console.log(`generating types for project "${project.project}"...`);
781
+ const start = performance.now();
782
+ console.log(`${cyan("\u25CF")} ${bold(project.project)}`);
778
783
  rmSync(resolve2(cwd, project.output), { force: true });
779
784
  const schema = await introspectSchema(project.url, project.project, project.token);
780
785
  const patterns = Array.isArray(project.documents) ? project.documents : [project.documents];
781
786
  const documentPaths = (await Promise.all(patterns.map((p) => Array.fromAsync(glob(p, { cwd }))))).flat().sort();
782
787
  if (documentPaths.length === 0) {
783
- console.warn(` no documents found matching "${project.documents}"`);
788
+ console.warn(` ${yellow("\u26A0")} no documents found matching "${project.documents}"`);
784
789
  continue;
785
790
  }
786
791
  const documents = documentPaths.map((p) => parse2(readFileSync(resolve2(cwd, p), "utf-8")));
787
792
  const fragments = collectFragments(documents);
788
793
  const operations = collectOperations(documents);
789
- console.log(` found ${operations.length} operations, ${fragments.size} fragments`);
790
794
  const transformedFragments = new Map(
791
795
  [...fragments].map(([name, frag]) => [name, transformFragment(schema, frag, fragments)])
792
796
  );
@@ -817,9 +821,11 @@ ${indented}
817
821
  const outputPath = resolve2(cwd, project.output);
818
822
  mkdirSync(dirname(outputPath), { recursive: true });
819
823
  writeFileSync(outputPath, output, "utf-8");
820
- console.log(` wrote ${outputPath}`);
824
+ const elapsed = Math.round(performance.now() - start);
825
+ console.log(` ${green("\u2713")} ${relative(cwd, outputPath)} ${dim(`${elapsed}ms`)}`);
826
+ console.log(` ${dim("operations")} ${operations.length} ${dim("fragments")} ${fragments.size}`);
827
+ console.log("");
821
828
  }
822
- console.log("done");
823
829
  }
824
830
  main().catch((err) => {
825
831
  console.error(err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contello/client",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "GraphQL client for Contello CMS with WebSocket transport, connection pooling, and built-in codegen",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -10,12 +10,6 @@
10
10
  "url": "https://github.com/entwico/contello-js",
11
11
  "directory": "packages/client"
12
12
  },
13
- "scripts": {
14
- "build": "tsup",
15
- "lint": "eslint .",
16
- "lint:fix": "eslint . --fix",
17
- "typecheck": "tsc --noEmit"
18
- },
19
13
  "dependencies": {
20
14
  "graphql": "^16.13.1",
21
15
  "graphql-ws": "^6.0.7"
@@ -42,5 +36,11 @@
42
36
  "publishConfig": {
43
37
  "access": "public",
44
38
  "registry": "https://registry.npmjs.org"
39
+ },
40
+ "scripts": {
41
+ "build": "tsup",
42
+ "lint": "eslint .",
43
+ "lint:fix": "eslint . --fix",
44
+ "typecheck": "tsc --noEmit"
45
45
  }
46
- }
46
+ }