@kubb/plugin-solid-query 3.0.0-alpha.20

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 (47) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +53 -0
  3. package/dist/chunk-7ZYWNHQM.js +291 -0
  4. package/dist/chunk-7ZYWNHQM.js.map +1 -0
  5. package/dist/chunk-D6TNFQR6.js +285 -0
  6. package/dist/chunk-D6TNFQR6.js.map +1 -0
  7. package/dist/chunk-KBTDHYL2.cjs +298 -0
  8. package/dist/chunk-KBTDHYL2.cjs.map +1 -0
  9. package/dist/chunk-WIE2QWZT.cjs +291 -0
  10. package/dist/chunk-WIE2QWZT.cjs.map +1 -0
  11. package/dist/components.cjs +20 -0
  12. package/dist/components.cjs.map +1 -0
  13. package/dist/components.d.cts +64 -0
  14. package/dist/components.d.ts +64 -0
  15. package/dist/components.js +3 -0
  16. package/dist/components.js.map +1 -0
  17. package/dist/generators.cjs +13 -0
  18. package/dist/generators.cjs.map +1 -0
  19. package/dist/generators.d.cts +8 -0
  20. package/dist/generators.d.ts +8 -0
  21. package/dist/generators.js +4 -0
  22. package/dist/generators.js.map +1 -0
  23. package/dist/index.cjs +125 -0
  24. package/dist/index.cjs.map +1 -0
  25. package/dist/index.d.cts +9 -0
  26. package/dist/index.d.ts +9 -0
  27. package/dist/index.js +118 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/types-DlkKXn9W.d.cts +128 -0
  30. package/dist/types-DlkKXn9W.d.ts +128 -0
  31. package/package.json +102 -0
  32. package/src/components/Query.tsx +121 -0
  33. package/src/components/QueryKey.tsx +73 -0
  34. package/src/components/QueryOptions.tsx +84 -0
  35. package/src/components/index.ts +3 -0
  36. package/src/generators/__snapshots__/clientDataReturnTypeFull.ts +51 -0
  37. package/src/generators/__snapshots__/clientGetImportPath.ts +51 -0
  38. package/src/generators/__snapshots__/findByTags.ts +51 -0
  39. package/src/generators/__snapshots__/findByTagsPathParamsObject.ts +51 -0
  40. package/src/generators/__snapshots__/findByTagsWithCustomQueryKey.ts +51 -0
  41. package/src/generators/__snapshots__/findByTagsWithZod.ts +51 -0
  42. package/src/generators/__snapshots__/postAsQuery.ts +49 -0
  43. package/src/generators/index.ts +1 -0
  44. package/src/generators/queryGenerator.tsx +121 -0
  45. package/src/index.ts +2 -0
  46. package/src/plugin.ts +137 -0
  47. package/src/types.ts +132 -0
@@ -0,0 +1,121 @@
1
+ import transformers from '@kubb/core/transformers'
2
+ import { pluginClientName } from '@kubb/plugin-client'
3
+ import { Client } from '@kubb/plugin-client/components'
4
+ import { createReactGenerator } from '@kubb/plugin-oas'
5
+ import { useOperationManager } from '@kubb/plugin-oas/hooks'
6
+ import { pluginTsName } from '@kubb/plugin-ts'
7
+ import { pluginZodName } from '@kubb/plugin-zod'
8
+ import { File, useApp } from '@kubb/react'
9
+ import { Query, QueryKey, QueryOptions } from '../components'
10
+ import type { PluginSolidQuery } from '../types'
11
+
12
+ export const queryGenerator = createReactGenerator<PluginSolidQuery>({
13
+ name: 'svelte-query',
14
+ Operation({ options, operation }) {
15
+ const {
16
+ plugin: {
17
+ options: { output },
18
+ },
19
+ } = useApp<PluginSolidQuery>()
20
+ const { getSchemas, getName, getFile } = useOperationManager()
21
+
22
+ const isQuery = typeof options.query === 'boolean' ? true : options.query?.methods.some((method) => operation.method === method)
23
+
24
+ const query = {
25
+ name: getName(operation, { type: 'function', prefix: 'create' }),
26
+ typeName: getName(operation, { type: 'type' }),
27
+ file: getFile(operation, { prefix: 'create' }),
28
+ }
29
+
30
+ const client = {
31
+ name: getName(operation, { type: 'function', pluginKey: [pluginClientName] }),
32
+ }
33
+
34
+ const queryOptions = {
35
+ name: transformers.camelCase(`${operation.getOperationId()} QueryOptions`),
36
+ }
37
+
38
+ const queryKey = {
39
+ name: transformers.camelCase(`${operation.getOperationId()} QueryKey`),
40
+ typeName: transformers.pascalCase(`${operation.getOperationId()} QueryKey`),
41
+ }
42
+
43
+ const type = {
44
+ file: getFile(operation, { pluginKey: [pluginTsName] }),
45
+ //todo remove type?
46
+ schemas: getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' }),
47
+ }
48
+
49
+ const zod = {
50
+ file: getFile(operation, { pluginKey: [pluginZodName] }),
51
+ schemas: getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' }),
52
+ }
53
+
54
+ if (!isQuery || typeof options.query === 'boolean') {
55
+ return null
56
+ }
57
+
58
+ return (
59
+ <File baseName={query.file.baseName} path={query.file.path} meta={query.file.meta} banner={output?.banner} footer={output?.footer}>
60
+ {options.parser === 'zod' && <File.Import extName={output?.extName} name={[zod.schemas.response.name]} root={query.file.path} path={zod.file.path} />}
61
+ <File.Import name={['createQuery', 'queryOptions']} path={options.query.importPath} />
62
+ <File.Import name={['QueryKey', 'WithRequired', 'CreateBaseQueryOptions', 'CreateQueryResult']} path={options.query.importPath} isTypeOnly />
63
+ <File.Import name={'client'} path={options.client.importPath} />
64
+ <File.Import name={['RequestConfig']} path={options.client.importPath} isTypeOnly />
65
+ {options.client.dataReturnType === 'full' && <File.Import name={['ResponseConfig']} path={options.client.importPath} isTypeOnly />}
66
+ <File.Import
67
+ extName={output?.extName}
68
+ name={[
69
+ type.schemas.request?.name,
70
+ type.schemas.response.name,
71
+ type.schemas.pathParams?.name,
72
+ type.schemas.queryParams?.name,
73
+ type.schemas.headerParams?.name,
74
+ ...(type.schemas.statusCodes?.map((item) => item.name) || []),
75
+ ].filter(Boolean)}
76
+ root={query.file.path}
77
+ path={type.file.path}
78
+ isTypeOnly
79
+ />
80
+
81
+ <QueryKey
82
+ name={queryKey.name}
83
+ typeName={queryKey.typeName}
84
+ operation={operation}
85
+ pathParamsType={options.pathParamsType}
86
+ typeSchemas={type.schemas}
87
+ keysFn={options.query.key}
88
+ />
89
+ <Client
90
+ name={client.name}
91
+ isExportable={false}
92
+ isIndexable={false}
93
+ baseURL={options.baseURL}
94
+ operation={operation}
95
+ typeSchemas={type.schemas}
96
+ zodSchemas={zod.schemas}
97
+ dataReturnType={options.client.dataReturnType}
98
+ pathParamsType={options.pathParamsType}
99
+ parser={options.parser}
100
+ />
101
+ <QueryOptions
102
+ name={queryOptions.name}
103
+ clientName={client.name}
104
+ queryKeyName={queryKey.name}
105
+ typeSchemas={type.schemas}
106
+ pathParamsType={options.pathParamsType}
107
+ />
108
+ <Query
109
+ name={query.name}
110
+ queryOptionsName={queryOptions.name}
111
+ typeSchemas={type.schemas}
112
+ pathParamsType={options.pathParamsType}
113
+ operation={operation}
114
+ dataReturnType={options.client.dataReturnType}
115
+ queryKeyName={queryKey.name}
116
+ queryKeyTypeName={queryKey.typeName}
117
+ />
118
+ </File>
119
+ )
120
+ },
121
+ })
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { pluginSolidQuery, pluginSolidQueryName } from './plugin.ts'
2
+ export type { PluginSolidQuery } from './types.ts'
package/src/plugin.ts ADDED
@@ -0,0 +1,137 @@
1
+ import path from 'node:path'
2
+
3
+ import { FileManager, PluginManager, createPlugin } from '@kubb/core'
4
+ import { camelCase, pascalCase } from '@kubb/core/transformers'
5
+ import { renderTemplate } from '@kubb/core/utils'
6
+ import { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'
7
+
8
+ import { pluginTsName } from '@kubb/plugin-ts'
9
+ import { pluginZodName } from '@kubb/plugin-zod'
10
+
11
+ import type { Plugin } from '@kubb/core'
12
+ import type { PluginOas } from '@kubb/plugin-oas'
13
+ import { queryGenerator } from './generators'
14
+ import type { PluginSolidQuery } from './types.ts'
15
+
16
+ export const pluginSolidQueryName = 'plugin-solid-query' satisfies PluginSolidQuery['name']
17
+
18
+ export const pluginSolidQuery = createPlugin<PluginSolidQuery>((options) => {
19
+ const {
20
+ output = { path: 'hooks' },
21
+ group,
22
+ exclude = [],
23
+ include,
24
+ override = [],
25
+ parser = 'client',
26
+ transformers = {},
27
+ pathParamsType = 'inline',
28
+ query = {},
29
+ } = options
30
+ const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`
31
+
32
+ return {
33
+ name: pluginSolidQueryName,
34
+ options: {
35
+ output: {
36
+ exportType: 'barrelNamed',
37
+ ...output,
38
+ },
39
+ baseURL: undefined,
40
+ client: {
41
+ importPath: '@kubb/plugin-client/client',
42
+ dataReturnType: 'data',
43
+ pathParamsType: 'inline',
44
+ ...options.client,
45
+ },
46
+ query: {
47
+ key: (key: unknown[]) => key,
48
+ methods: ['get'],
49
+ importPath: '@tanstack/solid-query',
50
+ ...query,
51
+ },
52
+ pathParamsType,
53
+ parser,
54
+ },
55
+ pre: [pluginOasName, pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),
56
+ resolvePath(baseName, pathMode, options) {
57
+ const root = path.resolve(this.config.root, this.config.output.path)
58
+ const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
59
+
60
+ if (mode === 'single') {
61
+ /**
62
+ * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
63
+ * Other plugins then need to call addOrAppend instead of just add from the fileManager class
64
+ */
65
+ return path.resolve(root, output.path)
66
+ }
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
+ return path.resolve(root, output.path, baseName)
75
+ },
76
+ resolveName(name, type) {
77
+ let resolvedName = camelCase(name)
78
+
79
+ if (type === 'file' || type === 'function') {
80
+ resolvedName = camelCase(name, {
81
+ isFile: type === 'file',
82
+ })
83
+ }
84
+ if (type === 'type') {
85
+ resolvedName = pascalCase(name)
86
+ }
87
+
88
+ if (type) {
89
+ return transformers?.name?.(resolvedName, type) || resolvedName
90
+ }
91
+
92
+ return resolvedName
93
+ },
94
+ async buildStart() {
95
+ const [swaggerPlugin]: [Plugin<PluginOas>] = PluginManager.getDependedPlugins<PluginOas>(this.plugins, [pluginOasName])
96
+
97
+ const oas = await swaggerPlugin.context.getOas()
98
+ const root = path.resolve(this.config.root, this.config.output.path)
99
+ const mode = FileManager.getMode(path.resolve(root, output.path))
100
+ const baseURL = await swaggerPlugin.context.getBaseURL()
101
+
102
+ const operationGenerator = new OperationGenerator(
103
+ {
104
+ ...this.plugin.options,
105
+ baseURL,
106
+ },
107
+ {
108
+ oas,
109
+ pluginManager: this.pluginManager,
110
+ plugin: this.plugin,
111
+ contentType: swaggerPlugin.context.contentType,
112
+ exclude,
113
+ include,
114
+ override,
115
+ mode,
116
+ },
117
+ )
118
+
119
+ const files = await operationGenerator.build(queryGenerator)
120
+ await this.addFile(...files)
121
+
122
+ if (this.config.output.exportType) {
123
+ const barrelFiles = await this.fileManager.getBarrelFiles({
124
+ root,
125
+ output,
126
+ files: this.fileManager.files,
127
+ meta: {
128
+ pluginKey: this.plugin.key,
129
+ },
130
+ logger: this.logger,
131
+ })
132
+
133
+ await this.addFile(...barrelFiles)
134
+ }
135
+ },
136
+ }
137
+ })
package/src/types.ts ADDED
@@ -0,0 +1,132 @@
1
+ import type { Output, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
2
+
3
+ import type { HttpMethod } from '@kubb/oas'
4
+ import type { Exclude, Include, Override, ResolvePathOptions } from '@kubb/plugin-oas'
5
+
6
+ type Query = {
7
+ /**
8
+ * Customize the queryKey, here you can specify a suffix.
9
+ */
10
+ key: (key: unknown[]) => unknown[]
11
+ /**
12
+ * Define which HttpMethods can be used for queries
13
+ * @default ['get']
14
+ */
15
+ methods: Array<HttpMethod>
16
+ /**
17
+ * Path to the useQuery that will be used to do the useQuery functionality.
18
+ * It will be used as `import { useQuery } from '${importPath}'`.
19
+ * It allows both relative and absolute path.
20
+ * the path will be applied as is, so relative path should be based on the file being generated.
21
+ * @default '@tanstack/svelte-query'
22
+ */
23
+ importPath?: string
24
+ }
25
+
26
+ export type Options = {
27
+ /**
28
+ * @default 'hooks'
29
+ */
30
+ output?: Output
31
+ /**
32
+ * Group the @tanstack/query hooks based on the provided name.
33
+ */
34
+ group?: {
35
+ /**
36
+ * Tag will group based on the operation tag inside the Swagger file
37
+ */
38
+ type: 'tag'
39
+ /**
40
+ * Relative path to save the grouped @tanstack/query hooks.
41
+ *
42
+ * `{{tag}}` will be replaced by the current tagName.
43
+ * @example `${output}/{{tag}}Controller` => `hooks/PetController`
44
+ * @default `${output}/{{tag}}Controller`
45
+ */
46
+ output?: string
47
+ /**
48
+ * Name to be used for the `export * as {{exportAs}} from './`
49
+ * @default `"{{tag}}Hooks"`
50
+ */
51
+ exportAs?: string
52
+ }
53
+
54
+ client?: {
55
+ /**
56
+ * Path to the client that will be used to do the API calls.
57
+ * It will be used as `import client from '${client.importPath}'`.
58
+ * It allows both relative and absolute path.
59
+ * the path will be applied as is, so relative path should be based on the file being generated.
60
+ * @default '@kubb/plugin-client/client'
61
+ */
62
+ importPath?: string
63
+ /**
64
+ * ReturnType that needs to be used when calling client().
65
+ *
66
+ * `Data` will return ResponseConfig[data].
67
+ *
68
+ * `Full` will return ResponseConfig.
69
+ * @default `'data'`
70
+ * @private
71
+ */
72
+ dataReturnType?: 'data' | 'full'
73
+ }
74
+ /**
75
+ * ReturnType that needs to be used when calling client().
76
+ *
77
+ * `Data` will return ResponseConfig[data].
78
+ *
79
+ * `Full` will return ResponseConfig.
80
+ * @default `'data'`
81
+ * @private
82
+ */
83
+
84
+ /**
85
+ * Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
86
+ */
87
+ exclude?: Array<Exclude>
88
+ /**
89
+ * Array containing include parameters to include tags/operations/methods/paths.
90
+ */
91
+ include?: Array<Include>
92
+ /**
93
+ * Array containing override parameters to override `options` based on tags/operations/methods/paths.
94
+ */
95
+ override?: Array<Override<ResolvedOptions>>
96
+ /**
97
+ * How to pass your pathParams.
98
+ *
99
+ * `object` will return the pathParams as an object.
100
+ *
101
+ * `inline` will return the pathParams as comma separated params.
102
+ * @default `'inline'`
103
+ * @private
104
+ */
105
+ pathParamsType?: 'object' | 'inline'
106
+ /**
107
+ * Override some useQuery behaviours.
108
+ */
109
+ query?: Partial<Query> | false
110
+ /**
111
+ * Which parser can be used before returning the data to `@tanstack/query`.
112
+ * `'zod'` will use `@kubb/plugin-zod` to parse the data.
113
+ */
114
+ parser?: 'client' | 'zod'
115
+ transformers?: {
116
+ /**
117
+ * Customize the names based on the type that is provided by the plugin.
118
+ */
119
+ name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
120
+ }
121
+ }
122
+
123
+ type ResolvedOptions = {
124
+ output: Output
125
+ baseURL: string | undefined
126
+ client: Required<NonNullable<PluginSolidQuery['options']['client']>>
127
+ parser: Required<NonNullable<Options['parser']>>
128
+ pathParamsType: NonNullable<Options['pathParamsType']>
129
+ query: NonNullable<Required<Query>> | false
130
+ }
131
+
132
+ export type PluginSolidQuery = PluginFactoryOptions<'plugin-solid-query', Options, ResolvedOptions, never, ResolvePathOptions>