UNPKG

@tokens-studio/graph-engine

Version:

An execution engine to handle Token Studios generators and resolvers

66 lines 2.22 kB
import { AnySchema, BooleanSchema, StringSchema } from '../../schemas/index.js'; import { Node } from '../../programmatic/node.js'; function getSchemaByPath(schema, path) { // Remove the leading dot if present if (path.startsWith('.')) { path = path.slice(1); } // Split the path by dots const keys = path.split('.'); // Traverse the schema using the keys let currentSchema = schema; for (const key of keys) { if (currentSchema.type === 'object' && currentSchema.properties) { currentSchema = currentSchema.properties[key]; } else { // If the path does not match the schema structure, return undefined return undefined; } } return currentSchema; } function getNestedProperty(obj, path) { // Remove the leading dot if present if (path.startsWith('.')) { path = path.slice(1); } // Split the path by dots const keys = path.split('.'); // Traverse the object using the keys return keys.reduce((acc, key) => acc && acc[key], obj); } export default class NodeDefinition extends Node { static title = 'Object Path'; static type = 'studio.tokens.generic.objectPath'; static description = 'Accesses an object at a given path.'; constructor(props) { super(props); this.addInput('object', { type: AnySchema }); this.addInput('path', { type: StringSchema }); this.addOutput('value', { type: AnySchema }); this.addOutput('missing', { type: BooleanSchema }); } execute() { const object = this.inputs.object; const path = this.inputs.path; if (!path.value) { this.outputs.missing.set(true); throw new Error('Path is required'); } //Attempt to access the object at the given path const property = getNestedProperty(object.value, path.value); const schema = getSchemaByPath(object.type, path.value); this.outputs.value.set(property, schema); this.outputs.missing.set(property == undefined); } } //# sourceMappingURL=objectProperty.js.map