Skip to content

Commit

Permalink
feat: Allow to use unknown instead of any (#795)
Browse files Browse the repository at this point in the history
* feat: Allow to use unknown instead of any

* chore: linting

* chore: update snaps

* chore: fix ts
  • Loading branch information
stijnvanhulle authored Jan 21, 2024
1 parent ea680fb commit 457ee7f
Show file tree
Hide file tree
Showing 37 changed files with 961 additions and 105 deletions.
7 changes: 7 additions & 0 deletions .changeset/short-fireants-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@kubb/swagger-faker": minor
"@kubb/swagger-zod": minor
"@kubb/swagger-ts": minor
---

Allow to use unknown instead of any
82 changes: 80 additions & 2 deletions docs/plugins/swagger-faker/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,11 @@ Choose to use `date` or `datetime` as JavaScript `Date` instead of `string`.
::: code-group

```typescript ['string']
date: string
faker.string.alpha
```

```typescript ['date']
date: Date
faker.date.anytime()
```

:::
Expand Down Expand Up @@ -517,6 +517,84 @@ export default defineConfig({

:::

### unknownType

Which type to use when the Swagger/OpenAPI file is not providing more information.

::: info type

::: code-group

```typescript ['any']
any
```

```typescript ['unknown']
unknown
```

:::

::: info
Type: `'any' | 'unknown'` <br/>
Default: `'any'`

:::

::: code-group

```typescript ['any']
import { defineConfig } from '@kubb/core'
import createSwagger from '@kubb/swagger'
import createSwaggerFaker from '@kubb/swagger-faker'
import createSwaggerTS from '@kubb/swagger-ts'

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

```typescript ['unknown']
import { defineConfig } from '@kubb/core'
import createSwagger from '@kubb/swagger'
import createSwaggerFaker from '@kubb/swagger-faker'
import createSwaggerTS from '@kubb/swagger-ts'

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

:::

### seed

The use of Seed is intended to allow for consistent values in a test.
Expand Down
74 changes: 74 additions & 0 deletions docs/plugins/swagger-ts/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ date: Date
Type: `'string' | 'date'` <br/>
Default: `'string'`

:::

::: code-group

```typescript ['string']
import { defineConfig } from '@kubb/core'
import createSwagger from '@kubb/swagger'
Expand Down Expand Up @@ -427,6 +431,76 @@ export default defineConfig({

:::

### unknownType

Which type to use when the Swagger/OpenAPI file is not providing more information.

::: info type

::: code-group

```typescript ['any']
any
```

```typescript ['unknown']
unknown
```

:::

::: info
Type: `'any' | 'unknown'` <br/>
Default: `'any'`

:::

::: code-group

```typescript ['any']
import { defineConfig } from '@kubb/core'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'

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

```typescript ['unknown']
import { defineConfig } from '@kubb/core'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'

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

:::

### optionalType

Choose what to use as mode for an optional value.<br/>
Expand Down
72 changes: 72 additions & 0 deletions docs/plugins/swagger-zod/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ date: Date
Type: `'string' | 'date'` <br/>
Default: `'string'`

::: code-group

```typescript ['string']
import { defineConfig } from '@kubb/core'
import createSwagger from '@kubb/swagger'
Expand Down Expand Up @@ -404,6 +406,76 @@ export default defineConfig({

:::

### unknownType

Which type to use when the Swagger/OpenAPI file is not providing more information.

::: info type

::: code-group

```typescript ['any']
any
```

```typescript ['unknown']
unknown
```

:::

::: info
Type: `'any' | 'unknown'` <br/>
Default: `'any'`

:::

::: code-group

```typescript ['any']
import { defineConfig } from '@kubb/core'
import createSwagger from '@kubb/swagger'
import createSwaggerZod from '@kubb/swagger-zod'

export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerZod({
unknownType: 'any',
}),
],
})
```

```typescript ['unknown']
import { defineConfig } from '@kubb/core'
import createSwagger from '@kubb/swagger'
import createSwaggerZod from '@kubb/swagger-zod'

export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerZod({
unknownType: 'unknown',
}),
],
})
```

:::

### transformers

#### transformers.name
Expand Down
8 changes: 4 additions & 4 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
"@kubb/swagger-ts": "workspace:*",
"@kubb/swagger-zod": "workspace:*",
"@kubb/swagger-zodios": "workspace:*",
"@tanstack/react-query": "^5.17.15",
"@tanstack/solid-query": "^5.17.15",
"@tanstack/svelte-query": "^5.17.15",
"@tanstack/vue-query": "^5.17.15",
"@tanstack/react-query": "^5.17.19",
"@tanstack/solid-query": "^5.17.19",
"@tanstack/svelte-query": "^5.17.19",
"@tanstack/vue-query": "^5.17.19",
"@zodios/core": "^10.9.6",
"axios": "^1.6.5",
"msw": "^1.3.2",
Expand Down
6 changes: 3 additions & 3 deletions examples/react-query-v5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@kubb/swagger-client": "workspace:*",
"@kubb/swagger-tanstack-query": "workspace:*",
"@kubb/swagger-ts": "workspace:*",
"@tanstack/react-query": "^5.17.15",
"@tanstack/react-query": "^5.17.19",
"@tanstack/react-query-devtools": "5.0.0",
"axios": "^1.6.5",
"react": "^18.2.0",
Expand All @@ -40,10 +40,10 @@
"@types/react": "^18.2.48",
"@types/react-dom": "^18.2.18",
"@vitejs/plugin-react": "^4.2.1",
"unplugin-kubb": "workspace:*",
"msw": "^1.3.2",
"typescript": "~5.2.2",
"vite": "^4.5.1"
"unplugin-kubb": "workspace:*",
"vite": "^4.5.2"
},
"packageManager": "[email protected]",
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion examples/react-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@vitejs/plugin-react": "^4.2.1",
"msw": "^1.3.2",
"typescript": "~5.2.2",
"vite": "^4.5.1"
"vite": "^4.5.2"
},
"packageManager": "[email protected]",
"engines": {
Expand Down
4 changes: 2 additions & 2 deletions examples/vue-query-v5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@kubb/swagger-tanstack-query": "workspace:*",
"@kubb/swagger-ts": "workspace:*",
"@kubb/swagger-zod": "workspace:*",
"@tanstack/vue-query": "^5.17.15",
"@tanstack/vue-query": "^5.17.19",
"axios": "^1.6.5",
"vue": "^3.4.15"
},
Expand All @@ -38,7 +38,7 @@
"msw": "^1.3.2",
"tsup": "^8.0.1",
"typescript": "~5.2.2",
"vite": "^4.5.1"
"vite": "^4.5.2"
},
"packageManager": "[email protected]",
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion examples/vue-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"msw": "^1.3.2",
"tsup": "^8.0.1",
"typescript": "~5.2.2",
"vite": "^4.5.1"
"vite": "^4.5.2"
},
"packageManager": "[email protected]",
"engines": {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@
"@types/node": "^20.11.5",
"@vitest/coverage-v8": "^1.2.1",
"@vitest/ui": "^1.2.1",
"bun-types": "^1.0.23",
"bun-types": "^1.0.25",
"dprint": "^0.45.0",
"prettier": "^3.2.4",
"prettier-eslint": "^16.2.0",
"prettier-eslint": "^16.3.0",
"rimraf": "^5.0.5",
"ts-node": "^10.9.2",
"turbo": "^1.11.3",
Expand Down
1 change: 1 addition & 0 deletions packages/parser/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ export function createOmitDeclaration({ keys, type, nonNullable }: { keys: Array

export const keywordTypeNodes = {
any: factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword),
unknown: factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword),
number: factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
integer: factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
object: factory.createKeywordTypeNode(ts.SyntaxKind.ObjectKeyword),
Expand Down
1 change: 1 addition & 0 deletions packages/swagger-faker/src/FakerGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('FakeGenerator enums', async () => {
mapper: {},
seed: 1,
transformers: {},
unknownType: 'any',
}, {
oas,
pluginManager: mockedPluginManager,
Expand Down
Loading

1 comment on commit 457ee7f

@vercel
Copy link

@vercel vercel bot commented on 457ee7f Jan 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

kubb – ./

kubb-git-main-kubb.vercel.app
kubb-kubb.vercel.app
www.kubb.dev
kubb.dev

Please sign in to comment.