From 5a26c41de03fdc4c2e3434220fd9b2c69b131beb Mon Sep 17 00:00:00 2001 From: Jeroen Claassens Date: Sun, 11 Feb 2024 13:04:26 +0100 Subject: [PATCH] feat: add `getConfig` and `setConfig` methods --- packages/core/UPGRADING-TO-V4.md | 9 +++++++++ packages/core/src/config.ts | 12 +++++++++++- packages/core/src/index.ts | 1 + 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/core/UPGRADING-TO-V4.md b/packages/core/UPGRADING-TO-V4.md index 8c58f2ac4..8d18ffbc6 100644 --- a/packages/core/UPGRADING-TO-V4.md +++ b/packages/core/UPGRADING-TO-V4.md @@ -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` diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 35d563461..7f5416e70 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -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): void { + config = Object.assign(config, partialConfig); +} export const defaultDiscordAvatars: Omit = { blue: 'https://cdn.discordapp.com/embed/avatars/0.png', diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 6fa5ac029..bbb91af8f 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -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 {