这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions dev/openapi-ts.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ export default defineConfig(() => {
// },
path: path.resolve(
getSpecsPath(),
// '3.0.x',
'3.1.x',
// 'circular.yaml',
'3.0.x',
// '3.1.x',
'circular.yaml',
// 'dutchie.json',
// 'invalid',
// 'full.yaml',
// 'openai.yaml',
'opencode.yaml',
// 'opencode.yaml',
// 'sdk-instance.yaml',
// 'string-with-format.yaml',
// 'transformers.json',
Expand Down
89 changes: 44 additions & 45 deletions packages/openapi-ts/src/plugins/@hey-api/sdk/shared/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const createRegistryClass = ({
f
.private()
.readonly()
.type($.type('Map').generics('string', 'T'))
// .type($.type('Map').generics('string', 'T'))
.assign($.new('Map')),
)
.newline()
Expand Down Expand Up @@ -132,12 +132,13 @@ const createClientClass = ({
.param('args', (p) =>
p
.optional(optionalClient)
.type()
.object((o) =>
o.prop('client', (p) =>
p.optional(optionalClient).type(symbolClient.placeholder),
),
),
// .type($.type
// .expr('todo')
// // .object()
// // .prop('client', (p) =>
// // p.optional(optionalClient).type(symbolClient.placeholder),
// // ),
// ),
)
.do(
$('this')
Expand Down Expand Up @@ -173,14 +174,14 @@ export const generateClassSdk = ({
context: plugin.context,
operation,
});
const symbolResponse = isNuxtClient
? plugin.querySymbol({
category: 'type',
resource: 'operation',
resourceId: operation.id,
role: 'response',
})
: undefined;
// const symbolResponse = isNuxtClient
// ? plugin.querySymbol({
// category: 'type',
// resource: 'operation',
// resourceId: operation.id,
// role: 'response',
// })
// : undefined;

const classes = operationClasses({
context: plugin.context,
Expand Down Expand Up @@ -266,28 +267,30 @@ export const generateClassSdk = ({
m
.generic(nuxtTypeComposable, (t) =>
t
.extends(
plugin.referenceSymbol({
category: 'external',
resource: 'client.Composable',
}).placeholder,
)
.default($.type.literal('$fetch')),
// .extends(
// plugin.referenceSymbol({
// category: 'external',
// resource: 'client.Composable',
// }).placeholder,
// )
// .default($.type.literal('$fetch')),
)
.generic(nuxtTypeDefault, (t) =>
t.$if(symbolResponse, (t, s) =>
t.extends(s.placeholder).default(s.placeholder),
),
t
// t.$if(symbolResponse, (t, s) =>
// t,
// // t.extends(s.placeholder).default(s.placeholder),
// ),
),
(m) =>
m.generic('ThrowOnError', (t) =>
t
.extends('boolean')
.default(
('throwOnError' in client.config
? client.config.throwOnError
: false) ?? false,
),
// .extends('boolean')
// .default(
// ('throwOnError' in client.config
// ? client.config.throwOnError
// : false) ?? false,
// ),
),
)
.params(...toParameterDeclarations(opParameters.parameters))
Expand Down Expand Up @@ -438,25 +441,21 @@ export const generateClassSdk = ({
category: 'client',
});
const isClientRequired = !plugin.config.client || !symClient;
const symbolClient = plugin.referenceSymbol({
category: 'external',
resource: 'client.Client',
});
// const symbolClient = plugin.referenceSymbol({
// category: 'external',
// resource: 'client.Client',
// });
const ctor = $.init((i) =>
i
.param('args', (p) =>
p
.optional(!isClientRequired)
.type()
.object((o) =>
o
.prop('client', (p) =>
p
.optional(!isClientRequired)
.type(symbolClient.placeholder),
)
.prop('key', (p) => p.optional().type('string')),
),
// .type($.type
// .expr('todo')
// // .object()
// // .prop('client', (p) => p.optional(!isClientRequired).type(symbolClient.placeholder))
// // .prop('key', (p) => p.optional().type('string')),
// )
)
.do(
$('super').call('args'),
Expand Down
27 changes: 10 additions & 17 deletions packages/openapi-ts/src/plugins/valibot/shared/export.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { Symbol } from '@hey-api/codegen-core';
import type ts from 'typescript';

import type { IR } from '~/ir/types';
import { createSchemaComment } from '~/plugins/shared/utils/schema';
import { tsc } from '~/tsc';
import { $ } from '~/ts-dsl';

import { identifiers } from '../v1/constants';
// import { identifiers } from '../v1/constants';
import { pipesToAst } from './pipesToAst';
import type { Ast, IrSchemaToAstOptions } from './types';

Expand All @@ -25,19 +24,13 @@ export const exportAst = ({
resource: 'valibot.v',
});

const statement = tsc.constVariable({
comment: plugin.config.comments
? createSchemaComment({ schema })
: undefined,
exportConst: symbol.exported,
expression: pipesToAst({ pipes: ast.pipes, plugin }),
name: symbol.placeholder,
typeName: state.hasLazyExpression.value
? (tsc.propertyAccessExpression({
expression: v.placeholder,
name: ast.typeName || identifiers.types.GenericSchema.text,
}) as unknown as ts.TypeNode)
: undefined,
});
const statement = $.const(symbol.placeholder)
.export(symbol.exported)
.$if(plugin.config.comments && createSchemaComment({ schema }), (c, v) => c.describe(v as Array<string>))
// .$if(state.hasLazyExpression.value, (c) =>
// // c.type($.type(v.placeholder))
// // c.type($.type(v.placeholder).attr(ast.typeName || identifiers.types.GenericSchema))
// )
.assign(pipesToAst({ pipes: ast.pipes, plugin }));
plugin.setSymbolValue(symbol, statement);
};
11 changes: 2 additions & 9 deletions packages/openapi-ts/src/plugins/valibot/shared/pipesToAst.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type ts from 'typescript';

import { tsc } from '~/tsc';
import { $ } from '~/ts-dsl';

import type { ValibotPlugin } from '../types';
import { identifiers } from '../v1/constants';
Expand All @@ -20,12 +20,5 @@ export const pipesToAst = ({
category: 'external',
resource: 'valibot.v',
});
const expression = tsc.callExpression({
functionName: tsc.propertyAccessExpression({
expression: v.placeholder,
name: identifiers.methods.pipe,
}),
parameters: pipes,
});
return expression;
return $(v.placeholder).attr(identifiers.methods.pipe).call(...pipes).$render();
};
57 changes: 34 additions & 23 deletions packages/openapi-ts/src/ts-dsl/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ export abstract class TsDsl<T extends ts.Node = ts.Node> implements ITsDsl<T> {
if (typeof input === 'string') {
return this.$expr(input) as NodeOfMaybe<I>;
}
if (typeof input === 'boolean') {
return (input ? ts.factory.createTrue() : ts.factory.createFalse()) as NodeOfMaybe<I>;
}
if (input instanceof Array) {
return input.map((item) => this._render(item)) as NodeOfMaybe<I>;
}
Expand All @@ -122,20 +125,21 @@ export abstract class TsDsl<T extends ts.Node = ts.Node> implements ITsDsl<T> {
});
}

protected $type<I>(type: I): TypeOfMaybe<I> {
if (type === undefined) {
protected $type<I>(input: I, args?: ReadonlyArray<ts.TypeNode>): TypeOfMaybe<I> {
if (input === undefined) {
return undefined as TypeOfMaybe<I>;
}
if (typeof type === 'string') {
return ts.factory.createTypeReferenceNode(
type,
) as unknown as TypeOfMaybe<I>;
if (typeof input === 'string') {
return ts.factory.createTypeReferenceNode(input, args) as TypeOfMaybe<I>;
}
if (typeof type === 'boolean') {
const literal = type ? ts.factory.createTrue() : ts.factory.createFalse();
if (typeof input === 'boolean') {
const literal = input ? ts.factory.createTrue() : ts.factory.createFalse();
return ts.factory.createLiteralTypeNode(literal) as TypeOfMaybe<I>;
}
return this._render(type as any) as TypeOfMaybe<I>;
if (input instanceof Array) {
return input.map((item) => this.$type(item, args)) as TypeOfMaybe<I>;
}
return this._render(input as any) as TypeOfMaybe<I>;
}

private _render<T extends ts.Node>(value: MaybeTsDsl<T>): T {
Expand All @@ -152,25 +156,30 @@ type NodeOf<I> =
? ReadonlyArray<U extends TsDsl<infer N> ? N : U>
: I extends string
? ts.Expression
: I extends TsDsl<infer N>
? N
: I extends ts.Node
? I
: never;
: I extends boolean
? ts.Expression
: I extends TsDsl<infer N>
? N
: I extends ts.Node
? I
: never;

type TypeOfMaybe<I> = undefined extends I
? TypeOf<NonNullable<I>> | undefined
: TypeOf<I>;

type TypeOf<I> = I extends string
? ts.TypeNode
: I extends boolean
? ts.LiteralTypeNode
: I extends TsDsl<infer N>
? N
: I extends ts.TypeNode
? I
: never;
type TypeOf<I> =
I extends ReadonlyArray<infer U>
? ReadonlyArray<TypeOf<U>>
: I extends string
? ts.TypeNode
: I extends boolean
? ts.LiteralTypeNode
: I extends TsDsl<infer N>
? N
: I extends ts.TypeNode
? I
: never;

export type MaybeTsDsl<T> =
// if T includes string in the union
Expand All @@ -182,3 +191,5 @@ export type MaybeTsDsl<T> =
T extends ts.Node
? T | TsDsl<T>
: never;

export type TypeOfTsDsl<T> = T extends TsDsl<infer U> ? U : never;
21 changes: 10 additions & 11 deletions packages/openapi-ts/src/ts-dsl/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import {
import { NewlineTsDsl } from './newline';

export class ClassTsDsl extends TsDsl<ts.ClassDeclaration> {
private heritageClauses: Array<ts.HeritageClause> = [];
private body: Array<MaybeTsDsl<ts.ClassElement> | NewlineTsDsl> = [];
private extended: Array<WithString> = [];
private modifiers = createModifierAccessor(this);
private name: string;

Expand All @@ -38,15 +38,7 @@ export class ClassTsDsl extends TsDsl<ts.ClassDeclaration> {

/** Adds a base class to extend from. */
extends(base?: WithString | false | null): this {
if (!base) return this;
this.heritageClauses.push(
ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [
ts.factory.createExpressionWithTypeArguments(
this.$expr(base),
undefined,
),
]),
);
if (base) this.extended.push(base);
return this;
}

Expand Down Expand Up @@ -84,7 +76,14 @@ export class ClassTsDsl extends TsDsl<ts.ClassDeclaration> {
[...this.$decorators(), ...this.modifiers.list()],
ts.factory.createIdentifier(this.name),
this.$generics(),
this.heritageClauses,
this.extended.map((extended) =>
ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [
ts.factory.createExpressionWithTypeArguments(
this.$expr(extended),
undefined,
),
])
),
body,
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/ts-dsl/expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { OperatorMixin } from './mixins/operator';
export class ExprTsDsl extends TsDsl<ts.Expression> {
private _exprInput: MaybeTsDsl<WithString>;

constructor(id: MaybeTsDsl<WithString>) {
constructor(expr: MaybeTsDsl<WithString>) {
super();
this._exprInput = id;
this._exprInput = expr;
}

$render(): ts.Expression {
Expand Down
8 changes: 4 additions & 4 deletions packages/openapi-ts/src/ts-dsl/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import {
ReadonlyMixin,
StaticMixin,
} from './mixins/modifiers';
import { createTypeAccessor, type TypeAccessor } from './mixins/type';
import { createType, type Type } from './mixins/type';
import { ValueMixin } from './mixins/value';

export class FieldTsDsl extends TsDsl<ts.PropertyDeclaration> {
private modifiers = createModifierAccessor(this);
private name: string;
private _type: TypeAccessor<FieldTsDsl> = createTypeAccessor(this);
private _type: Type<FieldTsDsl> = createType(this);

/** Sets the property's type. */
type: TypeAccessor<FieldTsDsl>['fn'] = this._type.fn;
type: Type<FieldTsDsl>['fn'] = this._type.fn;

constructor(name: string, fn?: (f: FieldTsDsl) => void) {
super();
Expand All @@ -36,7 +36,7 @@ export class FieldTsDsl extends TsDsl<ts.PropertyDeclaration> {
[...this.$decorators(), ...this.modifiers.list()],
ts.factory.createIdentifier(this.name),
undefined,
this._type.$render(),
this.$type(this._type),
this.$value(),
);
}
Expand Down
Loading
Loading