Skip to content

Commit

Permalink
refactor: re-export IFetchError from ofetch
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Aug 24, 2023
1 parent cf878ab commit 181a5a0
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 15 deletions.
2 changes: 1 addition & 1 deletion playground/pages/petStore.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import type { components } from '#nuxt-api-party/petStore'
import type { FetchError } from '#nuxt-api-party'
import type { components } from '#nuxt-api-party/petStore'
type Pet = components['schemas']['Pet']
Expand Down
2 changes: 1 addition & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ import type { UseApiData, UseOpenApiData, UseApiDataOptions } from '${relativeTo
${schemaEndpointIds.map(i => `import type { paths as ${pascalCase(i)}Paths } from '#${moduleName}/${i}'`).join('')}
export type { FetchError } from '${relativeTo('runtime/types')}'
export type { IFetchError as FetchError } from 'ofetch'
export type { $Api, $OpenApi, ApiFetchOptions, UseApiData, UseOpenApiData, UseApiDataOptions }
${endpointKeys.map(i => `
Expand Down
9 changes: 5 additions & 4 deletions src/runtime/composables/useApiData.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { computed, reactive } from 'vue'
import { hash } from 'ohash'
import type { IFetchError } from 'ofetch'
import type { NitroFetchOptions } from 'nitropack'
import type { WatchSource } from 'vue'
import type { AsyncData, AsyncDataOptions } from 'nuxt/app'
import type { ModuleOptions } from '../../module'
import { headersToObject, resolvePath, serializeMaybeEncodedBody, toValue } from '../utils'
import { isFormData } from '../formData'
import type { EndpointFetchOptions, MaybeRef, MaybeRefOrGetter } from '../utils'
import type { AllPaths, FetchError, GETPaths, GETPlainPaths, HttpMethod, IgnoreCase, OpenApiError, OpenApiRequestOptions, OpenApiResponse, PathItemObject } from '../types'
import type { AllPaths, GETPaths, GETPlainPaths, HttpMethod, IgnoreCase, OpenApiError, OpenApiRequestOptions, OpenApiResponse, PathItemObject } from '../types'
import { useAsyncData, useRequestHeaders, useRuntimeConfig } from '#imports'

type ComputedOptions<T extends Record<string, any>> = {
Expand Down Expand Up @@ -62,7 +63,7 @@ export type UseOpenApiDataOptions<
export type UseApiData = <T = any>(
path: MaybeRefOrGetter<string>,
opts?: UseApiDataOptions<T>,
) => AsyncData<T | undefined, FetchError>
) => AsyncData<T | undefined, IFetchError>

export interface UseOpenApiData<Paths extends Record<string, PathItemObject>> {
<P extends GETPlainPaths<Paths>>(
Expand Down Expand Up @@ -147,7 +148,7 @@ export function _useApiData<T = any>(
...(isFormData(toValue(body)) ? [] : [toValue(body)]),
])}`)

return useAsyncData<T, FetchError>(
return useAsyncData<T, IFetchError>(
key.value,
async (nuxt) => {
controller?.abort?.()
Expand Down Expand Up @@ -208,5 +209,5 @@ export function _useApiData<T = any>(
return result
},
_asyncDataOptions,
) as AsyncData<T | undefined, FetchError>
) as AsyncData<T | undefined, IFetchError>
}
4 changes: 2 additions & 2 deletions src/runtime/server/handler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createError, defineEventHandler, getRequestHeader, getRouterParam, readBody } from 'h3'
import type { IFetchError } from 'ofetch'
import { deserializeMaybeEncodedBody } from '../utils'
import type { ModuleOptions } from '../../module'
import type { EndpointFetchOptions } from '../utils'
import type { FetchError } from '../types'
import { useRuntimeConfig } from '#imports'

export default defineEventHandler(async (event): Promise<any> => {
Expand Down Expand Up @@ -72,7 +72,7 @@ export default defineEventHandler(async (event): Promise<any> => {
)
}
catch (error) {
const { response } = error as FetchError
const { response } = error as IFetchError

throw createError({
statusCode: response?.status,
Expand Down
7 changes: 0 additions & 7 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import type { IFetchError } from 'ofetch'
import type { NitroFetchOptions } from 'nitropack'

// --- General purpose types ---

export type FetchError<T = any> = IFetchError<T>

// --- OpenAPI types ---

export type IgnoreCase<T extends string> = Lowercase<T> | Uppercase<T>
export type RemovePrefix<T extends string, P extends string> = T extends `${P}${infer S}` ? S : never

Expand Down

0 comments on commit 181a5a0

Please sign in to comment.