Skip to content

Commit

Permalink
feat: change typings type and add OpenAPIBuildConfigurationReadV2
Browse files Browse the repository at this point in the history
  • Loading branch information
hairyf committed Feb 23, 2023
1 parent 9849a3b commit 417eaff
Show file tree
Hide file tree
Showing 32 changed files with 125 additions and 324 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@
"unconfig": "^0.3.7"
},
"devDependencies": {
"ptsup": "^0.3.1",
"@types/lodash": "^4.14.191",
"@antfu/eslint-config": "^0.35.2",
"@types/fs-extra": "^11.0.1",
"@types/lodash": "^4.14.191",
"@types/node": "^18.14.0",
"@types/prettier": "^2.7.2",
"bumpp": "^8.2.1",
"esbuild": "^0.17.10",
"esbuild-register": "^3.4.2",
"eslint": "^8.34.0"
"eslint": "^8.34.0",
"openapi-specification-types": "^0.0.1",
"ptsup": "^0.3.1"
}
}
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/helper/compiler/extra/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import { StatementFiled } from './types'
* @param name
* @param value
*/
export function createVariable(flag: ts.NodeFlags, name: string, value: string) {
export function createVariable(flag: ts.NodeFlags, name: string, value?: string) {
return factory.createVariableDeclarationList(
[factory.createVariableDeclaration(
factory.createIdentifier(name),
undefined,
undefined,
factory.createIdentifier(value),
value ? factory.createIdentifier(value) : undefined,
)],
flag,
) as any as ts.TypeAliasDeclaration
Expand Down
4 changes: 2 additions & 2 deletions src/helper/compiler/extra/function.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ts, { factory } from 'typescript'
import type { FunctionOptions, StatementFiled } from './types'
import type { StatementFunction, StatementFiled } from './types'
import {createMultilineComment} from './comment'

/**
Expand All @@ -8,7 +8,7 @@ import {createMultilineComment} from './comment'
* @param o
* @returns
*/
export function createFunction(o: FunctionOptions) {
export function createFunction(o: StatementFunction) {
const exportModifier = factory.createModifier(ts.SyntaxKind.ExportKeyword)
const functionName = factory.createIdentifier(o.name)
const parameters = o.parameters?.map(createParameter) || []
Expand Down
4 changes: 2 additions & 2 deletions src/helper/compiler/extra/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export * from './object'
export type {
LiteralFiled,
StatementFiled,
FunctionOptions,
InterfaceOptions,
StatementFunction,
StatementInterface,
} from './types'
4 changes: 2 additions & 2 deletions src/helper/compiler/extra/interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ts, { factory } from 'typescript'
import { createMultilineComment } from './comment'
import type { InterfaceOptions, StatementFiled } from './types'
import type { StatementInterface, StatementFiled } from './types'

patchInterfaceComment()

Expand All @@ -10,7 +10,7 @@ patchInterfaceComment()
* @param o
* @returns
*/
export function createInterface(o: InterfaceOptions) {
export function createInterface(o: StatementInterface) {
const exportModifier = factory.createModifier(ts.SyntaxKind.ExportKeyword)
const interfaceName = factory.createIdentifier(o.name)
const properties: ts.PropertySignature[] = o.properties.flatMap((item) => {
Expand Down
16 changes: 14 additions & 2 deletions src/helper/compiler/extra/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ts from "typescript"

export interface FunctionOptions {
export interface StatementFunction {
/**
* function name
*/
Expand All @@ -23,7 +23,7 @@ export interface FunctionOptions {
comment?: string | string[]
}

export interface InterfaceOptions {
export interface StatementInterface {
/**
* interface name
*/
Expand All @@ -45,6 +45,18 @@ export interface StatementFiled {
description?: string | string[]
}

export interface StatementImported {
name?: string
names?: string
value: string
}

export interface StatementVariable {
flag: ts.NodeFlags
name: string
value?: string
}

/**
* @example 'a' > { a }
* @example ['a', 'b'] > { a: b }
Expand Down
22 changes: 11 additions & 11 deletions src/helper/compiler/utils/ts-function.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import ts from 'typescript'
import type { MethodFunctionOptions } from '../../typings/parser'
import type { MethodStatementFunction } from '../../typings/parser'
import { createFunction, createVariable } from '../extra'
import { createRequest } from './ts-request'

export function createMethodFunction(o: MethodFunctionOptions) {
export function createMethodFunction(o: MethodStatementFunction) {

const nodes = createFunction({
name: o.name,
export: true,
comment: o.comment,
parameters: o.parameters,
body: [
const nodes = createFunction(
true,
o.comment,
o.name,
o.parameters,
[
createVariable(ts.NodeFlags.Const, 'url', o.url),
createRequest(o.httpImport.name, o.responseType, ['url', ['method', `"${o.method}"`], ...o.options]),
createRequest(o.lib, o.responseType, ['url', ['method', `"${o.method}"`], ...o.options]),
]
})

)
return nodes
}
2 changes: 1 addition & 1 deletion src/helper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export type {
OpenAPIBuildConfigurationRead,
OpenAPIBuildConfigurationServers,
OpenAPIDefineConfig,
} from './typings/generator.d'
} from './typings/generator'
2 changes: 1 addition & 1 deletion src/helper/parser/helper/parameter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { StatementFiled } from '../../typings/parser'
import { spliceEnumDescription, spliceEnumType } from '../helper/util'
import { helperPropertie } from '../helper/propertie'
import type * as OpenAPITypes from '../../typings/OpenAPI-Specification'
import type * as OpenAPITypes from 'openapi-specification-types'

export function helperParameter(parameter: OpenAPITypes.Parameter): StatementFiled {
const filed: StatementFiled = {
Expand Down
2 changes: 1 addition & 1 deletion src/helper/parser/helper/propertie.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type * as OpenAPITypes from 'openapi-specification-types'
import isArray from 'lodash/isArray'
import isEmpty from 'lodash/isEmpty'

import { useRefMap, varName } from '../helper/util'
import type * as OpenAPITypes from '../../typings/OpenAPI-Specification'
import * as extra from '../../compiler/extra'

export function helperPropertie(propertie: OpenAPITypes.Schema): string {
Expand Down
4 changes: 2 additions & 2 deletions src/helper/parser/helper/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import forIn from 'lodash/forIn'
import { transliterate } from 'transliteration'
import { pascalCase } from 'pascal-case'
import type { LiteralFiled, StatementFiled } from '../../typings/parser'
import type * as OpenAPITypes from '../../typings/OpenAPI-Specification'
import type * as OpenAPITypes from 'openapi-specification-types'

export interface TraverseParametersOptions {
path: string
Expand Down Expand Up @@ -142,7 +142,7 @@ export function signAnyInter(filed: StatementFiled[]) {
* @returns
*/
export function isRequiredParameter(filed: StatementFiled[]) {
return filed.some(({ type, required, name }) => required && !name.startsWith('[') && !type.endsWith('any'))
return filed.some(({ type, required, name }) => required && !name.startsWith('[') && !type?.endsWith('any'))
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/helper/parser/op-json.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import camelCase from 'lodash/camelCase'
import forIn from 'lodash/forIn'
import type { ParserRequestOptions, ParserTypingsOptions } from '../typings/parser'
import type * as OpenAPITypes from '../typings/OpenAPI-Specification'
import type * as OpenAPITypes from 'openapi-specification-types'
import type { OpenAPIBuildConfigurationRead } from '../typings/generator'
import { helperPropertie } from './helper/propertie'
import { helperParameter } from './helper/parameter'
Expand All @@ -19,7 +19,7 @@ import {
const FORM_DATA_FILED = { name: 'data', type: 'FormData', required: true }

class OpenAPI_JSONParserFactory {
private $source: OpenAPITypes.OpenAPISourceV2Json
private $source: OpenAPITypes.OpenAPISpecificationV2
private $options: OpenAPIBuildConfigurationRead
private $parser!: {
request: ParserRequestOptions
Expand All @@ -30,7 +30,7 @@ class OpenAPI_JSONParserFactory {
return this.$parser
}

constructor(data: OpenAPITypes.OpenAPISourceV2Json, options: OpenAPIBuildConfigurationRead) {
constructor(data: OpenAPITypes.OpenAPISpecificationV2, options: OpenAPIBuildConfigurationRead) {
this.$source = data
this.$options = options
this.transformAstOptions()
Expand Down Expand Up @@ -147,7 +147,7 @@ class OpenAPI_JSONParserFactory {
/** @转换Ast基础配置 */
private transformNameSpace() {
this.$parser.request.functions.forEach((item) => {
item.parameters.forEach(item => (item.type = this.helperType(item.type)))
item.parameters.forEach(item => (item.type = this.helperType(item.type!)))
item.responseType = this.helperType(item.responseType)
item.responseType = this.helperTypeResponse(item.responseType)
})
Expand Down
2 changes: 0 additions & 2 deletions src/helper/typings/OpenAPI-Specification/common.d.ts

This file was deleted.

52 changes: 0 additions & 52 deletions src/helper/typings/OpenAPI-Specification/components.d.ts

This file was deleted.

28 changes: 0 additions & 28 deletions src/helper/typings/OpenAPI-Specification/definitions.d.ts

This file was deleted.

30 changes: 0 additions & 30 deletions src/helper/typings/OpenAPI-Specification/index-v2.d.ts

This file was deleted.

25 changes: 0 additions & 25 deletions src/helper/typings/OpenAPI-Specification/index-v3.d.ts

This file was deleted.

Loading

0 comments on commit 417eaff

Please sign in to comment.