@hemingway_at/asset-url-toolkit
v1.0.3
Published
A toolkit for asset URL manipulation.
Downloads
5
Maintainers
Readme
Dynamic Asset URL Resolver and Favicon Setter
This TypeScript module provides functionalities to resolve asset URLs based on dynamic configuration and to set the favicon of a page.
Config Interface
export interface Config {
dynamicAssets?: {
urlPrefix: string
domainMap: { [key: string]: string }
assetsMap: { [key: string]: string }
} | null
}
The Config interface defines the structure of the dynamic asset configuration, including an optional dynamicAssets property. If present, it is an object with the following properties:
urlPrefix is the common prefix for all generated URLs.
domainMap is an object mapping different domains to their respective subpaths.
assetsMap is an object with asset names as keys and the corresponding asset paths under the specified domain as values.
getAssetUrl Function
export function getAssetUrl(
assetName: string,
config: Config
): string | undefined
This function takes an asset name and a configuration object as parameters and returns the URL of the asset based on the configuration. It returns undefined if it fails to obtain a valid URL. The function first validates the configuration, then looks up the corresponding domain and asset mapping based on the current hostname and asset name, and finally constructs and validates the URL.
isValidUrl Function
export function isValidUrl(urlString: string, base?: string): boolean
This function is used to validate whether a provided URL string is in a valid URL format. It achieves this by attempting to create a new URL object, returning true if successful and false otherwise.
setFavicon Function
export function setFavicon(url: string): void
This function takes the URL of a favicon and sets or updates the favicon of the current page. It first tries to update an existing favicon link element, and if it doesn't exist, it creates a new link element, sets the relevant properties, and appends it to the document head.
Summary
This module is primarily used for handling dynamic asset loading and favicon setting, providing a flexible object mapping mechanism based on configuration to accommodate varying requirements.