Skip to content

Commit 39d51d8

Browse files
authored
Merge pull request #6 from cuppachino/happycollision-fix-strictness-of-query-params
Use exact optional query parameters
2 parents 7091120 + c8b204a commit 39d51d8

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

.changeset/calm-parrots-clean.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cuppachino/openapi-fetch": patch
3+
---
4+
5+
Use exact optional query parameters

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ type _CreateFetch<OP, Q = never> = [Q] extends [never]
117117

118118
export type CreateFetch<M, OP> = M extends 'post' | 'put' | 'patch' | 'delete'
119119
? OP extends { parameters: { query: infer Q } }
120-
? _CreateFetch<OP, { [K in keyof Q]: true | 1 }>
120+
? _CreateFetch<OP, { [K in keyof Q]-?: true | 1 }>
121121
: _CreateFetch<OP>
122122
: _CreateFetch<OP>
123123

test/fetch.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ describe('fetch', () => {
8686
const fun = fetcher
8787
.path('/bodyquery/{id}')
8888
.method(method)
89-
.create({ scalar: 1 })
89+
.create({ scalar: 1, optional: 1 })
9090

9191
const { data } = await fun({
9292
id: 1,
@@ -235,7 +235,7 @@ describe('fetch', () => {
235235
const fun = fetcher
236236
.path('/bodyquery/{id}')
237237
.method('post')
238-
.create({ scalar: 1 })
238+
.create({ scalar: 1, optional: 1 })
239239

240240
const captured = { url: '', body: '' }
241241

test/infer.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
OpReturnType,
1010
TypedFetch,
1111
} from '../src'
12+
import type { paths } from './paths'
1213
import type { paths as paths2 } from './examples/stripe-openapi2'
1314
import type { paths as paths3 } from './examples/stripe-openapi3'
1415

@@ -40,6 +41,23 @@ interface Openapi3 {
4041
type Same<A, B> = A extends B ? (B extends A ? true : false) : false
4142

4243
describe('infer', () => {
44+
it('queryParams', () => {
45+
const fetcher = Fetcher.for<paths>()
46+
47+
fetcher
48+
.path('/bodyquery/{id}')
49+
.method('post')
50+
// @ts-expect-error // Missing the optional param is wrong
51+
.create({ scalar: 1 })
52+
53+
fetcher
54+
.path('/bodyquery/{id}')
55+
.method('post')
56+
.create({ scalar: 1, optional: 1 })
57+
58+
expect(true).toBe(true)
59+
})
60+
4361
it('argument', () => {
4462
const same: Same<Openapi2['Argument'], Openapi3['Argument']> = true
4563
expect(same).toBe(true)

test/paths.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export type Data = {
99
type Query = {
1010
parameters: {
1111
path: { a: number; b: string }
12-
query: { scalar: string; list: string[] }
12+
query: { scalar: string; list: string[]; optional?: string }
1313
}
1414
responses: { 200: { schema: Data } }
1515
}
@@ -33,7 +33,7 @@ type BodyArray = {
3333
type BodyAndQuery = {
3434
parameters: {
3535
path: { id: number }
36-
query: { scalar: string }
36+
query: { scalar: string; optional?: string }
3737
body: { payload: { list: string[] } }
3838
}
3939
responses: { 201: { schema: Data } }

0 commit comments

Comments
 (0)