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

next
ts 60 lines 1.24 KB
Raw
1 import { ValueBuilder } from './ValueBuilder';
2 import { PropertyValue } from './PropertyValue';
3 import type { Writer } from './Writer';
4
5 type ObjectTypeItem = PropertyValue;
6
7 export class ObjectValue extends ValueBuilder {
8 needsParenthesisWhenIndexed = true;
9
10 private items: ObjectTypeItem[] = [];
11 private inline = false;
12
13 add(item: ObjectTypeItem): this {
14 this.items.push(item);
15 return this;
16 }
17
18 addMultiple(items: ObjectTypeItem[]): this {
19 for (const item of items) {
20 this.add(item);
21 }
22 return this;
23 }
24
25 formatInline() {
26 this.inline = true;
27 return this;
28 }
29
30 write(writer: Writer): void {
31 if (this.items.length === 0) {
32 writer.write('{}');
33 } else if (this.inline) {
34 this.writeInline(writer);
35 } else {
36 this.writeMultiline(writer);
37 }
38 }
39
40 private writeMultiline(writer: Writer) {
41 writer
42 .writeLine('{')
43 .withIndent(() => {
44 for (const item of this.items) {
45 writer.write(item);
46 writer.write(',');
47 writer.newLine();
48 }
49 })
50 .write('}');
51 }
52
53 private writeInline(writer: Writer) {
54 writer.write('{ ').writeJoined(', ', this.items).write(' }');
55 }
56 }
57
58 export function objectValue() {
59 return new ObjectValue();
60 }