···991010import type { MaybeTsDsl, TypeTsDsl } from '../base';
1111import { TsDsl } from '../base';
1212-import { AsMixin, setAsFactory } from '../mixins/as';
1212+import { AsMixin } from '../mixins/as';
1313import { ExprMixin } from '../mixins/expr';
1414+import { f } from '../utils/factories';
14151516export type AsExpr = Symbol | string | MaybeTsDsl<ts.Expression>;
1617export type AsType = Symbol | string | TypeTsDsl;
···4445 }
4546}
46474747-setAsFactory((...args) => new AsTsDsl(...args));
4848+f.as.set((...args) => new AsTsDsl(...args));
+3-2
packages/openapi-ts/src/ts-dsl/expr/attr.ts
···1010import type { MaybeTsDsl } from '../base';
1111import { TsDsl } from '../base';
1212import { AsMixin } from '../mixins/as';
1313-import { ExprMixin, setAttrFactory } from '../mixins/expr';
1313+import { ExprMixin } from '../mixins/expr';
1414import { OperatorMixin } from '../mixins/operator';
1515import { OptionalMixin } from '../mixins/optional';
1616import { TokenTsDsl } from '../token';
1717+import { f } from '../utils/factories';
1718import { regexp } from '../utils/regexp';
1819import { LiteralTsDsl } from './literal';
1920···8384 }
8485}
85868686-setAttrFactory((...args) => new AttrTsDsl(...args));
8787+f.attr.set((...args) => new AttrTsDsl(...args));
+3-2
packages/openapi-ts/src/ts-dsl/expr/await.ts
···991010import type { MaybeTsDsl } from '../base';
1111import { TsDsl } from '../base';
1212-import { ExprMixin, setAwaitFactory } from '../mixins/expr';
1212+import { ExprMixin } from '../mixins/expr';
1313+import { f } from '../utils/factories';
13141415export type AwaitExpr = Symbol | string | MaybeTsDsl<ts.Expression>;
1516export type AwaitCtor = (expr: AwaitExpr) => AwaitTsDsl;
···3637 }
3738}
38393939-setAwaitFactory((...args) => new AwaitTsDsl(...args));
4040+f.await.set((...args) => new AwaitTsDsl(...args));
+3-2
packages/openapi-ts/src/ts-dsl/expr/call.ts
···99import { TsDsl } from '../base';
1010import { ArgsMixin } from '../mixins/args';
1111import { AsMixin } from '../mixins/as';
1212-import { ExprMixin, setCallFactory } from '../mixins/expr';
1212+import { ExprMixin } from '../mixins/expr';
1313import { TypeArgsMixin } from '../mixins/type-args';
1414+import { f } from '../utils/factories';
14151516export type CallCallee = string | MaybeTsDsl<ts.Expression>;
1617export type CallArg = Symbol | string | MaybeTsDsl<ts.Expression>;
···4647 }
4748}
48494949-setCallFactory((...args) => new CallTsDsl(...args));
5050+f.call.set((...args) => new CallTsDsl(...args));
+7-4
packages/openapi-ts/src/ts-dsl/expr/typeof.ts
···44import type { MaybeTsDsl } from '../base';
55import { TsDsl } from '../base';
66import { OperatorMixin } from '../mixins/operator';
77-import { setTypeOfExprFactory } from '../mixins/type-expr';
77+import { f } from '../utils/factories';
88+99+export type TypeOfExpr = string | MaybeTsDsl<ts.Expression>;
1010+export type TypeOfExprCtor = (expr: TypeOfExpr) => TypeOfExprTsDsl;
811912const Mixed = OperatorMixin(TsDsl<ts.TypeOfExpression>);
10131114export class TypeOfExprTsDsl extends Mixed {
1215 readonly '~dsl' = 'TypeOfExprTsDsl';
13161414- protected _expr: string | MaybeTsDsl<ts.Expression>;
1717+ protected _expr: TypeOfExpr;
15181616- constructor(expr: string | MaybeTsDsl<ts.Expression>) {
1919+ constructor(expr: TypeOfExpr) {
1720 super();
1821 this._expr = expr;
1922 }
···2831 }
2932}
30333131-setTypeOfExprFactory((...args) => new TypeOfExprTsDsl(...args));
3434+f.typeofExpr.set((...args) => new TypeOfExprTsDsl(...args));
+2-2
packages/openapi-ts/src/ts-dsl/index.ts
···321321 },
322322 ),
323323324324- /** Creates a runtime `typeof` expression (e.g. typeof x). */
324324+ /** Creates a `typeof` expression (e.g. `typeof value`). */
325325 typeofExpr: (...args: ConstructorParameters<typeof TypeOfExprTsDsl>) =>
326326 new TypeOfExprTsDsl(...args),
327327328328- /** Creates a variable declaration (var). */
328328+ /** Creates a variable declaration (`var`). */
329329 var: (...args: ConstructorParameters<typeof VarTsDsl>) =>
330330 new VarTsDsl(...args),
331331};
+7-11
packages/openapi-ts/src/ts-dsl/mixins/as.ts
···11import type { AnalysisContext, Node } from '@hey-api/codegen-core';
22import type ts from 'typescript';
3344-import type { AsCtor, AsTsDsl, AsType } from '../expr/as';
55-import type { BaseCtor, MixinCtor } from './types';
66-77-let asFactory: AsCtor | undefined;
88-/** Lazy register the factory to avoid circular imports. */
99-export function setAsFactory(factory: AsCtor): void {
1010- asFactory = factory;
1111-}
44+import { f } from '../utils/factories';
55+import type { BaseCtor, DropFirst, MixinCtor } from './types';
126137export interface AsMethods extends Node {
148 /** Creates an `as` type assertion expression (e.g. `value as Type`). */
1515- as(type: AsType): AsTsDsl;
99+ as(...args: DropFirst<Parameters<typeof f.as>>): ReturnType<typeof f.as>;
1610}
17111812export function AsMixin<T extends ts.Expression, TBase extends BaseCtor<T>>(
···2317 super.analyze(ctx);
2418 }
25192626- protected as(type: AsType): AsTsDsl {
2727- return asFactory!(this, type);
2020+ protected as(
2121+ ...args: DropFirst<Parameters<typeof f.as>>
2222+ ): ReturnType<typeof f.as> {
2323+ return f.as(this, ...args);
2824 }
2925 }
3026
+22-41
packages/openapi-ts/src/ts-dsl/mixins/expr.ts
···11import type { AnalysisContext, Node } from '@hey-api/codegen-core';
22import type ts from 'typescript';
3344-import type { AttrCtor, AttrRight, AttrTsDsl } from '../expr/attr';
55-import type { AwaitCtor, AwaitTsDsl } from '../expr/await';
66-import type { CallArgs, CallCtor, CallTsDsl } from '../expr/call';
77-import type { ReturnCtor, ReturnTsDsl } from '../stmt/return';
88-import type { BaseCtor, MixinCtor } from './types';
99-1010-let attrFactory: AttrCtor | undefined;
1111-/** Lazy register the factory to avoid circular imports. */
1212-export function setAttrFactory(fn: AttrCtor): void {
1313- attrFactory = fn;
1414-}
1515-1616-let awaitFactory: AwaitCtor | undefined;
1717-/** Lazy register the factory to avoid circular imports. */
1818-export function setAwaitFactory(fn: AwaitCtor): void {
1919- awaitFactory = fn;
2020-}
2121-2222-let callFactory: CallCtor | undefined;
2323-/** Lazy register the factory to avoid circular imports. */
2424-export function setCallFactory(fn: CallCtor): void {
2525- callFactory = fn;
2626-}
2727-2828-let returnFactory: ReturnCtor | undefined;
2929-/** Lazy register the factory to avoid circular imports. */
3030-export function setReturnFactory(fn: ReturnCtor): void {
3131- returnFactory = fn;
3232-}
44+import { f } from '../utils/factories';
55+import type { BaseCtor, DropFirst, MixinCtor } from './types';
336347export interface ExprMethods extends Node {
358 /** Accesses a property on the current expression (e.g. `this.foo`). */
3636- attr(name: AttrRight): AttrTsDsl;
99+ attr(
1010+ ...args: DropFirst<Parameters<typeof f.attr>>
1111+ ): ReturnType<typeof f.attr>;
3712 /** Awaits the current expression (e.g. `await expr`). */
3838- await(): AwaitTsDsl;
1313+ await(): ReturnType<typeof f.await>;
3914 /** Calls the current expression (e.g. `fn(arg1, arg2)`). */
4040- call(...args: CallArgs): CallTsDsl;
1515+ call(
1616+ ...args: DropFirst<Parameters<typeof f.call>>
1717+ ): ReturnType<typeof f.call>;
4118 /** Produces a `return` statement returning the current expression. */
4242- return(): ReturnTsDsl;
1919+ return(): ReturnType<typeof f.return>;
4320}
44214522export function ExprMixin<T extends ts.Expression, TBase extends BaseCtor<T>>(
···5027 super.analyze(ctx);
5128 }
52295353- protected attr(name: AttrRight): AttrTsDsl {
5454- return attrFactory!(this, name);
3030+ protected attr(
3131+ ...args: DropFirst<Parameters<typeof f.attr>>
3232+ ): ReturnType<typeof f.attr> {
3333+ return f.attr(this, ...args);
5534 }
56355757- protected await(): AwaitTsDsl {
5858- return awaitFactory!(this);
3636+ protected await(): ReturnType<typeof f.await> {
3737+ return f.await(this);
5938 }
60396161- protected call(...args: CallArgs): CallTsDsl {
6262- return callFactory!(this, ...args);
4040+ protected call(
4141+ ...args: DropFirst<Parameters<typeof f.call>>
4242+ ): ReturnType<typeof f.call> {
4343+ return f.call(this, ...args);
6344 }
64456565- protected return(): ReturnTsDsl {
6666- return returnFactory!(this);
4646+ protected return(): ReturnType<typeof f.return> {
4747+ return f.return(this);
6748 }
6849 }
6950
···11import type { TsDsl } from '../base';
2233-export type BaseCtor<T> = abstract new (...args: any[]) => TsDsl<T>;
33+export type BaseCtor<T> = abstract new (...args: Array<any>) => TsDsl<T>;
44+55+export type DropFirst<T extends Array<any>> = T extends [any, ...infer Rest]
66+ ? Rest
77+ : never;
88+49export type MixinCtor<T extends BaseCtor<any>, K> = abstract new (
55- ...args: any[]
1010+ ...args: Array<any>
611) => InstanceType<T> & K;
+2-2
packages/openapi-ts/src/ts-dsl/stmt/return.ts
···991010import type { MaybeTsDsl } from '../base';
1111import { TsDsl } from '../base';
1212-import { setReturnFactory } from '../mixins/expr';
1212+import { f } from '../utils/factories';
13131414export type ReturnExpr = Symbol | string | MaybeTsDsl<ts.Expression>;
1515export type ReturnCtor = (expr?: ReturnExpr) => ReturnTsDsl;
···3636 }
3737}
38383939-setReturnFactory((...args) => new ReturnTsDsl(...args));
3939+f.return.set((...args) => new ReturnTsDsl(...args));