这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
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
6 changes: 3 additions & 3 deletions dev/openapi-ts.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,9 @@ export default defineConfig(() => {
},
{
name: 'arktype',
// types: {
// infer: true,
// },
types: {
infer: true,
},
},
{
// case: 'SCREAMING_SNAKE_CASE',
Expand Down
148 changes: 73 additions & 75 deletions packages/openapi-ts/src/plugins/arktype/constants.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,64 @@
import { tsc } from '~/tsc';

export const identifiers = {
keywords: {
false: tsc.identifier({ text: 'false' }),
true: tsc.identifier({ text: 'true' }),
false: 'false',
true: 'true',
},
/**
* {@link https://arktype.io/docs/primitives#number Number}
*/
number: {
Infinity: tsc.identifier({ text: 'Infinity' }),
NaN: tsc.identifier({ text: 'NaN' }),
NegativeInfinity: tsc.identifier({ text: 'NegativeInfinity' }),
epoch: tsc.identifier({ text: 'epoch' }),
integer: tsc.identifier({ text: 'integer' }),
safe: tsc.identifier({ text: 'safe' }),
Infinity: 'Infinity',
NaN: 'NaN',
NegativeInfinity: 'NegativeInfinity',
epoch: 'epoch',
integer: 'integer',
safe: 'safe',
},
/**
* {@link https://arktype.io/docs/primitives Primitives}
*/
primitives: {
bigint: tsc.identifier({ text: 'bigint' }),
boolean: tsc.identifier({ text: 'boolean' }),
keywords: tsc.identifier({ text: 'keywords' }),
bigint: 'bigint',
boolean: 'boolean',
keywords: 'keywords',
null: 'null',
number: tsc.identifier({ text: 'number' }),
number: 'number',
string: 'string',
symbol: tsc.identifier({ text: 'symbol' }),
undefined: tsc.identifier({ text: 'undefined' }),
unit: tsc.identifier({ text: 'unit' }),
symbol: 'symbol',
undefined: 'undefined',
unit: 'unit',
},
/**
* {@link https://arktype.io/docs/primitives#string String}
*/
string: {
NFC: tsc.identifier({ text: 'NFC' }),
NFD: tsc.identifier({ text: 'NFD' }),
NFKC: tsc.identifier({ text: 'NFKC' }),
NFKD: tsc.identifier({ text: 'NFKD' }),
alpha: tsc.identifier({ text: 'alpha' }),
alphanumeric: tsc.identifier({ text: 'alphanumeric' }),
base64: tsc.identifier({ text: 'base64' }),
capitalize: tsc.identifier({ text: 'capitalize' }),
creditCard: tsc.identifier({ text: 'creditCard' }),
NFC: 'NFC',
NFD: 'NFD',
NFKC: 'NFKC',
NFKD: 'NFKD',
alpha: 'alpha',
alphanumeric: 'alphanumeric',
base64: 'base64',
capitalize: 'capitalize',
creditCard: 'creditCard',
date: 'date',
digits: tsc.identifier({ text: 'digits' }),
digits: 'digits',
email: 'email',
epoch: tsc.identifier({ text: 'epoch' }),
hex: tsc.identifier({ text: 'hex' }),
integer: tsc.identifier({ text: 'integer' }),
epoch: 'epoch',
hex: 'hex',
integer: 'integer',
ip: 'ip',
iso: 'iso',
json: tsc.identifier({ text: 'json' }),
lower: tsc.identifier({ text: 'lower' }),
normalize: tsc.identifier({ text: 'normalize' }),
numeric: tsc.identifier({ text: 'numeric' }),
parse: tsc.identifier({ text: 'parse' }),
preformatted: tsc.identifier({ text: 'preformatted' }),
regex: tsc.identifier({ text: 'regex' }),
semver: tsc.identifier({ text: 'semver' }),
trim: tsc.identifier({ text: 'trim' }),
upper: tsc.identifier({ text: 'upper' }),
json: 'json',
lower: 'lower',
normalize: 'normalize',
numeric: 'numeric',
parse: 'parse',
preformatted: 'preformatted',
regex: 'regex',
semver: 'semver',
trim: 'trim',
upper: 'upper',
url: 'url',
uuid: 'uuid',
v1: 'v1',
Expand All @@ -76,40 +74,40 @@ export const identifiers = {
* {@link https://arktype.io/docs/type-api Type API}
*/
type: {
$: tsc.identifier({ text: '$' }),
allows: tsc.identifier({ text: 'allows' }),
and: tsc.identifier({ text: 'and' }),
array: tsc.identifier({ text: 'array' }),
as: tsc.identifier({ text: 'as' }),
assert: tsc.identifier({ text: 'assert' }),
brand: tsc.identifier({ text: 'brand' }),
configure: tsc.identifier({ text: 'configure' }),
default: tsc.identifier({ text: 'default' }),
describe: tsc.identifier({ text: 'describe' }),
description: tsc.identifier({ text: 'description' }),
equals: tsc.identifier({ text: 'equals' }),
exclude: tsc.identifier({ text: 'exclude' }),
expression: tsc.identifier({ text: 'expression' }),
extends: tsc.identifier({ text: 'extends' }),
extract: tsc.identifier({ text: 'extract' }),
filter: tsc.identifier({ text: 'filter' }),
from: tsc.identifier({ text: 'from' }),
ifEquals: tsc.identifier({ text: 'ifEquals' }),
ifExtends: tsc.identifier({ text: 'ifExtends' }),
infer: tsc.identifier({ text: 'infer' }),
inferIn: tsc.identifier({ text: 'inferIn' }),
intersect: tsc.identifier({ text: 'intersect' }),
json: tsc.identifier({ text: 'json' }),
meta: tsc.identifier({ text: 'meta' }),
narrow: tsc.identifier({ text: 'narrow' }),
onDeepUndeclaredKey: tsc.identifier({ text: 'onDeepUndeclaredKey' }),
onUndeclaredKey: tsc.identifier({ text: 'onUndeclaredKey' }),
optional: tsc.identifier({ text: 'optional' }),
or: tsc.identifier({ text: 'or' }),
overlaps: tsc.identifier({ text: 'overlaps' }),
pipe: tsc.identifier({ text: 'pipe' }),
select: tsc.identifier({ text: 'select' }),
to: tsc.identifier({ text: 'to' }),
toJsonSchema: tsc.identifier({ text: 'toJsonSchema' }),
$: '$',
allows: 'allows',
and: 'and',
array: 'array',
as: 'as',
assert: 'assert',
brand: 'brand',
configure: 'configure',
default: 'default',
describe: 'describe',
description: 'description',
equals: 'equals',
exclude: 'exclude',
expression: 'expression',
extends: 'extends',
extract: 'extract',
filter: 'filter',
from: 'from',
ifEquals: 'ifEquals',
ifExtends: 'ifExtends',
infer: 'infer',
inferIn: 'inferIn',
intersect: 'intersect',
json: 'json',
meta: 'meta',
narrow: 'narrow',
onDeepUndeclaredKey: 'onDeepUndeclaredKey',
onUndeclaredKey: 'onUndeclaredKey',
optional: 'optional',
or: 'or',
overlaps: 'overlaps',
pipe: 'pipe',
select: 'select',
to: 'to',
toJsonSchema: 'toJsonSchema',
},
};
54 changes: 23 additions & 31 deletions packages/openapi-ts/src/plugins/arktype/shared/export.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { Symbol } from '@hey-api/codegen-core';
import 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 '../constants';
import type { ArktypePlugin } from '../types';
Expand All @@ -27,38 +26,31 @@ export const exportAst = ({
resource: 'arktype.type',
});

const statement = tsc.constVariable({
comment: plugin.config.comments
? createSchemaComment({ schema })
: undefined,
exportConst: symbol.exported,
expression: tsc.callExpression({
functionName: type.placeholder,
parameters: [
ast.def ? tsc.stringLiteral({ text: ast.def }) : ast.expression,
],
}),
name: symbol.placeholder,
// typeName: ast.typeName
// ? (tsc.propertyAccessExpression({
// expression: z.placeholder,
// name: ast.typeName,
// }) as unknown as ts.TypeNode)
// : undefined,
});
const statement = $.const(symbol.placeholder)
.export(symbol.exported)
.$if(plugin.config.comments && createSchemaComment({ schema }), (c, v) =>
c.doc(v as ReadonlyArray<string>),
)
// .type(
// ast.typeName
// ? (tsc.propertyAccessExpression({
// expression: z.placeholder,
// name: ast.typeName,
// }) as unknown as ts.TypeNode)
// : undefined,
// )
.assign(
$(type.placeholder).call(ast.def ? $.literal(ast.def) : ast.expression),
);
plugin.setSymbolValue(symbol, statement);

if (typeInferSymbol) {
const inferType = tsc.typeAliasDeclaration({
exportType: typeInferSymbol.exported,
name: typeInferSymbol.placeholder,
type: ts.factory.createTypeQueryNode(
ts.factory.createQualifiedName(
ts.factory.createIdentifier(symbol.placeholder),
identifiers.type.infer,
),
),
});
const inferType = $.type
.alias(typeInferSymbol.placeholder)
.export(typeInferSymbol.exported)
.type(
$.type(symbol.placeholder).attr(identifiers.type.infer).typeofType(),
);
plugin.setSymbolValue(typeInferSymbol, inferType);
}
};
3 changes: 2 additions & 1 deletion packages/openapi-ts/src/plugins/arktype/shared/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import type ts from 'typescript';

import type { IR } from '~/ir/types';
import type { ToRefs } from '~/plugins';
import type { $ } from '~/ts-dsl';

import type { ArktypePlugin } from '../types';

export type Ast = {
def: string;
expression: ts.Expression;
expression: ReturnType<typeof $.call | typeof $.expr | typeof $.object>;
hasLazyExpression?: boolean;
typeName?: string | ts.Identifier;
};
Expand Down
27 changes: 7 additions & 20 deletions packages/openapi-ts/src/plugins/arktype/v2/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { IR } from '~/ir/types';
import { buildName } from '~/openApi/shared/utils/name';
import type { SchemaWithType } from '~/plugins/shared/types/schema';
import { toRefs } from '~/plugins/shared/utils/refs';
import { tsc } from '~/tsc';
import { $ } from '~/ts-dsl';
import { pathToJsonPointer, refToName } from '~/utils/ref';

import { exportAst } from '../shared/export';
Expand Down Expand Up @@ -43,27 +43,14 @@ export const irSchemaToAst = ({
};
const refSymbol = plugin.referenceSymbol(query);
if (plugin.isSymbolRegistered(query)) {
const ref = tsc.identifier({ text: refSymbol.placeholder });
const ref = $(refSymbol.placeholder);
ast.expression = ref;
} else {
const lazyExpression = tsc.callExpression({
functionName: tsc.propertyAccessExpression({
// expression: z.placeholder,
expression: 'TODO',
name: 'TODO',
// name: identifiers.lazy,
}),
parameters: [
tsc.arrowFunction({
returnType: tsc.keywordTypeNode({ keyword: 'any' }),
statements: [
tsc.returnStatement({
expression: tsc.identifier({ text: refSymbol.placeholder }),
}),
],
}),
],
});
// expression: z.placeholder,
// name: identifiers.lazy,
const lazyExpression = $('TODO')
.attr('TODO')
.call($.func().returns('any').do($.return(refSymbol.placeholder)));
ast.expression = lazyExpression;
ast.hasLazyExpression = true;
state.hasLazyExpression.value = true;
Expand Down
32 changes: 6 additions & 26 deletions packages/openapi-ts/src/plugins/arktype/v2/toAst/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ts from 'typescript';

import type { SchemaWithType } from '~/plugins/shared/types/schema';
import { $ } from '~/ts-dsl';

import type { Ast, IrSchemaToAstOptions } from '../../shared/types';
import { nullToAst } from './null';
Expand Down Expand Up @@ -91,30 +90,11 @@ export const irSchemaWithTypeToAst = ({
resource: 'arktype.type',
});

const expression = ts.factory.createCallExpression(
ts.factory.createIdentifier(type.placeholder),
undefined,
[
ts.factory.createObjectLiteralExpression(
[
ts.factory.createPropertyAssignment(
'name',
ts.factory.createStringLiteral('string'),
),
ts.factory.createPropertyAssignment(
'platform',
ts.factory.createStringLiteral("'android' | 'ios'"),
),
ts.factory.createPropertyAssignment(
ts.factory.createComputedPropertyName(
ts.factory.createStringLiteral('versions?'),
),
ts.factory.createStringLiteral('(number | string)[]'),
),
],
true,
),
],
const expression = $(type.placeholder).call(
$.object()
.prop('name', $.literal('string'))
.prop('platform', $.literal("'android' | 'ios'"))
.computed('versions?', $.literal('(number | string)[]')),
);

return {
Expand Down
Loading
Loading