import { SDKCollection } from './core/collection.js' import { SDKModel, SDKModelSubclass } from './core/model.js' import Transport from './core/transport.js' import { AuthModel } from './models/auth.js' import { CollectionModel, Collections } from './models/collections.js' import { ComponentModel, Components } from './models/components.js' import { ContentHubOneDatasources, DatasourceModel, Datasources, XmDatasources } from './models/datasources.js' import { ContentHubOneContentTypeModel } from './models/external/content-hub-one-content-types.js' import { ExternalComponentModel } from './models/external/external-components.js' import { FormModel, Forms } from './models/external/forms.js' import { TenantModel, Tenants } from './models/external/tenants.js' import { XmTemplateModel } from './models/external/xm-templates.js' import { Libraries, LibraryModel, TemplateLibraries } from './models/libraries.js' import { StylesheetModel, Stylesheets } from './models/stylesheets.js' import { VersionBundleModel, VersionBundles } from './models/version-bundles.js' import { VersionModel, Versions } from './models/versions.js' import * as Style from './style/index.js' import * as CSS from './utils/css.js' import * as utils from './utils/index.js' import { RenderingHostModel } from './models/external/rendering-host.js' export * from './style/embed.js' export * from './utils/index.js' export { Style, CSS } // Create subclass that hassdk property defined] function assignApi(Base: T, sdk: SDK): T { const Subclass = class extends Base { constructor(...args: any[]) { super(...args) } } Object.defineProperty(Subclass.prototype, 'sdk', { enumerable: false, writable: true, value: sdk }) Object.defineProperty(Subclass.prototype, Symbol.toStringTag, { value: Base.name }) return Subclass } export class SDK extends Transport { Libraries = new Libraries(this) Collections = new Collections(this) Components = new Components(this) Tenants = new Tenants(this) Versions = new Versions(this) VersionBundles = new VersionBundles(this) Stylesheets = new Stylesheets(this) Datasources = new Datasources(this) XmDatasources = new XmDatasources(this) ContentHubOneDatasources = new ContentHubOneDatasources(this) TemplateLibraries = new TemplateLibraries(this) Forms = new Forms(this) Collection = assignApi(CollectionModel, this) Component = assignApi(ComponentModel, this) ExternalComponent = assignApi(ExternalComponentModel, this) Version = assignApi(VersionModel, this) VersionBundle = assignApi(VersionBundleModel, this) Library = assignApi(LibraryModel, this) Stylesheet = assignApi(StylesheetModel, this) Style = Object.assign(Style.Rule, Style) Datasource = assignApi(DatasourceModel, this) Tenant = assignApi(TenantModel, this) XmTemplate = assignApi(XmTemplateModel, this) Form = assignApi(FormModel, this) Auth = assignApi(AuthModel, this) RenderingHost = assignApi(RenderingHostModel, this) ContentHubOne = assignApi(ContentHubOneContentTypeModel, this) libraries = SDKCollection.construct(this.Library, () => ({ //sdk: this })) templateLibraries = SDKCollection.construct(this.Library, () => ({})) tenants = SDKCollection.construct(this.Tenant, () => {}) library: LibraryModel get externalComponents() { return this.renderingHost.expandedComponents } get externalDatasources() { return this.renderingHost.registeredDatasources } // combination of internal and external datasources datasources = SDKCollection.construct(this.Datasource, () => {}) static instance: SDK utils = utils CSS = CSS static Library = LibraryModel static Collection = CollectionModel static Component = ComponentModel static ExternalComponent = ExternalComponentModel static Version = VersionModel static Stylesheet = StylesheetModel static Style = Style static Datasource = DatasourceModel static Tenant = TenantModel static XmTemplate = XmTemplateModel auth: AuthModel = new this.Auth() renderingHost: RenderingHostModel = new this.RenderingHost() normalizeHTML(html: string) { return ( html // sort attributes .replace(/<([a-z0-9-]+) ([^>]+?)(\s?\/?)>/g, (m, tag, attrs, closer = '') => { const matches = attrs.match(/\b([\w-]+)(?:\s*=\s*("[^"]*"|'[^']*'|[^'">\s]+))?/g) if (!matches) return m matches.sort() return '<' + tag + ' ' + matches.join(' ') + closer + '>' }) // remove extra whitespace .replace(/>\s+<') .trim() ) } getFrontendURL(target: string | { getPath: () => string }) { const contextQuery = this.getContextQuery() const path = typeof target == 'string' ? target : target.getPath() return `${this.frontend}${path}${contextQuery ? '?' + contextQuery : ''}` } getContextQuery() { return this.auth.tenant ? new URLSearchParams(this.auth.tenant.getContext() as any).toString() : '' } getBackendURL(model: SDKModel & { getPath: () => string }) { return this.backend + '/' + model.getPath() } }