Skip to content

Commit

Permalink
feat: type for resolveName
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Oct 16, 2023
1 parent 40bd196 commit 53f9893
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 33 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ es
artifacts
coverage
*.tgz
mocks

## custom ones
# /e2e
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/managers/pluginManager/PluginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ export class PluginManager {
this.hookForPluginSync({
pluginName: params.pluginName,
hookName: 'resolveName',
parameters: [params.name],
parameters: [params.name, params.type],
}) || params.name
return transformReservedWord(name)
}
const name = this.hookFirstSync({
hookName: 'resolveName',
parameters: [params.name],
parameters: [params.name, params.type],
}).result

return transformReservedWord(name)
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export type PluginLifecycle<TOptions extends PluginFactoryOptions = PluginFactor
* @type hookFirst
* @example ('pet') => 'Pet'
*/
resolveName: (this: PluginContext, name: string) => string
resolveName: (this: PluginContext, name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
/**
* Makes it possible to run async logic to override the path defined previously by `resolvePath`.
* @type hookFirst
Expand Down Expand Up @@ -244,6 +244,7 @@ export type ResolveNameParams = {
* If not defined it will fall back on the resolvePath of the core plugin.
*/
pluginName?: string
type?: 'file' | 'function'
}

export type PluginContext<TOptions = Record<string, unknown>> = {
Expand Down
10 changes: 5 additions & 5 deletions packages/swagger-client/src/builders/ClientBuilder.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint- @typescript-eslint/explicit-module-boundary-types */
import { FunctionParams, getRelativePath, URLPath } from '@kubb/core'
import { createRoot, File } from '@kubb/react'
import { getASTParams, getComments, OasBuilder, useResolve, useSchemas } from '@kubb/swagger'
import { getASTParams, getComments, OasBuilder, useResolve, useResolveName, useSchemas } from '@kubb/swagger'
import { useResolve as useResolveType } from '@kubb/swagger-ts'

import { ClientFunction } from '../components/index.ts'
Expand Down Expand Up @@ -69,11 +69,11 @@ export class ClientBuilder extends OasBuilder<Config> {

const Component = () => {
const schemas = useSchemas()
const file = useResolve({ pluginName })
const name = useResolveName({ pluginName, type: 'function' })

return (
<ClientFunction
name={file.name}
name={name}
generics={generics.toString()}
clientGenerics={clientGenerics.toString()}
dataReturnType={dataReturnType}
Expand Down Expand Up @@ -110,8 +110,8 @@ export class ClientBuilder extends OasBuilder<Config> {

const Component = () => {
const schemas = useSchemas()
const file = useResolve({ pluginName })
const fileType = useResolveType()
const file = useResolve({ pluginName, type: 'file' })
const fileType = useResolveType({ type: 'file' })

const resolvedClientPath = clientPath ? getRelativePath(file.path, clientPath) : '@kubb/swagger-client/client'

Expand Down
10 changes: 5 additions & 5 deletions packages/swagger-msw/src/builders/MSWBuilder.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { getRelativePath, URLPath } from '@kubb/core'
import { createRoot, File } from '@kubb/react'
import { OasBuilder, useResolve } from '@kubb/swagger'
import { OasBuilder, useResolve, useResolveName } from '@kubb/swagger'
import { useResolve as useResolveFaker } from '@kubb/swagger-faker'

import { pluginName } from '../plugin.ts'
Expand Down Expand Up @@ -31,11 +31,11 @@ export class MSWBuilder extends OasBuilder<Config> {
const { responseName, operation } = this.config

const Component = () => {
const file = useResolve({ pluginName })
const name = useResolveName({ pluginName, type: 'function' })

return (
<>{`
export const ${file.name} = rest.${operation.method}('*${URLPath.toURLPath(operation.path)}', function handler(req, res, ctx) {
export const ${name} = rest.${operation.method}('*${URLPath.toURLPath(operation.path)}', function handler(req, res, ctx) {
return res(
ctx.json(${responseName}()),
);
Expand All @@ -58,8 +58,8 @@ export class MSWBuilder extends OasBuilder<Config> {
const root = createRoot<AppContextProps<AppMeta>>()

const Component = () => {
const file = useResolve({ pluginName })
const faker = useResolveFaker()
const file = useResolve({ pluginName, type: 'file' })
const faker = useResolveFaker({ type: 'file' })

return (
<File baseName={file.baseName} path={file.path}>
Expand Down
4 changes: 2 additions & 2 deletions packages/swagger-swr/src/builders/QueryBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export function ${name} <
const root = createRoot<AppContextProps<AppMeta>>()

const ComponentQuery = () => {
const file = useResolve({ name, pluginName })
const file = useResolve({ name, pluginName, type: 'file' })

return (
<File baseName={file.baseName} path={file.path}>
Expand All @@ -274,7 +274,7 @@ export function ${name} <
}

const ComponentMutation = () => {
const file = useResolve({ name, pluginName })
const file = useResolve({ name, pluginName, type: 'file' })

return (
<File baseName={file.baseName} path={file.path}>
Expand Down
6 changes: 3 additions & 3 deletions packages/swagger-tanstack-query/src/builders/QueryBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ export function ${name} <${generics.toString()}>(${params.toString()}): ${framew
const root = createRoot<AppContextProps<AppMeta>>()

const ComponentQuery = () => {
const file = useResolve({ name, pluginName })
const file = useResolve({ name, pluginName, type: 'file' })

return (
<File baseName={file.baseName} path={file.path}>
Expand All @@ -560,7 +560,7 @@ export function ${name} <${generics.toString()}>(${params.toString()}): ${framew
}

const ComponentQueryInfinite = () => {
const file = useResolve({ name, pluginName })
const file = useResolve({ name, pluginName, type: 'file' })

return (
<File baseName={file.baseName} path={file.path}>
Expand All @@ -579,7 +579,7 @@ export function ${name} <${generics.toString()}>(${params.toString()}): ${framew
}

const ComponentMutation = () => {
const file = useResolve({ name, pluginName })
const file = useResolve({ name, pluginName, type: 'file' })

return (
<File baseName={file.baseName} path={file.path}>
Expand Down
16 changes: 7 additions & 9 deletions packages/swagger/src/hooks/useResolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@ import { useOperation } from '@kubb/swagger'

import { resolve } from '../utils/resolve.ts'

import type { KubbFile, PluginContext, ResolveNameParams, ResolvePathParams } from '@kubb/core'
import type { ResolveProps, Resolver } from '@kubb/swagger'

type ResolvePath = ResolveProps['resolvePath']

type ResolveName = ResolveProps['resolveName']

export type UseResolveProps = Omit<ResolveProps, 'operation' | 'resolvePath' | 'resolveName'> & {
resolvePath?: ResolvePath
resolveName?: ResolveName
resolvePath?: PluginContext['resolvePath']
resolveName?: PluginContext['resolveName']
}

export function useResolvePath(props: Parameters<ResolvePath>[0]): ReturnType<ResolvePath> {
export function useResolvePath(props: ResolvePathParams): KubbFile.OptionalPath {
const pluginManager = usePluginManager()

return pluginManager.resolvePath(props)
}

export function useResolveName(props: Parameters<ResolveName>[0]): ReturnType<ResolveName> {
export function useResolveName(props: Partial<ResolveNameParams>): string {
const pluginManager = usePluginManager()
const operation = useOperation()

return pluginManager.resolveName(props)
return pluginManager.resolveName({ ...props, name: props.name || operation?.getOperationId() })
}

Check warning on line 25 in packages/swagger/src/hooks/useResolve.ts

View check run for this annotation

Codecov / codecov/patch

packages/swagger/src/hooks/useResolve.ts#L21-L25

Added lines #L21 - L25 were not covered by tests

export function useResolve(props: UseResolveProps = {}): Resolver {
Expand Down
4 changes: 2 additions & 2 deletions packages/swagger/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AppMeta as AppCoreMeta, KubbFile, PluginFactoryOptions } from '@kubb/core'
import type { AppMeta as AppCoreMeta, KubbFile, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
import type Oas from 'oas'
import type Operation from 'oas/operation'
import type { HttpMethods as HttpMethod } from 'oas/rmoas.types'
Expand All @@ -8,7 +8,7 @@ import type { GetSchemasProps } from './utils/getSchemas.ts'
// eslint-disable-next-line @typescript-eslint/ban-types
export type ContentType = 'application/json' | (string & {})

export type ResolvePathOptions = { pluginName?: string; tag?: string }
export type ResolvePathOptions = { pluginName?: string; tag?: string; type?: ResolveNameParams['type'] }

export type API = {
getOas: () => Promise<Oas>
Expand Down
9 changes: 5 additions & 4 deletions packages/swagger/src/utils/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PluginContext } from '@kubb/core'
import type { PluginContext, ResolveNameParams } from '@kubb/core'
import type { Resolver } from '@kubb/swagger'
import type { Operation, ResolvePathOptions } from '../types.ts'

Expand Down Expand Up @@ -26,14 +26,15 @@ export type ResolveProps = (PropsWithOperation | PropsWithoutOperation) & {
tag?: string
resolvePath: PluginContext<ResolvePathOptions>['resolvePath']
resolveName: PluginContext['resolveName']
type?: ResolveNameParams['type']
}

export function resolve({ operation, name, tag, pluginName, resolveName, resolvePath }: ResolveProps): Resolver {
export function resolve({ operation, name, tag, type, pluginName, resolveName, resolvePath }: ResolveProps): Resolver {
if (!name && !operation?.getOperationId()) {
throw new Error('name or operation should be set')
}

const resolvedName = name ? name : resolveName({ name: operation?.getOperationId() as string, pluginName })
const resolvedName = name ? name : resolveName({ name: operation?.getOperationId() as string, type, pluginName })

if (!resolvedName) {
throw new Error(`Name ${name || operation?.getOperationId()} should be defined`)
Expand All @@ -42,8 +43,8 @@ export function resolve({ operation, name, tag, pluginName, resolveName, resolve
const baseName = `${resolvedName}.ts` as const
const path = resolvePath({
baseName,
options: { pluginName, tag: tag || operation?.getTags()[0]?.name },
pluginName,
options: { pluginName, type, tag: tag || operation?.getTags()[0]?.name },
})

if (!path) {
Expand Down

0 comments on commit 53f9893

Please sign in to comment.