Skip to content

Commit

Permalink
chore: example for react-query templates
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Mar 5, 2024
1 parent c5e4921 commit 1ff5e6f
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 30 deletions.
4 changes: 2 additions & 2 deletions examples/client/kubb.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineConfig } from '@kubb/core'

import { templates } from './templates/CustomClientTemplate'
import * as client from './templates/client/index'

export default defineConfig(async () => {
await setTimeout(() => {
Expand Down Expand Up @@ -52,7 +52,7 @@ export default defineConfig(async () => {
pattern: 'user',
options: {
templates: {
client: templates,
client: client.templates,
},
},
},
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion examples/client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"noEmit": true
},
"include": [
"./src/**/*"
"./src/**/*",
"./templates/**/*"
],
"exclude": [
"**/node_modules",
Expand Down
6 changes: 0 additions & 6 deletions examples/react-query-v5/kubb-log.log

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { defineConfig } from '@kubb/core'
import createSwagger from '@kubb/swagger'
import createSwaggerTanstackQuery from '@kubb/swagger-tanstack-query'
import createSwaggerTS from '@kubb/swagger-ts'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerTanstackQuery } from '@kubb/swagger-tanstack-query'
import { definePlugin as createSwaggerTS } from '@kubb/swagger-ts'

import * as queryKey from './templates/queryKey/index'

/** @type {import('@kubb/core').UserConfig} */
export const config = {
Expand Down Expand Up @@ -50,6 +52,9 @@ export const config = {
initialPageParam: 0,
cursorParam: undefined,
},
templates: {
queryKey: queryKey.templates,
},
},
}],
}),
Expand Down
1 change: 1 addition & 0 deletions examples/react-query-v5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"dependencies": {
"@kubb/cli": "workspace:*",
"@kubb/core": "workspace:*",
"@kubb/react": "workspace:*",
"@kubb/swagger": "workspace:*",
"@kubb/swagger-client": "workspace:*",
"@kubb/swagger-tanstack-query": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type FindPetsByTags = {
return: Awaited<ReturnType<FindPetsByTagsClient>>
}
}
export const findPetsByTagsQueryKey = (params?: FindPetsByTags['queryParams']) => ['v5', { url: '/pet/findByTags' }, ...(params ? [params] : [])] as const
export const findPetsByTagsQueryKey = (params?: FindPetsByTags['queryParams']) => ['/pet/findByTags', ...(params ? [params] : [])] as const
export type FindPetsByTagsQueryKey = ReturnType<typeof findPetsByTagsQueryKey>
export function findPetsByTagsQueryOptions(params?: FindPetsByTags['queryParams'], options: FindPetsByTags['client']['parameters'] = {}) {
const queryKey = findPetsByTagsQueryKey(params)
Expand Down Expand Up @@ -69,8 +69,7 @@ export function useFindPetsByTagsHook<
query.queryKey = queryKey as TQueryKey
return query
}
export const findPetsByTagsInfiniteQueryKey = (params?: FindPetsByTags['queryParams']) =>
['v5', { url: '/pet/findByTags' }, ...(params ? [params] : [])] as const
export const findPetsByTagsInfiniteQueryKey = (params?: FindPetsByTags['queryParams']) => ['/pet/findByTags', ...(params ? [params] : [])] as const
export type FindPetsByTagsInfiniteQueryKey = ReturnType<typeof findPetsByTagsInfiniteQueryKey>
export function findPetsByTagsInfiniteQueryOptions(params?: FindPetsByTags['queryParams'], options: FindPetsByTags['client']['parameters'] = {}) {
const queryKey = findPetsByTagsInfiniteQueryKey(params)
Expand Down Expand Up @@ -120,8 +119,7 @@ export function useFindPetsByTagsHookInfinite<
query.queryKey = queryKey as TQueryKey
return query
}
export const findPetsByTagsSuspenseQueryKey = (params?: FindPetsByTags['queryParams']) =>
['v5', { url: '/pet/findByTags' }, ...(params ? [params] : [])] as const
export const findPetsByTagsSuspenseQueryKey = (params?: FindPetsByTags['queryParams']) => ['/pet/findByTags', ...(params ? [params] : [])] as const
export type FindPetsByTagsSuspenseQueryKey = ReturnType<typeof findPetsByTagsSuspenseQueryKey>
export function findPetsByTagsSuspenseQueryOptions(params?: FindPetsByTags['queryParams'], options: FindPetsByTags['client']['parameters'] = {}) {
const queryKey = findPetsByTagsSuspenseQueryKey(params)
Expand Down
36 changes: 36 additions & 0 deletions examples/react-query-v5/templates/queryKey/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Type, Function } from '@kubb/react'
import { useOperation, useSchemas } from '@kubb/swagger/hooks'
import { QueryKey } from '@kubb/swagger-tanstack-query/components'
import React from 'react'
import { URLObject, URLPath } from '@kubb/core/utils'

export const templates = {
...QueryKey.templates,
react: function({ name, typeName, params, generics, returnType, JSDoc }: React.ComponentProps<typeof QueryKey.templates.react>) {
const schemas = useSchemas()
const operation = useOperation()
const path = new URLPath(operation.path)
const withQueryParams = !!schemas.queryParams?.name

const pathObject = path.toObject({
type: 'path',
}) as URLObject

const keys = [
JSON.stringify(pathObject.url),
withQueryParams ? `...(params ? [params] : [])` : undefined,
].filter(Boolean)

return (
<>
<Function.Arrow name={name} export generics={generics} params={params} returnType={returnType} singleLine JSDoc={JSDoc}>
{`[${keys}] as const`}
</Function.Arrow>

<Type name={typeName} export>
{`ReturnType<typeof ${name}>`}
</Type>
</>
)
},
} as const
3 changes: 2 additions & 1 deletion examples/react-query-v5/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"noEmit": true
},
"include": [
"./src/**/*"
"./src/**/*",
"./templates/**/*"
],
"exclude": [
"**/node_modules",
Expand Down
20 changes: 16 additions & 4 deletions packages/swagger-client/src/OperationGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ export class OperationGenerator extends Generator<PluginOptions['resolvedOptions

const root = createRoot<AppContextProps>({ logger: pluginManager.logger })

if (!this.options.templates.operations) {
const templates = {
operations: Operations.templates,
client: Client.templates,
...this.options.templates,
}

if (!templates.operations) {

Check warning on line 23 in packages/swagger-client/src/OperationGenerator.tsx

View check run for this annotation

Codecov / codecov/patch

packages/swagger-client/src/OperationGenerator.tsx#L17-L23

Added lines #L17 - L23 were not covered by tests
return []
}

root.render(<Operations.File name="operations" paths={paths} templates={this.options.templates.operations} />, { meta: { oas, pluginManager, plugin } })
root.render(<Operations.File name="operations" paths={paths} templates={templates.operations} />, { meta: { oas, pluginManager, plugin } })

Check warning on line 27 in packages/swagger-client/src/OperationGenerator.tsx

View check run for this annotation

Codecov / codecov/patch

packages/swagger-client/src/OperationGenerator.tsx#L27

Added line #L27 was not covered by tests

return root.files
}
Expand All @@ -28,11 +34,17 @@ export class OperationGenerator extends Generator<PluginOptions['resolvedOptions

const root = createRoot<AppContextProps<PluginOptions['appMeta']>>({ logger: pluginManager.logger })

if (!options.templates.client) {
const templates = {
operations: Operations.templates,
client: Client.templates,
...options.templates,
}

if (!templates.client) {
return []
}

root.render(<Client.File templates={options.templates.client} />, { meta: { oas, pluginManager, plugin: { ...plugin, options }, schemas, operation } })
root.render(<Client.File templates={templates.client} />, { meta: { oas, pluginManager, plugin: { ...plugin, options }, schemas, operation } })

return root.files
}
Expand Down
20 changes: 16 additions & 4 deletions packages/swagger-msw/src/OperationGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ export class OperationGenerator extends Generator<PluginOptions['resolvedOptions

const root = createRoot<AppContextProps>({ logger: pluginManager.logger })

if (!this.options.templates?.handlers) {
const templates = {
handlers: Handlers.templates,
mock: Mock.templates,
...this.options.templates,
}

if (!templates?.handlers) {

Check warning on line 24 in packages/swagger-msw/src/OperationGenerator.tsx

View check run for this annotation

Codecov / codecov/patch

packages/swagger-msw/src/OperationGenerator.tsx#L18-L24

Added lines #L18 - L24 were not covered by tests
return []
}

root.render(<Handlers.File name="handlers" paths={paths} templates={this.options.templates.handlers} />, { meta: { oas, pluginManager, plugin } })
root.render(<Handlers.File name="handlers" paths={paths} templates={templates.handlers} />, { meta: { oas, pluginManager, plugin } })

Check warning on line 28 in packages/swagger-msw/src/OperationGenerator.tsx

View check run for this annotation

Codecov / codecov/patch

packages/swagger-msw/src/OperationGenerator.tsx#L28

Added line #L28 was not covered by tests

return root.files
}
Expand All @@ -29,11 +35,17 @@ export class OperationGenerator extends Generator<PluginOptions['resolvedOptions

const root = createRoot<AppContextProps<PluginOptions['appMeta']>>({ logger: pluginManager.logger })

if (!options.templates?.mock) {
const templates = {
handlers: Handlers.templates,
mock: Mock.templates,
...options.templates,
}

if (!templates?.mock) {

Check warning on line 44 in packages/swagger-msw/src/OperationGenerator.tsx

View check run for this annotation

Codecov / codecov/patch

packages/swagger-msw/src/OperationGenerator.tsx#L38-L44

Added lines #L38 - L44 were not covered by tests
return []
}

root.render(<Mock.File templates={options.templates.mock} />, { meta: { oas, pluginManager, plugin: { ...plugin, options }, schemas, operation } })
root.render(<Mock.File templates={templates.mock} />, { meta: { oas, pluginManager, plugin: { ...plugin, options }, schemas, operation } })

Check warning on line 48 in packages/swagger-msw/src/OperationGenerator.tsx

View check run for this annotation

Codecov / codecov/patch

packages/swagger-msw/src/OperationGenerator.tsx#L48

Added line #L48 was not covered by tests

return root.files
}
Expand Down
26 changes: 22 additions & 4 deletions packages/swagger-tanstack-query/src/OperationGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { OperationGenerator as Generator } from '@kubb/swagger'

import { Mutation } from './components/Mutation.tsx'
import { Query } from './components/Query.tsx'
import { QueryKey } from './components/QueryKey.tsx'
import { QueryOptions } from './components/QueryOptions.tsx'

import type { KubbFile } from '@kubb/core'
import type { AppContextProps } from '@kubb/react'
Expand All @@ -20,12 +22,20 @@ export class OperationGenerator extends Generator<PluginOptions['resolvedOptions

const root = createRoot<AppContextProps<PluginOptions['appMeta']>>({ logger: pluginManager.logger })

if (!options.templates?.query || !options.templates?.queryKey || !options.templates.queryOptions) {
const templates = {
mutation: Mutation.templates,
query: Query.templates,
queryOptions: QueryOptions.templates,
queryKey: QueryKey.templates,
...options.templates,
}

if (!templates?.query || !templates?.queryKey || !templates.queryOptions) {

Check warning on line 33 in packages/swagger-tanstack-query/src/OperationGenerator.tsx

View check run for this annotation

Codecov / codecov/patch

packages/swagger-tanstack-query/src/OperationGenerator.tsx#L25-L33

Added lines #L25 - L33 were not covered by tests
return []
}

root.render(
<Query.File templates={{ query: options.templates.query, queryKey: options.templates.queryKey, queryOptions: options.templates.queryOptions }} />,
<Query.File templates={{ query: templates.query, queryKey: templates.queryKey, queryOptions: templates.queryOptions }} />,

Check warning on line 38 in packages/swagger-tanstack-query/src/OperationGenerator.tsx

View check run for this annotation

Codecov / codecov/patch

packages/swagger-tanstack-query/src/OperationGenerator.tsx#L38

Added line #L38 was not covered by tests
{ meta: { oas, pluginManager, plugin: { ...plugin, options }, schemas, operation } },
)

Expand All @@ -37,11 +47,19 @@ export class OperationGenerator extends Generator<PluginOptions['resolvedOptions

const root = createRoot<AppContextProps<PluginOptions['appMeta']>>({ logger: pluginManager.logger })

if (!options.templates?.mutation) {
const templates = {
mutation: Mutation.templates,
query: Query.templates,
queryOptions: QueryOptions.templates,
queryKey: QueryKey.templates,
...options.templates,
}

if (!templates?.mutation) {

Check warning on line 58 in packages/swagger-tanstack-query/src/OperationGenerator.tsx

View check run for this annotation

Codecov / codecov/patch

packages/swagger-tanstack-query/src/OperationGenerator.tsx#L50-L58

Added lines #L50 - L58 were not covered by tests
return []
}

root.render(<Mutation.File templates={options.templates.mutation} />, { meta: { oas, pluginManager, plugin: { ...plugin, options }, schemas, operation } })
root.render(<Mutation.File templates={templates.mutation} />, { meta: { oas, pluginManager, plugin: { ...plugin, options }, schemas, operation } })

Check warning on line 62 in packages/swagger-tanstack-query/src/OperationGenerator.tsx

View check run for this annotation

Codecov / codecov/patch

packages/swagger-tanstack-query/src/OperationGenerator.tsx#L62

Added line #L62 was not covered by tests

return root.files
}
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1ff5e6f

Please sign in to comment.