@kubb/swagger-ts 2.0.0-beta.1 → 2.0.0-beta.11

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.
Files changed (44) hide show
  1. package/dist/components.cjs +3993 -0
  2. package/dist/components.cjs.map +1 -0
  3. package/dist/components.d.cts +68 -0
  4. package/dist/components.d.ts +68 -0
  5. package/dist/components.js +3969 -0
  6. package/dist/components.js.map +1 -0
  7. package/dist/index-PYW2PpJw.d.cts +392 -0
  8. package/dist/index-PYW2PpJw.d.ts +392 -0
  9. package/dist/index.cjs +3553 -289
  10. package/dist/index.cjs.map +1 -1
  11. package/dist/index.d.cts +9 -80
  12. package/dist/index.d.ts +9 -80
  13. package/dist/index.js +3555 -291
  14. package/dist/index.js.map +1 -1
  15. package/dist/oas.cjs +4 -0
  16. package/dist/oas.cjs.map +1 -0
  17. package/dist/oas.d.cts +6 -0
  18. package/dist/oas.d.ts +6 -0
  19. package/dist/oas.js +5 -0
  20. package/dist/oas.js.map +1 -0
  21. package/dist/types-IAThMYCO.d.cts +105 -0
  22. package/dist/types-IAThMYCO.d.ts +105 -0
  23. package/package.json +22 -8
  24. package/src/OperationGenerator.tsx +62 -0
  25. package/src/TypeBuilder.ts +58 -0
  26. package/src/{generators/TypeGenerator.ts → TypeGenerator.ts} +78 -68
  27. package/src/components/Mutation.tsx +137 -0
  28. package/src/components/Oas.tsx +84 -0
  29. package/src/components/Query.tsx +136 -0
  30. package/src/components/index.ts +3 -0
  31. package/src/index.ts +1 -1
  32. package/src/oas/index.ts +7 -0
  33. package/src/oas/infer.ts +58 -0
  34. package/src/oas/mappers.ts +93 -0
  35. package/src/oas/model.ts +38 -0
  36. package/src/oas/requestParams.ts +170 -0
  37. package/src/oas/response.ts +39 -0
  38. package/src/oas/security.ts +158 -0
  39. package/src/plugin.ts +52 -107
  40. package/src/types.ts +41 -13
  41. package/src/builders/TypeBuilder.ts +0 -94
  42. package/src/builders/index.ts +0 -1
  43. package/src/generators/OperationGenerator.ts +0 -213
  44. package/src/generators/index.ts +0 -2
package/dist/oas.cjs ADDED
@@ -0,0 +1,4 @@
1
+ 'use strict';
2
+
3
+ //# sourceMappingURL=out.js.map
4
+ //# sourceMappingURL=oas.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
package/dist/oas.d.cts ADDED
@@ -0,0 +1,6 @@
1
+ export { I as Infer, M as MethodMap, a as Model, P as PathMap, R as RequestParams, b as Response, S as StatusMap } from './index-PYW2PpJw.cjs';
2
+ import 'hotscript';
3
+ import 'ts-toolbelt';
4
+ import '@kubb/swagger';
5
+ import 'json-schema-to-ts';
6
+ import '@kubb/types';
package/dist/oas.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export { I as Infer, M as MethodMap, a as Model, P as PathMap, R as RequestParams, b as Response, S as StatusMap } from './index-PYW2PpJw.js';
2
+ import 'hotscript';
3
+ import 'ts-toolbelt';
4
+ import '@kubb/swagger';
5
+ import 'json-schema-to-ts';
6
+ import '@kubb/types';
package/dist/oas.js ADDED
@@ -0,0 +1,5 @@
1
+ import { createRequire } from 'module';
2
+
3
+ createRequire(import.meta.url);
4
+ //# sourceMappingURL=out.js.map
5
+ //# sourceMappingURL=oas.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","sourcesContent":[]}
@@ -0,0 +1,105 @@
1
+ import { KubbFile, ResolveNameParams, KubbPlugin, PluginFactoryOptions } from '@kubb/core';
2
+ import { Exclude, Include, Override, ResolvePathOptions, AppMeta as AppMeta$1 } from '@kubb/swagger';
3
+
4
+ type Options = {
5
+ output?: {
6
+ /**
7
+ * Relative path to save the TypeScript types.
8
+ * When output is a file it will save all models inside that file else it will create a file per schema item.
9
+ * @default 'types'
10
+ */
11
+ path: string;
12
+ /**
13
+ * Name to be used for the `export * as {{exportAs}} from './'`
14
+ */
15
+ exportAs?: string;
16
+ /**
17
+ * Add an extension to the generated imports and exports, default it will not use an extension
18
+ */
19
+ extName?: KubbFile.Extname;
20
+ /**
21
+ * Define what needs to exported, here you can also disable the export of barrel files
22
+ * @default `'barrel'`
23
+ */
24
+ exportType?: 'barrel' | false;
25
+ };
26
+ /**
27
+ * Group the TypeScript types based on the provided name.
28
+ */
29
+ group?: {
30
+ /**
31
+ * Tag will group based on the operation tag inside the Swagger file.
32
+ */
33
+ type: 'tag';
34
+ /**
35
+ * Relative path to save the grouped TypeScript Types.
36
+ *
37
+ * `{{tag}}` will be replaced by the current tagName.
38
+ * @example `${output}/{{tag}}Controller` => `models/PetController`
39
+ * @default `${output}/{{tag}}Controller`
40
+ */
41
+ output?: string;
42
+ };
43
+ /**
44
+ * Array containing exclude paramaters to exclude/skip tags/operations/methods/paths.
45
+ */
46
+ exclude?: Array<Exclude>;
47
+ /**
48
+ * Array containing include paramaters to include tags/operations/methods/paths.
49
+ */
50
+ include?: Array<Include>;
51
+ /**
52
+ * Array containing override paramaters to override `options` based on tags/operations/methods/paths.
53
+ */
54
+ override?: Array<Override<Options>>;
55
+ /**
56
+ * Choose to use `enum` or `as const` for enums
57
+ * @default 'asConst'
58
+ */
59
+ enumType?: 'enum' | 'asConst' | 'asPascalConst';
60
+ /**
61
+ * Choose to use `date` or `datetime` as JavaScript `Date` instead of `string`.
62
+ * @default 'string'
63
+ */
64
+ dateType?: 'string' | 'date';
65
+ /**
66
+ * Choose what to use as mode for an optional value.
67
+ * @examples 'questionToken': type?: string
68
+ * @examples 'undefined': type: string | undefined
69
+ * @examples 'questionTokenAndUndefined': type?: string | undefined
70
+ * @default 'questionToken'
71
+ */
72
+ optionalType?: 'questionToken' | 'undefined' | 'questionTokenAndUndefined';
73
+ transformers?: {
74
+ /**
75
+ * Customize the names based on the type that is provided by the plugin.
76
+ */
77
+ name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string;
78
+ };
79
+ /**
80
+ * Export an Oas object as Oas type with `import type { Infer } from '@kubb/swagger-ts/oas'`
81
+ */
82
+ oasType?: 'infer' | false;
83
+ };
84
+ type ResolvedOptions = {
85
+ enumType: NonNullable<Options['enumType']>;
86
+ dateType: NonNullable<Options['dateType']>;
87
+ optionalType: NonNullable<Options['optionalType']>;
88
+ transformers: NonNullable<Options['transformers']>;
89
+ oasType: NonNullable<Options['oasType']>;
90
+ usedEnumNames: Record<string, number>;
91
+ };
92
+ type FileMeta = {
93
+ pluginKey?: KubbPlugin['key'];
94
+ name?: string;
95
+ tag?: string;
96
+ };
97
+ type AppMeta = AppMeta$1;
98
+ type PluginOptions = PluginFactoryOptions<'swagger-ts', Options, ResolvedOptions, never, ResolvePathOptions, AppMeta>;
99
+ declare module '@kubb/core' {
100
+ interface _Register {
101
+ ['@kubb/swagger-ts']: PluginOptions;
102
+ }
103
+ }
104
+
105
+ export type { FileMeta as F, Options as O, PluginOptions as P };
@@ -0,0 +1,105 @@
1
+ import { KubbFile, ResolveNameParams, KubbPlugin, PluginFactoryOptions } from '@kubb/core';
2
+ import { Exclude, Include, Override, ResolvePathOptions, AppMeta as AppMeta$1 } from '@kubb/swagger';
3
+
4
+ type Options = {
5
+ output?: {
6
+ /**
7
+ * Relative path to save the TypeScript types.
8
+ * When output is a file it will save all models inside that file else it will create a file per schema item.
9
+ * @default 'types'
10
+ */
11
+ path: string;
12
+ /**
13
+ * Name to be used for the `export * as {{exportAs}} from './'`
14
+ */
15
+ exportAs?: string;
16
+ /**
17
+ * Add an extension to the generated imports and exports, default it will not use an extension
18
+ */
19
+ extName?: KubbFile.Extname;
20
+ /**
21
+ * Define what needs to exported, here you can also disable the export of barrel files
22
+ * @default `'barrel'`
23
+ */
24
+ exportType?: 'barrel' | false;
25
+ };
26
+ /**
27
+ * Group the TypeScript types based on the provided name.
28
+ */
29
+ group?: {
30
+ /**
31
+ * Tag will group based on the operation tag inside the Swagger file.
32
+ */
33
+ type: 'tag';
34
+ /**
35
+ * Relative path to save the grouped TypeScript Types.
36
+ *
37
+ * `{{tag}}` will be replaced by the current tagName.
38
+ * @example `${output}/{{tag}}Controller` => `models/PetController`
39
+ * @default `${output}/{{tag}}Controller`
40
+ */
41
+ output?: string;
42
+ };
43
+ /**
44
+ * Array containing exclude paramaters to exclude/skip tags/operations/methods/paths.
45
+ */
46
+ exclude?: Array<Exclude>;
47
+ /**
48
+ * Array containing include paramaters to include tags/operations/methods/paths.
49
+ */
50
+ include?: Array<Include>;
51
+ /**
52
+ * Array containing override paramaters to override `options` based on tags/operations/methods/paths.
53
+ */
54
+ override?: Array<Override<Options>>;
55
+ /**
56
+ * Choose to use `enum` or `as const` for enums
57
+ * @default 'asConst'
58
+ */
59
+ enumType?: 'enum' | 'asConst' | 'asPascalConst';
60
+ /**
61
+ * Choose to use `date` or `datetime` as JavaScript `Date` instead of `string`.
62
+ * @default 'string'
63
+ */
64
+ dateType?: 'string' | 'date';
65
+ /**
66
+ * Choose what to use as mode for an optional value.
67
+ * @examples 'questionToken': type?: string
68
+ * @examples 'undefined': type: string | undefined
69
+ * @examples 'questionTokenAndUndefined': type?: string | undefined
70
+ * @default 'questionToken'
71
+ */
72
+ optionalType?: 'questionToken' | 'undefined' | 'questionTokenAndUndefined';
73
+ transformers?: {
74
+ /**
75
+ * Customize the names based on the type that is provided by the plugin.
76
+ */
77
+ name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string;
78
+ };
79
+ /**
80
+ * Export an Oas object as Oas type with `import type { Infer } from '@kubb/swagger-ts/oas'`
81
+ */
82
+ oasType?: 'infer' | false;
83
+ };
84
+ type ResolvedOptions = {
85
+ enumType: NonNullable<Options['enumType']>;
86
+ dateType: NonNullable<Options['dateType']>;
87
+ optionalType: NonNullable<Options['optionalType']>;
88
+ transformers: NonNullable<Options['transformers']>;
89
+ oasType: NonNullable<Options['oasType']>;
90
+ usedEnumNames: Record<string, number>;
91
+ };
92
+ type FileMeta = {
93
+ pluginKey?: KubbPlugin['key'];
94
+ name?: string;
95
+ tag?: string;
96
+ };
97
+ type AppMeta = AppMeta$1;
98
+ type PluginOptions = PluginFactoryOptions<'swagger-ts', Options, ResolvedOptions, never, ResolvePathOptions, AppMeta>;
99
+ declare module '@kubb/core' {
100
+ interface _Register {
101
+ ['@kubb/swagger-ts']: PluginOptions;
102
+ }
103
+ }
104
+
105
+ export type { FileMeta as F, Options as O, PluginOptions as P };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubb/swagger-ts",
3
- "version": "2.0.0-beta.1",
3
+ "version": "2.0.0-beta.11",
4
4
  "description": "Generator swagger-ts",
5
5
  "keywords": [
6
6
  "typescript",
@@ -25,6 +25,14 @@
25
25
  "require": "./dist/index.cjs",
26
26
  "default": "./dist/index.cjs"
27
27
  },
28
+ "./components": {
29
+ "import": "./dist/components.js",
30
+ "require": "./dist/components.cjs",
31
+ "default": "./dist/components.cjs"
32
+ },
33
+ "./oas": {
34
+ "types": "./dist/oas.d.ts"
35
+ },
28
36
  "./package.json": "./package.json",
29
37
  "./*": "./*"
30
38
  },
@@ -38,21 +46,27 @@
38
46
  "!/**/__tests__/**"
39
47
  ],
40
48
  "dependencies": {
41
- "change-case": "^4.1.2",
42
- "typescript": "^5.3.2",
43
- "@kubb/core": "2.0.0-beta.1",
44
- "@kubb/parser": "2.0.0-beta.1",
45
- "@kubb/swagger": "2.0.0-beta.1"
49
+ "hotscript": "^1.0.13",
50
+ "json-schema-to-ts": "^2.9.2",
51
+ "ts-toolbelt": "^9.6.0",
52
+ "@kubb/parser": "2.0.0-beta.11",
53
+ "@kubb/react": "2.0.0-beta.11",
54
+ "@kubb/core": "2.0.0-beta.11",
55
+ "@kubb/swagger": "2.0.0-beta.11",
56
+ "@kubb/types": "2.0.0-beta.11"
46
57
  },
47
58
  "devDependencies": {
48
- "eslint": "^8.54.0",
59
+ "@types/react": "^18.2.41",
60
+ "eslint": "^8.55.0",
61
+ "expect-type": "^0.17.3",
62
+ "react": "^18.2.0",
49
63
  "tsup": "^8.0.1",
50
64
  "@kubb/eslint-config": "1.1.8",
51
65
  "@kubb/ts-config": "0.1.0",
52
66
  "@kubb/tsup-config": "1.1.8"
53
67
  },
54
68
  "peerDependencies": {
55
- "typescript": "^5.3.0"
69
+ "@kubb/react": "2.0.0-beta.11"
56
70
  },
57
71
  "packageManager": "pnpm@8.3.0",
58
72
  "engines": {
@@ -0,0 +1,62 @@
1
+ import { createRoot } from '@kubb/react'
2
+ import { OperationGenerator as Generator } from '@kubb/swagger'
3
+
4
+ import { Mutation } from './components/Mutation.tsx'
5
+ import { Oas } from './components/Oas.tsx'
6
+ import { Query } from './components/Query.tsx'
7
+
8
+ import type { AppContextProps } from '@kubb/react'
9
+ import type { Operation, OperationMethodResult, OperationSchemas } from '@kubb/swagger'
10
+ import type { FileMeta, PluginOptions } from './types.ts'
11
+
12
+ export class OperationGenerator extends Generator<PluginOptions['resolvedOptions'], PluginOptions> {
13
+ async all(): OperationMethodResult<FileMeta> {
14
+ const { oas, pluginManager, plugin } = this.context
15
+
16
+ if (!plugin.options.oasType) {
17
+ return null
18
+ }
19
+
20
+ const root = createRoot<AppContextProps>({ logger: pluginManager.logger })
21
+ root.render(
22
+ <Oas.File name="oas" typeName="Oas" />,
23
+ { meta: { oas, pluginManager, plugin } },
24
+ )
25
+
26
+ return root.files
27
+ }
28
+
29
+ async get(operation: Operation, schemas: OperationSchemas, options: PluginOptions['resolvedOptions']): OperationMethodResult<FileMeta> {
30
+ const { oas, pluginManager, plugin, mode = 'directory' } = this.context
31
+
32
+ const root = createRoot<AppContextProps<PluginOptions['appMeta']>>({ logger: pluginManager.logger })
33
+ root.render(
34
+ <Query.File mode={mode} />,
35
+ { meta: { oas, pluginManager, plugin: { ...plugin, options }, schemas, operation } },
36
+ )
37
+
38
+ return root.files
39
+ }
40
+
41
+ async post(operation: Operation, schemas: OperationSchemas, options: PluginOptions['resolvedOptions']): OperationMethodResult<FileMeta> {
42
+ const { oas, pluginManager, plugin, mode = 'directory' } = this.context
43
+
44
+ const root = createRoot<AppContextProps<PluginOptions['appMeta']>>({ logger: pluginManager.logger })
45
+ root.render(
46
+ <Mutation.File mode={mode} />,
47
+ { meta: { oas, pluginManager, plugin: { ...plugin, options }, schemas, operation } },
48
+ )
49
+
50
+ return root.files
51
+ }
52
+
53
+ async put(operation: Operation, schemas: OperationSchemas, options: PluginOptions['resolvedOptions']): OperationMethodResult<FileMeta> {
54
+ return this.post(operation, schemas, options)
55
+ }
56
+ async patch(operation: Operation, schemas: OperationSchemas, options: PluginOptions['resolvedOptions']): OperationMethodResult<FileMeta> {
57
+ return this.post(operation, schemas, options)
58
+ }
59
+ async delete(operation: Operation, schemas: OperationSchemas, options: PluginOptions['resolvedOptions']): OperationMethodResult<FileMeta> {
60
+ return this.post(operation, schemas, options)
61
+ }
62
+ }
@@ -0,0 +1,58 @@
1
+ import transformers from '@kubb/core/transformers'
2
+ import { print } from '@kubb/parser'
3
+ import { OasBuilder } from '@kubb/swagger'
4
+ import { refsSorter } from '@kubb/swagger/utils'
5
+
6
+ import { TypeGenerator } from './TypeGenerator.ts'
7
+
8
+ import type { KubbFile } from '@kubb/core'
9
+ import type { ImportMeta } from '@kubb/swagger'
10
+ import type { PluginOptions } from './types.ts'
11
+
12
+ export class TypeBuilder extends OasBuilder<PluginOptions['resolvedOptions']> {
13
+ build(name?: string): Required<Pick<KubbFile.File, 'imports' | 'source'>> {
14
+ const importMeta: ImportMeta[] = []
15
+ const codes: string[] = []
16
+
17
+ const generated = this.items
18
+ .filter((operationSchema) => (name ? operationSchema.name === name : true))
19
+ .sort(transformers.nameSorter)
20
+ .map((operationSchema) => {
21
+ const generator = new TypeGenerator(this.options, this.context)
22
+ const sources = generator.build({
23
+ schema: operationSchema.schema,
24
+ baseName: operationSchema.name,
25
+ description: operationSchema.description,
26
+ keysToOmit: operationSchema.keysToOmit,
27
+ })
28
+
29
+ importMeta.push(...generator.imports)
30
+
31
+ return {
32
+ import: {
33
+ refs: generator.refs,
34
+ name: operationSchema.name,
35
+ },
36
+ sources,
37
+ }
38
+ })
39
+ .sort(refsSorter)
40
+
41
+ generated.forEach((item) => {
42
+ codes.push(print(item.sources))
43
+ })
44
+
45
+ const imports: KubbFile.Import[] = importMeta.map((item) => {
46
+ return {
47
+ name: [item.ref.propertyName],
48
+ path: item.path,
49
+ isTypeOnly: item.isTypeOnly,
50
+ }
51
+ })
52
+
53
+ return {
54
+ imports,
55
+ source: transformers.combineCodes(codes),
56
+ }
57
+ }
58
+ }