fork of hey-api/openapi-ts because I need some additional things
1import type { AnalysisContext, Node } from '@hey-api/codegen-core';
2
3import type { py } from '../../py-compiler';
4import type { MaybePyDsl } from '../base';
5import type { ParamCtor, ParamFn, ParamName } from '../decl/param';
6import { ParamPyDsl } from '../decl/param';
7import type { BaseCtor, MixinCtor } from './types';
8
9export interface ParamMethods extends Node {
10 /** Renders the parameters into an array of `FunctionParameter`s. */
11 $params(): ReadonlyArray<py.FunctionParameter>;
12 /** Adds a parameter. */
13 param(...args: Parameters<ParamCtor>): this;
14 /** Adds multiple parameters. */
15 params(...params: ReadonlyArray<MaybePyDsl<py.FunctionParameter>>): this;
16}
17
18export function ParamMixin<T extends py.Node, TBase extends BaseCtor<T>>(Base: TBase) {
19 abstract class Param extends Base {
20 protected _params: Array<MaybePyDsl<py.FunctionParameter>> = [];
21
22 override analyze(ctx: AnalysisContext): void {
23 super.analyze(ctx);
24 for (const param of this._params) {
25 ctx.analyze(param);
26 }
27 }
28
29 protected param(name: ParamName, fn?: ParamFn): this {
30 const p = typeof name === 'function' ? new ParamPyDsl(name) : new ParamPyDsl(name, fn);
31 this._params.push(p);
32 return this;
33 }
34
35 protected params(...params: ReadonlyArray<MaybePyDsl<py.FunctionParameter>>): this {
36 this._params.push(...params);
37 return this;
38 }
39
40 protected $params(): ReadonlyArray<py.FunctionParameter> {
41 return this.$node(this._params);
42 }
43 }
44
45 return Param as unknown as MixinCtor<TBase, ParamMethods>;
46}