@canonical/design-system 0.1.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.
- package/.mcp.json +8 -0
- package/README.md +438 -0
- package/biome.json +6 -0
- package/bun.lock +349 -0
- package/data/apps/component/file_tree.ttl +8 -0
- package/data/apps/component/key_value_pair.ttl +8 -0
- package/data/apps/component/navigation.ttl +8 -0
- package/data/apps/component/skeleton_loading.ttl +8 -0
- package/data/apps/component/status_bar.ttl +8 -0
- package/data/apps/component/toolbar.ttl +8 -0
- package/data/apps/layout/application_layout.ttl +8 -0
- package/data/apps/layout/empty_layout.ttl +8 -0
- package/data/apps/layout/list_page_layout.ttl +8 -0
- package/data/apps/layout/settings_page_layout.ttl +8 -0
- package/data/apps/pattern/code_diff.ttl +8 -0
- package/data/apps/pattern/data_table.ttl +8 -0
- package/data/apps/pattern/destructive_modal.ttl +8 -0
- package/data/apps/pattern/key_value_summary.ttl +8 -0
- package/data/apps/pattern/keyboard_shortcuts.ttl +8 -0
- package/data/apps/pattern/notification_center.ttl +8 -0
- package/data/apps/pattern/side_panel.ttl +8 -0
- package/data/apps/pattern/sidepanel_stacked.ttl +8 -0
- package/data/apps.ttl +5 -0
- package/data/apps_wpe.ttl +5 -0
- package/data/documentation/layout/documentation_layout.ttl +8 -0
- package/data/documentation.ttl +5 -0
- package/data/global/component/accordion.ttl +14 -0
- package/data/global/component/announcement.ttl +8 -0
- package/data/global/component/badge.ttl +9 -0
- package/data/global/component/button.ttl +20 -0
- package/data/global/component/card.ttl +8 -0
- package/data/global/component/chip.ttl +8 -0
- package/data/global/component/code_snippet.ttl +8 -0
- package/data/global/component/contextual_menu.ttl +8 -0
- package/data/global/component/heading.ttl +14 -0
- package/data/global/component/icon.ttl +14 -0
- package/data/global/component/icon_in_container.ttl +8 -0
- package/data/global/component/keyboard_key.ttl +8 -0
- package/data/global/component/label.ttl +15 -0
- package/data/global/component/link.ttl +8 -0
- package/data/global/component/notification_inline.ttl +8 -0
- package/data/global/component/notification_toast.ttl +8 -0
- package/data/global/component/pagination.ttl +8 -0
- package/data/global/component/popover.ttl +8 -0
- package/data/global/component/rich_text_editor.ttl +8 -0
- package/data/global/component/search_bar.ttl +8 -0
- package/data/global/component/table.ttl +8 -0
- package/data/global/component/tabs.ttl +8 -0
- package/data/global/component/theme_switcher.ttl +8 -0
- package/data/global/component/tile.ttl +14 -0
- package/data/global/component/tooltip.ttl +8 -0
- package/data/global/group/buttons.ttl +8 -0
- package/data/global/group/cards.ttl +8 -0
- package/data/global/group/tiles.ttl +8 -0
- package/data/global/layout/error_layout.ttl +8 -0
- package/data/global/layout/form_layout.ttl +8 -0
- package/data/global/layout/identity_page_form.ttl +8 -0
- package/data/global/modifier/alpha.ttl +6 -0
- package/data/global/modifier/beta.ttl +6 -0
- package/data/global/modifier/caution.ttl +6 -0
- package/data/global/modifier/comfortable.ttl +6 -0
- package/data/global/modifier/completed.ttl +6 -0
- package/data/global/modifier/constructive.ttl +6 -0
- package/data/global/modifier/dark.ttl +6 -0
- package/data/global/modifier/dense.ttl +6 -0
- package/data/global/modifier/destructive.ttl +6 -0
- package/data/global/modifier/editable.ttl +6 -0
- package/data/global/modifier/elevated.ttl +6 -0
- package/data/global/modifier/error.ttl +6 -0
- package/data/global/modifier/experimental.ttl +6 -0
- package/data/global/modifier/failed.ttl +6 -0
- package/data/global/modifier/focused.ttl +6 -0
- package/data/global/modifier/highlighted.ttl +6 -0
- package/data/global/modifier/in_progress.ttl +6 -0
- package/data/global/modifier/information.ttl +6 -0
- package/data/global/modifier/light.ttl +6 -0
- package/data/global/modifier/loading.ttl +6 -0
- package/data/global/modifier/muted.ttl +6 -0
- package/data/global/modifier/paper.ttl +6 -0
- package/data/global/modifier/planned.ttl +6 -0
- package/data/global/modifier/primary.ttl +6 -0
- package/data/global/modifier/secondary.ttl +6 -0
- package/data/global/modifier/stable.ttl +6 -0
- package/data/global/modifier/success.ttl +6 -0
- package/data/global/modifier/tertiary.ttl +6 -0
- package/data/global/modifier/warning.ttl +6 -0
- package/data/global/modifier/wireframe.ttl +6 -0
- package/data/global/modifier_family/_color_luminosity.ttl +7 -0
- package/data/global/modifier_family/_emphasis.ttl +7 -0
- package/data/global/modifier_family/_other.ttl +7 -0
- package/data/global/modifier_family/anticipation.ttl +7 -0
- package/data/global/modifier_family/criticality.ttl +7 -0
- package/data/global/modifier_family/density.ttl +7 -0
- package/data/global/modifier_family/importance.ttl +7 -0
- package/data/global/modifier_family/lifecycle.ttl +7 -0
- package/data/global/modifier_family/release.ttl +7 -0
- package/data/global/pattern/breadcrumbs.ttl +14 -0
- package/data/global/pattern/info_table.ttl +8 -0
- package/data/global/pattern/modal.ttl +8 -0
- package/data/global/pattern/navigation.ttl +8 -0
- package/data/global/pattern/select_button.ttl +8 -0
- package/data/global/pattern/stepper.ttl +8 -0
- package/data/global/pattern/table_of_contents.ttl +8 -0
- package/data/global/pattern/timeline.ttl +14 -0
- package/data/global/subcomponent/accordion-item.ttl +19 -0
- package/data/global/subcomponent/breadcrumbs-item.ttl +19 -0
- package/data/global/subcomponent/card-content.ttl +14 -0
- package/data/global/subcomponent/card-header.ttl +14 -0
- package/data/global/subcomponent/card-image.ttl +20 -0
- package/data/global/subcomponent/divider.ttl +8 -0
- package/data/global/subcomponent/modal-content.ttl +8 -0
- package/data/global/subcomponent/modal-footer.ttl +8 -0
- package/data/global/subcomponent/modal-header.ttl +8 -0
- package/data/global/subcomponent/spinner.ttl +8 -0
- package/data/global/subcomponent/table-cell.ttl +8 -0
- package/data/global/subcomponent/table-row.ttl +8 -0
- package/data/global/subcomponent/tile-content.ttl +14 -0
- package/data/global/subcomponent/tile-header.ttl +14 -0
- package/data/global/subcomponent/timeline-content.ttl +14 -0
- package/data/global/subcomponent/timeline-event.ttl +31 -0
- package/data/global/subcomponent/timeline-footer.ttl +8 -0
- package/data/global/subcomponent/timeline-header.ttl +8 -0
- package/data/global.ttl +5 -0
- package/data/global_form/component/checkbox_input.ttl +8 -0
- package/data/global_form/component/choices_input.ttl +8 -0
- package/data/global_form/component/combobox_input.ttl +8 -0
- package/data/global_form/component/custom_choices_input.ttl +8 -0
- package/data/global_form/component/datepicker_input.ttl +8 -0
- package/data/global_form/component/rating_input.ttl +8 -0
- package/data/global_form/component/segmented_control_input.ttl +8 -0
- package/data/global_form/component/slider_input.ttl +8 -0
- package/data/global_form/component/switch_input.ttl +8 -0
- package/data/global_form/component/text_input.ttl +8 -0
- package/data/global_form/component/textarea_input.ttl +8 -0
- package/data/global_form/component/wysiwginput.ttl +8 -0
- package/data/global_form/pattern/field.ttl +8 -0
- package/data/global_form/pattern/form.ttl +8 -0
- package/data/global_form/subcomponent/field-description.ttl +8 -0
- package/data/global_form/subcomponent/field-label.ttl +8 -0
- package/data/global_form.ttl +5 -0
- package/data/site/component/quote.ttl +8 -0
- package/data/site/component/rule.ttl +8 -0
- package/data/site/component/section.ttl +8 -0
- package/data/site/layout/site_layout.ttl +8 -0
- package/data/site/pattern/hero-github.ttl +8 -0
- package/data/site/pattern/hero.ttl +8 -0
- package/data/site/pattern/info_table.ttl +8 -0
- package/data/site/pattern/skip_link.ttl +8 -0
- package/data/site.ttl +5 -0
- package/data/stores.ttl +5 -0
- package/definitions/ontology.ttl +498 -0
- package/package.json +42 -0
- package/session/A.DSL.md +429 -0
- package/session/B.NOTES.md +84 -0
- package/session/global.component.accordion.yaml +13 -0
- package/session/global.component.button.yaml +26 -0
- package/session/global.component.card.yaml +24 -0
- package/session/global.component.icon.yaml +7 -0
- package/session/global.component.label.yaml +7 -0
- package/session/global.component.tile.yaml +21 -0
- package/session/global.component.timeline.yaml +20 -0
- package/session/global.pattern.breadcrumbs.yaml +13 -0
- package/session/global.subcomponent.accordion-item.yaml +37 -0
- package/session/global.subcomponent.breadcrumbs-item.yaml +21 -0
- package/session/global.subcomponent.card-content.yaml +7 -0
- package/session/global.subcomponent.card-header.yaml +22 -0
- package/session/global.subcomponent.card-image.yaml +7 -0
- package/session/global.subcomponent.tile-content.yaml +7 -0
- package/session/global.subcomponent.tile-header.yaml +8 -0
- package/session/global.subcomponent.timeline-content.yaml +13 -0
- package/session/global.subcomponent.timeline-event.yaml +20 -0
- package/session/global.subcomponent.timeline-footer.yaml +9 -0
- package/session/global.subcomponent.timeline-header.yaml +18 -0
- package/skills/anatomy-author/ANATOMY_DSL_SPEC.md +423 -0
- package/skills/anatomy-author/SKILL.md +911 -0
- package/skills/component-specifier/SKILL.md +413 -0
- package/skills/design-auditor/SKILL.md +428 -0
- package/source.json +101 -0
- package/src/cli.ts +45 -0
- package/src/collect/generateTurtle.tests.ts +200 -0
- package/src/collect/generateTurtle.ts +187 -0
- package/src/collect/index.ts +4 -0
- package/src/collect/scanAnnotations.tests.ts +138 -0
- package/src/collect/scanAnnotations.ts +78 -0
- package/src/collect/types.ts +57 -0
- package/src/collect/validateCollectConfig.tests.ts +116 -0
- package/src/collect/validateCollectConfig.ts +74 -0
- package/src/collect-implementations.ts +99 -0
- package/src/commands/extract.tests.ts +41 -0
- package/src/commands/extract.ts +48 -0
- package/src/commands/index.ts +3 -0
- package/src/commands/list.tests.ts +26 -0
- package/src/commands/list.ts +26 -0
- package/src/commands/transform.tests.ts +28 -0
- package/src/commands/transform.ts +133 -0
- package/src/config/index.ts +2 -0
- package/src/config/source.schema.json +110 -0
- package/src/config/types.ts +97 -0
- package/src/config/validateConfig.tests.ts +46 -0
- package/src/config/validateConfig.ts +21 -0
- package/src/constants.ts +20 -0
- package/src/graph/GraphStore.tests.ts +157 -0
- package/src/graph/GraphStore.ts +200 -0
- package/src/graph/PrefixMap.tests.ts +129 -0
- package/src/graph/PrefixMap.ts +95 -0
- package/src/graph/buildUri.tests.ts +158 -0
- package/src/graph/buildUri.ts +69 -0
- package/src/graph/index.ts +4 -0
- package/src/graph/types.ts +20 -0
- package/src/providers/CodaProvider.tests.ts +241 -0
- package/src/providers/CodaProvider.ts +174 -0
- package/src/providers/CodaProvider.types.generated.ts +12236 -0
- package/src/providers/index.ts +18 -0
- package/src/serializers/JSONSerializer.tests.ts +79 -0
- package/src/serializers/JSONSerializer.ts +87 -0
- package/src/serializers/TTLSerializer.tests.ts +75 -0
- package/src/serializers/TTLSerializer.ts +99 -0
- package/src/serializers/index.ts +3 -0
- package/src/serializers/types.ts +16 -0
- package/src/transform/resolveReferences.tests.ts +63 -0
- package/src/transform/resolveReferences.ts +36 -0
- package/src/transform/resolveValue.tests.ts +76 -0
- package/src/transform/resolveValue.ts +87 -0
- package/src/transform/transform.tests.ts +99 -0
- package/src/transform/transform.ts +109 -0
- package/src/transform/transformRow.tests.ts +269 -0
- package/src/transform/transformRow.ts +220 -0
- package/src/transform/types.ts +26 -0
- package/tsconfig.json +8 -0
- package/vite.config.ts +11 -0
- package/vitest.config.ts +21 -0
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
2
|
+
import CodaProvider from "./CodaProvider.js";
|
|
3
|
+
|
|
4
|
+
// Mock fetch globally
|
|
5
|
+
const mockFetch = vi.fn();
|
|
6
|
+
global.fetch = mockFetch;
|
|
7
|
+
|
|
8
|
+
describe("CodaProvider", () => {
|
|
9
|
+
const apiKey = "test-api-key";
|
|
10
|
+
let provider: CodaProvider;
|
|
11
|
+
let originalEnv: string | undefined;
|
|
12
|
+
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
originalEnv = process.env.CODA_API_KEY;
|
|
15
|
+
process.env.CODA_API_KEY = apiKey;
|
|
16
|
+
provider = new CodaProvider();
|
|
17
|
+
mockFetch.mockReset();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
afterEach(() => {
|
|
21
|
+
process.env.CODA_API_KEY = originalEnv;
|
|
22
|
+
vi.restoreAllMocks();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe("listTables", () => {
|
|
26
|
+
it("should fetch and return table metadata", async () => {
|
|
27
|
+
const mockResponse = {
|
|
28
|
+
items: [
|
|
29
|
+
{
|
|
30
|
+
id: "grid-abc123",
|
|
31
|
+
name: "Components",
|
|
32
|
+
displayColumn: { id: "c-1", name: "Name" },
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
id: "grid-def456",
|
|
36
|
+
name: "Tiers",
|
|
37
|
+
displayColumn: { id: "c-2", name: "Name" },
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
mockFetch.mockResolvedValueOnce({
|
|
43
|
+
ok: true,
|
|
44
|
+
json: () => Promise.resolve(mockResponse),
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const tables = await provider.listTables("dNyzE_TLZDh");
|
|
48
|
+
|
|
49
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
50
|
+
"https://coda.io/apis/v1/docs/dNyzE_TLZDh/tables",
|
|
51
|
+
expect.objectContaining({
|
|
52
|
+
headers: expect.objectContaining({
|
|
53
|
+
Authorization: "Bearer test-api-key",
|
|
54
|
+
}),
|
|
55
|
+
}),
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
expect(tables).toEqual([
|
|
59
|
+
{ id: "grid-abc123", name: "Components" },
|
|
60
|
+
{ id: "grid-def456", name: "Tiers" },
|
|
61
|
+
]);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("should throw on API error", async () => {
|
|
65
|
+
mockFetch.mockResolvedValueOnce({
|
|
66
|
+
ok: false,
|
|
67
|
+
status: 401,
|
|
68
|
+
statusText: "Unauthorized",
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
await expect(provider.listTables("dNyzE_TLZDh")).rejects.toThrow(
|
|
72
|
+
"Coda API error: 401 Unauthorized",
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("should handle pagination", async () => {
|
|
77
|
+
// First page
|
|
78
|
+
mockFetch.mockResolvedValueOnce({
|
|
79
|
+
ok: true,
|
|
80
|
+
json: () =>
|
|
81
|
+
Promise.resolve({
|
|
82
|
+
items: [{ id: "grid-1", name: "Table1", displayColumn: null }],
|
|
83
|
+
nextPageToken: "token123",
|
|
84
|
+
}),
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// Second page
|
|
88
|
+
mockFetch.mockResolvedValueOnce({
|
|
89
|
+
ok: true,
|
|
90
|
+
json: () =>
|
|
91
|
+
Promise.resolve({
|
|
92
|
+
items: [{ id: "grid-2", name: "Table2", displayColumn: null }],
|
|
93
|
+
}),
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const tables = await provider.listTables("doc123");
|
|
97
|
+
|
|
98
|
+
expect(mockFetch).toHaveBeenCalledTimes(2);
|
|
99
|
+
expect(tables).toHaveLength(2);
|
|
100
|
+
expect(tables[1].id).toBe("grid-2");
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe("fetchTable", () => {
|
|
105
|
+
it("should fetch all rows from a table", async () => {
|
|
106
|
+
const mockResponse = {
|
|
107
|
+
items: [
|
|
108
|
+
{
|
|
109
|
+
id: "i-row1",
|
|
110
|
+
values: {
|
|
111
|
+
"c-Name": "Button",
|
|
112
|
+
"c-Description": "A clickable button",
|
|
113
|
+
"c-Version": "1.0.0",
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
id: "i-row2",
|
|
118
|
+
values: {
|
|
119
|
+
"c-Name": "Modal",
|
|
120
|
+
"c-Description": "A dialog overlay",
|
|
121
|
+
"c-Version": "2.0.0",
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
],
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
mockFetch.mockResolvedValueOnce({
|
|
128
|
+
ok: true,
|
|
129
|
+
json: () => Promise.resolve(mockResponse),
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
const rows = await provider.fetchTable("dNyzE_TLZDh", "grid-abc123");
|
|
133
|
+
|
|
134
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
135
|
+
expect.stringContaining(
|
|
136
|
+
"https://coda.io/apis/v1/docs/dNyzE_TLZDh/tables/grid-abc123/rows",
|
|
137
|
+
),
|
|
138
|
+
expect.objectContaining({
|
|
139
|
+
headers: expect.objectContaining({
|
|
140
|
+
Authorization: "Bearer test-api-key",
|
|
141
|
+
}),
|
|
142
|
+
}),
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
expect(rows).toEqual([
|
|
146
|
+
{
|
|
147
|
+
_codaId: "i-row1",
|
|
148
|
+
Name: "Button",
|
|
149
|
+
Description: "A clickable button",
|
|
150
|
+
Version: "1.0.0",
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
_codaId: "i-row2",
|
|
154
|
+
Name: "Modal",
|
|
155
|
+
Description: "A dialog overlay",
|
|
156
|
+
Version: "2.0.0",
|
|
157
|
+
},
|
|
158
|
+
]);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("should handle pagination for large tables", async () => {
|
|
162
|
+
// First page
|
|
163
|
+
mockFetch.mockResolvedValueOnce({
|
|
164
|
+
ok: true,
|
|
165
|
+
json: () =>
|
|
166
|
+
Promise.resolve({
|
|
167
|
+
items: [{ id: "i-row1", values: { "c-Name": "Item1" } }],
|
|
168
|
+
nextPageToken: "page2token",
|
|
169
|
+
}),
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
// Second page
|
|
173
|
+
mockFetch.mockResolvedValueOnce({
|
|
174
|
+
ok: true,
|
|
175
|
+
json: () =>
|
|
176
|
+
Promise.resolve({
|
|
177
|
+
items: [{ id: "i-row2", values: { "c-Name": "Item2" } }],
|
|
178
|
+
}),
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
const rows = await provider.fetchTable("doc123", "grid-abc");
|
|
182
|
+
|
|
183
|
+
expect(mockFetch).toHaveBeenCalledTimes(2);
|
|
184
|
+
expect(rows).toHaveLength(2);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("should normalize column names (strip c- prefix)", async () => {
|
|
188
|
+
mockFetch.mockResolvedValueOnce({
|
|
189
|
+
ok: true,
|
|
190
|
+
json: () =>
|
|
191
|
+
Promise.resolve({
|
|
192
|
+
items: [
|
|
193
|
+
{
|
|
194
|
+
id: "i-row1",
|
|
195
|
+
values: {
|
|
196
|
+
"c-MyColumn": "value1",
|
|
197
|
+
"c-Another Column": "value2",
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
],
|
|
201
|
+
}),
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
const rows = await provider.fetchTable("doc", "grid");
|
|
205
|
+
|
|
206
|
+
expect(rows[0]).toEqual({
|
|
207
|
+
_codaId: "i-row1",
|
|
208
|
+
MyColumn: "value1",
|
|
209
|
+
"Another Column": "value2",
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
describe("fetchTableColumns", () => {
|
|
215
|
+
it("should fetch column metadata for a table", async () => {
|
|
216
|
+
const textFormat = { type: "text", isArray: false };
|
|
217
|
+
const lookupFormat = { type: "lookup", isArray: false };
|
|
218
|
+
|
|
219
|
+
const mockResponse = {
|
|
220
|
+
items: [
|
|
221
|
+
{ id: "c-Name", name: "Name", format: textFormat },
|
|
222
|
+
{ id: "c-Version", name: "Version", format: textFormat },
|
|
223
|
+
{ id: "c-Tier", name: "Tier", format: lookupFormat },
|
|
224
|
+
],
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
mockFetch.mockResolvedValueOnce({
|
|
228
|
+
ok: true,
|
|
229
|
+
json: () => Promise.resolve(mockResponse),
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
const columns = await provider.fetchTableColumns("doc", "grid");
|
|
233
|
+
|
|
234
|
+
expect(columns).toEqual([
|
|
235
|
+
{ id: "c-Name", name: "Name", type: "text", format: textFormat },
|
|
236
|
+
{ id: "c-Version", name: "Version", type: "text", format: textFormat },
|
|
237
|
+
{ id: "c-Tier", name: "Tier", type: "lookup", format: lookupFormat },
|
|
238
|
+
]);
|
|
239
|
+
});
|
|
240
|
+
});
|
|
241
|
+
});
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import type { components } from "./CodaProvider.types.generated.js";
|
|
2
|
+
|
|
3
|
+
const CODA_API_BASE = "https://coda.io/apis/v1";
|
|
4
|
+
|
|
5
|
+
// Re-export generated types for external use
|
|
6
|
+
export type CodaTable = components["schemas"]["Table"];
|
|
7
|
+
export type CodaTableReference = components["schemas"]["TableReference"];
|
|
8
|
+
export type CodaTableList = components["schemas"]["TableList"];
|
|
9
|
+
export type CodaRow = components["schemas"]["Row"];
|
|
10
|
+
export type CodaRowList = components["schemas"]["RowList"];
|
|
11
|
+
export type CodaColumn = components["schemas"]["Column"];
|
|
12
|
+
export type CodaColumnList = components["schemas"]["ColumnList"];
|
|
13
|
+
export type CodaCellValue = components["schemas"]["CellValue"];
|
|
14
|
+
|
|
15
|
+
// Simplified types for this provider's API
|
|
16
|
+
export interface TableMetadata {
|
|
17
|
+
id: string;
|
|
18
|
+
name: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ColumnMetadata {
|
|
22
|
+
id: string;
|
|
23
|
+
name: string;
|
|
24
|
+
type: string;
|
|
25
|
+
format: components["schemas"]["ColumnFormat"];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface TableRow {
|
|
29
|
+
_codaId: string;
|
|
30
|
+
[key: string]: unknown;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Coda API client for fetching document tables and data
|
|
35
|
+
*/
|
|
36
|
+
export default class CodaProvider {
|
|
37
|
+
private apiKey: string;
|
|
38
|
+
|
|
39
|
+
constructor() {
|
|
40
|
+
const apiKey = process.env.CODA_API_KEY;
|
|
41
|
+
if (!apiKey) {
|
|
42
|
+
throw new Error("CODA_API_KEY environment variable is required");
|
|
43
|
+
}
|
|
44
|
+
this.apiKey = apiKey;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private get headers(): Record<string, string> {
|
|
48
|
+
return {
|
|
49
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
50
|
+
"Content-Type": "application/json",
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private async fetchWithAuth<T>(url: string): Promise<T> {
|
|
55
|
+
const response = await fetch(url, {
|
|
56
|
+
method: "GET",
|
|
57
|
+
headers: this.headers,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
if (!response.ok) {
|
|
61
|
+
throw new Error(
|
|
62
|
+
`Coda API error: ${response.status} ${response.statusText}`,
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return response.json() as Promise<T>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* List all tables in a document
|
|
71
|
+
*
|
|
72
|
+
* @see https://coda.io/developers/apis/v1#tag/Tables/operation/listTables
|
|
73
|
+
*/
|
|
74
|
+
async listTables(documentId: string): Promise<TableMetadata[]> {
|
|
75
|
+
const tables: TableMetadata[] = [];
|
|
76
|
+
let pageToken: string | undefined;
|
|
77
|
+
|
|
78
|
+
do {
|
|
79
|
+
const url = new URL(`${CODA_API_BASE}/docs/${documentId}/tables`);
|
|
80
|
+
if (pageToken) {
|
|
81
|
+
url.searchParams.set("pageToken", pageToken);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const response = await this.fetchWithAuth<CodaTableList>(url.toString());
|
|
85
|
+
|
|
86
|
+
for (const table of response.items) {
|
|
87
|
+
tables.push({
|
|
88
|
+
id: table.id,
|
|
89
|
+
name: table.name,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
pageToken = response.nextPageToken;
|
|
94
|
+
} while (pageToken);
|
|
95
|
+
|
|
96
|
+
return tables;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Fetch all rows from a table
|
|
101
|
+
*
|
|
102
|
+
* @see https://coda.io/developers/apis/v1#tag/Rows/operation/listRows
|
|
103
|
+
*/
|
|
104
|
+
async fetchTable(documentId: string, tableId: string): Promise<TableRow[]> {
|
|
105
|
+
const rows: TableRow[] = [];
|
|
106
|
+
let pageToken: string | undefined;
|
|
107
|
+
|
|
108
|
+
do {
|
|
109
|
+
const url = new URL(
|
|
110
|
+
`${CODA_API_BASE}/docs/${documentId}/tables/${tableId}/rows`,
|
|
111
|
+
);
|
|
112
|
+
url.searchParams.set("useColumnNames", "true");
|
|
113
|
+
if (pageToken) {
|
|
114
|
+
url.searchParams.set("pageToken", pageToken);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const response = await this.fetchWithAuth<CodaRowList>(url.toString());
|
|
118
|
+
|
|
119
|
+
for (const row of response.items) {
|
|
120
|
+
const normalizedRow: TableRow = { _codaId: row.id };
|
|
121
|
+
|
|
122
|
+
for (const [key, value] of Object.entries(row.values)) {
|
|
123
|
+
// Coda API prefixes column names with "c-" when using useColumnNames=true.
|
|
124
|
+
// Strip this prefix to get clean column names matching the display names.
|
|
125
|
+
const normalizedKey = key.startsWith("c-") ? key.slice(2) : key;
|
|
126
|
+
normalizedRow[normalizedKey] = value;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
rows.push(normalizedRow);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
pageToken = response.nextPageToken;
|
|
133
|
+
} while (pageToken);
|
|
134
|
+
|
|
135
|
+
return rows;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Fetch column metadata for a table
|
|
140
|
+
*
|
|
141
|
+
* @see https://coda.io/developers/apis/v1#tag/Columns/operation/listColumns
|
|
142
|
+
*/
|
|
143
|
+
async fetchTableColumns(
|
|
144
|
+
documentId: string,
|
|
145
|
+
tableId: string,
|
|
146
|
+
): Promise<ColumnMetadata[]> {
|
|
147
|
+
const columns: ColumnMetadata[] = [];
|
|
148
|
+
let pageToken: string | undefined;
|
|
149
|
+
|
|
150
|
+
do {
|
|
151
|
+
const url = new URL(
|
|
152
|
+
`${CODA_API_BASE}/docs/${documentId}/tables/${tableId}/columns`,
|
|
153
|
+
);
|
|
154
|
+
if (pageToken) {
|
|
155
|
+
url.searchParams.set("pageToken", pageToken);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const response = await this.fetchWithAuth<CodaColumnList>(url.toString());
|
|
159
|
+
|
|
160
|
+
for (const col of response.items) {
|
|
161
|
+
columns.push({
|
|
162
|
+
id: col.id,
|
|
163
|
+
name: col.name,
|
|
164
|
+
type: col.format.type,
|
|
165
|
+
format: col.format,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
pageToken = response.nextPageToken;
|
|
170
|
+
} while (pageToken);
|
|
171
|
+
|
|
172
|
+
return columns;
|
|
173
|
+
}
|
|
174
|
+
}
|