@bygd/opus-hl-config
v0.1.14
Published
The `@bygd/opus-hl-config` project is designed to streamline the process of fetching configuration data for your webpage. It aims to simplify interactions with the HeaderLift API by structuring and caching responses based on the page's URL and asset infor
Downloads
4
Readme
@bygd/opus-hl-config
The @bygd/opus-hl-config
project is designed to streamline the process of fetching configuration data for your webpage. It aims to simplify interactions with the HeaderLift API by structuring and caching responses based on the page's URL and asset information.
How It Works
The project uses a combination of given and default parameters to build a uniquely identifiable request to the HeaderLift API. It checks if the required data is already cached to minimize repeated network requests, enhancing performance by reducing latency. If the information isn't cached, it retrieves the necessary data by creating a query to a predefined API endpoint and processes the response for use.
Key Features
- URL Parsing: Automatically parses the page URL to extract the domain, ensuring correct API request formatting.
- Conditional Caching: Utilizes a caching mechanism to store and reuse data for repeated requests with the same parameters.
- Configurable Parameters: Allows overriding of default URL, page width, and type selections to accommodate a variety of use cases.
- Robust Error Handling: Provides error feedback for missing required parameters and failed network or data responses.
Conclusion
The @bygd/opus-hl-config
script is a helpful tool for developers looking to efficiently configure their webpages using HeaderLift data. Its caching functionality saves time and resources by reducing unnecessary network requests, ensuring response data is available quickly and reliably.
Developer Guide for @bygd/opus-hl-config
Overview
The @bygd/opus-hl-config
library provides a simple interface for loading configuration data tailored to specific pages or assets on your website. With its key feature of caching responses, the library reduces the need for repetitive and resource-intensive network requests. This makes it particularly useful for applications where configurations change infrequently but need to be accessed often.
Installation
To integrate @bygd/opus-hl-config
into your project, you can install it using either npm or yarn:
Using npm:
npm install @bygd/opus-hl-config
Using yarn:
yarn add @bygd/opus-hl-config
Usage
The main functionality of the library is accessible through the default export function. This function fetches configuration data based on the provided URL and other optional parameters.
Function Signature:
async function({
page_url,
page_type,
asset_id,
width,
} = {})
Parameters:
page_url
(optional): The URL of the page for which you want the configuration. If not specified, the current window URL will be used.page_type
(optional): A string representing the page type, defaults to"gd"
ifasset_id
is provided.asset_id
(optional): A unique identifier for the asset. If provided, it influences the resulting page type.width
(optional): The width of the page in pixels. Defaults to the current window width.
Returns:
A promise that resolves to the configuration data specific to the provided or inferred URL parameters.
Examples
Below are some examples demonstrating how to use the @bygd/opus-hl-config
library:
Basic Example
This example shows how to obtain configuration data for the current page:
import loadConfig from '@bygd/opus-hl-config';
(async () => {
try {
const configData = await loadConfig();
console.log(configData);
} catch (error) {
console.error('Error loading configuration:', error);
}
})();
Specifying Page URL and Asset ID
You can also specify a page URL and asset ID to get configuration data for a specific asset:
import loadConfig from '@bygd/opus-hl-config';
(async () => {
try {
const configData = await loadConfig({
page_url: 'https://example.com',
asset_id: '12345'
});
console.log(configData);
} catch (error) {
console.error('Error loading configuration:', error);
}
})();
Acknowledgement
There are no specific acknowledgments required for @bygd/opus-hl-config
.
Input Schema
$schema: https://json-schema.org/draft/2020-12/schema
type: object
properties:
page_url:
type: string
format: uri
description: The URL of the page. Defaults to the current window URL or
'https://gry.pl'.
page_type:
type: string
description: The type of the page. Defaults to 'gd' if asset_id is provided,
otherwise 'default'.
asset_id:
type: string
description: The ID of the asset. Optional parameter.
width:
type: integer
description: The width of the page. Defaults to the current window width or 720.
required:
- page_url