@inlang/paraglide-js
Version:
[](https://www.npmjs.com/package/@inlang/paraglide-js) [ => Promise<string | undefined> | (string | undefined) }} CustomServerStrategyHandler
*/
/**
* @typedef {{ getLocale: () => Promise<string|undefined> | (string | undefined), setLocale: (locale: string) => Promise<void> | void }} CustomClientStrategyHandler
*/
/** @type {Map<string, CustomServerStrategyHandler>} */
export const customServerStrategies = new Map();
/** @type {Map<string, CustomClientStrategyHandler>} */
export const customClientStrategies = new Map();
/**
* Checks if the given strategy is a custom strategy.
*
* @param {unknown} strategy The name of the custom strategy to validate.
* Must be a string that starts with "custom-" followed by alphanumeric characters, hyphens, or underscores.
* @returns {boolean} Returns true if it is a custom strategy, false otherwise.
*/
export function isCustomStrategy(strategy) {
return (typeof strategy === "string" && /^custom-[A-Za-z0-9_-]+$/.test(strategy));
}
/**
* Defines a custom strategy that is executed on the server.
*
* @see https://paraglidejs.com/strategy#write-your-own-strategy
*
* @param {string} strategy The name of the custom strategy to define. Must follow the pattern custom-name with alphanumeric characters, hyphens, or underscores.
* @param {CustomServerStrategyHandler} handler The handler for the custom strategy, which should implement
* the method getLocale.
* @returns {void}
*/
export function defineCustomServerStrategy(strategy, handler) {
if (!isCustomStrategy(strategy)) {
throw new Error(`Invalid custom strategy: "${strategy}". Must be a custom strategy following the pattern custom-name.`);
}
customServerStrategies.set(strategy, handler);
}
/**
* Defines a custom strategy that is executed on the client.
*
* @see https://paraglidejs.com/strategy#write-your-own-strategy
*
* @param {string} strategy The name of the custom strategy to define. Must follow the pattern custom-name with alphanumeric characters, hyphens, or underscores.
* @param {CustomClientStrategyHandler} handler The handler for the custom strategy, which should implement the
* methods getLocale and setLocale.
* @returns {void}
*/
export function defineCustomClientStrategy(strategy, handler) {
if (!isCustomStrategy(strategy)) {
throw new Error(`Invalid custom strategy: "${strategy}". Must be a custom strategy following the pattern custom-name.`);
}
customClientStrategies.set(strategy, handler);
}
//# sourceMappingURL=strategy.js.map