Skip to content

Commit

Permalink
chore: improve erorr messages, restrict available methods for registry
Browse files Browse the repository at this point in the history
  • Loading branch information
jpudysz committed Nov 25, 2023
1 parent e3a6a75 commit 89ebb03
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
19 changes: 10 additions & 9 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
23 changes: 19 additions & 4 deletions src/core/UnistyleRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,21 @@ export class UnistyleRegistry {
this.unistylesBridge.themes = keys
this.themeNames = keys

return this
return {
addBreakpoints: this.addBreakpoints,
addConfig: this.addConfig
}
}

public addBreakpoints = (breakpoints: UnistylesBreakpoints) => {
this.breakpoints = breakpoints
this.unistylesBridge.useBreakpoints(breakpoints)
this.sortedBreakpointPairs = this.unistylesBridge.sortedBreakpointPairs

return this
return {
addThemes: this.addThemes,
addConfig: this.addConfig
}
}

public addConfig = (config: UnistylesConfig) => {
Expand All @@ -48,18 +54,27 @@ export class UnistyleRegistry {
if (config.initialTheme) {
this.unistylesBridge.useTheme(config.initialTheme)
}

return {
addBreakpoints: this.addBreakpoints,
addThemes: this.addThemes
}
}

public getTheme = (forName: keyof UnistylesThemes) => {
if (this.themeNames.length === 0) {
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) => {
Expand Down

0 comments on commit 89ebb03

Please sign in to comment.