Skip to content

Commit

Permalink
chore: operations for tanstack query
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Mar 9, 2024
1 parent 2e6d2ba commit 412325a
Show file tree
Hide file tree
Showing 22 changed files with 142 additions and 33 deletions.
4 changes: 2 additions & 2 deletions docs/plugins/development/system.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
name: pluginName,
options,
pre: [],
resolvePath(baseName, directory, options) {
resolvePath(baseName, mode, options) {
const root = path.resolve(this.config.root, this.config.output.path)

return path.resolve(root, output.path, baseName)
Expand Down Expand Up @@ -176,7 +176,7 @@ Add some extra functionality to your plugin, here you can even use functions whi

This will be called when pluginManager.resolvePath is called, see [Pluginmanager and resolving a path](/reference/pluginManager/#pluginmanager-resolvepath).

- **Type:** `(this: PluginContext, baseName: string, directory?: string | undefined, options?: object) => KubbFile.OptionalPath` <br/>
- **Type:** `(this: PluginContext, baseName: string, mode?: 'file' | 'directory', options?: object) => KubbFile.OptionalPath` <br/>

### resolveName

Expand Down
4 changes: 2 additions & 2 deletions examples/client/templates/client/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export const templates = {
const clientParams = [client.path.template, client.withData ? 'data' : undefined, 'options'].filter(Boolean).join(', ')

return (
<Editor language="typescript">
<>
<File.Import name="axios" path="axios" />
<Function name={name} async export generics={generics} returnType={returnType} params={params} JSDoc={JSDoc}>
{`return axios.${client.method}(${clientParams})`}
</Function>
</Editor>
</>
)
},
} as const
4 changes: 4 additions & 0 deletions examples/react-query-v5/kubb.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import createSwaggerTanstackQuery from '@kubb/swagger-tanstack-query'
import createSwaggerTS from '@kubb/swagger-ts'

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

/** @type {import('@kubb/core').UserConfig} */
export const config = {
Expand Down Expand Up @@ -57,6 +58,9 @@ export const config = {
},
},
}],
templates: {
operations: operations.templates,
},
}),
],
}
Expand Down
1 change: 1 addition & 0 deletions examples/react-query-v5/src/gen/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./operations";
export * from "./useAddPetHook";
export * from "./useCreateUserHook";
export * from "./useCreateUsersWithListInputHook";
Expand Down
60 changes: 60 additions & 0 deletions examples/react-query-v5/src/gen/hooks/operations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
export function updatePet() {
return "/pet";
}
export function addPet() {
return "/pet";
}
export function findPetsByStatus() {
return "/pet/findByStatus";
}
export function findPetsByTags() {
return "/pet/findByTags";
}
export function getPetById() {
return "/pet/{petId}";
}
export function updatePetWithForm() {
return "/pet/{petId}";
}
export function deletePet() {
return "/pet/{petId}";
}
export function uploadFile() {
return "/pet/{petId}/uploadImage";
}
export function getInventory() {
return "/store/inventory";
}
export function placeOrder() {
return "/store/order";
}
export function placeOrderPatch() {
return "/store/order";
}
export function getOrderById() {
return "/store/order/{orderId}";
}
export function deleteOrder() {
return "/store/order/{orderId}";
}
export function createUser() {
return "/user";
}
export function createUsersWithListInput() {
return "/user/createWithList";
}
export function loginUser() {
return "/user/login";
}
export function logoutUser() {
return "/user/logout";
}
export function getUserByName() {
return "/user/{username}";
}
export function updateUser() {
return "/user/{username}";
}
export function deleteUser() {
return "/user/{username}";
}
44 changes: 44 additions & 0 deletions examples/react-query-v5/templates/operations/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Editor, Function, File, useFile, usePlugin } from '@kubb/react'
import { useOperations } from '@kubb/swagger/hooks'
import { Operations } from '@kubb/swagger-tanstack-query/components'
import React from 'react'
import { FileMeta, PluginOptions } from '@kubb/swagger-tanstack-query'
import { Operation } from '@kubb/swagger/oas'

export const templates = {
...Operations.templates,
editor: function({ children }: React.ComponentProps<typeof Operations.templates.editor>) {
const { key: pluginKey } = usePlugin<PluginOptions>()

const file = useFile({ name: 'operations', extName: '.ts', pluginKey })

return (
<Editor language="typescript">
<File<FileMeta>
baseName={file.baseName}
path={file.path}
meta={file.meta}
>
<File.Source>
{children}
</File.Source>
</File>
</Editor>
)
},
default: function({}: React.ComponentProps<typeof Operations.templates.default>) {
const operations = useOperations()

return (
<>
{operations.map((item) => {
return (
<Function name={item.getOperationId()} export>
return {JSON.stringify(item.path)}
</Function>
)
})}
</>
)
},
} as const
4 changes: 2 additions & 2 deletions examples/react-query-v5/templates/queryKey/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ export const templates = {
].filter(Boolean)

return (
<Editor language="typescript">
<>
<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>
</Editor>
</>
)
},
} as const
4 changes: 2 additions & 2 deletions packages/core/src/PluginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class PluginManager {
const paths = this.hookForPluginSync({
pluginKey: params.pluginKey,
hookName: 'resolvePath',
parameters: [params.baseName, params.directory, params.options as object],
parameters: [params.baseName, params.mode, params.options as object],
})

if (paths && paths?.length > 1 && this.logger.logLevel === LogLevel.debug) {
Expand All @@ -138,7 +138,7 @@ export class PluginManager {
}
return this.hookFirstSync({
hookName: 'resolvePath',
parameters: [params.baseName, params.directory, params.options as object],
parameters: [params.baseName, params.mode, params.options as object],
}).result
}
resolveName = (params: ResolveNameParams): string => {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export type PluginLifecycle<TOptions extends PluginFactoryOptions = PluginFactor
* @type hookFirst
* @example ('./Pet.ts', './src/gen/') => '/src/gen/Pet.ts'
*/
resolvePath?: (this: PluginContext<TOptions>, baseName: string, directory?: string, options?: TOptions['resolvePathOptions']) => KubbFile.OptionalPath
resolvePath?: (this: PluginContext<TOptions>, baseName: string, mode?: KubbFile.Mode, options?: TOptions['resolvePathOptions']) => KubbFile.OptionalPath
/**
* Resolve to a name based on a string.
* Useful when converting to PascalCase or camelCase.
Expand Down Expand Up @@ -288,7 +288,7 @@ export type PluginCache = Record<string, [number, unknown]>
export type ResolvePathParams<TOptions = object> = {
pluginKey?: Plugin['key']
baseName: string
directory?: string | undefined
mode?: KubbFile.Mode
/**
* Options to be passed to 'resolvePath' 3th parameter
*/
Expand Down
5 changes: 3 additions & 2 deletions packages/react/src/hooks/useFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import type { KubbFile, Plugin } from '@kubb/core'

type Props<TOptions = object> = {
name: string
mode?: KubbFile.Mode
extName: KubbFile.Extname
pluginKey: Plugin['key']
options?: TOptions
}

export function useFile<TOptions = object>({ name, extName, pluginKey, options }: Props<TOptions>): KubbFile.File<{ pluginKey: Plugin['key'] }> {
export function useFile<TOptions = object>({ name, mode, extName, pluginKey, options }: Props<TOptions>): KubbFile.File<{ pluginKey: Plugin['key'] }> {
const pluginManager = usePluginManager()

let source = ''
const baseName = `${name}${extName}` as const
const path = pluginManager.resolvePath({ baseName, pluginKey, options })
const path = pluginManager.resolvePath({ baseName, mode, pluginKey, options })

if (!path) {
throw new Error(`Filepath should be defined for resolvedName "${name}" and pluginKey [${JSON.stringify(pluginKey)}]`)
Expand Down
2 changes: 1 addition & 1 deletion packages/swagger-client/src/components/Client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,4 @@ Client.File = function(props: FileProps): KubbNode {
)
}

Client.templates = defaultTemplates as Templates
Client.templates = defaultTemplates
2 changes: 1 addition & 1 deletion packages/swagger-client/src/components/Operations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,4 @@ Operations.File = function(props: FileProps): KubbNode {
)
}

Operations.templates = defaultTemplates as Templates
Operations.templates = defaultTemplates
4 changes: 2 additions & 2 deletions packages/swagger-client/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
},
},
pre: [swaggerPluginName],
resolvePath(baseName, directory, options) {
resolvePath(baseName, pathMode, options) {
const root = path.resolve(this.config.root, this.config.output.path)
const mode = FileManager.getMode(path.resolve(root, output.path))
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))

if (mode === 'file') {
/**
Expand Down
4 changes: 2 additions & 2 deletions packages/swagger-faker/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
unknownType,
},
pre: [swaggerPluginName, swaggerTypeScriptPluginName],
resolvePath(baseName, directory, options) {
resolvePath(baseName, pathMode, options) {
const root = path.resolve(this.config.root, this.config.output.path)
const mode = FileManager.getMode(path.resolve(root, output.path))
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))

if (mode === 'file') {
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/swagger-msw/src/components/Operations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,4 @@ Operations.File = function(props: FileProps): KubbNode {
)
}

Check warning on line 121 in packages/swagger-msw/src/components/Operations.tsx

View check run for this annotation

Codecov / codecov/patch

packages/swagger-msw/src/components/Operations.tsx#L111-L121

Added lines #L111 - L121 were not covered by tests

Operations.templates = defaultTemplates as Templates
Operations.templates = defaultTemplates
4 changes: 2 additions & 2 deletions packages/swagger-msw/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
},
},
pre: [swaggerPluginName, swaggerTypeScriptPluginName, swaggerFakerPluginName],
resolvePath(baseName, directory, options) {
resolvePath(baseName, pathMode, options) {
const root = path.resolve(this.config.root, this.config.output.path)
const mode = FileManager.getMode(path.resolve(root, output.path))
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))

if (mode === 'file') {
/**
Expand Down
4 changes: 2 additions & 2 deletions packages/swagger-swr/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
dataReturnType,
},
pre: [swaggerPluginName],
resolvePath(baseName, directory, options) {
resolvePath(baseName, pathMode, options) {
const root = path.resolve(this.config.root, this.config.output.path)
const mode = FileManager.getMode(path.resolve(root, output.path))
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))

if (mode === 'file') {
/**
Expand Down
4 changes: 2 additions & 2 deletions packages/swagger-tanstack-query/src/components/Operations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type EditorTemplateProps = {

function EditorTemplate({ children }: EditorTemplateProps) {
const { key: pluginKey } = usePlugin<PluginOptions>()
const file = useFile({ name: 'operations', extName: '.ts', pluginKey })
const file = useFile({ name: 'operations', mode: 'directory', extName: '.ts', pluginKey })

return (
<Editor language="typescript">
Expand Down Expand Up @@ -73,4 +73,4 @@ Operations.File = function(props: FileProps): KubbNode {
)
}

Check warning on line 74 in packages/swagger-tanstack-query/src/components/Operations.tsx

View check run for this annotation

Codecov / codecov/patch

packages/swagger-tanstack-query/src/components/Operations.tsx#L64-L74

Added lines #L64 - L74 were not covered by tests

Operations.templates = defaultTemplates as Templates
Operations.templates = defaultTemplates
5 changes: 2 additions & 3 deletions packages/swagger-tanstack-query/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
parser,
},
pre: [swaggerPluginName, swaggerTsPluginName, parser === 'zod' ? swaggerZodPluginName : undefined].filter(Boolean),
resolvePath(baseName, directory, options) {
resolvePath(baseName, pathMode, options) {
const root = path.resolve(this.config.root, this.config.output.path)
const mode = FileManager.getMode(path.resolve(root, output.path))
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))

if (mode === 'file') {
/**
Expand Down Expand Up @@ -127,7 +127,6 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
)

const files = await operationGenerator.build()

await this.addFile(...files)
},
async writeFile(source, writePath) {
Expand Down
4 changes: 2 additions & 2 deletions packages/swagger-ts/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
unknownType,
},
pre: [swaggerPluginName],
resolvePath(baseName, directory, options) {
resolvePath(baseName, pathMode, options) {
const root = path.resolve(this.config.root, this.config.output.path)
const mode = FileManager.getMode(path.resolve(root, output.path))
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))

if (mode === 'file') {
/**
Expand Down
4 changes: 2 additions & 2 deletions packages/swagger-zod/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
unknownType,
},
pre: [swaggerPluginName, typed ? swaggerTypeScriptPluginName : undefined].filter(Boolean),
resolvePath(baseName, directory, options) {
resolvePath(baseName, pathMode, options) {
const root = path.resolve(this.config.root, this.config.output.path)
const mode = FileManager.getMode(path.resolve(root, output.path))
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))

if (mode === 'file') {
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/swagger-zodios/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
baseURL: undefined,
},
pre: [swaggerPluginName, swaggerZodPluginName],
resolvePath(baseName, _directory) {
resolvePath(baseName) {
const root = path.resolve(this.config.root, this.config.output.path)

return path.resolve(root, baseName)
Expand Down

0 comments on commit 412325a

Please sign in to comment.