Skip to content

Commit e2535b9

Browse files
happycollisionRendez
authored andcommitted
Fix strictness of query params
In cases where a non-GET request has optional query params, the `.create()` function incorrectly allow you to omit them. This runtime code _must_ be present so the library can distinguish between body and query parameters, even if the actual call omits the optional param
1 parent 159b9a5 commit e2535b9

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

src/types.ts

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

139139
export type CreateFetch<M, OP> = M extends 'post' | 'put' | 'patch' | 'delete'
140140
? OP extends { parameters: { query: infer Q } }
141-
? _CreateFetch<OP, { [K in keyof Q]: true | 1 }>
141+
? _CreateFetch<OP, { [K in keyof Q]-?: true | 1 }>
142142
: _CreateFetch<OP>
143143
: _CreateFetch<OP>
144144

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,
@@ -242,7 +242,7 @@ describe('fetch', () => {
242242
const fun = fetcher
243243
.path('/bodyquery/{id}')
244244
.method('post')
245-
.create({ scalar: 1 })
245+
.create({ scalar: 1, optional: 1 })
246246

247247
const captured = { url: '', body: '' }
248248

test/infer.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
OpReturnType,
1010
TypedFetch,
1111
} from '../src'
12+
import { paths } from './paths'
1213
import { paths as paths2 } from './examples/stripe-openapi2'
1314
import { 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)