Skip to content

Commit ec012a1

Browse files
authored
Merge pull request #17 from cuppachino/make-empty-record-arg-optional
Make empty record arg optional
2 parents 22c67c8 + b1b5b74 commit ec012a1

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

.changeset/smooth-teachers-develop.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+
pass type errors

src/index.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,39 @@ export type _TypedFetch<OP> = (
9393
init?: RequestInit,
9494
) => Promise<ApiResponse<OpReturnType<OP>>>
9595

96-
export type _TypedFetchOptional<OP> = OpArgType<OP> extends Record<
97-
PropertyKey,
98-
never
99-
>
100-
? (
101-
arg?: OpArgType<OP>,
102-
init?: RequestInit,
103-
) => Promise<ApiResponse<OpReturnType<OP>>>
104-
: _TypedFetch<OP>
96+
export type _OptionalTypedFetch<OP> = (
97+
arg?: OpArgType<OP>,
98+
init?: RequestInit,
99+
) => Promise<ApiResponse<OpReturnType<OP>>>
100+
101+
// export type _TypedFetchOptional<OP> = OpArgType<OP> extends Record<
102+
// PropertyKey,
103+
// never
104+
// >
105+
// ? _OptionalTypedFetch<OP>
106+
// : _TypedFetch<OP>
105107

106-
export type TypedFetch<OP> = _TypedFetchOptional<OP> & {
108+
export type TypedFetch<OP> = _TypedFetch<OP> & {
107109
Error: new (error: ApiError) => ApiError & {
108110
getActualType: () => OpErrorType<OP>
109111
}
110112
}
111113

114+
export type TypedFetchOptional<OP> = OpArgType<OP> extends Record<
115+
PropertyKey,
116+
never
117+
>
118+
? _OptionalTypedFetch<OP> & {
119+
Error: new (error: ApiError) => ApiError & {
120+
getActualType: () => OpErrorType<OP>
121+
}
122+
}
123+
: _TypedFetch<OP> & {
124+
Error: new (error: ApiError) => ApiError & {
125+
getActualType: () => OpErrorType<OP>
126+
}
127+
}
128+
112129
export type FetchArgType<F> = F extends TypedFetch<infer OP>
113130
? OpArgType<OP>
114131
: never
@@ -357,7 +374,7 @@ async function fetchUrl<R>(request: Request) {
357374
})) as ApiResponse<R>
358375
}
359376

360-
function createFetch<OP>(fetch: _TypedFetchOptional<OP>): TypedFetch<OP> {
377+
function createFetch<OP>(fetch: _TypedFetch<OP>): TypedFetchOptional<OP> {
361378
const fun = async (payload: OpArgType<OP>, init?: RequestInit) => {
362379
try {
363380
return await fetch(payload, init)
@@ -382,7 +399,7 @@ function createFetch<OP>(fetch: _TypedFetchOptional<OP>): TypedFetch<OP> {
382399
}
383400
}
384401

385-
return fun
402+
return fun as TypedFetchOptional<OP>
386403
}
387404

388405
function fetcher<Paths>() {

0 commit comments

Comments
 (0)