Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

1.3 KB
Raw Blame
import { DocComment } from './DocComment';
import { isValidJsIdentifier } from './isValidJsIdentifier';
import { WellKnownSymbol } from './WellKnownSymbol';
import { ValueBuilder } from './ValueBuilder';
import { Writer } from './Writer';
import type { BasicBuilder } from './BasicBuilder';

export class PropertyValue implements BasicBuilder {
  private isOptional = false;
  private docComment?: DocComment;

  constructor(
    public name: string | WellKnownSymbol,
    public value: ValueBuilder
  ) {}

  optional(): this {
    this.isOptional = true;
    return this;
  }

  setDocComment(docComment: DocComment): this {
    this.docComment = docComment;
    return this;
  }

  write(writer: Writer): void {
    if (this.docComment) {
      writer.write(this.docComment);
    }

    if (typeof this.name === 'string') {
      if (isValidJsIdentifier(this.name)) {
        writer.write(this.name);
      } else {
        writer.write('[').write(JSON.stringify(this.name)).write(']');
      }
    } else {
      writer.write('[').write(this.name).write(']');
    }

    if (this.isOptional) {
      writer.write('?');
    }
    writer.write(': ').write(this.value);
  }
}

export function propertyValue(name: string | WellKnownSymbol, value: ValueBuilder) {
  return new PropertyValue(name, value);
}