Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ type _CreateFetch<OP, Q = never> = [Q] extends [never]

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

Expand Down
4 changes: 2 additions & 2 deletions test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('fetch', () => {
const fun = fetcher
.path('/bodyquery/{id}')
.method(method)
.create({ scalar: 1 })
.create({ scalar: 1, optional: 1 })

const { data } = await fun({
id: 1,
Expand Down Expand Up @@ -235,7 +235,7 @@ describe('fetch', () => {
const fun = fetcher
.path('/bodyquery/{id}')
.method('post')
.create({ scalar: 1 })
.create({ scalar: 1, optional: 1 })

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

Expand Down
18 changes: 18 additions & 0 deletions test/infer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
OpReturnType,
TypedFetch,
} from '../src'
import { paths } from './paths'
import { paths as paths2 } from './examples/stripe-openapi2'
import { paths as paths3 } from './examples/stripe-openapi3'

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

describe('infer', () => {
it('queryParams', () => {
const fetcher = Fetcher.for<paths>()

fetcher
.path('/bodyquery/{id}')
.method('post')
// @ts-expect-error // Missing the optional param is wrong
.create({ scalar: 1 })

fetcher
.path('/bodyquery/{id}')
.method('post')
.create({ scalar: 1, optional: 1 })

expect(true).toBe(true)
})

it('argument', () => {
const same: Same<Openapi2['Argument'], Openapi3['Argument']> = true
expect(same).toBe(true)
Expand Down
4 changes: 2 additions & 2 deletions test/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type Data = {
type Query = {
parameters: {
path: { a: number; b: string }
query: { scalar: string; list: string[] }
query: { scalar: string; list: string[]; optional?: string }
}
responses: { 200: { schema: Data } }
}
Expand All @@ -33,7 +33,7 @@ type BodyArray = {
type BodyAndQuery = {
parameters: {
path: { id: number }
query: { scalar: string }
query: { scalar: string; optional?: string }
body: { payload: { list: string[] } }
}
responses: { 201: { schema: Data } }
Expand Down