Skip to content

Commit

Permalink
feat: add getConfig and setConfig methods
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed Feb 11, 2024
1 parent 3182884 commit 5a26c41
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/core/UPGRADING-TO-V4.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ It is no longer possible to use this library simply by importing the unpkg URL.
You have to use a bundler such as [Vite](https://vitejs.dev) or
[Webpack](https://webpack.js.org) to use this library.

### Configuration

Previously configuration of this library was only possible through configuring
`$discordMessage` on the `window` object. This library now also exposes 2
functions `getConfig` and `setConfig` to set the config. This is primarily
because when using this library with server-side rendering the `window` object
is not always available and it may not be possible to use
`window.$discordMessage` to set the configuration.

### Component changes

#### `discord-inline-code`
Expand Down
12 changes: 11 additions & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import type { Avatars, Profile } from './types.js';
import type { Avatars, DiscordMessageOptions, Profile } from './types.js';

let config: DiscordMessageOptions = globalThis.$discordMessage ?? {};

export function getConfig(): DiscordMessageOptions {
return config;
}

export function setConfig(partialConfig: Partial<DiscordMessageOptions>): void {
config = Object.assign(config, partialConfig);
}

export const defaultDiscordAvatars: Omit<Avatars, 'default'> = {
blue: 'https://cdn.discordapp.com/embed/avatars/0.png',
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export { DiscordTime } from './components/discord-time/DiscordTime.js';
export { DiscordUnderlined } from './components/discord-underlined/DiscordUnderlined.js';

/* EXPORTS END */
export { getConfig, setConfig } from './config.js';
export type * from './types.js';

declare global {
Expand Down

0 comments on commit 5a26c41

Please sign in to comment.