Skip to content

Commit

Permalink
feat: improve function parameter reusage
Browse files Browse the repository at this point in the history
  • Loading branch information
plantain-00 committed Jan 3, 2022
1 parent 1217ff0 commit 9799a83
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 11 deletions.
1 change: 0 additions & 1 deletion demo/cases.gql
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ type TypeIntersection2 {
type TypeIntersection {
typeIntersectionMember1: TypeIntersection1!
typeIntersectionMember2: TypeIntersection2!
typeIntersectionMember3: JSON!
}

type TypeUnionAndIntersection {
Expand Down
11 changes: 10 additions & 1 deletion demo/cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,16 @@
"typeIntersectionMember2": {
"$ref": "#/definitions/TypeIntersection2"
},
"typeIntersectionMember3": {}
"typeIntersectionMember3": {
"anyOf": [
{
"$ref": "#/definitions/TypeLiteral"
},
{
"$ref": "#/definitions/Interface"
}
]
}
},
"required": [
"typeIntersectionMember1",
Expand Down
2 changes: 1 addition & 1 deletion demo/cases.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Field | Required | Type | Description
--- | --- | --- | ---
`typeIntersectionMember1` | `true` | [`TypeIntersection1`](#TypeIntersection1) |
`typeIntersectionMember2` | `true` | [`TypeIntersection2`](#TypeIntersection2) |
`typeIntersectionMember3` | `true` | `unknown` |
`typeIntersectionMember3` | `true` | [`TypeLiteral`](#TypeLiteral) | [`Interface`](#Interface) |

## `TypeUnionAndIntersection`

Expand Down
21 changes: 21 additions & 0 deletions demo/debug.json
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,27 @@
{
"name": "typeIntersectionMember3",
"type": {
"kind": "union",
"members": [
{
"kind": "reference",
"name": "TypeLiteral",
"position": {
"file": "demo/cases.ts",
"line": 80,
"character": 27
}
},
{
"kind": "reference",
"name": "Interface",
"position": {
"file": "demo/cases.ts",
"line": 80,
"character": 41
}
}
],
"position": {
"file": "demo/cases.ts",
"line": 80,
Expand Down
2 changes: 1 addition & 1 deletion demo/root-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export interface TypeIntersection2 {
export interface TypeIntersection {
typeIntersectionMember1: TypeIntersection1
typeIntersectionMember2: TypeIntersection2
typeIntersectionMember3: any
typeIntersectionMember3: TypeLiteral | Interface
}

export interface TypeUnionAndIntersection {
Expand Down
2 changes: 1 addition & 1 deletion demo/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ interface TypeIntersection2 {
interface TypeIntersection {
typeIntersectionMember1: TypeIntersection1
typeIntersectionMember2: TypeIntersection2
typeIntersectionMember3: unknown
typeIntersectionMember3: TypeLiteral | Interface
}

interface TypeUnionAndIntersection {
Expand Down
1 change: 1 addition & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,4 @@ export class Generator {
export * from './utils'
export * from './typescript-generator'
export * from './json-schema-generator'
export * from './swagger-doc-generator'
6 changes: 3 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,8 @@ export class Parser {
if (ts.isTypeReferenceNode(type)) {
return this.getTypeOfTypeReference(type, sourceFile)
}
if (ts.isUnionTypeNode(type)) {
return this.getTypeOfUnionType(type, sourceFile)
if (ts.isUnionTypeNode(type) || ts.isIntersectionTypeNode(type)) {
return this.getTypeOfUnionTypeOrIntersectionType(type, sourceFile)
}
if (ts.isLiteralTypeNode(type)) {
if (type.literal.kind === ts.SyntaxKind.NullKeyword) {
Expand Down Expand Up @@ -712,7 +712,7 @@ export class Parser {
}
}

private getTypeOfUnionType(unionType: ts.UnionTypeNode, sourceFile: ts.SourceFile): Type {
private getTypeOfUnionTypeOrIntersectionType(unionType: ts.UnionTypeNode | ts.IntersectionTypeNode, sourceFile: ts.SourceFile): Type {
if (unionType.types.every(u => ts.isLiteralTypeNode(u))) {
let enumType: EnumValueType | undefined
const enums: unknown[] = []
Expand Down
58 changes: 55 additions & 3 deletions src/swagger-doc-generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, getReferencesInType } from './utils'
import { Context, FunctionDeclaration, FunctionParameter, getReferencesInType, TypeDeclaration } from './utils'
import { getAllDefinitions, getReferencedDefinitions, Definition, getJsonSchemaProperty } from './json-schema-generator'

export function generateSwaggerDoc(context: Context, swaggerBase?: Record<string, unknown>) {
Expand All @@ -12,11 +12,12 @@ export function generateSwaggerDoc(context: Context, swaggerBase?: Record<string
paths[typeDeclaration.path] = {}
}
referenceNames.push(...getReferencesInType(typeDeclaration.type).map((r) => r.name))
const useFormData = typeDeclaration.parameters.some((p) => p.type.kind === 'file')
const declarationParameters = getDeclarationParameters(typeDeclaration, context.declarations)
const useFormData = declarationParameters.some((p) => p.type.kind === 'file')
paths[typeDeclaration.path]![typeDeclaration.method] = {
consumes: useFormData ? ['multipart/form-data'] : undefined,
operationId: typeDeclaration.name,
parameters: typeDeclaration.parameters.map((parameter) => {
parameters: declarationParameters.map((parameter) => {
referenceNames.push(...getReferencesInType(parameter.type).map((r) => r.name))
const schema = getJsonSchemaProperty(parameter.type, context)
return {
Expand Down Expand Up @@ -55,3 +56,54 @@ export function generateSwaggerDoc(context: Context, swaggerBase?: Record<string
}
return JSON.stringify(result, null, 2)
}

const allTypes = ['path', 'query', 'body']

/**
* @public
*/
export function getDeclarationParameters(
declaration: FunctionDeclaration,
typeDeclarations: TypeDeclaration[],
) {
const result: FunctionParameter[] = []
for (const parameter of declaration.parameters) {
if (!parameter.in && allTypes.includes(parameter.name)) {
const parameterType = parameter.type
if (parameterType.kind === 'reference') {
const typeDeclaration = typeDeclarations.find((d) => d.name === parameterType.name)
if (typeDeclaration && typeDeclaration.kind === 'object') {
result.push(...typeDeclaration.members.map((m) => ({
...m,
in: parameter.name,
})))
}
} else if (parameterType.kind === 'union') {
for (const member of parameterType.members) {
if (member.kind === 'reference') {
const typeDeclaration = typeDeclarations.find((d) => d.name === member.name)
if (typeDeclaration && typeDeclaration.kind === 'object') {
result.push(...typeDeclaration.members.map((m) => ({
...m,
in: parameter.name,
})))
}
} else if (member.kind === 'object') {
result.push(...member.members.map((m) => ({
...m,
in: parameter.name,
})))
}
}
} else if (parameterType.kind === 'object') {
result.push(...parameterType.members.map((m) => ({
...m,
in: parameter.name,
})))
}
} else {
result.push(parameter)
}
}
return result
}

0 comments on commit 9799a83

Please sign in to comment.