From 89ebb03ecbd0ed4b0a2cbe2ad72df6faad8ca783 Mon Sep 17 00:00:00 2001 From: Jacek Pudysz Date: Sat, 25 Nov 2023 15:38:43 +0100 Subject: [PATCH] chore: improve erorr messages, restrict available methods for registry --- src/common.ts | 19 ++++++++++--------- src/core/UnistyleRegistry.ts | 23 +++++++++++++++++++---- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/common.ts b/src/common.ts index cfb58b19..d0d71948 100644 --- a/src/common.ts +++ b/src/common.ts @@ -22,13 +22,14 @@ export enum UnistylesEventType { } export enum UnistylesError { - RuntimeUnavailable = 'Unistyles runtime is not available. Make sure you followed the installation instructions.', - ThemeNotFound = 'You are trying to get a theme that is not registered.', - ThemeNotRegistered = 'You are trying to set a theme that is not registered.', - ThemesCannotBeEmpty = 'You are trying to register an empty themes object.', - BreakpointsCannotBeEmpty = 'You are trying to register an empty breakpoints object.', - BreakpointsMustStartFromZero = 'One breakpoint must start from 0.', - InvalidPluginName = 'Plugin name can\'t start from reserved prefix. Use another name.', - DuplicatePluginName = 'You are trying to register a plugin with a name that is already registered.', - CantRemoveInternalPlugin = 'You are trying to remove an internal unistyles plugin.' + RuntimeUnavailable = 'Unistyles runtime is not available. Make sure you followed the installation instructions', + ThemeNotFound = 'You are trying to get a theme that is not registered with UnistylesRegistry', + ThemeNotRegistered = 'You are trying to set a theme that was not registered with UnistylesRegistry', + ThemeNotSelected = 'Your themes are registered, but you didn\'t select the initial theme', + ThemesCannotBeEmpty = 'You are trying to register empty themes object', + BreakpointsCannotBeEmpty = 'You are trying to register empty breakpoints object', + BreakpointsMustStartFromZero = 'You are trying to register breakpoints that don\'t start from 0', + InvalidPluginName = 'Plugin name can\'t start from reserved prefix __unistyles', + DuplicatePluginName = 'You are trying to register a plugin with a name that is already registered', + CantRemoveInternalPlugin = 'You are trying to remove an internal unistyles plugin' } diff --git a/src/core/UnistyleRegistry.ts b/src/core/UnistyleRegistry.ts index 2050f850..f90efbab 100644 --- a/src/core/UnistyleRegistry.ts +++ b/src/core/UnistyleRegistry.ts @@ -23,7 +23,10 @@ export class UnistyleRegistry { this.unistylesBridge.themes = keys this.themeNames = keys - return this + return { + addBreakpoints: this.addBreakpoints, + addConfig: this.addConfig + } } public addBreakpoints = (breakpoints: UnistylesBreakpoints) => { @@ -31,7 +34,10 @@ export class UnistyleRegistry { this.unistylesBridge.useBreakpoints(breakpoints) this.sortedBreakpointPairs = this.unistylesBridge.sortedBreakpointPairs - return this + return { + addThemes: this.addThemes, + addConfig: this.addConfig + } } public addConfig = (config: UnistylesConfig) => { @@ -48,6 +54,11 @@ export class UnistyleRegistry { if (config.initialTheme) { this.unistylesBridge.useTheme(config.initialTheme) } + + return { + addBreakpoints: this.addBreakpoints, + addThemes: this.addThemes + } } public getTheme = (forName: keyof UnistylesThemes) => { @@ -55,11 +66,15 @@ export class UnistyleRegistry { return {} as UnistylesThemes[keyof UnistylesThemes] } - if (!this.hasTheme(forName)) { + if (this.hasTheme(forName)) { + return this.themes[forName] + } + + if (this.unistylesBridge.themeName) { throw new Error(UnistylesError.ThemeNotFound) } - return this.themes[forName] + throw new Error(UnistylesError.ThemeNotSelected) } public addPlugin = (plugin: UnistylesPlugin, notify: boolean = true) => {