@kubb/plugin-react-query 5.0.0-beta.3 → 5.0.0-beta.30

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/README.md +34 -83
  2. package/dist/{components-DTGLu4UV.js → components-CDmg-RPi.js} +275 -255
  3. package/dist/components-CDmg-RPi.js.map +1 -0
  4. package/dist/{components-dAKJEn9b.cjs → components-MPBTffPl.cjs} +299 -255
  5. package/dist/components-MPBTffPl.cjs.map +1 -0
  6. package/dist/components.cjs +1 -1
  7. package/dist/components.d.ts +1 -75
  8. package/dist/components.js +1 -1
  9. package/dist/{generators-C_fbcjpG.js → generators-Bma51Uar.js} +301 -261
  10. package/dist/generators-Bma51Uar.js.map +1 -0
  11. package/dist/{generators-CWEQsdO9.cjs → generators-BtsWNz-6.cjs} +299 -259
  12. package/dist/generators-BtsWNz-6.cjs.map +1 -0
  13. package/dist/generators.cjs +1 -1
  14. package/dist/generators.d.ts +41 -1
  15. package/dist/generators.js +1 -1
  16. package/dist/index.cjs +143 -20
  17. package/dist/index.cjs.map +1 -1
  18. package/dist/index.d.ts +30 -1
  19. package/dist/index.js +143 -20
  20. package/dist/index.js.map +1 -1
  21. package/dist/types-DiZPLTXl.d.ts +400 -0
  22. package/extension.yaml +1507 -0
  23. package/package.json +16 -18
  24. package/src/components/InfiniteQuery.tsx +19 -13
  25. package/src/components/InfiniteQueryOptions.tsx +29 -47
  26. package/src/components/Mutation.tsx +35 -15
  27. package/src/components/MutationOptions.tsx +14 -13
  28. package/src/components/Query.tsx +9 -10
  29. package/src/components/QueryOptions.tsx +6 -27
  30. package/src/components/SuspenseInfiniteQuery.tsx +19 -13
  31. package/src/components/SuspenseInfiniteQueryOptions.tsx +29 -47
  32. package/src/components/SuspenseQuery.tsx +9 -10
  33. package/src/generators/customHookOptionsFileGenerator.tsx +18 -14
  34. package/src/generators/hookOptionsGenerator.tsx +36 -33
  35. package/src/generators/infiniteQueryGenerator.tsx +46 -64
  36. package/src/generators/mutationGenerator.tsx +42 -50
  37. package/src/generators/queryGenerator.tsx +43 -49
  38. package/src/generators/suspenseInfiniteQueryGenerator.tsx +41 -51
  39. package/src/generators/suspenseQueryGenerator.tsx +44 -62
  40. package/src/plugin.ts +42 -16
  41. package/src/resolvers/resolverReactQuery.ts +102 -6
  42. package/src/types.ts +199 -61
  43. package/src/utils.ts +10 -33
  44. package/dist/components-DTGLu4UV.js.map +0 -1
  45. package/dist/components-dAKJEn9b.cjs.map +0 -1
  46. package/dist/generators-CWEQsdO9.cjs.map +0 -1
  47. package/dist/generators-C_fbcjpG.js.map +0 -1
  48. package/dist/types-DfaFRSBf.d.ts +0 -284
@@ -1,11 +1,11 @@
1
+ import { getOperationParameters } from '@internals/shared'
1
2
  import { ast } from '@kubb/core'
2
3
  import type { ResolverTs } from '@kubb/plugin-ts'
3
4
  import { functionPrinter } from '@kubb/plugin-ts'
4
5
  import { File, Function } from '@kubb/renderer-jsx'
5
6
  import type { KubbReactNode } from '@kubb/renderer-jsx/types'
6
7
  import type { Infinite, PluginReactQuery } from '../types.ts'
7
- import { getComments, resolveErrorNames } from '../utils.ts'
8
- import { QueryKey } from './QueryKey.tsx'
8
+ import { buildQueryKeyParams, getComments, resolveErrorNames, resolveSuccessNames } from '../utils.ts'
9
9
  import { getQueryOptionsParams } from './QueryOptions.tsx'
10
10
 
11
11
  type Props = {
@@ -27,7 +27,7 @@ type Props = {
27
27
  const declarationPrinter = functionPrinter({ mode: 'declaration' })
28
28
  const callPrinter = functionPrinter({ mode: 'call' })
29
29
 
30
- function getParams(
30
+ function buildSuspenseInfiniteQueryParamsNode(
31
31
  node: ast.OperationNode,
32
32
  options: {
33
33
  paramsType: PluginReactQuery['resolvedOptions']['paramsType']
@@ -39,7 +39,7 @@ function getParams(
39
39
  },
40
40
  ): ast.FunctionParametersNode {
41
41
  const { paramsType, paramsCasing, pathParamsType, resolver, pageParamGeneric } = options
42
- const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : undefined
42
+ const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : null
43
43
 
44
44
  const optionsParam = ast.createFunctionParameter({
45
45
  name: 'options',
@@ -77,7 +77,8 @@ export function SuspenseInfiniteQuery({
77
77
  initialPageParam,
78
78
  queryParam,
79
79
  }: Props): KubbReactNode {
80
- const responseName = tsResolver.resolveResponseName(node)
80
+ const successNames = resolveSuccessNames(node, tsResolver)
81
+ const responseName = successNames.length > 0 ? successNames.join(' | ') : tsResolver.resolveResponseName(node)
81
82
  const errorNames = resolveErrorNames(node, tsResolver)
82
83
 
83
84
  const responseType = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
@@ -98,17 +99,17 @@ export function SuspenseInfiniteQuery({
98
99
  ? 'boolean'
99
100
  : 'unknown'
100
101
 
101
- const rawQueryParams = node.parameters.filter((p) => p.in === 'query')
102
+ const rawQueryParams = getOperationParameters(node).query
102
103
  const queryParamsTypeName =
103
104
  rawQueryParams.length > 0
104
105
  ? (() => {
105
106
  const groupName = tsResolver.resolveQueryParamsName(node, rawQueryParams[0]!)
106
107
  const individualName = tsResolver.resolveParamName(node, rawQueryParams[0]!)
107
- return groupName !== individualName ? groupName : undefined
108
+ return groupName !== individualName ? groupName : null
108
109
  })()
109
- : undefined
110
+ : null
110
111
 
111
- const queryParamType = queryParam && queryParamsTypeName ? `${queryParamsTypeName}['${queryParam}']` : undefined
112
+ const queryParamType = queryParam && queryParamsTypeName ? `${queryParamsTypeName}['${queryParam}']` : null
112
113
  const pageParamType = queryParamType ? (isInitialPageParamDefined ? `NonNullable<${queryParamType}>` : queryParamType) : fallbackPageParamType
113
114
 
114
115
  const returnType = 'UseSuspenseInfiniteQueryResult<TData, TError> & { queryKey: TQueryKey }'
@@ -120,13 +121,20 @@ export function SuspenseInfiniteQuery({
120
121
  `TPageParam = ${pageParamType}`,
121
122
  ]
122
123
 
123
- const queryKeyParamsNode = QueryKey.getParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
124
+ const queryKeyParamsNode = buildQueryKeyParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
124
125
  const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
125
126
 
126
127
  const queryOptionsParamsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
127
128
  const queryOptionsParamsCall = callPrinter.print(queryOptionsParamsNode) ?? ''
128
129
 
129
- const paramsNode = getParams(node, { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver: tsResolver, pageParamGeneric: 'TPageParam' })
130
+ const paramsNode = buildSuspenseInfiniteQueryParamsNode(node, {
131
+ paramsType,
132
+ paramsCasing,
133
+ pathParamsType,
134
+ dataReturnType,
135
+ resolver: tsResolver,
136
+ pageParamGeneric: 'TPageParam',
137
+ })
130
138
  const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
131
139
 
132
140
  return (
@@ -152,5 +160,3 @@ export function SuspenseInfiniteQuery({
152
160
  </File.Source>
153
161
  )
154
162
  }
155
-
156
- SuspenseInfiniteQuery.getParams = getParams
@@ -1,3 +1,4 @@
1
+ import { getOperationParameters } from '@internals/shared'
1
2
  import { getNestedAccessor } from '@internals/utils'
2
3
  import type { ast } from '@kubb/core'
3
4
  import type { ResolverTs } from '@kubb/plugin-ts'
@@ -5,9 +6,9 @@ import { functionPrinter } from '@kubb/plugin-ts'
5
6
  import { File, Function } from '@kubb/renderer-jsx'
6
7
  import type { KubbReactNode } from '@kubb/renderer-jsx/types'
7
8
  import type { Infinite, PluginReactQuery } from '../types.ts'
8
- import { resolveErrorNames } from '../utils.ts'
9
- import { QueryKey } from './QueryKey.tsx'
10
- import { buildEnabledCheck, getQueryOptionsParams } from './QueryOptions.tsx'
9
+ import { buildQueryKeyParams, resolveErrorNames, resolveSuccessNames } from '../utils.ts'
10
+ import { buildEnabledCheck } from '@internals/tanstack-query'
11
+ import { getQueryOptionsParams } from './QueryOptions.tsx'
11
12
 
12
13
  type Props = {
13
14
  name: string
@@ -45,7 +46,8 @@ export function SuspenseInfiniteQueryOptions({
45
46
  queryParam,
46
47
  queryKeyName,
47
48
  }: Props): KubbReactNode {
48
- const responseName = tsResolver.resolveResponseName(node)
49
+ const successNames = resolveSuccessNames(node, tsResolver)
50
+ const responseName = successNames.length > 0 ? successNames.join(' | ') : tsResolver.resolveResponseName(node)
49
51
  const queryFnDataType = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
50
52
  const errorNames = resolveErrorNames(node, tsResolver)
51
53
  const errorType = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
@@ -65,17 +67,17 @@ export function SuspenseInfiniteQueryOptions({
65
67
  ? 'boolean'
66
68
  : 'unknown'
67
69
 
68
- const rawQueryParams = node.parameters.filter((p) => p.in === 'query')
70
+ const rawQueryParams = getOperationParameters(node).query
69
71
  const queryParamsTypeName =
70
72
  rawQueryParams.length > 0
71
73
  ? (() => {
72
74
  const groupName = tsResolver.resolveQueryParamsName(node, rawQueryParams[0]!)
73
75
  const individualName = tsResolver.resolveParamName(node, rawQueryParams[0]!)
74
- return groupName !== individualName ? groupName : undefined
76
+ return groupName !== individualName ? groupName : null
75
77
  })()
76
- : undefined
78
+ : null
77
79
 
78
- const queryParamType = queryParam && queryParamsTypeName ? `${queryParamsTypeName}['${queryParam}']` : undefined
80
+ const queryParamType = queryParam && queryParamsTypeName ? `${queryParamsTypeName}['${queryParam}']` : null
79
81
  const pageParamType = queryParamType ? (isInitialPageParamDefined ? `NonNullable<${queryParamType}>` : queryParamType) : fallbackPageParamType
80
82
 
81
83
  const paramsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
@@ -83,43 +85,33 @@ export function SuspenseInfiniteQueryOptions({
83
85
  const rawParamsCall = callPrinter.print(paramsNode) ?? ''
84
86
  const clientCallStr = rawParamsCall.replace(/\bconfig\b(?=[^,]*$)/, '{ ...config, signal: config.signal ?? signal }')
85
87
 
86
- const queryKeyParamsNode = QueryKey.getParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
88
+ const queryKeyParamsNode = buildQueryKeyParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
87
89
  const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
88
90
 
89
91
  const enabledSource = buildEnabledCheck(queryKeyParamsNode)
90
92
  const enabledText = enabledSource ? `enabled: !!(${enabledSource}),` : ''
91
93
 
92
- const hasNewParams = nextParam !== undefined || previousParam !== undefined
94
+ const hasNewParams = nextParam != null || previousParam != null
93
95
 
94
- let getNextPageParamExpr: string | undefined
95
- let getPreviousPageParamExpr: string | undefined
96
-
97
- if (hasNewParams) {
98
- if (nextParam) {
99
- const accessor = getNestedAccessor(nextParam, 'lastPage')
100
- if (accessor) {
101
- getNextPageParamExpr = `getNextPageParam: (lastPage) => ${accessor}`
102
- }
103
- }
104
- if (previousParam) {
105
- const accessor = getNestedAccessor(previousParam, 'firstPage')
106
- if (accessor) {
107
- getPreviousPageParamExpr = `getPreviousPageParam: (firstPage) => ${accessor}`
108
- }
96
+ const [getNextPageParamExpr, getPreviousPageParamExpr] = (() => {
97
+ if (hasNewParams) {
98
+ const nextAccessor = nextParam ? getNestedAccessor(nextParam, 'lastPage') : null
99
+ const prevAccessor = previousParam ? getNestedAccessor(previousParam, 'firstPage') : null
100
+ return [
101
+ nextAccessor ? `getNextPageParam: (lastPage) => ${nextAccessor}` : null,
102
+ prevAccessor ? `getPreviousPageParam: (firstPage) => ${prevAccessor}` : null,
103
+ ] as const
109
104
  }
110
- } else if (cursorParam) {
111
- getNextPageParamExpr = `getNextPageParam: (lastPage) => lastPage['${cursorParam}']`
112
- getPreviousPageParamExpr = `getPreviousPageParam: (firstPage) => firstPage['${cursorParam}']`
113
- } else {
114
- if (dataReturnType === 'full') {
115
- getNextPageParamExpr =
116
- 'getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage.data) && lastPage.data.length === 0 ? undefined : lastPageParam + 1'
117
- } else {
118
- getNextPageParamExpr =
119
- 'getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1'
105
+ if (cursorParam) {
106
+ return [`getNextPageParam: (lastPage) => lastPage['${cursorParam}']`, `getPreviousPageParam: (firstPage) => firstPage['${cursorParam}']`] as const
120
107
  }
121
- getPreviousPageParamExpr = 'getPreviousPageParam: (_firstPage, _allPages, firstPageParam) => firstPageParam <= 1 ? undefined : firstPageParam - 1'
122
- }
108
+ return [
109
+ dataReturnType === 'full'
110
+ ? 'getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage.data) && lastPage.data.length === 0 ? undefined : lastPageParam + 1'
111
+ : 'getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1',
112
+ 'getPreviousPageParam: (_firstPage, _allPages, firstPageParam) => firstPageParam <= 1 ? undefined : firstPageParam - 1',
113
+ ] as const
114
+ })()
123
115
 
124
116
  const queryOptionsArr = [
125
117
  `initialPageParam: ${typeof initialPageParam === 'string' ? JSON.stringify(initialPageParam) : initialPageParam}`,
@@ -175,13 +167,3 @@ export function SuspenseInfiniteQueryOptions({
175
167
  </File.Source>
176
168
  )
177
169
  }
178
-
179
- SuspenseInfiniteQueryOptions.getParams = (
180
- node: ast.OperationNode,
181
- options: {
182
- paramsType: PluginReactQuery['resolvedOptions']['paramsType']
183
- paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
184
- pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
185
- resolver: ResolverTs
186
- },
187
- ) => getQueryOptionsParams(node, options)
@@ -4,8 +4,7 @@ import { functionPrinter } from '@kubb/plugin-ts'
4
4
  import { File, Function } from '@kubb/renderer-jsx'
5
5
  import type { KubbReactNode } from '@kubb/renderer-jsx/types'
6
6
  import type { PluginReactQuery } from '../types.ts'
7
- import { getComments, resolveErrorNames } from '../utils.ts'
8
- import { QueryKey } from './QueryKey.tsx'
7
+ import { buildQueryKeyParams, getComments, resolveErrorNames, resolveSuccessNames } from '../utils.ts'
9
8
  import { getQueryOptionsParams } from './QueryOptions.tsx'
10
9
 
11
10
  type Props = {
@@ -25,7 +24,7 @@ type Props = {
25
24
  const declarationPrinter = functionPrinter({ mode: 'declaration' })
26
25
  const callPrinter = functionPrinter({ mode: 'call' })
27
26
 
28
- function getParams(
27
+ function buildSuspenseQueryParamsNode(
29
28
  node: ast.OperationNode,
30
29
  options: {
31
30
  paramsType: PluginReactQuery['resolvedOptions']['paramsType']
@@ -36,8 +35,9 @@ function getParams(
36
35
  },
37
36
  ): ast.FunctionParametersNode {
38
37
  const { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver } = options
39
- const responseName = resolver.resolveResponseName(node)
40
- const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : undefined
38
+ const successNames = resolveSuccessNames(node, resolver)
39
+ const responseName = successNames.length > 0 ? successNames.join(' | ') : resolver.resolveResponseName(node)
40
+ const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : null
41
41
  const errorNames = resolveErrorNames(node, resolver)
42
42
 
43
43
  const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
@@ -77,7 +77,8 @@ export function SuspenseQuery({
77
77
  tsResolver,
78
78
  customOptions,
79
79
  }: Props): KubbReactNode {
80
- const responseName = tsResolver.resolveResponseName(node)
80
+ const successNames = resolveSuccessNames(node, tsResolver)
81
+ const responseName = successNames.length > 0 ? successNames.join(' | ') : tsResolver.resolveResponseName(node)
81
82
  const errorNames = resolveErrorNames(node, tsResolver)
82
83
 
83
84
  const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
@@ -85,13 +86,13 @@ export function SuspenseQuery({
85
86
  const returnType = `UseSuspenseQueryResult<${'TData'}, ${TError}> & { queryKey: TQueryKey }`
86
87
  const generics = [`TData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}`]
87
88
 
88
- const queryKeyParamsNode = QueryKey.getParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
89
+ const queryKeyParamsNode = buildQueryKeyParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
89
90
  const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
90
91
 
91
92
  const queryOptionsParamsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
92
93
  const queryOptionsParamsCall = callPrinter.print(queryOptionsParamsNode) ?? ''
93
94
 
94
- const paramsNode = getParams(node, { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver: tsResolver })
95
+ const paramsNode = buildSuspenseQueryParamsNode(node, { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver: tsResolver })
95
96
  const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
96
97
 
97
98
  return (
@@ -117,5 +118,3 @@ export function SuspenseQuery({
117
118
  </File.Source>
118
119
  )
119
120
  }
120
-
121
- SuspenseQuery.getParams = getParams
@@ -2,34 +2,38 @@ import fs from 'node:fs'
2
2
  import path from 'node:path'
3
3
 
4
4
  import { defineGenerator } from '@kubb/core'
5
- import { File, Function, jsxRenderer } from '@kubb/renderer-jsx'
5
+ import { File, Function, jsxRendererSync } from '@kubb/renderer-jsx'
6
6
  import type { PluginReactQuery } from '../types'
7
- import { transformName } from '../utils.ts'
8
7
 
8
+ /**
9
+ * Scaffolds the user-editable `useCustomHookOptions` file when
10
+ * `pluginReactQuery({ customOptions: { ... } })` is configured. The file is
11
+ * only created when it does not already exist, so user edits persist across
12
+ * regeneration.
13
+ */
9
14
  export const customHookOptionsFileGenerator = defineGenerator<PluginReactQuery>({
10
15
  name: 'react-query-custom-hook-options-file',
11
- renderer: jsxRenderer,
16
+ renderer: jsxRendererSync,
12
17
  operations(nodes, ctx) {
13
18
  const { resolver, config, root } = ctx
14
- const { output, customOptions, query, group, transformers } = ctx.options
19
+ const { output, customOptions, query, group } = ctx.options
15
20
 
16
21
  if (!customOptions) return null
17
22
 
18
23
  const override = output.override ?? config.output.override ?? false
19
24
  const { importPath, name } = customOptions
25
+ const hookOptionsName = resolver.resolveHookOptionsName()
26
+ const customHookOptionsName = resolver.resolveCustomHookOptionsName()
20
27
 
21
28
  const reactQueryImportPath = query ? query.importPath : '@tanstack/react-query'
22
29
 
23
- const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1)
24
-
25
30
  let hookFilePath: string
26
31
  const firstNode = nodes[0]
27
32
  if (firstNode) {
28
- const baseName = resolver.resolveName(firstNode.operationId)
29
- const hookName = transformName(`use${capitalize(baseName)}`, 'function', transformers)
33
+ const hookName = resolver.resolveQueryName(firstNode)
30
34
  const hookFile = resolver.resolveFile(
31
35
  { name: hookName, extname: '.ts', tag: firstNode.tags[0] ?? 'default', path: firstNode.path },
32
- { root, output, group },
36
+ { root, output, group: group ?? undefined },
33
37
  )
34
38
  hookFilePath = hookFile.path
35
39
  } else {
@@ -55,9 +59,9 @@ export const customHookOptionsFileGenerator = defineGenerator<PluginReactQuery>(
55
59
  <File baseName={file.baseName} path={file.path}>
56
60
  <File.Import name={['QueryClient']} path={reactQueryImportPath} isTypeOnly />
57
61
  <File.Import name={['useQueryClient']} path={reactQueryImportPath} />
58
- <File.Import name={['HookOptions']} root={file.path} path={path.resolve(root, './index.ts')} />
62
+ <File.Import name={[hookOptionsName]} root={file.path} path={path.resolve(root, './index.ts')} />
59
63
  <File.Source name={file.name} isExportable isIndexable>
60
- <Function name="getCustomHookOptions" params="{ queryClient }: { queryClient: QueryClient }" returnType="Partial<HookOptions>">
64
+ <Function name={customHookOptionsName} params="{ queryClient }: { queryClient: QueryClient }" returnType={`Partial<${hookOptionsName}>`}>
61
65
  {`return {
62
66
  // TODO: Define custom hook options here
63
67
  // Example:
@@ -70,13 +74,13 @@ export const customHookOptionsFileGenerator = defineGenerator<PluginReactQuery>(
70
74
  </Function>
71
75
  <Function
72
76
  name={name}
73
- generics="T extends keyof HookOptions"
77
+ generics={`T extends keyof ${hookOptionsName}`}
74
78
  params="{ hookName, operationId }: { hookName: T, operationId: string }"
75
- returnType="HookOptions[T]"
79
+ returnType={`${hookOptionsName}[T]`}
76
80
  export
77
81
  >
78
82
  {`const queryClient = useQueryClient()
79
- const customOptions = getCustomHookOptions({ queryClient })
83
+ const customOptions = ${customHookOptionsName}({ queryClient })
80
84
  return customOptions[hookName] ?? {}`}
81
85
  </Function>
82
86
  </File.Source>
@@ -1,41 +1,46 @@
1
+ import { getOperationParameters } from '@internals/shared'
1
2
  import { defineGenerator } from '@kubb/core'
2
- import { File, jsxRenderer, Type } from '@kubb/renderer-jsx'
3
+ import { File, jsxRendererSync, Type } from '@kubb/renderer-jsx'
3
4
  import type { KubbReactNode } from '@kubb/renderer-jsx/types'
4
5
  import { difference } from 'remeda'
5
6
  import type { PluginReactQuery } from '../types'
6
- import { resolveOperationOverrides, transformName } from '../utils.ts'
7
+ import { resolveOperationOverrides } from '../utils.ts'
7
8
 
8
9
  type QueryOption = PluginReactQuery['resolvedOptions']['query']
9
10
  type MutationOption = PluginReactQuery['resolvedOptions']['mutation']
10
11
 
12
+ /**
13
+ * Emits the `HookOptions` type used by `customOptions`. Enabled when
14
+ * `pluginReactQuery({ customOptions: { ... } })`. The generated type lists
15
+ * every hook keyed by name so user-supplied options stay in sync with the
16
+ * generated hooks at compile time.
17
+ */
11
18
  export const hookOptionsGenerator = defineGenerator<PluginReactQuery>({
12
19
  name: 'react-query-hook-options',
13
- renderer: jsxRenderer,
20
+ renderer: jsxRendererSync,
14
21
  operations(nodes, ctx) {
15
- const { resolver, config, root, adapter } = ctx
16
- const { output, customOptions, query, mutation, suspense, infinite, group, transformers, override } = ctx.options
22
+ const { resolver, config, root } = ctx
23
+ const { output, customOptions, query, mutation, suspense, infinite, group, override } = ctx.options
17
24
 
18
25
  if (!customOptions) return null
19
26
 
20
- const resolvedFile = resolver.resolveFile({ name: 'HookOptions', extname: '.ts' }, { root, output, group })
27
+ const name = resolver.resolveHookOptionsName()
28
+ const resolvedFile = resolver.resolveFile({ name, extname: '.ts' }, { root, output, group: group ?? undefined })
21
29
  const hookOptionsFile = {
22
30
  ...resolvedFile,
23
- baseName: 'HookOptions.ts' as const,
24
- path: resolvedFile.path.replace(/hookOptions\.ts$/, 'HookOptions.ts'),
31
+ baseName: `${name}.ts` as const,
32
+ path: resolvedFile.path.replace(/[^/\\]+\.ts$/, `${name}.ts`),
25
33
  }
26
34
 
27
- const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1)
28
-
29
- const imports: KubbReactNode[] = []
35
+ const imports: Array<KubbReactNode> = []
30
36
  const hookOptions: Record<string, string> = {}
31
37
 
32
38
  for (const node of nodes) {
33
- const baseName = resolver.resolveName(node.operationId)
34
39
  const opOverrides = resolveOperationOverrides(node, override)
35
40
  const nodeQuery: QueryOption = 'query' in opOverrides ? (opOverrides.query as QueryOption) : query
36
41
  const nodeMutation: MutationOption = 'mutation' in opOverrides ? (opOverrides.mutation as MutationOption) : mutation
37
42
  const nodeInfinite = 'infinite' in opOverrides ? opOverrides.infinite : infinite
38
- const nodeInfiniteOptions = nodeInfinite && typeof nodeInfinite === 'object' ? nodeInfinite : undefined
43
+ const nodeInfiniteOptions = nodeInfinite && typeof nodeInfinite === 'object' ? nodeInfinite : null
39
44
 
40
45
  // query: false means "still a query but skip the useQuery hook"
41
46
  const isQueryOp =
@@ -50,21 +55,21 @@ export const hookOptionsGenerator = defineGenerator<PluginReactQuery>({
50
55
  const isInfiniteOp = !!nodeInfiniteOptions
51
56
 
52
57
  if (isQueryOp) {
53
- const queryOptionsName = transformName(`${baseName}QueryOptions`, 'function', transformers)
54
- const queryHookName = transformName(`use${capitalize(baseName)}`, 'function', transformers)
58
+ const queryOptionsName = resolver.resolveQueryOptionsName(node)
59
+ const queryHookName = resolver.resolveQueryName(node)
55
60
  const queryHookFile = resolver.resolveFile(
56
61
  { name: queryHookName, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
57
- { root, output, group },
62
+ { root, output, group: group ?? undefined },
58
63
  )
59
64
  imports.push(<File.Import name={[queryOptionsName]} root={hookOptionsFile.path} path={queryHookFile.path} />)
60
65
  hookOptions[queryHookName] = `Partial<ReturnType<typeof ${queryOptionsName}>>`
61
66
 
62
67
  if (isSuspenseOp) {
63
- const suspenseOptionsName = transformName(`${baseName}SuspenseQueryOptions`, 'function', transformers)
64
- const suspenseHookName = transformName(`use${capitalize(baseName)}Suspense`, 'function', transformers)
68
+ const suspenseOptionsName = resolver.resolveSuspenseQueryOptionsName(node)
69
+ const suspenseHookName = resolver.resolveSuspenseQueryName(node)
65
70
  const suspenseHookFile = resolver.resolveFile(
66
71
  { name: suspenseHookName, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
67
- { root, output, group },
72
+ { root, output, group: group ?? undefined },
68
73
  )
69
74
  imports.push(<File.Import name={[suspenseOptionsName]} root={hookOptionsFile.path} path={suspenseHookFile.path} />)
70
75
  hookOptions[suspenseHookName] = `Partial<ReturnType<typeof ${suspenseOptionsName}>>`
@@ -73,25 +78,25 @@ export const hookOptionsGenerator = defineGenerator<PluginReactQuery>({
73
78
  if (isInfiniteOp) {
74
79
  // Validate queryParam
75
80
  const normalizeKey = (key: string) => key.replace(/\?$/, '')
76
- const queryParamKeys = node.parameters.filter((p) => p.in === 'query').map((p) => p.name)
81
+ const queryParamKeys = getOperationParameters(node).query.map((p) => p.name)
77
82
  const hasQueryParam = nodeInfiniteOptions!.queryParam ? queryParamKeys.some((k) => normalizeKey(k) === nodeInfiniteOptions!.queryParam) : false
78
83
 
79
84
  if (hasQueryParam) {
80
- const infiniteOptionsName = transformName(`${baseName}InfiniteQueryOptions`, 'function', transformers)
81
- const infiniteHookName = transformName(`use${capitalize(baseName)}Infinite`, 'function', transformers)
85
+ const infiniteOptionsName = resolver.resolveInfiniteQueryOptionsName(node)
86
+ const infiniteHookName = resolver.resolveInfiniteQueryName(node)
82
87
  const infiniteHookFile = resolver.resolveFile(
83
88
  { name: infiniteHookName, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
84
- { root, output, group },
89
+ { root, output, group: group ?? undefined },
85
90
  )
86
91
  imports.push(<File.Import name={[infiniteOptionsName]} root={hookOptionsFile.path} path={infiniteHookFile.path} />)
87
92
  hookOptions[infiniteHookName] = `Partial<ReturnType<typeof ${infiniteOptionsName}>>`
88
93
 
89
94
  if (isSuspenseOp) {
90
- const suspenseInfiniteOptionsName = transformName(`${baseName}SuspenseInfiniteQueryOptions`, 'function', transformers)
91
- const suspenseInfiniteHookName = transformName(`use${capitalize(baseName)}SuspenseInfinite`, 'function', transformers)
95
+ const suspenseInfiniteOptionsName = resolver.resolveSuspenseInfiniteQueryOptionsName(node)
96
+ const suspenseInfiniteHookName = resolver.resolveSuspenseInfiniteQueryName(node)
92
97
  const suspenseInfiniteHookFile = resolver.resolveFile(
93
98
  { name: suspenseInfiniteHookName, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
94
- { root, output, group },
99
+ { root, output, group: group ?? undefined },
95
100
  )
96
101
  imports.push(<File.Import name={[suspenseInfiniteOptionsName]} root={hookOptionsFile.path} path={suspenseInfiniteHookFile.path} />)
97
102
  hookOptions[suspenseInfiniteHookName] = `Partial<ReturnType<typeof ${suspenseInfiniteOptionsName}>>`
@@ -101,26 +106,24 @@ export const hookOptionsGenerator = defineGenerator<PluginReactQuery>({
101
106
  }
102
107
 
103
108
  if (isMutationOp) {
104
- const mutationOptionsName = transformName(`${baseName}MutationOptions`, 'function', transformers)
105
- const mutationHookName = transformName(`use${capitalize(baseName)}`, 'function', transformers)
109
+ const mutationOptionsName = resolver.resolveMutationOptionsName(node)
110
+ const mutationHookName = resolver.resolveMutationName(node)
106
111
  const mutationHookFile = resolver.resolveFile(
107
112
  { name: mutationHookName, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
108
- { root, output, group },
113
+ { root, output, group: group ?? undefined },
109
114
  )
110
115
  imports.push(<File.Import name={[mutationOptionsName]} root={hookOptionsFile.path} path={mutationHookFile.path} />)
111
116
  hookOptions[mutationHookName] = `Partial<ReturnType<typeof ${mutationOptionsName}>>`
112
117
  }
113
118
  }
114
119
 
115
- const name = 'HookOptions'
116
-
117
120
  return (
118
121
  <File
119
122
  baseName={hookOptionsFile.baseName}
120
123
  path={hookOptionsFile.path}
121
124
  meta={hookOptionsFile.meta}
122
- banner={resolver.resolveBanner(adapter.inputNode, { output, config })}
123
- footer={resolver.resolveFooter(adapter.inputNode, { output, config })}
125
+ banner={resolver.resolveBanner(ctx.meta, { output, config, file: { path: hookOptionsFile.path, baseName: hookOptionsFile.baseName } })}
126
+ footer={resolver.resolveFooter(ctx.meta, { output, config, file: { path: hookOptionsFile.path, baseName: hookOptionsFile.baseName } })}
124
127
  >
125
128
  {imports}
126
129
  <File.Source name={name} isExportable isIndexable isTypeOnly>