Skip to content

Commit

Permalink
fix(OpenAPI): extracting default fetch response data
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Oct 31, 2024
1 parent d361460 commit 5390ed1
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
File renamed without changes.
6 changes: 3 additions & 3 deletions src/runtime/composables/$api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { joinURL } from 'ufo'
import { hash } from 'ohash'
import type { NitroFetchOptions } from 'nitropack'
Expand Down Expand Up @@ -37,7 +38,6 @@ export type ApiClientFetchOptions =
Omit<NitroFetchOptions<string>, 'body' | 'cache'>
& {
path?: Record<string, string>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
body?: string | Record<string, any> | FormData | null
}

Expand All @@ -62,9 +62,9 @@ export type OpenAPIClient<Paths> = <
ReqT extends Extract<keyof Paths, string>,
Methods extends FilterMethods<Paths[ReqT]>,
Method extends Extract<keyof Methods, string> | Uppercase<Extract<keyof Methods, string>>,
LowercasedMethod extends Lowercase<Method> extends keyof FilterMethods<Paths[ReqT]> ? Lowercase<Method> : never,
LowercasedMethod extends Lowercase<Method> extends keyof Methods ? Lowercase<Method> : never,
DefaultMethod extends 'get' extends LowercasedMethod ? 'get' : LowercasedMethod,
ResT = FetchResponseData<Paths[ReqT][DefaultMethod]>,
ResT = Methods[DefaultMethod] extends Record<PropertyKey, any> ? FetchResponseData<Methods[DefaultMethod]> : never,
>(
path: ReqT,
options?: OpenAPIClientFetchOptions<Method, LowercasedMethod, Methods>
Expand Down
7 changes: 3 additions & 4 deletions src/runtime/composables/useApiData.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { computed, reactive, toValue } from 'vue'
import { hash } from 'ohash'
import { joinURL } from 'ufo'
Expand All @@ -17,7 +18,6 @@ type ComputedOptions<T> = {
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
[K in keyof T]: T[K] extends Function
? T[K]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
: T[K] extends Record<string, any>
? ComputedOptions<T[K]> | MaybeRef<T[K]>
: MaybeRef<T[K]>;
Expand Down Expand Up @@ -69,7 +69,6 @@ export type UseApiDataOptions<T> = Pick<
| 'timeout'
> & {
path?: MaybeRefOrGetter<Record<string, string>>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
body?: MaybeRef<string | Record<string, any> | FormData | null>
} & SharedAsyncDataOptions<T>

Expand Down Expand Up @@ -99,8 +98,8 @@ export type UseOpenAPIData<Paths> = <
Method extends Extract<keyof Methods, string> | Uppercase<Extract<keyof Methods, string>>,
LowercasedMethod extends Lowercase<Method> extends keyof Methods ? Lowercase<Method> : never,
DefaultMethod extends 'get' extends LowercasedMethod ? 'get' : LowercasedMethod,
ResT = FetchResponseData<Methods[DefaultMethod]>,
ErrorT = FetchResponseError<Methods[DefaultMethod]>,
ResT = Methods[DefaultMethod] extends Record<PropertyKey, any> ? FetchResponseData<Methods[DefaultMethod]> : never,
ErrorT = Methods[DefaultMethod] extends Record<PropertyKey, any> ? FetchResponseError<Methods[DefaultMethod]> : never,
DataT = ResT,
>(
path: MaybeRefOrGetter<ReqT>,
Expand Down
6 changes: 4 additions & 2 deletions src/runtime/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import type {
SuccessResponse,
} from 'openapi-typescript-helpers'

export type FetchResponseData<T> = SuccessResponse<ResponseObjectMap<T>, MediaType>
export type FetchResponseError<T> = NuxtError<ErrorResponse<ResponseObjectMap<T>, MediaType>>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type FetchResponseData<T extends Record<PropertyKey, any>> = SuccessResponse<ResponseObjectMap<T>, MediaType>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type FetchResponseError<T extends Record<PropertyKey, any>> = NuxtError<ErrorResponse<ResponseObjectMap<T>, MediaType>>

export type MethodOption<M, P> = 'get' extends keyof P ? { method?: M } : { method: M }

Expand Down

0 comments on commit 5390ed1

Please sign in to comment.