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 62 lines 1.52 KB
Raw
1 import type { BasicBuilder } from './BasicBuilder';
2 import { DocComment } from './DocComment';
3 import { isValidJsIdentifier } from './isValidJsIdentifier';
4 import { TypeContext } from './OperatorPrecedence';
5 import { TypeBuilder } from './TypeBuilder';
6 import { WellKnownSymbol } from './WellKnownSymbol';
7 import { Writer } from './Writer';
8
9 export class Property implements BasicBuilder {
10 private isOptional = false;
11 private isReadonly = false;
12 private docComment?: DocComment;
13
14 constructor(
15 private name: string | WellKnownSymbol,
16 private type: TypeBuilder
17 ) {}
18
19 optional(): this {
20 this.isOptional = true;
21 return this;
22 }
23
24 readonly(): this {
25 this.isReadonly = true;
26 return this;
27 }
28
29 setDocComment(docComment: DocComment): this {
30 this.docComment = docComment;
31 return this;
32 }
33
34 write(writer: Writer): void {
35 if (this.docComment) {
36 writer.write(this.docComment);
37 }
38 if (this.isReadonly) {
39 writer.write('readonly ');
40 }
41
42 if (typeof this.name === 'string') {
43 if (isValidJsIdentifier(this.name)) {
44 writer.write(this.name);
45 } else {
46 writer.write('[').write(JSON.stringify(this.name)).write(']');
47 }
48 } else {
49 writer.write('[').write(this.name).write(']');
50 }
51
52 if (this.isOptional) {
53 writer.write('?');
54 }
55 writer.write(': ');
56 this.type.writeInContext(writer, TypeContext.ObjectProperty);
57 }
58 }
59
60 export function property(name: string | WellKnownSymbol, type: TypeBuilder) {
61 return new Property(name, type);
62 }