|
1 | | -import type { ChenActionItem, ChenAuthResponse, ChenProfile, ChenSqlHints, ChenTreeNode } from "~/chen/types"; |
| 1 | +import type { |
| 2 | + ChenActionItem, |
| 3 | + ChenAuthResponse, |
| 4 | + ChenProfile, |
| 5 | + ChenTreeNode |
| 6 | +} from "~/chen/types"; |
| 7 | +import type { |
| 8 | + ChenQualifiedRelation, |
| 9 | + ChenRelationColumnsMetadata, |
| 10 | + ChenRelationMetadataPage, |
| 11 | + ChenSqlMetadataScope |
| 12 | +} from "~/chen/types/sqlMetadata"; |
2 | 13 |
|
3 | 14 | const buildHeaders = (token?: string, init?: HeadersInit) => ({ |
4 | 15 | ...getWebApiHeaders(), |
5 | 16 | ...(token ? { token } : {}), |
6 | 17 | ...(init || {}) |
7 | 18 | }); |
8 | 19 |
|
9 | | -export function chenPath(path: string, endpointUrl = window.location.origin) { |
| 20 | +export function chenPath(path: string, endpointUrl?: string) { |
10 | 21 | const connectorPath = `/chen${path.startsWith("/") ? path : `/${path}`}`; |
11 | | - const endpoint = new URL(endpointUrl || window.location.origin, window.location.origin); |
| 22 | + const currentOrigin = typeof window === "undefined" ? "http://localhost" : window.location.origin; |
| 23 | + const endpoint = new URL(endpointUrl || currentOrigin, currentOrigin); |
12 | 24 |
|
13 | | - if (endpoint.origin === window.location.origin) { |
| 25 | + if (endpoint.origin === currentOrigin) { |
14 | 26 | return withWebSitePrefix(connectorPath); |
15 | 27 | } |
16 | 28 |
|
@@ -76,35 +88,100 @@ export async function uploadChenSqlFile( |
76 | 88 | return { path: result.path }; |
77 | 89 | } |
78 | 90 |
|
79 | | -export async function fetchChenSqlHints( |
| 91 | +export async function fetchChenSqlRelations( |
80 | 92 | chenToken: string, |
81 | | - nodeKey: string, |
82 | | - context: string, |
| 93 | + scope: ChenSqlMetadataScope, |
| 94 | + prefix = "", |
| 95 | + limit = 100, |
83 | 96 | fetchImpl: typeof fetch = fetch, |
84 | 97 | endpointUrl?: string |
85 | | -): Promise<ChenSqlHints> { |
86 | | - const response = await fetchImpl(chenPath("/api/resources/hints", endpointUrl), { |
| 98 | +): Promise<ChenRelationMetadataPage> { |
| 99 | + const response = await fetchImpl(chenPath("/api/resources/metadata/relations", endpointUrl), { |
87 | 100 | method: "POST", |
88 | 101 | credentials: "include", |
89 | 102 | headers: { |
90 | 103 | ...buildHeaders(chenToken, getWebApiMutationHeaders()), |
91 | 104 | "Content-Type": "application/json" |
92 | 105 | }, |
93 | | - body: JSON.stringify({ nodeKey, context }) |
| 106 | + body: JSON.stringify({ ...scope, prefix, limit }) |
94 | 107 | }); |
95 | 108 | const result = await readJson<unknown>(response); |
96 | | - if (!result || typeof result !== "object" || Array.isArray(result)) { |
97 | | - throw new Error("Chen returned malformed SQL hints"); |
| 109 | + if (!isRecord(result) || !Array.isArray(result.items) || typeof result.truncated !== "boolean") { |
| 110 | + throw new Error("Chen returned malformed SQL relation metadata"); |
98 | 111 | } |
99 | 112 |
|
100 | | - return Object.fromEntries( |
101 | | - Object.entries(result) |
102 | | - .filter( |
103 | | - (entry): entry is [string, string[]] => |
104 | | - Array.isArray(entry[1]) && entry[1].every((column) => typeof column === "string") |
105 | | - ) |
106 | | - .map(([table, columns]) => [table, [...columns]]) |
107 | | - ); |
| 113 | + return { |
| 114 | + items: result.items.map(parseQualifiedRelation), |
| 115 | + truncated: result.truncated |
| 116 | + }; |
| 117 | +} |
| 118 | + |
| 119 | +export async function fetchChenSqlColumns( |
| 120 | + chenToken: string, |
| 121 | + scope: ChenSqlMetadataScope, |
| 122 | + relations: ChenQualifiedRelation[], |
| 123 | + fetchImpl: typeof fetch = fetch, |
| 124 | + endpointUrl?: string |
| 125 | +): Promise<ChenRelationColumnsMetadata[]> { |
| 126 | + const response = await fetchImpl(chenPath("/api/resources/metadata/columns", endpointUrl), { |
| 127 | + method: "POST", |
| 128 | + credentials: "include", |
| 129 | + headers: { |
| 130 | + ...buildHeaders(chenToken, getWebApiMutationHeaders()), |
| 131 | + "Content-Type": "application/json" |
| 132 | + }, |
| 133 | + body: JSON.stringify({ ...scope, relations }) |
| 134 | + }); |
| 135 | + const result = await readJson<unknown>(response); |
| 136 | + if (!isRecord(result) || !Array.isArray(result.items)) { |
| 137 | + throw new Error("Chen returned malformed SQL column metadata"); |
| 138 | + } |
| 139 | + |
| 140 | + return result.items.map((item) => { |
| 141 | + if (!isRecord(item) || !Array.isArray(item.columns)) { |
| 142 | + throw new Error("Chen returned malformed SQL column metadata"); |
| 143 | + } |
| 144 | + return { |
| 145 | + relation: parseQualifiedRelation(item.relation), |
| 146 | + columns: item.columns.map((column) => { |
| 147 | + if ( |
| 148 | + !isRecord(column) || |
| 149 | + typeof column.name !== "string" || |
| 150 | + !(typeof column.dataType === "string" || column.dataType === null) || |
| 151 | + typeof column.nullable !== "boolean" |
| 152 | + ) { |
| 153 | + throw new Error("Chen returned malformed SQL column metadata"); |
| 154 | + } |
| 155 | + return { |
| 156 | + name: column.name, |
| 157 | + dataType: column.dataType, |
| 158 | + nullable: column.nullable |
| 159 | + }; |
| 160 | + }) |
| 161 | + }; |
| 162 | + }); |
| 163 | +} |
| 164 | + |
| 165 | +function isRecord(value: unknown): value is Record<string, unknown> { |
| 166 | + return Boolean(value) && typeof value === "object" && !Array.isArray(value); |
| 167 | +} |
| 168 | + |
| 169 | +function parseQualifiedRelation(value: unknown): ChenQualifiedRelation { |
| 170 | + if ( |
| 171 | + !isRecord(value) || |
| 172 | + !(typeof value.catalog === "string" || value.catalog === null) || |
| 173 | + typeof value.schema !== "string" || |
| 174 | + typeof value.name !== "string" || |
| 175 | + !(value.kind === "table" || value.kind === "view") |
| 176 | + ) { |
| 177 | + throw new Error("Chen returned malformed SQL relation metadata"); |
| 178 | + } |
| 179 | + return { |
| 180 | + catalog: value.catalog, |
| 181 | + schema: value.schema, |
| 182 | + name: value.name, |
| 183 | + kind: value.kind |
| 184 | + }; |
108 | 185 | } |
109 | 186 |
|
110 | 187 | export function sanitizeChenExportFileName(value: string, fallback = "chen-export") { |
|
0 commit comments