@kubb/plugin-zod 3.0.0-alpha.0
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/LICENSE +21 -0
- package/README.md +44 -0
- package/dist/Operations-BlQtRP31.d.cts +47 -0
- package/dist/Operations-BlQtRP31.d.ts +47 -0
- package/dist/chunk-7IAXQL4T.cjs +1291 -0
- package/dist/chunk-7IAXQL4T.cjs.map +1 -0
- package/dist/chunk-OXCOZC6T.js +1291 -0
- package/dist/chunk-OXCOZC6T.js.map +1 -0
- package/dist/components.cjs +11 -0
- package/dist/components.cjs.map +1 -0
- package/dist/components.d.cts +28 -0
- package/dist/components.d.ts +28 -0
- package/dist/components.js +11 -0
- package/dist/components.js.map +1 -0
- package/dist/index.cjs +16 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +156 -0
- package/dist/index.d.ts +156 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/package.json +97 -0
- package/src/OperationGenerator.tsx +54 -0
- package/src/SchemaGenerator.tsx +31 -0
- package/src/components/OperationSchema.tsx +66 -0
- package/src/components/Operations.tsx +133 -0
- package/src/components/Schema.tsx +171 -0
- package/src/components/__snapshots__/operations.ts +50 -0
- package/src/components/index.ts +3 -0
- package/src/index.ts +15 -0
- package/src/parser/index.ts +345 -0
- package/src/plugin.ts +167 -0
- package/src/types.ts +151 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import * as _kubb_core from '@kubb/core';
|
|
2
|
+
import { PluginFactoryOptions, ResolveNameParams } from '@kubb/core';
|
|
3
|
+
import * as KubbFile from '@kubb/fs/types';
|
|
4
|
+
import { SchemaObject } from '@kubb/oas';
|
|
5
|
+
import { ResolvePathOptions, Exclude, Include, Override, Schema } from '@kubb/plugin-oas';
|
|
6
|
+
import { O as Operations } from './Operations-BlQtRP31.js';
|
|
7
|
+
import '@kubb/react';
|
|
8
|
+
import 'react';
|
|
9
|
+
|
|
10
|
+
type Templates = {
|
|
11
|
+
operations?: typeof Operations.templates | false;
|
|
12
|
+
};
|
|
13
|
+
type Options = {
|
|
14
|
+
output?: {
|
|
15
|
+
/**
|
|
16
|
+
* Relative path to save the Zod schemas.
|
|
17
|
+
* When output is a file it will save all models inside that file else it will create a file per schema item.
|
|
18
|
+
* @default 'zod'
|
|
19
|
+
*/
|
|
20
|
+
path: string;
|
|
21
|
+
/**
|
|
22
|
+
* Name to be used for the `export * as {{exportAs}} from './'`
|
|
23
|
+
*/
|
|
24
|
+
exportAs?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Add an extension to the generated imports and exports, default it will not use an extension
|
|
27
|
+
*/
|
|
28
|
+
extName?: KubbFile.Extname;
|
|
29
|
+
/**
|
|
30
|
+
* Define what needs to exported, here you can also disable the export of barrel files
|
|
31
|
+
* @default `'barrel'`
|
|
32
|
+
*/
|
|
33
|
+
exportType?: 'barrel' | 'barrelNamed' | false;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Group the Zod schemas based on the provided name.
|
|
37
|
+
*/
|
|
38
|
+
group?: {
|
|
39
|
+
/**
|
|
40
|
+
* Tag will group based on the operation tag inside the Swagger file
|
|
41
|
+
*/
|
|
42
|
+
type: 'tag';
|
|
43
|
+
/**
|
|
44
|
+
* Relative path to save the grouped Zod schemas.
|
|
45
|
+
*
|
|
46
|
+
* `{{tag}}` will be replaced by the current tagName.
|
|
47
|
+
* @example `${output}/{{tag}}Controller` => `zod/PetController`
|
|
48
|
+
* @default `${output}/{{tag}}Controller`
|
|
49
|
+
*/
|
|
50
|
+
output?: string;
|
|
51
|
+
/**
|
|
52
|
+
* Name to be used for the `export * as {{exportAs}} from './`
|
|
53
|
+
* @default `"{{tag}}Schemas"`
|
|
54
|
+
*/
|
|
55
|
+
exportAs?: string;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
|
|
59
|
+
*/
|
|
60
|
+
exclude?: Array<Exclude>;
|
|
61
|
+
/**
|
|
62
|
+
* Array containing include parameters to include tags/operations/methods/paths.
|
|
63
|
+
*/
|
|
64
|
+
include?: Array<Include>;
|
|
65
|
+
/**
|
|
66
|
+
* Array containing override parameters to override `options` based on tags/operations/methods/paths.
|
|
67
|
+
*/
|
|
68
|
+
override?: Array<Override<ResolvedOptions>>;
|
|
69
|
+
transformers?: {
|
|
70
|
+
/**
|
|
71
|
+
* Customize the names based on the type that is provided by the plugin.
|
|
72
|
+
*/
|
|
73
|
+
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string;
|
|
74
|
+
/**
|
|
75
|
+
* Receive schema and baseName(propertName) and return FakerMeta array
|
|
76
|
+
* TODO TODO add docs
|
|
77
|
+
* @beta
|
|
78
|
+
*/
|
|
79
|
+
schema?: (props: {
|
|
80
|
+
schema?: SchemaObject;
|
|
81
|
+
name?: string;
|
|
82
|
+
parentName?: string;
|
|
83
|
+
}, defaultSchemas: Schema[]) => Schema[] | undefined;
|
|
84
|
+
};
|
|
85
|
+
mapper?: Record<string, string>;
|
|
86
|
+
/**
|
|
87
|
+
* Make it possible to override one of the templates
|
|
88
|
+
*/
|
|
89
|
+
templates?: Partial<Templates>;
|
|
90
|
+
/**
|
|
91
|
+
* Choose to use `date` or `datetime` as JavaScript `Date` instead of `string`.
|
|
92
|
+
* False will fallback on a simple z.string() format
|
|
93
|
+
* @default 'string' 'stringOffset' will become the default in Kubb v3
|
|
94
|
+
*/
|
|
95
|
+
dateType?: false | 'string' | 'stringOffset' | 'stringLocal' | 'date';
|
|
96
|
+
/**
|
|
97
|
+
* Which type to use when the Swagger/OpenAPI file is not providing more information
|
|
98
|
+
* @default 'any'
|
|
99
|
+
*/
|
|
100
|
+
unknownType?: 'any' | 'unknown';
|
|
101
|
+
/**
|
|
102
|
+
* Use TypeScript(`@kubb/plugin-ts`) to add type annotation.
|
|
103
|
+
*/
|
|
104
|
+
typed?: boolean;
|
|
105
|
+
/**
|
|
106
|
+
* Return Zod generated schema as type with z.infer<TYPE>
|
|
107
|
+
*/
|
|
108
|
+
typedSchema?: boolean;
|
|
109
|
+
/**
|
|
110
|
+
* Use of z.coerce.string() instead of z.string()
|
|
111
|
+
*/
|
|
112
|
+
coercion?: boolean;
|
|
113
|
+
/**
|
|
114
|
+
* Path to Zod
|
|
115
|
+
* It will be used as `import { z } from '${importPath}'`.
|
|
116
|
+
* It allows both relative and absolute path.
|
|
117
|
+
* the path will be applied as is, so relative path should be based on the file being generated.
|
|
118
|
+
* @default 'zod'
|
|
119
|
+
*/
|
|
120
|
+
importPath?: string;
|
|
121
|
+
};
|
|
122
|
+
type ResolvedOptions = {
|
|
123
|
+
extName: KubbFile.Extname | undefined;
|
|
124
|
+
transformers: NonNullable<Options['transformers']>;
|
|
125
|
+
exclude: Options['exclude'];
|
|
126
|
+
include: Options['include'];
|
|
127
|
+
override: Options['override'];
|
|
128
|
+
dateType: NonNullable<Options['dateType']>;
|
|
129
|
+
unknownType: NonNullable<Options['unknownType']>;
|
|
130
|
+
typed: NonNullable<Options['typed']>;
|
|
131
|
+
typedSchema: NonNullable<Options['typedSchema']>;
|
|
132
|
+
templates: NonNullable<Templates>;
|
|
133
|
+
mapper: NonNullable<Options['mapper']>;
|
|
134
|
+
importPath: NonNullable<Options['importPath']>;
|
|
135
|
+
coercion: NonNullable<Options['coercion']>;
|
|
136
|
+
};
|
|
137
|
+
type PluginZod = PluginFactoryOptions<'plugin-zod', Options, ResolvedOptions, never, ResolvePathOptions>;
|
|
138
|
+
declare module '@kubb/core' {
|
|
139
|
+
interface _Register {
|
|
140
|
+
['@kubb/plugin-zod']: PluginZod;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
declare const pluginZodName = "plugin-zod";
|
|
145
|
+
declare const pluginZod: (options?: Options | undefined) => _kubb_core.UserPluginWithLifeCycle<PluginZod>;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @deprecated Use `import { pluginZod } from '@kubb/plugin-zod'` instead
|
|
149
|
+
*/
|
|
150
|
+
declare const definePluginDefault: (options?: Options | undefined) => _kubb_core.UserPluginWithLifeCycle<PluginZod>;
|
|
151
|
+
/**
|
|
152
|
+
* @deprecated Use `import { pluginZod } from '@kubb/plugin-zod'` instead
|
|
153
|
+
*/
|
|
154
|
+
declare const definePlugin: (options?: Options | undefined) => _kubb_core.UserPluginWithLifeCycle<PluginZod>;
|
|
155
|
+
|
|
156
|
+
export { type PluginZod, definePluginDefault as default, definePlugin, pluginZod, pluginZodName };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {
|
|
2
|
+
pluginZod,
|
|
3
|
+
pluginZodName
|
|
4
|
+
} from "./chunk-OXCOZC6T.js";
|
|
5
|
+
|
|
6
|
+
// src/index.ts
|
|
7
|
+
var definePluginDefault = pluginZod;
|
|
8
|
+
var definePlugin = pluginZod;
|
|
9
|
+
var src_default = definePluginDefault;
|
|
10
|
+
export {
|
|
11
|
+
src_default as default,
|
|
12
|
+
definePlugin,
|
|
13
|
+
pluginZod,
|
|
14
|
+
pluginZodName
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { pluginZod } from './plugin.ts'\n\nexport { pluginZod, pluginZodName } from './plugin.ts'\nexport type { PluginZod } from './types.ts'\n\n/**\n * @deprecated Use `import { pluginZod } from '@kubb/plugin-zod'` instead\n */\nconst definePluginDefault = pluginZod\n/**\n * @deprecated Use `import { pluginZod } from '@kubb/plugin-zod'` instead\n */\nexport const definePlugin = pluginZod\n\nexport default definePluginDefault\n"],"mappings":";;;;;;AAQA,IAAM,sBAAsB;AAIrB,IAAM,eAAe;AAE5B,IAAO,cAAQ;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kubb/plugin-zod",
|
|
3
|
+
"version": "3.0.0-alpha.0",
|
|
4
|
+
"description": "Generator plugin-zod",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"typescript",
|
|
7
|
+
"plugins",
|
|
8
|
+
"kubb",
|
|
9
|
+
"codegen",
|
|
10
|
+
"swagger",
|
|
11
|
+
"openapi"
|
|
12
|
+
],
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git://github.com/kubb-labs/kubb.git",
|
|
16
|
+
"directory": "packages/plugin-zod"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"author": "Stijn Van Hulle <stijn@stijnvanhulle.be",
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"type": "module",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"import": "./dist/index.js",
|
|
25
|
+
"require": "./dist/index.cjs",
|
|
26
|
+
"default": "./dist/index.cjs"
|
|
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
|
+
},
|
|
36
|
+
"./package.json": "./package.json",
|
|
37
|
+
"./*": "./*"
|
|
38
|
+
},
|
|
39
|
+
"main": "dist/index.cjs",
|
|
40
|
+
"module": "dist/index.js",
|
|
41
|
+
"types": "./dist/index.d.ts",
|
|
42
|
+
"typesVersions": {
|
|
43
|
+
"*": {
|
|
44
|
+
"components": [
|
|
45
|
+
"./dist/components.d.ts"
|
|
46
|
+
],
|
|
47
|
+
"oas": [
|
|
48
|
+
"./dist/oas.d.ts"
|
|
49
|
+
]
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"files": [
|
|
53
|
+
"src",
|
|
54
|
+
"dist",
|
|
55
|
+
"!/**/**.test.**",
|
|
56
|
+
"!/**/__tests__/**"
|
|
57
|
+
],
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"@kubb/core": "3.0.0-alpha.0",
|
|
60
|
+
"@kubb/oas": "3.0.0-alpha.0",
|
|
61
|
+
"@kubb/fs": "3.0.0-alpha.0",
|
|
62
|
+
"@kubb/parser-ts": "3.0.0-alpha.0",
|
|
63
|
+
"@kubb/plugin-oas": "3.0.0-alpha.0",
|
|
64
|
+
"@kubb/react": "3.0.0-alpha.0",
|
|
65
|
+
"@kubb/types": "3.0.0-alpha.0"
|
|
66
|
+
},
|
|
67
|
+
"devDependencies": {
|
|
68
|
+
"@types/react": "^18.3.3",
|
|
69
|
+
"prettier": "^3.3.3",
|
|
70
|
+
"react": "^18.3.1",
|
|
71
|
+
"tsup": "^8.2.4",
|
|
72
|
+
"@kubb/config-biome": "3.0.0-alpha.0",
|
|
73
|
+
"@kubb/config-ts": "3.0.0-alpha.0",
|
|
74
|
+
"@kubb/config-tsup": "3.0.0-alpha.0"
|
|
75
|
+
},
|
|
76
|
+
"peerDependencies": {
|
|
77
|
+
"@kubb/react": "3.0.0-alpha.0"
|
|
78
|
+
},
|
|
79
|
+
"engines": {
|
|
80
|
+
"node": ">=20"
|
|
81
|
+
},
|
|
82
|
+
"publishConfig": {
|
|
83
|
+
"access": "public",
|
|
84
|
+
"registry": "https://registry.npmjs.org/"
|
|
85
|
+
},
|
|
86
|
+
"scripts": {
|
|
87
|
+
"build": "tsup",
|
|
88
|
+
"clean": "npx rimraf ./dist",
|
|
89
|
+
"lint": "bun biome lint .",
|
|
90
|
+
"lint:fix": "bun biome lint --apply-unsafe .",
|
|
91
|
+
"release": "pnpm publish --no-git-check",
|
|
92
|
+
"release:canary": "bash ../../.github/canary.sh && node ../../scripts/build.js canary && pnpm publish --no-git-check",
|
|
93
|
+
"start": "tsup --watch",
|
|
94
|
+
"test": "vitest --passWithNoTests",
|
|
95
|
+
"typecheck": "tsc -p ./tsconfig.json --noEmit --emitDeclarationOnly false"
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { OperationGenerator as Generator } from '@kubb/plugin-oas'
|
|
2
|
+
import { Oas } from '@kubb/plugin-oas/components'
|
|
3
|
+
import { App, createRoot } from '@kubb/react'
|
|
4
|
+
|
|
5
|
+
import { OperationSchema } from './components/OperationSchema.tsx'
|
|
6
|
+
|
|
7
|
+
import type { Operation } from '@kubb/oas'
|
|
8
|
+
import type { OperationMethodResult, OperationsByMethod } from '@kubb/plugin-oas'
|
|
9
|
+
import { Operations } from './components/Operations.tsx'
|
|
10
|
+
import type { FileMeta, PluginZod } from './types.ts'
|
|
11
|
+
|
|
12
|
+
export class OperationGenerator extends Generator<PluginZod['resolvedOptions'], PluginZod> {
|
|
13
|
+
async all(operations: Operation[], _operationsByMethod: OperationsByMethod): OperationMethodResult<FileMeta> {
|
|
14
|
+
const { pluginManager, oas, plugin, mode } = this.context
|
|
15
|
+
|
|
16
|
+
const root = createRoot({
|
|
17
|
+
logger: pluginManager.logger,
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
const templates = {
|
|
21
|
+
operations: Operations.templates,
|
|
22
|
+
...this.options.templates,
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
root.render(
|
|
26
|
+
<App pluginManager={pluginManager} plugin={plugin} mode={mode}>
|
|
27
|
+
<Oas oas={oas} operations={operations} generator={this}>
|
|
28
|
+
{templates.operations && <Operations.File templates={templates.operations} />}
|
|
29
|
+
</Oas>
|
|
30
|
+
</App>,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
return root.files
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async operation(operation: Operation, options: PluginZod['resolvedOptions']): OperationMethodResult<FileMeta> {
|
|
37
|
+
const { oas, pluginManager, plugin, mode } = this.context
|
|
38
|
+
|
|
39
|
+
const root = createRoot({
|
|
40
|
+
logger: pluginManager.logger,
|
|
41
|
+
})
|
|
42
|
+
root.render(
|
|
43
|
+
<App pluginManager={pluginManager} plugin={{ ...plugin, options }} mode={mode}>
|
|
44
|
+
<Oas oas={oas} operations={[operation]} generator={this}>
|
|
45
|
+
<Oas.Operation operation={operation}>
|
|
46
|
+
<OperationSchema.File />
|
|
47
|
+
</Oas.Operation>
|
|
48
|
+
</Oas>
|
|
49
|
+
</App>,
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
return root.files
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { SchemaObject } from '@kubb/oas'
|
|
2
|
+
import { SchemaGenerator as Generator } from '@kubb/plugin-oas'
|
|
3
|
+
import type { SchemaMethodResult } from '@kubb/plugin-oas'
|
|
4
|
+
import { Oas } from '@kubb/plugin-oas/components'
|
|
5
|
+
import { App, createRoot } from '@kubb/react'
|
|
6
|
+
import { Schema } from './components/Schema.tsx'
|
|
7
|
+
import type { FileMeta, PluginZod } from './types.ts'
|
|
8
|
+
|
|
9
|
+
export class SchemaGenerator extends Generator<PluginZod['resolvedOptions'], PluginZod> {
|
|
10
|
+
async schema(name: string, schema: SchemaObject, options: PluginZod['resolvedOptions']): SchemaMethodResult<FileMeta> {
|
|
11
|
+
const { oas, pluginManager, plugin, mode, output } = this.context
|
|
12
|
+
|
|
13
|
+
const root = createRoot({
|
|
14
|
+
logger: pluginManager.logger,
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
const tree = this.parse({ schema, name })
|
|
18
|
+
|
|
19
|
+
root.render(
|
|
20
|
+
<App pluginManager={pluginManager} plugin={{ ...plugin, options }} mode={mode}>
|
|
21
|
+
<Oas oas={oas}>
|
|
22
|
+
<Oas.Schema name={name} value={schema} tree={tree}>
|
|
23
|
+
<Schema.File />
|
|
24
|
+
</Oas.Schema>
|
|
25
|
+
</Oas>
|
|
26
|
+
</App>,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
return root.files
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { schemaKeywords } from '@kubb/plugin-oas'
|
|
2
|
+
import { Oas } from '@kubb/plugin-oas/components'
|
|
3
|
+
import { useOas, useOperation, useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
4
|
+
import { File, Parser, useApp } from '@kubb/react'
|
|
5
|
+
|
|
6
|
+
import { SchemaGenerator } from '../SchemaGenerator.tsx'
|
|
7
|
+
|
|
8
|
+
import type { OperationSchema as OperationSchemaType } from '@kubb/plugin-oas'
|
|
9
|
+
import type { ReactNode } from 'react'
|
|
10
|
+
import type { FileMeta, PluginZod } from '../types.ts'
|
|
11
|
+
import { Schema } from './Schema.tsx'
|
|
12
|
+
|
|
13
|
+
type Props = {
|
|
14
|
+
description?: string
|
|
15
|
+
keysToOmit?: string[]
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function OperationSchema({ description, keysToOmit }: Props): ReactNode {
|
|
19
|
+
return <Schema keysToOmit={keysToOmit} withTypeAnnotation={false} description={description} />
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type FileProps = {}
|
|
23
|
+
|
|
24
|
+
OperationSchema.File = function ({}: FileProps): ReactNode {
|
|
25
|
+
const { pluginManager, plugin, mode } = useApp<PluginZod>()
|
|
26
|
+
const { getSchemas, getFile } = useOperationManager()
|
|
27
|
+
const oas = useOas()
|
|
28
|
+
const operation = useOperation()
|
|
29
|
+
|
|
30
|
+
const file = getFile(operation)
|
|
31
|
+
const schemas = getSchemas(operation)
|
|
32
|
+
const generator = new SchemaGenerator(plugin.options, {
|
|
33
|
+
oas,
|
|
34
|
+
plugin,
|
|
35
|
+
pluginManager,
|
|
36
|
+
mode,
|
|
37
|
+
override: plugin.options.override,
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
const items = [schemas.pathParams, schemas.queryParams, schemas.headerParams, schemas.statusCodes, schemas.request, schemas.response].flat().filter(Boolean)
|
|
41
|
+
|
|
42
|
+
const mapItem = ({ name, schema, description, keysToOmit, ...options }: OperationSchemaType, i: number) => {
|
|
43
|
+
// hack so Params can be optional when needed
|
|
44
|
+
const required = Array.isArray(schema?.required) ? !!schema.required.length : !!schema?.required
|
|
45
|
+
const optional = !required && !!name.includes('Params')
|
|
46
|
+
const tree = generator.parse({ schema, name })
|
|
47
|
+
|
|
48
|
+
return (
|
|
49
|
+
<Oas.Schema key={i} name={name} value={schema} tree={[...tree, optional ? { keyword: schemaKeywords.optional } : undefined].filter(Boolean)}>
|
|
50
|
+
{mode === 'split' && <Oas.Schema.Imports extName={plugin.options.extName} />}
|
|
51
|
+
<File.Source>
|
|
52
|
+
<OperationSchema description={description} keysToOmit={keysToOmit} />
|
|
53
|
+
</File.Source>
|
|
54
|
+
</Oas.Schema>
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<Parser language="typescript">
|
|
60
|
+
<File<FileMeta> baseName={file.baseName} path={file.path} meta={file.meta}>
|
|
61
|
+
<File.Import name={['z']} path={plugin.options.importPath} />
|
|
62
|
+
{items.map(mapItem)}
|
|
63
|
+
</File>
|
|
64
|
+
</Parser>
|
|
65
|
+
)
|
|
66
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { useOperationManager, useOperations } from '@kubb/plugin-oas/hooks'
|
|
2
|
+
import { Const, File, Parser, useApp } from '@kubb/react'
|
|
3
|
+
|
|
4
|
+
import transformers from '@kubb/core/transformers'
|
|
5
|
+
import type { HttpMethod, Operation } from '@kubb/oas'
|
|
6
|
+
import type { KubbNode } from '@kubb/react'
|
|
7
|
+
import type { ComponentProps, ComponentType } from 'react'
|
|
8
|
+
import type { FileMeta, PluginZod } from '../types.ts'
|
|
9
|
+
|
|
10
|
+
type TemplateProps = {
|
|
11
|
+
/**
|
|
12
|
+
* Name of the function
|
|
13
|
+
*/
|
|
14
|
+
operationsName: string
|
|
15
|
+
/**
|
|
16
|
+
* Name of the function
|
|
17
|
+
*/
|
|
18
|
+
pathsName: string
|
|
19
|
+
operations: Operation[]
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function Template({ operationsName, pathsName, operations }: TemplateProps): KubbNode {
|
|
23
|
+
const { groupSchemasByName } = useOperationManager()
|
|
24
|
+
const transformedOperations = operations.map((operation) => ({ operation, data: groupSchemasByName(operation, { type: 'function' }) }))
|
|
25
|
+
|
|
26
|
+
const operationsJSON = transformedOperations.reduce(
|
|
27
|
+
(prev, acc) => {
|
|
28
|
+
prev[`"${acc.operation.getOperationId()}"`] = acc.data
|
|
29
|
+
|
|
30
|
+
return prev
|
|
31
|
+
},
|
|
32
|
+
{} as Record<string, unknown>,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
const pathsJSON = transformedOperations.reduce(
|
|
36
|
+
(prev, acc) => {
|
|
37
|
+
prev[`"${acc.operation.path}"`] = {
|
|
38
|
+
...(prev[`"${acc.operation.path}"`] || ({} as Record<HttpMethod, string>)),
|
|
39
|
+
[acc.operation.method]: `operations["${acc.operation.getOperationId()}"]`,
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return prev
|
|
43
|
+
},
|
|
44
|
+
{} as Record<string, Record<HttpMethod, string>>,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
return (
|
|
48
|
+
<>
|
|
49
|
+
<Const export name={operationsName} asConst>
|
|
50
|
+
{`{${transformers.stringifyObject(operationsJSON)}}`}
|
|
51
|
+
</Const>
|
|
52
|
+
<Const export name={pathsName} asConst>
|
|
53
|
+
{`{${transformers.stringifyObject(pathsJSON)}}`}
|
|
54
|
+
</Const>
|
|
55
|
+
</>
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
type RootTemplateProps = {
|
|
60
|
+
children?: React.ReactNode
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function RootTemplate({ children }: RootTemplateProps) {
|
|
64
|
+
const {
|
|
65
|
+
mode,
|
|
66
|
+
pluginManager,
|
|
67
|
+
plugin: {
|
|
68
|
+
key: pluginKey,
|
|
69
|
+
options: { extName },
|
|
70
|
+
},
|
|
71
|
+
} = useApp<PluginZod>()
|
|
72
|
+
const { getFile } = useOperationManager()
|
|
73
|
+
const operations = useOperations()
|
|
74
|
+
const { groupSchemasByName } = useOperationManager()
|
|
75
|
+
const transformedOperations = operations.map((operation) => ({ operation, data: groupSchemasByName(operation, { type: 'function' }) }))
|
|
76
|
+
|
|
77
|
+
const file = pluginManager.getFile({ name: 'operations', extName: '.ts', pluginKey })
|
|
78
|
+
const imports = Object.entries(transformedOperations)
|
|
79
|
+
.map(([_key, { data, operation }], index) => {
|
|
80
|
+
const names = [data.request, ...Object.values(data.responses), ...Object.values(data.parameters)].filter(Boolean)
|
|
81
|
+
|
|
82
|
+
return <File.Import key={index} extName={extName} name={names} root={file.path} path={getFile(operation).path} />
|
|
83
|
+
})
|
|
84
|
+
.filter(Boolean)
|
|
85
|
+
|
|
86
|
+
return (
|
|
87
|
+
<Parser language="typescript">
|
|
88
|
+
<File<FileMeta> baseName={file.baseName} path={file.path} meta={file.meta} exportable={false}>
|
|
89
|
+
{mode === 'split' && imports}
|
|
90
|
+
<File.Source>{children}</File.Source>
|
|
91
|
+
</File>
|
|
92
|
+
</Parser>
|
|
93
|
+
)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const defaultTemplates = { default: Template, root: RootTemplate } as const
|
|
97
|
+
|
|
98
|
+
type Templates = Partial<typeof defaultTemplates>
|
|
99
|
+
|
|
100
|
+
type Props = {
|
|
101
|
+
/**
|
|
102
|
+
* This will make it possible to override the default behaviour.
|
|
103
|
+
*/
|
|
104
|
+
Template?: ComponentType<ComponentProps<typeof Template>>
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function Operations({ Template = defaultTemplates.default }: Props): KubbNode {
|
|
108
|
+
const operations = useOperations()
|
|
109
|
+
|
|
110
|
+
return <Template operationsName="operations" pathsName="paths" operations={operations} />
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
type FileProps = {
|
|
114
|
+
/**
|
|
115
|
+
* This will make it possible to override the default behaviour.
|
|
116
|
+
*/
|
|
117
|
+
templates?: Templates
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
Operations.File = function (props: FileProps): KubbNode {
|
|
121
|
+
const templates = { ...defaultTemplates, ...props.templates }
|
|
122
|
+
|
|
123
|
+
const Template = templates.default
|
|
124
|
+
const RootTemplate = templates.root
|
|
125
|
+
|
|
126
|
+
return (
|
|
127
|
+
<RootTemplate>
|
|
128
|
+
<Operations Template={Template} />
|
|
129
|
+
</RootTemplate>
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
Operations.templates = defaultTemplates
|