@eventcatalog/sdk 0.0.6 → 0.0.8
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/README.md +29 -5
- package/dist/commands.js +15 -6
- package/dist/commands.js.map +1 -1
- package/dist/commands.mjs +15 -6
- package/dist/commands.mjs.map +1 -1
- package/dist/domains.d.mts +133 -0
- package/dist/domains.d.ts +133 -0
- package/dist/domains.js +195 -0
- package/dist/domains.js.map +1 -0
- package/dist/domains.mjs +155 -0
- package/dist/domains.mjs.map +1 -0
- package/dist/events.js +15 -6
- package/dist/events.js.map +1 -1
- package/dist/events.mjs +15 -6
- package/dist/events.mjs.map +1 -1
- package/dist/index.d.mts +53 -1
- package/dist/index.d.ts +53 -1
- package/dist/index.js +95 -27
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +95 -27
- package/dist/index.mjs.map +1 -1
- package/dist/services.js +15 -6
- package/dist/services.js.map +1 -1
- package/dist/services.mjs +15 -6
- package/dist/services.mjs.map +1 -1
- package/dist/types.d.d.mts +5 -1
- package/dist/types.d.d.ts +5 -1
- package/dist/types.d.js.map +1 -1
- package/package.json +1 -1
package/dist/events.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/events.ts","../src/internal/resources.ts","../src/internal/utils.ts"],"sourcesContent":["import matter from 'gray-matter';\nimport fs from 'node:fs/promises';\nimport { join } from 'node:path';\nimport { dirname } from 'node:path';\nimport { copyDir, findFileById, getFiles, searchFilesForId, versionExists } from './internal/utils';\nimport type { Event } from './types';\nimport { addFileToResource, getResource, rmResourceById, versionResource, writeResource } from './internal/resources';\n\n/**\n * Returns an event from EventCatalog.\n *\n * You can optionally specify a version to get a specific version of the event\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { getEvent } = utils('/path/to/eventcatalog');\n *\n * // Gets the latest version of the event\n * const event = await getEvent('InventoryAdjusted');\n *\n * // Gets a version of the event\n * const event = await getEvent('InventoryAdjusted', '0.0.1');\n * ```\n */\nexport const getEvent =\n (directory: string) =>\n async (id: string, version?: string): Promise<Event> =>\n getResource(directory, id, version, { type: 'event' }) as Promise<Event>;\n\n/**\n * Write an event to EventCatalog.\n *\n * You can optionally overide the path of the event.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { writeEvent } = utils('/path/to/eventcatalog');\n *\n * // Write an event to the catalog\n * // Event would be written to events/InventoryAdjusted\n * await writeEvent({\n * id: 'InventoryAdjusted',\n * name: 'Inventory Adjusted',\n * version: '0.0.1',\n * summary: 'This is a summary',\n * markdown: '# Hello world',\n * });\n *\n * // Write an event to the catalog but override the path\n * // Event would be written to events/Inventory/InventoryAdjusted\n * await writeEvent({\n * id: 'InventoryAdjusted',\n * name: 'Inventory Adjusted',\n * version: '0.0.1',\n * summary: 'This is a summary',\n * markdown: '# Hello world',\n * }, { path: \"/Inventory/InventoryAdjusted\"});\n * ```\n */\nexport const writeEvent =\n (directory: string) =>\n async (event: Event, options: { path: string } = { path: '' }) =>\n writeResource(directory, { ...event }, { ...options, type: 'event' });\n\n/**\n * Delete an event at it's given path.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { rmEvent } = utils('/path/to/eventcatalog');\n *\n * // removes an event at the given path (events dir is appended to the given path)\n * // Removes the event at events/InventoryAdjusted\n * await rmEvent('/InventoryAdjusted');\n * ```\n */\nexport const rmEvent = (directory: string) => async (path: string) => {\n await fs.rm(join(directory, path), { recursive: true });\n};\n\n/**\n * Delete an event by it's id.\n *\n * Optionally specify a version to delete a specific version of the event.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { rmEventById } = utils('/path/to/eventcatalog');\n *\n * // deletes the latest InventoryAdjusted event\n * await rmEventById('InventoryAdjusted');\n *\n * // deletes a specific version of the InventoryAdjusted event\n * await rmEventById('InventoryAdjusted', '0.0.1');\n * ```\n */\nexport const rmEventById = (directory: string) => async (id: string, version?: string) =>\n rmResourceById(directory, id, version, { type: 'event' });\n\n/**\n * Version an event by it's id.\n *\n * Takes the latest event and moves it to a versioned directory.\n * All files with this event are also versioned (e.g /events/InventoryAdjusted/schema.json)\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { versionEvent } = utils('/path/to/eventcatalog');\n *\n * // moves the latest InventoryAdjusted event to a versioned directory\n * // the version within that event is used as the version number.\n * await versionEvent('InventoryAdjusted');\n *\n * ```\n */\nexport const versionEvent = (directory: string) => async (id: string) => versionResource(directory, id);\n\n/**\n * Add a file to an event by it's id.\n *\n * Optionally specify a version to add a file to a specific version of the event.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { addFileToEvent } = utils('/path/to/eventcatalog');\n *\n * // adds a file to the latest InventoryAdjusted event\n * await addFileToEvent('InventoryAdjusted', { content: 'Hello world', fileName: 'hello.txt' });\n *\n * // adds a file to a specific version of the InventoryAdjusted event\n * await addFileToEvent('InventoryAdjusted', { content: 'Hello world', fileName: 'hello.txt' }, '0.0.1');\n *\n * ```\n */\nexport const addFileToEvent =\n (directory: string) => async (id: string, file: { content: string; fileName: string }, version?: string) =>\n addFileToResource(directory, id, file, version);\n\n/**\n * Add a schema to an event by it's id.\n *\n * Optionally specify a version to add a schema to a specific version of the event.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { addSchemaToEvent } = utils('/path/to/eventcatalog');\n *\n * // JSON schema example\n * const schema = {\n * \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n * \"type\": \"object\",\n * \"properties\": {\n * \"name\": {\n * \"type\": \"string\"\n * },\n * \"age\": {\n * \"type\": \"number\"\n * }\n * },\n * \"required\": [\"name\", \"age\"]\n * };\n *\n * // adds a schema to the latest InventoryAdjusted event\n * await addSchemaToEvent('InventoryAdjusted', { schema, fileName: 'schema.json' });\n *\n * // adds a file to a specific version of the InventoryAdjusted event\n * await addSchemaToEvent('InventoryAdjusted', { schema, fileName: 'schema.json' }, '0.0.1');\n *\n * ```\n */\nexport const addSchemaToEvent =\n (directory: string) => async (id: string, schema: { schema: string; fileName: string }, version?: string) => {\n await addFileToEvent(directory)(id, { content: schema.schema, fileName: schema.fileName }, version);\n };\n","import { dirname, join } from 'path';\nimport { copyDir, findFileById, getFiles, searchFilesForId, versionExists } from './utils';\nimport matter from 'gray-matter';\nimport fs from 'node:fs/promises';\nimport { Message, Service } from '../types';\n\ntype Resource = Service | Message;\n\nexport const versionResource = async (catalogDir: string, id: string) => {\n // Find all the events in the directory\n const files = await getFiles(`${catalogDir}/**/index.md`);\n const matchedFiles = await searchFilesForId(files, id);\n\n if (matchedFiles.length === 0) {\n throw new Error(`No event found with id: ${id}`);\n }\n\n // Event that is in the route of the project\n const file = matchedFiles[0];\n const sourceDirectory = dirname(file);\n const { data: { version = '0.0.1' } = {} } = matter.read(file);\n const targetDirectory = join(sourceDirectory, 'versioned', version);\n\n await fs.mkdir(targetDirectory, { recursive: true });\n\n // Copy the event to the versioned directory\n await copyDir(catalogDir, sourceDirectory, targetDirectory, (src) => {\n return !src.includes('versioned');\n });\n\n // Remove all the files in the root of the resource as they have now been versioned\n await fs.readdir(sourceDirectory).then(async (resourceFiles) => {\n await Promise.all(\n resourceFiles.map(async (file) => {\n if (file !== 'versioned') {\n await fs.rm(join(sourceDirectory, file), { recursive: true });\n }\n })\n );\n });\n};\n\nexport const writeResource = async (\n catalogDir: string,\n resource: Resource,\n options: { path: string; type: string } = { path: '', type: '' }\n) => {\n // Get the path\n const path = options.path || `/${resource.id}`;\n const exists = await versionExists(catalogDir, resource.id, resource.version);\n\n if (exists) {\n throw new Error(`Failed to write ${options.type} as the version ${resource.version} already exists`);\n }\n\n const { markdown, ...frontmatter } = resource;\n const document = matter.stringify(markdown.trim(), frontmatter);\n await fs.mkdir(join(catalogDir, path), { recursive: true });\n await fs.writeFile(join(catalogDir, path, 'index.md'), document);\n};\n\nexport const getResource = async (\n catalogDir: string,\n id: string,\n version?: string,\n options?: { type: string }\n): Promise<Resource> => {\n const file = await findFileById(catalogDir, id, version);\n\n if (!file)\n throw new Error(\n `No ${options?.type || 'resource'} found for the given id: ${id}` + (version ? ` and version ${version}` : '')\n );\n\n const { data, content } = matter.read(file);\n\n return {\n ...data,\n markdown: content.trim(),\n } as Resource;\n};\n\nexport const rmResourceById = async (catalogDir: string, id: string, version?: string, options?: { type: string }) => {\n const files = await getFiles(`${catalogDir}/**/index.md`);\n const matchedFiles = await searchFilesForId(files, id, version);\n\n if (matchedFiles.length === 0) {\n throw new Error(`No ${options?.type || 'resource'} found with id: ${id}`);\n }\n\n await Promise.all(matchedFiles.map((file) => fs.rm(file)));\n};\n\nexport const addFileToResource = async (\n catalogDir: string,\n id: string,\n file: { content: string; fileName: string },\n version?: string\n) => {\n const pathToResource = await findFileById(catalogDir, id, version);\n\n if (!pathToResource) throw new Error('Cannot find directory to write file to');\n\n await fs.writeFile(join(dirname(pathToResource), file.fileName), file.content);\n};\n","import { glob } from 'glob';\nimport fs from 'node:fs/promises';\nimport { copy, CopyFilterAsync, CopyFilterSync } from 'fs-extra';\nimport { join } from 'node:path';\n\n/**\n * Returns true if a given version of a resource id exists in the catalog\n */\nexport const versionExists = async (catalogDir: string, id: string, version: string) => {\n const files = await getFiles(`${catalogDir}/**/index.md`);\n const matchedFiles = (await searchFilesForId(files, id, version)) || [];\n return matchedFiles.length > 0;\n};\n\nexport const findFileById = async (catalogDir: string, id: string, version?: string): Promise<string | undefined> => {\n const files = await getFiles(`${catalogDir}/**/index.md`);\n const matchedFiles = (await searchFilesForId(files, id)) || [];\n\n // Return the latest one\n if (!version) {\n return matchedFiles.find((path) => !path.includes('versioned'));\n }\n\n // Find the versioned event\n return matchedFiles.find((path) => path.includes(`versioned/${version}`));\n};\n\nexport const getFiles = async (pattern: string) => {\n try {\n const files = await glob(pattern, { ignore: 'node_modules/**' });\n return files;\n } catch (error) {\n throw new Error(`Error finding files: ${error}`);\n }\n};\n\nexport const searchFilesForId = async (files: string[], id: string, version?: string) => {\n const idRegex = new RegExp(`^id:\\\\s*['\"]?${id}['\"]?\\\\s*$`, 'm');\n const versionRegex = new RegExp(`^version:\\\\s*['\"]?${version}['\"]?\\\\s*$`, 'm');\n\n const matches = await Promise.all(\n files.map(async (file) => {\n const content = await fs.readFile(file, 'utf-8');\n const hasIdMatch = content.match(idRegex);\n\n // Check version if provided\n if (version && !content.match(versionRegex)) {\n return undefined;\n }\n\n if (hasIdMatch) {\n return file;\n }\n })\n );\n\n return matches.filter(Boolean).filter((file) => file !== undefined);\n};\n\n/**\n * Function to copy a directory from source to target, uses a tmp directory\n * @param catalogDir\n * @param source\n * @param target\n * @param filter\n */\nexport const copyDir = async (catalogDir: string, source: string, target: string, filter?: CopyFilterAsync | CopyFilterSync) => {\n const tmpDirectory = join(catalogDir, 'tmp');\n await fs.mkdir(tmpDirectory, { recursive: true });\n\n // Copy everything over\n await copy(source, tmpDirectory, {\n overwrite: true,\n filter,\n });\n\n await copy(tmpDirectory, target, {\n overwrite: true,\n filter,\n });\n\n // Remove the tmp directory\n await fs.rm(tmpDirectory, { recursive: true });\n};\n"],"mappings":";AACA,OAAOA,SAAQ;AACf,SAAS,QAAAC,aAAY;;;ACFrB,SAAS,SAAS,QAAAC,aAAY;;;ACA9B,SAAS,YAAY;AACrB,OAAO,QAAQ;AACf,SAAS,YAA6C;AACtD,SAAS,YAAY;AAKd,IAAM,gBAAgB,OAAO,YAAoB,IAAY,YAAoB;AACtF,QAAM,QAAQ,MAAM,SAAS,GAAG,UAAU,cAAc;AACxD,QAAM,eAAgB,MAAM,iBAAiB,OAAO,IAAI,OAAO,KAAM,CAAC;AACtE,SAAO,aAAa,SAAS;AAC/B;AAEO,IAAM,eAAe,OAAO,YAAoB,IAAY,YAAkD;AACnH,QAAM,QAAQ,MAAM,SAAS,GAAG,UAAU,cAAc;AACxD,QAAM,eAAgB,MAAM,iBAAiB,OAAO,EAAE,KAAM,CAAC;AAG7D,MAAI,CAAC,SAAS;AACZ,WAAO,aAA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,YACjB,YAAY,WAAW,IAAI,SAAS,EAAE,MAAM,QAAQ,CAAC;AAkClD,IAAM,aACX,CAAC,cACD,OAAO,OAAc,UAA4B,EAAE,MAAM,GAAG,MAC1D,cAAc,WAAW,EAAE,GAAG,MAAM,GAAG,EAAE,GAAG,SAAS,MAAM,QAAQ,CAAC;AAgBjE,IAAM,UAAU,CAAC,cAAsB,OAAO,SAAiB;AACpE,QAAME,IAAG,GAAGC,MAAK,WAAW,IAAI,GAAG,EAAE,WAAW,KAAK,CAAC;AACxD;AAoBO,IAAM,cAAc,CAAC,cAAsB,OAAO,IAAY,YACnE,eAAe,WAAW,IAAI,SAAS,EAAE,MAAM,QAAQ,CAAC;AAoBnD,IAAM,eAAe,CAAC,cAAsB,OAAO,OAAe,gBAAgB,WAAW,EAAE;AAqB/F,IAAM,iBACX,CAAC,cAAsB,OAAO,IAAY,MAA6C,YACrF,kBAAkB,WAAW,IAAI,MAAM,OAAO;AAoC3C,IAAM,mBACX,CAAC,cAAsB,OAAO,IAAY,QAA8C,YAAqB;AAC3G,QAAM,eAAe,SAAS,EAAE,IAAI,EAAE,SAAS,OAAO,QAAQ,UAAU,OAAO,SAAS,GAAG,OAAO;AACpG;","names":["fs","join","join","fs","join","file","fs","join"]}
|
|
1
|
+
{"version":3,"sources":["../src/events.ts","../src/internal/resources.ts","../src/internal/utils.ts"],"sourcesContent":["import matter from 'gray-matter';\nimport fs from 'node:fs/promises';\nimport { join } from 'node:path';\nimport { dirname } from 'node:path';\nimport { copyDir, findFileById, getFiles, searchFilesForId, versionExists } from './internal/utils';\nimport type { Event } from './types';\nimport { addFileToResource, getResource, rmResourceById, versionResource, writeResource } from './internal/resources';\n\n/**\n * Returns an event from EventCatalog.\n *\n * You can optionally specify a version to get a specific version of the event\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { getEvent } = utils('/path/to/eventcatalog');\n *\n * // Gets the latest version of the event\n * const event = await getEvent('InventoryAdjusted');\n *\n * // Gets a version of the event\n * const event = await getEvent('InventoryAdjusted', '0.0.1');\n * ```\n */\nexport const getEvent =\n (directory: string) =>\n async (id: string, version?: string): Promise<Event> =>\n getResource(directory, id, version, { type: 'event' }) as Promise<Event>;\n\n/**\n * Write an event to EventCatalog.\n *\n * You can optionally overide the path of the event.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { writeEvent } = utils('/path/to/eventcatalog');\n *\n * // Write an event to the catalog\n * // Event would be written to events/InventoryAdjusted\n * await writeEvent({\n * id: 'InventoryAdjusted',\n * name: 'Inventory Adjusted',\n * version: '0.0.1',\n * summary: 'This is a summary',\n * markdown: '# Hello world',\n * });\n *\n * // Write an event to the catalog but override the path\n * // Event would be written to events/Inventory/InventoryAdjusted\n * await writeEvent({\n * id: 'InventoryAdjusted',\n * name: 'Inventory Adjusted',\n * version: '0.0.1',\n * summary: 'This is a summary',\n * markdown: '# Hello world',\n * }, { path: \"/Inventory/InventoryAdjusted\"});\n * ```\n */\nexport const writeEvent =\n (directory: string) =>\n async (event: Event, options: { path: string } = { path: '' }) =>\n writeResource(directory, { ...event }, { ...options, type: 'event' });\n\n/**\n * Delete an event at it's given path.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { rmEvent } = utils('/path/to/eventcatalog');\n *\n * // removes an event at the given path (events dir is appended to the given path)\n * // Removes the event at events/InventoryAdjusted\n * await rmEvent('/InventoryAdjusted');\n * ```\n */\nexport const rmEvent = (directory: string) => async (path: string) => {\n await fs.rm(join(directory, path), { recursive: true });\n};\n\n/**\n * Delete an event by it's id.\n *\n * Optionally specify a version to delete a specific version of the event.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { rmEventById } = utils('/path/to/eventcatalog');\n *\n * // deletes the latest InventoryAdjusted event\n * await rmEventById('InventoryAdjusted');\n *\n * // deletes a specific version of the InventoryAdjusted event\n * await rmEventById('InventoryAdjusted', '0.0.1');\n * ```\n */\nexport const rmEventById = (directory: string) => async (id: string, version?: string) =>\n rmResourceById(directory, id, version, { type: 'event' });\n\n/**\n * Version an event by it's id.\n *\n * Takes the latest event and moves it to a versioned directory.\n * All files with this event are also versioned (e.g /events/InventoryAdjusted/schema.json)\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { versionEvent } = utils('/path/to/eventcatalog');\n *\n * // moves the latest InventoryAdjusted event to a versioned directory\n * // the version within that event is used as the version number.\n * await versionEvent('InventoryAdjusted');\n *\n * ```\n */\nexport const versionEvent = (directory: string) => async (id: string) => versionResource(directory, id);\n\n/**\n * Add a file to an event by it's id.\n *\n * Optionally specify a version to add a file to a specific version of the event.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { addFileToEvent } = utils('/path/to/eventcatalog');\n *\n * // adds a file to the latest InventoryAdjusted event\n * await addFileToEvent('InventoryAdjusted', { content: 'Hello world', fileName: 'hello.txt' });\n *\n * // adds a file to a specific version of the InventoryAdjusted event\n * await addFileToEvent('InventoryAdjusted', { content: 'Hello world', fileName: 'hello.txt' }, '0.0.1');\n *\n * ```\n */\nexport const addFileToEvent =\n (directory: string) => async (id: string, file: { content: string; fileName: string }, version?: string) =>\n addFileToResource(directory, id, file, version);\n\n/**\n * Add a schema to an event by it's id.\n *\n * Optionally specify a version to add a schema to a specific version of the event.\n *\n * @example\n * ```ts\n * import utils from '@eventcatalog/utils';\n *\n * const { addSchemaToEvent } = utils('/path/to/eventcatalog');\n *\n * // JSON schema example\n * const schema = {\n * \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n * \"type\": \"object\",\n * \"properties\": {\n * \"name\": {\n * \"type\": \"string\"\n * },\n * \"age\": {\n * \"type\": \"number\"\n * }\n * },\n * \"required\": [\"name\", \"age\"]\n * };\n *\n * // adds a schema to the latest InventoryAdjusted event\n * await addSchemaToEvent('InventoryAdjusted', { schema, fileName: 'schema.json' });\n *\n * // adds a file to a specific version of the InventoryAdjusted event\n * await addSchemaToEvent('InventoryAdjusted', { schema, fileName: 'schema.json' }, '0.0.1');\n *\n * ```\n */\nexport const addSchemaToEvent =\n (directory: string) => async (id: string, schema: { schema: string; fileName: string }, version?: string) => {\n await addFileToEvent(directory)(id, { content: schema.schema, fileName: schema.fileName }, version);\n };\n","import { dirname, join } from 'path';\nimport { copyDir, findFileById, getFiles, searchFilesForId, versionExists } from './utils';\nimport matter from 'gray-matter';\nimport fs from 'node:fs/promises';\nimport { Message, Service } from '../types';\n\ntype Resource = Service | Message;\n\nexport const versionResource = async (catalogDir: string, id: string) => {\n // Find all the events in the directory\n const files = await getFiles(`${catalogDir}/**/index.md`);\n const matchedFiles = await searchFilesForId(files, id);\n\n if (matchedFiles.length === 0) {\n throw new Error(`No event found with id: ${id}`);\n }\n\n // Event that is in the route of the project\n const file = matchedFiles[0];\n const sourceDirectory = dirname(file);\n const { data: { version = '0.0.1' } = {} } = matter.read(file);\n const targetDirectory = join(sourceDirectory, 'versioned', version);\n\n await fs.mkdir(targetDirectory, { recursive: true });\n\n // Copy the event to the versioned directory\n await copyDir(catalogDir, sourceDirectory, targetDirectory, (src) => {\n return !src.includes('versioned');\n });\n\n // Remove all the files in the root of the resource as they have now been versioned\n await fs.readdir(sourceDirectory).then(async (resourceFiles) => {\n await Promise.all(\n resourceFiles.map(async (file) => {\n if (file !== 'versioned') {\n await fs.rm(join(sourceDirectory, file), { recursive: true });\n }\n })\n );\n });\n};\n\nexport const writeResource = async (\n catalogDir: string,\n resource: Resource,\n options: { path: string; type: string } = { path: '', type: '' }\n) => {\n // Get the path\n const path = options.path || `/${resource.id}`;\n const exists = await versionExists(catalogDir, resource.id, resource.version);\n\n if (exists) {\n throw new Error(`Failed to write ${options.type} as the version ${resource.version} already exists`);\n }\n\n const { markdown, ...frontmatter } = resource;\n const document = matter.stringify(markdown.trim(), frontmatter);\n await fs.mkdir(join(catalogDir, path), { recursive: true });\n await fs.writeFile(join(catalogDir, path, 'index.md'), document);\n};\n\nexport const getResource = async (\n catalogDir: string,\n id: string,\n version?: string,\n options?: { type: string }\n): Promise<Resource> => {\n const file = await findFileById(catalogDir, id, version);\n\n if (!file)\n throw new Error(\n `No ${options?.type || 'resource'} found for the given id: ${id}` + (version ? ` and version ${version}` : '')\n );\n\n const { data, content } = matter.read(file);\n\n return {\n ...data,\n markdown: content.trim(),\n } as Resource;\n};\n\nexport const rmResourceById = async (catalogDir: string, id: string, version?: string, options?: { type: string }) => {\n const files = await getFiles(`${catalogDir}/**/index.md`);\n const matchedFiles = await searchFilesForId(files, id, version);\n\n if (matchedFiles.length === 0) {\n throw new Error(`No ${options?.type || 'resource'} found with id: ${id}`);\n }\n\n await Promise.all(matchedFiles.map((file) => fs.rm(file)));\n};\n\nexport const addFileToResource = async (\n catalogDir: string,\n id: string,\n file: { content: string; fileName: string },\n version?: string\n) => {\n const pathToResource = await findFileById(catalogDir, id, version);\n\n if (!pathToResource) throw new Error('Cannot find directory to write file to');\n\n await fs.writeFile(join(dirname(pathToResource), file.fileName), file.content);\n};\n","import { glob } from 'glob';\nimport fs from 'node:fs/promises';\nimport { copy, CopyFilterAsync, CopyFilterSync } from 'fs-extra';\nimport { join } from 'node:path';\nimport matter from 'gray-matter';\n\n/**\n * Returns true if a given version of a resource id exists in the catalog\n */\nexport const versionExists = async (catalogDir: string, id: string, version: string) => {\n const files = await getFiles(`${catalogDir}/**/index.md`);\n const matchedFiles = (await searchFilesForId(files, id, version)) || [];\n return matchedFiles.length > 0;\n};\n\nexport const findFileById = async (catalogDir: string, id: string, version?: string): Promise<string | undefined> => {\n const files = await getFiles(`${catalogDir}/**/index.md`);\n const matchedFiles = (await searchFilesForId(files, id)) || [];\n const latestVersion = matchedFiles.find((path) => !path.includes('versioned'));\n\n // If no version is provided, return the latest version\n if (!version) {\n return latestVersion;\n }\n\n // Check if the version exists\n const match = matchedFiles.find((path) => path.includes(`versioned/${version}`));\n\n // Version is given but can't be found in the versioned directory, check if it's the latest version\n if (!match && latestVersion) {\n const { data } = matter.read(latestVersion);\n if (data.version === version) {\n return latestVersion;\n }\n }\n\n return match;\n};\n\nexport const getFiles = async (pattern: string) => {\n try {\n const files = await glob(pattern, { ignore: 'node_modules/**' });\n return files;\n } catch (error) {\n throw new Error(`Error finding files: ${error}`);\n }\n};\n\nexport const searchFilesForId = async (files: string[], id: string, version?: string) => {\n const idRegex = new RegExp(`^id:\\\\s*['\"]?${id}['\"]?\\\\s*$`, 'm');\n const versionRegex = new RegExp(`^version:\\\\s*['\"]?${version}['\"]?\\\\s*$`, 'm');\n\n const matches = await Promise.all(\n files.map(async (file) => {\n const content = await fs.readFile(file, 'utf-8');\n const hasIdMatch = content.match(idRegex);\n\n // Check version if provided\n if (version && !content.match(versionRegex)) {\n return undefined;\n }\n\n if (hasIdMatch) {\n return file;\n }\n })\n );\n\n return matches.filter(Boolean).filter((file) => file !== undefined);\n};\n\n/**\n * Function to copy a directory from source to target, uses a tmp directory\n * @param catalogDir\n * @param source\n * @param target\n * @param filter\n */\nexport const copyDir = async (catalogDir: string, source: string, target: string, filter?: CopyFilterAsync | CopyFilterSync) => {\n const tmpDirectory = join(catalogDir, 'tmp');\n await fs.mkdir(tmpDirectory, { recursive: true });\n\n // Copy everything over\n await copy(source, tmpDirectory, {\n overwrite: true,\n filter,\n });\n\n await copy(tmpDirectory, target, {\n overwrite: true,\n filter,\n });\n\n // Remove the tmp directory\n await fs.rm(tmpDirectory, { recursive: true });\n};\n"],"mappings":";AACA,OAAOA,SAAQ;AACf,SAAS,QAAAC,aAAY;;;ACFrB,SAAS,SAAS,QAAAC,aAAY;;;ACA9B,SAAS,YAAY;AACrB,OAAO,QAAQ;AACf,SAAS,YAA6C;AACtD,SAAS,YAAY;AACrB,OAAO,YAAY;AAKZ,IAAM,gBAAgB,OAAO,YAAoB,IAAY,YAAoB;AACtF,QAAM,QAAQ,MAAM,SAAS,GAAG,UAAU,cAAc;AACxD,QAAM,eAAgB,MAAM,iBAAiB,OAAO,IAAI,OAAO,KAAM,CAAC;AACtE,SAAO,aAAa,SAAS;AAC/B;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service } from './types.d.mjs';
|
|
1
|
+
import { Event, Command, Service, Domain } from './types.d.mjs';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Init the SDK for EventCatalog
|
|
@@ -180,6 +180,58 @@ declare const _default: (path: string) => {
|
|
|
180
180
|
content: string;
|
|
181
181
|
fileName: string;
|
|
182
182
|
}, version?: string) => Promise<void>;
|
|
183
|
+
/**
|
|
184
|
+
* ================================
|
|
185
|
+
* Domains
|
|
186
|
+
* ================================
|
|
187
|
+
*/
|
|
188
|
+
/**
|
|
189
|
+
* Adds a domain to EventCatalog
|
|
190
|
+
*
|
|
191
|
+
* @param domain - The domain to write
|
|
192
|
+
* @param options - Optional options to write the event
|
|
193
|
+
*
|
|
194
|
+
*/
|
|
195
|
+
writeDomain: (domain: Domain, options?: {
|
|
196
|
+
path: string;
|
|
197
|
+
}) => Promise<void>;
|
|
198
|
+
/**
|
|
199
|
+
* Returns a domain from EventCatalog
|
|
200
|
+
* @param id - The id of the domain to retrieve
|
|
201
|
+
* @param version - Optional id of the version to get
|
|
202
|
+
* @returns
|
|
203
|
+
*/
|
|
204
|
+
getDomain: (id: string, version?: string) => Promise<Domain>;
|
|
205
|
+
/**
|
|
206
|
+
* Moves a given domain id to the version directory
|
|
207
|
+
* @param directory
|
|
208
|
+
*/
|
|
209
|
+
versionDomain: (id: string) => Promise<void>;
|
|
210
|
+
/**
|
|
211
|
+
* Remove a domain from EventCatalog (modeled on the standard POSIX rm utility)
|
|
212
|
+
*
|
|
213
|
+
* @param path - The path to your domain, e.g. `/Payment`
|
|
214
|
+
*
|
|
215
|
+
*/
|
|
216
|
+
rmDomain: (path: string) => Promise<void>;
|
|
217
|
+
/**
|
|
218
|
+
* Remove an service by an domain id
|
|
219
|
+
*
|
|
220
|
+
* @param id - The id of the domain you want to remove
|
|
221
|
+
*
|
|
222
|
+
*/
|
|
223
|
+
rmDomainById: (id: string, version?: string) => Promise<void>;
|
|
224
|
+
/**
|
|
225
|
+
* Adds a file to the given domain
|
|
226
|
+
* @param id - The id of the domain to add the file to
|
|
227
|
+
* @param file - File contents to add including the content and the file name
|
|
228
|
+
* @param version - Optional version of the domain to add the file to
|
|
229
|
+
* @returns
|
|
230
|
+
*/
|
|
231
|
+
addFileToDomain: (id: string, file: {
|
|
232
|
+
content: string;
|
|
233
|
+
fileName: string;
|
|
234
|
+
}, version?: string) => Promise<void>;
|
|
183
235
|
};
|
|
184
236
|
|
|
185
237
|
export { _default as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Event, Command, Service } from './types.d.js';
|
|
1
|
+
import { Event, Command, Service, Domain } from './types.d.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Init the SDK for EventCatalog
|
|
@@ -180,6 +180,58 @@ declare const _default: (path: string) => {
|
|
|
180
180
|
content: string;
|
|
181
181
|
fileName: string;
|
|
182
182
|
}, version?: string) => Promise<void>;
|
|
183
|
+
/**
|
|
184
|
+
* ================================
|
|
185
|
+
* Domains
|
|
186
|
+
* ================================
|
|
187
|
+
*/
|
|
188
|
+
/**
|
|
189
|
+
* Adds a domain to EventCatalog
|
|
190
|
+
*
|
|
191
|
+
* @param domain - The domain to write
|
|
192
|
+
* @param options - Optional options to write the event
|
|
193
|
+
*
|
|
194
|
+
*/
|
|
195
|
+
writeDomain: (domain: Domain, options?: {
|
|
196
|
+
path: string;
|
|
197
|
+
}) => Promise<void>;
|
|
198
|
+
/**
|
|
199
|
+
* Returns a domain from EventCatalog
|
|
200
|
+
* @param id - The id of the domain to retrieve
|
|
201
|
+
* @param version - Optional id of the version to get
|
|
202
|
+
* @returns
|
|
203
|
+
*/
|
|
204
|
+
getDomain: (id: string, version?: string) => Promise<Domain>;
|
|
205
|
+
/**
|
|
206
|
+
* Moves a given domain id to the version directory
|
|
207
|
+
* @param directory
|
|
208
|
+
*/
|
|
209
|
+
versionDomain: (id: string) => Promise<void>;
|
|
210
|
+
/**
|
|
211
|
+
* Remove a domain from EventCatalog (modeled on the standard POSIX rm utility)
|
|
212
|
+
*
|
|
213
|
+
* @param path - The path to your domain, e.g. `/Payment`
|
|
214
|
+
*
|
|
215
|
+
*/
|
|
216
|
+
rmDomain: (path: string) => Promise<void>;
|
|
217
|
+
/**
|
|
218
|
+
* Remove an service by an domain id
|
|
219
|
+
*
|
|
220
|
+
* @param id - The id of the domain you want to remove
|
|
221
|
+
*
|
|
222
|
+
*/
|
|
223
|
+
rmDomainById: (id: string, version?: string) => Promise<void>;
|
|
224
|
+
/**
|
|
225
|
+
* Adds a file to the given domain
|
|
226
|
+
* @param id - The id of the domain to add the file to
|
|
227
|
+
* @param file - File contents to add including the content and the file name
|
|
228
|
+
* @param version - Optional version of the domain to add the file to
|
|
229
|
+
* @returns
|
|
230
|
+
*/
|
|
231
|
+
addFileToDomain: (id: string, file: {
|
|
232
|
+
content: string;
|
|
233
|
+
fileName: string;
|
|
234
|
+
}, version?: string) => Promise<void>;
|
|
183
235
|
};
|
|
184
236
|
|
|
185
237
|
export { _default as default };
|
package/dist/index.js
CHANGED
|
@@ -33,7 +33,7 @@ __export(src_exports, {
|
|
|
33
33
|
default: () => src_default
|
|
34
34
|
});
|
|
35
35
|
module.exports = __toCommonJS(src_exports);
|
|
36
|
-
var
|
|
36
|
+
var import_node_path6 = require("path");
|
|
37
37
|
|
|
38
38
|
// src/events.ts
|
|
39
39
|
var import_promises3 = __toESM(require("fs/promises"));
|
|
@@ -47,6 +47,7 @@ var import_glob = require("glob");
|
|
|
47
47
|
var import_promises = __toESM(require("fs/promises"));
|
|
48
48
|
var import_fs_extra = require("fs-extra");
|
|
49
49
|
var import_node_path = require("path");
|
|
50
|
+
var import_gray_matter = __toESM(require("gray-matter"));
|
|
50
51
|
var versionExists = async (catalogDir, id, version) => {
|
|
51
52
|
const files = await getFiles(`${catalogDir}/**/index.md`);
|
|
52
53
|
const matchedFiles = await searchFilesForId(files, id, version) || [];
|
|
@@ -55,10 +56,18 @@ var versionExists = async (catalogDir, id, version) => {
|
|
|
55
56
|
var findFileById = async (catalogDir, id, version) => {
|
|
56
57
|
const files = await getFiles(`${catalogDir}/**/index.md`);
|
|
57
58
|
const matchedFiles = await searchFilesForId(files, id) || [];
|
|
59
|
+
const latestVersion = matchedFiles.find((path) => !path.includes("versioned"));
|
|
58
60
|
if (!version) {
|
|
59
|
-
return
|
|
61
|
+
return latestVersion;
|
|
62
|
+
}
|
|
63
|
+
const match = matchedFiles.find((path) => path.includes(`versioned/${version}`));
|
|
64
|
+
if (!match && latestVersion) {
|
|
65
|
+
const { data } = import_gray_matter.default.read(latestVersion);
|
|
66
|
+
if (data.version === version) {
|
|
67
|
+
return latestVersion;
|
|
68
|
+
}
|
|
60
69
|
}
|
|
61
|
-
return
|
|
70
|
+
return match;
|
|
62
71
|
};
|
|
63
72
|
var getFiles = async (pattern) => {
|
|
64
73
|
try {
|
|
@@ -100,7 +109,7 @@ var copyDir = async (catalogDir, source, target, filter) => {
|
|
|
100
109
|
};
|
|
101
110
|
|
|
102
111
|
// src/internal/resources.ts
|
|
103
|
-
var
|
|
112
|
+
var import_gray_matter2 = __toESM(require("gray-matter"));
|
|
104
113
|
var import_promises2 = __toESM(require("fs/promises"));
|
|
105
114
|
var versionResource = async (catalogDir, id) => {
|
|
106
115
|
const files = await getFiles(`${catalogDir}/**/index.md`);
|
|
@@ -110,7 +119,7 @@ var versionResource = async (catalogDir, id) => {
|
|
|
110
119
|
}
|
|
111
120
|
const file = matchedFiles[0];
|
|
112
121
|
const sourceDirectory = (0, import_path.dirname)(file);
|
|
113
|
-
const { data: { version = "0.0.1" } = {} } =
|
|
122
|
+
const { data: { version = "0.0.1" } = {} } = import_gray_matter2.default.read(file);
|
|
114
123
|
const targetDirectory = (0, import_path.join)(sourceDirectory, "versioned", version);
|
|
115
124
|
await import_promises2.default.mkdir(targetDirectory, { recursive: true });
|
|
116
125
|
await copyDir(catalogDir, sourceDirectory, targetDirectory, (src) => {
|
|
@@ -133,7 +142,7 @@ var writeResource = async (catalogDir, resource, options = { path: "", type: ""
|
|
|
133
142
|
throw new Error(`Failed to write ${options.type} as the version ${resource.version} already exists`);
|
|
134
143
|
}
|
|
135
144
|
const { markdown, ...frontmatter } = resource;
|
|
136
|
-
const document =
|
|
145
|
+
const document = import_gray_matter2.default.stringify(markdown.trim(), frontmatter);
|
|
137
146
|
await import_promises2.default.mkdir((0, import_path.join)(catalogDir, path), { recursive: true });
|
|
138
147
|
await import_promises2.default.writeFile((0, import_path.join)(catalogDir, path, "index.md"), document);
|
|
139
148
|
};
|
|
@@ -143,7 +152,7 @@ var getResource = async (catalogDir, id, version, options) => {
|
|
|
143
152
|
throw new Error(
|
|
144
153
|
`No ${options?.type || "resource"} found for the given id: ${id}` + (version ? ` and version ${version}` : "")
|
|
145
154
|
);
|
|
146
|
-
const { data, content } =
|
|
155
|
+
const { data, content } = import_gray_matter2.default.read(file);
|
|
147
156
|
return {
|
|
148
157
|
...data,
|
|
149
158
|
markdown: content.trim()
|
|
@@ -203,6 +212,18 @@ var rmService = (directory) => async (path) => {
|
|
|
203
212
|
var rmServiceById = (directory) => async (id, version) => rmResourceById(directory, id, version, { type: "service" });
|
|
204
213
|
var addFileToService = (directory) => async (id, file, version) => addFileToResource(directory, id, file, version);
|
|
205
214
|
|
|
215
|
+
// src/domains.ts
|
|
216
|
+
var import_promises6 = __toESM(require("fs/promises"));
|
|
217
|
+
var import_node_path5 = require("path");
|
|
218
|
+
var getDomain = (directory) => async (id, version) => getResource(directory, id, version, { type: "domain" });
|
|
219
|
+
var writeDomain = (directory) => async (domain, options = { path: "" }) => writeResource(directory, { ...domain }, { ...options, type: "domain" });
|
|
220
|
+
var versionDomain = (directory) => async (id) => versionResource(directory, id);
|
|
221
|
+
var rmDomain = (directory) => async (path) => {
|
|
222
|
+
await import_promises6.default.rm((0, import_node_path5.join)(directory, path), { recursive: true });
|
|
223
|
+
};
|
|
224
|
+
var rmDomainById = (directory) => async (id, version) => rmResourceById(directory, id, version, { type: "domain" });
|
|
225
|
+
var addFileToDomain = (directory) => async (id, file, version) => addFileToResource(directory, id, file, version);
|
|
226
|
+
|
|
206
227
|
// src/index.ts
|
|
207
228
|
var src_default = (path) => {
|
|
208
229
|
return {
|
|
@@ -212,7 +233,7 @@ var src_default = (path) => {
|
|
|
212
233
|
* @param version - Optional id of the version to get
|
|
213
234
|
* @returns
|
|
214
235
|
*/
|
|
215
|
-
getEvent: getEvent((0,
|
|
236
|
+
getEvent: getEvent((0, import_node_path6.join)(path, "events")),
|
|
216
237
|
/**
|
|
217
238
|
* Adds an event to EventCatalog
|
|
218
239
|
*
|
|
@@ -220,26 +241,26 @@ var src_default = (path) => {
|
|
|
220
241
|
* @param options - Optional options to write the event
|
|
221
242
|
*
|
|
222
243
|
*/
|
|
223
|
-
writeEvent: writeEvent((0,
|
|
244
|
+
writeEvent: writeEvent((0, import_node_path6.join)(path, "events")),
|
|
224
245
|
/**
|
|
225
246
|
* Remove an event to EventCatalog (modeled on the standard POSIX rm utility)
|
|
226
247
|
*
|
|
227
248
|
* @param path - The path to your event, e.g. `/Inventory/InventoryAdjusted`
|
|
228
249
|
*
|
|
229
250
|
*/
|
|
230
|
-
rmEvent: rmEvent((0,
|
|
251
|
+
rmEvent: rmEvent((0, import_node_path6.join)(path, "events")),
|
|
231
252
|
/**
|
|
232
253
|
* Remove an event by an Event id
|
|
233
254
|
*
|
|
234
255
|
* @param id - The id of the event you want to remove
|
|
235
256
|
*
|
|
236
257
|
*/
|
|
237
|
-
rmEventById: rmEventById((0,
|
|
258
|
+
rmEventById: rmEventById((0, import_node_path6.join)(path, "events")),
|
|
238
259
|
/**
|
|
239
260
|
* Moves a given event id to the version directory
|
|
240
261
|
* @param directory
|
|
241
262
|
*/
|
|
242
|
-
versionEvent: versionEvent((0,
|
|
263
|
+
versionEvent: versionEvent((0, import_node_path6.join)(path, "events")),
|
|
243
264
|
/**
|
|
244
265
|
* Adds a file to the given event
|
|
245
266
|
* @param id - The id of the event to add the file to
|
|
@@ -247,7 +268,7 @@ var src_default = (path) => {
|
|
|
247
268
|
* @param version - Optional version of the event to add the file to
|
|
248
269
|
* @returns
|
|
249
270
|
*/
|
|
250
|
-
addFileToEvent: addFileToEvent((0,
|
|
271
|
+
addFileToEvent: addFileToEvent((0, import_node_path6.join)(path, "events")),
|
|
251
272
|
/**
|
|
252
273
|
* Adds a schema to the given event
|
|
253
274
|
* @param id - The id of the event to add the schema to
|
|
@@ -255,7 +276,7 @@ var src_default = (path) => {
|
|
|
255
276
|
* @param version - Optional version of the event to add the schema to
|
|
256
277
|
* @returns
|
|
257
278
|
*/
|
|
258
|
-
addSchemaToEvent: addSchemaToEvent((0,
|
|
279
|
+
addSchemaToEvent: addSchemaToEvent((0, import_node_path6.join)(path, "events")),
|
|
259
280
|
/**
|
|
260
281
|
* ================================
|
|
261
282
|
* Commands
|
|
@@ -267,7 +288,7 @@ var src_default = (path) => {
|
|
|
267
288
|
* @param version - Optional id of the version to get
|
|
268
289
|
* @returns
|
|
269
290
|
*/
|
|
270
|
-
getCommand: getCommand((0,
|
|
291
|
+
getCommand: getCommand((0, import_node_path6.join)(path, "commands")),
|
|
271
292
|
/**
|
|
272
293
|
* Adds an command to EventCatalog
|
|
273
294
|
*
|
|
@@ -275,26 +296,26 @@ var src_default = (path) => {
|
|
|
275
296
|
* @param options - Optional options to write the command
|
|
276
297
|
*
|
|
277
298
|
*/
|
|
278
|
-
writeCommand: writeCommand((0,
|
|
299
|
+
writeCommand: writeCommand((0, import_node_path6.join)(path, "commands")),
|
|
279
300
|
/**
|
|
280
301
|
* Remove an command to EventCatalog (modeled on the standard POSIX rm utility)
|
|
281
302
|
*
|
|
282
303
|
* @param path - The path to your command, e.g. `/Inventory/InventoryAdjusted`
|
|
283
304
|
*
|
|
284
305
|
*/
|
|
285
|
-
rmCommand: rmCommand((0,
|
|
306
|
+
rmCommand: rmCommand((0, import_node_path6.join)(path, "commands")),
|
|
286
307
|
/**
|
|
287
308
|
* Remove an command by an Event id
|
|
288
309
|
*
|
|
289
310
|
* @param id - The id of the command you want to remove
|
|
290
311
|
*
|
|
291
312
|
*/
|
|
292
|
-
rmCommandById: rmCommandById((0,
|
|
313
|
+
rmCommandById: rmCommandById((0, import_node_path6.join)(path, "commands")),
|
|
293
314
|
/**
|
|
294
315
|
* Moves a given command id to the version directory
|
|
295
316
|
* @param directory
|
|
296
317
|
*/
|
|
297
|
-
versionCommand: versionCommand((0,
|
|
318
|
+
versionCommand: versionCommand((0, import_node_path6.join)(path, "commands")),
|
|
298
319
|
/**
|
|
299
320
|
* Adds a file to the given command
|
|
300
321
|
* @param id - The id of the command to add the file to
|
|
@@ -302,7 +323,7 @@ var src_default = (path) => {
|
|
|
302
323
|
* @param version - Optional version of the command to add the file to
|
|
303
324
|
* @returns
|
|
304
325
|
*/
|
|
305
|
-
addFileToCommand: addFileToCommand((0,
|
|
326
|
+
addFileToCommand: addFileToCommand((0, import_node_path6.join)(path, "commands")),
|
|
306
327
|
/**
|
|
307
328
|
* Adds a schema to the given command
|
|
308
329
|
* @param id - The id of the command to add the schema to
|
|
@@ -310,7 +331,7 @@ var src_default = (path) => {
|
|
|
310
331
|
* @param version - Optional version of the command to add the schema to
|
|
311
332
|
* @returns
|
|
312
333
|
*/
|
|
313
|
-
addSchemaToCommand: addSchemaToCommand((0,
|
|
334
|
+
addSchemaToCommand: addSchemaToCommand((0, import_node_path6.join)(path, "commands")),
|
|
314
335
|
/**
|
|
315
336
|
* ================================
|
|
316
337
|
* SERVICES
|
|
@@ -323,33 +344,33 @@ var src_default = (path) => {
|
|
|
323
344
|
* @param options - Optional options to write the event
|
|
324
345
|
*
|
|
325
346
|
*/
|
|
326
|
-
writeService: writeService((0,
|
|
347
|
+
writeService: writeService((0, import_node_path6.join)(path, "services")),
|
|
327
348
|
/**
|
|
328
349
|
* Returns a service from EventCatalog
|
|
329
350
|
* @param id - The id of the service to retrieve
|
|
330
351
|
* @param version - Optional id of the version to get
|
|
331
352
|
* @returns
|
|
332
353
|
*/
|
|
333
|
-
getService: getService((0,
|
|
354
|
+
getService: getService((0, import_node_path6.join)(path, "services")),
|
|
334
355
|
/**
|
|
335
356
|
* Moves a given service id to the version directory
|
|
336
357
|
* @param directory
|
|
337
358
|
*/
|
|
338
|
-
versionService: versionService((0,
|
|
359
|
+
versionService: versionService((0, import_node_path6.join)(path, "services")),
|
|
339
360
|
/**
|
|
340
361
|
* Remove a service from EventCatalog (modeled on the standard POSIX rm utility)
|
|
341
362
|
*
|
|
342
363
|
* @param path - The path to your service, e.g. `/InventoryService`
|
|
343
364
|
*
|
|
344
365
|
*/
|
|
345
|
-
rmService: rmService((0,
|
|
366
|
+
rmService: rmService((0, import_node_path6.join)(path, "services")),
|
|
346
367
|
/**
|
|
347
368
|
* Remove an service by an service id
|
|
348
369
|
*
|
|
349
370
|
* @param id - The id of the service you want to remove
|
|
350
371
|
*
|
|
351
372
|
*/
|
|
352
|
-
rmServiceById: rmServiceById((0,
|
|
373
|
+
rmServiceById: rmServiceById((0, import_node_path6.join)(path, "services")),
|
|
353
374
|
/**
|
|
354
375
|
* Adds a file to the given service
|
|
355
376
|
* @param id - The id of the service to add the file to
|
|
@@ -357,7 +378,54 @@ var src_default = (path) => {
|
|
|
357
378
|
* @param version - Optional version of the service to add the file to
|
|
358
379
|
* @returns
|
|
359
380
|
*/
|
|
360
|
-
addFileToService: addFileToService((0,
|
|
381
|
+
addFileToService: addFileToService((0, import_node_path6.join)(path, "services")),
|
|
382
|
+
/**
|
|
383
|
+
* ================================
|
|
384
|
+
* Domains
|
|
385
|
+
* ================================
|
|
386
|
+
*/
|
|
387
|
+
/**
|
|
388
|
+
* Adds a domain to EventCatalog
|
|
389
|
+
*
|
|
390
|
+
* @param domain - The domain to write
|
|
391
|
+
* @param options - Optional options to write the event
|
|
392
|
+
*
|
|
393
|
+
*/
|
|
394
|
+
writeDomain: writeDomain((0, import_node_path6.join)(path, "domains")),
|
|
395
|
+
/**
|
|
396
|
+
* Returns a domain from EventCatalog
|
|
397
|
+
* @param id - The id of the domain to retrieve
|
|
398
|
+
* @param version - Optional id of the version to get
|
|
399
|
+
* @returns
|
|
400
|
+
*/
|
|
401
|
+
getDomain: getDomain((0, import_node_path6.join)(path, "domains")),
|
|
402
|
+
/**
|
|
403
|
+
* Moves a given domain id to the version directory
|
|
404
|
+
* @param directory
|
|
405
|
+
*/
|
|
406
|
+
versionDomain: versionDomain((0, import_node_path6.join)(path, "domains")),
|
|
407
|
+
/**
|
|
408
|
+
* Remove a domain from EventCatalog (modeled on the standard POSIX rm utility)
|
|
409
|
+
*
|
|
410
|
+
* @param path - The path to your domain, e.g. `/Payment`
|
|
411
|
+
*
|
|
412
|
+
*/
|
|
413
|
+
rmDomain: rmDomain((0, import_node_path6.join)(path, "domains")),
|
|
414
|
+
/**
|
|
415
|
+
* Remove an service by an domain id
|
|
416
|
+
*
|
|
417
|
+
* @param id - The id of the domain you want to remove
|
|
418
|
+
*
|
|
419
|
+
*/
|
|
420
|
+
rmDomainById: rmDomainById((0, import_node_path6.join)(path, "domains")),
|
|
421
|
+
/**
|
|
422
|
+
* Adds a file to the given domain
|
|
423
|
+
* @param id - The id of the domain to add the file to
|
|
424
|
+
* @param file - File contents to add including the content and the file name
|
|
425
|
+
* @param version - Optional version of the domain to add the file to
|
|
426
|
+
* @returns
|
|
427
|
+
*/
|
|
428
|
+
addFileToDomain: addFileToDomain((0, import_node_path6.join)(path, "domains"))
|
|
361
429
|
};
|
|
362
430
|
};
|
|
363
431
|
//# sourceMappingURL=index.js.map
|