Skip to content

Commit

Permalink
chore: update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Mar 10, 2024
1 parent a60fd4c commit 3da2307
Show file tree
Hide file tree
Showing 24 changed files with 199 additions and 23 deletions.
18 changes: 18 additions & 0 deletions docs/plugins/react/hooks/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ function Component() {

:::

## useLanguage

`useLanguage` will return the current language set by the parent `Editor` component.

::: code-group

```typescript
import { useLanguage } from '@kubb/react'

function Component() {
const language = useLanguage()

return null
}
```

:::

## usePluginManager

`usePluginManager` will return the PluginManager instance.
Expand Down
86 changes: 86 additions & 0 deletions docs/plugins/swagger-tanstack-query/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,92 @@ export default defineConfig({

:::

### variablesType

Define the way of passing thought the queryParams, headerParams and data.

`'mutate'` will use the `mutate` or `mutateAsync` function. <br/>
`'hook'` will use the `useMutation` hook.

::: info type

::: code-group

```typescript ['mutate']
const { mutate } = useDeletePet()

mutate({
petId: 1,
})
```

```typescript ['hook']
const { mutate } = useDeletePet(1)

mutate()
```

:::

::: info

Type: `'mutate' | 'hook'` <br/>
Default: `'hook'`

::: code-group

```typescript ['mutate']
import { defineConfig } from '@kubb/core'
import createSwagger from '@kubb/swagger'
import createSwaggerTanstackQuery from '@kubb/swagger-tanstack-query'
import createSwaggerTS from '@kubb/swagger-ts'

export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerTanstackQuery(
{
variablesType: 'mutate',
},
),
],
})
```

```typescript ['hook']
import { defineConfig } from '@kubb/core'
import createSwagger from '@kubb/swagger'
import createSwaggerTanstackQuery from '@kubb/swagger-tanstack-query'
import createSwaggerTS from '@kubb/swagger-ts'

export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerTanstackQuery(
{
variablesType: 'hook',
},
),
],
})
```

:::

### parser

Which parser can be used before returning the data to `@tanstack/query`.
Expand Down
38 changes: 37 additions & 1 deletion docs/plugins/swagger/hooks/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ See [Oas](https://github.com/readmeio/oas) to understand how to use the `Oas` in
import { useOas } from '@kubb/react'

function Component() {
const { oas } = useOas()
const oas = useOas()

return null
}
Expand All @@ -46,6 +46,24 @@ function Component() {

:::

## useOperations

`useOperations` will return all the Operations.<br/>

::: code-group

```typescript
import { useOperations } from '@kubb/react'

function Component() {
const operations = useOperations()

return null
}
```

:::

## useSchemas

`useSchemas` will return the schemas of the current `Operation`.<br/>
Expand All @@ -64,6 +82,24 @@ function Component() {

:::

## useOperationHelpers

`useOperationHelpers` will return some helper functions that can be used to get the operation file, get the operation name.<br/>

::: code-group

```typescript
import { useOperationHelpers } from '@kubb/react'

function Component() {
const { getName, getFile } = useOperationHelpers()

return null
}
```

:::

## useOperationName

`useOperationName` will return the name based on the current operation and plugin(when `pluginKey` is not provided).<br/>
Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/hooks/useFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ type Props<TOptions = object> = {
options?: TOptions
}

/**
* With `useFile` you can get all props needed to create a file(path, baseName, source).
*/
export function useFile<TOptions = object>({ name, mode, extName, pluginKey, options }: Props<TOptions>): KubbFile.File<{ pluginKey: Plugin['key'] }> {
const pluginManager = usePluginManager()

Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/hooks/useFileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { useApp } from './useApp.ts'

import type { FileManager, PluginManager } from '@kubb/core'

/**
* `useFileManager` will return the current FileManager instance.
*/
export function useFileManager(): FileManager {
const app = useApp<{ pluginManager: PluginManager }>()

Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/hooks/useLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { Editor } from '../components/index.ts'

import type { EditorContextProps } from '../components/Editor.tsx'

/**
* `useLanguage` will return the current language set by the parent `Editor` component.
*/
export function useLanguage(): EditorContextProps {
return useContext(Editor.Context)
}
3 changes: 3 additions & 0 deletions packages/react/src/hooks/useMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { useApp } from './useApp.ts'

import type { AppContextProps } from '../components/App.tsx'

/**
* `useMeta` will return an object containing the meta that has been provided with the `root.render` functionality.
*/
export function useMeta<Meta extends Record<string, unknown> = Record<string, unknown>>(): AppContextProps<Meta>['meta'] {
const app = useApp<Meta>()

Expand Down
4 changes: 3 additions & 1 deletion packages/react/src/hooks/usePackageVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ type Props = {
dependency: string
version: string
}

/**
* With `usePackageVersion` you can validate of a specific package is set in the `package.json`.
*/
export function usePackageVersion({ dependency, version }: Props): boolean {
const manager = new PackageManager()

Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/hooks/usePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { useApp } from './useApp.ts'

import type { Plugin, PluginFactoryOptions } from '@kubb/core'

/**
* `usePlugin` will return the current plugin.
*/
export function usePlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(): Plugin<TOptions> {
const app = useApp<{ plugin: Plugin<TOptions> }>()

Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/hooks/usePluginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { useApp } from './useApp.ts'

import type { PluginManager } from '@kubb/core'

/**
* `usePluginManager` will return the PluginManager instance.
*/
export function usePluginManager(): PluginManager {
const app = useApp<{ pluginManager: PluginManager }>()

Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/hooks/useResolveName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import type { ResolveNameParams } from '@kubb/core'

type Props = ResolveNameParams

/**
* Resolve a name based on what has been set inside the `resolveName` of a specific plugin.
* Use `pluginKey` to retreive the name of that specific plugin.
*/
export function useResolveName(props: Props): string {
const pluginManager = usePluginManager()

Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/hooks/useResolvePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import type { KubbFile, ResolvePathParams } from '@kubb/core'

type Props = ResolvePathParams

/**
* Resolve a path based on what has been set inside the `resolvePath` of a specific plugin.
* Use `pluginKey` to retreive the path of that specific plugin.
*/
export function useResolvePath(props: Props): KubbFile.OptionalPath {
const pluginManager = usePluginManager()

Expand Down
2 changes: 1 addition & 1 deletion packages/swagger-faker/src/components/Mutation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Mutation.File = function({}: FileProps): ReactNode {

const schemas = useSchemas()
const pluginManager = usePluginManager()
const { oas } = useOas()
const oas = useOas()
const file = useOperationFile()

const builder = new FakerBuilder(options, { oas, pluginManager })
Expand Down
2 changes: 1 addition & 1 deletion packages/swagger-faker/src/components/Query.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Query.File = function({}: FileProps): ReactNode {

const schemas = useSchemas()
const pluginManager = usePluginManager()
const { oas } = useOas()
const oas = useOas()
const file = useOperationFile()

const builder = new FakerBuilder(options, { oas, pluginManager })
Expand Down
1 change: 1 addition & 0 deletions packages/swagger-tanstack-query/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type Query = {

export type Mutate = {
/**
* Define the way of passing thought the queryParams, headerParams and data.
* @default `'hook'`
*/
variablesType?: 'mutate' | 'hook'
Expand Down
2 changes: 1 addition & 1 deletion packages/swagger-ts/src/components/Mutation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Mutation.File = function({ mode }: FileProps): ReactNode {

const schemas = useSchemas()
const pluginManager = usePluginManager()
const { oas } = useOas()
const oas = useOas()
const file = useOperationFile()
const factoryName = useOperationName({ type: 'type' })
const operation = useOperation()
Expand Down
2 changes: 1 addition & 1 deletion packages/swagger-ts/src/components/OasType.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function OasType({
typeName,
Template = defaultTemplates.default,
}: Props): ReactNode {
const { oas } = useOas()
const oas = useOas()

return <Template name={name} typeName={typeName} api={oas.api} />

Check warning on line 52 in packages/swagger-ts/src/components/OasType.tsx

View check run for this annotation

Codecov / codecov/patch

packages/swagger-ts/src/components/OasType.tsx#L50-L52

Added lines #L50 - L52 were not covered by tests
}
Expand Down
2 changes: 1 addition & 1 deletion packages/swagger-ts/src/components/Query.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Query.File = function({ mode }: FileProps): ReactNode {

const schemas = useSchemas()
const pluginManager = usePluginManager()
const { oas } = useOas()
const oas = useOas()
const file = useOperationFile()
const factoryName = useOperationName({ type: 'type' })
const operation = useOperation()
Expand Down
2 changes: 1 addition & 1 deletion packages/swagger-zod/src/components/Mutation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Mutation.File = function({ mode = 'directory' }: FileProps): ReactNode {

const schemas = useSchemas()
const pluginManager = usePluginManager()
const { oas } = useOas()
const oas = useOas()
const file = useOperationFile()

const builder = new ZodBuilder(options, { oas, pluginManager })
Expand Down
2 changes: 1 addition & 1 deletion packages/swagger-zod/src/components/Query.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Query.File = function({ mode = 'directory' }: FileProps): ReactNode {

const schemas = useSchemas()
const pluginManager = usePluginManager()
const { oas } = useOas()
const oas = useOas()
const file = useOperationFile()

const builder = new ZodBuilder(options, { oas, pluginManager })
Expand Down
17 changes: 4 additions & 13 deletions packages/swagger/src/hooks/useOas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,14 @@ import { useContext } from '@kubb/react'

import { Oas } from '../components/Oas.tsx'

import type { GetSchemas } from '../components/Oas.tsx'
import type { Oas as OasType } from '../oas/index.ts'

type Result = {
oas: OasType
/**
* @deprecated replace 'getSchemas' with `useOperationHelpers`
*/
getSchemas: GetSchemas
}

export function useOas(): Result {
const { oas, getSchemas } = useContext(Oas.Context)
export function useOas(): OasType {
const { oas } = useContext(Oas.Context)

if (!oas || !getSchemas) {
if (!oas) {
throw new Error('Oas is not defined')
}

return { oas, getSchemas }
return oas
}
10 changes: 10 additions & 0 deletions packages/swagger/src/hooks/useOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { useOperationHelpers } from './useOperationHelpers.ts'
import type { KubbFile, Plugin, ResolveNameParams } from '@kubb/core'
import type { Operation as OperationType } from '../oas/index.ts'

/**
* `useOperations` will return the current `Operation`
*/
export function useOperation(): OperationType {
const { operation } = useContext(Operation.Context)

Expand All @@ -21,6 +24,9 @@ type UseOperationNameProps = {
pluginKey?: Plugin['key']
}

/**
* `useOperationName` will return the name based on the current operation and plugin(when `pluginKey` is not provided).
*/
export function useOperationName({ type, ...rest }: UseOperationNameProps): string {
const plugin = usePlugin()
const operation = useOperation()
Expand All @@ -43,6 +49,10 @@ type UseOperationFileProps = {
pluginKey?: Plugin['key']
}

/**
* `useOperationFile` will create all the props used for `<File/>` based on the current operation and plugin(when `pluginKey` is not provided)
* Internally `useFile` of `@kubb/react` is getting used.
*/
export function useOperationFile(props: UseOperationFileProps = {}): KubbFile.File<FileMeta> {
const plugin = usePlugin()
const operation = useOperation()
Expand Down
Loading

0 comments on commit 3da2307

Please sign in to comment.