Core
Core widget functionality for data persistence, identity, and navigation.
setWidgetData(data)
The setWidgetData function allows you to persist data for your widget. This data is saved by the host application and can be restored when the widget is reloaded.
Usage
import { setWidgetData } from "@buildcosmo/widget";
// Save a simple object
setWidgetData({
theme: "dark",
lastUpdated: Date.now(),
});Parameters
| Name | Type | Description |
|---|---|---|
data | any | The data to save. If an object is passed, it will be automatically stringified. |
Behavior
- The data is sent to the host application via
window.webkit.messageHandlers.saveWidgetData. - If
datais an object, it isJSON.stringify'd before sending. - This data is associated with the specific widget instance.
getWidgetId()
Retrieves the unique identifier for the current widget instance.
Usage
import { getWidgetId } from "@buildcosmo/widget";
const widgetId = await getWidgetId();
console.log("Widget ID:", widgetId);Returns
Promise<string>: The unique ID of the widget.
getUserId()
Retrieves the unique identifier for the current user.
Usage
import { getUserId } from "@buildcosmo/widget";
const userId = await getUserId();
console.log("User ID:", userId);Returns
Promise<string>: The unique ID of the user.
openUrl(url)
Opens a URL in the user's default web browser.
Usage
import { openUrl } from "@buildcosmo/widget";
openUrl("https://example.com");Parameters
| Name | Type | Description |
|---|---|---|
url | string | The URL to open. |
openCosmoUrl(path)
Opens a path within the Cosmo application.
Usage
import { openCosmoUrl } from "@buildcosmo/widget";
openCosmoUrl("/settings");Parameters
| Name | Type | Description |
|---|---|---|
path | string | The internal path to navigate to. |
getCosmoUrl(path)
Generates a full Cosmo URL for a given path.
Usage
import { getCosmoUrl } from "@buildcosmo/widget";
const url = await getCosmoUrl("/widget/123");Parameters
| Name | Type | Description |
|---|---|---|
path | string | The internal path. |
Returns
Promise<string>: The full URL.