Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Mar 9, 2024
1 parent 9355259 commit 7fc909a
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 192 deletions.
4 changes: 2 additions & 2 deletions examples/msw/src/gen/msw/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { addPetHandler } from './petHandlers/addPetHandler'
import { updatePetHandler } from './petHandlers/updatePetHandler'
import { addPetHandler } from './petHandlers/addPetHandler'
import { findPetsByStatusHandler } from './petHandlers/findPetsByStatusHandler'
import { findPetsByTagsHandler } from './petHandlers/findPetsByTagsHandler'
import { getPetByIdHandler } from './petHandlers/getPetByIdHandler'
Expand All @@ -20,8 +20,8 @@ import { updateUserHandler } from './userHandlers/updateUserHandler'
import { deleteUserHandler } from './userHandlers/deleteUserHandler'

export const handlers = [
addPetHandler,
updatePetHandler,
addPetHandler,
findPetsByStatusHandler,
findPetsByTagsHandler,
getPetByIdHandler,
Expand Down
77 changes: 53 additions & 24 deletions examples/react-query-v5/src/gen/invalidations.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
import type { UpdatePetMutationResponse, UpdatePetMutationRequest, AddPetMutationResponse, AddPetMutationRequest, FindPetsByStatusQueryResponse, FindPetsByTagsQueryResponse, GetPetByIdQueryResponse, UpdatePetWithFormMutationResponse, DeletePetMutationResponse, UploadFileMutationResponse, UploadFileMutationRequest, GetInventoryQueryResponse, PlaceOrderMutationResponse, PlaceOrderMutationRequest, PlaceOrderPatchMutationResponse, PlaceOrderPatchMutationRequest, GetOrderByIdQueryResponse, DeleteOrderMutationResponse, CreateUserMutationResponse, CreateUserMutationRequest, CreateUsersWithListInputMutationResponse, CreateUsersWithListInputMutationRequest, LoginUserQueryResponse, LogoutUserQueryResponse, GetUserByNameQueryResponse, UpdateUserMutationResponse, UpdateUserMutationRequest, DeleteUserMutationResponse } from "./index";
import type { UseMutationOptions } from "@tanstack/react-query";
import type {
UpdatePetMutationResponse,
UpdatePetMutationRequest,
AddPetMutationResponse,
AddPetMutationRequest,
FindPetsByStatusQueryResponse,
FindPetsByTagsQueryResponse,
GetPetByIdQueryResponse,
UpdatePetWithFormMutationResponse,
DeletePetMutationResponse,
UploadFileMutationResponse,
UploadFileMutationRequest,
GetInventoryQueryResponse,
PlaceOrderMutationResponse,
PlaceOrderMutationRequest,
PlaceOrderPatchMutationResponse,
PlaceOrderPatchMutationRequest,
GetOrderByIdQueryResponse,
DeleteOrderMutationResponse,
CreateUserMutationResponse,
CreateUserMutationRequest,
CreateUsersWithListInputMutationResponse,
CreateUsersWithListInputMutationRequest,
LoginUserQueryResponse,
LogoutUserQueryResponse,
GetUserByNameQueryResponse,
UpdateUserMutationResponse,
UpdateUserMutationRequest,
DeleteUserMutationResponse,
} from './index'
import type { UseMutationOptions } from '@tanstack/react-query'

export type Invalidations = {
"useUpdatePetHook": UseMutationOptions<UpdatePetMutationResponse, unknown, UpdatePetMutationRequest>["onSuccess"];
"useAddPetHook": UseMutationOptions<AddPetMutationResponse, unknown, AddPetMutationRequest>["onSuccess"];
"useFindPetsByStatusHook": UseMutationOptions<FindPetsByStatusQueryResponse, unknown, void>["onSuccess"];
"useFindPetsByTagsHook": UseMutationOptions<FindPetsByTagsQueryResponse, unknown, void>["onSuccess"];
"useGetPetByIdHook": UseMutationOptions<GetPetByIdQueryResponse, unknown, void>["onSuccess"];
"useUpdatePetWithFormHook": UseMutationOptions<UpdatePetWithFormMutationResponse, unknown, void>["onSuccess"];
"useDeletePetHook": UseMutationOptions<DeletePetMutationResponse, unknown, void>["onSuccess"];
"useUploadFileHook": UseMutationOptions<UploadFileMutationResponse, unknown, UploadFileMutationRequest>["onSuccess"];
"useGetInventoryHook": UseMutationOptions<GetInventoryQueryResponse, unknown, void>["onSuccess"];
"usePlaceOrderHook": UseMutationOptions<PlaceOrderMutationResponse, unknown, PlaceOrderMutationRequest>["onSuccess"];
"usePlaceOrderPatchHook": UseMutationOptions<PlaceOrderPatchMutationResponse, unknown, PlaceOrderPatchMutationRequest>["onSuccess"];
"useGetOrderByIdHook": UseMutationOptions<GetOrderByIdQueryResponse, unknown, void>["onSuccess"];
"useDeleteOrderHook": UseMutationOptions<DeleteOrderMutationResponse, unknown, void>["onSuccess"];
"useCreateUserHook": UseMutationOptions<CreateUserMutationResponse, unknown, CreateUserMutationRequest>["onSuccess"];
"useCreateUsersWithListInputHook": UseMutationOptions<CreateUsersWithListInputMutationResponse, unknown, CreateUsersWithListInputMutationRequest>["onSuccess"];
"useLoginUserHook": UseMutationOptions<LoginUserQueryResponse, unknown, void>["onSuccess"];
"useLogoutUserHook": UseMutationOptions<LogoutUserQueryResponse, unknown, void>["onSuccess"];
"useGetUserByNameHook": UseMutationOptions<GetUserByNameQueryResponse, unknown, void>["onSuccess"];
"useUpdateUserHook": UseMutationOptions<UpdateUserMutationResponse, unknown, UpdateUserMutationRequest>["onSuccess"];
"useDeleteUserHook": UseMutationOptions<DeleteUserMutationResponse, unknown, void>["onSuccess"];
};
export type Invalidations = {
'useUpdatePetHook': UseMutationOptions<UpdatePetMutationResponse, unknown, UpdatePetMutationRequest>['onSuccess']
'useAddPetHook': UseMutationOptions<AddPetMutationResponse, unknown, AddPetMutationRequest>['onSuccess']
'useFindPetsByStatusHook': UseMutationOptions<FindPetsByStatusQueryResponse, unknown, void>['onSuccess']
'useFindPetsByTagsHook': UseMutationOptions<FindPetsByTagsQueryResponse, unknown, void>['onSuccess']
'useGetPetByIdHook': UseMutationOptions<GetPetByIdQueryResponse, unknown, void>['onSuccess']
'useUpdatePetWithFormHook': UseMutationOptions<UpdatePetWithFormMutationResponse, unknown, void>['onSuccess']
'useDeletePetHook': UseMutationOptions<DeletePetMutationResponse, unknown, void>['onSuccess']
'useUploadFileHook': UseMutationOptions<UploadFileMutationResponse, unknown, UploadFileMutationRequest>['onSuccess']
'useGetInventoryHook': UseMutationOptions<GetInventoryQueryResponse, unknown, void>['onSuccess']
'usePlaceOrderHook': UseMutationOptions<PlaceOrderMutationResponse, unknown, PlaceOrderMutationRequest>['onSuccess']
'usePlaceOrderPatchHook': UseMutationOptions<PlaceOrderPatchMutationResponse, unknown, PlaceOrderPatchMutationRequest>['onSuccess']
'useGetOrderByIdHook': UseMutationOptions<GetOrderByIdQueryResponse, unknown, void>['onSuccess']
'useDeleteOrderHook': UseMutationOptions<DeleteOrderMutationResponse, unknown, void>['onSuccess']
'useCreateUserHook': UseMutationOptions<CreateUserMutationResponse, unknown, CreateUserMutationRequest>['onSuccess']
'useCreateUsersWithListInputHook': UseMutationOptions<CreateUsersWithListInputMutationResponse, unknown, CreateUsersWithListInputMutationRequest>['onSuccess']
'useLoginUserHook': UseMutationOptions<LoginUserQueryResponse, unknown, void>['onSuccess']
'useLogoutUserHook': UseMutationOptions<LogoutUserQueryResponse, unknown, void>['onSuccess']
'useGetUserByNameHook': UseMutationOptions<GetUserByNameQueryResponse, unknown, void>['onSuccess']
'useUpdateUserHook': UseMutationOptions<UpdateUserMutationResponse, unknown, UpdateUserMutationRequest>['onSuccess']
'useDeleteUserHook': UseMutationOptions<DeleteUserMutationResponse, unknown, void>['onSuccess']
}
2 changes: 1 addition & 1 deletion examples/react-query-v5/templates/mutate/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import transformers from '@kubb/core/transformers'

export const templates = {
...Mutation.templates,
react: function({ name, generics, returnType, params, JSDoc, client, hook, dataReturnType }: React.ComponentProps<typeof Mutation.templates.react>) {
react: function({ name, params, JSDoc, client, hook, dataReturnType }: React.ComponentProps<typeof Mutation.templates.react>) {
const pluginManager = usePluginManager()
const file = useOperationFile()
const clientOptions = [
Expand Down
66 changes: 42 additions & 24 deletions examples/react-query-v5/templates/operations/index.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,62 @@
import { Editor, Function, File, useFile, usePlugin } from '@kubb/react'
import { useOperations } from '@kubb/swagger/hooks'
import { Editor, Type, File, usePlugin } from '@kubb/react'
import { useOperationHelpers, useOperations, useOas } 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'
import path from 'node:path'
import { usePluginManager } from '@kubb/react'

export const templates = {
...Operations.templates,
editor: function({ children }: React.ComponentProps<typeof Operations.templates.editor>) {
editor: function({}: React.ComponentProps<typeof Operations.templates.editor>) {
const pluginManager = usePluginManager()
const { key: pluginKey } = usePlugin<PluginOptions>()
const operations = useOperations()
const { getSchemas } = useOas()
const { getOperationName } = useOperationHelpers()

const root = path.resolve(pluginManager.config.root, pluginManager.config.output.path)

const imports = operations.map(operation => {
const schemas = getSchemas(operation)

return [schemas.response?.name, schemas.request?.name]
}).flat().filter(Boolean)

const invalidations = operations.reduce((acc, operation) => {
const name = getOperationName(operation, { pluginKey, type: 'function' })
const schemas = getSchemas(operation)

acc[name] = `UseMutationOptions<${schemas.response.name}, unknown, ${schemas.request?.name || 'void'}>['onSuccess']`

const file = useFile({ name: 'operations', extName: '.ts', pluginKey })
return acc
}, {} as Record<string, string>)

return (
<Editor language="typescript">
<File<FileMeta>
baseName={file.baseName}
path={file.path}
meta={file.meta}
baseName={'invalidations.ts'}
path={path.join(root, './invalidations.ts')}
>
<File.Import
name={imports}
path={path.join(root, './index.ts')}
root={path.join(root, './invalidations.ts')}
isTypeOnly
/>

<File.Import isTypeOnly name={['UseMutationOptions']} path="@tanstack/react-query" />
<File.Source>
{children}
<Type export name="Invalidations">
{`{ ${
Object.keys(invalidations).map(key => {
return `${JSON.stringify(key)}: ${invalidations[key]}`
})
} }`}
</Type>
</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
2 changes: 1 addition & 1 deletion packages/swagger-client/src/OperationGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class OperationGenerator extends Generator<PluginOptions['resolvedOptions

root.render(
<Oas oas={oas} operations={operations} getSchemas={(...props) => this.getSchemas(...props)}>
{templates.operations && <Operations.File operationsByMethod={operationsByMethod} templates={templates.operations} />}
{templates.operations && <Operations.File templates={templates.operations} />}
</Oas>,

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#L26-L27

Added lines #L26 - L27 were not covered by tests
{ meta: { pluginManager, plugin } },
)
Expand Down
14 changes: 2 additions & 12 deletions packages/swagger-client/src/components/Operations.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Operations } from './Operations.tsx'

import type { Plugin } from '@kubb/core'
import type { AppContextProps } from '@kubb/react'
import type { GetOperationGeneratorOptions, OperationsByMethod } from '@kubb/swagger'
import type { GetOperationGeneratorOptions } from '@kubb/swagger'
import type { PluginOptions } from '../types.ts'

describe('<Operations/>', async () => {
Expand Down Expand Up @@ -44,24 +44,14 @@ describe('<Operations/>', async () => {

test('showPetById', async () => {
const operation = oas.operation('/pets/{pet_id}', 'get')
const schemas = og.getSchemas(operation)

const context: AppContextProps<PluginOptions['appMeta']> = { meta: { pluginManager: mockedPluginManager, plugin } }

const Component = () => {
return (
<Oas oas={oas} operations={[operation]} getSchemas={(...props) => og.getSchemas(...props)}>
<Oas.Operation operation={operation}>
<Operations.File
operationsByMethod={{
'/pets/{pet_id}': {
get: {
operation,
schemas,
},
},
} as unknown as OperationsByMethod}
/>
<Operations.File />
</Oas.Operation>
</Oas>
)
Expand Down
48 changes: 15 additions & 33 deletions packages/swagger-client/src/components/Operations.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { URLPath } from '@kubb/core/utils'
import { Editor, File, usePlugin } from '@kubb/react'
import { useFile } from '@kubb/react'
import { useOas } from '@kubb/swagger/hooks'
import { useOperations } from '@kubb/swagger/hooks'

import type { KubbNode } from '@kubb/react'
import type { OperationsByMethod } from '@kubb/swagger'
import type { HttpMethod, Oas } from '@kubb/swagger/oas'
import type { HttpMethod, Operation } from '@kubb/swagger/oas'
import type { ComponentProps, ComponentType } from 'react'
import type { FileMeta, PluginOptions } from '../types.ts'

Expand All @@ -14,16 +13,25 @@ type TemplateProps = {
* Name of the function
*/
name: string
operations: Record<string, { path: string; method: HttpMethod }>
operations: Operation[]
}

function Template({
name,
operations,
}: TemplateProps): KubbNode {
const operationsObject: Record<string, { path: string; method: HttpMethod }> = {}

operations.forEach(operation => {
operationsObject[operation.getOperationId()] = {
path: new URLPath(operation.path).URL,
method: operation.method,
}
})

return (
<>
{`export const ${name} = ${JSON.stringify(operations)} as const;`}
{`export const ${name} = ${JSON.stringify(operationsObject)} as const;`}
</>
)
}
Expand Down Expand Up @@ -55,40 +63,18 @@ const defaultTemplates = { default: Template, editor: EditorTemplate } as const

type Templates = Partial<typeof defaultTemplates>

function getOperations(oas: Oas, operationsByMethod: OperationsByMethod): Record<string, { path: string; method: HttpMethod }> {
const operations: Record<string, { path: string; method: HttpMethod }> = {}

Object.keys(operationsByMethod).forEach((path) => {
const methods = operationsByMethod[path] || []
Object.keys(methods).forEach((method) => {
const operation = oas.operation(path, method as HttpMethod)
if (operation) {
operations[operation.getOperationId()] = {
path: new URLPath(path).URL,
method: method as HttpMethod,
}
}
})
})

return operations
}

type Props = {
operationsByMethod: OperationsByMethod
/**
* This will make it possible to override the default behaviour.
*/
Template?: ComponentType<ComponentProps<typeof Template>>
}

export function Operations({
operationsByMethod,
Template = defaultTemplates.default,
}: Props): KubbNode {
const { oas } = useOas()
const operations = useOperations()

const operations = getOperations(oas, operationsByMethod)
return (
<Template
name="operations"
Expand All @@ -98,10 +84,6 @@ export function Operations({
}

type FileProps = {
/**
* @deprecated
*/
operationsByMethod: OperationsByMethod
/**
* This will make it possible to override the default behaviour.
*/
Expand All @@ -116,7 +98,7 @@ Operations.File = function(props: FileProps): KubbNode {

return (
<EditorTemplate>
<Operations Template={Template} operationsByMethod={props.operationsByMethod} />
<Operations Template={Template} />
</EditorTemplate>
)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/swagger-msw/src/OperationGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class OperationGenerator extends Generator<PluginOptions['resolvedOptions

root.render(
<Oas oas={oas} operations={operations} getSchemas={(...props) => this.getSchemas(...props)}>
{templates?.operations && <Operations.File operationsByMethod={operationsByMethod} templates={templates.operations} />}
{templates?.operations && <Operations.File templates={templates.operations} />}
</Oas>,
{ meta: { pluginManager, plugin } },
)
Expand Down
Loading

0 comments on commit 7fc909a

Please sign in to comment.