@kubb/plugin-oas 3.0.0-alpha.15 → 3.0.0-alpha.17

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 (76) hide show
  1. package/dist/{OperationGenerator-BqfyX5-8.d.cts → OperationGenerator-B5CoXmjA.d.cts} +22 -20
  2. package/dist/{OperationGenerator-BqfyX5-8.d.ts → OperationGenerator-B5CoXmjA.d.ts} +22 -20
  3. package/dist/{Schema-BgpIlA79.d.cts → Schema-BsgDay0E.d.cts} +1 -12
  4. package/dist/{Schema-ExMIJOhD.d.ts → Schema-Dvt6i_X3.d.ts} +1 -12
  5. package/dist/{chunk-PZTS5YZV.js → chunk-AWRVBD2X.js} +15 -13
  6. package/dist/chunk-AWRVBD2X.js.map +1 -0
  7. package/dist/chunk-BEKIIF6H.cjs +1110 -0
  8. package/dist/chunk-BEKIIF6H.cjs.map +1 -0
  9. package/dist/chunk-CQMUJN22.cjs +42 -0
  10. package/dist/chunk-CQMUJN22.cjs.map +1 -0
  11. package/dist/chunk-FGOPORE3.js +38 -0
  12. package/dist/chunk-FGOPORE3.js.map +1 -0
  13. package/dist/{chunk-AAIEMW6V.cjs → chunk-FZABPEYO.cjs} +28 -798
  14. package/dist/chunk-FZABPEYO.cjs.map +1 -0
  15. package/dist/{chunk-M3V3WMCW.js → chunk-I6N3YPCE.js} +11 -773
  16. package/dist/chunk-I6N3YPCE.js.map +1 -0
  17. package/dist/chunk-IVSE3A7W.cjs +84 -0
  18. package/dist/chunk-IVSE3A7W.cjs.map +1 -0
  19. package/dist/chunk-VLHK3XAR.js +1098 -0
  20. package/dist/chunk-VLHK3XAR.js.map +1 -0
  21. package/dist/chunk-XIFGKAM3.js +76 -0
  22. package/dist/chunk-XIFGKAM3.js.map +1 -0
  23. package/dist/{chunk-I3ZL3GUQ.cjs → chunk-XON3FUPP.cjs} +19 -17
  24. package/dist/chunk-XON3FUPP.cjs.map +1 -0
  25. package/dist/components.cjs +5 -6
  26. package/dist/components.d.cts +2 -2
  27. package/dist/components.d.ts +2 -2
  28. package/dist/components.js +2 -3
  29. package/dist/generators.cjs +4 -5
  30. package/dist/generators.d.cts +1 -1
  31. package/dist/generators.d.ts +1 -1
  32. package/dist/generators.js +3 -4
  33. package/dist/hooks.cjs +102 -31
  34. package/dist/hooks.cjs.map +1 -1
  35. package/dist/hooks.d.cts +39 -6
  36. package/dist/hooks.d.ts +39 -6
  37. package/dist/hooks.js +92 -20
  38. package/dist/hooks.js.map +1 -1
  39. package/dist/index.cjs +25 -410
  40. package/dist/index.cjs.map +1 -1
  41. package/dist/index.d.cts +2 -2
  42. package/dist/index.d.ts +2 -2
  43. package/dist/index.js +5 -400
  44. package/dist/index.js.map +1 -1
  45. package/dist/utils.cjs +12 -12
  46. package/dist/utils.cjs.map +1 -1
  47. package/dist/utils.d.cts +2 -2
  48. package/dist/utils.d.ts +2 -2
  49. package/dist/utils.js +2 -3
  50. package/dist/utils.js.map +1 -1
  51. package/package.json +10 -10
  52. package/src/OperationGenerator.ts +11 -15
  53. package/src/SchemaGenerator.ts +15 -6
  54. package/src/components/Schema.tsx +1 -98
  55. package/src/generator.tsx +22 -14
  56. package/src/generators/jsonGenerator.ts +4 -4
  57. package/src/hooks/index.ts +2 -0
  58. package/src/hooks/useOperationManager.ts +76 -29
  59. package/src/hooks/useSchemaManager.ts +76 -0
  60. package/src/utils/getParams.ts +1 -1
  61. package/dist/chunk-2PMRS7PB.cjs +0 -8
  62. package/dist/chunk-2PMRS7PB.cjs.map +0 -1
  63. package/dist/chunk-2V64GHQ4.js +0 -32
  64. package/dist/chunk-2V64GHQ4.js.map +0 -1
  65. package/dist/chunk-4JIFKRM5.js +0 -6
  66. package/dist/chunk-4JIFKRM5.js.map +0 -1
  67. package/dist/chunk-5ULY3TQB.cjs +0 -90
  68. package/dist/chunk-5ULY3TQB.cjs.map +0 -1
  69. package/dist/chunk-AAIEMW6V.cjs.map +0 -1
  70. package/dist/chunk-CFU6KMOB.cjs +0 -38
  71. package/dist/chunk-CFU6KMOB.cjs.map +0 -1
  72. package/dist/chunk-GAPWY2CE.js +0 -84
  73. package/dist/chunk-GAPWY2CE.js.map +0 -1
  74. package/dist/chunk-I3ZL3GUQ.cjs.map +0 -1
  75. package/dist/chunk-M3V3WMCW.js.map +0 -1
  76. package/dist/chunk-PZTS5YZV.js.map +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubb/plugin-oas",
3
- "version": "3.0.0-alpha.15",
3
+ "version": "3.0.0-alpha.17",
4
4
  "description": "Generator swagger",
5
5
  "keywords": [
6
6
  "typescript",
@@ -77,23 +77,23 @@
77
77
  "@stoplight/yaml": "^4.3.0",
78
78
  "execa": "^9.3.1",
79
79
  "remeda": "^2.12.0",
80
- "@kubb/core": "3.0.0-alpha.15",
81
- "@kubb/fs": "3.0.0-alpha.15",
82
- "@kubb/oas": "3.0.0-alpha.15",
83
- "@kubb/react": "3.0.0-alpha.15"
80
+ "@kubb/core": "3.0.0-alpha.17",
81
+ "@kubb/fs": "3.0.0-alpha.17",
82
+ "@kubb/oas": "3.0.0-alpha.17",
83
+ "@kubb/react": "3.0.0-alpha.17"
84
84
  },
85
85
  "devDependencies": {
86
86
  "@types/react": "^18.3.5",
87
87
  "@types/react-dom": "^18.3.0",
88
88
  "react": "^18.3.1",
89
89
  "tsup": "^8.2.4",
90
- "typescript": "^5.5.4",
91
- "@kubb/config-biome": "3.0.0-alpha.15",
92
- "@kubb/config-ts": "3.0.0-alpha.15",
93
- "@kubb/config-tsup": "3.0.0-alpha.15"
90
+ "typescript": "^5.6.2",
91
+ "@kubb/config-biome": "3.0.0-alpha.17",
92
+ "@kubb/config-ts": "3.0.0-alpha.17",
93
+ "@kubb/config-tsup": "3.0.0-alpha.17"
94
94
  },
95
95
  "peerDependencies": {
96
- "@kubb/react": "3.0.0-alpha.15"
96
+ "@kubb/react": "3.0.0-alpha.17"
97
97
  },
98
98
  "engines": {
99
99
  "node": ">=20"
@@ -12,7 +12,7 @@ import type { Exclude, Include, OperationSchemas, OperationsByMethod, Override }
12
12
  /**
13
13
  * @deprecated
14
14
  */
15
- export type GetOperationGeneratorOptions<T extends OperationGenerator<any, any, any>> = T extends OperationGenerator<infer Options, any, any> ? Options : never
15
+ export type GetOperationGeneratorOptions<T = any> = any
16
16
 
17
17
  export type OperationMethodResult<TFileMeta extends FileMetaBase> = Promise<KubbFile.File<TFileMeta> | Array<KubbFile.File<TFileMeta>> | null>
18
18
 
@@ -31,10 +31,9 @@ type Context<TOptions, TPluginOptions extends PluginFactoryOptions> = {
31
31
  }
32
32
 
33
33
  export class OperationGenerator<
34
- TOptions = unknown,
35
34
  TPluginOptions extends PluginFactoryOptions = PluginFactoryOptions,
36
35
  TFileMeta extends FileMetaBase = FileMetaBase,
37
- > extends BaseGenerator<TOptions, Context<TOptions, TPluginOptions>> {
36
+ > extends BaseGenerator<TPluginOptions['resolvedOptions'], Context<TPluginOptions['resolvedOptions'], TPluginOptions>> {
38
37
  #operationsByMethod: OperationsByMethod = {}
39
38
  get operationsByMethod(): OperationsByMethod {
40
39
  return this.#operationsByMethod
@@ -44,7 +43,7 @@ export class OperationGenerator<
44
43
  this.#operationsByMethod = paths
45
44
  }
46
45
 
47
- #getOptions(operation: Operation, method: HttpMethod): Partial<TOptions> {
46
+ #getOptions(operation: Operation, method: HttpMethod): Partial<TPluginOptions['resolvedOptions']> {
48
47
  const { override = [] } = this.context
49
48
 
50
49
  return (
@@ -131,10 +130,8 @@ export class OperationGenerator<
131
130
  getSchemas(
132
131
  operation: Operation,
133
132
  {
134
- forStatusCode,
135
133
  resolveName = (name) => name,
136
134
  }: {
137
- forStatusCode?: string | number
138
135
  resolveName?: (name: string) => string
139
136
  } = {},
140
137
  ): OperationSchemas {
@@ -142,8 +139,7 @@ export class OperationGenerator<
142
139
  const queryParamsSchema = this.context.oas.getParametersSchema(operation, 'query')
143
140
  const headerParamsSchema = this.context.oas.getParametersSchema(operation, 'header')
144
141
  const requestSchema = this.context.oas.getRequestSchema(operation)
145
- const responseStatusCode =
146
- forStatusCode || (operation.schema.responses && Object.keys(operation.schema.responses).find((key) => key.startsWith('2'))) || 200
142
+ const responseStatusCode = (operation.schema.responses && Object.keys(operation.schema.responses).find((key) => key.startsWith('2'))) || 200
147
143
  const responseSchema = this.context.oas.getResponseSchema(operation, responseStatusCode)
148
144
  const statusCodes = operation.getResponseStatusCodes().map((statusCode) => {
149
145
  let name = statusCode
@@ -231,7 +227,7 @@ export class OperationGenerator<
231
227
 
232
228
  #methods = ['get', 'post', 'patch', 'put', 'delete']
233
229
 
234
- async build(...generators: Array<Generator<Extract<TOptions, PluginFactoryOptions>>>): Promise<Array<KubbFile.File<TFileMeta>>> {
230
+ async build(...generators: Array<Generator<TPluginOptions>>): Promise<Array<KubbFile.File<TFileMeta>>> {
235
231
  const { oas } = this.context
236
232
 
237
233
  const paths = oas.getPaths()
@@ -336,41 +332,41 @@ export class OperationGenerator<
336
332
  /**
337
333
  * Operation
338
334
  */
339
- async operation(operation: Operation, options: TOptions): OperationMethodResult<TFileMeta> {
335
+ async operation(operation: Operation, options: TPluginOptions['resolvedOptions']): OperationMethodResult<TFileMeta> {
340
336
  return []
341
337
  }
342
338
 
343
339
  /**
344
340
  * GET
345
341
  */
346
- async get(operation: Operation, options: TOptions): OperationMethodResult<TFileMeta> {
342
+ async get(operation: Operation, options: TPluginOptions['resolvedOptions']): OperationMethodResult<TFileMeta> {
347
343
  return []
348
344
  }
349
345
 
350
346
  /**
351
347
  * POST
352
348
  */
353
- async post(operation: Operation, options: TOptions): OperationMethodResult<TFileMeta> {
349
+ async post(operation: Operation, options: TPluginOptions['resolvedOptions']): OperationMethodResult<TFileMeta> {
354
350
  return []
355
351
  }
356
352
  /**
357
353
  * PATCH
358
354
  */
359
- async patch(operation: Operation, options: TOptions): OperationMethodResult<TFileMeta> {
355
+ async patch(operation: Operation, options: TPluginOptions['resolvedOptions']): OperationMethodResult<TFileMeta> {
360
356
  return []
361
357
  }
362
358
 
363
359
  /**
364
360
  * PUT
365
361
  */
366
- async put(operation: Operation, options: TOptions): OperationMethodResult<TFileMeta> {
362
+ async put(operation: Operation, options: TPluginOptions['resolvedOptions']): OperationMethodResult<TFileMeta> {
367
363
  return []
368
364
  }
369
365
 
370
366
  /**
371
367
  * DELETE
372
368
  */
373
- async delete(operation: Operation, options: TOptions): OperationMethodResult<TFileMeta> {
369
+ async delete(operation: Operation, options: TPluginOptions['resolvedOptions']): OperationMethodResult<TFileMeta> {
374
370
  return []
375
371
  }
376
372
 
@@ -808,7 +808,7 @@ export class SchemaGenerator<
808
808
  ].filter(Boolean)
809
809
  }
810
810
 
811
- if (!['boolean', 'object', 'number', 'string', 'integer'].includes(schema.type)) {
811
+ if (!['boolean', 'object', 'number', 'string', 'integer', 'null'].includes(schema.type)) {
812
812
  this.context.pluginManager.logger.emit('warning', `Schema type '${schema.type}' is not valid for schema ${parentName}.${name}`)
813
813
  }
814
814
 
@@ -819,14 +819,18 @@ export class SchemaGenerator<
819
819
  return [{ keyword: unknownReturn }]
820
820
  }
821
821
 
822
- async build(...generators: Array<Generator<Extract<TOptions, PluginFactoryOptions>>>): Promise<Array<KubbFile.File<TFileMeta>>> {
822
+ async build(...generators: Array<Generator<TPluginOptions>>): Promise<Array<KubbFile.File<TFileMeta>>> {
823
823
  const { oas, contentType, include } = this.context
824
824
 
825
825
  const schemas = getSchemas({ oas, contentType, includes: include })
826
826
 
827
- const promises = Object.entries(schemas).reduce((acc, [name, schema]) => {
827
+ const promises = Object.entries(schemas).reduce((acc, [name, value]) => {
828
+ if (!value) {
829
+ return acc
830
+ }
831
+
828
832
  const options = this.#getOptions({ name })
829
- const promiseOperation = this.schema.call(this, name, schema, {
833
+ const promiseOperation = this.schema.call(this, name, value, {
830
834
  ...this.options,
831
835
  ...options,
832
836
  })
@@ -836,10 +840,15 @@ export class SchemaGenerator<
836
840
  }
837
841
 
838
842
  generators?.forEach((generator) => {
843
+ const tree = this.parse({ schema: value, name: name })
844
+
839
845
  const promise = generator.schema?.({
840
846
  instance: this,
841
- name,
842
- schema,
847
+ schema: {
848
+ name,
849
+ value,
850
+ tree,
851
+ },
843
852
  options: {
844
853
  ...this.options,
845
854
  ...options,
@@ -1,14 +1,8 @@
1
- import { File, createContext, useApp, useFile } from '@kubb/react'
2
-
3
- import { schemaKeywords } from '../SchemaMapper.ts'
4
- import { useSchema } from '../hooks/useSchema.ts'
1
+ import { createContext } from '@kubb/react'
5
2
 
6
3
  import type { SchemaObject } from '@kubb/oas'
7
4
  import type { KubbNode } from '@kubb/react/types'
8
- import type { ReactNode } from 'react'
9
- import { SchemaGenerator } from '../SchemaGenerator.ts'
10
5
  import type { Schema as SchemaType } from '../SchemaMapper.ts'
11
- import type { PluginOas } from '../types.ts'
12
6
 
13
7
  export type SchemaContextProps = {
14
8
  name: string
@@ -32,95 +26,4 @@ export function Schema({ name, value, tree = [], children }: Props): KubbNode {
32
26
  return <SchemaContext.Provider value={{ name, schema: value, tree }}>{children}</SchemaContext.Provider>
33
27
  }
34
28
 
35
- type FileProps = {
36
- isTypeOnly?: boolean
37
- output: string | undefined
38
- children?: KubbNode
39
- }
40
-
41
- Schema.File = function ({ isTypeOnly, children }: FileProps): ReactNode {
42
- const { plugin, pluginManager, mode } = useApp<PluginOas>()
43
- const { name } = useSchema()
44
-
45
- if (mode === 'single') {
46
- const baseName = `${pluginManager.resolveName({
47
- name,
48
- pluginKey: plugin.key,
49
- type: 'file',
50
- })}.ts` as const
51
-
52
- const resolvedPath = pluginManager.resolvePath({
53
- baseName: '',
54
- pluginKey: plugin.key,
55
- })
56
-
57
- if (!resolvedPath) {
58
- return null
59
- }
60
-
61
- return (
62
- <File
63
- baseName={baseName}
64
- path={resolvedPath}
65
- meta={{
66
- pluginKey: plugin.key,
67
- }}
68
- >
69
- {children}
70
- </File>
71
- )
72
- }
73
-
74
- const baseName = `${pluginManager.resolveName({
75
- name,
76
- pluginKey: plugin.key,
77
- type: 'file',
78
- })}.ts` as const
79
- const resolvedPath = pluginManager.resolvePath({
80
- baseName,
81
- pluginKey: plugin.key,
82
- })
83
-
84
- if (!resolvedPath) {
85
- return null
86
- }
87
-
88
- return (
89
- <File
90
- baseName={baseName}
91
- path={resolvedPath}
92
- meta={{
93
- pluginKey: plugin.key,
94
- }}
95
- >
96
- <Schema.Imports isTypeOnly={isTypeOnly} />
97
- {children}
98
- </File>
99
- )
100
- }
101
-
102
- type SchemaImportsProps = {
103
- isTypeOnly?: boolean
104
- }
105
-
106
- Schema.Imports = ({ isTypeOnly }: SchemaImportsProps): ReactNode => {
107
- const { tree } = useSchema()
108
- const { path: root } = useFile()
109
-
110
- const refs = SchemaGenerator.deepSearch(tree, schemaKeywords.ref)
111
-
112
- return (
113
- <>
114
- {refs
115
- ?.map((item, i) => {
116
- if (!item.args.path) {
117
- return undefined
118
- }
119
-
120
- return <File.Import key={i} root={root} name={[item.args.name]} path={item.args.path} isTypeOnly={isTypeOnly} />
121
- })
122
- .filter(Boolean)}
123
- </>
124
- )
125
- }
126
29
  Schema.Context = SchemaContext
package/src/generator.tsx CHANGED
@@ -6,6 +6,7 @@ import { App, createRoot } from '@kubb/react'
6
6
  import type { KubbNode } from '@kubb/react/types'
7
7
  import type { OperationGenerator } from './OperationGenerator.ts'
8
8
  import type { SchemaGenerator, SchemaGeneratorOptions } from './SchemaGenerator.ts'
9
+ import type { Schema } from './SchemaMapper.ts'
9
10
  import type { OperationsByMethod } from './types.ts'
10
11
 
11
12
  type OperationsProps<TOptions extends PluginFactoryOptions> = {
@@ -23,16 +24,19 @@ type OperationProps<TOptions extends PluginFactoryOptions> = {
23
24
 
24
25
  type SchemaProps<TOptions extends PluginFactoryOptions> = {
25
26
  instance: Omit<SchemaGenerator<SchemaGeneratorOptions, TOptions>, 'build'>
26
- name: string
27
- schema: SchemaObject
27
+ schema: {
28
+ name: string
29
+ tree: Array<Schema>
30
+ value: SchemaObject
31
+ }
28
32
  options: TOptions['resolvedOptions']
29
33
  }
30
34
 
31
35
  export type GeneratorOptions<TOptions extends PluginFactoryOptions> = {
32
36
  name: string
33
- operations?: (props: OperationsProps<TOptions>) => Promise<KubbFile.File[]>
34
- operation?: (props: OperationProps<TOptions>) => Promise<KubbFile.File[]>
35
- schema?: (props: SchemaProps<TOptions>) => Promise<KubbFile.File[]>
37
+ operations?: (this: GeneratorOptions<TOptions>, props: OperationsProps<TOptions>) => Promise<KubbFile.File[]>
38
+ operation?: (this: GeneratorOptions<TOptions>, props: OperationProps<TOptions>) => Promise<KubbFile.File[]>
39
+ schema?: (this: GeneratorOptions<TOptions>, props: SchemaProps<TOptions>) => Promise<KubbFile.File[]>
36
40
  }
37
41
 
38
42
  export type Generator<TOptions extends PluginFactoryOptions> = GeneratorOptions<TOptions>
@@ -43,9 +47,9 @@ export function createGenerator<TOptions extends PluginFactoryOptions>(parseOpti
43
47
 
44
48
  export type ReactGeneratorOptions<TOptions extends PluginFactoryOptions> = {
45
49
  name: string
46
- Operations?: (props: OperationsProps<TOptions>) => KubbNode
47
- Operation?: (props: OperationProps<TOptions>) => KubbNode
48
- Schema?: (props: SchemaProps<TOptions>) => KubbNode
50
+ Operations?: (this: ReactGeneratorOptions<TOptions>, props: OperationsProps<TOptions>) => KubbNode
51
+ Operation?: (this: ReactGeneratorOptions<TOptions>, props: OperationProps<TOptions>) => KubbNode
52
+ Schema?: (this: ReactGeneratorOptions<TOptions>, props: SchemaProps<TOptions>) => KubbNode
49
53
  /**
50
54
  * Combine all react nodes and only render ones(to string or render)
51
55
  */
@@ -65,10 +69,12 @@ export function createReactGenerator<TOptions extends PluginFactoryOptions>(pars
65
69
  logger: pluginManager.logger,
66
70
  })
67
71
 
72
+ const Component = parseOptions.Operations.bind(this)
73
+
68
74
  root.render(
69
75
  <App pluginManager={pluginManager} plugin={plugin} mode={mode}>
70
76
  <Oas oas={oas} operations={operations} generator={instance}>
71
- <parseOptions.Operations operations={operations} instance={instance} operationsByMethod={operationsByMethod} options={options} />
77
+ <Component operations={operations} instance={instance} operationsByMethod={operationsByMethod} options={options} />
72
78
  </Oas>
73
79
  </App>,
74
80
  )
@@ -85,11 +91,13 @@ export function createReactGenerator<TOptions extends PluginFactoryOptions>(pars
85
91
  logger: pluginManager.logger,
86
92
  })
87
93
 
94
+ const Component = parseOptions.Operation.bind(this)
95
+
88
96
  root.render(
89
97
  <App pluginManager={pluginManager} plugin={{ ...plugin, options }} mode={mode}>
90
98
  <Oas oas={oas} operations={[operation]} generator={instance}>
91
99
  <Oas.Operation operation={operation}>
92
- <parseOptions.Operation operation={operation} options={options} instance={instance} />
100
+ <Component operation={operation} options={options} instance={instance} />
93
101
  </Oas.Operation>
94
102
  </Oas>
95
103
  </App>,
@@ -97,7 +105,7 @@ export function createReactGenerator<TOptions extends PluginFactoryOptions>(pars
97
105
 
98
106
  return root.files
99
107
  },
100
- async schema({ instance, schema, name, options }) {
108
+ async schema({ instance, schema, options }) {
101
109
  if (!parseOptions.Schema) {
102
110
  return []
103
111
  }
@@ -107,13 +115,13 @@ export function createReactGenerator<TOptions extends PluginFactoryOptions>(pars
107
115
  logger: pluginManager.logger,
108
116
  })
109
117
 
110
- const tree = instance.parse({ schema, name })
118
+ const Component = parseOptions.Schema.bind(this)
111
119
 
112
120
  root.render(
113
121
  <App pluginManager={pluginManager} plugin={{ ...plugin, options }} mode={mode}>
114
122
  <Oas oas={oas}>
115
- <Oas.Schema name={name} value={schema} tree={tree}>
116
- <parseOptions.Schema schema={schema} options={options} instance={instance} name={name} />
123
+ <Oas.Schema name={schema.name} value={schema.value} tree={schema.tree}>
124
+ <Component schema={schema} options={options} instance={instance} />
117
125
  </Oas.Schema>
118
126
  </Oas>
119
127
  </App>,
@@ -4,10 +4,10 @@ import type { PluginOas } from '../types.ts'
4
4
 
5
5
  export const jsonGenerator = createGenerator<PluginOas>({
6
6
  name: 'plugin-oas',
7
- async schema({ schema, name, instance }) {
7
+ async schema({ schema, instance }) {
8
8
  const { pluginManager, plugin } = instance.context
9
9
  const file = pluginManager.getFile({
10
- name: camelCase(name),
10
+ name: camelCase(schema.name),
11
11
  extName: '.json',
12
12
  mode: 'split',
13
13
  pluginKey: plugin.key,
@@ -18,10 +18,10 @@ export const jsonGenerator = createGenerator<PluginOas>({
18
18
  ...file,
19
19
  sources: [
20
20
  {
21
- name: camelCase(name),
21
+ name: camelCase(schema.name),
22
22
  isExportable: false,
23
23
  isIndexable: false,
24
- value: JSON.stringify(schema),
24
+ value: JSON.stringify(schema.value),
25
25
  },
26
26
  ],
27
27
  },
@@ -1,5 +1,7 @@
1
1
  export { useOas } from './useOas.ts'
2
2
  export { useOperation } from './useOperation.ts'
3
3
  export { useOperationManager } from './useOperationManager.ts'
4
+ export type { SchemaNames } from './useOperationManager.ts'
4
5
  export { useOperations } from './useOperations.ts'
5
6
  export { useSchema } from './useSchema.ts'
7
+ export { useSchemaManager } from './useSchemaManager.ts'
@@ -13,7 +13,7 @@ type FileMeta = FileMetaBase & {
13
13
  tag?: string
14
14
  }
15
15
 
16
- type SchemaNames = {
16
+ export type SchemaNames = {
17
17
  request: string | undefined
18
18
  parameters: {
19
19
  path: string | undefined
@@ -25,14 +25,41 @@ type SchemaNames = {
25
25
  }
26
26
 
27
27
  type UseOperationManagerResult = {
28
- getName: (operation: OperationType, params: { pluginKey?: Plugin['key']; type: ResolveNameParams['type'] }) => string
29
- getFile: (operation: OperationType, params?: { pluginKey?: Plugin['key']; extName?: KubbFile.Extname }) => KubbFile.File<FileMeta>
30
- groupSchemasByName: (operation: OperationType, params: { pluginKey?: Plugin['key']; type: ResolveNameParams['type'] }) => SchemaNames
31
- getSchemas: (
28
+ getName: (
29
+ operation: OperationType,
30
+ params: {
31
+ prefix?: string
32
+ suffix?: string
33
+ pluginKey?: Plugin['key']
34
+ type: ResolveNameParams['type']
35
+ },
36
+ ) => string
37
+ getSchemaName: (
32
38
  operation: Operation,
33
- params?: { pluginKey?: Plugin['key']; type?: ResolveNameParams['type'] },
34
- forStatusCode?: string | number,
35
- ) => OperationSchemas
39
+ key: keyof Omit<OperationSchemas, 'errors' | 'statusCodes'>,
40
+ params?: {
41
+ pluginKey?: Plugin['key']
42
+ type?: ResolveNameParams['type']
43
+ },
44
+ ) => string
45
+ getFile: (
46
+ operation: OperationType,
47
+ params?: {
48
+ prefix?: string
49
+ suffix?: string
50
+ pluginKey?: Plugin['key']
51
+ extName?: KubbFile.Extname
52
+ tag?: string
53
+ },
54
+ ) => KubbFile.File<FileMeta>
55
+ groupSchemasByName: (
56
+ operation: OperationType,
57
+ params: {
58
+ pluginKey?: Plugin['key']
59
+ type: ResolveNameParams['type']
60
+ },
61
+ ) => SchemaNames
62
+ getSchemas: (operation: Operation, params?: { pluginKey?: Plugin['key']; type?: ResolveNameParams['type'] }) => OperationSchemas
36
63
  }
37
64
 
38
65
  /**
@@ -42,24 +69,48 @@ export function useOperationManager(): UseOperationManagerResult {
42
69
  const { plugin, pluginManager } = useApp()
43
70
  const { generator } = useContext(Oas.Context)
44
71
 
45
- if (!generator) {
46
- throw new Error(`'generator' is not defined`)
47
- }
48
-
49
- const getName: UseOperationManagerResult['getName'] = (operation, { pluginKey = plugin.key, type }) => {
72
+ const getName: UseOperationManagerResult['getName'] = (operation, { prefix = '', suffix = '', pluginKey = plugin.key, type }) => {
50
73
  return pluginManager.resolveName({
51
- name: operation.getOperationId(),
74
+ name: `${prefix} ${operation.getOperationId()} ${suffix}`,
52
75
  pluginKey,
53
76
  type,
54
77
  })
55
78
  }
56
79
 
57
- const getFile: UseOperationManagerResult['getFile'] = (operation, { pluginKey = plugin.key, extName = '.ts' } = {}) => {
58
- // needed for the `output.group`
59
- const tag = operation.getTags().at(0)?.name
60
- //TODO replace with group
61
- const name = getName(operation, { type: 'file', pluginKey })
80
+ const getSchemas: UseOperationManagerResult['getSchemas'] = (operation, params) => {
81
+ if (!generator) {
82
+ throw new Error(`'generator' is not defined`)
83
+ }
62
84
 
85
+ return generator.getSchemas(operation, {
86
+ resolveName: (name) =>
87
+ pluginManager.resolveName({
88
+ name,
89
+ pluginKey: params?.pluginKey,
90
+ type: params?.type,
91
+ }),
92
+ })
93
+ }
94
+
95
+ const getSchemaName: UseOperationManagerResult['getSchemaName'] = (operation, key, { pluginKey = plugin.key, type } = {}) => {
96
+ const schemas = getSchemas(operation)
97
+
98
+ if (!schemas[key]?.name) {
99
+ throw new Error(`SchemaName not found for ${operation.getOperationId()}`)
100
+ }
101
+
102
+ return pluginManager.resolveName({
103
+ name: schemas[key]?.name,
104
+ pluginKey,
105
+ type,
106
+ })
107
+ }
108
+ //TODO replace tag with group
109
+ const getFile: UseOperationManagerResult['getFile'] = (
110
+ operation,
111
+ { prefix, suffix, pluginKey = plugin.key, tag = operation.getTags().at(0)?.name, extName = '.ts' } = {},
112
+ ) => {
113
+ const name = getName(operation, { type: 'file', pluginKey, prefix, suffix })
63
114
  const file = pluginManager.getFile({
64
115
  name,
65
116
  extName,
@@ -79,6 +130,10 @@ export function useOperationManager(): UseOperationManagerResult {
79
130
  }
80
131
 
81
132
  const groupSchemasByName: UseOperationManagerResult['groupSchemasByName'] = (operation, { pluginKey = plugin.key, type }) => {
133
+ if (!generator) {
134
+ throw new Error(`'generator' is not defined`)
135
+ }
136
+
82
137
  const schemas = generator.getSchemas(operation)
83
138
 
84
139
  const errors = (schemas.errors || []).reduce(
@@ -149,16 +204,8 @@ export function useOperationManager(): UseOperationManagerResult {
149
204
  return {
150
205
  getName,
151
206
  getFile,
152
- getSchemas: (operation, params, forStatusCode) =>
153
- generator.getSchemas(operation, {
154
- forStatusCode,
155
- resolveName: (name) =>
156
- pluginManager.resolveName({
157
- name,
158
- pluginKey: params?.pluginKey,
159
- type: params?.type,
160
- }),
161
- }),
207
+ getSchemas,
208
+ getSchemaName,
162
209
  groupSchemasByName,
163
210
  }
164
211
  }
@@ -0,0 +1,76 @@
1
+ import { File, useApp } from '@kubb/react'
2
+
3
+ import type { FileMetaBase, Plugin, ResolveNameParams } from '@kubb/core'
4
+ import type * as KubbFile from '@kubb/fs/types'
5
+ import type { Mode } from '@kubb/fs/types'
6
+ import { type Schema, SchemaGenerator, schemaKeywords } from '@kubb/plugin-oas'
7
+
8
+ type FileMeta = FileMetaBase & {
9
+ pluginKey: Plugin['key']
10
+ name: string
11
+ tag?: string
12
+ }
13
+
14
+ type UseSchemaManagerResult = {
15
+ getName: (name: string, params: { pluginKey?: Plugin['key']; type: ResolveNameParams['type'] }) => string
16
+ getFile: (name: string, params?: { pluginKey?: Plugin['key']; mode?: Mode; extName?: KubbFile.Extname; tag?: string }) => KubbFile.File<FileMeta>
17
+ getImports: (tree: Array<Schema>) => Array<KubbFile.Import>
18
+ }
19
+
20
+ /**
21
+ * `useSchemaManager` will return some helper functions that can be used to get the schema file, get the schema name.
22
+ */
23
+ export function useSchemaManager(): UseSchemaManagerResult {
24
+ const { mode, plugin, pluginManager, fileManager } = useApp()
25
+
26
+ const getName: UseSchemaManagerResult['getName'] = (name, { pluginKey = plugin.key, type }) => {
27
+ return pluginManager.resolveName({
28
+ name,
29
+ pluginKey,
30
+ type,
31
+ })
32
+ }
33
+ //TODO replace tag with group
34
+ const getFile: UseSchemaManagerResult['getFile'] = (name, { mode = 'split', pluginKey = plugin.key, extName = '.ts', tag } = {}) => {
35
+ const resolvedName = mode === 'single' ? '' : getName(name, { type: 'file', pluginKey })
36
+
37
+ const file = pluginManager.getFile({
38
+ name: resolvedName,
39
+ extName,
40
+ pluginKey,
41
+ options: { type: 'file', pluginKey, tag },
42
+ })
43
+
44
+ return {
45
+ ...file,
46
+ meta: {
47
+ ...file.meta,
48
+ name: resolvedName,
49
+ pluginKey,
50
+ },
51
+ }
52
+ }
53
+
54
+ const getImports: UseSchemaManagerResult['getImports'] = (tree) => {
55
+ const refs = SchemaGenerator.deepSearch(tree, schemaKeywords.ref)
56
+
57
+ return refs
58
+ ?.map((item, i) => {
59
+ if (!item.args.path) {
60
+ return undefined
61
+ }
62
+
63
+ return {
64
+ name: [item.args.name],
65
+ path: item.args.path,
66
+ }
67
+ })
68
+ .filter(Boolean)
69
+ }
70
+
71
+ return {
72
+ getName,
73
+ getFile,
74
+ getImports,
75
+ }
76
+ }
@@ -1,10 +1,10 @@
1
1
  import { isParameterObject } from '@kubb/oas'
2
2
 
3
+ import { camelCase } from '@kubb/core/transformers'
3
4
  import type { FunctionParamsAST } from '@kubb/core/utils'
4
5
  import type { OasTypes } from '@kubb/oas'
5
6
  import type { Params } from '@kubb/react/types'
6
7
  import type { OperationSchema } from '../types.ts'
7
- import { camelCase } from '@kubb/core/transformers'
8
8
  /**
9
9
  *
10
10
  * @deprecated