Skip to content

Commit 1ced4cc

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 ccdb224 commit 1ced4cc

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
@@ -123,7 +123,7 @@ type _CreateFetch<OP, Q = never> = [Q] extends [never]
123123

124124
export type CreateFetch<M, OP> = M extends 'post' | 'put' | 'patch' | 'delete'
125125
? OP extends { parameters: { query: infer Q } }
126-
? _CreateFetch<OP, { [K in keyof Q]: true | 1 }>
126+
? _CreateFetch<OP, { [K in keyof Q]-?: true | 1 }>
127127
: _CreateFetch<OP>
128128
: _CreateFetch<OP>
129129

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 {
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)