diff --git a/package-lock.json b/package-lock.json index be3fbfcc..1e99a3b7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@ory/kratos-selfservice-ui-node", - "version": "0.13.0-25", + "version": "0.14.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@ory/kratos-selfservice-ui-node", - "version": "0.13.0-25", + "version": "0.14.0", "license": "Apache-2.0", "dependencies": { "@ory/client": "1.6.2", diff --git a/src/pkg/index.ts b/src/pkg/index.ts index 46df7e6a..9c1a71b5 100644 --- a/src/pkg/index.ts +++ b/src/pkg/index.ts @@ -2,18 +2,13 @@ // SPDX-License-Identifier: Apache-2.0 import { RouteOptionsCreator } from "./route" import sdk, { apiBaseUrl } from "./sdk" +import { filterNodesByGroups } from "./ui_nodes" import { UiNode, ErrorAuthenticatorAssuranceLevelNotSatisfied, OAuth2LogoutRequest, } from "@ory/client" -import { - ButtonLink, - Divider, - MenuLink, - Typography, - filterNodesByGroups, -} from "@ory/elements-markup" +import { ButtonLink, Divider, MenuLink, Typography } from "@ory/elements-markup" import { AxiosError } from "axios" import { NextFunction, Response } from "express" import { UnknownObject } from "express-handlebars/types" diff --git a/src/pkg/ui_nodes.ts b/src/pkg/ui_nodes.ts new file mode 100644 index 00000000..9871eeb8 --- /dev/null +++ b/src/pkg/ui_nodes.ts @@ -0,0 +1,192 @@ +// Copyright © 2024 Ory Corp +// SPDX-License-Identifier: Apache-2.0 +import { + UiNode, + UiNodeAnchorAttributes, + UiNodeAttributes, + UiNodeGroupEnum, + UiNodeImageAttributes, + UiNodeInputAttributes, + UiNodeInputAttributesTypeEnum, + UiNodeScriptAttributes, + UiNodeTextAttributes, +} from "@ory/client" + +/** + * Returns the node's label. + * + * @param node + * @return label + */ +export const getNodeLabel = (node: UiNode): string => { + const attributes = node.attributes + if (isUiNodeAnchorAttributes(attributes)) { + return attributes.title.text + } + + if (isUiNodeImageAttributes(attributes)) { + return node.meta.label?.text || "" + } + + if (isUiNodeInputAttributes(attributes)) { + if (attributes.label?.text) { + return attributes.label.text + } + } + + return node.meta.label?.text || "" +} + +/** + * A TypeScript type guard for nodes of the type + * + * @param attrs + */ +export function isUiNodeAnchorAttributes( + attrs: UiNodeAttributes, +): attrs is UiNodeAnchorAttributes & { node_type: "a" } { + return attrs.node_type === "a" +} + +/** + * A TypeScript type guard for nodes of the type + * + * @param attrs + */ +export function isUiNodeImageAttributes( + attrs: UiNodeAttributes, +): attrs is UiNodeImageAttributes & { node_type: "img" } { + return attrs.node_type === "img" +} + +/** + * A TypeScript type guard for nodes of the type + * + * @param attrs + */ +export function isUiNodeInputAttributes( + attrs: UiNodeAttributes, +): attrs is UiNodeInputAttributes & { node_type: "input" } { + return attrs.node_type === "input" +} + +/** + * A TypeScript type guard for nodes of the type {text} + * + * @param attrs + */ +export function isUiNodeTextAttributes( + attrs: UiNodeAttributes, +): attrs is UiNodeTextAttributes & { node_type: "text" } { + return attrs.node_type === "text" +} + +/** + * A TypeScript type guard for nodes of the type