@cogover/sdk 0.12.1 → 0.13.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.
@@ -27,14 +27,23 @@ function extractObjects(documentValue) {
27
27
  }
28
28
 
29
29
  function renderDeclaration(objectsValue) {
30
+ // SDK types used by the generated field types. The augmentation cannot see the module's own exports, so the
31
+ // declaration imports them; an unresolved name would silently become `any` under skipLibCheck.
32
+ const sdkTypes = new Set();
30
33
  const objectLines = objectsValue
31
34
  .filter((object) => typeof object?.slug === "string" && object.slug.length > 0)
32
35
  .sort((left, right) => left.slug.localeCompare(right.slug))
33
- .map(renderObject)
36
+ .map((object) => renderObject(object, sdkTypes))
34
37
  .join("\n");
35
38
 
39
+ // Only the names in use are imported, so the file also compiles with noUnusedLocals.
40
+ const usedTypes = [...sdkTypes].sort();
41
+ const importLine = usedTypes.length === 0
42
+ ? `import "@cogover/sdk";\n\n`
43
+ : `import type { ${usedTypes.join(", ")} } from "@cogover/sdk";\n\n`;
44
+
36
45
  return `// Generated by @cogover/sdk. Do not edit manually.\n`
37
- + `import "@cogover/sdk";\n\n`
46
+ + importLine
38
47
  + `declare module "@cogover/sdk" {\n`
39
48
  + ` interface WorkspaceObjects {\n`
40
49
  + objectLines
@@ -42,25 +51,25 @@ function renderDeclaration(objectsValue) {
42
51
  + `}\n`;
43
52
  }
44
53
 
45
- function renderObject(object) {
54
+ function renderObject(object, sdkTypes) {
46
55
  const fields = Array.isArray(object.fields) ? object.fields : [];
47
56
  const fieldLines = fields
48
57
  .filter((field) => typeof field?.slug === "string" && field.slug.length > 0)
49
58
  .sort((left, right) => left.slug.localeCompare(right.slug))
50
- .map((field) => ` ${property(field.slug)}: ${fieldType(field)};\n`)
59
+ .map((field) => ` ${property(field.slug)}: ${fieldType(field, sdkTypes)};\n`)
51
60
  .join("");
52
61
  return ` ${property(object.slug)}: {\n${fieldLines} };\n`;
53
62
  }
54
63
 
55
- function fieldType(field) {
56
- const baseType = baseFieldType(field);
64
+ function fieldType(field, sdkTypes) {
65
+ const baseType = baseFieldType(field, sdkTypes);
57
66
  const nullableType = field.required === true ? baseType : `${baseType} | null`;
58
67
  return field.multiple === true && !baseType.endsWith("[]")
59
68
  ? `readonly ${parenthesize(baseType)}[]${field.required === true ? "" : " | null"}`
60
69
  : nullableType;
61
70
  }
62
71
 
63
- function baseFieldType(field) {
72
+ function baseFieldType(field, sdkTypes) {
64
73
  switch (field.fieldType) {
65
74
  case "short_text":
66
75
  case "long_text":
@@ -88,11 +97,14 @@ function baseFieldType(field) {
88
97
  case "cascading":
89
98
  return `readonly ${parenthesize(optionUnion(field.options))}[]`;
90
99
  case "url":
100
+ sdkTypes.add("UrlValue");
91
101
  return "UrlValue";
92
102
  case "file":
103
+ sdkTypes.add("FileValue");
93
104
  return "FileValue";
94
105
  case "lookup_normal":
95
106
  case "reference": {
107
+ sdkTypes.add("RecordReference");
96
108
  const objectSlug = referenceObjectSlug(field);
97
109
  return objectSlug === null
98
110
  ? "RecordReference"