@kubb/plugin-ts 3.0.0-alpha.9 → 3.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -5
- package/dist/{chunk-UVAKTXUA.js → chunk-AG2BUY33.js} +86 -398
- package/dist/chunk-AG2BUY33.js.map +1 -0
- package/dist/chunk-L7QZ6DBL.cjs +332 -0
- package/dist/chunk-L7QZ6DBL.cjs.map +1 -0
- package/dist/chunk-QZ2DBYXR.js +303 -0
- package/dist/chunk-QZ2DBYXR.js.map +1 -0
- package/dist/{chunk-PV5Q7I5O.cjs → chunk-XMTK3ZZE.cjs} +185 -470
- package/dist/chunk-XMTK3ZZE.cjs.map +1 -0
- package/dist/components.cjs +11 -12
- package/dist/components.cjs.map +1 -1
- package/dist/components.d.cts +14 -44
- package/dist/components.d.ts +14 -44
- package/dist/components.js +2 -16
- package/dist/components.js.map +1 -1
- package/dist/generators.cjs +17 -0
- package/dist/generators.cjs.map +1 -0
- package/dist/generators.d.cts +10 -0
- package/dist/generators.d.ts +10 -0
- package/dist/generators.js +4 -0
- package/dist/generators.js.map +1 -0
- package/dist/index.cjs +12 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -123
- package/dist/index.d.ts +4 -123
- package/dist/index.js +3 -9
- package/dist/index.js.map +1 -1
- package/dist/types-DoCOabdx.d.cts +98 -0
- package/dist/types-DoCOabdx.d.ts +98 -0
- package/package.json +21 -15
- package/src/components/OasType.tsx +3 -50
- package/src/components/{Schema.tsx → Type.tsx} +19 -54
- package/src/components/index.ts +1 -2
- package/src/generators/__snapshots__/bodyUploadFileApiAssetsPost.ts +6 -0
- package/src/generators/__snapshots__/catTypeAsConst.ts +11 -0
- package/src/generators/__snapshots__/createPet.ts +41 -0
- package/src/generators/__snapshots__/createPetWithUnknownTypeUnknown.ts +41 -0
- package/src/generators/__snapshots__/deletePet.ts +5 -0
- package/src/generators/__snapshots__/dogTypeAsConst.ts +11 -0
- package/src/generators/__snapshots__/enumAllOf.ts +10 -0
- package/src/generators/__snapshots__/enumArray.ts +19 -0
- package/src/generators/__snapshots__/enumInObject.ts +13 -0
- package/src/generators/__snapshots__/enumItems.ts +8 -0
- package/src/generators/__snapshots__/enumNames.ts +6 -0
- package/src/generators/__snapshots__/enumNamesConst.ts +6 -0
- package/src/generators/__snapshots__/enumNamesLiteral.ts +3 -0
- package/src/generators/__snapshots__/enumNamesPascalConst.ts +8 -0
- package/src/generators/__snapshots__/enumNamesType.ts +8 -0
- package/src/generators/__snapshots__/enumNullableMember.ts +8 -0
- package/src/generators/__snapshots__/enumNullableType.ts +8 -0
- package/src/generators/__snapshots__/enumString.ts +10 -0
- package/src/generators/__snapshots__/enumVarNamesType.ts +8 -0
- package/src/generators/__snapshots__/getPets.ts +27 -0
- package/src/generators/__snapshots__/mixedValueTypeConst.ts +9 -0
- package/src/generators/__snapshots__/nullConstNull.ts +7 -0
- package/src/generators/__snapshots__/numberValueConst.ts +9 -0
- package/src/generators/__snapshots__/petMapper.ts +15 -0
- package/src/generators/__snapshots__/petQuestionToken.ts +19 -0
- package/src/generators/__snapshots__/petQuestionTokenAndUndefined.ts +19 -0
- package/src/generators/__snapshots__/petUndefined.ts +19 -0
- package/src/generators/__snapshots__/pets.ts +14 -0
- package/src/generators/__snapshots__/petsStoreAdvancedDiscriminator.ts +31 -0
- package/src/generators/__snapshots__/petsStoreDiscriminator.ts +13 -0
- package/src/generators/__snapshots__/petsStoreRef.ts +1 -0
- package/src/generators/__snapshots__/plainDateDate.ts +1 -0
- package/src/generators/__snapshots__/plainDateString.ts +1 -0
- package/src/generators/__snapshots__/plainEmail.ts +1 -0
- package/src/generators/__snapshots__/plainFile.ts +1 -0
- package/src/generators/__snapshots__/plainTimeDate.ts +1 -0
- package/src/generators/__snapshots__/plainUuid.ts +1 -0
- package/src/generators/__snapshots__/readOnly.ts +20 -0
- package/src/{components/__snapshots__/Schema → generators/__snapshots__}/showPetById.ts +9 -9
- package/src/generators/__snapshots__/stringValueConst.ts +9 -0
- package/src/generators/index.ts +2 -0
- package/src/generators/oasGenerator.tsx +29 -0
- package/src/generators/typeGenerator.tsx +200 -0
- package/src/parser/index.ts +12 -3
- package/src/plugin.ts +26 -33
- package/src/types.ts +16 -52
- package/dist/chunk-PV5Q7I5O.cjs.map +0 -1
- package/dist/chunk-SEH6NUCX.cjs +0 -42
- package/dist/chunk-SEH6NUCX.cjs.map +0 -1
- package/dist/chunk-TTGZBH7H.js +0 -42
- package/dist/chunk-TTGZBH7H.js.map +0 -1
- package/dist/chunk-UVAKTXUA.js.map +0 -1
- package/dist/oas.cjs +0 -7
- package/dist/oas.cjs.map +0 -1
- package/dist/oas.d.cts +0 -1
- package/dist/oas.d.ts +0 -1
- package/dist/oas.js +0 -7
- package/dist/oas.js.map +0 -1
- package/src/OperationGenerator.tsx +0 -49
- package/src/SchemaGenerator.tsx +0 -31
- package/src/components/OperationSchema.tsx +0 -161
- package/src/components/__snapshots__/Schema/pets.ts +0 -27
- package/src/oas/index.ts +0 -1
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
|
|
2
|
+
export const advancedTypeEnum2 = {
|
|
3
|
+
"enum": "enum",
|
|
4
|
+
"range": "range",
|
|
5
|
+
"regex": "regex",
|
|
6
|
+
"slider": "slider"
|
|
7
|
+
} as const;
|
|
8
|
+
|
|
9
|
+
export type AdvancedTypeEnum2 = (typeof advancedTypeEnum2)[keyof typeof advancedTypeEnum2];
|
|
10
|
+
|
|
11
|
+
export type advanced = ((enumerationValueSpecificationDto & {
|
|
12
|
+
/**
|
|
13
|
+
* @type string
|
|
14
|
+
*/
|
|
15
|
+
readonly type: AdvancedTypeEnum2;
|
|
16
|
+
}) | (rangeValueSpecificationDto & {
|
|
17
|
+
/**
|
|
18
|
+
* @type string
|
|
19
|
+
*/
|
|
20
|
+
readonly type: AdvancedTypeEnum2;
|
|
21
|
+
}) | (regexValueSpecificationDto & {
|
|
22
|
+
/**
|
|
23
|
+
* @type string
|
|
24
|
+
*/
|
|
25
|
+
readonly type: AdvancedTypeEnum2;
|
|
26
|
+
}) | (sliderValueSpecificationDto & {
|
|
27
|
+
/**
|
|
28
|
+
* @type string
|
|
29
|
+
*/
|
|
30
|
+
readonly type: AdvancedTypeEnum2;
|
|
31
|
+
}));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type pets = pet[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type plainDate = Date;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type plainDate = string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type plainEmail = string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type plainFile = Blob;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type plainTime = Date;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type plainUuid = string;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
|
|
2
|
+
export type demo = {
|
|
3
|
+
/**
|
|
4
|
+
* @type integer | undefined
|
|
5
|
+
*/
|
|
6
|
+
readonly attribute_readonly?: number;
|
|
7
|
+
/**
|
|
8
|
+
* @description not required
|
|
9
|
+
* @type integer | undefined
|
|
10
|
+
*/
|
|
11
|
+
attribute_writeOnly?: number;
|
|
12
|
+
/**
|
|
13
|
+
* @type integer | undefined
|
|
14
|
+
*/
|
|
15
|
+
readonly attribute_with_ref?: attributeReadonly;
|
|
16
|
+
/**
|
|
17
|
+
* @type integer | undefined
|
|
18
|
+
*/
|
|
19
|
+
attribute_with_ref_readonly?: attributeWriteOnly;
|
|
20
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type
|
|
1
|
+
export type ShowPetByIdPathParams = {
|
|
2
2
|
/**
|
|
3
3
|
* @description The id of the pet to retrieve
|
|
4
4
|
* @type string
|
|
@@ -11,22 +11,22 @@ export type showPetByIdPathParams = {
|
|
|
11
11
|
testId: string;
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
-
/**
|
|
14
|
+
/**
|
|
15
15
|
* @description Expected response to a valid request
|
|
16
16
|
*/
|
|
17
|
-
export type
|
|
17
|
+
export type ShowPetById200 = pet;
|
|
18
18
|
|
|
19
|
-
/**
|
|
19
|
+
/**
|
|
20
20
|
* @description unexpected error
|
|
21
21
|
*/
|
|
22
|
-
export type
|
|
22
|
+
export type ShowPetByIdError = error;
|
|
23
23
|
|
|
24
|
-
/**
|
|
24
|
+
/**
|
|
25
25
|
* @description Expected response to a valid request
|
|
26
26
|
*/
|
|
27
|
-
export type
|
|
27
|
+
export type ShowPetByIdQueryResponse = pet;
|
|
28
28
|
|
|
29
|
-
export type
|
|
29
|
+
export type showPetByIdQuery = {
|
|
30
30
|
Response: showPetByIdQueryResponse;
|
|
31
31
|
PathParams: showPetByIdPathParams;
|
|
32
|
-
};
|
|
32
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { createReactGenerator } from '@kubb/plugin-oas'
|
|
2
|
+
import { useOas } from '@kubb/plugin-oas/hooks'
|
|
3
|
+
import { File, useApp } from '@kubb/react'
|
|
4
|
+
import { OasType } from '../components'
|
|
5
|
+
import type { PluginTs } from '../types.ts'
|
|
6
|
+
|
|
7
|
+
export const oasGenerator = createReactGenerator<PluginTs>({
|
|
8
|
+
name: 'oas',
|
|
9
|
+
Operations() {
|
|
10
|
+
const {
|
|
11
|
+
pluginManager,
|
|
12
|
+
plugin: {
|
|
13
|
+
options: { output },
|
|
14
|
+
key: pluginKey,
|
|
15
|
+
},
|
|
16
|
+
} = useApp<PluginTs>()
|
|
17
|
+
const oas = useOas()
|
|
18
|
+
|
|
19
|
+
const file = pluginManager.getFile({ name: 'oas', extname: '.ts', pluginKey })
|
|
20
|
+
|
|
21
|
+
return (
|
|
22
|
+
<File baseName={file.baseName} path={file.path} meta={file.meta} banner={output?.banner} footer={output?.footer}>
|
|
23
|
+
<File.Import name={['Infer']} path="@kubb/oas" isTypeOnly />
|
|
24
|
+
|
|
25
|
+
<OasType name={'oas'} typeName={'Oas'} api={oas.api} />
|
|
26
|
+
</File>
|
|
27
|
+
)
|
|
28
|
+
},
|
|
29
|
+
})
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import type { PluginManager } from '@kubb/core'
|
|
2
|
+
import transformers from '@kubb/core/transformers'
|
|
3
|
+
import { print } from '@kubb/parser-ts'
|
|
4
|
+
import * as factory from '@kubb/parser-ts/factory'
|
|
5
|
+
import { type OperationSchema as OperationSchemaType, type OperationSchemas, SchemaGenerator, createReactGenerator, schemaKeywords } from '@kubb/plugin-oas'
|
|
6
|
+
import { Oas } from '@kubb/plugin-oas/components'
|
|
7
|
+
import { useOas, useOperationManager, useSchemaManager } from '@kubb/plugin-oas/hooks'
|
|
8
|
+
import { pluginTsName } from '@kubb/plugin-ts'
|
|
9
|
+
import { File, useApp } from '@kubb/react'
|
|
10
|
+
import type ts from 'typescript'
|
|
11
|
+
import { Type } from '../components'
|
|
12
|
+
import type { PluginTs } from '../types'
|
|
13
|
+
|
|
14
|
+
function printCombinedSchema({ name, schemas, pluginManager }: { name: string; schemas: OperationSchemas; pluginManager: PluginManager }): string {
|
|
15
|
+
const properties: Record<string, ts.TypeNode> = {}
|
|
16
|
+
|
|
17
|
+
if (schemas.response) {
|
|
18
|
+
const identifier = pluginManager.resolveName({
|
|
19
|
+
name: schemas.response.name,
|
|
20
|
+
pluginKey: [pluginTsName],
|
|
21
|
+
type: 'function',
|
|
22
|
+
})
|
|
23
|
+
properties['response'] = factory.createTypeReferenceNode(factory.createIdentifier(identifier), undefined)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (schemas.request) {
|
|
27
|
+
const identifier = pluginManager.resolveName({
|
|
28
|
+
name: schemas.request.name,
|
|
29
|
+
pluginKey: [pluginTsName],
|
|
30
|
+
type: 'function',
|
|
31
|
+
})
|
|
32
|
+
properties['request'] = factory.createTypeReferenceNode(factory.createIdentifier(identifier), undefined)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (schemas.pathParams) {
|
|
36
|
+
const identifier = pluginManager.resolveName({
|
|
37
|
+
name: schemas.pathParams.name,
|
|
38
|
+
pluginKey: [pluginTsName],
|
|
39
|
+
type: 'function',
|
|
40
|
+
})
|
|
41
|
+
properties['pathParams'] = factory.createTypeReferenceNode(factory.createIdentifier(identifier), undefined)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (schemas.queryParams) {
|
|
45
|
+
const identifier = pluginManager.resolveName({
|
|
46
|
+
name: schemas.queryParams.name,
|
|
47
|
+
pluginKey: [pluginTsName],
|
|
48
|
+
type: 'function',
|
|
49
|
+
})
|
|
50
|
+
properties['queryParams'] = factory.createTypeReferenceNode(factory.createIdentifier(identifier), undefined)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (schemas.headerParams) {
|
|
54
|
+
const identifier = pluginManager.resolveName({
|
|
55
|
+
name: schemas.headerParams.name,
|
|
56
|
+
pluginKey: [pluginTsName],
|
|
57
|
+
type: 'function',
|
|
58
|
+
})
|
|
59
|
+
properties['headerParams'] = factory.createTypeReferenceNode(factory.createIdentifier(identifier), undefined)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (schemas.errors) {
|
|
63
|
+
properties['errors'] = factory.createUnionDeclaration({
|
|
64
|
+
nodes: schemas.errors.map((error) => {
|
|
65
|
+
const identifier = pluginManager.resolveName({
|
|
66
|
+
name: error.name,
|
|
67
|
+
pluginKey: [pluginTsName],
|
|
68
|
+
type: 'function',
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
return factory.createTypeReferenceNode(factory.createIdentifier(identifier), undefined)
|
|
72
|
+
}),
|
|
73
|
+
})!
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const namespaceNode = factory.createTypeAliasDeclaration({
|
|
77
|
+
name,
|
|
78
|
+
type: factory.createTypeLiteralNode(
|
|
79
|
+
Object.keys(properties)
|
|
80
|
+
.map((key) => {
|
|
81
|
+
const type = properties[key]
|
|
82
|
+
if (!type) {
|
|
83
|
+
return undefined
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return factory.createPropertySignature({
|
|
87
|
+
name: transformers.pascalCase(key),
|
|
88
|
+
type,
|
|
89
|
+
})
|
|
90
|
+
})
|
|
91
|
+
.filter(Boolean),
|
|
92
|
+
),
|
|
93
|
+
modifiers: [factory.modifiers.export],
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
return print([namespaceNode])
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export const typeGenerator = createReactGenerator<PluginTs>({
|
|
100
|
+
name: 'typescript',
|
|
101
|
+
Operation({ operation, options }) {
|
|
102
|
+
const { mapper, enumType, optionalType } = options
|
|
103
|
+
|
|
104
|
+
const { plugin, pluginManager, mode } = useApp<PluginTs>()
|
|
105
|
+
const oas = useOas()
|
|
106
|
+
const { getSchemas, getFile, getName } = useOperationManager()
|
|
107
|
+
const schemaManager = useSchemaManager()
|
|
108
|
+
|
|
109
|
+
const file = getFile(operation)
|
|
110
|
+
const schemas = getSchemas(operation)
|
|
111
|
+
const type = getName(operation, { type: 'function', pluginKey: [pluginTsName] })
|
|
112
|
+
const combinedSchemaName = operation.method === 'get' ? `${type}Query` : `${type}Mutation`
|
|
113
|
+
const schemaGenerator = new SchemaGenerator(options, {
|
|
114
|
+
oas,
|
|
115
|
+
plugin,
|
|
116
|
+
pluginManager,
|
|
117
|
+
mode,
|
|
118
|
+
override: options.override,
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
const operationSchemas = [schemas.pathParams, schemas.queryParams, schemas.headerParams, schemas.statusCodes, schemas.request, schemas.response]
|
|
122
|
+
.flat()
|
|
123
|
+
.filter(Boolean)
|
|
124
|
+
|
|
125
|
+
const mapOperationSchema = ({ name, schema, description, keysToOmit, ...options }: OperationSchemaType, i: number) => {
|
|
126
|
+
// hack so Params can be optional when needed
|
|
127
|
+
const tree = schemaGenerator.parse({ schema, name })
|
|
128
|
+
const imports = schemaManager.getImports(tree)
|
|
129
|
+
|
|
130
|
+
const type = {
|
|
131
|
+
name: schemaManager.getName(name, { type: 'type' }),
|
|
132
|
+
typedName: schemaManager.getName(name, { type: 'type' }),
|
|
133
|
+
file: schemaManager.getFile(options.operationName || name, { tag: options.operation?.getTags()[0]?.name }),
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return (
|
|
137
|
+
<Oas.Schema key={i} name={name} value={schema} tree={tree}>
|
|
138
|
+
{mode === 'split' && imports.map((imp, index) => <File.Import key={index} root={file.path} path={imp.path} name={imp.name} isTypeOnly />)}
|
|
139
|
+
<Type
|
|
140
|
+
name={type.name}
|
|
141
|
+
typedName={type.typedName}
|
|
142
|
+
description={description}
|
|
143
|
+
tree={tree}
|
|
144
|
+
mapper={mapper}
|
|
145
|
+
enumType={enumType}
|
|
146
|
+
optionalType={optionalType}
|
|
147
|
+
keysToOmit={keysToOmit}
|
|
148
|
+
/>
|
|
149
|
+
</Oas.Schema>
|
|
150
|
+
)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return (
|
|
154
|
+
<File baseName={file.baseName} path={file.path} meta={file.meta} banner={plugin.options.output?.banner} footer={plugin.options.output?.footer}>
|
|
155
|
+
{operationSchemas.map(mapOperationSchema)}
|
|
156
|
+
|
|
157
|
+
<File.Source name={combinedSchemaName} isExportable isIndexable isTypeOnly>
|
|
158
|
+
{printCombinedSchema({ name: combinedSchemaName, schemas, pluginManager })}
|
|
159
|
+
</File.Source>
|
|
160
|
+
</File>
|
|
161
|
+
)
|
|
162
|
+
},
|
|
163
|
+
Schema({ schema, options }) {
|
|
164
|
+
const { mapper, enumType, optionalType } = options
|
|
165
|
+
const {
|
|
166
|
+
mode,
|
|
167
|
+
plugin: {
|
|
168
|
+
options: { output },
|
|
169
|
+
},
|
|
170
|
+
} = useApp<PluginTs>()
|
|
171
|
+
|
|
172
|
+
const { getName, getImports, getFile } = useSchemaManager()
|
|
173
|
+
const imports = getImports(schema.tree)
|
|
174
|
+
|
|
175
|
+
if (enumType === 'asPascalConst') {
|
|
176
|
+
console.warn(`enumType '${enumType}' is deprecated`)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const type = {
|
|
180
|
+
name: getName(schema.name, { type: 'function' }),
|
|
181
|
+
typedName: getName(schema.name, { type: 'type' }),
|
|
182
|
+
file: getFile(schema.name),
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return (
|
|
186
|
+
<File baseName={type.file.baseName} path={type.file.path} meta={type.file.meta} banner={output?.banner} footer={output?.footer}>
|
|
187
|
+
{mode === 'split' && imports.map((imp, index) => <File.Import key={index} root={type.file.path} path={imp.path} name={imp.name} isTypeOnly />)}
|
|
188
|
+
<Type
|
|
189
|
+
name={type.name}
|
|
190
|
+
typedName={type.typedName}
|
|
191
|
+
description={schema.value.description}
|
|
192
|
+
tree={schema.tree}
|
|
193
|
+
mapper={mapper}
|
|
194
|
+
enumType={enumType}
|
|
195
|
+
optionalType={optionalType}
|
|
196
|
+
/>
|
|
197
|
+
</File>
|
|
198
|
+
)
|
|
199
|
+
},
|
|
200
|
+
})
|
package/src/parser/index.ts
CHANGED
|
@@ -2,8 +2,8 @@ import transformers from '@kubb/core/transformers'
|
|
|
2
2
|
import * as factory from '@kubb/parser-ts/factory'
|
|
3
3
|
import { isKeyword, schemaKeywords } from '@kubb/plugin-oas'
|
|
4
4
|
|
|
5
|
-
import type { ts } from '@kubb/parser-ts'
|
|
6
5
|
import type { Schema, SchemaKeywordMapper, SchemaMapper } from '@kubb/plugin-oas'
|
|
6
|
+
import type ts from 'typescript'
|
|
7
7
|
|
|
8
8
|
export const typeKeywordMapper = {
|
|
9
9
|
any: () => factory.keywordTypeNodes.any,
|
|
@@ -54,12 +54,20 @@ export const typeKeywordMapper = {
|
|
|
54
54
|
nodes,
|
|
55
55
|
})
|
|
56
56
|
},
|
|
57
|
-
const: (name?: string | number, format?: 'string' | 'number') => {
|
|
57
|
+
const: (name?: string | number | boolean, format?: 'string' | 'number' | 'boolean') => {
|
|
58
58
|
if (!name) {
|
|
59
59
|
return undefined
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
if (format === '
|
|
62
|
+
if (format === 'boolean') {
|
|
63
|
+
if (name === true) {
|
|
64
|
+
return factory.createLiteralTypeNode(factory.createTrue())
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return factory.createLiteralTypeNode(factory.createFalse())
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (format === 'number' && typeof name === 'number') {
|
|
63
71
|
return factory.createLiteralTypeNode(factory.createNumericLiteral(name))
|
|
64
72
|
}
|
|
65
73
|
|
|
@@ -95,6 +103,7 @@ export const typeKeywordMapper = {
|
|
|
95
103
|
password: undefined,
|
|
96
104
|
phone: undefined,
|
|
97
105
|
readOnly: undefined,
|
|
106
|
+
writeOnly: undefined,
|
|
98
107
|
ref: (propertyName?: string) => {
|
|
99
108
|
if (!propertyName) {
|
|
100
109
|
return undefined
|
package/src/plugin.ts
CHANGED
|
@@ -1,22 +1,20 @@
|
|
|
1
1
|
import path from 'node:path'
|
|
2
2
|
|
|
3
|
-
import { FileManager, PluginManager, createPlugin } from '@kubb/core'
|
|
3
|
+
import { FileManager, type Group, PluginManager, createPlugin } from '@kubb/core'
|
|
4
4
|
import { camelCase, pascalCase } from '@kubb/core/transformers'
|
|
5
5
|
import { renderTemplate } from '@kubb/core/utils'
|
|
6
|
-
import { pluginOasName } from '@kubb/plugin-oas'
|
|
7
|
-
|
|
8
|
-
import { OperationGenerator } from './OperationGenerator.tsx'
|
|
9
|
-
import { SchemaGenerator } from './SchemaGenerator.tsx'
|
|
6
|
+
import { OperationGenerator, SchemaGenerator, pluginOasName } from '@kubb/plugin-oas'
|
|
10
7
|
|
|
11
8
|
import type { Plugin } from '@kubb/core'
|
|
12
9
|
import type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'
|
|
10
|
+
import { oasGenerator, typeGenerator } from './generators'
|
|
13
11
|
import type { PluginTs } from './types.ts'
|
|
14
12
|
|
|
15
13
|
export const pluginTsName = 'plugin-ts' satisfies PluginTs['name']
|
|
16
14
|
|
|
17
15
|
export const pluginTs = createPlugin<PluginTs>((options) => {
|
|
18
16
|
const {
|
|
19
|
-
output = { path: 'types' },
|
|
17
|
+
output = { path: 'types', barrelType: 'named' },
|
|
20
18
|
group,
|
|
21
19
|
exclude = [],
|
|
22
20
|
include,
|
|
@@ -29,17 +27,13 @@ export const pluginTs = createPlugin<PluginTs>((options) => {
|
|
|
29
27
|
transformers = {},
|
|
30
28
|
oasType = false,
|
|
31
29
|
mapper = {},
|
|
30
|
+
generators = [typeGenerator, oasType === 'infer' ? oasGenerator : undefined].filter(Boolean),
|
|
32
31
|
} = options
|
|
33
|
-
const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`
|
|
34
32
|
|
|
35
33
|
return {
|
|
36
34
|
name: pluginTsName,
|
|
37
|
-
output: {
|
|
38
|
-
exportType: 'barrelNamed',
|
|
39
|
-
...output,
|
|
40
|
-
},
|
|
41
35
|
options: {
|
|
42
|
-
|
|
36
|
+
output,
|
|
43
37
|
transformers,
|
|
44
38
|
dateType,
|
|
45
39
|
optionalType,
|
|
@@ -57,6 +51,12 @@ export const pluginTs = createPlugin<PluginTs>((options) => {
|
|
|
57
51
|
const root = path.resolve(this.config.root, this.config.output.path)
|
|
58
52
|
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
|
|
59
53
|
|
|
54
|
+
if (options?.tag && group?.type === 'tag') {
|
|
55
|
+
const groupName: Group['name'] = group?.name ? group.name : (ctx) => `${ctx.group}Controller`
|
|
56
|
+
|
|
57
|
+
return path.resolve(root, output.path, groupName({ group: camelCase(options.tag) }), baseName)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
60
|
if (mode === 'single') {
|
|
61
61
|
/**
|
|
62
62
|
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
|
|
@@ -65,12 +65,6 @@ export const pluginTs = createPlugin<PluginTs>((options) => {
|
|
|
65
65
|
return path.resolve(root, output.path)
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
if (options?.tag && group?.type === 'tag') {
|
|
69
|
-
const tag = camelCase(options.tag)
|
|
70
|
-
|
|
71
|
-
return path.resolve(root, renderTemplate(template, { tag }), baseName)
|
|
72
|
-
}
|
|
73
|
-
|
|
74
68
|
return path.resolve(root, output.path, baseName)
|
|
75
69
|
},
|
|
76
70
|
resolveName(name, type) {
|
|
@@ -100,7 +94,7 @@ export const pluginTs = createPlugin<PluginTs>((options) => {
|
|
|
100
94
|
output: output.path,
|
|
101
95
|
})
|
|
102
96
|
|
|
103
|
-
const schemaFiles = await schemaGenerator.build()
|
|
97
|
+
const schemaFiles = await schemaGenerator.build(...generators)
|
|
104
98
|
await this.addFile(...schemaFiles)
|
|
105
99
|
|
|
106
100
|
const operationGenerator = new OperationGenerator(this.plugin.options, {
|
|
@@ -114,22 +108,21 @@ export const pluginTs = createPlugin<PluginTs>((options) => {
|
|
|
114
108
|
mode,
|
|
115
109
|
})
|
|
116
110
|
|
|
117
|
-
const operationFiles = await operationGenerator.build()
|
|
111
|
+
const operationFiles = await operationGenerator.build(...generators)
|
|
118
112
|
await this.addFile(...operationFiles)
|
|
119
113
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
114
|
+
const barrelFiles = await this.fileManager.getBarrelFiles({
|
|
115
|
+
type: output.barrelType ?? 'named',
|
|
116
|
+
root,
|
|
117
|
+
output,
|
|
118
|
+
files: this.fileManager.files,
|
|
119
|
+
meta: {
|
|
120
|
+
pluginKey: this.plugin.key,
|
|
121
|
+
},
|
|
122
|
+
logger: this.logger,
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
await this.addFile(...barrelFiles)
|
|
133
126
|
},
|
|
134
127
|
}
|
|
135
128
|
})
|
package/src/types.ts
CHANGED
|
@@ -1,47 +1,17 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type
|
|
3
|
-
import type
|
|
4
|
-
import type { Exclude, Include, Override, ResolvePathOptions } from '@kubb/plugin-oas'
|
|
1
|
+
import type { Group, Output, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
|
|
2
|
+
import type { Exclude, Generator, Include, Override, ResolvePathOptions } from '@kubb/plugin-oas'
|
|
3
|
+
import type ts from 'typescript'
|
|
5
4
|
|
|
6
5
|
export type Options = {
|
|
7
|
-
output?: {
|
|
8
|
-
/**
|
|
9
|
-
* Relative path to save the TypeScript types.
|
|
10
|
-
* When output is a file it will save all models inside that file else it will create a file per schema item.
|
|
11
|
-
* @default 'types'
|
|
12
|
-
*/
|
|
13
|
-
path: string
|
|
14
|
-
/**
|
|
15
|
-
* Name to be used for the `export * as {{exportAs}} from './'`
|
|
16
|
-
*/
|
|
17
|
-
exportAs?: string
|
|
18
|
-
/**
|
|
19
|
-
* Add an extension to the generated imports and exports, default it will not use an extension
|
|
20
|
-
*/
|
|
21
|
-
extName?: KubbFile.Extname
|
|
22
|
-
/**
|
|
23
|
-
* Define what needs to exported, here you can also disable the export of barrel files
|
|
24
|
-
* @default `'barrel'`
|
|
25
|
-
*/
|
|
26
|
-
exportType?: 'barrel' | 'barrelNamed' | false
|
|
27
|
-
}
|
|
28
6
|
/**
|
|
29
|
-
*
|
|
7
|
+
* Specify the export location for the files and define the behavior of the output
|
|
8
|
+
* @default { path: 'types', barrelType: 'named' }
|
|
30
9
|
*/
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Relative path to save the grouped TypeScript Types.
|
|
38
|
-
*
|
|
39
|
-
* `{{tag}}` will be replaced by the current tagName.
|
|
40
|
-
* @example `${output}/{{tag}}Controller` => `models/PetController`
|
|
41
|
-
* @default `${output}/{{tag}}Controller`
|
|
42
|
-
*/
|
|
43
|
-
output?: string
|
|
44
|
-
}
|
|
10
|
+
output?: Output
|
|
11
|
+
/**
|
|
12
|
+
* Group the clients based on the provided name.
|
|
13
|
+
*/
|
|
14
|
+
group?: Group
|
|
45
15
|
/**
|
|
46
16
|
* Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
|
|
47
17
|
*/
|
|
@@ -66,7 +36,6 @@ export type Options = {
|
|
|
66
36
|
* Default will be `'enum'` in version 3 of Kubb
|
|
67
37
|
*/
|
|
68
38
|
enumSuffix?: string
|
|
69
|
-
|
|
70
39
|
/**
|
|
71
40
|
* Choose to use `date` or `datetime` as JavaScript `Date` instead of `string`.
|
|
72
41
|
* @default 'string'
|
|
@@ -106,29 +75,24 @@ export type Options = {
|
|
|
106
75
|
* )
|
|
107
76
|
*/
|
|
108
77
|
mapper?: Record<string, ts.PropertySignature>
|
|
78
|
+
/**
|
|
79
|
+
* Define some generators next to the ts generators
|
|
80
|
+
*/
|
|
81
|
+
generators?: Array<Generator<PluginTs>>
|
|
109
82
|
}
|
|
110
83
|
|
|
111
84
|
type ResolvedOptions = {
|
|
112
|
-
|
|
85
|
+
output: Output
|
|
86
|
+
override: NonNullable<Options['override']>
|
|
113
87
|
enumType: NonNullable<Options['enumType']>
|
|
114
88
|
enumSuffix: NonNullable<Options['enumSuffix']>
|
|
115
89
|
dateType: NonNullable<Options['dateType']>
|
|
116
90
|
unknownType: NonNullable<Options['unknownType']>
|
|
117
91
|
optionalType: NonNullable<Options['optionalType']>
|
|
118
|
-
override: NonNullable<Options['override']>
|
|
119
92
|
transformers: NonNullable<Options['transformers']>
|
|
120
93
|
oasType: NonNullable<Options['oasType']>
|
|
121
94
|
usedEnumNames: Record<string, number>
|
|
122
95
|
mapper: Record<string, any>
|
|
123
96
|
}
|
|
124
97
|
|
|
125
|
-
export type FileMeta = {
|
|
126
|
-
pluginKey?: Plugin['key']
|
|
127
|
-
name?: string
|
|
128
|
-
tag?: string
|
|
129
|
-
}
|
|
130
|
-
|
|
131
98
|
export type PluginTs = PluginFactoryOptions<'plugin-ts', Options, ResolvedOptions, never, ResolvePathOptions>
|
|
132
|
-
|
|
133
|
-
// external packages
|
|
134
|
-
export * as Oas from './oas/index.ts'
|