UNPKG

@elgato/streamdeck

Version:

The official Node.js SDK for creating Stream Deck plugins.

70 lines (69 loc) 2.73 kB
import { Event } from "./event.js"; /** * Event information received from Stream Deck as part of a deep-link message being routed to the plugin. */ export class DidReceiveDeepLinkEvent extends Event { /** * Deep-link URL routed from Stream Deck. */ url; /** * Initializes a new instance of the {@link DidReceiveDeepLinkEvent} class. * @param source Source of the event, i.e. the original message from Stream Deck. */ constructor(source) { super(source); this.url = new DeepLinkURL(source.payload.url); } } const PREFIX = "streamdeck://"; /** * Provides information associated with a URL received as part of a deep-link message, conforming to the URI syntax defined within RFC-3986 (https://datatracker.ietf.org/doc/html/rfc3986#section-3). */ export class DeepLinkURL { /** * Fragment of the URL, with the number sign (#) omitted. For example, a URL of "/test#heading" would result in a {@link DeepLinkURL.fragment} of "heading". */ fragment; /** * Original URL. For example, a URL of "/test?one=two#heading" would result in a {@link DeepLinkURL.href} of "/test?one=two#heading". */ href; /** * Path of the URL; the full URL with the query and fragment omitted. For example, a URL of "/test?one=two#heading" would result in a {@link DeepLinkURL.path} of "/test". */ path; /** * Query of the URL, with the question mark (?) omitted. For example, a URL of "/test?name=elgato&key=123" would result in a {@link DeepLinkURL.query} of "name=elgato&key=123". * See also {@link DeepLinkURL.queryParameters}. */ query; /** * Query string parameters parsed from the URL. See also {@link DeepLinkURL.query}. */ queryParameters; /** * Initializes a new instance of the {@link DeepLinkURL} class. * @param url URL of the deep-link, with the schema and authority omitted. */ constructor(url) { const refUrl = new URL(`${PREFIX}${url}`); this.fragment = refUrl.hash.substring(1); this.href = refUrl.href.substring(PREFIX.length); this.path = DeepLinkURL.parsePath(this.href); this.query = refUrl.search.substring(1); this.queryParameters = refUrl.searchParams; } /** * Parses the {@link DeepLinkURL.path} from the specified {@link href}. * @param href Partial URL that contains the path to parse. * @returns The path of the URL. */ static parsePath(href) { const indexOf = (char) => { const index = href.indexOf(char); return index >= 0 ? index : href.length; }; return href.substring(0, Math.min(indexOf("?"), indexOf("#"))); } }