Skip to content

@bight-ts/core — Custom IDs

import {
defineCustomId,
type BightCustomId,
type BightCustomIdValues,
} from "@bight-ts/core";

Creates a typed custom ID helper for encoding and decoding structured data in Discord component custom IDs.

PropertyTypeRequiredDescription
prefixstringYesThe stable prefix for this custom ID family
fieldsreadonly string[]NoNamed fields to encode after the prefix
separatorstringNoDelimiter between prefix and fields. Defaults to ":"
Property / MethodTypeDescription
prefixstringThe raw prefix string
customIdPrefixstringThe prefix with trailing separator (for use in handler routing). Equals prefix when no fields are defined.
fieldsreadonly string[]The field names
build(values)(values: Record<field, string>) => stringEncodes values into a custom ID string
parse(customId)(customId: string) => Record<field, string> | nullDecodes a custom ID string. Returns null on mismatch.
matches(customId)(customId: string) => booleanReturns true if the custom ID matches this definition
const ticketId = defineCustomId({
prefix: "ticket",
fields: ["action", "id"] as const,
});
ticketId.build({ action: "close", id: "42" }); // "ticket:close:42"
ticketId.parse("ticket:close:42"); // { action: "close", id: "42" }
ticketId.parse("other:data"); // null
ticketId.customIdPrefix; // "ticket:"
// Without fields
const simpleId = defineCustomId({ prefix: "confirm" });
simpleId.build({}); // "confirm"
simpleId.parse("confirm"); // {}

Values are URI-encoded during build() and decoded during parse(), so fields can contain special characters.