diff --git a/browser/CHANGELOG.md b/browser/CHANGELOG.md index 465c1133..296e3335 100644 --- a/browser/CHANGELOG.md +++ b/browser/CHANGELOG.md @@ -18,6 +18,12 @@ This changelog covers all five packages, as they are (for now) updated as a whol - BREAKING CHANGE: removed the `importJsonAdString` function. - Added `store.importJsonAD()` method. +- Added support for commonJS modules. + +### @tomic/cli + +- Fix shortnames in externals.ts are not converted to camelCase. +- Filter out duplicate classes and properties in generated types. ### @tomic/create-template diff --git a/browser/cli/src/generateBaseObject.ts b/browser/cli/src/generateBaseObject.ts index f48cef3a..66c60556 100644 --- a/browser/cli/src/generateBaseObject.ts +++ b/browser/cli/src/generateBaseObject.ts @@ -1,6 +1,6 @@ -import { Resource, urls } from '@tomic/lib'; +import { core, Resource } from '@tomic/lib'; import { store } from './store.js'; -import { camelCaseify } from './utils.js'; +import { camelCaseify, dedupe } from './utils.js'; export type ReverseMapping = Record; @@ -16,8 +16,10 @@ export const generateBaseObject = async ( throw ontology.error; } - const classes = ontology.get(urls.properties.classes) as string[]; - const properties = ontology.get(urls.properties.properties) as string[]; + const classes = dedupe(ontology.get(core.properties.classes)) as string[]; + const properties = dedupe( + ontology.get(core.properties.properties), + ) as string[]; const name = camelCaseify(ontology.title); const baseObj = { @@ -36,7 +38,7 @@ export const generateBaseObject = async ( const listToObj = async (list: string[]): Promise> => { const entries = await Promise.all( list.map(async subject => { - const resource = await store.getResourceAsync(subject); + const resource = await store.getResource(subject); return [camelCaseify(resource.title), subject]; }), diff --git a/browser/cli/src/generateClasses.ts b/browser/cli/src/generateClasses.ts index d5b36fe7..00af3d76 100644 --- a/browser/cli/src/generateClasses.ts +++ b/browser/cli/src/generateClasses.ts @@ -1,14 +1,15 @@ -import { Core, Resource } from '@tomic/lib'; +import { type Core, type Resource } from '@tomic/lib'; import { store } from './store.js'; import { ReverseMapping } from './generateBaseObject.js'; import { PropertyRecord } from './PropertyRecord.js'; +import { dedupe } from './utils.js'; export const generateClasses = ( ontology: Resource, reverseMapping: ReverseMapping, propertyRecord: PropertyRecord, ): string => { - const classes = ontology.props.classes ?? []; + const classes = dedupe(ontology.props.classes ?? []); const classStringList = classes.map(subject => { return generateClass(subject, reverseMapping, propertyRecord); diff --git a/browser/cli/src/generateExternals.ts b/browser/cli/src/generateExternals.ts index 995fa55f..36fb2618 100644 --- a/browser/cli/src/generateExternals.ts +++ b/browser/cli/src/generateExternals.ts @@ -38,7 +38,7 @@ const generateTypeMapping = (properties: Resource[]) => { const lines = properties.map(prop => { const type = DatatypeToTSTypeMap[prop.props.datatype as Datatype]; - return `["${prop.getSubject()}"]: ${type};`; + return `["${prop.subject}"]: ${type};`; }); return lines.join('\n'); @@ -48,7 +48,7 @@ const generateNameMapping = (properties: Resource[]) => { const lines = properties.map(prop => { const name = camelCaseify(prop.props.shortname); - return `["${prop.getSubject()}"]: "${name}";`; + return `["${prop.subject}"]: "${name}";`; }); return lines.join('\n'); @@ -58,7 +58,7 @@ const generateBaseObjectProperties = ( properties: Resource[], ) => { const lines = properties.map( - p => `${p.props.shortname}: '${p.getSubject()}',`, + p => `${camelCaseify(p.props.shortname)}: '${p.subject}',`, ); return lines.join('\n'); @@ -66,7 +66,7 @@ const generateBaseObjectProperties = ( export const generateExternals = async (props: string[]) => { const properties: Resource[] = await Promise.all( - props.map(p => store.getResourceAsync(p)), + props.map(p => store.getResource(p)), ); const baseOjbectProperties = generateBaseObjectProperties(properties); diff --git a/browser/cli/src/utils.ts b/browser/cli/src/utils.ts index fe6e1406..574fb27c 100644 --- a/browser/cli/src/utils.ts +++ b/browser/cli/src/utils.ts @@ -2,3 +2,7 @@ export const camelCaseify = (str: string) => str.replace(/-([a-z])/g, g => { return g[1].toUpperCase(); }); + +export const dedupe = (array: T[]): T[] => { + return Array.from(new Set(array)); +}; diff --git a/browser/data-browser/src/components/forms/ResourceForm.tsx b/browser/data-browser/src/components/forms/ResourceForm.tsx index 9f516e8c..a3045cf7 100644 --- a/browser/data-browser/src/components/forms/ResourceForm.tsx +++ b/browser/data-browser/src/components/forms/ResourceForm.tsx @@ -247,7 +247,6 @@ export function ResourceForm({ setSubject={set => { handleAddProp(set); }} - error={newPropErr} isA={core.classes.property} /> diff --git a/browser/lib/package.json b/browser/lib/package.json index 0cbf7fcc..6a13b018 100644 --- a/browser/lib/package.json +++ b/browser/lib/package.json @@ -22,7 +22,11 @@ ], "gitHead": "2172c73d8df4e5f273e6386676abc91b6c5b2707", "license": "MIT", - "main": "dist/index.js", + "exports": { + "import": "./dist/index.js", + "require": "./dist/index.cjs", + "types": "./dist/src/index.d.ts" + }, "main-dev": "src/index.ts", "name": "@tomic/lib", "publishConfig": { diff --git a/browser/react/src/components/Image.tsx b/browser/react/src/components/Image.tsx index 03866494..e7755303 100644 --- a/browser/react/src/components/Image.tsx +++ b/browser/react/src/components/Image.tsx @@ -1,6 +1,6 @@ -import type { Resource, Server } from '@tomic/lib'; +import { type Resource, type Server, unknownSubject, server } from '@tomic/lib'; import React from 'react'; -import { server, unknownSubject, useResource, useString } from '../index.js'; +import { useResource, useString } from '../index.js'; const imageFormatsWithBasicSupport = new Set([ 'image/svg+xml',