Skip to content

Commit

Permalink
fix: deprecate client and replace by clientImportPath #621 (#622)
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle authored Nov 10, 2023
1 parent b5fc0fe commit 74f562b
Show file tree
Hide file tree
Showing 13 changed files with 17 additions and 131 deletions.
13 changes: 0 additions & 13 deletions docs/plugins/swagger-client/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,6 @@ export default defineConfig({

:::

### client <Badge type="danger" text="deprecated" />

Path to the client that will be used to do the API calls.<br/>
Relative to the root

::: info

Type: `string` <br/>
Default: `'@kubb/swagger-client/client'`

Deprecated. Use `clientImportPath` instead. It will be skipped if `clientImportPath` is provided.
:::

### clientImportPath

Path to the client import path that will be used to do the API calls.<br/>
Expand Down
13 changes: 0 additions & 13 deletions docs/plugins/swagger-swr.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,6 @@ export default defineConfig({

:::

### client <Badge type="danger" text="deprecated" />

Path to the client that will be used to do the API calls.<br/>
Relative to the root

::: info

Type: `string` <br/>
Default: `'@kubb/swagger-client/client'`

Deprecated. Use `clientImportPath` instead. It will be skipped if `clientImportPath` is provided.
:::

### clientImportPath

Path to the client import path that will be used to do the API calls.<br/>
Expand Down
14 changes: 0 additions & 14 deletions docs/plugins/swagger-tanstack-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,6 @@ export default defineConfig({

:::

### client <Badge type="danger" text="deprecated" />

Path to the client that will be used to do the API calls.
Relative to the root

::: info

Type: `string` <br/>
Default: `'@kubb/swagger-client/client'`

Deprecated. Use `clientImportPath` instead. It will be skipped if `clientImportPath` is provided.

:::

### clientImportPath

Path to the client import path that will be used to do the API calls.<br/>
Expand Down
13 changes: 4 additions & 9 deletions packages/swagger-client/src/components/Client.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import path from 'node:path'

import { FunctionParams, getRelativePath, transformers } from '@kubb/core/utils'
import { FunctionParams, transformers } from '@kubb/core/utils'
import { URLPath } from '@kubb/core/utils'
import { File, Function, usePlugin, usePluginManager } from '@kubb/react'
import { File, Function, usePlugin } from '@kubb/react'
import { useOperation, useResolve, useSchemas } from '@kubb/swagger/hooks'
import { getASTParams, getComments } from '@kubb/swagger/utils'
import { useResolve as useResolveType } from '@kubb/swagger-ts/hooks'
Expand Down Expand Up @@ -167,16 +165,13 @@ type FileProps = {

Client.File = function({ templates = defaultTemplates }: FileProps): ReactNode {
const { key: pluginKey, options } = usePlugin<PluginOptions>()
const { config } = usePluginManager()
const schemas = useSchemas()
const operation = useOperation()
const file = useResolve({ pluginKey, type: 'file' })
const fileType = useResolveType({ type: 'file' })

const { clientImportPath, client } = options
const root = path.resolve(config.root, config.output.path)
const clientPath = client ? path.resolve(root, 'client.ts') : undefined
const resolvedClientPath = clientImportPath ? clientImportPath : clientPath ? getRelativePath(file.path, clientPath) : '@kubb/swagger-client/client'
const { clientImportPath } = options
const resolvedClientPath = clientImportPath ? clientImportPath : '@kubb/swagger-client/client'

const Template = templates.default

Expand Down
30 changes: 0 additions & 30 deletions packages/swagger-client/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
skipBy = [],
overrideBy = [],
transformers = {},
client,
clientImportPath,
dataReturnType = 'data',
pathParamsType = 'inline',
Expand All @@ -36,7 +35,6 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
name: pluginName,
options: {
dataReturnType,
client,
clientImportPath,
pathParamsType,
},
Expand Down Expand Up @@ -121,34 +119,6 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
await this.addFile(...rootFiles)
}

// Copy `client.ts` file only when the 'client' option is provided.
if (client) {
const packageManager = new PackageManager(process.cwd())

const clientPath = path.resolve(root, 'client.ts')
const originalClientPath: KubbFile.OptionalPath = options.client
? path.resolve(this.config.root, options.client)
: packageManager.getLocation('@kubb/swagger-client/ts-client')

if (!originalClientPath) {
throw new Error(
`Cannot find the 'client.ts' file, or 'client' is not set in the options or '@kubb/swagger-client' is not included in your dependencies`,
)
}

const baseURL = await swaggerPlugin.api.getBaseURL()

await this.addFile({
baseName: 'client.ts',
path: clientPath,
source: await read(originalClientPath),
env: {
AXIOS_BASE: baseURL,
AXIOS_HEADERS: JSON.stringify({}),
},
})
}

await this.fileManager.addIndexes({ root, extName: '.ts', meta: { pluginKey: this.plugin.key } })
},
}
Expand Down
8 changes: 0 additions & 8 deletions packages/swagger-client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ export type Options = {
* Array containing overrideBy paramaters to override `options` based on tags/operations/methods/paths.
*/
overrideBy?: Array<OverrideBy<ResolvedOptions>>
/**
* Path to the client that will be used to do the API calls.
* Relative to the root.
* @default '@kubb/swagger-client/client'
* @deprecated Use `clientImportPath` instead. It will be skipped if `clientImportPath` is provided.
*/
client?: string
/**
* Path to the client import path that will be used to do the API calls.
* It will be used as `import client from '${clientImportPath}'`.
Expand Down Expand Up @@ -88,7 +81,6 @@ type ResolvedOptions = {
clientImportPath?: Options['clientImportPath']
dataReturnType: NonNullable<Options['dataReturnType']>
pathParamsType: NonNullable<Options['pathParamsType']>
client?: Options['client']
}

export type FileMeta = {
Expand Down
17 changes: 6 additions & 11 deletions packages/swagger-swr/src/generators/OperationGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type { Operation, OperationSchema, OperationSchemas, Resolver } from '@ku
import type { FileMeta, PluginOptions } from '../types.ts'

type Options = {
clientPath?: NonNullable<PluginOptions['options']['client']>
clientImportPath?: NonNullable<PluginOptions['options']['clientImportPath']>
dataReturnType: NonNullable<PluginOptions['options']['dataReturnType']>
}
Expand Down Expand Up @@ -68,15 +67,13 @@ export class OperationGenerator extends Generator<Options, PluginOptions> {
}

async get(operation: Operation, schemas: OperationSchemas, options: Options): Promise<KubbFile.File<FileMeta> | null> {
const { clientPath, dataReturnType } = options
const { dataReturnType } = options
const { pluginManager, oas, plugin } = this.context

const hook = this.resolve(operation)
const type = this.resolveType(operation)
const clientImportPath = this.options.clientImportPath
? this.options.clientImportPath
: clientPath
? getRelativePath(hook.path, clientPath)
const clientImportPath = options.clientImportPath
? options.clientImportPath
: '@kubb/swagger-client/client'

let errors: Resolver[] = []
Expand Down Expand Up @@ -137,15 +134,13 @@ export class OperationGenerator extends Generator<Options, PluginOptions> {
}

async post(operation: Operation, schemas: OperationSchemas, options: Options): Promise<KubbFile.File<FileMeta> | null> {
const { clientPath, dataReturnType } = options
const { dataReturnType } = options
const { pluginManager, oas, plugin } = this.context

const hook = this.resolve(operation)
const type = this.resolveType(operation)
const clientImportPath = this.options.clientImportPath
? this.options.clientImportPath
: clientPath
? getRelativePath(hook.path, clientPath)
const clientImportPath = options.clientImportPath
? options.clientImportPath
: '@kubb/swagger-client/client'

let errors: Resolver[] = []
Expand Down
5 changes: 1 addition & 4 deletions packages/swagger-swr/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { camelCase, camelCaseTransformMerge } from 'change-case'

import { OperationGenerator } from './generators/index.ts'

import type { KubbFile, KubbPlugin } from '@kubb/core'
import type { KubbPlugin } from '@kubb/core'
import type { PluginOptions as SwaggerPluginOptions } from '@kubb/swagger'
import type { PluginOptions } from './types.ts'

Expand Down Expand Up @@ -60,13 +60,10 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {

const oas = await swaggerPlugin.api.getOas()

const clientPath: KubbFile.OptionalPath = options.client ? path.resolve(this.config.root, options.client) : undefined

const operationGenerator = new OperationGenerator(
{
dataReturnType,
clientImportPath: options.clientImportPath,
clientPath,
},
{
oas,
Expand Down
7 changes: 0 additions & 7 deletions packages/swagger-swr/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ export type Options = {
* Array containing overrideBy paramaters to override `options` based on tags/operations/methods/paths.
*/
overrideBy?: Array<OverrideBy<Options>>
/**
* Path to the client that will be used to do the API calls.
* Relative to the root.
* @default '@kubb/swagger-client/client'
* @deprecated Use `clientImportPath` instead. It will be skipped if `clientImportPath` is provided.
*/
client?: string
/**
* Path to the client import path that will be used to do the API calls.
* It will be used as `import client from '${clientImportPath}'`.
Expand Down
7 changes: 2 additions & 5 deletions packages/swagger-tanstack-query/src/components/Mutation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -344,16 +344,13 @@ type FileProps = {

Mutation.File = function({ templates = defaultTemplates, imports = MutationImports.templates }: FileProps): ReactNode {
const { key: pluginKey, options } = usePlugin<PluginOptions>()
const pluginManager = usePluginManager()
const schemas = useSchemas()
const operation = useOperation()
const file = useResolve({ pluginKey, type: 'file' })
const fileType = useResolveType({ type: 'file' })

const { clientImportPath, client, templatesPath, framework } = options
const root = path.resolve(pluginManager.config.root, pluginManager.config.output.path)
const clientPath = client ? path.resolve(root, 'client.ts') : undefined
const resolvedClientPath = clientImportPath ? clientImportPath : clientPath ? getRelativePath(file.path, clientPath) : '@kubb/swagger-client/client'
const { clientImportPath, templatesPath, framework } = options
const resolvedClientPath = clientImportPath ? clientImportPath : '@kubb/swagger-client/client'

const importNames = getImportNames()
const Template = templates[framework]
Expand Down
11 changes: 4 additions & 7 deletions packages/swagger-tanstack-query/src/components/Query.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'node:path'

import { PackageManager } from '@kubb/core'
import { FunctionParams, getRelativePath, URLPath } from '@kubb/core/utils'
import { File, Function, Type, usePlugin, usePluginManager } from '@kubb/react'
import { FunctionParams, URLPath } from '@kubb/core/utils'
import { File, Function, Type, usePlugin } from '@kubb/react'
import { useOperation, useResolve, useSchemas } from '@kubb/swagger/hooks'
import { getASTParams, getComments, getParams } from '@kubb/swagger/utils'
import { useResolve as useResolveType } from '@kubb/swagger-ts/hooks'
Expand Down Expand Up @@ -393,16 +393,13 @@ type FileProps = {

Query.File = function({ templates = defaultTemplates, imports = QueryImports.templates }: FileProps): ReactNode {
const { key: pluginKey, options } = usePlugin<PluginOptions>()
const pluginManager = usePluginManager()
const schemas = useSchemas()
const operation = useOperation()
const file = useResolve({ pluginKey, type: 'file' })
const fileType = useResolveType({ type: 'file' })

const { clientImportPath, client, templatesPath, framework, infinite } = options
const root = path.resolve(pluginManager.config.root, pluginManager.config.output.path)
const clientPath = client ? path.resolve(root, 'client.ts') : undefined
const resolvedClientPath = clientImportPath ? clientImportPath : clientPath ? getRelativePath(file.path, clientPath) : '@kubb/swagger-client/client'
const { clientImportPath, templatesPath, framework, infinite } = options
const resolvedClientPath = clientImportPath ? clientImportPath : '@kubb/swagger-client/client'

const importNames = getImportNames()
const Template = templates[framework]
Expand Down
2 changes: 0 additions & 2 deletions packages/swagger-tanstack-query/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
skipBy = [],
overrideBy = [],
framework = 'react',
client,
clientImportPath,
infinite,
transformers = {},
Expand All @@ -38,7 +37,6 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
options: {
framework,
clientImportPath,
client,
dataReturnType,
infinite: infinite
? {
Expand Down
8 changes: 0 additions & 8 deletions packages/swagger-tanstack-query/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ export type Options = {
*/
exportAs?: string
}
/**
* Path to the client that will be used to do the API calls.
* Relative to the root.
* @default '@kubb/swagger-client/client'
* @deprecated Use `clientImportPath` instead. It will be skipped if `clientImportPath` is provided.
*/
client?: string
/**
* Path to the client import path that will be used to do the API calls.
* It will be used as `import client from '${clientImportPath}'`.
Expand Down Expand Up @@ -110,7 +103,6 @@ export type Framework = 'react' | 'solid' | 'svelte' | 'vue'

type ResolvedOptions = {
framework: NonNullable<PluginOptions['options']['framework']>
client?: Options['client']
clientImportPath?: PluginOptions['options']['clientImportPath']
dataReturnType: NonNullable<PluginOptions['options']['dataReturnType']>
/**
Expand Down

0 comments on commit 74f562b

Please sign in to comment.