@eventcatalog/core 3.44.2 → 3.46.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.
Files changed (39) hide show
  1. package/dist/analytics/analytics.cjs +1 -1
  2. package/dist/analytics/analytics.js +2 -2
  3. package/dist/analytics/log-build.cjs +1 -1
  4. package/dist/analytics/log-build.js +3 -3
  5. package/dist/{chunk-THDZCUFV.js → chunk-DOHA5HNJ.js} +1 -1
  6. package/dist/{chunk-BLJ5FRR3.js → chunk-FNOYJEUK.js} +1 -1
  7. package/dist/{chunk-LIIOK6SZ.js → chunk-JS6IYB55.js} +1 -1
  8. package/dist/{chunk-IU3D7JRW.js → chunk-TLLUDBO4.js} +1 -1
  9. package/dist/{chunk-RUACTEJR.js → chunk-X4AESI6E.js} +1 -1
  10. package/dist/constants.cjs +1 -1
  11. package/dist/constants.js +1 -1
  12. package/dist/eventcatalog.cjs +1 -1
  13. package/dist/eventcatalog.config.d.cts +29 -0
  14. package/dist/eventcatalog.config.d.ts +29 -0
  15. package/dist/eventcatalog.js +10 -10
  16. package/dist/generate.cjs +1 -1
  17. package/dist/generate.js +3 -3
  18. package/dist/utils/cli-logger.cjs +1 -1
  19. package/dist/utils/cli-logger.js +2 -2
  20. package/eventcatalog/astro.config.mjs +7 -10
  21. package/eventcatalog/ec.config.mjs +20 -0
  22. package/eventcatalog/src/components/MDX/SchemaViewer/SchemaViewerPortal.tsx +1 -1
  23. package/eventcatalog/src/components/MDX/SchemaViewer/SchemaViewerRoot.astro +19 -47
  24. package/eventcatalog/src/components/MDX/SchemaViewer/schema-viewer-utils.spec.ts +53 -0
  25. package/eventcatalog/src/components/MDX/SchemaViewer/schema-viewer-utils.ts +133 -0
  26. package/eventcatalog/src/content.config.ts +65 -0
  27. package/eventcatalog/src/enterprise/api/schemas/[collection]/[id]/[version]/index.ts +42 -50
  28. package/eventcatalog/src/enterprise/tools/catalog-tools.ts +23 -0
  29. package/eventcatalog/src/pages/docs/[type]/[id]/[version]/index.astro +1 -1
  30. package/eventcatalog/src/pages/docs/llm/schemas.txt.ts +25 -17
  31. package/eventcatalog/src/pages/schemas/[type]/[id]/[version]/_index.data.ts +33 -16
  32. package/eventcatalog/src/pages/schemas/[type]/[id]/[version]/index.astro +18 -8
  33. package/eventcatalog/src/pages/schemas/explorer/_index.data.ts +45 -21
  34. package/eventcatalog/src/stores/sidebar-store/builders/message.ts +25 -2
  35. package/eventcatalog/src/stores/sidebar-store/builders/shared.ts +1 -0
  36. package/eventcatalog/src/stores/sidebar-store/state.ts +3 -0
  37. package/eventcatalog/src/utils/collections/schema-loader.ts +523 -0
  38. package/eventcatalog/src/utils/files.ts +1 -1
  39. package/package.json +4 -4
@@ -36,7 +36,7 @@ module.exports = __toCommonJS(analytics_exports);
36
36
  var import_os = __toESM(require("os"), 1);
37
37
 
38
38
  // package.json
39
- var version = "3.44.2";
39
+ var version = "3.46.0";
40
40
 
41
41
  // src/constants.ts
42
42
  var VERSION = version;
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  raiseEvent
3
- } from "../chunk-LIIOK6SZ.js";
4
- import "../chunk-THDZCUFV.js";
3
+ } from "../chunk-JS6IYB55.js";
4
+ import "../chunk-DOHA5HNJ.js";
5
5
  export {
6
6
  raiseEvent
7
7
  };
@@ -140,7 +140,7 @@ var verifyRequiredFieldsAreInCatalogConfigFile = async (projectDirectory) => {
140
140
  var import_os = __toESM(require("os"), 1);
141
141
 
142
142
  // package.json
143
- var version = "3.44.2";
143
+ var version = "3.46.0";
144
144
 
145
145
  // src/constants.ts
146
146
  var VERSION = version;
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  log_build_default
3
- } from "../chunk-BLJ5FRR3.js";
4
- import "../chunk-LIIOK6SZ.js";
3
+ } from "../chunk-FNOYJEUK.js";
4
+ import "../chunk-JS6IYB55.js";
5
5
  import "../chunk-DAOXTQVS.js";
6
- import "../chunk-THDZCUFV.js";
6
+ import "../chunk-DOHA5HNJ.js";
7
7
  import "../chunk-6QENHZZP.js";
8
8
  export {
9
9
  log_build_default as default
@@ -1,5 +1,5 @@
1
1
  // package.json
2
- var version = "3.44.2";
2
+ var version = "3.46.0";
3
3
 
4
4
  // src/constants.ts
5
5
  var VERSION = version;
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  raiseEvent
3
- } from "./chunk-LIIOK6SZ.js";
3
+ } from "./chunk-JS6IYB55.js";
4
4
  import {
5
5
  countResources,
6
6
  serializeCounts
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  VERSION
3
- } from "./chunk-THDZCUFV.js";
3
+ } from "./chunk-DOHA5HNJ.js";
4
4
 
5
5
  // src/analytics/analytics.js
6
6
  import os from "os";
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  VERSION
3
- } from "./chunk-THDZCUFV.js";
3
+ } from "./chunk-DOHA5HNJ.js";
4
4
 
5
5
  // src/utils/cli-logger.ts
6
6
  import pc from "picocolors";
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  logger
3
- } from "./chunk-IU3D7JRW.js";
3
+ } from "./chunk-TLLUDBO4.js";
4
4
  import {
5
5
  cleanup,
6
6
  getEventCatalogConfigFile
@@ -25,7 +25,7 @@ __export(constants_exports, {
25
25
  module.exports = __toCommonJS(constants_exports);
26
26
 
27
27
  // package.json
28
- var version = "3.44.2";
28
+ var version = "3.46.0";
29
29
 
30
30
  // src/constants.ts
31
31
  var VERSION = version;
package/dist/constants.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  VERSION
3
- } from "./chunk-THDZCUFV.js";
3
+ } from "./chunk-DOHA5HNJ.js";
4
4
  export {
5
5
  VERSION
6
6
  };
@@ -144,7 +144,7 @@ var verifyRequiredFieldsAreInCatalogConfigFile = async (projectDirectory) => {
144
144
  var import_picocolors = __toESM(require("picocolors"), 1);
145
145
 
146
146
  // package.json
147
- var version = "3.44.2";
147
+ var version = "3.46.0";
148
148
 
149
149
  // src/constants.ts
150
150
  var VERSION = version;
@@ -58,6 +58,28 @@ type DirectorySource = {
58
58
  loadUsers?: () => Promise<DirectoryEntry[]>;
59
59
  loadTeams?: () => Promise<DirectoryEntry[]>;
60
60
  };
61
+ type SchemaEntry = {
62
+ id: string;
63
+ name?: string;
64
+ format?: string;
65
+ content: string;
66
+ source: {
67
+ provider: string;
68
+ id?: string;
69
+ url?: string;
70
+ ref?: string;
71
+ path?: string;
72
+ [key: string]: unknown;
73
+ };
74
+ };
75
+ type SchemaSource = {
76
+ type: 'schemas';
77
+ name: string;
78
+ canResolve: (id: string) => boolean;
79
+ resolve: (id: string, context?: {
80
+ messageFilePath?: string;
81
+ }) => Promise<SchemaEntry | undefined>;
82
+ };
61
83
  type DirectoryConfig = {
62
84
  /**
63
85
  * External sources that sync users and teams into EventCatalog.
@@ -71,6 +93,12 @@ type DirectoryConfig = {
71
93
  */
72
94
  conflictStrategy?: 'local-wins' | 'source-wins' | 'error';
73
95
  };
96
+ type SchemaConfig = {
97
+ /**
98
+ * External sources that resolve message schema references into the generated schemas collection.
99
+ */
100
+ sources?: SchemaSource[];
101
+ };
74
102
  type AuthConfig = {
75
103
  enabled: boolean;
76
104
  };
@@ -221,6 +249,7 @@ interface Config {
221
249
  domains?: ResourceDependency[];
222
250
  };
223
251
  directory?: DirectoryConfig;
252
+ schemas?: SchemaConfig;
224
253
  mermaid?: {
225
254
  maxTextSize?: number;
226
255
  iconPacks?: string[];
@@ -58,6 +58,28 @@ type DirectorySource = {
58
58
  loadUsers?: () => Promise<DirectoryEntry[]>;
59
59
  loadTeams?: () => Promise<DirectoryEntry[]>;
60
60
  };
61
+ type SchemaEntry = {
62
+ id: string;
63
+ name?: string;
64
+ format?: string;
65
+ content: string;
66
+ source: {
67
+ provider: string;
68
+ id?: string;
69
+ url?: string;
70
+ ref?: string;
71
+ path?: string;
72
+ [key: string]: unknown;
73
+ };
74
+ };
75
+ type SchemaSource = {
76
+ type: 'schemas';
77
+ name: string;
78
+ canResolve: (id: string) => boolean;
79
+ resolve: (id: string, context?: {
80
+ messageFilePath?: string;
81
+ }) => Promise<SchemaEntry | undefined>;
82
+ };
61
83
  type DirectoryConfig = {
62
84
  /**
63
85
  * External sources that sync users and teams into EventCatalog.
@@ -71,6 +93,12 @@ type DirectoryConfig = {
71
93
  */
72
94
  conflictStrategy?: 'local-wins' | 'source-wins' | 'error';
73
95
  };
96
+ type SchemaConfig = {
97
+ /**
98
+ * External sources that resolve message schema references into the generated schemas collection.
99
+ */
100
+ sources?: SchemaSource[];
101
+ };
74
102
  type AuthConfig = {
75
103
  enabled: boolean;
76
104
  };
@@ -221,6 +249,7 @@ interface Config {
221
249
  domains?: ResourceDependency[];
222
250
  };
223
251
  directory?: DirectoryConfig;
252
+ schemas?: SchemaConfig;
224
253
  mermaid?: {
225
254
  maxTextSize?: number;
226
255
  iconPacks?: string[];
@@ -1,7 +1,8 @@
1
1
  import {
2
- runMigrations
3
- } from "./chunk-XUAF2H54.js";
4
- import "./chunk-CA4U2JP7.js";
2
+ log_build_default
3
+ } from "./chunk-FNOYJEUK.js";
4
+ import "./chunk-JS6IYB55.js";
5
+ import "./chunk-DAOXTQVS.js";
5
6
  import {
6
7
  resolve_catalog_dependencies_default
7
8
  } from "./chunk-LHR4G2UO.js";
@@ -12,10 +13,9 @@ import {
12
13
  watch
13
14
  } from "./chunk-3H2RT3CM.js";
14
15
  import {
15
- log_build_default
16
- } from "./chunk-BLJ5FRR3.js";
17
- import "./chunk-LIIOK6SZ.js";
18
- import "./chunk-DAOXTQVS.js";
16
+ runMigrations
17
+ } from "./chunk-XUAF2H54.js";
18
+ import "./chunk-CA4U2JP7.js";
19
19
  import {
20
20
  catalogToAstro
21
21
  } from "./chunk-4SMA4HQ3.js";
@@ -28,13 +28,13 @@ import {
28
28
  } from "./chunk-B7HCX5HM.js";
29
29
  import {
30
30
  generate
31
- } from "./chunk-RUACTEJR.js";
31
+ } from "./chunk-X4AESI6E.js";
32
32
  import {
33
33
  logger
34
- } from "./chunk-IU3D7JRW.js";
34
+ } from "./chunk-TLLUDBO4.js";
35
35
  import {
36
36
  VERSION
37
- } from "./chunk-THDZCUFV.js";
37
+ } from "./chunk-DOHA5HNJ.js";
38
38
  import {
39
39
  getEventCatalogConfigFile,
40
40
  verifyRequiredFieldsAreInCatalogConfigFile
package/dist/generate.cjs CHANGED
@@ -108,7 +108,7 @@ var getEventCatalogConfigFile = async (projectDirectory) => {
108
108
  var import_picocolors = __toESM(require("picocolors"), 1);
109
109
 
110
110
  // package.json
111
- var version = "3.44.2";
111
+ var version = "3.46.0";
112
112
 
113
113
  // src/constants.ts
114
114
  var VERSION = version;
package/dist/generate.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  generate
3
- } from "./chunk-RUACTEJR.js";
4
- import "./chunk-IU3D7JRW.js";
5
- import "./chunk-THDZCUFV.js";
3
+ } from "./chunk-X4AESI6E.js";
4
+ import "./chunk-TLLUDBO4.js";
5
+ import "./chunk-DOHA5HNJ.js";
6
6
  import "./chunk-6QENHZZP.js";
7
7
  export {
8
8
  generate
@@ -36,7 +36,7 @@ module.exports = __toCommonJS(cli_logger_exports);
36
36
  var import_picocolors = __toESM(require("picocolors"), 1);
37
37
 
38
38
  // package.json
39
- var version = "3.44.2";
39
+ var version = "3.46.0";
40
40
 
41
41
  // src/constants.ts
42
42
  var VERSION = version;
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  logger
3
- } from "../chunk-IU3D7JRW.js";
4
- import "../chunk-THDZCUFV.js";
3
+ } from "../chunk-TLLUDBO4.js";
4
+ import "../chunk-DOHA5HNJ.js";
5
5
  export {
6
6
  logger
7
7
  };
@@ -19,6 +19,11 @@ import rehypeExpressiveCode from 'rehype-expressive-code';
19
19
  /** @type {import('bin/eventcatalog.config').Config} */
20
20
  import config from './eventcatalog.config';
21
21
  import expressiveCode from 'astro-expressive-code';
22
+ // Expressive Code options (including the non-serializable `themeCssSelector`
23
+ // function) live in ec.config.mjs. The `expressiveCode()` integration loads that
24
+ // file automatically; the rehype plugin used by mdx() below needs it passed
25
+ // explicitly, so we import it here to keep a single source of truth.
26
+ import expressiveCodeConfig from './ec.config.mjs';
22
27
  import ecstudioWatcher from './integrations/ecstudio-watcher.mjs';
23
28
  import eventCatalogIntegration from './src/enterprise/integrations/eventcatalog-features.ts';
24
29
 
@@ -30,13 +35,6 @@ const isDevMode = process.env.EVENTCATALOG_DEV_MODE === 'true';
30
35
  const effectiveOutput = isDevMode ? 'server' : config.output || 'static';
31
36
  const searchType = config.search?.type || 'resource';
32
37
 
33
- const expressiveCodeConfig = {
34
- themes: ['github-light', 'github-dark'],
35
- defaultProps: {
36
- wrap: true,
37
- },
38
- };
39
-
40
38
  const markdownRemarkPlugins = [remarkDirective, remarkDirectives, remarkComment, mermaid, plantuml];
41
39
  const mdxRemarkPlugins = [...markdownRemarkPlugins, remarkResourceRef];
42
40
 
@@ -78,9 +76,8 @@ export default defineConfig({
78
76
  devToolbar: { enabled: false },
79
77
  integrations: [
80
78
  react(),
81
- expressiveCode({
82
- ...expressiveCodeConfig,
83
- }),
79
+ // Options are loaded automatically from ec.config.mjs.
80
+ expressiveCode(),
84
81
  mdx({
85
82
  // https://docs.astro.build/en/guides/integrations-guide/mdx/#optimize
86
83
  optimize: config.mdxOptimize || false,
@@ -0,0 +1,20 @@
1
+ import { defineEcConfig } from 'astro-expressive-code';
2
+
3
+ // Expressive Code configuration lives in this dedicated file (rather than inline
4
+ // in astro.config.mjs) because it contains non-serializable options such as the
5
+ // `themeCssSelector` function. Astro's `<Code>` component (used on the print
6
+ // pages) requires Expressive Code options coming from the Astro config to be
7
+ // JSON-serializable, so functions must be defined here instead.
8
+ //
9
+ // `useDarkModeMediaQuery: false` + `themeCssSelector` make code blocks follow
10
+ // EventCatalog's own `data-theme` toggle instead of the OS `prefers-color-scheme`
11
+ // media query, so they stay readable when the catalog theme and the OS
12
+ // preference disagree (see issue #2607).
13
+ export default defineEcConfig({
14
+ themes: ['github-light', 'github-dark'],
15
+ useDarkModeMediaQuery: false,
16
+ themeCssSelector: (theme) => `[data-theme='${theme.type}']`,
17
+ defaultProps: {
18
+ wrap: true,
19
+ },
20
+ });
@@ -5,7 +5,7 @@ const SchemaViewerPortal = (props: any) => {
5
5
 
6
6
  return (
7
7
  <div
8
- id={`${props.id}-${props.file}-SchemaViewer-portal`}
8
+ data-schema-viewer-portal-id={props.id}
9
9
  data-expand={expandBool ? 'true' : 'false'}
10
10
  data-max-height={props.maxHeight}
11
11
  data-search={searchBool ? 'true' : 'false'}
@@ -1,13 +1,13 @@
1
1
  ---
2
- const { id, filePath } = Astro.props;
2
+ const { id, filePath, collection, version } = Astro.props;
3
3
  import fs from 'node:fs/promises';
4
- import { existsSync } from 'fs';
5
- import yaml from 'js-yaml';
4
+ import { getCollection } from 'astro:content';
6
5
  import JSONSchemaViewer from '@components/SchemaExplorer/JSONSchemaViewer';
7
6
  import AvroSchemaViewer from '@components/SchemaExplorer/AvroSchemaViewer';
8
7
  import Admonition from '../Admonition';
9
8
  import { getMDXComponentsByName } from '@utils/markdown';
10
- import { getAbsoluteFilePathForAstroFile, resolveProjectPath, isAvroSchema } from '@utils/files';
9
+ import { resolveProjectPath } from '@utils/files';
10
+ import { resolveSchemaViewer } from './schema-viewer-utils';
11
11
 
12
12
  let schemas = [];
13
13
 
@@ -15,45 +15,11 @@ try {
15
15
  const absoluteFilePath = resolveProjectPath(filePath);
16
16
  const file = await fs.readFile(absoluteFilePath, 'utf-8');
17
17
  const schemaViewers = getMDXComponentsByName(file, 'SchemaViewer');
18
+ const collectionSchemas = collection && version ? await getCollection('schemas') : [];
18
19
 
19
- // Loop around all the possible SchemaViewers in the file.
20
- const getAllComponents = schemaViewers.map(async (schemaViewerProps: any, index: number) => {
21
- const schemaPath = getAbsoluteFilePathForAstroFile(filePath, schemaViewerProps.file);
22
- const exists = existsSync(schemaPath);
23
- let schema;
24
- let render = true;
25
- let isAvro = false;
26
-
27
- if (exists) {
28
- // Check if this is an Avro schema file
29
- isAvro = isAvroSchema(schemaPath);
30
-
31
- // Load the schema for the component
32
- schema = await fs.readFile(schemaPath, 'utf-8');
33
-
34
- if (schemaPath.endsWith('.yml') || schemaPath.endsWith('.yaml')) {
35
- schema = yaml.load(schema);
36
- } else {
37
- schema = JSON.parse(schema);
38
-
39
- // For non-Avro schemas, let JSON schema control if the component should be rendered
40
- if (!isAvro && schema['x-eventcatalog-render-schema-viewer'] !== undefined) {
41
- render = schema['x-eventcatalog-render-schema-viewer'];
42
- }
43
- }
44
- }
45
-
46
- return {
47
- id: schemaViewerProps.id || id,
48
- exists,
49
- schema,
50
- schemaPath,
51
- isAvroSchema: isAvro,
52
- ...schemaViewerProps,
53
- render,
54
- index,
55
- };
56
- });
20
+ const getAllComponents = schemaViewers.map((schemaViewerProps: any, index: number) =>
21
+ resolveSchemaViewer({ id, version, collection, filePath, schemaViewerProps, collectionSchemas, index })
22
+ );
57
23
 
58
24
  schemas = await Promise.all(getAllComponents);
59
25
  } catch (error) {
@@ -71,8 +37,10 @@ try {
71
37
  <div>
72
38
  {schema.exists && (
73
39
  <div
74
- id={`${schema.id}-${schema.file}-SchemaViewer-client`}
40
+ id={`${schema.id}-${schema.index}-SchemaViewer-client`}
75
41
  class="not-prose my-4"
42
+ data-schema-viewer-client-id={schema.id}
43
+ data-schema-viewer-index={schema.index}
76
44
  data-expand={schema.expand ? 'true' : 'false'}
77
45
  data-search={schema.search !== false ? 'true' : 'false'}
78
46
  >
@@ -105,7 +73,11 @@ try {
105
73
  <Admonition type="warning">
106
74
  <div>
107
75
  <span class="block font-bold">{`<SchemaViewer/>`} failed to load</span>
108
- <span class="block">Tried to load schema from {schema.schemaPath}, but no schema can be found</span>
76
+ <span class="block">
77
+ {schema.parseError
78
+ ? `Tried to parse schema from ${schema.schemaPath}, but it failed: ${schema.parseError}`
79
+ : `Tried to load schema from ${schema.schemaPath || schema.file || 'the message schema collection'}, but no schema can be found`}
80
+ </span>
109
81
  </div>
110
82
  </Admonition>
111
83
  )}
@@ -120,9 +92,9 @@ try {
120
92
  // and then we can move the SchemaViewerClient to that container?
121
93
 
122
94
  function moveSchemaViewerToPortal(schema) {
123
- const portalId = `${schema.id}-${schema.file}-SchemaViewer-portal`;
124
- const schemaViewerContainer = document.getElementById(portalId);
125
- const schemaViewerClient = document.getElementById(`${schema.id}-${schema.file}-SchemaViewer-client`);
95
+ const schemaViewerContainers = document.querySelectorAll(`[data-schema-viewer-portal-id="${schema.id}"]`);
96
+ const schemaViewerContainer = schemaViewerContainers[schema.index];
97
+ const schemaViewerClient = document.getElementById(`${schema.id}-${schema.index}-SchemaViewer-client`);
126
98
 
127
99
  if (schemaViewerContainer && schemaViewerClient) {
128
100
  // Get attributes from the portal
@@ -0,0 +1,53 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { resolveSchemaViewer } from './schema-viewer-utils';
3
+
4
+ const currentMessageSchema = {
5
+ id: 'schema:events:OrderPlaced:1.0.0:git://contracts/events/OrderPlaced.schema.json',
6
+ data: {
7
+ ref: 'git://contracts/events/OrderPlaced.schema.json',
8
+ format: 'jsonschema',
9
+ content: '{"type":"object","title":"OrderPlaced"}',
10
+ default: true,
11
+ message: {
12
+ collection: 'events',
13
+ id: 'OrderPlaced',
14
+ version: '1.0.0',
15
+ },
16
+ },
17
+ };
18
+
19
+ describe('resolveSchemaViewer', () => {
20
+ it('uses the current message default schema when no file is set', async () => {
21
+ const schema = await resolveSchemaViewer({
22
+ id: 'OrderPlaced',
23
+ version: '1.0.0',
24
+ collection: 'events',
25
+ filePath: 'events/OrderPlaced/index.mdx',
26
+ schemaViewerProps: {},
27
+ collectionSchemas: [currentMessageSchema],
28
+ index: 0,
29
+ });
30
+
31
+ expect(schema.exists).toBe(true);
32
+ expect(schema.schema.title).toBe('OrderPlaced');
33
+ expect(schema.schemaPath).toBe('git://contracts/events/OrderPlaced.schema.json');
34
+ });
35
+
36
+ it('does not use the schema collection when a file is set and missing', async () => {
37
+ const schema = await resolveSchemaViewer({
38
+ id: 'OrderPlaced',
39
+ version: '1.0.0',
40
+ collection: 'events',
41
+ filePath: 'events/OrderPlaced/index.mdx',
42
+ schemaViewerProps: {
43
+ file: 'missing-schema.json',
44
+ },
45
+ collectionSchemas: [currentMessageSchema],
46
+ index: 0,
47
+ });
48
+
49
+ expect(schema.exists).toBe(false);
50
+ expect(schema.schema).toBeUndefined();
51
+ expect(schema.schemaKey).toBe('missing-schema.json');
52
+ });
53
+ });
@@ -0,0 +1,133 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { existsSync } from 'node:fs';
3
+ import { load as loadYaml } from 'js-yaml';
4
+ import { getAbsoluteFilePathForAstroFile, isAvroSchema } from '../../../utils/files';
5
+
6
+ type SchemaViewerProps = {
7
+ id?: string;
8
+ file?: string;
9
+ [key: string]: any;
10
+ };
11
+
12
+ type CollectionSchema = {
13
+ id: string;
14
+ data: {
15
+ content?: string;
16
+ file?: string;
17
+ filePath?: string;
18
+ ref?: string;
19
+ format?: string;
20
+ default?: boolean;
21
+ source?: {
22
+ path?: string;
23
+ branch?: string;
24
+ };
25
+ message?: {
26
+ collection: string;
27
+ id: string;
28
+ version: string;
29
+ };
30
+ };
31
+ };
32
+
33
+ type ResolveSchemaViewerOptions = {
34
+ id: string;
35
+ version?: string;
36
+ collection?: string;
37
+ filePath: string;
38
+ schemaViewerProps: SchemaViewerProps;
39
+ collectionSchemas: CollectionSchema[];
40
+ index: number;
41
+ };
42
+
43
+ export const getSchemaViewerKey = (schemaViewerProps: SchemaViewerProps) => schemaViewerProps.file || 'default';
44
+
45
+ const isYamlSchema = (schemaPath?: string, format?: string) =>
46
+ schemaPath?.endsWith('.yml') || schemaPath?.endsWith('.yaml') || format === 'yaml';
47
+
48
+ const isAvroSchemaReference = (schemaPath?: string, format?: string) =>
49
+ format === 'avro' || (schemaPath ? isAvroSchema(schemaPath) : false);
50
+
51
+ const parseSchemaContent = (content: string, schemaPath?: string, format?: string) => {
52
+ if (isYamlSchema(schemaPath, format)) return loadYaml(content);
53
+ return JSON.parse(content);
54
+ };
55
+
56
+ const shouldRenderSchema = (schema: any, isAvro: boolean) => {
57
+ if (isAvro) return true;
58
+ return schema?.['x-eventcatalog-render-schema-viewer'] !== undefined ? schema['x-eventcatalog-render-schema-viewer'] : true;
59
+ };
60
+
61
+ const getCollectionSchemaForViewer = ({
62
+ collectionSchemas,
63
+ collection,
64
+ id,
65
+ version,
66
+ }: Pick<ResolveSchemaViewerOptions, 'collectionSchemas' | 'collection' | 'id' | 'version'>) => {
67
+ if (!collection || !version) return undefined;
68
+
69
+ const resourceSchemas = collectionSchemas.filter((schema) => {
70
+ const message = schema.data.message;
71
+ if (!message) return false;
72
+ return message.collection === collection && message.id === id && message.version === version;
73
+ });
74
+
75
+ return resourceSchemas.find((schema) => schema.data.default) || resourceSchemas[0];
76
+ };
77
+
78
+ const getCollectionSchemaPath = (schema?: CollectionSchema) =>
79
+ schema?.data.filePath || schema?.data.file || schema?.data.source?.path || schema?.data.ref;
80
+
81
+ export const resolveSchemaViewer = async ({
82
+ id,
83
+ version,
84
+ collection,
85
+ filePath,
86
+ schemaViewerProps,
87
+ collectionSchemas,
88
+ index,
89
+ }: ResolveSchemaViewerOptions) => {
90
+ const schemaKey = getSchemaViewerKey(schemaViewerProps);
91
+ const localSchemaPath = schemaViewerProps.file ? getAbsoluteFilePathForAstroFile(filePath, schemaViewerProps.file) : undefined;
92
+ const localSchemaExists = localSchemaPath ? existsSync(localSchemaPath) : false;
93
+
94
+ let schema;
95
+ let render = true;
96
+ let isAvro = false;
97
+ let schemaPath = localSchemaPath;
98
+ let parseError;
99
+
100
+ if (localSchemaExists && localSchemaPath) {
101
+ isAvro = isAvroSchema(localSchemaPath);
102
+ const content = await readFile(localSchemaPath, 'utf-8');
103
+ schema = parseSchemaContent(content, localSchemaPath);
104
+ render = shouldRenderSchema(schema, isAvro);
105
+ } else if (!schemaViewerProps.file) {
106
+ const collectionSchema = getCollectionSchemaForViewer({ collectionSchemas, collection, id, version });
107
+ const content = collectionSchema?.data.content;
108
+ schemaPath = getCollectionSchemaPath(collectionSchema);
109
+
110
+ if (content) {
111
+ try {
112
+ isAvro = isAvroSchemaReference(schemaPath, collectionSchema?.data.format);
113
+ schema = parseSchemaContent(content, schemaPath, collectionSchema?.data.format);
114
+ render = shouldRenderSchema(schema, isAvro);
115
+ } catch (error) {
116
+ parseError = error instanceof Error ? error.message : 'Unknown parsing error';
117
+ }
118
+ }
119
+ }
120
+
121
+ return {
122
+ id: schemaViewerProps.id || id,
123
+ exists: localSchemaExists || schema !== undefined,
124
+ schema,
125
+ schemaPath,
126
+ schemaKey,
127
+ isAvroSchema: isAvro,
128
+ parseError,
129
+ ...schemaViewerProps,
130
+ render,
131
+ index,
132
+ };
133
+ };