### Install Datastar SDK Tests as Go Library Source: https://github.com/starfederation/datastar/blob/develop/sdk/tests/README.md Use this command to get the Datastar SDK test suite as a Go library for your project. ```bash go get github.com/starfederation/datastar/sdk/tests ``` -------------------------------- ### Minimal ServerSentEventGenerator.PatchElements Example Source: https://github.com/starfederation/datastar/blob/develop/sdk/ADR.md This example demonstrates the minimal configuration for patching elements, specifying only the event type and the elements to be sent. ```text event: datastar-patch-elements data: elements
1
``` -------------------------------- ### Full ServerSentEventGenerator.PatchElements Example with All Options Source: https://github.com/starfederation/datastar/blob/develop/sdk/ADR.md This example showcases the full capabilities of PatchElements, including event ID, retry duration, mode, selector, view transition options, namespace, and the elements to be patched. ```text event: datastar-patch-elements id: 123 retry: 2000 data: mode inner data: selector #feed data: useViewTransition true data: viewTransitionSelector #main data: namespace html data: elements
data: elements 1 data: elements
``` -------------------------------- ### Datastar Element Example Source: https://github.com/starfederation/datastar/blob/develop/sdk/ADR.md Datastar requires complete HTML elements for patching. This example shows a valid element. ```html
Hello
``` -------------------------------- ### Minimal Server-Sent Event for Patching Signals Source: https://github.com/starfederation/datastar/blob/develop/sdk/ADR.md This minimal example demonstrates the server-sent event format for patching signals, including the event type and the signals data. ```text event: datastar-patch-signals data: signals {"output":"Patched Output Test","show":true,"input":"Test","user":{"name":"","email":""}} ``` -------------------------------- ### Full Server-Sent Event with All Options Source: https://github.com/starfederation/datastar/blob/develop/sdk/ADR.md This example demonstrates sending a script with all available options, including `id` for event tracking and `retry` duration. The `elements` data includes a `script` tag with `type` and `data-effect` attributes. ```text event: datastar-patch-elements id: 123 retry: 2000 data: mode append data: selector body data: elements ``` -------------------------------- ### Minimal Server-Sent Event for Script Execution Source: https://github.com/starfederation/datastar/blob/develop/sdk/ADR.md Use this minimal example to send a script to the client for execution. Ensure the `event` type is `datastar-patch-elements` and specify the `data` fields for `mode`, `selector`, and `elements`. ```text event: datastar-patch-elements data: mode append data: selector body data: elements ``` -------------------------------- ### Full Server-Sent Event for Patching Signals with Options Source: https://github.com/starfederation/datastar/blob/develop/sdk/ADR.md This example shows a full server-sent event for patching signals, including the event ID, retry duration, and the 'onlyIfMissing' option. ```text event: datastar-patch-signals id: 123 retry: 2000 data: onlyIfMissing true data: signals {"output":"Patched Output Test","show":true,"input":"Test","user":{"name":"","email":""}} ``` -------------------------------- ### Patch Elements by ID using ServerSentEventGenerator.PatchElements Source: https://github.com/starfederation/datastar/blob/develop/sdk/ADR.md This example shows how to patch multiple elements simultaneously by providing their IDs and new content. ```text event: datastar-patch-elements data: elements
New content.
data: elements
Other new content.
``` -------------------------------- ### Datastar Data Attributes for Reactivity Source: https://github.com/starfederation/datastar/blob/develop/README.md Use data-* attributes to enable frontend reactivity. Examples include data-bind for input binding, data-text for dynamic content, and data-on for event handling. ```html ``` ```html
``` ```html ``` -------------------------------- ### HTMX Fragment Example Source: https://github.com/starfederation/datastar/blob/develop/sdk/ADR.md HTMX can utilize HTML fragments, which are partial HTML structures. This is contrasted with Datastar's element requirement. ```html Hello World ``` -------------------------------- ### Remove Elements by Selector using ServerSentEventGenerator.PatchElements Source: https://github.com/starfederation/datastar/blob/develop/sdk/ADR.md This example illustrates how to remove elements from the DOM by specifying a CSS selector and using the 'remove' patch mode. ```text event: datastar-patch-elements data: mode remove data: selector #feed, #otherid ``` -------------------------------- ### Datastar SDK Test Output SSE Format Source: https://github.com/starfederation/datastar/blob/develop/sdk/tests/README.md Example of the Server-Sent Events (SSE) response format expected from the Datastar test endpoint. It includes event type, ID, retry interval, and data fields for patching elements. ```text event: datastar-patch-elements id: event1 retry: 2000 data: mode append data: selector body data: elements ``` -------------------------------- ### RFC 7386 JSON Merge Patch Signal Operations Source: https://github.com/starfederation/datastar/blob/develop/sdk/ADR.md Examples illustrating how to add, update, remove, and perform complex nested patches using RFC 7386 JSON Merge Patch semantics for signals. ```json // Add signal {"newSignal": "value"} ``` ```json // Update signal {"existingSignal": "newValue"} ``` ```json // Remove signal {"signalToRemove": null} ``` ```json // Complex nested patch { "user": { "name": "Johnny", "email": null, "preferences": { "theme": "dark" } } } ``` -------------------------------- ### Run Datastar SDK Tests with Go Test Source: https://github.com/starfederation/datastar/blob/develop/sdk/tests/README.md Utilize the standard Go test command to run the Datastar SDK test suite after cloning the repository. You can specify the server URL via an environment variable. ```bash # Clone the repository and navigate to tests directory cd sdk/tests # Run all tests go test -v # Run with custom server TEST_SERVER_URL=http://localhost:8080 go test -v ``` -------------------------------- ### Run Datastar SDK Tests CLI Source: https://github.com/starfederation/datastar/blob/develop/sdk/tests/README.md Execute the Datastar SDK test suite using the command-line interface. Supports running against a default or custom server, filtering by test type (GET/POST), and enabling verbose output. ```bash # Run all tests against default server (localhost:7331) go run github.com/starfederation/datastar/sdk/tests/cmd/datastar-sdk-tests@latest # Run with custom server go run github.com/starfederation/datastar/sdk/tests/cmd/datastar-sdk-tests@latest -server http://localhost:8080 # Run only GET tests go run github.com/starfederation/datastar/sdk/tests/cmd/datastar-sdk-tests@latest -type get # Run only POST tests go run github.com/starfederation/datastar/sdk/tests/cmd/datastar-sdk-tests@latest -type post # Verbose output go run github.com/starfederation/datastar/sdk/tests/cmd/datastar-sdk-tests@latest -v ``` -------------------------------- ### Run Datastar SDK Test Suite Source: https://github.com/starfederation/datastar/blob/develop/sdk/test/README.md Execute the Datastar SDK test suite against a specified server address. If no output is produced, all tests have passed. ```shell $ ./test-all.sh $server_address Running tests with argument: $server_address Processing GET cases... Processing POST cases... ``` -------------------------------- ### Configure Enabled Languages for Datastar Source: https://github.com/starfederation/datastar/blob/develop/tools/vscode-extension/README.md Customize which languages and file extensions have Datastar support by configuring the `datastar.enabledLanguages` setting in VS Code. ```json { "datastar.enabledLanguages": [ "html", "php", "twig", ".edge", ".custom" ] } ``` -------------------------------- ### Configure Custom Attributes for Datastar Plugins Source: https://github.com/starfederation/datastar/blob/develop/tools/vscode-extension/README.md Add syntax highlighting support for custom Datastar plugins using the `datastar.customAttributes` setting. Add plugin names without the `data-` prefix. Reload VS Code to apply changes. ```json { "datastar.customAttributes": [ "my-plugin" ] } ``` -------------------------------- ### Include Datastar Script Source: https://github.com/starfederation/datastar/blob/develop/README.md Add this script tag to your HTML to include the Datastar framework. Ensure you use the correct version. ```html ``` -------------------------------- ### Insert New Element by Selector using ServerSentEventGenerator.PatchElements Source: https://github.com/starfederation/datastar/blob/develop/sdk/ADR.md Demonstrates inserting a new HTML element into a specified container using the 'append' mode and a CSS selector. ```text event: datastar-patch-elements data: mode append data: selector #mycontainer data: elements
New content
``` -------------------------------- ### Datastar SDK Test Case Output Text Format Source: https://github.com/starfederation/datastar/blob/develop/sdk/test/README.md Specifies the expected format for the output.txt file in Datastar SDK test cases, adhering to the text/eventstream format. This includes 'event', 'id', 'retry', and 'elements' fields. ```text event: datastar-patch-elements id: 1 retry: 2000 elements: ; ``` -------------------------------- ### Datastar SDK Special Case: Multiline Signals Input Source: https://github.com/starfederation/datastar/blob/develop/sdk/test/README.md For the 'patchSignals' event type, this shows how to provide multiline signals using 'signals-raw' as a String with '\n' characters. This is necessary because JSON parsers would otherwise ignore line breaks. ```json { "events": [ { "type": "patchSignals", "signals-raw": "line1\nline2\nline3" } ] } ``` -------------------------------- ### ServerSentEventGenerator.PatchSignals Go Function Signature Source: https://github.com/starfederation/datastar/blob/develop/sdk/ADR.md The Go function signature for sending patch signals. It includes optional parameters for controlling the patching behavior. ```go ServerSentEventGenerator.PatchSignals( signals: string, options ?: { onlyIfMissing?: boolean, eventId?: string, retryDuration?: durationInMilliseconds } ) ``` -------------------------------- ### Datastar SDK Test Case Input JSON Structure Source: https://github.com/starfederation/datastar/blob/develop/sdk/test/README.md Defines the structure for the input.json file used in Datastar SDK test cases. It must contain an 'events' array, where each event specifies its type, script (if applicable), eventId, retryDuration, and attributes. ```json { "events": [ { "type": "executeScript", "script": "console.log('hello');", "eventId": 1, "retryDuration": 2000, "attributes": { "type": "text/javascript", "blocking": false }, "autoRemove": false } ] } ``` -------------------------------- ### ServerSentEventGenerator.PatchElements Function Signature Source: https://github.com/starfederation/datastar/blob/develop/sdk/ADR.md The function signature for patching HTML elements. It accepts elements, a selector, mode, and other options for DOM manipulation. ```go ServerSentEventGenerator.PatchElements( elements?: string, options?: { selector?: string, mode?: ElementPatchMode, useViewTransition?: boolean, viewTransitionSelector?: string, namespace?: 'html' | 'svg' | 'mathml', eventId?: string, retryDuration?: durationInMilliseconds } ) ``` -------------------------------- ### Append Script Element with Datastar Source: https://github.com/starfederation/datastar/blob/develop/sdk/test/get-cases/executeScriptWithAllOptions/output.txt This event payload demonstrates how to append a script element to the body using the datastar-patch-elements event. The script is non-blocking and logs a message to the console. ```text event: datastar-patch-elements id: event1 retry: 2000 data: mode append data: selector body data: elements ``` -------------------------------- ### ServerSentEventGenerator.PatchElements Source: https://github.com/starfederation/datastar/blob/develop/sdk/ADR.md Sends HTML elements to the browser for DOM manipulation, supporting various patching modes and options. ```APIDOC ## ServerSentEventGenerator.PatchElements ### Description Sends HTML elements to the browser for DOM manipulation. Supports patching, appending, removing, and using view transitions. ### Method Signature ```go ServerSentEventGenerator.PatchElements( elements?: string, options?: { selector?: string, mode?: ElementPatchMode, useViewTransition?: boolean, viewTransitionSelector?: string, namespace?: 'html' | 'svg' | 'mathml', eventId?: string, retryDuration?: durationInMilliseconds } ) ``` ### Parameters #### Elements The HTML elements to be patched or manipulated. #### Options - **selector** (string, Optional): CSS selector to target elements for patching or manipulation. - **mode** (ElementPatchMode, Optional): The patching mode. Use `remove` to delete elements. - **useViewTransition** (boolean, Optional): If true, uses the browser's View Transitions API. - **viewTransitionSelector** (string, Optional): A selector to scope the view transition. - **namespace** ('html' | 'svg' | 'mathml', Optional): The namespace of the elements being patched. - **eventId** (string, Optional): Unique event identifier for replay functionality. - **retryDuration** (ms, Optional): Reconnection delay after connection loss. ### Example Output Minimal Example: ``` event: datastar-patch-elements data: elements
1
``` Full Example (all options): ``` event: datastar-patch-elements id: 123 retry: 2000 data: mode inner data: selector #feed data: useViewTransition true data: viewTransitionSelector #main data: namespace html data: elements
1
``` Patch elements based on their ID: ``` event: datastar-patch-elements data: elements
New content.
data: elements
Other new content.
``` Insert a new element based on a selector: ``` event: datastar-patch-elements data: mode append data: selector #mycontainer data: elements
New content
``` Remove elements based on a selector: ``` event: datastar-patch-elements data: mode remove data: selector #feed, #otherid ``` Patch SVG elements: ``` event: datastar-patch-elements data: mode append data: selector #vis data: namespace svg data: elements data: elements data: elements ``` ``` -------------------------------- ### Datastar SDK Test Input JSON Format Source: https://github.com/starfederation/datastar/blob/develop/sdk/tests/README.md Defines the structure for the input JSON payload used in Datastar SDK tests. It includes an array of events, each with a type, ID, and optional parameters like retry duration and attributes. ```json { "events": [ { "type": "executeScript", "script": "console.log('hello');", "eventId": "event1", "retryDuration": 2000, "attributes": { "type": "text/javascript", "blocking": "false" }, "autoRemove": false } ] } ``` -------------------------------- ### Patch SVG Elements using ServerSentEventGenerator.PatchElements Source: https://github.com/starfederation/datastar/blob/develop/sdk/ADR.md Shows how to patch SVG elements into a specified container using the 'append' mode and setting the 'svg' namespace. ```text event: datastar-patch-elements data: mode append data: selector #vis data: namespace svg data: elements data: elements data: elements ``` -------------------------------- ### ReadSignals Source: https://github.com/starfederation/datastar/blob/develop/sdk/ADR.md Parses incoming signal data from the browser into backend objects. It supports different HTTP methods for data extraction. ```APIDOC ## `ReadSignals` ### Description Parses incoming signal data from the browser into backend objects. ### Method Signature `ReadSignals(request *http.Request, signals any) error` ### Parameters #### Path Parameters None #### Query Parameters - **datastar** (string) - Required - URL-encoded JSON data for GET and DELETE methods. #### Request Body - **signals** (any) - Required - Target object/struct for unmarshaling. Expected to be JSON for PATCH, POST, and PUT methods. ### Implementation Details This function handles different HTTP methods as follows: - **GET**: Extracts data from the `datastar` query parameter, expecting URL-encoded JSON. - **PATCH**: Parses data directly from the request body as JSON. - **POST**: Parses data directly from the request body as JSON. - **PUT**: Parses data directly from the request body as JSON. - **DELETE**: Extracts data from the `datastar` query parameter, expecting URL-encoded JSON. ### Error Handling Returns an error for invalid JSON parsing. ``` -------------------------------- ### ServerSentEventGenerator.send Source: https://github.com/starfederation/datastar/blob/develop/sdk/ADR.md A unified function to send Server-Sent Events. It supports different event types and optional parameters for event ID and retry duration. ```APIDOC ## ServerSentEventGenerator.send ### Description A unified sending function that should be used internally (private/protected) to send Server-Sent Events. ### Method Signature ``` ServerSentEventGenerator.send( eventType: EventType, dataLines: string[], options?: { eventId?: string, retryDuration?: durationInMilliseconds } ) ``` ### Parameters #### EventType String enum of supported events: - `datastar-patch-elements`: Patches HTML elements into the DOM - `datastar-patch-signals`: Patches signals into the signal store #### DataLines A string array representing the data for the event. #### Options - **eventId** (string, Optional): Unique event identifier for replay functionality. - **retryDuration** (ms, Optional, Default: `1000`): Reconnection delay after connection loss. ### Implementation Requirements Writes to the response buffer in the following order: 1. `event: EVENT_TYPE\n` 2. `id: EVENT_ID\n` (if `eventId` provided) 3. `retry: RETRY_DURATION\n` (unless default of `1000`) 4. `data: DATA\n` (for each of the `dataLines`) 5. `\n` (end of event) Should flush immediately. Error handling must return/throw errors per language conventions. ``` -------------------------------- ### Append Script Element to Body Source: https://github.com/starfederation/datastar/blob/develop/sdk/test/get-cases/executeScriptWithDefaults/output.txt Use this to append a script element to the body of the document. The script will execute and then be removed. ```html ``` -------------------------------- ### Append Script Element to Body Source: https://github.com/starfederation/datastar/blob/develop/sdk/test/get-cases/executeScriptWithoutDefaults/output.txt Use this to append a script element to the body of the document. The script will execute and then be removed. ```html event: datastar-patch-elements data: mode append data: selector body data: elements ``` -------------------------------- ### ServerSentEventGenerator.ExecuteScript Source: https://github.com/starfederation/datastar/blob/develop/sdk/ADR.md Executes a given JavaScript script on the client-side via server-sent events. Allows for options like auto-removal of the script tag, custom attributes, event IDs, and retry durations. ```APIDOC ## ServerSentEventGenerator.ExecuteScript ### Description Executes a given JavaScript script on the client-side via server-sent events. Allows for options like auto-removal of the script tag, custom attributes, event IDs, and retry durations. ### Method ServerSentEventGenerator.ExecuteScript ### Parameters - **script**: (string) - Required - One or more lines of JavaScript. - **options**: (object) - Optional - Configuration for script execution. - **autoRemove**: (boolean) - Optional - Removes the script tag after executing. Defaults to `true`. - **attributes**: ([]string) - Optional - Attributes to add to the script tag. - **eventId**: (string) - Optional - Identifier for the event. - **retryDuration**: (durationInMilliseconds) - Optional - Duration in milliseconds before retrying. ### Request Example #### Minimal Example ``` event: datastar-patch-elements data: mode append data: selector body data: elements ``` #### Full Example (all options) ``` event: datastar-patch-elements id: 123 retry: 2000 data: mode append data: selector body data: elements ``` ### Response This method does not return a value directly but sends a server-sent event. ### Implementation Details Calls `ServerSentEventGenerator.send` with event type `datastar-patch-elements`. The `script` tag is constructed based on the provided JavaScript and options. If `autoRemove` is `true`, `data-effect="el.remove()"` is added. Custom `attributes` are also included. The data format for the event includes `selector body`, `mode append`, and `elements SCRIPT_TAG`. ``` -------------------------------- ### ServerSentEventGenerator.send Function Signature Source: https://github.com/starfederation/datastar/blob/develop/sdk/ADR.md The unified sending function for Server-Sent Events. It supports event types, data lines, and optional event IDs or retry durations. ```typescript ServerSentEventGenerator.send( eventType: EventType, dataLines: string[], options?: { eventId?: string, retryDuration?: durationInMilliseconds } ) ``` -------------------------------- ### ReadSignals Function Signature Source: https://github.com/starfederation/datastar/blob/develop/sdk/ADR.md This is the function signature for ReadSignals. It takes an HTTP request and a target object for unmarshaling signal data. ```go ReadSignals(request *http.Request, signals any) error ``` -------------------------------- ### Append Script Element Source: https://github.com/starfederation/datastar/blob/develop/sdk/tests/golden/get/executeScriptWithMultilineScript/output.txt Appends a script element to the body. Use this to inject custom JavaScript or modify existing elements. ```text event: datastar-patch-elements data: mode append data: selector body data: elements ``` -------------------------------- ### Append Script Element to Body Source: https://github.com/starfederation/datastar/blob/develop/sdk/test/get-cases/executeScriptWithMultilineScript/output.txt Appends a script element to the body of the document. The script contains a simple console log statement and is configured to be removed by a data-effect attribute. ```html ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.