fork of hey-api/openapi-ts because I need some additional things
1import type { AnalysisContext, NodeName, Ref } from '@hey-api/codegen-core';
2import { ref } from '@hey-api/codegen-core';
3
4import { py } from '../../py-compiler';
5import type { MaybePyDsl } from '../base';
6import { PyDsl } from '../base';
7import { ArgsMixin } from '../mixins/args';
8import { ExprMixin } from '../mixins/expr';
9import { f } from '../utils/factories';
10
11export type CallArgs = ReadonlyArray<CallCallee | undefined>;
12export type CallCallee = NodeName | MaybePyDsl<py.Expression>;
13export type CallCtor = (callee: CallCallee, ...args: CallArgs) => CallPyDsl;
14
15const Mixed = ArgsMixin(ExprMixin(PyDsl<py.CallExpression>));
16
17export class CallPyDsl extends Mixed {
18 readonly '~dsl' = 'CallPyDsl';
19
20 protected _callee: Ref<CallCallee>;
21
22 constructor(callee: CallCallee, ...args: CallArgs) {
23 super();
24 this._callee = ref(callee);
25 this.args(...args);
26 }
27
28 override analyze(ctx: AnalysisContext): void {
29 super.analyze(ctx);
30 ctx.analyze(this._callee);
31 }
32
33 /** Returns true when all required builder calls are present. */
34 get isValid(): boolean {
35 return !this.missingRequiredCalls().length;
36 }
37
38 override toAst() {
39 this.$validate();
40
41 return py.factory.createCallExpression(this.$node(this._callee!), this.$args());
42 }
43
44 $validate(): asserts this is this & {
45 _callee: MaybePyDsl<py.Expression>;
46 } {
47 const missing = this.missingRequiredCalls();
48 if (!missing.length) return;
49 throw new Error(`Call expression missing ${missing.join(' and ')}`);
50 }
51
52 private missingRequiredCalls(): ReadonlyArray<string> {
53 const missing: Array<string> = [];
54 if (!this._callee) missing.push('callee');
55 return missing;
56 }
57}
58
59f.call.set((...args) => new CallPyDsl(...args));