@malloy-publisher/sdk 0.8.0 → 0.8.1

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,7 +27,11 @@ import { DashboardBar } from "../Dashboard/DashboardBar";
27
27
  import { Loading } from "../Loading";
28
28
  import { TILE_MAX_HEIGHT } from "../RenderedResult/resultSizing";
29
29
  import { useServer } from "../ServerProvider";
30
- import { buildCatalog, type PackageCatalog } from "./catalog";
30
+ import {
31
+ buildCatalog,
32
+ visibleToDashboard,
33
+ type PackageCatalog,
34
+ } from "./catalog";
31
35
  import { DashboardBuilder } from "./DashboardBuilder";
32
36
  import type { DashboardDocument } from "./document";
33
37
  import { previewGivens, previewTileQuery } from "./preview";
@@ -785,36 +789,51 @@ function Surface({
785
789
  [dashboardList, slug],
786
790
  );
787
791
 
788
- // The catalog: the models this file imports, which is where its tiles'
789
- // sources and their fields are declared.
790
- const importPaths = useMemo(() => {
792
+ // The catalog: what the package publishes, limited to what this file can
793
+ // see. A tile runs against this file and may read only the package surface,
794
+ // so offering a source from any other file would offer a tile that 404s.
795
+ // Files off the surface are not readable anyway (their model GET is 404).
796
+ const imports = useMemo(() => {
791
797
  const dir = modelPath.slice(0, modelPath.lastIndexOf("/") + 1);
792
- const resolved = new Set<string>();
793
- for (const imported of opened.document.imports) {
794
- const url = new URL(imported.from, `https://malloy.invalid/${dir}`);
795
- resolved.add(url.pathname.slice(1));
796
- }
797
- return [...resolved];
798
+ return opened.document.imports.map((imported) => ({
799
+ ...imported,
800
+ path: new URL(
801
+ imported.from,
802
+ `https://malloy.invalid/${dir}`,
803
+ ).pathname.slice(1),
804
+ }));
798
805
  }, [opened.document.imports, modelPath]);
799
806
  const { data: catalog } = useQueryWithApiError<PackageCatalog>({
800
807
  queryKey: [
801
808
  "dashboard-editor-catalog",
802
809
  environmentName,
803
810
  packageName,
804
- ...importPaths,
811
+ ...imports.map((i) => i.path),
805
812
  versionId,
806
813
  ],
807
814
  queryFn: async () => {
808
- const models = await Promise.all(
809
- importPaths.map((path) =>
815
+ const listed = (
816
+ await apiClients.models.listModels(
817
+ environmentName,
818
+ packageName,
819
+ versionId,
820
+ )
821
+ ).data.filter((m) => m.path && !m.error);
822
+ // One model that fails to load (a reload racing this fetch, say)
823
+ // costs the catalog that model's sources, not every suggestion.
824
+ const settled = await Promise.allSettled(
825
+ listed.map((m) =>
810
826
  apiClients.models
811
- .getModel(environmentName, packageName, path, versionId)
827
+ .getModel(environmentName, packageName, m.path!, versionId)
812
828
  .then((response) => response.data),
813
829
  ),
814
830
  );
815
- return buildCatalog(models);
831
+ const models = settled.flatMap((result) =>
832
+ result.status === "fulfilled" ? [result.value] : [],
833
+ );
834
+ return visibleToDashboard(buildCatalog(models), imports, models);
816
835
  },
817
- enabled: importPaths.length > 0,
836
+ enabled: imports.length > 0,
818
837
  });
819
838
 
820
839
  const [doc, setDoc] = useState(opened.document);
@@ -9,6 +9,7 @@ import {
9
9
  docOf,
10
10
  filterableFields,
11
11
  isDashboardModel,
12
+ visibleToDashboard,
12
13
  } from "./catalog";
13
14
 
14
15
  /**
@@ -137,6 +138,49 @@ describe("buildCatalog", () => {
137
138
  });
138
139
  });
139
140
 
141
+ describe("visibleToDashboard", () => {
142
+ const published: CompiledModel[] = [
143
+ {
144
+ path: "index.malloy",
145
+ sources: [{ name: "orders" }, { name: "users" }],
146
+ },
147
+ ];
148
+ const catalog = buildCatalog(published);
149
+ const names = (c: ReturnType<typeof buildCatalog>) =>
150
+ c.sources.map((s) => s.name).sort();
151
+
152
+ it("offers only the names a named import brings", () => {
153
+ const seen = visibleToDashboard(
154
+ catalog,
155
+ [{ kind: "names", names: ["orders"], path: "orders.malloy" }],
156
+ published,
157
+ );
158
+ expect(names(seen)).toEqual(["orders"]);
159
+ });
160
+
161
+ it("offers a published model's sources to a whole-file import of it", () => {
162
+ const seen = visibleToDashboard(
163
+ catalog,
164
+ [{ kind: "all", path: "index.malloy" }],
165
+ published,
166
+ );
167
+ expect(names(seen)).toEqual(["orders", "users"]);
168
+ });
169
+
170
+ it("offers everything published when a whole-file import cannot be read", () => {
171
+ // The imported file is off the surface, so what it declares is unknown.
172
+ const seen = visibleToDashboard(
173
+ catalog,
174
+ [
175
+ { kind: "names", names: ["orders"], path: "orders.malloy" },
176
+ { kind: "all", path: "hidden.malloy" },
177
+ ],
178
+ published,
179
+ );
180
+ expect(names(seen)).toEqual(["orders", "users"]);
181
+ });
182
+ });
183
+
140
184
  describe("filterableFields", () => {
141
185
  // Captured from the storefront package's `order_items`: a join's fields are
142
186
  // what people filter on, and a view is not a field at all.
@@ -223,3 +223,39 @@ export function buildCatalog(models: CompiledModel[]): PackageCatalog {
223
223
 
224
224
  return { sources };
225
225
  }
226
+
227
+ /**
228
+ * The part of the package catalog a dashboard file can actually use.
229
+ *
230
+ * The builder never writes an import, so a source the file cannot see would
231
+ * produce a tile that does not compile. A named import says exactly which
232
+ * names it brings. A whole-file import of a published model brings that
233
+ * model's sources. A whole-file import of a file off the surface cannot be
234
+ * read, so then every published source is offered and a wrong pick is caught
235
+ * by the compile check the editor already runs.
236
+ */
237
+ export function visibleToDashboard(
238
+ catalog: PackageCatalog,
239
+ imports: ReadonlyArray<
240
+ | { kind: "all"; path: string }
241
+ | { kind: "names"; names: string[]; path: string }
242
+ >,
243
+ published: CompiledModel[],
244
+ ): PackageCatalog {
245
+ const visible = new Set<string>();
246
+ for (const imported of imports) {
247
+ if (imported.kind === "names") {
248
+ for (const name of imported.names) visible.add(name);
249
+ continue;
250
+ }
251
+ const model = published.find((m) => pathOf(m) === imported.path);
252
+ if (!model) return catalog;
253
+ for (const source of model.sources ?? []) {
254
+ if (source.name) visible.add(source.name);
255
+ }
256
+ }
257
+ return {
258
+ ...catalog,
259
+ sources: catalog.sources.filter((source) => visible.has(source.name)),
260
+ };
261
+ }