@kubb/plugin-solid-query 0.0.0-canary-20241104172400

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