diff --git a/.changeset/client-unwrap-method.md b/.changeset/client-unwrap-method.md new file mode 100644 index 000000000..f3cc02b65 --- /dev/null +++ b/.changeset/client-unwrap-method.md @@ -0,0 +1,20 @@ +--- +'@kubb/plugin-axios': minor +'@kubb/plugin-fetch': minor +'@kubb/plugin-react-query': patch +'@kubb/plugin-vue-query': patch +'@kubb/plugin-swr': patch +--- + +Generated calls now return a promise with an extra `unwrap()` method. Calling it gives you the bare +success body, or rejects with `error` when the call was made with `throwOnError: false`. + +```ts +const { data, error } = await getPetById({ path: { petId: 1 } }) +const pet = await getPetById({ path: { petId: 1 } }).unwrap() +``` + +Awaiting the call directly still gives the full result, so nothing existing changes. + +`plugin-react-query`, `plugin-vue-query`, and `plugin-swr` now build their generated query and +mutation bodies on top of `unwrap()` too, instead of destructuring the result by hand. diff --git a/examples/advanced/src/gen/.kubb/client.ts b/examples/advanced/src/gen/.kubb/client.ts index 72bedcf64..cb9cb6470 100644 --- a/examples/advanced/src/gen/.kubb/client.ts +++ b/examples/advanced/src/gen/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/examples/advanced/src/gen/clients/axios/petService/addFiles.ts b/examples/advanced/src/gen/clients/axios/petService/addFiles.ts index 46c56c7c1..31de976f7 100644 --- a/examples/advanced/src/gen/clients/axios/petService/addFiles.ts +++ b/examples/advanced/src/gen/clients/axios/petService/addFiles.ts @@ -1,6 +1,6 @@ -import type { Options, RequestResult } from '../../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../../.kubb/client' import type { AddFilesOptions, AddFilesResponses } from '../../../models/ts/pet/AddFiles' -import { client } from '../../../.kubb/client' +import { client, withUnwrap } from '../../../.kubb/client' /** * @description Place a new file in the store @@ -9,8 +9,8 @@ import { client } from '../../../.kubb/client' */ export function addFiles( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/files', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/files', ...config }) as Promise>) } diff --git a/examples/advanced/src/gen/clients/axios/petService/addPet.ts b/examples/advanced/src/gen/clients/axios/petService/addPet.ts index b434cfb41..e72908e8c 100644 --- a/examples/advanced/src/gen/clients/axios/petService/addPet.ts +++ b/examples/advanced/src/gen/clients/axios/petService/addPet.ts @@ -1,6 +1,6 @@ -import type { Options, RequestResult } from '../../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../../../models/ts/pet/AddPet' -import { client } from '../../../.kubb/client' +import { client, withUnwrap } from '../../../.kubb/client' import { addPetResponseSchema, addPetErrorSchema } from '../../../zod/pet/addPetSchema' /** @@ -10,14 +10,16 @@ import { addPetResponseSchema, addPetErrorSchema } from '../../../zod/pet/addPet */ export function addPet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'POST', - url: '/pet', - security: [{ type: 'oauth2' }], - validator: { response: addPetResponseSchema, error: addPetErrorSchema }, - ...config, - }) as Promise> + return withUnwrap( + request({ + method: 'POST', + url: '/pet', + security: [{ type: 'oauth2' }], + validator: { response: addPetResponseSchema, error: addPetErrorSchema }, + ...config, + }) as Promise>, + ) } diff --git a/examples/advanced/src/gen/clients/axios/petService/deletePet.ts b/examples/advanced/src/gen/clients/axios/petService/deletePet.ts index 6661b56cc..3b43fbe5f 100644 --- a/examples/advanced/src/gen/clients/axios/petService/deletePet.ts +++ b/examples/advanced/src/gen/clients/axios/petService/deletePet.ts @@ -1,6 +1,6 @@ -import type { Options, RequestResult } from '../../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../../../models/ts/pet/DeletePet' -import { client } from '../../../.kubb/client' +import { client, withUnwrap } from '../../../.kubb/client' import { deletePetResponseSchema, deletePetErrorSchema } from '../../../zod/pet/deletePetSchema' /** @@ -10,14 +10,16 @@ import { deletePetResponseSchema, deletePetErrorSchema } from '../../../zod/pet/ */ export function deletePet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'DELETE', - url: '/pet/{petId}:search', - security: [{ type: 'oauth2' }], - validator: { response: deletePetResponseSchema, error: deletePetErrorSchema }, - ...config, - }) as Promise> + return withUnwrap( + request({ + method: 'DELETE', + url: '/pet/{petId}:search', + security: [{ type: 'oauth2' }], + validator: { response: deletePetResponseSchema, error: deletePetErrorSchema }, + ...config, + }) as Promise>, + ) } diff --git a/examples/advanced/src/gen/clients/axios/petService/findPetsByStatus.ts b/examples/advanced/src/gen/clients/axios/petService/findPetsByStatus.ts index 620dbf972..9ea2dd6a8 100644 --- a/examples/advanced/src/gen/clients/axios/petService/findPetsByStatus.ts +++ b/examples/advanced/src/gen/clients/axios/petService/findPetsByStatus.ts @@ -1,6 +1,6 @@ -import type { Options, RequestResult } from '../../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../../../models/ts/pet/FindPetsByStatus' -import { client } from '../../../.kubb/client' +import { client, withUnwrap } from '../../../.kubb/client' import { findPetsByStatusResponseSchema, findPetsByStatusErrorSchema } from '../../../zod/pet/findPetsByStatusSchema' /** @@ -10,14 +10,16 @@ import { findPetsByStatusResponseSchema, findPetsByStatusErrorSchema } from '../ */ export function findPetsByStatus( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/findByStatus/{step_id}', - security: [{ type: 'oauth2' }], - validator: { response: findPetsByStatusResponseSchema, error: findPetsByStatusErrorSchema }, - ...config, - }) as Promise> + return withUnwrap( + request({ + method: 'GET', + url: '/pet/findByStatus/{step_id}', + security: [{ type: 'oauth2' }], + validator: { response: findPetsByStatusResponseSchema, error: findPetsByStatusErrorSchema }, + ...config, + }) as Promise>, + ) } diff --git a/examples/advanced/src/gen/clients/axios/petService/findPetsByTags.ts b/examples/advanced/src/gen/clients/axios/petService/findPetsByTags.ts index 61d92758f..ee7242ca4 100644 --- a/examples/advanced/src/gen/clients/axios/petService/findPetsByTags.ts +++ b/examples/advanced/src/gen/clients/axios/petService/findPetsByTags.ts @@ -1,6 +1,6 @@ -import type { Options, RequestResult } from '../../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../../.kubb/client' import type { FindPetsByTagsOptions, FindPetsByTagsResponses } from '../../../models/ts/pet/FindPetsByTags' -import { client } from '../../../.kubb/client' +import { client, withUnwrap } from '../../../.kubb/client' import { findPetsByTagsResponseSchema, findPetsByTagsErrorSchema } from '../../../zod/pet/findPetsByTagsSchema' /** @@ -10,15 +10,17 @@ import { findPetsByTagsResponseSchema, findPetsByTagsErrorSchema } from '../../. */ export function findPetsByTags( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/findByTags', - security: [{ type: 'oauth2' }], - styles: { query: { tags: { explode: true } } }, - validator: { response: findPetsByTagsResponseSchema, error: findPetsByTagsErrorSchema }, - ...config, - }) as Promise> + return withUnwrap( + request({ + method: 'GET', + url: '/pet/findByTags', + security: [{ type: 'oauth2' }], + styles: { query: { tags: { explode: true } } }, + validator: { response: findPetsByTagsResponseSchema, error: findPetsByTagsErrorSchema }, + ...config, + }) as Promise>, + ) } diff --git a/examples/advanced/src/gen/clients/axios/petService/getPetById.ts b/examples/advanced/src/gen/clients/axios/petService/getPetById.ts index 2fc916be0..8904f6486 100644 --- a/examples/advanced/src/gen/clients/axios/petService/getPetById.ts +++ b/examples/advanced/src/gen/clients/axios/petService/getPetById.ts @@ -1,6 +1,6 @@ -import type { Options, RequestResult } from '../../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../../../models/ts/pet/GetPetById' -import { client } from '../../../.kubb/client' +import { client, withUnwrap } from '../../../.kubb/client' import { getPetByIdResponseSchema, getPetByIdErrorSchema } from '../../../zod/pet/getPetByIdSchema' /** @@ -10,14 +10,16 @@ import { getPetByIdResponseSchema, getPetByIdErrorSchema } from '../../../zod/pe */ export function getPetById( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/{petId}:search', - security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], - validator: { response: getPetByIdResponseSchema, error: getPetByIdErrorSchema }, - ...config, - }) as Promise> + return withUnwrap( + request({ + method: 'GET', + url: '/pet/{petId}:search', + security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], + validator: { response: getPetByIdResponseSchema, error: getPetByIdErrorSchema }, + ...config, + }) as Promise>, + ) } diff --git a/examples/advanced/src/gen/clients/axios/petService/updatePet.ts b/examples/advanced/src/gen/clients/axios/petService/updatePet.ts index 7a15569f8..de286c676 100644 --- a/examples/advanced/src/gen/clients/axios/petService/updatePet.ts +++ b/examples/advanced/src/gen/clients/axios/petService/updatePet.ts @@ -1,6 +1,6 @@ -import type { Options, RequestResult } from '../../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../../.kubb/client' import type { UpdatePetOptions, UpdatePetResponses } from '../../../models/ts/pet/UpdatePet' -import { client } from '../../../.kubb/client' +import { client, withUnwrap } from '../../../.kubb/client' import { updatePetResponseSchema, updatePetErrorSchema } from '../../../zod/pet/updatePetSchema' /** @@ -10,14 +10,16 @@ import { updatePetResponseSchema, updatePetErrorSchema } from '../../../zod/pet/ */ export function updatePet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'PUT', - url: '/pet', - security: [{ type: 'oauth2' }], - validator: { response: updatePetResponseSchema, error: updatePetErrorSchema }, - ...config, - }) as Promise> + return withUnwrap( + request({ + method: 'PUT', + url: '/pet', + security: [{ type: 'oauth2' }], + validator: { response: updatePetResponseSchema, error: updatePetErrorSchema }, + ...config, + }) as Promise>, + ) } diff --git a/examples/advanced/src/gen/clients/axios/petService/updatePetWithForm.ts b/examples/advanced/src/gen/clients/axios/petService/updatePetWithForm.ts index f7b71e1cd..3638ec754 100644 --- a/examples/advanced/src/gen/clients/axios/petService/updatePetWithForm.ts +++ b/examples/advanced/src/gen/clients/axios/petService/updatePetWithForm.ts @@ -1,6 +1,6 @@ -import type { Options, RequestResult } from '../../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../../.kubb/client' import type { UpdatePetWithFormOptions, UpdatePetWithFormResponses } from '../../../models/ts/pet/UpdatePetWithForm' -import { client } from '../../../.kubb/client' +import { client, withUnwrap } from '../../../.kubb/client' import { updatePetWithFormResponseSchema, updatePetWithFormErrorSchema } from '../../../zod/pet/updatePetWithFormSchema' /** @@ -9,14 +9,16 @@ import { updatePetWithFormResponseSchema, updatePetWithFormErrorSchema } from '. */ export function updatePetWithForm( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'POST', - url: '/pet/{petId}:search', - security: [{ type: 'oauth2' }], - validator: { response: updatePetWithFormResponseSchema, error: updatePetWithFormErrorSchema }, - ...config, - }) as Promise> + return withUnwrap( + request({ + method: 'POST', + url: '/pet/{petId}:search', + security: [{ type: 'oauth2' }], + validator: { response: updatePetWithFormResponseSchema, error: updatePetWithFormErrorSchema }, + ...config, + }) as Promise>, + ) } diff --git a/examples/advanced/src/gen/clients/axios/petService/uploadFile.ts b/examples/advanced/src/gen/clients/axios/petService/uploadFile.ts index 19097e39c..0739b656a 100644 --- a/examples/advanced/src/gen/clients/axios/petService/uploadFile.ts +++ b/examples/advanced/src/gen/clients/axios/petService/uploadFile.ts @@ -1,6 +1,6 @@ -import type { Options, RequestResult } from '../../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../../../models/ts/pet/UploadFile' -import { client } from '../../../.kubb/client' +import { client, withUnwrap } from '../../../.kubb/client' /** * @summary uploads an image @@ -8,14 +8,16 @@ import { client } from '../../../.kubb/client' */ export function uploadFile( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'POST', - url: '/pet/{petId}/uploadImage', - security: [{ type: 'oauth2' }], - contentType: { request: 'application/octet-stream' }, - ...config, - }) as Promise> + return withUnwrap( + request({ + method: 'POST', + url: '/pet/{petId}/uploadImage', + security: [{ type: 'oauth2' }], + contentType: { request: 'application/octet-stream' }, + ...config, + }) as Promise>, + ) } diff --git a/examples/advanced/src/gen/clients/axios/petsService/createPets.ts b/examples/advanced/src/gen/clients/axios/petsService/createPets.ts index 972e97865..aa822557c 100644 --- a/examples/advanced/src/gen/clients/axios/petsService/createPets.ts +++ b/examples/advanced/src/gen/clients/axios/petsService/createPets.ts @@ -1,6 +1,6 @@ -import type { Options, RequestResult } from '../../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../../.kubb/client' import type { CreatePetsOptions, CreatePetsResponses } from '../../../models/ts/pets/CreatePets' -import { client } from '../../../.kubb/client' +import { client, withUnwrap } from '../../../.kubb/client' import { createPetsResponseSchema, createPetsErrorSchema } from '../../../zod/pets/createPetsSchema' /** @@ -9,13 +9,12 @@ import { createPetsResponseSchema, createPetsErrorSchema } from '../../../zod/pe */ export function createPets( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'POST', - url: '/pets/{uuid}', - validator: { response: createPetsResponseSchema, error: createPetsErrorSchema }, - ...config, - }) as Promise> + return withUnwrap( + request({ method: 'POST', url: '/pets/{uuid}', validator: { response: createPetsResponseSchema, error: createPetsErrorSchema }, ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/advanced/src/gen/clients/hooks/pet/useAddFiles.ts b/examples/advanced/src/gen/clients/hooks/pet/useAddFiles.ts index 03be6a659..6e28592d6 100644 --- a/examples/advanced/src/gen/clients/hooks/pet/useAddFiles.ts +++ b/examples/advanced/src/gen/clients/hooks/pet/useAddFiles.ts @@ -15,8 +15,7 @@ export function addFilesMutationOptions( return mutationOptions, AddFilesOptions, TContext>({ mutationKey, mutationFn: async ({ body }) => { - const { data } = await addFiles({ ...config, body, throwOnError: true }) - return data + return addFiles({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/examples/advanced/src/gen/clients/hooks/pet/useAddPet.ts b/examples/advanced/src/gen/clients/hooks/pet/useAddPet.ts index 06af41b62..609a829de 100644 --- a/examples/advanced/src/gen/clients/hooks/pet/useAddPet.ts +++ b/examples/advanced/src/gen/clients/hooks/pet/useAddPet.ts @@ -15,8 +15,7 @@ export function addPetMutationOptions( return mutationOptions, AddPetOptions, TContext>({ mutationKey, mutationFn: async ({ body }) => { - const { data } = await addPet({ ...config, body, throwOnError: true }) - return data + return addPet({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/examples/advanced/src/gen/clients/hooks/pet/useDeletePet.ts b/examples/advanced/src/gen/clients/hooks/pet/useDeletePet.ts index a9134abe9..ce28e5dff 100644 --- a/examples/advanced/src/gen/clients/hooks/pet/useDeletePet.ts +++ b/examples/advanced/src/gen/clients/hooks/pet/useDeletePet.ts @@ -11,8 +11,7 @@ export function deletePetMutationOptions(config: Partial, DeletePetOptions, TContext>({ mutationKey, mutationFn: async ({ path, headers }) => { - const { data } = await deletePet({ ...config, path, headers, throwOnError: true }) - return data + return deletePet({ ...config, path, headers, throwOnError: true }).unwrap() }, }) } diff --git a/examples/advanced/src/gen/clients/hooks/pet/useFindPetsByStatus.ts b/examples/advanced/src/gen/clients/hooks/pet/useFindPetsByStatus.ts index 27230b41d..9a5305bb5 100644 --- a/examples/advanced/src/gen/clients/hooks/pet/useFindPetsByStatus.ts +++ b/examples/advanced/src/gen/clients/hooks/pet/useFindPetsByStatus.ts @@ -16,8 +16,7 @@ export function findPetsByStatusQueryOptions( return queryOptions, FindPetsByStatusStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, path, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, path, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/examples/advanced/src/gen/clients/hooks/pet/useFindPetsByTags.ts b/examples/advanced/src/gen/clients/hooks/pet/useFindPetsByTags.ts index ddb821c8a..d9647cd1f 100644 --- a/examples/advanced/src/gen/clients/hooks/pet/useFindPetsByTags.ts +++ b/examples/advanced/src/gen/clients/hooks/pet/useFindPetsByTags.ts @@ -17,8 +17,7 @@ export function findPetsByTagsQueryOptions( return queryOptions, FindPetsByTagsStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByTags({ ...config, query, headers, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query, headers, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/examples/advanced/src/gen/clients/hooks/pet/useFindPetsByTagsInfinite.ts b/examples/advanced/src/gen/clients/hooks/pet/useFindPetsByTagsInfinite.ts index a6c8d658f..a02c086a9 100644 --- a/examples/advanced/src/gen/clients/hooks/pet/useFindPetsByTagsInfinite.ts +++ b/examples/advanced/src/gen/clients/hooks/pet/useFindPetsByTagsInfinite.ts @@ -27,8 +27,7 @@ export function findPetsByTagsInfiniteQueryOptions( ...(query ?? {}), ['pageSize']: pageParam as unknown as FindPetsByTagsQuery['pageSize'], } as FindPetsByTagsQuery - const { data } = await findPetsByTags({ ...config, query, headers, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query, headers, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, initialPageParam: 0, getNextPageParam: (lastPage, _allPages, lastPageParam) => (Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1), diff --git a/examples/advanced/src/gen/clients/hooks/pet/useGetPetById.ts b/examples/advanced/src/gen/clients/hooks/pet/useGetPetById.ts index a09692ab7..4faef1da6 100644 --- a/examples/advanced/src/gen/clients/hooks/pet/useGetPetById.ts +++ b/examples/advanced/src/gen/clients/hooks/pet/useGetPetById.ts @@ -13,8 +13,7 @@ export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Part return queryOptions, GetPetByIdStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/examples/advanced/src/gen/clients/hooks/pet/useUpdatePet.ts b/examples/advanced/src/gen/clients/hooks/pet/useUpdatePet.ts index 0959cfd55..5adc62bc5 100644 --- a/examples/advanced/src/gen/clients/hooks/pet/useUpdatePet.ts +++ b/examples/advanced/src/gen/clients/hooks/pet/useUpdatePet.ts @@ -27,8 +27,7 @@ export function updatePetMutationOptions( >({ mutationKey, mutationFn: async ({ body }) => { - const { data } = await updatePet({ ...config, body, throwOnError: true }) - return data + return updatePet({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/examples/advanced/src/gen/clients/hooks/pet/useUpdatePetWithForm.ts b/examples/advanced/src/gen/clients/hooks/pet/useUpdatePetWithForm.ts index 698bdeea4..71e617646 100644 --- a/examples/advanced/src/gen/clients/hooks/pet/useUpdatePetWithForm.ts +++ b/examples/advanced/src/gen/clients/hooks/pet/useUpdatePetWithForm.ts @@ -11,8 +11,7 @@ export function updatePetWithFormMutationOptions(config: Par return mutationOptions, UpdatePetWithFormOptions, TContext>({ mutationKey, mutationFn: async ({ path, query }) => { - const { data } = await updatePetWithForm({ ...config, path, query, throwOnError: true }) - return data + return updatePetWithForm({ ...config, path, query, throwOnError: true }).unwrap() }, }) } diff --git a/examples/advanced/src/gen/clients/hooks/pet/useUploadFile.ts b/examples/advanced/src/gen/clients/hooks/pet/useUploadFile.ts index 333c4935f..8e0775da7 100644 --- a/examples/advanced/src/gen/clients/hooks/pet/useUploadFile.ts +++ b/examples/advanced/src/gen/clients/hooks/pet/useUploadFile.ts @@ -11,8 +11,7 @@ export function uploadFileMutationOptions(config: Partial, UploadFileOptions, TContext>({ mutationKey, mutationFn: async ({ path, query, body }) => { - const { data } = await uploadFile({ ...config, path, query, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, query, body, throwOnError: true }).unwrap() }, }) } diff --git a/examples/advanced/src/gen/clients/hooks/pets/useCreatePets.ts b/examples/advanced/src/gen/clients/hooks/pets/useCreatePets.ts index 73d3be8b5..975b7dd51 100644 --- a/examples/advanced/src/gen/clients/hooks/pets/useCreatePets.ts +++ b/examples/advanced/src/gen/clients/hooks/pets/useCreatePets.ts @@ -11,8 +11,7 @@ export function createPetsMutationOptions(config: Partial, CreatePetsOptions, TContext>({ mutationKey, mutationFn: async ({ path, query, body, headers }) => { - const { data } = await createPets({ ...config, path, query, body, headers, throwOnError: true }) - return data + return createPets({ ...config, path, query, body, headers, throwOnError: true }).unwrap() }, }) } diff --git a/examples/axios/src/gen/.kubb/client.ts b/examples/axios/src/gen/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/examples/axios/src/gen/.kubb/client.ts +++ b/examples/axios/src/gen/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/examples/axios/src/gen/clients/pet/addPet.ts b/examples/axios/src/gen/clients/pet/addPet.ts index 952e15db2..3628776a0 100644 --- a/examples/axios/src/gen/clients/pet/addPet.ts +++ b/examples/axios/src/gen/clients/pet/addPet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../../models/pet/AddPet' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Add a new pet to the store @@ -14,8 +14,10 @@ import { client } from '../../.kubb/client' */ export function addPet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap( + request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/examples/axios/src/gen/clients/pet/deletePet.ts b/examples/axios/src/gen/clients/pet/deletePet.ts index 24725dc26..8fe616c13 100644 --- a/examples/axios/src/gen/clients/pet/deletePet.ts +++ b/examples/axios/src/gen/clients/pet/deletePet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../../models/pet/DeletePet' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description delete a pet @@ -14,10 +14,10 @@ import { client } from '../../.kubb/client' */ export function deletePet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/examples/axios/src/gen/clients/pet/findPetsByStatus.ts b/examples/axios/src/gen/clients/pet/findPetsByStatus.ts index 306835cef..78c47179c 100644 --- a/examples/axios/src/gen/clients/pet/findPetsByStatus.ts +++ b/examples/axios/src/gen/clients/pet/findPetsByStatus.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../../models/pet/FindPetsByStatus' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings @@ -14,14 +14,16 @@ import { client } from '../../.kubb/client' */ export function findPetsByStatus( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/findByStatus', - security: [{ type: 'oauth2' }], - styles: { query: { status: { explode: true } } }, - ...config, - }) as Promise> + return withUnwrap( + request({ + method: 'GET', + url: '/pet/findByStatus', + security: [{ type: 'oauth2' }], + styles: { query: { status: { explode: true } } }, + ...config, + }) as Promise>, + ) } diff --git a/examples/axios/src/gen/clients/pet/findPetsByTags.ts b/examples/axios/src/gen/clients/pet/findPetsByTags.ts index b7227971b..e4b9b5f55 100644 --- a/examples/axios/src/gen/clients/pet/findPetsByTags.ts +++ b/examples/axios/src/gen/clients/pet/findPetsByTags.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { FindPetsByTagsOptions, FindPetsByTagsResponses } from '../../models/pet/FindPetsByTags' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. @@ -14,14 +14,12 @@ import { client } from '../../.kubb/client' */ export function findPetsByTags( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/findByTags', - security: [{ type: 'oauth2' }], - styles: { query: { tags: { explode: true } } }, - ...config, - }) as Promise> + return withUnwrap( + request({ method: 'GET', url: '/pet/findByTags', security: [{ type: 'oauth2' }], styles: { query: { tags: { explode: true } } }, ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/axios/src/gen/clients/pet/getPetById.ts b/examples/axios/src/gen/clients/pet/getPetById.ts index 771e59ee1..81e309e82 100644 --- a/examples/axios/src/gen/clients/pet/getPetById.ts +++ b/examples/axios/src/gen/clients/pet/getPetById.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../../models/pet/GetPetById' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Returns a single pet @@ -14,13 +14,12 @@ import { client } from '../../.kubb/client' */ export function getPetById( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/{petId}', - security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], - ...config, - }) as Promise> + return withUnwrap( + request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/axios/src/gen/clients/pet/updatePet.ts b/examples/axios/src/gen/clients/pet/updatePet.ts index 7a287a9fe..f662fd79f 100644 --- a/examples/axios/src/gen/clients/pet/updatePet.ts +++ b/examples/axios/src/gen/clients/pet/updatePet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { UpdatePetOptions, UpdatePetResponses } from '../../models/pet/UpdatePet' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Update an existing pet by Id @@ -14,8 +14,10 @@ import { client } from '../../.kubb/client' */ export function updatePet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'PUT', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap( + request({ method: 'PUT', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/examples/axios/src/gen/clients/pet/updatePetWithForm.ts b/examples/axios/src/gen/clients/pet/updatePetWithForm.ts index f36371438..1db98bc49 100644 --- a/examples/axios/src/gen/clients/pet/updatePetWithForm.ts +++ b/examples/axios/src/gen/clients/pet/updatePetWithForm.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { UpdatePetWithFormOptions, UpdatePetWithFormResponses } from '../../models/pet/UpdatePetWithForm' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary Updates a pet in the store with form data @@ -13,10 +13,12 @@ import { client } from '../../.kubb/client' */ export function updatePetWithForm( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'POST', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/axios/src/gen/clients/pet/uploadFile.ts b/examples/axios/src/gen/clients/pet/uploadFile.ts index 65e79b47b..92ce7c699 100644 --- a/examples/axios/src/gen/clients/pet/uploadFile.ts +++ b/examples/axios/src/gen/clients/pet/uploadFile.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../../models/pet/UploadFile' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary uploads an image @@ -13,10 +13,12 @@ import { client } from '../../.kubb/client' */ export function uploadFile( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/axios/src/gen/clients/store/deleteOrder.ts b/examples/axios/src/gen/clients/store/deleteOrder.ts index 74eec3514..689cbaa9a 100644 --- a/examples/axios/src/gen/clients/store/deleteOrder.ts +++ b/examples/axios/src/gen/clients/store/deleteOrder.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { DeleteOrderOptions, DeleteOrderResponses } from '../../models/store/DeleteOrder' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function deleteOrder( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/store/order/{orderId}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/store/order/{orderId}', ...config }) as Promise>) } diff --git a/examples/axios/src/gen/clients/store/getInventory.ts b/examples/axios/src/gen/clients/store/getInventory.ts index f011e8dde..d44938f8e 100644 --- a/examples/axios/src/gen/clients/store/getInventory.ts +++ b/examples/axios/src/gen/clients/store/getInventory.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../../models/store/GetInventory' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Returns a map of status codes to quantities @@ -14,10 +14,12 @@ import { client } from '../../.kubb/client' */ export function getInventory( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/axios/src/gen/clients/store/getOrderById.ts b/examples/axios/src/gen/clients/store/getOrderById.ts index 91aa5bd78..a3ce96a72 100644 --- a/examples/axios/src/gen/clients/store/getOrderById.ts +++ b/examples/axios/src/gen/clients/store/getOrderById.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { GetOrderByIdOptions, GetOrderByIdResponses } from '../../models/store/GetOrderById' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions. @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function getOrderById( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/order/{orderId}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/order/{orderId}', ...config }) as Promise>) } diff --git a/examples/axios/src/gen/clients/store/placeOrder.ts b/examples/axios/src/gen/clients/store/placeOrder.ts index 8c1d56394..3a3da9192 100644 --- a/examples/axios/src/gen/clients/store/placeOrder.ts +++ b/examples/axios/src/gen/clients/store/placeOrder.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../../models/store/PlaceOrder' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Place a new order in the store @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function placeOrder( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/examples/axios/src/gen/clients/store/placeOrderPatch.ts b/examples/axios/src/gen/clients/store/placeOrderPatch.ts index 0772af4f7..692337b25 100644 --- a/examples/axios/src/gen/clients/store/placeOrderPatch.ts +++ b/examples/axios/src/gen/clients/store/placeOrderPatch.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { PlaceOrderPatchOptions, PlaceOrderPatchResponses } from '../../models/store/PlaceOrderPatch' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Place a new order in the store with patch @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function placeOrderPatch( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'PATCH', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'PATCH', url: '/store/order', ...config }) as Promise>) } diff --git a/examples/axios/src/gen/clients/user/createUser.ts b/examples/axios/src/gen/clients/user/createUser.ts index af5bbc3e7..d9c8c13bc 100644 --- a/examples/axios/src/gen/clients/user/createUser.ts +++ b/examples/axios/src/gen/clients/user/createUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { CreateUserOptions, CreateUserResponses } from '../../models/user/CreateUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description This can only be done by the logged in user. @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function createUser( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/user', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/user', ...config }) as Promise>) } diff --git a/examples/axios/src/gen/clients/user/createUsersWithListInput.ts b/examples/axios/src/gen/clients/user/createUsersWithListInput.ts index b69d5d908..1e6a9e2ac 100644 --- a/examples/axios/src/gen/clients/user/createUsersWithListInput.ts +++ b/examples/axios/src/gen/clients/user/createUsersWithListInput.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { CreateUsersWithListInputOptions, CreateUsersWithListInputResponses } from '../../models/user/CreateUsersWithListInput' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Creates list of users with given input array @@ -14,8 +14,10 @@ import { client } from '../../.kubb/client' */ export function createUsersWithListInput( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/user/createWithList', ...config }) as Promise> + return withUnwrap( + request({ method: 'POST', url: '/user/createWithList', ...config }) as Promise>, + ) } diff --git a/examples/axios/src/gen/clients/user/deleteUser.ts b/examples/axios/src/gen/clients/user/deleteUser.ts index dff00a13e..8dda91163 100644 --- a/examples/axios/src/gen/clients/user/deleteUser.ts +++ b/examples/axios/src/gen/clients/user/deleteUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { DeleteUserOptions, DeleteUserResponses } from '../../models/user/DeleteUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description This can only be done by the logged in user. @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function deleteUser( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/user/{username}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/user/{username}', ...config }) as Promise>) } diff --git a/examples/axios/src/gen/clients/user/getUserByName.ts b/examples/axios/src/gen/clients/user/getUserByName.ts index 4db3b9d4d..2e0afe704 100644 --- a/examples/axios/src/gen/clients/user/getUserByName.ts +++ b/examples/axios/src/gen/clients/user/getUserByName.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { GetUserByNameOptions, GetUserByNameResponses } from '../../models/user/GetUserByName' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary Get user by user name @@ -13,8 +13,8 @@ import { client } from '../../.kubb/client' */ export function getUserByName( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/user/{username}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/user/{username}', ...config }) as Promise>) } diff --git a/examples/axios/src/gen/clients/user/loginUser.ts b/examples/axios/src/gen/clients/user/loginUser.ts index b619b4b9b..4a46c2208 100644 --- a/examples/axios/src/gen/clients/user/loginUser.ts +++ b/examples/axios/src/gen/clients/user/loginUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { LoginUserOptions, LoginUserResponses } from '../../models/user/LoginUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary Logs user into the system @@ -13,8 +13,8 @@ import { client } from '../../.kubb/client' */ export function loginUser( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/user/login', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/user/login', ...config }) as Promise>) } diff --git a/examples/axios/src/gen/clients/user/logoutUser.ts b/examples/axios/src/gen/clients/user/logoutUser.ts index e41276de9..a5c939fbb 100644 --- a/examples/axios/src/gen/clients/user/logoutUser.ts +++ b/examples/axios/src/gen/clients/user/logoutUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { LogoutUserOptions, LogoutUserResponses } from '../../models/user/LogoutUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary Logs out current logged in user session @@ -13,8 +13,8 @@ import { client } from '../../.kubb/client' */ export function logoutUser( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/user/logout', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/user/logout', ...config }) as Promise>) } diff --git a/examples/axios/src/gen/clients/user/updateUser.ts b/examples/axios/src/gen/clients/user/updateUser.ts index ce0faef1f..b6c6e7de1 100644 --- a/examples/axios/src/gen/clients/user/updateUser.ts +++ b/examples/axios/src/gen/clients/user/updateUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { UpdateUserOptions, UpdateUserResponses } from '../../models/user/UpdateUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description This can only be done by the logged in user. @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function updateUser( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'PUT', url: '/user/{username}', ...config }) as Promise> + return withUnwrap(request({ method: 'PUT', url: '/user/{username}', ...config }) as Promise>) } diff --git a/examples/fetch/src/gen/.kubb/client.ts b/examples/fetch/src/gen/.kubb/client.ts index f80182aca..ea6223eff 100644 --- a/examples/fetch/src/gen/.kubb/client.ts +++ b/examples/fetch/src/gen/.kubb/client.ts @@ -82,6 +82,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/examples/fetch/src/gen/clients/pet/addPet.ts b/examples/fetch/src/gen/clients/pet/addPet.ts index 952e15db2..3628776a0 100644 --- a/examples/fetch/src/gen/clients/pet/addPet.ts +++ b/examples/fetch/src/gen/clients/pet/addPet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../../models/pet/AddPet' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Add a new pet to the store @@ -14,8 +14,10 @@ import { client } from '../../.kubb/client' */ export function addPet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap( + request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/examples/fetch/src/gen/clients/pet/deletePet.ts b/examples/fetch/src/gen/clients/pet/deletePet.ts index 24725dc26..8fe616c13 100644 --- a/examples/fetch/src/gen/clients/pet/deletePet.ts +++ b/examples/fetch/src/gen/clients/pet/deletePet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../../models/pet/DeletePet' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description delete a pet @@ -14,10 +14,10 @@ import { client } from '../../.kubb/client' */ export function deletePet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/examples/fetch/src/gen/clients/pet/findPetsByStatus.ts b/examples/fetch/src/gen/clients/pet/findPetsByStatus.ts index 306835cef..78c47179c 100644 --- a/examples/fetch/src/gen/clients/pet/findPetsByStatus.ts +++ b/examples/fetch/src/gen/clients/pet/findPetsByStatus.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../../models/pet/FindPetsByStatus' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings @@ -14,14 +14,16 @@ import { client } from '../../.kubb/client' */ export function findPetsByStatus( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/findByStatus', - security: [{ type: 'oauth2' }], - styles: { query: { status: { explode: true } } }, - ...config, - }) as Promise> + return withUnwrap( + request({ + method: 'GET', + url: '/pet/findByStatus', + security: [{ type: 'oauth2' }], + styles: { query: { status: { explode: true } } }, + ...config, + }) as Promise>, + ) } diff --git a/examples/fetch/src/gen/clients/pet/findPetsByTags.ts b/examples/fetch/src/gen/clients/pet/findPetsByTags.ts index b7227971b..e4b9b5f55 100644 --- a/examples/fetch/src/gen/clients/pet/findPetsByTags.ts +++ b/examples/fetch/src/gen/clients/pet/findPetsByTags.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { FindPetsByTagsOptions, FindPetsByTagsResponses } from '../../models/pet/FindPetsByTags' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. @@ -14,14 +14,12 @@ import { client } from '../../.kubb/client' */ export function findPetsByTags( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/findByTags', - security: [{ type: 'oauth2' }], - styles: { query: { tags: { explode: true } } }, - ...config, - }) as Promise> + return withUnwrap( + request({ method: 'GET', url: '/pet/findByTags', security: [{ type: 'oauth2' }], styles: { query: { tags: { explode: true } } }, ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/fetch/src/gen/clients/pet/getPetById.ts b/examples/fetch/src/gen/clients/pet/getPetById.ts index 771e59ee1..81e309e82 100644 --- a/examples/fetch/src/gen/clients/pet/getPetById.ts +++ b/examples/fetch/src/gen/clients/pet/getPetById.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../../models/pet/GetPetById' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Returns a single pet @@ -14,13 +14,12 @@ import { client } from '../../.kubb/client' */ export function getPetById( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/{petId}', - security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], - ...config, - }) as Promise> + return withUnwrap( + request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/fetch/src/gen/clients/pet/updatePet.ts b/examples/fetch/src/gen/clients/pet/updatePet.ts index 7a287a9fe..f662fd79f 100644 --- a/examples/fetch/src/gen/clients/pet/updatePet.ts +++ b/examples/fetch/src/gen/clients/pet/updatePet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { UpdatePetOptions, UpdatePetResponses } from '../../models/pet/UpdatePet' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Update an existing pet by Id @@ -14,8 +14,10 @@ import { client } from '../../.kubb/client' */ export function updatePet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'PUT', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap( + request({ method: 'PUT', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/examples/fetch/src/gen/clients/pet/updatePetWithForm.ts b/examples/fetch/src/gen/clients/pet/updatePetWithForm.ts index f36371438..1db98bc49 100644 --- a/examples/fetch/src/gen/clients/pet/updatePetWithForm.ts +++ b/examples/fetch/src/gen/clients/pet/updatePetWithForm.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { UpdatePetWithFormOptions, UpdatePetWithFormResponses } from '../../models/pet/UpdatePetWithForm' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary Updates a pet in the store with form data @@ -13,10 +13,12 @@ import { client } from '../../.kubb/client' */ export function updatePetWithForm( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'POST', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/fetch/src/gen/clients/pet/uploadFile.ts b/examples/fetch/src/gen/clients/pet/uploadFile.ts index 65e79b47b..92ce7c699 100644 --- a/examples/fetch/src/gen/clients/pet/uploadFile.ts +++ b/examples/fetch/src/gen/clients/pet/uploadFile.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../../models/pet/UploadFile' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary uploads an image @@ -13,10 +13,12 @@ import { client } from '../../.kubb/client' */ export function uploadFile( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/fetch/src/gen/clients/store/deleteOrder.ts b/examples/fetch/src/gen/clients/store/deleteOrder.ts index 74eec3514..689cbaa9a 100644 --- a/examples/fetch/src/gen/clients/store/deleteOrder.ts +++ b/examples/fetch/src/gen/clients/store/deleteOrder.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { DeleteOrderOptions, DeleteOrderResponses } from '../../models/store/DeleteOrder' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function deleteOrder( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/store/order/{orderId}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/store/order/{orderId}', ...config }) as Promise>) } diff --git a/examples/fetch/src/gen/clients/store/getInventory.ts b/examples/fetch/src/gen/clients/store/getInventory.ts index f011e8dde..d44938f8e 100644 --- a/examples/fetch/src/gen/clients/store/getInventory.ts +++ b/examples/fetch/src/gen/clients/store/getInventory.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../../models/store/GetInventory' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Returns a map of status codes to quantities @@ -14,10 +14,12 @@ import { client } from '../../.kubb/client' */ export function getInventory( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/fetch/src/gen/clients/store/getOrderById.ts b/examples/fetch/src/gen/clients/store/getOrderById.ts index 91aa5bd78..a3ce96a72 100644 --- a/examples/fetch/src/gen/clients/store/getOrderById.ts +++ b/examples/fetch/src/gen/clients/store/getOrderById.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { GetOrderByIdOptions, GetOrderByIdResponses } from '../../models/store/GetOrderById' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions. @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function getOrderById( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/order/{orderId}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/order/{orderId}', ...config }) as Promise>) } diff --git a/examples/fetch/src/gen/clients/store/placeOrder.ts b/examples/fetch/src/gen/clients/store/placeOrder.ts index 8c1d56394..3a3da9192 100644 --- a/examples/fetch/src/gen/clients/store/placeOrder.ts +++ b/examples/fetch/src/gen/clients/store/placeOrder.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../../models/store/PlaceOrder' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Place a new order in the store @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function placeOrder( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/examples/fetch/src/gen/clients/store/placeOrderPatch.ts b/examples/fetch/src/gen/clients/store/placeOrderPatch.ts index 0772af4f7..692337b25 100644 --- a/examples/fetch/src/gen/clients/store/placeOrderPatch.ts +++ b/examples/fetch/src/gen/clients/store/placeOrderPatch.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { PlaceOrderPatchOptions, PlaceOrderPatchResponses } from '../../models/store/PlaceOrderPatch' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Place a new order in the store with patch @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function placeOrderPatch( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'PATCH', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'PATCH', url: '/store/order', ...config }) as Promise>) } diff --git a/examples/fetch/src/gen/clients/user/createUser.ts b/examples/fetch/src/gen/clients/user/createUser.ts index af5bbc3e7..d9c8c13bc 100644 --- a/examples/fetch/src/gen/clients/user/createUser.ts +++ b/examples/fetch/src/gen/clients/user/createUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { CreateUserOptions, CreateUserResponses } from '../../models/user/CreateUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description This can only be done by the logged in user. @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function createUser( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/user', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/user', ...config }) as Promise>) } diff --git a/examples/fetch/src/gen/clients/user/createUsersWithListInput.ts b/examples/fetch/src/gen/clients/user/createUsersWithListInput.ts index b69d5d908..1e6a9e2ac 100644 --- a/examples/fetch/src/gen/clients/user/createUsersWithListInput.ts +++ b/examples/fetch/src/gen/clients/user/createUsersWithListInput.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { CreateUsersWithListInputOptions, CreateUsersWithListInputResponses } from '../../models/user/CreateUsersWithListInput' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Creates list of users with given input array @@ -14,8 +14,10 @@ import { client } from '../../.kubb/client' */ export function createUsersWithListInput( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/user/createWithList', ...config }) as Promise> + return withUnwrap( + request({ method: 'POST', url: '/user/createWithList', ...config }) as Promise>, + ) } diff --git a/examples/fetch/src/gen/clients/user/deleteUser.ts b/examples/fetch/src/gen/clients/user/deleteUser.ts index dff00a13e..8dda91163 100644 --- a/examples/fetch/src/gen/clients/user/deleteUser.ts +++ b/examples/fetch/src/gen/clients/user/deleteUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { DeleteUserOptions, DeleteUserResponses } from '../../models/user/DeleteUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description This can only be done by the logged in user. @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function deleteUser( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/user/{username}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/user/{username}', ...config }) as Promise>) } diff --git a/examples/fetch/src/gen/clients/user/getUserByName.ts b/examples/fetch/src/gen/clients/user/getUserByName.ts index 4db3b9d4d..2e0afe704 100644 --- a/examples/fetch/src/gen/clients/user/getUserByName.ts +++ b/examples/fetch/src/gen/clients/user/getUserByName.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { GetUserByNameOptions, GetUserByNameResponses } from '../../models/user/GetUserByName' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary Get user by user name @@ -13,8 +13,8 @@ import { client } from '../../.kubb/client' */ export function getUserByName( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/user/{username}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/user/{username}', ...config }) as Promise>) } diff --git a/examples/fetch/src/gen/clients/user/loginUser.ts b/examples/fetch/src/gen/clients/user/loginUser.ts index b619b4b9b..4a46c2208 100644 --- a/examples/fetch/src/gen/clients/user/loginUser.ts +++ b/examples/fetch/src/gen/clients/user/loginUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { LoginUserOptions, LoginUserResponses } from '../../models/user/LoginUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary Logs user into the system @@ -13,8 +13,8 @@ import { client } from '../../.kubb/client' */ export function loginUser( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/user/login', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/user/login', ...config }) as Promise>) } diff --git a/examples/fetch/src/gen/clients/user/logoutUser.ts b/examples/fetch/src/gen/clients/user/logoutUser.ts index e41276de9..a5c939fbb 100644 --- a/examples/fetch/src/gen/clients/user/logoutUser.ts +++ b/examples/fetch/src/gen/clients/user/logoutUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { LogoutUserOptions, LogoutUserResponses } from '../../models/user/LogoutUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary Logs out current logged in user session @@ -13,8 +13,8 @@ import { client } from '../../.kubb/client' */ export function logoutUser( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/user/logout', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/user/logout', ...config }) as Promise>) } diff --git a/examples/fetch/src/gen/clients/user/updateUser.ts b/examples/fetch/src/gen/clients/user/updateUser.ts index ce0faef1f..b6c6e7de1 100644 --- a/examples/fetch/src/gen/clients/user/updateUser.ts +++ b/examples/fetch/src/gen/clients/user/updateUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { UpdateUserOptions, UpdateUserResponses } from '../../models/user/UpdateUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description This can only be done by the logged in user. @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function updateUser( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'PUT', url: '/user/{username}', ...config }) as Promise> + return withUnwrap(request({ method: 'PUT', url: '/user/{username}', ...config }) as Promise>) } diff --git a/examples/mcp/src/gen/.kubb/client.ts b/examples/mcp/src/gen/.kubb/client.ts index ff65e0b9d..fba2f3f25 100644 --- a/examples/mcp/src/gen/.kubb/client.ts +++ b/examples/mcp/src/gen/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/examples/mcp/src/gen/clients/addFiles.ts b/examples/mcp/src/gen/clients/addFiles.ts index 9574992c0..011ed283c 100644 --- a/examples/mcp/src/gen/clients/addFiles.ts +++ b/examples/mcp/src/gen/clients/addFiles.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddFilesOptions, AddFilesResponses } from '../models/ts/AddFiles' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new file in the store @@ -14,8 +14,8 @@ import { client } from '../.kubb/client' */ export function addFiles( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/files', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/files', ...config }) as Promise>) } diff --git a/examples/mcp/src/gen/clients/addPet.ts b/examples/mcp/src/gen/clients/addPet.ts index 844da0911..9a0496cf5 100644 --- a/examples/mcp/src/gen/clients/addPet.ts +++ b/examples/mcp/src/gen/clients/addPet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../models/ts/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store @@ -14,8 +14,10 @@ import { client } from '../.kubb/client' */ export function addPet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap( + request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/examples/mcp/src/gen/clients/createPets.ts b/examples/mcp/src/gen/clients/createPets.ts index b42432659..40884cc56 100644 --- a/examples/mcp/src/gen/clients/createPets.ts +++ b/examples/mcp/src/gen/clients/createPets.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { CreatePetsOptions, CreatePetsResponses } from '../models/ts/CreatePets' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary Create a pet @@ -13,8 +13,8 @@ import { client } from '../.kubb/client' */ export function createPets( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pets/{uuid}', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pets/{uuid}', ...config }) as Promise>) } diff --git a/examples/mcp/src/gen/clients/deleteOrder.ts b/examples/mcp/src/gen/clients/deleteOrder.ts index 2abd6ff35..db9b7c999 100644 --- a/examples/mcp/src/gen/clients/deleteOrder.ts +++ b/examples/mcp/src/gen/clients/deleteOrder.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeleteOrderOptions, DeleteOrderResponses } from '../models/ts/DeleteOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors @@ -14,8 +14,8 @@ import { client } from '../.kubb/client' */ export function deleteOrder( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/store/order/{orderId}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/store/order/{orderId}', ...config }) as Promise>) } diff --git a/examples/mcp/src/gen/clients/deletePet.ts b/examples/mcp/src/gen/clients/deletePet.ts index 08c7066d6..c16f63230 100644 --- a/examples/mcp/src/gen/clients/deletePet.ts +++ b/examples/mcp/src/gen/clients/deletePet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../models/ts/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet @@ -14,10 +14,10 @@ import { client } from '../.kubb/client' */ export function deletePet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/examples/mcp/src/gen/clients/findPetsByStatus.ts b/examples/mcp/src/gen/clients/findPetsByStatus.ts index d52b61fc5..03a2e6cb7 100644 --- a/examples/mcp/src/gen/clients/findPetsByStatus.ts +++ b/examples/mcp/src/gen/clients/findPetsByStatus.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../models/ts/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings @@ -14,10 +14,12 @@ import { client } from '../.kubb/client' */ export function findPetsByStatus( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus/{step_id}', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'GET', url: '/pet/findByStatus/{step_id}', security: [{ type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/mcp/src/gen/clients/findPetsByTags.ts b/examples/mcp/src/gen/clients/findPetsByTags.ts index 3c9dce11b..ff34b628b 100644 --- a/examples/mcp/src/gen/clients/findPetsByTags.ts +++ b/examples/mcp/src/gen/clients/findPetsByTags.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByTagsOptions, FindPetsByTagsResponses } from '../models/ts/FindPetsByTags' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. @@ -14,14 +14,12 @@ import { client } from '../.kubb/client' */ export function findPetsByTags( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/findByTags', - security: [{ type: 'oauth2' }], - styles: { query: { tags: { explode: true } } }, - ...config, - }) as Promise> + return withUnwrap( + request({ method: 'GET', url: '/pet/findByTags', security: [{ type: 'oauth2' }], styles: { query: { tags: { explode: true } } }, ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/mcp/src/gen/clients/getInventory.ts b/examples/mcp/src/gen/clients/getInventory.ts index 59db43bc6..15b384ed9 100644 --- a/examples/mcp/src/gen/clients/getInventory.ts +++ b/examples/mcp/src/gen/clients/getInventory.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../models/ts/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities @@ -14,10 +14,12 @@ import { client } from '../.kubb/client' */ export function getInventory( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/mcp/src/gen/clients/getOrderById.ts b/examples/mcp/src/gen/clients/getOrderById.ts index 514a59bed..0a9dac45a 100644 --- a/examples/mcp/src/gen/clients/getOrderById.ts +++ b/examples/mcp/src/gen/clients/getOrderById.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetOrderByIdOptions, GetOrderByIdResponses } from '../models/ts/GetOrderById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions. @@ -14,8 +14,8 @@ import { client } from '../.kubb/client' */ export function getOrderById( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/order/{orderId}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/order/{orderId}', ...config }) as Promise>) } diff --git a/examples/mcp/src/gen/clients/getPetById.ts b/examples/mcp/src/gen/clients/getPetById.ts index d05d6b0c7..18e629de1 100644 --- a/examples/mcp/src/gen/clients/getPetById.ts +++ b/examples/mcp/src/gen/clients/getPetById.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../models/ts/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet @@ -14,13 +14,12 @@ import { client } from '../.kubb/client' */ export function getPetById( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/{petId}', - security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], - ...config, - }) as Promise> + return withUnwrap( + request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/mcp/src/gen/clients/placeOrder.ts b/examples/mcp/src/gen/clients/placeOrder.ts index 3e40bda53..7879b1b42 100644 --- a/examples/mcp/src/gen/clients/placeOrder.ts +++ b/examples/mcp/src/gen/clients/placeOrder.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../models/ts/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store @@ -14,8 +14,8 @@ import { client } from '../.kubb/client' */ export function placeOrder( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/examples/mcp/src/gen/clients/placeOrderPatch.ts b/examples/mcp/src/gen/clients/placeOrderPatch.ts index 8a4df3573..16b68f959 100644 --- a/examples/mcp/src/gen/clients/placeOrderPatch.ts +++ b/examples/mcp/src/gen/clients/placeOrderPatch.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderPatchOptions, PlaceOrderPatchResponses } from '../models/ts/PlaceOrderPatch' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store with patch @@ -14,8 +14,8 @@ import { client } from '../.kubb/client' */ export function placeOrderPatch( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'PATCH', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'PATCH', url: '/store/order', ...config }) as Promise>) } diff --git a/examples/mcp/src/gen/clients/updatePet.ts b/examples/mcp/src/gen/clients/updatePet.ts index 6020c27bd..9a347a403 100644 --- a/examples/mcp/src/gen/clients/updatePet.ts +++ b/examples/mcp/src/gen/clients/updatePet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UpdatePetOptions, UpdatePetResponses } from '../models/ts/UpdatePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Update an existing pet by Id @@ -14,8 +14,10 @@ import { client } from '../.kubb/client' */ export function updatePet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'PUT', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap( + request({ method: 'PUT', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/examples/mcp/src/gen/clients/updatePetWithForm.ts b/examples/mcp/src/gen/clients/updatePetWithForm.ts index 5e20e3cb6..cde83a933 100644 --- a/examples/mcp/src/gen/clients/updatePetWithForm.ts +++ b/examples/mcp/src/gen/clients/updatePetWithForm.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UpdatePetWithFormOptions, UpdatePetWithFormResponses } from '../models/ts/UpdatePetWithForm' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary Updates a pet in the store with form data @@ -13,10 +13,12 @@ import { client } from '../.kubb/client' */ export function updatePetWithForm( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'POST', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/react-query/src/gen/.kubb/client.ts b/examples/react-query/src/gen/.kubb/client.ts index f80182aca..ea6223eff 100644 --- a/examples/react-query/src/gen/.kubb/client.ts +++ b/examples/react-query/src/gen/.kubb/client.ts @@ -82,6 +82,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/examples/react-query/src/gen/clients/pet/addPet.ts b/examples/react-query/src/gen/clients/pet/addPet.ts index 952e15db2..3628776a0 100644 --- a/examples/react-query/src/gen/clients/pet/addPet.ts +++ b/examples/react-query/src/gen/clients/pet/addPet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../../models/pet/AddPet' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Add a new pet to the store @@ -14,8 +14,10 @@ import { client } from '../../.kubb/client' */ export function addPet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap( + request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/examples/react-query/src/gen/clients/pet/deletePet.ts b/examples/react-query/src/gen/clients/pet/deletePet.ts index 24725dc26..8fe616c13 100644 --- a/examples/react-query/src/gen/clients/pet/deletePet.ts +++ b/examples/react-query/src/gen/clients/pet/deletePet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../../models/pet/DeletePet' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description delete a pet @@ -14,10 +14,10 @@ import { client } from '../../.kubb/client' */ export function deletePet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/examples/react-query/src/gen/clients/pet/findPetsByStatus.ts b/examples/react-query/src/gen/clients/pet/findPetsByStatus.ts index 306835cef..78c47179c 100644 --- a/examples/react-query/src/gen/clients/pet/findPetsByStatus.ts +++ b/examples/react-query/src/gen/clients/pet/findPetsByStatus.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../../models/pet/FindPetsByStatus' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings @@ -14,14 +14,16 @@ import { client } from '../../.kubb/client' */ export function findPetsByStatus( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/findByStatus', - security: [{ type: 'oauth2' }], - styles: { query: { status: { explode: true } } }, - ...config, - }) as Promise> + return withUnwrap( + request({ + method: 'GET', + url: '/pet/findByStatus', + security: [{ type: 'oauth2' }], + styles: { query: { status: { explode: true } } }, + ...config, + }) as Promise>, + ) } diff --git a/examples/react-query/src/gen/clients/pet/findPetsByTags.ts b/examples/react-query/src/gen/clients/pet/findPetsByTags.ts index b7227971b..e4b9b5f55 100644 --- a/examples/react-query/src/gen/clients/pet/findPetsByTags.ts +++ b/examples/react-query/src/gen/clients/pet/findPetsByTags.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { FindPetsByTagsOptions, FindPetsByTagsResponses } from '../../models/pet/FindPetsByTags' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. @@ -14,14 +14,12 @@ import { client } from '../../.kubb/client' */ export function findPetsByTags( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/findByTags', - security: [{ type: 'oauth2' }], - styles: { query: { tags: { explode: true } } }, - ...config, - }) as Promise> + return withUnwrap( + request({ method: 'GET', url: '/pet/findByTags', security: [{ type: 'oauth2' }], styles: { query: { tags: { explode: true } } }, ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/react-query/src/gen/clients/pet/getPetById.ts b/examples/react-query/src/gen/clients/pet/getPetById.ts index 771e59ee1..81e309e82 100644 --- a/examples/react-query/src/gen/clients/pet/getPetById.ts +++ b/examples/react-query/src/gen/clients/pet/getPetById.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../../models/pet/GetPetById' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Returns a single pet @@ -14,13 +14,12 @@ import { client } from '../../.kubb/client' */ export function getPetById( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/{petId}', - security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], - ...config, - }) as Promise> + return withUnwrap( + request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/react-query/src/gen/clients/pet/updatePet.ts b/examples/react-query/src/gen/clients/pet/updatePet.ts index 7a287a9fe..f662fd79f 100644 --- a/examples/react-query/src/gen/clients/pet/updatePet.ts +++ b/examples/react-query/src/gen/clients/pet/updatePet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { UpdatePetOptions, UpdatePetResponses } from '../../models/pet/UpdatePet' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Update an existing pet by Id @@ -14,8 +14,10 @@ import { client } from '../../.kubb/client' */ export function updatePet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'PUT', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap( + request({ method: 'PUT', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/examples/react-query/src/gen/clients/pet/updatePetWithForm.ts b/examples/react-query/src/gen/clients/pet/updatePetWithForm.ts index f36371438..1db98bc49 100644 --- a/examples/react-query/src/gen/clients/pet/updatePetWithForm.ts +++ b/examples/react-query/src/gen/clients/pet/updatePetWithForm.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { UpdatePetWithFormOptions, UpdatePetWithFormResponses } from '../../models/pet/UpdatePetWithForm' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary Updates a pet in the store with form data @@ -13,10 +13,12 @@ import { client } from '../../.kubb/client' */ export function updatePetWithForm( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'POST', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/react-query/src/gen/clients/pet/uploadFile.ts b/examples/react-query/src/gen/clients/pet/uploadFile.ts index 65e79b47b..92ce7c699 100644 --- a/examples/react-query/src/gen/clients/pet/uploadFile.ts +++ b/examples/react-query/src/gen/clients/pet/uploadFile.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../../models/pet/UploadFile' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary uploads an image @@ -13,10 +13,12 @@ import { client } from '../../.kubb/client' */ export function uploadFile( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/react-query/src/gen/clients/store/deleteOrder.ts b/examples/react-query/src/gen/clients/store/deleteOrder.ts index 74eec3514..689cbaa9a 100644 --- a/examples/react-query/src/gen/clients/store/deleteOrder.ts +++ b/examples/react-query/src/gen/clients/store/deleteOrder.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { DeleteOrderOptions, DeleteOrderResponses } from '../../models/store/DeleteOrder' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function deleteOrder( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/store/order/{orderId}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/store/order/{orderId}', ...config }) as Promise>) } diff --git a/examples/react-query/src/gen/clients/store/getInventory.ts b/examples/react-query/src/gen/clients/store/getInventory.ts index f011e8dde..d44938f8e 100644 --- a/examples/react-query/src/gen/clients/store/getInventory.ts +++ b/examples/react-query/src/gen/clients/store/getInventory.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../../models/store/GetInventory' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Returns a map of status codes to quantities @@ -14,10 +14,12 @@ import { client } from '../../.kubb/client' */ export function getInventory( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/react-query/src/gen/clients/store/getOrderById.ts b/examples/react-query/src/gen/clients/store/getOrderById.ts index 91aa5bd78..a3ce96a72 100644 --- a/examples/react-query/src/gen/clients/store/getOrderById.ts +++ b/examples/react-query/src/gen/clients/store/getOrderById.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { GetOrderByIdOptions, GetOrderByIdResponses } from '../../models/store/GetOrderById' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions. @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function getOrderById( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/order/{orderId}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/order/{orderId}', ...config }) as Promise>) } diff --git a/examples/react-query/src/gen/clients/store/placeOrder.ts b/examples/react-query/src/gen/clients/store/placeOrder.ts index 8c1d56394..3a3da9192 100644 --- a/examples/react-query/src/gen/clients/store/placeOrder.ts +++ b/examples/react-query/src/gen/clients/store/placeOrder.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../../models/store/PlaceOrder' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Place a new order in the store @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function placeOrder( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/examples/react-query/src/gen/clients/store/placeOrderPatch.ts b/examples/react-query/src/gen/clients/store/placeOrderPatch.ts index 0772af4f7..692337b25 100644 --- a/examples/react-query/src/gen/clients/store/placeOrderPatch.ts +++ b/examples/react-query/src/gen/clients/store/placeOrderPatch.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { PlaceOrderPatchOptions, PlaceOrderPatchResponses } from '../../models/store/PlaceOrderPatch' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Place a new order in the store with patch @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function placeOrderPatch( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'PATCH', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'PATCH', url: '/store/order', ...config }) as Promise>) } diff --git a/examples/react-query/src/gen/clients/user/createUser.ts b/examples/react-query/src/gen/clients/user/createUser.ts index af5bbc3e7..d9c8c13bc 100644 --- a/examples/react-query/src/gen/clients/user/createUser.ts +++ b/examples/react-query/src/gen/clients/user/createUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { CreateUserOptions, CreateUserResponses } from '../../models/user/CreateUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description This can only be done by the logged in user. @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function createUser( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/user', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/user', ...config }) as Promise>) } diff --git a/examples/react-query/src/gen/clients/user/createUsersWithListInput.ts b/examples/react-query/src/gen/clients/user/createUsersWithListInput.ts index b69d5d908..1e6a9e2ac 100644 --- a/examples/react-query/src/gen/clients/user/createUsersWithListInput.ts +++ b/examples/react-query/src/gen/clients/user/createUsersWithListInput.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { CreateUsersWithListInputOptions, CreateUsersWithListInputResponses } from '../../models/user/CreateUsersWithListInput' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Creates list of users with given input array @@ -14,8 +14,10 @@ import { client } from '../../.kubb/client' */ export function createUsersWithListInput( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/user/createWithList', ...config }) as Promise> + return withUnwrap( + request({ method: 'POST', url: '/user/createWithList', ...config }) as Promise>, + ) } diff --git a/examples/react-query/src/gen/clients/user/deleteUser.ts b/examples/react-query/src/gen/clients/user/deleteUser.ts index dff00a13e..8dda91163 100644 --- a/examples/react-query/src/gen/clients/user/deleteUser.ts +++ b/examples/react-query/src/gen/clients/user/deleteUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { DeleteUserOptions, DeleteUserResponses } from '../../models/user/DeleteUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description This can only be done by the logged in user. @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function deleteUser( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/user/{username}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/user/{username}', ...config }) as Promise>) } diff --git a/examples/react-query/src/gen/clients/user/getUserByName.ts b/examples/react-query/src/gen/clients/user/getUserByName.ts index 4db3b9d4d..2e0afe704 100644 --- a/examples/react-query/src/gen/clients/user/getUserByName.ts +++ b/examples/react-query/src/gen/clients/user/getUserByName.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { GetUserByNameOptions, GetUserByNameResponses } from '../../models/user/GetUserByName' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary Get user by user name @@ -13,8 +13,8 @@ import { client } from '../../.kubb/client' */ export function getUserByName( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/user/{username}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/user/{username}', ...config }) as Promise>) } diff --git a/examples/react-query/src/gen/clients/user/loginUser.ts b/examples/react-query/src/gen/clients/user/loginUser.ts index b619b4b9b..4a46c2208 100644 --- a/examples/react-query/src/gen/clients/user/loginUser.ts +++ b/examples/react-query/src/gen/clients/user/loginUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { LoginUserOptions, LoginUserResponses } from '../../models/user/LoginUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary Logs user into the system @@ -13,8 +13,8 @@ import { client } from '../../.kubb/client' */ export function loginUser( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/user/login', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/user/login', ...config }) as Promise>) } diff --git a/examples/react-query/src/gen/clients/user/logoutUser.ts b/examples/react-query/src/gen/clients/user/logoutUser.ts index e41276de9..a5c939fbb 100644 --- a/examples/react-query/src/gen/clients/user/logoutUser.ts +++ b/examples/react-query/src/gen/clients/user/logoutUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { LogoutUserOptions, LogoutUserResponses } from '../../models/user/LogoutUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary Logs out current logged in user session @@ -13,8 +13,8 @@ import { client } from '../../.kubb/client' */ export function logoutUser( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/user/logout', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/user/logout', ...config }) as Promise>) } diff --git a/examples/react-query/src/gen/clients/user/updateUser.ts b/examples/react-query/src/gen/clients/user/updateUser.ts index ce0faef1f..b6c6e7de1 100644 --- a/examples/react-query/src/gen/clients/user/updateUser.ts +++ b/examples/react-query/src/gen/clients/user/updateUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { UpdateUserOptions, UpdateUserResponses } from '../../models/user/UpdateUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description This can only be done by the logged in user. @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function updateUser( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'PUT', url: '/user/{username}', ...config }) as Promise> + return withUnwrap(request({ method: 'PUT', url: '/user/{username}', ...config }) as Promise>) } diff --git a/examples/react-query/src/gen/hooks/pet/useAddPet.ts b/examples/react-query/src/gen/hooks/pet/useAddPet.ts index af4402c13..a2b816526 100644 --- a/examples/react-query/src/gen/hooks/pet/useAddPet.ts +++ b/examples/react-query/src/gen/hooks/pet/useAddPet.ts @@ -19,8 +19,7 @@ export function addPetMutationOptions( return mutationOptions, AddPetOptions, TContext>({ mutationKey, mutationFn: async ({ body }) => { - const { data } = await addPet({ ...config, body, throwOnError: true }) - return data + return addPet({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/examples/react-query/src/gen/hooks/pet/useDeletePet.ts b/examples/react-query/src/gen/hooks/pet/useDeletePet.ts index 5db82fc0c..02cbf2896 100644 --- a/examples/react-query/src/gen/hooks/pet/useDeletePet.ts +++ b/examples/react-query/src/gen/hooks/pet/useDeletePet.ts @@ -15,8 +15,7 @@ export function deletePetMutationOptions(config: Partial, DeletePetOptions, TContext>({ mutationKey, mutationFn: async ({ path, headers }) => { - const { data } = await deletePet({ ...config, path, headers, throwOnError: true }) - return data + return deletePet({ ...config, path, headers, throwOnError: true }).unwrap() }, }) } diff --git a/examples/react-query/src/gen/hooks/pet/useFindPetsByStatus.ts b/examples/react-query/src/gen/hooks/pet/useFindPetsByStatus.ts index 629d08f9b..eced35d23 100644 --- a/examples/react-query/src/gen/hooks/pet/useFindPetsByStatus.ts +++ b/examples/react-query/src/gen/hooks/pet/useFindPetsByStatus.ts @@ -21,8 +21,7 @@ export function findPetsByStatusQueryOptions( return queryOptions, FindPetsByStatusStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/examples/react-query/src/gen/hooks/pet/useFindPetsByTags.ts b/examples/react-query/src/gen/hooks/pet/useFindPetsByTags.ts index a366e1dd7..9c673f0f2 100644 --- a/examples/react-query/src/gen/hooks/pet/useFindPetsByTags.ts +++ b/examples/react-query/src/gen/hooks/pet/useFindPetsByTags.ts @@ -21,8 +21,7 @@ export function findPetsByTagsQueryOptions( return queryOptions, FindPetsByTagsStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/examples/react-query/src/gen/hooks/pet/useGetPetById.ts b/examples/react-query/src/gen/hooks/pet/useGetPetById.ts index 004c4425a..4222a610a 100644 --- a/examples/react-query/src/gen/hooks/pet/useGetPetById.ts +++ b/examples/react-query/src/gen/hooks/pet/useGetPetById.ts @@ -17,8 +17,7 @@ export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Part return queryOptions, GetPetByIdStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/examples/react-query/src/gen/hooks/pet/useUpdatePet.ts b/examples/react-query/src/gen/hooks/pet/useUpdatePet.ts index 3267d0aa8..447099311 100644 --- a/examples/react-query/src/gen/hooks/pet/useUpdatePet.ts +++ b/examples/react-query/src/gen/hooks/pet/useUpdatePet.ts @@ -19,8 +19,7 @@ export function updatePetMutationOptions( return mutationOptions, UpdatePetOptions, TContext>({ mutationKey, mutationFn: async ({ body }) => { - const { data } = await updatePet({ ...config, body, throwOnError: true }) - return data + return updatePet({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/examples/react-query/src/gen/hooks/pet/useUpdatePetWithForm.ts b/examples/react-query/src/gen/hooks/pet/useUpdatePetWithForm.ts index f81714f05..20544e301 100644 --- a/examples/react-query/src/gen/hooks/pet/useUpdatePetWithForm.ts +++ b/examples/react-query/src/gen/hooks/pet/useUpdatePetWithForm.ts @@ -15,8 +15,7 @@ export function updatePetWithFormMutationOptions(config: Par return mutationOptions, UpdatePetWithFormOptions, TContext>({ mutationKey, mutationFn: async ({ path, query }) => { - const { data } = await updatePetWithForm({ ...config, path, query, throwOnError: true }) - return data + return updatePetWithForm({ ...config, path, query, throwOnError: true }).unwrap() }, }) } diff --git a/examples/react-query/src/gen/hooks/pet/useUploadFile.ts b/examples/react-query/src/gen/hooks/pet/useUploadFile.ts index 60336c24d..5e3954d6d 100644 --- a/examples/react-query/src/gen/hooks/pet/useUploadFile.ts +++ b/examples/react-query/src/gen/hooks/pet/useUploadFile.ts @@ -19,8 +19,7 @@ export function uploadFileMutationOptions( return mutationOptions, UploadFileOptions, TContext>({ mutationKey, mutationFn: async ({ path, query, body }) => { - const { data } = await uploadFile({ ...config, path, query, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, query, body, throwOnError: true }).unwrap() }, }) } diff --git a/examples/react-query/src/gen/hooks/store/useDeleteOrder.ts b/examples/react-query/src/gen/hooks/store/useDeleteOrder.ts index 4904a0fd4..7b58916d5 100644 --- a/examples/react-query/src/gen/hooks/store/useDeleteOrder.ts +++ b/examples/react-query/src/gen/hooks/store/useDeleteOrder.ts @@ -15,8 +15,7 @@ export function deleteOrderMutationOptions(config: Partial, DeleteOrderOptions, TContext>({ mutationKey, mutationFn: async ({ path }) => { - const { data } = await deleteOrder({ ...config, path, throwOnError: true }) - return data + return deleteOrder({ ...config, path, throwOnError: true }).unwrap() }, }) } diff --git a/examples/react-query/src/gen/hooks/store/useGetInventory.ts b/examples/react-query/src/gen/hooks/store/useGetInventory.ts index 88bd01d8b..83f2662d7 100644 --- a/examples/react-query/src/gen/hooks/store/useGetInventory.ts +++ b/examples/react-query/src/gen/hooks/store/useGetInventory.ts @@ -17,8 +17,7 @@ export function getInventoryQueryOptions(config: Partial, GetInventoryStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/examples/react-query/src/gen/hooks/store/useGetOrderById.ts b/examples/react-query/src/gen/hooks/store/useGetOrderById.ts index a072a17b0..edf5a1b0e 100644 --- a/examples/react-query/src/gen/hooks/store/useGetOrderById.ts +++ b/examples/react-query/src/gen/hooks/store/useGetOrderById.ts @@ -20,8 +20,7 @@ export function getOrderByIdQueryOptions( return queryOptions, GetOrderByIdStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getOrderById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }) - return data + return getOrderById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/examples/react-query/src/gen/hooks/store/usePlaceOrder.ts b/examples/react-query/src/gen/hooks/store/usePlaceOrder.ts index 492735b23..cb932e02a 100644 --- a/examples/react-query/src/gen/hooks/store/usePlaceOrder.ts +++ b/examples/react-query/src/gen/hooks/store/usePlaceOrder.ts @@ -19,8 +19,7 @@ export function placeOrderMutationOptions( return mutationOptions, PlaceOrderOptions, TContext>({ mutationKey, mutationFn: async ({ body }) => { - const { data } = await placeOrder({ ...config, body, throwOnError: true }) - return data + return placeOrder({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/examples/react-query/src/gen/hooks/store/usePlaceOrderPatch.ts b/examples/react-query/src/gen/hooks/store/usePlaceOrderPatch.ts index 89c5537d4..2233745a9 100644 --- a/examples/react-query/src/gen/hooks/store/usePlaceOrderPatch.ts +++ b/examples/react-query/src/gen/hooks/store/usePlaceOrderPatch.ts @@ -19,8 +19,7 @@ export function placeOrderPatchMutationOptions( return mutationOptions, PlaceOrderPatchOptions, TContext>({ mutationKey, mutationFn: async ({ body }) => { - const { data } = await placeOrderPatch({ ...config, body, throwOnError: true }) - return data + return placeOrderPatch({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/examples/react-query/src/gen/hooks/user/useCreateUser.ts b/examples/react-query/src/gen/hooks/user/useCreateUser.ts index 285e155b6..890f54c41 100644 --- a/examples/react-query/src/gen/hooks/user/useCreateUser.ts +++ b/examples/react-query/src/gen/hooks/user/useCreateUser.ts @@ -19,8 +19,7 @@ export function createUserMutationOptions( return mutationOptions, CreateUserOptions, TContext>({ mutationKey, mutationFn: async ({ body }) => { - const { data } = await createUser({ ...config, body, throwOnError: true }) - return data + return createUser({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/examples/react-query/src/gen/hooks/user/useCreateUsersWithListInput.ts b/examples/react-query/src/gen/hooks/user/useCreateUsersWithListInput.ts index 658ea650c..810081dd9 100644 --- a/examples/react-query/src/gen/hooks/user/useCreateUsersWithListInput.ts +++ b/examples/react-query/src/gen/hooks/user/useCreateUsersWithListInput.ts @@ -19,8 +19,7 @@ export function createUsersWithListInputMutationOptions( return mutationOptions, CreateUsersWithListInputOptions, TContext>({ mutationKey, mutationFn: async ({ body }) => { - const { data } = await createUsersWithListInput({ ...config, body, throwOnError: true }) - return data + return createUsersWithListInput({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/examples/react-query/src/gen/hooks/user/useDeleteUser.ts b/examples/react-query/src/gen/hooks/user/useDeleteUser.ts index 5f4adbb3e..b62d5f4fb 100644 --- a/examples/react-query/src/gen/hooks/user/useDeleteUser.ts +++ b/examples/react-query/src/gen/hooks/user/useDeleteUser.ts @@ -15,8 +15,7 @@ export function deleteUserMutationOptions(config: Partial, DeleteUserOptions, TContext>({ mutationKey, mutationFn: async ({ path }) => { - const { data } = await deleteUser({ ...config, path, throwOnError: true }) - return data + return deleteUser({ ...config, path, throwOnError: true }).unwrap() }, }) } diff --git a/examples/react-query/src/gen/hooks/user/useGetUserByName.ts b/examples/react-query/src/gen/hooks/user/useGetUserByName.ts index 5fe7e2519..34cb4e8c7 100644 --- a/examples/react-query/src/gen/hooks/user/useGetUserByName.ts +++ b/examples/react-query/src/gen/hooks/user/useGetUserByName.ts @@ -20,8 +20,7 @@ export function getUserByNameQueryOptions( return queryOptions, GetUserByNameStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getUserByName({ ...config, path, signal: config.signal ?? signal, throwOnError: true }) - return data + return getUserByName({ ...config, path, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/examples/react-query/src/gen/hooks/user/useLoginUser.ts b/examples/react-query/src/gen/hooks/user/useLoginUser.ts index 3f768f767..58a158b8a 100644 --- a/examples/react-query/src/gen/hooks/user/useLoginUser.ts +++ b/examples/react-query/src/gen/hooks/user/useLoginUser.ts @@ -20,8 +20,7 @@ export function loginUserQueryOptions( return queryOptions, LoginUserStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await loginUser({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return loginUser({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/examples/react-query/src/gen/hooks/user/useLogoutUser.ts b/examples/react-query/src/gen/hooks/user/useLogoutUser.ts index 7e1864082..82025e315 100644 --- a/examples/react-query/src/gen/hooks/user/useLogoutUser.ts +++ b/examples/react-query/src/gen/hooks/user/useLogoutUser.ts @@ -17,8 +17,7 @@ export function logoutUserQueryOptions(config: Partial, LogoutUserResponse, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await logoutUser({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return logoutUser({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/examples/react-query/src/gen/hooks/user/useUpdateUser.ts b/examples/react-query/src/gen/hooks/user/useUpdateUser.ts index 10f3952c4..846de211b 100644 --- a/examples/react-query/src/gen/hooks/user/useUpdateUser.ts +++ b/examples/react-query/src/gen/hooks/user/useUpdateUser.ts @@ -19,8 +19,7 @@ export function updateUserMutationOptions( return mutationOptions, UpdateUserOptions, TContext>({ mutationKey, mutationFn: async ({ path, body }) => { - const { data } = await updateUser({ ...config, path, body, throwOnError: true }) - return data + return updateUser({ ...config, path, body, throwOnError: true }).unwrap() }, }) } diff --git a/examples/sdk/src/gen/.kubb/client.ts b/examples/sdk/src/gen/.kubb/client.ts index f80182aca..ea6223eff 100644 --- a/examples/sdk/src/gen/.kubb/client.ts +++ b/examples/sdk/src/gen/.kubb/client.ts @@ -82,6 +82,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/examples/sdk/src/gen/sdk/pet/pet.ts b/examples/sdk/src/gen/sdk/pet/pet.ts index e3ef0e684..b41c07e8a 100644 --- a/examples/sdk/src/gen/sdk/pet/pet.ts +++ b/examples/sdk/src/gen/sdk/pet/pet.ts @@ -3,7 +3,7 @@ * Do not edit manually. */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from '../../.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../../models/pet/AddPet' import type { DeletePetOptions, DeletePetResponses } from '../../models/pet/DeletePet' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../../models/pet/FindPetsByStatus' @@ -12,7 +12,7 @@ import type { GetPetByIdOptions, GetPetByIdResponses } from '../../models/pet/Ge import type { UpdatePetOptions, UpdatePetResponses } from '../../models/pet/UpdatePet' import type { UpdatePetWithFormOptions, UpdatePetWithFormResponses } from '../../models/pet/UpdatePetWithForm' import type { UploadFileOptions, UploadFileResponses } from '../../models/pet/UploadFile' -import { createClient } from '../../.kubb/client' +import { createClient, withUnwrap } from '../../.kubb/client' export class pet { private readonly client: ClientInstance @@ -28,10 +28,12 @@ export class pet { */ public updatePet( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'PUT', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap( + request({ method: 'PUT', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } /** @@ -39,10 +41,12 @@ export class pet { * @summary Add a new pet to the store * {@link /pet} */ - public addPet(options: Options): Promise> { + public addPet(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap( + request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } /** @@ -52,12 +56,14 @@ export class pet { */ public findPetsByStatus( options: Options = {}, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } /** @@ -67,12 +73,14 @@ export class pet { */ public findPetsByTags( options: Options = {}, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/findByTags', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'GET', url: '/pet/findByTags', security: [{ type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } /** @@ -82,15 +90,14 @@ export class pet { */ public getPetById( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ - method: 'GET', - url: '/pet/{petId}', - security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], - ...config, - }) as Promise> + return withUnwrap( + request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } /** @@ -99,12 +106,14 @@ export class pet { */ public updatePetWithForm( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'POST', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } /** @@ -114,12 +123,12 @@ export class pet { */ public deletePet( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } /** @@ -128,11 +137,13 @@ export class pet { */ public uploadFile( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } } diff --git a/examples/sdk/src/gen/sdk/store/store.ts b/examples/sdk/src/gen/sdk/store/store.ts index 6686f3a6d..15fd3ecda 100644 --- a/examples/sdk/src/gen/sdk/store/store.ts +++ b/examples/sdk/src/gen/sdk/store/store.ts @@ -3,13 +3,13 @@ * Do not edit manually. */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from '../../.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { DeleteOrderOptions, DeleteOrderResponses } from '../../models/store/DeleteOrder' import type { GetInventoryOptions, GetInventoryResponses } from '../../models/store/GetInventory' import type { GetOrderByIdOptions, GetOrderByIdResponses } from '../../models/store/GetOrderById' import type { PlaceOrderOptions, PlaceOrderResponses } from '../../models/store/PlaceOrder' import type { PlaceOrderPatchOptions, PlaceOrderPatchResponses } from '../../models/store/PlaceOrderPatch' -import { createClient } from '../../.kubb/client' +import { createClient, withUnwrap } from '../../.kubb/client' export class store { private readonly client: ClientInstance @@ -25,12 +25,14 @@ export class store { */ public getInventory( options: Options = {}, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise< + RequestResult + >, + ) } /** @@ -40,10 +42,10 @@ export class store { */ public placeOrder( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } /** @@ -53,10 +55,10 @@ export class store { */ public placeOrderPatch( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'PATCH', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'PATCH', url: '/store/order', ...config }) as Promise>) } /** @@ -66,10 +68,10 @@ export class store { */ public getOrderById( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/store/order/{orderId}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/order/{orderId}', ...config }) as Promise>) } /** @@ -79,9 +81,9 @@ export class store { */ public deleteOrder( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'DELETE', url: '/store/order/{orderId}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/store/order/{orderId}', ...config }) as Promise>) } } diff --git a/examples/simple-single/src/gen/.kubb/client.ts b/examples/simple-single/src/gen/.kubb/client.ts index f80182aca..ea6223eff 100644 --- a/examples/simple-single/src/gen/.kubb/client.ts +++ b/examples/simple-single/src/gen/.kubb/client.ts @@ -82,6 +82,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/examples/simple-single/src/gen/clients/addPet.ts b/examples/simple-single/src/gen/clients/addPet.ts index c94fa5e36..5c86cf2c2 100644 --- a/examples/simple-single/src/gen/clients/addPet.ts +++ b/examples/simple-single/src/gen/clients/addPet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../models' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store @@ -14,8 +14,10 @@ import { client } from '../.kubb/client' */ export function addPet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap( + request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/examples/simple-single/src/gen/clients/deleteOrder.ts b/examples/simple-single/src/gen/clients/deleteOrder.ts index 28e685afa..18239d317 100644 --- a/examples/simple-single/src/gen/clients/deleteOrder.ts +++ b/examples/simple-single/src/gen/clients/deleteOrder.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeleteOrderOptions, DeleteOrderResponses } from '../models' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors @@ -14,8 +14,8 @@ import { client } from '../.kubb/client' */ export function deleteOrder( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/store/order/{orderId}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/store/order/{orderId}', ...config }) as Promise>) } diff --git a/examples/simple-single/src/gen/clients/deletePet.ts b/examples/simple-single/src/gen/clients/deletePet.ts index 673bfb395..6ffc70d69 100644 --- a/examples/simple-single/src/gen/clients/deletePet.ts +++ b/examples/simple-single/src/gen/clients/deletePet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../models' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet @@ -14,10 +14,10 @@ import { client } from '../.kubb/client' */ export function deletePet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/examples/simple-single/src/gen/clients/findPetsByStatus.ts b/examples/simple-single/src/gen/clients/findPetsByStatus.ts index bcd9a9139..2120d9126 100644 --- a/examples/simple-single/src/gen/clients/findPetsByStatus.ts +++ b/examples/simple-single/src/gen/clients/findPetsByStatus.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../models' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings @@ -14,14 +14,16 @@ import { client } from '../.kubb/client' */ export function findPetsByStatus( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/findByStatus', - security: [{ type: 'oauth2' }], - styles: { query: { status: { explode: true } } }, - ...config, - }) as Promise> + return withUnwrap( + request({ + method: 'GET', + url: '/pet/findByStatus', + security: [{ type: 'oauth2' }], + styles: { query: { status: { explode: true } } }, + ...config, + }) as Promise>, + ) } diff --git a/examples/simple-single/src/gen/clients/findPetsByTags.ts b/examples/simple-single/src/gen/clients/findPetsByTags.ts index 748391fb9..55a0ea5fd 100644 --- a/examples/simple-single/src/gen/clients/findPetsByTags.ts +++ b/examples/simple-single/src/gen/clients/findPetsByTags.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByTagsOptions, FindPetsByTagsResponses } from '../models' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. @@ -14,14 +14,12 @@ import { client } from '../.kubb/client' */ export function findPetsByTags( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/findByTags', - security: [{ type: 'oauth2' }], - styles: { query: { tags: { explode: true } } }, - ...config, - }) as Promise> + return withUnwrap( + request({ method: 'GET', url: '/pet/findByTags', security: [{ type: 'oauth2' }], styles: { query: { tags: { explode: true } } }, ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/simple-single/src/gen/clients/getInventory.ts b/examples/simple-single/src/gen/clients/getInventory.ts index 5b3ad5705..47d7d4048 100644 --- a/examples/simple-single/src/gen/clients/getInventory.ts +++ b/examples/simple-single/src/gen/clients/getInventory.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../models' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities @@ -14,10 +14,12 @@ import { client } from '../.kubb/client' */ export function getInventory( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/simple-single/src/gen/clients/getOrderById.ts b/examples/simple-single/src/gen/clients/getOrderById.ts index fd51b011e..b9e498ecf 100644 --- a/examples/simple-single/src/gen/clients/getOrderById.ts +++ b/examples/simple-single/src/gen/clients/getOrderById.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetOrderByIdOptions, GetOrderByIdResponses } from '../models' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions. @@ -14,8 +14,8 @@ import { client } from '../.kubb/client' */ export function getOrderById( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/order/{orderId}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/order/{orderId}', ...config }) as Promise>) } diff --git a/examples/simple-single/src/gen/clients/getPetById.ts b/examples/simple-single/src/gen/clients/getPetById.ts index cec7858f5..0c8ae4d07 100644 --- a/examples/simple-single/src/gen/clients/getPetById.ts +++ b/examples/simple-single/src/gen/clients/getPetById.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../models' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet @@ -14,13 +14,12 @@ import { client } from '../.kubb/client' */ export function getPetById( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/{petId}', - security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], - ...config, - }) as Promise> + return withUnwrap( + request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/simple-single/src/gen/clients/placeOrder.ts b/examples/simple-single/src/gen/clients/placeOrder.ts index 2ee6d5818..24add1fff 100644 --- a/examples/simple-single/src/gen/clients/placeOrder.ts +++ b/examples/simple-single/src/gen/clients/placeOrder.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../models' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store @@ -14,8 +14,8 @@ import { client } from '../.kubb/client' */ export function placeOrder( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/examples/simple-single/src/gen/clients/placeOrderPatch.ts b/examples/simple-single/src/gen/clients/placeOrderPatch.ts index c678c1cd4..9c6da9f33 100644 --- a/examples/simple-single/src/gen/clients/placeOrderPatch.ts +++ b/examples/simple-single/src/gen/clients/placeOrderPatch.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderPatchOptions, PlaceOrderPatchResponses } from '../models' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store with patch @@ -14,8 +14,8 @@ import { client } from '../.kubb/client' */ export function placeOrderPatch( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'PATCH', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'PATCH', url: '/store/order', ...config }) as Promise>) } diff --git a/examples/simple-single/src/gen/clients/updatePet.ts b/examples/simple-single/src/gen/clients/updatePet.ts index dbd5b3731..e5f8638ac 100644 --- a/examples/simple-single/src/gen/clients/updatePet.ts +++ b/examples/simple-single/src/gen/clients/updatePet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UpdatePetOptions, UpdatePetResponses } from '../models' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Update an existing pet by Id @@ -14,8 +14,10 @@ import { client } from '../.kubb/client' */ export function updatePet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'PUT', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap( + request({ method: 'PUT', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/examples/simple-single/src/gen/clients/updatePetWithForm.ts b/examples/simple-single/src/gen/clients/updatePetWithForm.ts index 07af7e471..a98d15f06 100644 --- a/examples/simple-single/src/gen/clients/updatePetWithForm.ts +++ b/examples/simple-single/src/gen/clients/updatePetWithForm.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UpdatePetWithFormOptions, UpdatePetWithFormResponses } from '../models' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary Updates a pet in the store with form data @@ -13,10 +13,12 @@ import { client } from '../.kubb/client' */ export function updatePetWithForm( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'POST', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/simple-single/src/gen/clients/uploadFile.ts b/examples/simple-single/src/gen/clients/uploadFile.ts index 31f23e9a5..d482003f9 100644 --- a/examples/simple-single/src/gen/clients/uploadFile.ts +++ b/examples/simple-single/src/gen/clients/uploadFile.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../models' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image @@ -13,14 +13,16 @@ import { client } from '../.kubb/client' */ export function uploadFile( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'POST', - url: '/pet/{petId}/uploadImage', - security: [{ type: 'oauth2' }], - contentType: { request: 'application/octet-stream' }, - ...config, - }) as Promise> + return withUnwrap( + request({ + method: 'POST', + url: '/pet/{petId}/uploadImage', + security: [{ type: 'oauth2' }], + contentType: { request: 'application/octet-stream' }, + ...config, + }) as Promise>, + ) } diff --git a/examples/simple-single/src/gen/hooks.ts b/examples/simple-single/src/gen/hooks.ts index a578ea6b3..a6014217d 100644 --- a/examples/simple-single/src/gen/hooks.ts +++ b/examples/simple-single/src/gen/hooks.ts @@ -73,8 +73,7 @@ export function updatePetMutationOptions( return mutationOptions, UpdatePetOptions, TContext>({ mutationKey, mutationFn: async ({ body }) => { - const { data } = await updatePet({ ...config, body, throwOnError: true }) - return data + return updatePet({ ...config, body, throwOnError: true }).unwrap() }, }) } @@ -90,8 +89,7 @@ export function addPetMutationOptions( return mutationOptions, AddPetOptions, TContext>({ mutationKey, mutationFn: async ({ body }) => { - const { data } = await addPet({ ...config, body, throwOnError: true }) - return data + return addPet({ ...config, body, throwOnError: true }).unwrap() }, }) } @@ -109,8 +107,7 @@ export function findPetsByStatusQueryOptions( return queryOptions, FindPetsByStatusStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } @@ -128,8 +125,7 @@ export function findPetsByTagsQueryOptions( return queryOptions, FindPetsByTagsStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } @@ -143,8 +139,7 @@ export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Part return queryOptions, GetPetByIdStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } @@ -156,8 +151,7 @@ export function updatePetWithFormMutationOptions(config: Par return mutationOptions, UpdatePetWithFormOptions, TContext>({ mutationKey, mutationFn: async ({ path, query }) => { - const { data } = await updatePetWithForm({ ...config, path, query, throwOnError: true }) - return data + return updatePetWithForm({ ...config, path, query, throwOnError: true }).unwrap() }, }) } @@ -169,8 +163,7 @@ export function deletePetMutationOptions(config: Partial, DeletePetOptions, TContext>({ mutationKey, mutationFn: async ({ path, headers }) => { - const { data } = await deletePet({ ...config, path, headers, throwOnError: true }) - return data + return deletePet({ ...config, path, headers, throwOnError: true }).unwrap() }, }) } @@ -182,8 +175,7 @@ export function uploadFileMutationOptions(config: Partial, UploadFileOptions, TContext>({ mutationKey, mutationFn: async ({ path, query, body }) => { - const { data } = await uploadFile({ ...config, path, query, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, query, body, throwOnError: true }).unwrap() }, }) } @@ -197,8 +189,7 @@ export function getInventoryQueryOptions(config: Partial, GetInventoryStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } @@ -214,8 +205,7 @@ export function placeOrderMutationOptions( return mutationOptions, PlaceOrderOptions, TContext>({ mutationKey, mutationFn: async ({ body }) => { - const { data } = await placeOrder({ ...config, body, throwOnError: true }) - return data + return placeOrder({ ...config, body, throwOnError: true }).unwrap() }, }) } @@ -231,8 +221,7 @@ export function placeOrderPatchMutationOptions( return mutationOptions, PlaceOrderPatchOptions, TContext>({ mutationKey, mutationFn: async ({ body }) => { - const { data } = await placeOrderPatch({ ...config, body, throwOnError: true }) - return data + return placeOrderPatch({ ...config, body, throwOnError: true }).unwrap() }, }) } @@ -249,8 +238,7 @@ export function getOrderByIdQueryOptions( return queryOptions, GetOrderByIdStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getOrderById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }) - return data + return getOrderById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } @@ -262,8 +250,7 @@ export function deleteOrderMutationOptions(config: Partial, DeleteOrderOptions, TContext>({ mutationKey, mutationFn: async ({ path }) => { - const { data } = await deleteOrder({ ...config, path, throwOnError: true }) - return data + return deleteOrder({ ...config, path, throwOnError: true }).unwrap() }, }) } diff --git a/examples/swr/src/gen/.kubb/client.ts b/examples/swr/src/gen/.kubb/client.ts index f80182aca..ea6223eff 100644 --- a/examples/swr/src/gen/.kubb/client.ts +++ b/examples/swr/src/gen/.kubb/client.ts @@ -82,6 +82,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/examples/swr/src/gen/clients/pet/addPet.ts b/examples/swr/src/gen/clients/pet/addPet.ts index 952e15db2..3628776a0 100644 --- a/examples/swr/src/gen/clients/pet/addPet.ts +++ b/examples/swr/src/gen/clients/pet/addPet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../../models/pet/AddPet' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Add a new pet to the store @@ -14,8 +14,10 @@ import { client } from '../../.kubb/client' */ export function addPet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap( + request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/examples/swr/src/gen/clients/pet/deletePet.ts b/examples/swr/src/gen/clients/pet/deletePet.ts index 24725dc26..8fe616c13 100644 --- a/examples/swr/src/gen/clients/pet/deletePet.ts +++ b/examples/swr/src/gen/clients/pet/deletePet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../../models/pet/DeletePet' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description delete a pet @@ -14,10 +14,10 @@ import { client } from '../../.kubb/client' */ export function deletePet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/examples/swr/src/gen/clients/pet/findPetsByStatus.ts b/examples/swr/src/gen/clients/pet/findPetsByStatus.ts index 306835cef..78c47179c 100644 --- a/examples/swr/src/gen/clients/pet/findPetsByStatus.ts +++ b/examples/swr/src/gen/clients/pet/findPetsByStatus.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../../models/pet/FindPetsByStatus' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings @@ -14,14 +14,16 @@ import { client } from '../../.kubb/client' */ export function findPetsByStatus( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/findByStatus', - security: [{ type: 'oauth2' }], - styles: { query: { status: { explode: true } } }, - ...config, - }) as Promise> + return withUnwrap( + request({ + method: 'GET', + url: '/pet/findByStatus', + security: [{ type: 'oauth2' }], + styles: { query: { status: { explode: true } } }, + ...config, + }) as Promise>, + ) } diff --git a/examples/swr/src/gen/clients/pet/findPetsByTags.ts b/examples/swr/src/gen/clients/pet/findPetsByTags.ts index b7227971b..e4b9b5f55 100644 --- a/examples/swr/src/gen/clients/pet/findPetsByTags.ts +++ b/examples/swr/src/gen/clients/pet/findPetsByTags.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { FindPetsByTagsOptions, FindPetsByTagsResponses } from '../../models/pet/FindPetsByTags' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. @@ -14,14 +14,12 @@ import { client } from '../../.kubb/client' */ export function findPetsByTags( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/findByTags', - security: [{ type: 'oauth2' }], - styles: { query: { tags: { explode: true } } }, - ...config, - }) as Promise> + return withUnwrap( + request({ method: 'GET', url: '/pet/findByTags', security: [{ type: 'oauth2' }], styles: { query: { tags: { explode: true } } }, ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/swr/src/gen/clients/pet/getPetById.ts b/examples/swr/src/gen/clients/pet/getPetById.ts index 771e59ee1..81e309e82 100644 --- a/examples/swr/src/gen/clients/pet/getPetById.ts +++ b/examples/swr/src/gen/clients/pet/getPetById.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../../models/pet/GetPetById' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Returns a single pet @@ -14,13 +14,12 @@ import { client } from '../../.kubb/client' */ export function getPetById( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/{petId}', - security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], - ...config, - }) as Promise> + return withUnwrap( + request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/swr/src/gen/clients/pet/updatePet.ts b/examples/swr/src/gen/clients/pet/updatePet.ts index 7a287a9fe..f662fd79f 100644 --- a/examples/swr/src/gen/clients/pet/updatePet.ts +++ b/examples/swr/src/gen/clients/pet/updatePet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { UpdatePetOptions, UpdatePetResponses } from '../../models/pet/UpdatePet' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Update an existing pet by Id @@ -14,8 +14,10 @@ import { client } from '../../.kubb/client' */ export function updatePet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'PUT', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap( + request({ method: 'PUT', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/examples/swr/src/gen/clients/pet/updatePetWithForm.ts b/examples/swr/src/gen/clients/pet/updatePetWithForm.ts index f36371438..1db98bc49 100644 --- a/examples/swr/src/gen/clients/pet/updatePetWithForm.ts +++ b/examples/swr/src/gen/clients/pet/updatePetWithForm.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { UpdatePetWithFormOptions, UpdatePetWithFormResponses } from '../../models/pet/UpdatePetWithForm' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary Updates a pet in the store with form data @@ -13,10 +13,12 @@ import { client } from '../../.kubb/client' */ export function updatePetWithForm( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'POST', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/swr/src/gen/clients/pet/uploadFile.ts b/examples/swr/src/gen/clients/pet/uploadFile.ts index 65e79b47b..92ce7c699 100644 --- a/examples/swr/src/gen/clients/pet/uploadFile.ts +++ b/examples/swr/src/gen/clients/pet/uploadFile.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../../models/pet/UploadFile' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary uploads an image @@ -13,10 +13,12 @@ import { client } from '../../.kubb/client' */ export function uploadFile( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/swr/src/gen/clients/store/deleteOrder.ts b/examples/swr/src/gen/clients/store/deleteOrder.ts index 74eec3514..689cbaa9a 100644 --- a/examples/swr/src/gen/clients/store/deleteOrder.ts +++ b/examples/swr/src/gen/clients/store/deleteOrder.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { DeleteOrderOptions, DeleteOrderResponses } from '../../models/store/DeleteOrder' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function deleteOrder( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/store/order/{orderId}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/store/order/{orderId}', ...config }) as Promise>) } diff --git a/examples/swr/src/gen/clients/store/getInventory.ts b/examples/swr/src/gen/clients/store/getInventory.ts index f011e8dde..d44938f8e 100644 --- a/examples/swr/src/gen/clients/store/getInventory.ts +++ b/examples/swr/src/gen/clients/store/getInventory.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../../models/store/GetInventory' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Returns a map of status codes to quantities @@ -14,10 +14,12 @@ import { client } from '../../.kubb/client' */ export function getInventory( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/swr/src/gen/clients/store/getOrderById.ts b/examples/swr/src/gen/clients/store/getOrderById.ts index 91aa5bd78..a3ce96a72 100644 --- a/examples/swr/src/gen/clients/store/getOrderById.ts +++ b/examples/swr/src/gen/clients/store/getOrderById.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { GetOrderByIdOptions, GetOrderByIdResponses } from '../../models/store/GetOrderById' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions. @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function getOrderById( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/order/{orderId}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/order/{orderId}', ...config }) as Promise>) } diff --git a/examples/swr/src/gen/clients/store/placeOrder.ts b/examples/swr/src/gen/clients/store/placeOrder.ts index 8c1d56394..3a3da9192 100644 --- a/examples/swr/src/gen/clients/store/placeOrder.ts +++ b/examples/swr/src/gen/clients/store/placeOrder.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../../models/store/PlaceOrder' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Place a new order in the store @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function placeOrder( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/examples/swr/src/gen/clients/store/placeOrderPatch.ts b/examples/swr/src/gen/clients/store/placeOrderPatch.ts index 0772af4f7..692337b25 100644 --- a/examples/swr/src/gen/clients/store/placeOrderPatch.ts +++ b/examples/swr/src/gen/clients/store/placeOrderPatch.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { PlaceOrderPatchOptions, PlaceOrderPatchResponses } from '../../models/store/PlaceOrderPatch' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Place a new order in the store with patch @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function placeOrderPatch( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'PATCH', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'PATCH', url: '/store/order', ...config }) as Promise>) } diff --git a/examples/swr/src/gen/clients/user/createUser.ts b/examples/swr/src/gen/clients/user/createUser.ts index af5bbc3e7..d9c8c13bc 100644 --- a/examples/swr/src/gen/clients/user/createUser.ts +++ b/examples/swr/src/gen/clients/user/createUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { CreateUserOptions, CreateUserResponses } from '../../models/user/CreateUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description This can only be done by the logged in user. @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function createUser( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/user', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/user', ...config }) as Promise>) } diff --git a/examples/swr/src/gen/clients/user/createUsersWithListInput.ts b/examples/swr/src/gen/clients/user/createUsersWithListInput.ts index b69d5d908..1e6a9e2ac 100644 --- a/examples/swr/src/gen/clients/user/createUsersWithListInput.ts +++ b/examples/swr/src/gen/clients/user/createUsersWithListInput.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { CreateUsersWithListInputOptions, CreateUsersWithListInputResponses } from '../../models/user/CreateUsersWithListInput' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Creates list of users with given input array @@ -14,8 +14,10 @@ import { client } from '../../.kubb/client' */ export function createUsersWithListInput( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/user/createWithList', ...config }) as Promise> + return withUnwrap( + request({ method: 'POST', url: '/user/createWithList', ...config }) as Promise>, + ) } diff --git a/examples/swr/src/gen/clients/user/deleteUser.ts b/examples/swr/src/gen/clients/user/deleteUser.ts index dff00a13e..8dda91163 100644 --- a/examples/swr/src/gen/clients/user/deleteUser.ts +++ b/examples/swr/src/gen/clients/user/deleteUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { DeleteUserOptions, DeleteUserResponses } from '../../models/user/DeleteUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description This can only be done by the logged in user. @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function deleteUser( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/user/{username}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/user/{username}', ...config }) as Promise>) } diff --git a/examples/swr/src/gen/clients/user/getUserByName.ts b/examples/swr/src/gen/clients/user/getUserByName.ts index 4db3b9d4d..2e0afe704 100644 --- a/examples/swr/src/gen/clients/user/getUserByName.ts +++ b/examples/swr/src/gen/clients/user/getUserByName.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { GetUserByNameOptions, GetUserByNameResponses } from '../../models/user/GetUserByName' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary Get user by user name @@ -13,8 +13,8 @@ import { client } from '../../.kubb/client' */ export function getUserByName( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/user/{username}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/user/{username}', ...config }) as Promise>) } diff --git a/examples/swr/src/gen/clients/user/loginUser.ts b/examples/swr/src/gen/clients/user/loginUser.ts index b619b4b9b..4a46c2208 100644 --- a/examples/swr/src/gen/clients/user/loginUser.ts +++ b/examples/swr/src/gen/clients/user/loginUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { LoginUserOptions, LoginUserResponses } from '../../models/user/LoginUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary Logs user into the system @@ -13,8 +13,8 @@ import { client } from '../../.kubb/client' */ export function loginUser( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/user/login', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/user/login', ...config }) as Promise>) } diff --git a/examples/swr/src/gen/clients/user/logoutUser.ts b/examples/swr/src/gen/clients/user/logoutUser.ts index e41276de9..a5c939fbb 100644 --- a/examples/swr/src/gen/clients/user/logoutUser.ts +++ b/examples/swr/src/gen/clients/user/logoutUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { LogoutUserOptions, LogoutUserResponses } from '../../models/user/LogoutUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary Logs out current logged in user session @@ -13,8 +13,8 @@ import { client } from '../../.kubb/client' */ export function logoutUser( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/user/logout', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/user/logout', ...config }) as Promise>) } diff --git a/examples/swr/src/gen/clients/user/updateUser.ts b/examples/swr/src/gen/clients/user/updateUser.ts index ce0faef1f..b6c6e7de1 100644 --- a/examples/swr/src/gen/clients/user/updateUser.ts +++ b/examples/swr/src/gen/clients/user/updateUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { UpdateUserOptions, UpdateUserResponses } from '../../models/user/UpdateUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description This can only be done by the logged in user. @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function updateUser( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'PUT', url: '/user/{username}', ...config }) as Promise> + return withUnwrap(request({ method: 'PUT', url: '/user/{username}', ...config }) as Promise>) } diff --git a/examples/swr/src/gen/hooks/pet/useAddPet.ts b/examples/swr/src/gen/hooks/pet/useAddPet.ts index 0e402317d..9c9f294c5 100644 --- a/examples/swr/src/gen/hooks/pet/useAddPet.ts +++ b/examples/swr/src/gen/hooks/pet/useAddPet.ts @@ -40,8 +40,7 @@ export function useAddPet( return useSWRMutation, AddPetMutationKey | null, AddPetMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { body } }) => { - const { data } = await addPet({ ...config, body, throwOnError: true }) - return data + return addPet({ ...config, body, throwOnError: true }).unwrap() }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/pet/useDeletePet.ts b/examples/swr/src/gen/hooks/pet/useDeletePet.ts index 41a271de5..4064d803e 100644 --- a/examples/swr/src/gen/hooks/pet/useDeletePet.ts +++ b/examples/swr/src/gen/hooks/pet/useDeletePet.ts @@ -35,8 +35,7 @@ export function useDeletePet( return useSWRMutation, DeletePetMutationKey | null, DeletePetMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { path, headers } }) => { - const { data } = await deletePet({ ...config, path, headers, throwOnError: true }) - return data + return deletePet({ ...config, path, headers, throwOnError: true }).unwrap() }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/pet/useFindPetsByStatus.ts b/examples/swr/src/gen/hooks/pet/useFindPetsByStatus.ts index c6e3785c6..ef1ad60f5 100644 --- a/examples/swr/src/gen/hooks/pet/useFindPetsByStatus.ts +++ b/examples/swr/src/gen/hooks/pet/useFindPetsByStatus.ts @@ -20,8 +20,7 @@ export function findPetsByStatusQueryOptions( ) { return { fetcher: async () => { - const { data } = await findPetsByStatus({ ...config, query, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, throwOnError: true }).unwrap() }, } } diff --git a/examples/swr/src/gen/hooks/pet/useFindPetsByTags.ts b/examples/swr/src/gen/hooks/pet/useFindPetsByTags.ts index 40a873e9e..79d5336f3 100644 --- a/examples/swr/src/gen/hooks/pet/useFindPetsByTags.ts +++ b/examples/swr/src/gen/hooks/pet/useFindPetsByTags.ts @@ -20,8 +20,7 @@ export function findPetsByTagsQueryOptions( ) { return { fetcher: async () => { - const { data } = await findPetsByTags({ ...config, query, throwOnError: true }) - return data + return findPetsByTags({ ...config, query, throwOnError: true }).unwrap() }, } } diff --git a/examples/swr/src/gen/hooks/pet/useGetPetById.ts b/examples/swr/src/gen/hooks/pet/useGetPetById.ts index b971a1208..d5705fea2 100644 --- a/examples/swr/src/gen/hooks/pet/useGetPetById.ts +++ b/examples/swr/src/gen/hooks/pet/useGetPetById.ts @@ -16,8 +16,7 @@ type GetPetByIdQueryKey = ReturnType export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await getPetById({ ...config, path, throwOnError: true }) - return data + return getPetById({ ...config, path, throwOnError: true }).unwrap() }, } } diff --git a/examples/swr/src/gen/hooks/pet/useUpdatePet.ts b/examples/swr/src/gen/hooks/pet/useUpdatePet.ts index b83bc286d..71e0877c2 100644 --- a/examples/swr/src/gen/hooks/pet/useUpdatePet.ts +++ b/examples/swr/src/gen/hooks/pet/useUpdatePet.ts @@ -48,8 +48,7 @@ export function useUpdatePet( >( shouldFetch ? mutationKey : null, async (_url, { arg: { body } }) => { - const { data } = await updatePet({ ...config, body, throwOnError: true }) - return data + return updatePet({ ...config, body, throwOnError: true }).unwrap() }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/pet/useUpdatePetWithForm.ts b/examples/swr/src/gen/hooks/pet/useUpdatePetWithForm.ts index 0f9788bd7..728876e8f 100644 --- a/examples/swr/src/gen/hooks/pet/useUpdatePetWithForm.ts +++ b/examples/swr/src/gen/hooks/pet/useUpdatePetWithForm.ts @@ -42,8 +42,7 @@ export function useUpdatePetWithForm( >( shouldFetch ? mutationKey : null, async (_url, { arg: { path, query } }) => { - const { data } = await updatePetWithForm({ ...config, path, query, throwOnError: true }) - return data + return updatePetWithForm({ ...config, path, query, throwOnError: true }).unwrap() }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/pet/useUploadFile.ts b/examples/swr/src/gen/hooks/pet/useUploadFile.ts index c010a2ba4..486ba6bee 100644 --- a/examples/swr/src/gen/hooks/pet/useUploadFile.ts +++ b/examples/swr/src/gen/hooks/pet/useUploadFile.ts @@ -36,8 +36,7 @@ export function useUploadFile( return useSWRMutation, UploadFileMutationKey | null, UploadFileMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { path, query, body } }) => { - const { data } = await uploadFile({ ...config, path, query, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, query, body, throwOnError: true }).unwrap() }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/store/useDeleteOrder.ts b/examples/swr/src/gen/hooks/store/useDeleteOrder.ts index 3ec482754..3fc379f17 100644 --- a/examples/swr/src/gen/hooks/store/useDeleteOrder.ts +++ b/examples/swr/src/gen/hooks/store/useDeleteOrder.ts @@ -43,8 +43,7 @@ export function useDeleteOrder( >( shouldFetch ? mutationKey : null, async (_url, { arg: { path } }) => { - const { data } = await deleteOrder({ ...config, path, throwOnError: true }) - return data + return deleteOrder({ ...config, path, throwOnError: true }).unwrap() }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/store/useGetInventory.ts b/examples/swr/src/gen/hooks/store/useGetInventory.ts index 81300c3d9..0fff1e558 100644 --- a/examples/swr/src/gen/hooks/store/useGetInventory.ts +++ b/examples/swr/src/gen/hooks/store/useGetInventory.ts @@ -16,8 +16,7 @@ type GetInventoryQueryKey = ReturnType export function getInventoryQueryOptions(config: Partial> = {}) { return { fetcher: async () => { - const { data } = await getInventory({ ...config, throwOnError: true }) - return data + return getInventory({ ...config, throwOnError: true }).unwrap() }, } } diff --git a/examples/swr/src/gen/hooks/store/useGetOrderById.ts b/examples/swr/src/gen/hooks/store/useGetOrderById.ts index 279f39a0a..99027099c 100644 --- a/examples/swr/src/gen/hooks/store/useGetOrderById.ts +++ b/examples/swr/src/gen/hooks/store/useGetOrderById.ts @@ -19,8 +19,7 @@ export function getOrderByIdQueryOptions( ) { return { fetcher: async () => { - const { data } = await getOrderById({ ...config, path, throwOnError: true }) - return data + return getOrderById({ ...config, path, throwOnError: true }).unwrap() }, } } diff --git a/examples/swr/src/gen/hooks/store/usePlaceOrder.ts b/examples/swr/src/gen/hooks/store/usePlaceOrder.ts index 2f905d982..4d92f527d 100644 --- a/examples/swr/src/gen/hooks/store/usePlaceOrder.ts +++ b/examples/swr/src/gen/hooks/store/usePlaceOrder.ts @@ -37,8 +37,7 @@ export function usePlaceOrder( return useSWRMutation, PlaceOrderMutationKey | null, PlaceOrderMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { body } }) => { - const { data } = await placeOrder({ ...config, body, throwOnError: true }) - return data + return placeOrder({ ...config, body, throwOnError: true }).unwrap() }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/store/usePlaceOrderPatch.ts b/examples/swr/src/gen/hooks/store/usePlaceOrderPatch.ts index d1ec839d1..66b27b012 100644 --- a/examples/swr/src/gen/hooks/store/usePlaceOrderPatch.ts +++ b/examples/swr/src/gen/hooks/store/usePlaceOrderPatch.ts @@ -40,8 +40,7 @@ export function usePlaceOrderPatch( return useSWRMutation, PlaceOrderPatchMutationKey | null, PlaceOrderPatchMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { body } }) => { - const { data } = await placeOrderPatch({ ...config, body, throwOnError: true }) - return data + return placeOrderPatch({ ...config, body, throwOnError: true }).unwrap() }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/user/useCreateUser.ts b/examples/swr/src/gen/hooks/user/useCreateUser.ts index a1fce9bca..2f35dd7d4 100644 --- a/examples/swr/src/gen/hooks/user/useCreateUser.ts +++ b/examples/swr/src/gen/hooks/user/useCreateUser.ts @@ -37,8 +37,7 @@ export function useCreateUser( return useSWRMutation, CreateUserMutationKey | null, CreateUserMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { body } }) => { - const { data } = await createUser({ ...config, body, throwOnError: true }) - return data + return createUser({ ...config, body, throwOnError: true }).unwrap() }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/user/useCreateUsersWithListInput.ts b/examples/swr/src/gen/hooks/user/useCreateUsersWithListInput.ts index cb359cb5a..ccacf7356 100644 --- a/examples/swr/src/gen/hooks/user/useCreateUsersWithListInput.ts +++ b/examples/swr/src/gen/hooks/user/useCreateUsersWithListInput.ts @@ -45,8 +45,7 @@ export function useCreateUsersWithListInput( >( shouldFetch ? mutationKey : null, async (_url, { arg: { body } }) => { - const { data } = await createUsersWithListInput({ ...config, body, throwOnError: true }) - return data + return createUsersWithListInput({ ...config, body, throwOnError: true }).unwrap() }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/user/useDeleteUser.ts b/examples/swr/src/gen/hooks/user/useDeleteUser.ts index a6a3b99f6..5b25f7ed7 100644 --- a/examples/swr/src/gen/hooks/user/useDeleteUser.ts +++ b/examples/swr/src/gen/hooks/user/useDeleteUser.ts @@ -43,8 +43,7 @@ export function useDeleteUser( >( shouldFetch ? mutationKey : null, async (_url, { arg: { path } }) => { - const { data } = await deleteUser({ ...config, path, throwOnError: true }) - return data + return deleteUser({ ...config, path, throwOnError: true }).unwrap() }, mutationOptions, ) diff --git a/examples/swr/src/gen/hooks/user/useGetUserByName.ts b/examples/swr/src/gen/hooks/user/useGetUserByName.ts index fbb641a5f..805295dcc 100644 --- a/examples/swr/src/gen/hooks/user/useGetUserByName.ts +++ b/examples/swr/src/gen/hooks/user/useGetUserByName.ts @@ -19,8 +19,7 @@ export function getUserByNameQueryOptions( ) { return { fetcher: async () => { - const { data } = await getUserByName({ ...config, path, throwOnError: true }) - return data + return getUserByName({ ...config, path, throwOnError: true }).unwrap() }, } } diff --git a/examples/swr/src/gen/hooks/user/useLoginUser.ts b/examples/swr/src/gen/hooks/user/useLoginUser.ts index 4757ac8f9..94f94ccec 100644 --- a/examples/swr/src/gen/hooks/user/useLoginUser.ts +++ b/examples/swr/src/gen/hooks/user/useLoginUser.ts @@ -19,8 +19,7 @@ export function loginUserQueryOptions( ) { return { fetcher: async () => { - const { data } = await loginUser({ ...config, query, throwOnError: true }) - return data + return loginUser({ ...config, query, throwOnError: true }).unwrap() }, } } diff --git a/examples/swr/src/gen/hooks/user/useLogoutUser.ts b/examples/swr/src/gen/hooks/user/useLogoutUser.ts index 204cfcf80..4dfdb8a19 100644 --- a/examples/swr/src/gen/hooks/user/useLogoutUser.ts +++ b/examples/swr/src/gen/hooks/user/useLogoutUser.ts @@ -16,8 +16,7 @@ type LogoutUserQueryKey = ReturnType export function logoutUserQueryOptions(config: Partial> = {}) { return { fetcher: async () => { - const { data } = await logoutUser({ ...config, throwOnError: true }) - return data + return logoutUser({ ...config, throwOnError: true }).unwrap() }, } } diff --git a/examples/swr/src/gen/hooks/user/useUpdateUser.ts b/examples/swr/src/gen/hooks/user/useUpdateUser.ts index 855106e13..bc956a546 100644 --- a/examples/swr/src/gen/hooks/user/useUpdateUser.ts +++ b/examples/swr/src/gen/hooks/user/useUpdateUser.ts @@ -37,8 +37,7 @@ export function useUpdateUser( return useSWRMutation, UpdateUserMutationKey | null, UpdateUserMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { path, body } }) => { - const { data } = await updateUser({ ...config, path, body, throwOnError: true }) - return data + return updateUser({ ...config, path, body, throwOnError: true }).unwrap() }, mutationOptions, ) diff --git a/examples/vue-query/src/gen/.kubb/client.ts b/examples/vue-query/src/gen/.kubb/client.ts index f80182aca..ea6223eff 100644 --- a/examples/vue-query/src/gen/.kubb/client.ts +++ b/examples/vue-query/src/gen/.kubb/client.ts @@ -82,6 +82,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/examples/vue-query/src/gen/clients/pet/addPet.ts b/examples/vue-query/src/gen/clients/pet/addPet.ts index 952e15db2..3628776a0 100644 --- a/examples/vue-query/src/gen/clients/pet/addPet.ts +++ b/examples/vue-query/src/gen/clients/pet/addPet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../../models/pet/AddPet' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Add a new pet to the store @@ -14,8 +14,10 @@ import { client } from '../../.kubb/client' */ export function addPet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap( + request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/examples/vue-query/src/gen/clients/pet/deletePet.ts b/examples/vue-query/src/gen/clients/pet/deletePet.ts index 24725dc26..8fe616c13 100644 --- a/examples/vue-query/src/gen/clients/pet/deletePet.ts +++ b/examples/vue-query/src/gen/clients/pet/deletePet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../../models/pet/DeletePet' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description delete a pet @@ -14,10 +14,10 @@ import { client } from '../../.kubb/client' */ export function deletePet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/examples/vue-query/src/gen/clients/pet/findPetsByStatus.ts b/examples/vue-query/src/gen/clients/pet/findPetsByStatus.ts index 306835cef..78c47179c 100644 --- a/examples/vue-query/src/gen/clients/pet/findPetsByStatus.ts +++ b/examples/vue-query/src/gen/clients/pet/findPetsByStatus.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../../models/pet/FindPetsByStatus' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings @@ -14,14 +14,16 @@ import { client } from '../../.kubb/client' */ export function findPetsByStatus( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/findByStatus', - security: [{ type: 'oauth2' }], - styles: { query: { status: { explode: true } } }, - ...config, - }) as Promise> + return withUnwrap( + request({ + method: 'GET', + url: '/pet/findByStatus', + security: [{ type: 'oauth2' }], + styles: { query: { status: { explode: true } } }, + ...config, + }) as Promise>, + ) } diff --git a/examples/vue-query/src/gen/clients/pet/findPetsByTags.ts b/examples/vue-query/src/gen/clients/pet/findPetsByTags.ts index b7227971b..e4b9b5f55 100644 --- a/examples/vue-query/src/gen/clients/pet/findPetsByTags.ts +++ b/examples/vue-query/src/gen/clients/pet/findPetsByTags.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { FindPetsByTagsOptions, FindPetsByTagsResponses } from '../../models/pet/FindPetsByTags' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. @@ -14,14 +14,12 @@ import { client } from '../../.kubb/client' */ export function findPetsByTags( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/findByTags', - security: [{ type: 'oauth2' }], - styles: { query: { tags: { explode: true } } }, - ...config, - }) as Promise> + return withUnwrap( + request({ method: 'GET', url: '/pet/findByTags', security: [{ type: 'oauth2' }], styles: { query: { tags: { explode: true } } }, ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/vue-query/src/gen/clients/pet/getPetById.ts b/examples/vue-query/src/gen/clients/pet/getPetById.ts index 771e59ee1..81e309e82 100644 --- a/examples/vue-query/src/gen/clients/pet/getPetById.ts +++ b/examples/vue-query/src/gen/clients/pet/getPetById.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../../models/pet/GetPetById' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Returns a single pet @@ -14,13 +14,12 @@ import { client } from '../../.kubb/client' */ export function getPetById( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/{petId}', - security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], - ...config, - }) as Promise> + return withUnwrap( + request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/vue-query/src/gen/clients/pet/updatePet.ts b/examples/vue-query/src/gen/clients/pet/updatePet.ts index 7a287a9fe..f662fd79f 100644 --- a/examples/vue-query/src/gen/clients/pet/updatePet.ts +++ b/examples/vue-query/src/gen/clients/pet/updatePet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { UpdatePetOptions, UpdatePetResponses } from '../../models/pet/UpdatePet' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Update an existing pet by Id @@ -14,8 +14,10 @@ import { client } from '../../.kubb/client' */ export function updatePet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'PUT', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap( + request({ method: 'PUT', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/examples/vue-query/src/gen/clients/pet/updatePetWithForm.ts b/examples/vue-query/src/gen/clients/pet/updatePetWithForm.ts index f36371438..1db98bc49 100644 --- a/examples/vue-query/src/gen/clients/pet/updatePetWithForm.ts +++ b/examples/vue-query/src/gen/clients/pet/updatePetWithForm.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { UpdatePetWithFormOptions, UpdatePetWithFormResponses } from '../../models/pet/UpdatePetWithForm' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary Updates a pet in the store with form data @@ -13,10 +13,12 @@ import { client } from '../../.kubb/client' */ export function updatePetWithForm( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'POST', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/vue-query/src/gen/clients/pet/uploadFile.ts b/examples/vue-query/src/gen/clients/pet/uploadFile.ts index 65e79b47b..92ce7c699 100644 --- a/examples/vue-query/src/gen/clients/pet/uploadFile.ts +++ b/examples/vue-query/src/gen/clients/pet/uploadFile.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../../models/pet/UploadFile' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary uploads an image @@ -13,10 +13,12 @@ import { client } from '../../.kubb/client' */ export function uploadFile( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/vue-query/src/gen/clients/store/deleteOrder.ts b/examples/vue-query/src/gen/clients/store/deleteOrder.ts index 74eec3514..689cbaa9a 100644 --- a/examples/vue-query/src/gen/clients/store/deleteOrder.ts +++ b/examples/vue-query/src/gen/clients/store/deleteOrder.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { DeleteOrderOptions, DeleteOrderResponses } from '../../models/store/DeleteOrder' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function deleteOrder( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/store/order/{orderId}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/store/order/{orderId}', ...config }) as Promise>) } diff --git a/examples/vue-query/src/gen/clients/store/getInventory.ts b/examples/vue-query/src/gen/clients/store/getInventory.ts index f011e8dde..d44938f8e 100644 --- a/examples/vue-query/src/gen/clients/store/getInventory.ts +++ b/examples/vue-query/src/gen/clients/store/getInventory.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../../models/store/GetInventory' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Returns a map of status codes to quantities @@ -14,10 +14,12 @@ import { client } from '../../.kubb/client' */ export function getInventory( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/examples/vue-query/src/gen/clients/store/getOrderById.ts b/examples/vue-query/src/gen/clients/store/getOrderById.ts index 91aa5bd78..a3ce96a72 100644 --- a/examples/vue-query/src/gen/clients/store/getOrderById.ts +++ b/examples/vue-query/src/gen/clients/store/getOrderById.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { GetOrderByIdOptions, GetOrderByIdResponses } from '../../models/store/GetOrderById' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions. @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function getOrderById( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/order/{orderId}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/order/{orderId}', ...config }) as Promise>) } diff --git a/examples/vue-query/src/gen/clients/store/placeOrder.ts b/examples/vue-query/src/gen/clients/store/placeOrder.ts index 8c1d56394..3a3da9192 100644 --- a/examples/vue-query/src/gen/clients/store/placeOrder.ts +++ b/examples/vue-query/src/gen/clients/store/placeOrder.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../../models/store/PlaceOrder' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Place a new order in the store @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function placeOrder( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/examples/vue-query/src/gen/clients/store/placeOrderPatch.ts b/examples/vue-query/src/gen/clients/store/placeOrderPatch.ts index 0772af4f7..692337b25 100644 --- a/examples/vue-query/src/gen/clients/store/placeOrderPatch.ts +++ b/examples/vue-query/src/gen/clients/store/placeOrderPatch.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { PlaceOrderPatchOptions, PlaceOrderPatchResponses } from '../../models/store/PlaceOrderPatch' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Place a new order in the store with patch @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function placeOrderPatch( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'PATCH', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'PATCH', url: '/store/order', ...config }) as Promise>) } diff --git a/examples/vue-query/src/gen/clients/user/createUser.ts b/examples/vue-query/src/gen/clients/user/createUser.ts index af5bbc3e7..d9c8c13bc 100644 --- a/examples/vue-query/src/gen/clients/user/createUser.ts +++ b/examples/vue-query/src/gen/clients/user/createUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { CreateUserOptions, CreateUserResponses } from '../../models/user/CreateUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description This can only be done by the logged in user. @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function createUser( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/user', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/user', ...config }) as Promise>) } diff --git a/examples/vue-query/src/gen/clients/user/createUsersWithListInput.ts b/examples/vue-query/src/gen/clients/user/createUsersWithListInput.ts index b69d5d908..1e6a9e2ac 100644 --- a/examples/vue-query/src/gen/clients/user/createUsersWithListInput.ts +++ b/examples/vue-query/src/gen/clients/user/createUsersWithListInput.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { CreateUsersWithListInputOptions, CreateUsersWithListInputResponses } from '../../models/user/CreateUsersWithListInput' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description Creates list of users with given input array @@ -14,8 +14,10 @@ import { client } from '../../.kubb/client' */ export function createUsersWithListInput( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/user/createWithList', ...config }) as Promise> + return withUnwrap( + request({ method: 'POST', url: '/user/createWithList', ...config }) as Promise>, + ) } diff --git a/examples/vue-query/src/gen/clients/user/deleteUser.ts b/examples/vue-query/src/gen/clients/user/deleteUser.ts index dff00a13e..8dda91163 100644 --- a/examples/vue-query/src/gen/clients/user/deleteUser.ts +++ b/examples/vue-query/src/gen/clients/user/deleteUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { DeleteUserOptions, DeleteUserResponses } from '../../models/user/DeleteUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description This can only be done by the logged in user. @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function deleteUser( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/user/{username}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/user/{username}', ...config }) as Promise>) } diff --git a/examples/vue-query/src/gen/clients/user/getUserByName.ts b/examples/vue-query/src/gen/clients/user/getUserByName.ts index 4db3b9d4d..2e0afe704 100644 --- a/examples/vue-query/src/gen/clients/user/getUserByName.ts +++ b/examples/vue-query/src/gen/clients/user/getUserByName.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { GetUserByNameOptions, GetUserByNameResponses } from '../../models/user/GetUserByName' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary Get user by user name @@ -13,8 +13,8 @@ import { client } from '../../.kubb/client' */ export function getUserByName( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/user/{username}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/user/{username}', ...config }) as Promise>) } diff --git a/examples/vue-query/src/gen/clients/user/loginUser.ts b/examples/vue-query/src/gen/clients/user/loginUser.ts index b619b4b9b..4a46c2208 100644 --- a/examples/vue-query/src/gen/clients/user/loginUser.ts +++ b/examples/vue-query/src/gen/clients/user/loginUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { LoginUserOptions, LoginUserResponses } from '../../models/user/LoginUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary Logs user into the system @@ -13,8 +13,8 @@ import { client } from '../../.kubb/client' */ export function loginUser( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/user/login', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/user/login', ...config }) as Promise>) } diff --git a/examples/vue-query/src/gen/clients/user/logoutUser.ts b/examples/vue-query/src/gen/clients/user/logoutUser.ts index e41276de9..a5c939fbb 100644 --- a/examples/vue-query/src/gen/clients/user/logoutUser.ts +++ b/examples/vue-query/src/gen/clients/user/logoutUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { LogoutUserOptions, LogoutUserResponses } from '../../models/user/LogoutUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @summary Logs out current logged in user session @@ -13,8 +13,8 @@ import { client } from '../../.kubb/client' */ export function logoutUser( options: Options = {}, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/user/logout', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/user/logout', ...config }) as Promise>) } diff --git a/examples/vue-query/src/gen/clients/user/updateUser.ts b/examples/vue-query/src/gen/clients/user/updateUser.ts index ce0faef1f..b6c6e7de1 100644 --- a/examples/vue-query/src/gen/clients/user/updateUser.ts +++ b/examples/vue-query/src/gen/clients/user/updateUser.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../../.kubb/client' import type { UpdateUserOptions, UpdateUserResponses } from '../../models/user/UpdateUser' -import { client } from '../../.kubb/client' +import { client, withUnwrap } from '../../.kubb/client' /** * @description This can only be done by the logged in user. @@ -14,8 +14,8 @@ import { client } from '../../.kubb/client' */ export function updateUser( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'PUT', url: '/user/{username}', ...config }) as Promise> + return withUnwrap(request({ method: 'PUT', url: '/user/{username}', ...config }) as Promise>) } diff --git a/examples/vue-query/src/gen/hooks/pet/useFindPetsByStatus.ts b/examples/vue-query/src/gen/hooks/pet/useFindPetsByStatus.ts index 7f1ca2ffc..5092df3bf 100644 --- a/examples/vue-query/src/gen/hooks/pet/useFindPetsByStatus.ts +++ b/examples/vue-query/src/gen/hooks/pet/useFindPetsByStatus.ts @@ -23,8 +23,7 @@ export function findPetsByStatusQueryOptions( return queryOptions, FindPetsByStatusStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/examples/vue-query/src/gen/hooks/pet/useFindPetsByTags.ts b/examples/vue-query/src/gen/hooks/pet/useFindPetsByTags.ts index 5666fd3a3..c722cd609 100644 --- a/examples/vue-query/src/gen/hooks/pet/useFindPetsByTags.ts +++ b/examples/vue-query/src/gen/hooks/pet/useFindPetsByTags.ts @@ -23,8 +23,7 @@ export function findPetsByTagsQueryOptions( return queryOptions, FindPetsByTagsStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByTags({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/examples/vue-query/src/gen/hooks/pet/useGetPetById.ts b/examples/vue-query/src/gen/hooks/pet/useGetPetById.ts index a0f20c238..fa4011c9f 100644 --- a/examples/vue-query/src/gen/hooks/pet/useGetPetById.ts +++ b/examples/vue-query/src/gen/hooks/pet/useGetPetById.ts @@ -23,8 +23,7 @@ export function getPetByIdQueryOptions( return queryOptions, GetPetByIdStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/examples/vue-query/src/gen/hooks/store/useGetInventory.ts b/examples/vue-query/src/gen/hooks/store/useGetInventory.ts index 7c84ec2cc..a7105fafa 100644 --- a/examples/vue-query/src/gen/hooks/store/useGetInventory.ts +++ b/examples/vue-query/src/gen/hooks/store/useGetInventory.ts @@ -17,8 +17,7 @@ export function getInventoryQueryOptions(config: Partial, GetInventoryStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/examples/vue-query/src/gen/hooks/store/useGetOrderById.ts b/examples/vue-query/src/gen/hooks/store/useGetOrderById.ts index 483acb8ab..01e22d71b 100644 --- a/examples/vue-query/src/gen/hooks/store/useGetOrderById.ts +++ b/examples/vue-query/src/gen/hooks/store/useGetOrderById.ts @@ -23,8 +23,7 @@ export function getOrderByIdQueryOptions( return queryOptions, GetOrderByIdStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getOrderById({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }) - return data + return getOrderById({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/examples/vue-query/src/gen/hooks/user/useGetUserByName.ts b/examples/vue-query/src/gen/hooks/user/useGetUserByName.ts index 76ce1373c..63f4df212 100644 --- a/examples/vue-query/src/gen/hooks/user/useGetUserByName.ts +++ b/examples/vue-query/src/gen/hooks/user/useGetUserByName.ts @@ -23,8 +23,7 @@ export function getUserByNameQueryOptions( return queryOptions, GetUserByNameStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getUserByName({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }) - return data + return getUserByName({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/examples/vue-query/src/gen/hooks/user/useLoginUser.ts b/examples/vue-query/src/gen/hooks/user/useLoginUser.ts index c7ca46264..8c036e36e 100644 --- a/examples/vue-query/src/gen/hooks/user/useLoginUser.ts +++ b/examples/vue-query/src/gen/hooks/user/useLoginUser.ts @@ -23,8 +23,7 @@ export function loginUserQueryOptions( return queryOptions, LoginUserStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await loginUser({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }) - return data + return loginUser({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/examples/vue-query/src/gen/hooks/user/useLogoutUser.ts b/examples/vue-query/src/gen/hooks/user/useLogoutUser.ts index 2783a0036..d5cb23843 100644 --- a/examples/vue-query/src/gen/hooks/user/useLogoutUser.ts +++ b/examples/vue-query/src/gen/hooks/user/useLogoutUser.ts @@ -17,8 +17,7 @@ export function logoutUserQueryOptions(config: Partial, LogoutUserResponse>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await logoutUser({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return logoutUser({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/internals/client/src/builders/returnStatement.test.ts b/internals/client/src/builders/returnStatement.test.ts index 27f5d005a..89a21ac21 100644 --- a/internals/client/src/builders/returnStatement.test.ts +++ b/internals/client/src/builders/returnStatement.test.ts @@ -12,10 +12,10 @@ const node = ast.factory.createOperation({ }) describe('buildReturnStatement', () => { - test('forwards the call config and casts to the operation RequestResult', () => { + test('casts to RequestResult first, then wraps the call in withUnwrap', () => { const callConfig = "{ method: 'POST', url: '/pet', ...config }" expect(buildReturnStatement({ node, types: resolverTs, callConfig })).toBe( - "return request({ method: 'POST', url: '/pet', ...config }) as Promise>", + "return withUnwrap(request({ method: 'POST', url: '/pet', ...config }) as Promise>)", ) }) }) diff --git a/internals/client/src/builders/returnStatement.ts b/internals/client/src/builders/returnStatement.ts index 19d680019..2f7687831 100644 --- a/internals/client/src/builders/returnStatement.ts +++ b/internals/client/src/builders/returnStatement.ts @@ -4,12 +4,18 @@ import { buildRequestResultGenerics } from './generics.ts' /** * Builds the return statement of a generated operation function. The runtime call already resolves - * to `{ data, error, request, response }`; the generated code forwards that result and casts it to - * the operation's `RequestResult`, which carries the `throwOnError` discrimination. + * to `{ data, error, request, response }`. The generated code casts that result to the operation's + * `RequestResult`, then wraps it with `withUnwrap`, so the caller can `await` it directly or call + * `.unwrap()` for the bare success body. + * + * Cast first, wrap second. That order keeps `withUnwrap`'s generic inferred as `RequestResult` + * instead of the runtime's own internal result type. Casting an `Unwrappable` straight to + * `Unwrappable` does not work: the `.then` overload stays pinned to `A`, and `as` rejects that + * two-generic swap even where `A` and `B` on their own would satisfy it. * * @example - * `return request({ method: 'POST', url: '/pet', ...config }) as Promise>` + * `return withUnwrap(request({ method: 'POST', url: '/pet', ...config }) as Promise>)` */ export function buildReturnStatement({ node, types, callConfig }: { node: ast.OperationNode; types: OperationTypeNames; callConfig: string }): string { - return `return request(${callConfig}) as Promise>` + return `return withUnwrap(request(${callConfig}) as Promise>)` } diff --git a/internals/client/src/builders/sdkMethod.ts b/internals/client/src/builders/sdkMethod.ts index c569a5cc6..aba529e28 100644 --- a/internals/client/src/builders/sdkMethod.ts +++ b/internals/client/src/builders/sdkMethod.ts @@ -47,8 +47,9 @@ function buildCallConfig({ /** * Builds a single instance method for a generated SDK class. The body forwards the single grouped * `options` object to the instance's own client (`this.client`, built once in the constructor) and - * returns the `RequestResult`. A per-call `options.client` still overrides the instance client, so - * one operation can be routed to a different environment without a new instance. + * returns the `Unwrappable`. A per-call `options.client` still overrides the + * instance client, so one operation can be routed to a different environment without a new + * instance. */ export function buildSdkMethod({ node, diff --git a/internals/client/src/builders/signature.test.ts b/internals/client/src/builders/signature.test.ts index a3efd0c71..a09a9b575 100644 --- a/internals/client/src/builders/signature.test.ts +++ b/internals/client/src/builders/signature.test.ts @@ -41,8 +41,8 @@ describe('buildGroupedOptionsSignature', () => { expect(signature.paramsSignature).toBe('options: Options = {}') }) - test('keys the return type on the plugin-ts per-status responses record', () => { + test('keys the return type on the plugin-ts per-status responses record, wrapped in Unwrappable', () => { const signature = buildGroupedOptionsSignature({ node: addPet, types: resolverTs }) - expect(signature.returnType).toBe('Promise>') + expect(signature.returnType).toBe('Unwrappable>') }) }) diff --git a/internals/client/src/builders/signature.ts b/internals/client/src/builders/signature.ts index efd4f12e8..d9c77a00b 100644 --- a/internals/client/src/builders/signature.ts +++ b/internals/client/src/builders/signature.ts @@ -15,7 +15,7 @@ export type GroupedOptionsSignature = { */ paramsSignature: string /** - * The function return type: `PromiseResponses, ThrowOnError>>`. + * The function return type: `UnwrappableResponses, ThrowOnError>>`. */ returnType: string /** @@ -35,7 +35,6 @@ export type GroupedOptionsSignature = { */ export function buildGroupedOptionsSignature({ node, types }: { node: ast.OperationNode; types: OperationTypeNames }): GroupedOptionsSignature { const optionsName = types.response.options(node) - const resultGenerics = buildRequestResultGenerics({ node, types }) const { isOptional } = getRequestGroupOptionality(node) const paramsSignature = @@ -47,7 +46,7 @@ export function buildGroupedOptionsSignature({ node, types }: { node: ast.Operat return { paramsSignature, - returnType: `Promise>`, + returnType: `Unwrappable>`, generics: ['ThrowOnError extends boolean = true'], } } diff --git a/internals/client/src/components/Operation.tsx b/internals/client/src/components/Operation.tsx index a1f3baa7b..86c498d9d 100644 --- a/internals/client/src/components/Operation.tsx +++ b/internals/client/src/components/Operation.tsx @@ -44,8 +44,9 @@ type Props = { /** * Renders one client operation: the grouped `Request` type and the function that forwards a - * single `options` object to the resolved client and returns the `RequestResult`. The type, signature, - * and call config are built with the AST factory, and only the jsx-renderer emits the source. + * single `options` object to the resolved client and returns the `Unwrappable`. The + * type, signature, and call config are built with the AST factory, and only the jsx-renderer emits + * the source. */ export function Operation({ name, node, types, zodResolver, validator, security, isExportable = true, isIndexable = true }: Props): KubbReactNode { if (!ast.isHttpOperationNode(node)) return null diff --git a/internals/client/src/generators/clientGenerator.tsx b/internals/client/src/generators/clientGenerator.tsx index eca24ef39..2b46e626e 100644 --- a/internals/client/src/generators/clientGenerator.tsx +++ b/internals/client/src/generators/clientGenerator.tsx @@ -14,8 +14,9 @@ import type { ContractClientFactory } from '../types.ts' * Builds the built-in per-operation generator shared by the client plugins (`@kubb/plugin-fetch`, * `@kubb/plugin-axios`). Emits one async function per OpenAPI operation using the shared * `Operation` component: a grouped `Request` type and a function that forwards a single - * `options` object to the bundled `client` and returns the `RequestResult`. Only the generator - * `name` differs between plugins; every other resolution, import, and rendering step is identical. + * `options` object to the bundled `client` and returns the `Unwrappable`. Only the + * generator `name` differs between plugins. Every other resolution, import, and rendering step is + * identical. */ export function createClientGenerator(name: string): Generator { return defineGenerator({ @@ -87,9 +88,9 @@ export function createClientGenerator(na banner={resolver.default.banner(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })} footer={resolver.default.footer(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })} > - + (): Ge return ( - - + + {validator === 'zod' && ops.some((op) => op.node.requestBody?.content?.[0]?.schema != null) && } diff --git a/internals/tanstack-query/src/components/InfiniteQueryOptions.tsx b/internals/tanstack-query/src/components/InfiniteQueryOptions.tsx index 65769576a..c9f5c1063 100644 --- a/internals/tanstack-query/src/components/InfiniteQueryOptions.tsx +++ b/internals/tanstack-query/src/components/InfiniteQueryOptions.tsx @@ -63,8 +63,7 @@ export function InfiniteQueryOptions({ const paramsNode = buildQueryOptionsParams(node, { resolver: tsResolver, memberTypeWrapper }) const paramsSignature = declarationPrinter.print(paramsNode) ?? '' - const queryFnBody = `const { data } = await ${buildClientCall(node, { clientName, signal: true, unwrapName })} - return data` + const queryFnBody = `return ${buildClientCall(node, { clientName, signal: true, unwrapName })}.unwrap()` const hasNewParams = nextParam != null || previousParam != null diff --git a/packages/plugin-axios/src/generators/__snapshots__/addPetMultiStatus/addPet.ts b/packages/plugin-axios/src/generators/__snapshots__/addPetMultiStatus/addPet.ts index 3cd33d53f..5c0782280 100644 --- a/packages/plugin-axios/src/generators/__snapshots__/addPetMultiStatus/addPet.ts +++ b/packages/plugin-axios/src/generators/__snapshots__/addPetMultiStatus/addPet.ts @@ -1,16 +1,16 @@ /* eslint-disable no-alert, no-console */ -import type { Options, RequestResult } from './.kubb/client' +import type { Options, Unwrappable, RequestResult } from './.kubb/client' import type { AddPetOptions, AddPetResponses } from './AddPet' -import { client } from './.kubb/client' +import { client, withUnwrap } from './.kubb/client' /** * {@link /pet} */ export function addPet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', ...config }) as Promise>) } diff --git a/packages/plugin-axios/src/generators/__snapshots__/addPetMultiStatusWithZod/addPet.ts b/packages/plugin-axios/src/generators/__snapshots__/addPetMultiStatusWithZod/addPet.ts index 321048af4..e3896f335 100644 --- a/packages/plugin-axios/src/generators/__snapshots__/addPetMultiStatusWithZod/addPet.ts +++ b/packages/plugin-axios/src/generators/__snapshots__/addPetMultiStatusWithZod/addPet.ts @@ -1,8 +1,8 @@ /* eslint-disable no-alert, no-console */ -import type { Options, RequestResult } from './.kubb/client' +import type { Options, Unwrappable, RequestResult } from './.kubb/client' import type { AddPetOptions, AddPetResponses } from './AddPet' -import { client } from './.kubb/client' +import { client, withUnwrap } from './.kubb/client' import { AddPetResponse } from './AddPet' /** @@ -10,8 +10,10 @@ import { AddPetResponse } from './AddPet' */ export function addPet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', validator: { response: AddPetResponse }, ...config }) as Promise> + return withUnwrap( + request({ method: 'POST', url: '/pet', validator: { response: AddPetResponse }, ...config }) as Promise>, + ) } diff --git a/packages/plugin-axios/src/generators/__snapshots__/deletePetNoContent/deletePet.ts b/packages/plugin-axios/src/generators/__snapshots__/deletePetNoContent/deletePet.ts index d03515c81..091e8543c 100644 --- a/packages/plugin-axios/src/generators/__snapshots__/deletePetNoContent/deletePet.ts +++ b/packages/plugin-axios/src/generators/__snapshots__/deletePetNoContent/deletePet.ts @@ -1,16 +1,16 @@ /* eslint-disable no-alert, no-console */ -import type { Options, RequestResult } from './.kubb/client' +import type { Options, Unwrappable, RequestResult } from './.kubb/client' import type { DeletePetOptions, DeletePetResponses } from './DeletePet' -import { client } from './.kubb/client' +import { client, withUnwrap } from './.kubb/client' /** * {@link /pet/:petId} */ export function deletePet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', ...config }) as Promise>) } diff --git a/packages/plugin-axios/src/generators/__snapshots__/findPetsByTags/findPetsByTags.ts b/packages/plugin-axios/src/generators/__snapshots__/findPetsByTags/findPetsByTags.ts index 56b72240e..9cc2198f1 100644 --- a/packages/plugin-axios/src/generators/__snapshots__/findPetsByTags/findPetsByTags.ts +++ b/packages/plugin-axios/src/generators/__snapshots__/findPetsByTags/findPetsByTags.ts @@ -1,16 +1,16 @@ /* eslint-disable no-alert, no-console */ -import type { Options, RequestResult } from './.kubb/client' +import type { Options, Unwrappable, RequestResult } from './.kubb/client' import type { FindPetsByTagsOptions, FindPetsByTagsResponses } from './FindPetsByTags' -import { client } from './.kubb/client' +import { client, withUnwrap } from './.kubb/client' /** * {@link /pet/findByTags} */ export function findPetsByTags( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByTags', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByTags', ...config }) as Promise>) } diff --git a/packages/plugin-axios/src/generators/__snapshots__/getPetById/getPetById.ts b/packages/plugin-axios/src/generators/__snapshots__/getPetById/getPetById.ts index 695457e65..81bae0783 100644 --- a/packages/plugin-axios/src/generators/__snapshots__/getPetById/getPetById.ts +++ b/packages/plugin-axios/src/generators/__snapshots__/getPetById/getPetById.ts @@ -1,16 +1,16 @@ /* eslint-disable no-alert, no-console */ -import type { Options, RequestResult } from './.kubb/client' +import type { Options, Unwrappable, RequestResult } from './.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from './GetPetById' -import { client } from './.kubb/client' +import { client, withUnwrap } from './.kubb/client' /** * {@link /pet/:petId} */ export function getPetById( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', ...config }) as Promise>) } diff --git a/packages/plugin-axios/src/generators/__snapshots__/getPetByIdWithSecurity/getPetById.ts b/packages/plugin-axios/src/generators/__snapshots__/getPetByIdWithSecurity/getPetById.ts index b0481cf25..a56ab7525 100644 --- a/packages/plugin-axios/src/generators/__snapshots__/getPetByIdWithSecurity/getPetById.ts +++ b/packages/plugin-axios/src/generators/__snapshots__/getPetByIdWithSecurity/getPetById.ts @@ -1,21 +1,20 @@ /* eslint-disable no-alert, no-console */ -import type { Options, RequestResult } from './.kubb/client' +import type { Options, Unwrappable, RequestResult } from './.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from './GetPetById' -import { client } from './.kubb/client' +import { client, withUnwrap } from './.kubb/client' /** * {@link /pet/:petId} */ export function getPetById( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/{petId}', - security: [{ type: 'oauth2' }, { type: 'apiKey', name: 'api_key', in: 'header' }], - ...config, - }) as Promise> + return withUnwrap( + request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'oauth2' }, { type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/packages/plugin-axios/src/generators/__snapshots__/getProject/getProject.ts b/packages/plugin-axios/src/generators/__snapshots__/getProject/getProject.ts index e82d50621..5738129e3 100644 --- a/packages/plugin-axios/src/generators/__snapshots__/getProject/getProject.ts +++ b/packages/plugin-axios/src/generators/__snapshots__/getProject/getProject.ts @@ -1,16 +1,16 @@ /* eslint-disable no-alert, no-console */ -import type { Options, RequestResult } from './.kubb/client' +import type { Options, Unwrappable, RequestResult } from './.kubb/client' import type { GetProjectOptions, GetProjectResponses } from './GetProject' -import { client } from './.kubb/client' +import { client, withUnwrap } from './.kubb/client' /** * {@link /projects/:project_id} */ export function getProject( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/projects/{project_id}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/projects/{project_id}', ...config }) as Promise>) } diff --git a/packages/plugin-axios/src/generators/__snapshots__/listPetsWithStyles/listPetsStyled.ts b/packages/plugin-axios/src/generators/__snapshots__/listPetsWithStyles/listPetsStyled.ts index eea2dbbc7..6c8c494a2 100644 --- a/packages/plugin-axios/src/generators/__snapshots__/listPetsWithStyles/listPetsStyled.ts +++ b/packages/plugin-axios/src/generators/__snapshots__/listPetsWithStyles/listPetsStyled.ts @@ -1,21 +1,23 @@ /* eslint-disable no-alert, no-console */ -import type { Options, RequestResult } from './.kubb/client' +import type { Options, Unwrappable, RequestResult } from './.kubb/client' import type { ListPetsStyledOptions, ListPetsStyledResponses } from './ListPetsStyled' -import { client } from './.kubb/client' +import { client, withUnwrap } from './.kubb/client' /** * {@link /pets/:petId} */ export function listPetsStyled( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pets/{petId}', - styles: { path: { petId: { style: 'matrix', explode: true } }, query: { tags: { style: 'pipeDelimited', explode: false } } }, - ...config, - }) as Promise> + return withUnwrap( + request({ + method: 'GET', + url: '/pets/{petId}', + styles: { path: { petId: { style: 'matrix', explode: true } }, query: { tags: { style: 'pipeDelimited', explode: false } } }, + ...config, + }) as Promise>, + ) } diff --git a/packages/plugin-axios/src/generators/__snapshots__/sdkClass/petClient.ts b/packages/plugin-axios/src/generators/__snapshots__/sdkClass/petClient.ts index 1955a4ead..7a1318bf1 100644 --- a/packages/plugin-axios/src/generators/__snapshots__/sdkClass/petClient.ts +++ b/packages/plugin-axios/src/generators/__snapshots__/sdkClass/petClient.ts @@ -1,9 +1,9 @@ /* eslint-disable no-alert, no-console */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from './.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from './.kubb/client' import type { DeletePetOptions, DeletePetResponses } from './DeletePet' import type { GetPetByIdOptions, GetPetByIdResponses } from './GetPetById' -import { createClient } from './.kubb/client' +import { createClient, withUnwrap } from './.kubb/client' export class PetClient { private readonly client: ClientInstance @@ -17,10 +17,10 @@ export class PetClient { */ public getPetById( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', ...config }) as Promise>) } /** @@ -28,9 +28,9 @@ export class PetClient { */ public deletePet( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', ...config }) as Promise>) } } diff --git a/packages/plugin-axios/src/generators/__snapshots__/sdkClass/projectClient.ts b/packages/plugin-axios/src/generators/__snapshots__/sdkClass/projectClient.ts index 1dd660841..68173b89e 100644 --- a/packages/plugin-axios/src/generators/__snapshots__/sdkClass/projectClient.ts +++ b/packages/plugin-axios/src/generators/__snapshots__/sdkClass/projectClient.ts @@ -1,8 +1,8 @@ /* eslint-disable no-alert, no-console */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from './.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from './.kubb/client' import type { GetProjectOptions, GetProjectResponses } from './GetProject' -import { createClient } from './.kubb/client' +import { createClient, withUnwrap } from './.kubb/client' export class ProjectClient { private readonly client: ClientInstance @@ -16,9 +16,9 @@ export class ProjectClient { */ public getProject( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/projects/{project_id}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/projects/{project_id}', ...config }) as Promise>) } } diff --git a/packages/plugin-axios/src/generators/__snapshots__/sdkClass/storeClient.ts b/packages/plugin-axios/src/generators/__snapshots__/sdkClass/storeClient.ts index 4dc93913f..aaa66f613 100644 --- a/packages/plugin-axios/src/generators/__snapshots__/sdkClass/storeClient.ts +++ b/packages/plugin-axios/src/generators/__snapshots__/sdkClass/storeClient.ts @@ -1,8 +1,8 @@ /* eslint-disable no-alert, no-console */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from './.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from './.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from './GetInventory' -import { createClient } from './.kubb/client' +import { createClient, withUnwrap } from './.kubb/client' export class StoreClient { private readonly client: ClientInstance @@ -16,9 +16,9 @@ export class StoreClient { */ public getInventory( options: Options = {}, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', ...config }) as Promise>) } } diff --git a/packages/plugin-axios/src/generators/__snapshots__/sdkClassWithName/petClient.ts b/packages/plugin-axios/src/generators/__snapshots__/sdkClassWithName/petClient.ts index 1955a4ead..7a1318bf1 100644 --- a/packages/plugin-axios/src/generators/__snapshots__/sdkClassWithName/petClient.ts +++ b/packages/plugin-axios/src/generators/__snapshots__/sdkClassWithName/petClient.ts @@ -1,9 +1,9 @@ /* eslint-disable no-alert, no-console */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from './.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from './.kubb/client' import type { DeletePetOptions, DeletePetResponses } from './DeletePet' import type { GetPetByIdOptions, GetPetByIdResponses } from './GetPetById' -import { createClient } from './.kubb/client' +import { createClient, withUnwrap } from './.kubb/client' export class PetClient { private readonly client: ClientInstance @@ -17,10 +17,10 @@ export class PetClient { */ public getPetById( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', ...config }) as Promise>) } /** @@ -28,9 +28,9 @@ export class PetClient { */ public deletePet( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', ...config }) as Promise>) } } diff --git a/packages/plugin-axios/src/generators/__snapshots__/sdkClassWithName/projectClient.ts b/packages/plugin-axios/src/generators/__snapshots__/sdkClassWithName/projectClient.ts index 1dd660841..68173b89e 100644 --- a/packages/plugin-axios/src/generators/__snapshots__/sdkClassWithName/projectClient.ts +++ b/packages/plugin-axios/src/generators/__snapshots__/sdkClassWithName/projectClient.ts @@ -1,8 +1,8 @@ /* eslint-disable no-alert, no-console */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from './.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from './.kubb/client' import type { GetProjectOptions, GetProjectResponses } from './GetProject' -import { createClient } from './.kubb/client' +import { createClient, withUnwrap } from './.kubb/client' export class ProjectClient { private readonly client: ClientInstance @@ -16,9 +16,9 @@ export class ProjectClient { */ public getProject( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/projects/{project_id}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/projects/{project_id}', ...config }) as Promise>) } } diff --git a/packages/plugin-axios/src/generators/__snapshots__/sdkClassWithName/storeClient.ts b/packages/plugin-axios/src/generators/__snapshots__/sdkClassWithName/storeClient.ts index 4dc93913f..aaa66f613 100644 --- a/packages/plugin-axios/src/generators/__snapshots__/sdkClassWithName/storeClient.ts +++ b/packages/plugin-axios/src/generators/__snapshots__/sdkClassWithName/storeClient.ts @@ -1,8 +1,8 @@ /* eslint-disable no-alert, no-console */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from './.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from './.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from './GetInventory' -import { createClient } from './.kubb/client' +import { createClient, withUnwrap } from './.kubb/client' export class StoreClient { private readonly client: ClientInstance @@ -16,9 +16,9 @@ export class StoreClient { */ public getInventory( options: Options = {}, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', ...config }) as Promise>) } } diff --git a/packages/plugin-axios/src/generators/__snapshots__/sdkSingle/petStore.ts b/packages/plugin-axios/src/generators/__snapshots__/sdkSingle/petStore.ts index fea34019d..dcbe184f5 100644 --- a/packages/plugin-axios/src/generators/__snapshots__/sdkSingle/petStore.ts +++ b/packages/plugin-axios/src/generators/__snapshots__/sdkSingle/petStore.ts @@ -1,11 +1,11 @@ /* eslint-disable no-alert, no-console */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from './.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from './.kubb/client' import type { DeletePetOptions, DeletePetResponses } from './DeletePet' import type { GetInventoryOptions, GetInventoryResponses } from './GetInventory' import type { GetPetByIdOptions, GetPetByIdResponses } from './GetPetById' import type { GetProjectOptions, GetProjectResponses } from './GetProject' -import { createClient } from './.kubb/client' +import { createClient, withUnwrap } from './.kubb/client' export class PetStore { private readonly client: ClientInstance @@ -19,10 +19,10 @@ export class PetStore { */ public getPetById( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', ...config }) as Promise>) } /** @@ -30,10 +30,10 @@ export class PetStore { */ public deletePet( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', ...config }) as Promise>) } /** @@ -41,10 +41,10 @@ export class PetStore { */ public getInventory( options: Options = {}, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', ...config }) as Promise>) } /** @@ -52,9 +52,9 @@ export class PetStore { */ public getProject( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/projects/{project_id}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/projects/{project_id}', ...config }) as Promise>) } } diff --git a/packages/plugin-axios/templates/axios.test.ts b/packages/plugin-axios/templates/axios.test.ts index 30bc1bb51..1bf430224 100644 --- a/packages/plugin-axios/templates/axios.test.ts +++ b/packages/plugin-axios/templates/axios.test.ts @@ -1,6 +1,6 @@ import type { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios' import { describe, expect, test, vi } from 'vitest' -import { type CallResult, createClientCore, parseEventStream, ResponseError, resolveAuth } from './axios.ts' +import { type CallResult, createClientCore, parseEventStream, ResponseError, resolveAuth, withUnwrap } from './axios.ts' import { applyHeaderStyles, defaultBodySerializer, defaultPathSerializer, defaultQuerySerializer, serializeCookies } from './serializers.ts' type Programmed = { data?: unknown; status?: number; statusText?: string } @@ -716,6 +716,24 @@ describe('getUrl', () => { }) }) +describe('withUnwrap', () => { + test('unwrap() resolves to the success data', async () => { + const result = await withUnwrap(Promise.resolve({ data: { id: 1 }, error: undefined })).unwrap() + expect(result).toStrictEqual({ id: 1 }) + }) + + test('unwrap() rejects with error for a non-throwing error result', async () => { + await expect(withUnwrap(Promise.resolve({ data: undefined, error: { message: 'not found' } })).unwrap()).rejects.toStrictEqual({ + message: 'not found', + }) + }) + + test('a rejected call propagates the rejection unchanged', async () => { + const error = new ResponseError({ data: { message: 'not found' }, status: 404, statusText: 'Not Found', request: {}, response: {} }) + await expect(withUnwrap(Promise.reject(error)).unwrap()).rejects.toBe(error) + }) +}) + describe('resolveAuth', () => { test('places a bearer token on the Authorization header', async () => { const headers: Record = {} diff --git a/packages/plugin-axios/templates/axios.ts b/packages/plugin-axios/templates/axios.ts index d928c18f3..9ccace88f 100644 --- a/packages/plugin-axios/templates/axios.ts +++ b/packages/plugin-axios/templates/axios.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/packages/plugin-fetch/src/generators/__snapshots__/addPetMultiStatus/addPet.ts b/packages/plugin-fetch/src/generators/__snapshots__/addPetMultiStatus/addPet.ts index 3cd33d53f..5c0782280 100644 --- a/packages/plugin-fetch/src/generators/__snapshots__/addPetMultiStatus/addPet.ts +++ b/packages/plugin-fetch/src/generators/__snapshots__/addPetMultiStatus/addPet.ts @@ -1,16 +1,16 @@ /* eslint-disable no-alert, no-console */ -import type { Options, RequestResult } from './.kubb/client' +import type { Options, Unwrappable, RequestResult } from './.kubb/client' import type { AddPetOptions, AddPetResponses } from './AddPet' -import { client } from './.kubb/client' +import { client, withUnwrap } from './.kubb/client' /** * {@link /pet} */ export function addPet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', ...config }) as Promise>) } diff --git a/packages/plugin-fetch/src/generators/__snapshots__/addPetMultiStatusWithZod/addPet.ts b/packages/plugin-fetch/src/generators/__snapshots__/addPetMultiStatusWithZod/addPet.ts index 321048af4..e3896f335 100644 --- a/packages/plugin-fetch/src/generators/__snapshots__/addPetMultiStatusWithZod/addPet.ts +++ b/packages/plugin-fetch/src/generators/__snapshots__/addPetMultiStatusWithZod/addPet.ts @@ -1,8 +1,8 @@ /* eslint-disable no-alert, no-console */ -import type { Options, RequestResult } from './.kubb/client' +import type { Options, Unwrappable, RequestResult } from './.kubb/client' import type { AddPetOptions, AddPetResponses } from './AddPet' -import { client } from './.kubb/client' +import { client, withUnwrap } from './.kubb/client' import { AddPetResponse } from './AddPet' /** @@ -10,8 +10,10 @@ import { AddPetResponse } from './AddPet' */ export function addPet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', validator: { response: AddPetResponse }, ...config }) as Promise> + return withUnwrap( + request({ method: 'POST', url: '/pet', validator: { response: AddPetResponse }, ...config }) as Promise>, + ) } diff --git a/packages/plugin-fetch/src/generators/__snapshots__/addPetWithSecurity/addPet.ts b/packages/plugin-fetch/src/generators/__snapshots__/addPetWithSecurity/addPet.ts index 37bea9794..173260630 100644 --- a/packages/plugin-fetch/src/generators/__snapshots__/addPetWithSecurity/addPet.ts +++ b/packages/plugin-fetch/src/generators/__snapshots__/addPetWithSecurity/addPet.ts @@ -1,16 +1,18 @@ /* eslint-disable no-alert, no-console */ -import type { Options, RequestResult } from './.kubb/client' +import type { Options, Unwrappable, RequestResult } from './.kubb/client' import type { AddPetOptions, AddPetResponses } from './AddPet' -import { client } from './.kubb/client' +import { client, withUnwrap } from './.kubb/client' /** * {@link /pet} */ export function addPet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap( + request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>, + ) } diff --git a/packages/plugin-fetch/src/generators/__snapshots__/deletePetNoContent/deletePet.ts b/packages/plugin-fetch/src/generators/__snapshots__/deletePetNoContent/deletePet.ts index d03515c81..091e8543c 100644 --- a/packages/plugin-fetch/src/generators/__snapshots__/deletePetNoContent/deletePet.ts +++ b/packages/plugin-fetch/src/generators/__snapshots__/deletePetNoContent/deletePet.ts @@ -1,16 +1,16 @@ /* eslint-disable no-alert, no-console */ -import type { Options, RequestResult } from './.kubb/client' +import type { Options, Unwrappable, RequestResult } from './.kubb/client' import type { DeletePetOptions, DeletePetResponses } from './DeletePet' -import { client } from './.kubb/client' +import { client, withUnwrap } from './.kubb/client' /** * {@link /pet/:petId} */ export function deletePet( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', ...config }) as Promise>) } diff --git a/packages/plugin-fetch/src/generators/__snapshots__/findPetsByTags/findPetsByTags.ts b/packages/plugin-fetch/src/generators/__snapshots__/findPetsByTags/findPetsByTags.ts index 56b72240e..9cc2198f1 100644 --- a/packages/plugin-fetch/src/generators/__snapshots__/findPetsByTags/findPetsByTags.ts +++ b/packages/plugin-fetch/src/generators/__snapshots__/findPetsByTags/findPetsByTags.ts @@ -1,16 +1,16 @@ /* eslint-disable no-alert, no-console */ -import type { Options, RequestResult } from './.kubb/client' +import type { Options, Unwrappable, RequestResult } from './.kubb/client' import type { FindPetsByTagsOptions, FindPetsByTagsResponses } from './FindPetsByTags' -import { client } from './.kubb/client' +import { client, withUnwrap } from './.kubb/client' /** * {@link /pet/findByTags} */ export function findPetsByTags( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByTags', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByTags', ...config }) as Promise>) } diff --git a/packages/plugin-fetch/src/generators/__snapshots__/findPetsByTagsWithGlobalSecurity/findPetsByTags.ts b/packages/plugin-fetch/src/generators/__snapshots__/findPetsByTagsWithGlobalSecurity/findPetsByTags.ts index 568c2b5bf..398a970a8 100644 --- a/packages/plugin-fetch/src/generators/__snapshots__/findPetsByTagsWithGlobalSecurity/findPetsByTags.ts +++ b/packages/plugin-fetch/src/generators/__snapshots__/findPetsByTagsWithGlobalSecurity/findPetsByTags.ts @@ -1,18 +1,20 @@ /* eslint-disable no-alert, no-console */ -import type { Options, RequestResult } from './.kubb/client' +import type { Options, Unwrappable, RequestResult } from './.kubb/client' import type { FindPetsByTagsOptions, FindPetsByTagsResponses } from './FindPetsByTags' -import { client } from './.kubb/client' +import { client, withUnwrap } from './.kubb/client' /** * {@link /pet/findByTags} */ export function findPetsByTags( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByTags', security: [{ type: 'http', scheme: 'bearer' }], ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'GET', url: '/pet/findByTags', security: [{ type: 'http', scheme: 'bearer' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/packages/plugin-fetch/src/generators/__snapshots__/getPetById/getPetById.ts b/packages/plugin-fetch/src/generators/__snapshots__/getPetById/getPetById.ts index 695457e65..81bae0783 100644 --- a/packages/plugin-fetch/src/generators/__snapshots__/getPetById/getPetById.ts +++ b/packages/plugin-fetch/src/generators/__snapshots__/getPetById/getPetById.ts @@ -1,16 +1,16 @@ /* eslint-disable no-alert, no-console */ -import type { Options, RequestResult } from './.kubb/client' +import type { Options, Unwrappable, RequestResult } from './.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from './GetPetById' -import { client } from './.kubb/client' +import { client, withUnwrap } from './.kubb/client' /** * {@link /pet/:petId} */ export function getPetById( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', ...config }) as Promise>) } diff --git a/packages/plugin-fetch/src/generators/__snapshots__/getPetByIdWithMultipleSchemes/getPetById.ts b/packages/plugin-fetch/src/generators/__snapshots__/getPetByIdWithMultipleSchemes/getPetById.ts index b0481cf25..a56ab7525 100644 --- a/packages/plugin-fetch/src/generators/__snapshots__/getPetByIdWithMultipleSchemes/getPetById.ts +++ b/packages/plugin-fetch/src/generators/__snapshots__/getPetByIdWithMultipleSchemes/getPetById.ts @@ -1,21 +1,20 @@ /* eslint-disable no-alert, no-console */ -import type { Options, RequestResult } from './.kubb/client' +import type { Options, Unwrappable, RequestResult } from './.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from './GetPetById' -import { client } from './.kubb/client' +import { client, withUnwrap } from './.kubb/client' /** * {@link /pet/:petId} */ export function getPetById( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pet/{petId}', - security: [{ type: 'oauth2' }, { type: 'apiKey', name: 'api_key', in: 'header' }], - ...config, - }) as Promise> + return withUnwrap( + request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'oauth2' }, { type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise< + RequestResult + >, + ) } diff --git a/packages/plugin-fetch/src/generators/__snapshots__/getProject/getProject.ts b/packages/plugin-fetch/src/generators/__snapshots__/getProject/getProject.ts index e82d50621..5738129e3 100644 --- a/packages/plugin-fetch/src/generators/__snapshots__/getProject/getProject.ts +++ b/packages/plugin-fetch/src/generators/__snapshots__/getProject/getProject.ts @@ -1,16 +1,16 @@ /* eslint-disable no-alert, no-console */ -import type { Options, RequestResult } from './.kubb/client' +import type { Options, Unwrappable, RequestResult } from './.kubb/client' import type { GetProjectOptions, GetProjectResponses } from './GetProject' -import { client } from './.kubb/client' +import { client, withUnwrap } from './.kubb/client' /** * {@link /projects/:project_id} */ export function getProject( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/projects/{project_id}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/projects/{project_id}', ...config }) as Promise>) } diff --git a/packages/plugin-fetch/src/generators/__snapshots__/listPetsWithStyles/listPetsStyled.ts b/packages/plugin-fetch/src/generators/__snapshots__/listPetsWithStyles/listPetsStyled.ts index eea2dbbc7..6c8c494a2 100644 --- a/packages/plugin-fetch/src/generators/__snapshots__/listPetsWithStyles/listPetsStyled.ts +++ b/packages/plugin-fetch/src/generators/__snapshots__/listPetsWithStyles/listPetsStyled.ts @@ -1,21 +1,23 @@ /* eslint-disable no-alert, no-console */ -import type { Options, RequestResult } from './.kubb/client' +import type { Options, Unwrappable, RequestResult } from './.kubb/client' import type { ListPetsStyledOptions, ListPetsStyledResponses } from './ListPetsStyled' -import { client } from './.kubb/client' +import { client, withUnwrap } from './.kubb/client' /** * {@link /pets/:petId} */ export function listPetsStyled( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ - method: 'GET', - url: '/pets/{petId}', - styles: { path: { petId: { style: 'matrix', explode: true } }, query: { tags: { style: 'pipeDelimited', explode: false } } }, - ...config, - }) as Promise> + return withUnwrap( + request({ + method: 'GET', + url: '/pets/{petId}', + styles: { path: { petId: { style: 'matrix', explode: true } }, query: { tags: { style: 'pipeDelimited', explode: false } } }, + ...config, + }) as Promise>, + ) } diff --git a/packages/plugin-fetch/src/generators/__snapshots__/sdkClass/petClient.ts b/packages/plugin-fetch/src/generators/__snapshots__/sdkClass/petClient.ts index 1955a4ead..7a1318bf1 100644 --- a/packages/plugin-fetch/src/generators/__snapshots__/sdkClass/petClient.ts +++ b/packages/plugin-fetch/src/generators/__snapshots__/sdkClass/petClient.ts @@ -1,9 +1,9 @@ /* eslint-disable no-alert, no-console */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from './.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from './.kubb/client' import type { DeletePetOptions, DeletePetResponses } from './DeletePet' import type { GetPetByIdOptions, GetPetByIdResponses } from './GetPetById' -import { createClient } from './.kubb/client' +import { createClient, withUnwrap } from './.kubb/client' export class PetClient { private readonly client: ClientInstance @@ -17,10 +17,10 @@ export class PetClient { */ public getPetById( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', ...config }) as Promise>) } /** @@ -28,9 +28,9 @@ export class PetClient { */ public deletePet( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', ...config }) as Promise>) } } diff --git a/packages/plugin-fetch/src/generators/__snapshots__/sdkClass/projectClient.ts b/packages/plugin-fetch/src/generators/__snapshots__/sdkClass/projectClient.ts index 1dd660841..68173b89e 100644 --- a/packages/plugin-fetch/src/generators/__snapshots__/sdkClass/projectClient.ts +++ b/packages/plugin-fetch/src/generators/__snapshots__/sdkClass/projectClient.ts @@ -1,8 +1,8 @@ /* eslint-disable no-alert, no-console */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from './.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from './.kubb/client' import type { GetProjectOptions, GetProjectResponses } from './GetProject' -import { createClient } from './.kubb/client' +import { createClient, withUnwrap } from './.kubb/client' export class ProjectClient { private readonly client: ClientInstance @@ -16,9 +16,9 @@ export class ProjectClient { */ public getProject( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/projects/{project_id}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/projects/{project_id}', ...config }) as Promise>) } } diff --git a/packages/plugin-fetch/src/generators/__snapshots__/sdkClass/storeClient.ts b/packages/plugin-fetch/src/generators/__snapshots__/sdkClass/storeClient.ts index 4dc93913f..aaa66f613 100644 --- a/packages/plugin-fetch/src/generators/__snapshots__/sdkClass/storeClient.ts +++ b/packages/plugin-fetch/src/generators/__snapshots__/sdkClass/storeClient.ts @@ -1,8 +1,8 @@ /* eslint-disable no-alert, no-console */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from './.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from './.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from './GetInventory' -import { createClient } from './.kubb/client' +import { createClient, withUnwrap } from './.kubb/client' export class StoreClient { private readonly client: ClientInstance @@ -16,9 +16,9 @@ export class StoreClient { */ public getInventory( options: Options = {}, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', ...config }) as Promise>) } } diff --git a/packages/plugin-fetch/src/generators/__snapshots__/sdkClassWithName/petClient.ts b/packages/plugin-fetch/src/generators/__snapshots__/sdkClassWithName/petClient.ts index 1955a4ead..7a1318bf1 100644 --- a/packages/plugin-fetch/src/generators/__snapshots__/sdkClassWithName/petClient.ts +++ b/packages/plugin-fetch/src/generators/__snapshots__/sdkClassWithName/petClient.ts @@ -1,9 +1,9 @@ /* eslint-disable no-alert, no-console */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from './.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from './.kubb/client' import type { DeletePetOptions, DeletePetResponses } from './DeletePet' import type { GetPetByIdOptions, GetPetByIdResponses } from './GetPetById' -import { createClient } from './.kubb/client' +import { createClient, withUnwrap } from './.kubb/client' export class PetClient { private readonly client: ClientInstance @@ -17,10 +17,10 @@ export class PetClient { */ public getPetById( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', ...config }) as Promise>) } /** @@ -28,9 +28,9 @@ export class PetClient { */ public deletePet( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', ...config }) as Promise>) } } diff --git a/packages/plugin-fetch/src/generators/__snapshots__/sdkClassWithName/projectClient.ts b/packages/plugin-fetch/src/generators/__snapshots__/sdkClassWithName/projectClient.ts index 1dd660841..68173b89e 100644 --- a/packages/plugin-fetch/src/generators/__snapshots__/sdkClassWithName/projectClient.ts +++ b/packages/plugin-fetch/src/generators/__snapshots__/sdkClassWithName/projectClient.ts @@ -1,8 +1,8 @@ /* eslint-disable no-alert, no-console */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from './.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from './.kubb/client' import type { GetProjectOptions, GetProjectResponses } from './GetProject' -import { createClient } from './.kubb/client' +import { createClient, withUnwrap } from './.kubb/client' export class ProjectClient { private readonly client: ClientInstance @@ -16,9 +16,9 @@ export class ProjectClient { */ public getProject( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/projects/{project_id}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/projects/{project_id}', ...config }) as Promise>) } } diff --git a/packages/plugin-fetch/src/generators/__snapshots__/sdkClassWithName/storeClient.ts b/packages/plugin-fetch/src/generators/__snapshots__/sdkClassWithName/storeClient.ts index 4dc93913f..aaa66f613 100644 --- a/packages/plugin-fetch/src/generators/__snapshots__/sdkClassWithName/storeClient.ts +++ b/packages/plugin-fetch/src/generators/__snapshots__/sdkClassWithName/storeClient.ts @@ -1,8 +1,8 @@ /* eslint-disable no-alert, no-console */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from './.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from './.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from './GetInventory' -import { createClient } from './.kubb/client' +import { createClient, withUnwrap } from './.kubb/client' export class StoreClient { private readonly client: ClientInstance @@ -16,9 +16,9 @@ export class StoreClient { */ public getInventory( options: Options = {}, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', ...config }) as Promise>) } } diff --git a/packages/plugin-fetch/src/generators/__snapshots__/sdkClassWithSecurity/petStore.ts b/packages/plugin-fetch/src/generators/__snapshots__/sdkClassWithSecurity/petStore.ts index df57dc8d8..18a4e34ce 100644 --- a/packages/plugin-fetch/src/generators/__snapshots__/sdkClassWithSecurity/petStore.ts +++ b/packages/plugin-fetch/src/generators/__snapshots__/sdkClassWithSecurity/petStore.ts @@ -1,11 +1,11 @@ /* eslint-disable no-alert, no-console */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from './.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from './.kubb/client' import type { DeletePetOptions, DeletePetResponses } from './DeletePet' import type { GetInventoryOptions, GetInventoryResponses } from './GetInventory' import type { GetPetByIdOptions, GetPetByIdResponses } from './GetPetById' import type { GetProjectOptions, GetProjectResponses } from './GetProject' -import { createClient } from './.kubb/client' +import { createClient, withUnwrap } from './.kubb/client' export class PetStore { private readonly client: ClientInstance @@ -19,15 +19,14 @@ export class PetStore { */ public getPetById( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ - method: 'GET', - url: '/pet/{petId}', - security: [{ type: 'oauth2' }, { type: 'apiKey', name: 'api_key', in: 'header' }], - ...config, - }) as Promise> + return withUnwrap( + request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'oauth2' }, { type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise< + RequestResult + >, + ) } /** @@ -35,10 +34,10 @@ export class PetStore { */ public deletePet( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', ...config }) as Promise>) } /** @@ -46,10 +45,10 @@ export class PetStore { */ public getInventory( options: Options = {}, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', ...config }) as Promise>) } /** @@ -57,9 +56,9 @@ export class PetStore { */ public getProject( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/projects/{project_id}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/projects/{project_id}', ...config }) as Promise>) } } diff --git a/packages/plugin-fetch/src/generators/__snapshots__/sdkSingle/petStore.ts b/packages/plugin-fetch/src/generators/__snapshots__/sdkSingle/petStore.ts index fea34019d..dcbe184f5 100644 --- a/packages/plugin-fetch/src/generators/__snapshots__/sdkSingle/petStore.ts +++ b/packages/plugin-fetch/src/generators/__snapshots__/sdkSingle/petStore.ts @@ -1,11 +1,11 @@ /* eslint-disable no-alert, no-console */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from './.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from './.kubb/client' import type { DeletePetOptions, DeletePetResponses } from './DeletePet' import type { GetInventoryOptions, GetInventoryResponses } from './GetInventory' import type { GetPetByIdOptions, GetPetByIdResponses } from './GetPetById' import type { GetProjectOptions, GetProjectResponses } from './GetProject' -import { createClient } from './.kubb/client' +import { createClient, withUnwrap } from './.kubb/client' export class PetStore { private readonly client: ClientInstance @@ -19,10 +19,10 @@ export class PetStore { */ public getPetById( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', ...config }) as Promise>) } /** @@ -30,10 +30,10 @@ export class PetStore { */ public deletePet( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', ...config }) as Promise>) } /** @@ -41,10 +41,10 @@ export class PetStore { */ public getInventory( options: Options = {}, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', ...config }) as Promise>) } /** @@ -52,9 +52,9 @@ export class PetStore { */ public getProject( options: Options, - ): Promise> { + ): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/projects/{project_id}', ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/projects/{project_id}', ...config }) as Promise>) } } diff --git a/packages/plugin-fetch/src/generators/__snapshots__/uploadFileMultipart/uploadFile.ts b/packages/plugin-fetch/src/generators/__snapshots__/uploadFileMultipart/uploadFile.ts index 55cddb6e0..11ba6bbad 100644 --- a/packages/plugin-fetch/src/generators/__snapshots__/uploadFileMultipart/uploadFile.ts +++ b/packages/plugin-fetch/src/generators/__snapshots__/uploadFileMultipart/uploadFile.ts @@ -1,18 +1,20 @@ /* eslint-disable no-alert, no-console */ -import type { Options, RequestResult } from './.kubb/client' +import type { Options, Unwrappable, RequestResult } from './.kubb/client' import type { UploadFileOptions, UploadFileResponses } from './UploadFile' -import { client } from './.kubb/client' +import { client, withUnwrap } from './.kubb/client' /** * {@link /pet/:petId/uploadImage} */ export function uploadFile( options: Options, -): Promise> { +): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', contentType: { request: 'multipart/form-data' }, ...config }) as Promise< - RequestResult - > + return withUnwrap( + request({ method: 'POST', url: '/pet/{petId}/uploadImage', contentType: { request: 'multipart/form-data' }, ...config }) as Promise< + RequestResult + >, + ) } diff --git a/packages/plugin-fetch/templates/fetch.test.ts b/packages/plugin-fetch/templates/fetch.test.ts index e2dfc90b9..2dbff01ae 100644 --- a/packages/plugin-fetch/templates/fetch.test.ts +++ b/packages/plugin-fetch/templates/fetch.test.ts @@ -10,6 +10,7 @@ import { type ServerSentEvent, type Transport, type TransportResult, + withUnwrap, } from './fetch.ts' import { applyHeaderStyles, defaultBodySerializer, defaultPathSerializer, defaultQuerySerializer, serializeCookies } from './serializers.ts' @@ -649,6 +650,22 @@ describe('getUrl', () => { }) }) +describe('withUnwrap', () => { + test('unwrap() resolves to the success data', async () => { + const result = await withUnwrap(Promise.resolve({ data: { id: 1 }, error: undefined })).unwrap() + expect(result).toStrictEqual({ id: 1 }) + }) + + test('unwrap() rejects with error for a non-throwing error result', async () => { + await expect(withUnwrap(Promise.resolve({ data: undefined, error: { message: 'invalid' } })).unwrap()).rejects.toStrictEqual({ message: 'invalid' }) + }) + + test('a rejected call propagates the rejection unchanged', async () => { + const error = new ResponseError({ data: { message: 'invalid' }, status: 405, statusText: 'Method Not Allowed', request: 'REQ', response: 'RES' }) + await expect(withUnwrap(Promise.reject(error)).unwrap()).rejects.toBe(error) + }) +}) + describe('resolveAuth', () => { test('places a bearer token on the Authorization header', async () => { const headers: Record = {} diff --git a/packages/plugin-fetch/templates/fetch.ts b/packages/plugin-fetch/templates/fetch.ts index f80182aca..ea6223eff 100644 --- a/packages/plugin-fetch/templates/fetch.ts +++ b/packages/plugin-fetch/templates/fetch.ts @@ -82,6 +82,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/packages/plugin-react-query/src/components/MutationOptions.tsx b/packages/plugin-react-query/src/components/MutationOptions.tsx index d5e36d5b2..66a271483 100644 --- a/packages/plugin-react-query/src/components/MutationOptions.tsx +++ b/packages/plugin-react-query/src/components/MutationOptions.tsx @@ -41,8 +41,7 @@ export function MutationOptions({ name, clientName, node, tsResolver, mutationKe const groupedParamsNode = createFunctionParameters({ params: groupedParam ? [groupedParam] : [] }) const TRequest = hasMutationParams ? tsResolver.response.options(node) : 'undefined' const argBindingStr = hasMutationParams ? (callPrinter.print(groupedParamsNode) ?? '') : '_' - const mutationFnBody = `const { data } = await ${buildClientCall(node, { clientName, signal: false })} - return data` + const mutationFnBody = `return ${buildClientCall(node, { clientName, signal: false })}.unwrap()` return ( diff --git a/packages/plugin-react-query/src/components/QueryOptions.tsx b/packages/plugin-react-query/src/components/QueryOptions.tsx index 7fff89fd5..c17c67431 100644 --- a/packages/plugin-react-query/src/components/QueryOptions.tsx +++ b/packages/plugin-react-query/src/components/QueryOptions.tsx @@ -25,8 +25,7 @@ export function QueryOptions({ name, clientName, node, tsResolver, queryKeyName const paramsNode = buildQueryOptionsParams(node, { resolver: tsResolver }) const paramsSignature = declarationPrinter.print(paramsNode) ?? '' - const queryFnBody = `const { data } = await ${buildClientCall(node, { clientName, signal: true })} - return data` + const queryFnBody = `return ${buildClientCall(node, { clientName, signal: true })}.unwrap()` return ( diff --git a/packages/plugin-react-query/src/generators/__snapshots__/clientPostImportPath/useFindPetsByTags.ts b/packages/plugin-react-query/src/generators/__snapshots__/clientPostImportPath/useFindPetsByTags.ts index e08ba0ce5..b66ab985b 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/clientPostImportPath/useFindPetsByTags.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/clientPostImportPath/useFindPetsByTags.ts @@ -22,8 +22,7 @@ export function findPetsByTagsQueryOptions( return queryOptions, FindPetsByTagsStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/packages/plugin-react-query/src/generators/__snapshots__/clientPostImportPath/useFindPetsByTagsInfinite.ts b/packages/plugin-react-query/src/generators/__snapshots__/clientPostImportPath/useFindPetsByTagsInfinite.ts index 860876fc0..a012a03c7 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/clientPostImportPath/useFindPetsByTagsInfinite.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/clientPostImportPath/useFindPetsByTagsInfinite.ts @@ -33,8 +33,7 @@ export function findPetsByTagsInfiniteQueryOptions( ...(query ?? {}), ['pageSize']: pageParam as unknown as FindPetsByTagsQuery['pageSize'], } as FindPetsByTagsQuery - const { data } = await findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, initialPageParam: 0, getNextPageParam: (lastPage, _allPages, lastPageParam) => (Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1), diff --git a/packages/plugin-react-query/src/generators/__snapshots__/clientPostImportPath/useFindPetsByTagsSuspense.ts b/packages/plugin-react-query/src/generators/__snapshots__/clientPostImportPath/useFindPetsByTagsSuspense.ts index d054f8643..6e98c9671 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/clientPostImportPath/useFindPetsByTagsSuspense.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/clientPostImportPath/useFindPetsByTagsSuspense.ts @@ -23,8 +23,7 @@ export function findPetsByTagsSuspenseQueryOptions( return queryOptions, FindPetsByTagsStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/packages/plugin-react-query/src/generators/__snapshots__/clientPostImportPath/useFindPetsByTagsSuspenseInfinite.ts b/packages/plugin-react-query/src/generators/__snapshots__/clientPostImportPath/useFindPetsByTagsSuspenseInfinite.ts index 2aa308155..6b1feae67 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/clientPostImportPath/useFindPetsByTagsSuspenseInfinite.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/clientPostImportPath/useFindPetsByTagsSuspenseInfinite.ts @@ -33,8 +33,7 @@ export function findPetsByTagsSuspenseInfiniteQueryOptions( ...(query ?? {}), ['pageSize']: pageParam as unknown as FindPetsByTagsQuery['pageSize'], } as FindPetsByTagsQuery - const { data } = await findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, initialPageParam: 0, getNextPageParam: (lastPage, _allPages, lastPageParam) => (Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1), diff --git a/packages/plugin-react-query/src/generators/__snapshots__/clientPostImportPath/useUpdatePetWithForm.ts b/packages/plugin-react-query/src/generators/__snapshots__/clientPostImportPath/useUpdatePetWithForm.ts index cd96517c7..db709c4ea 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/clientPostImportPath/useUpdatePetWithForm.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/clientPostImportPath/useUpdatePetWithForm.ts @@ -17,8 +17,7 @@ export function updatePetWithFormMutationOptions(config: Par return mutationOptions, UpdatePetWithFormOptions, TContext>({ mutationKey, mutationFn: async ({ path, body }) => { - const { data } = await updatePetWithForm({ ...config, path, body, throwOnError: true }) - return data + return updatePetWithForm({ ...config, path, body, throwOnError: true }).unwrap() }, }) } diff --git a/packages/plugin-react-query/src/generators/__snapshots__/createUsersWithListInputAsQuery/useCreateUsersWithListInput.ts b/packages/plugin-react-query/src/generators/__snapshots__/createUsersWithListInputAsQuery/useCreateUsersWithListInput.ts index fef6408a4..f904e4d46 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/createUsersWithListInputAsQuery/useCreateUsersWithListInput.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/createUsersWithListInputAsQuery/useCreateUsersWithListInput.ts @@ -23,8 +23,7 @@ export function createUsersWithListInputQueryOptions( return queryOptions, CreateUsersWithListInputStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await createUsersWithListInput({ ...config, body, signal: config.signal ?? signal, throwOnError: true }) - return data + return createUsersWithListInput({ ...config, body, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/packages/plugin-react-query/src/generators/__snapshots__/deletePet/useDeletePet.ts b/packages/plugin-react-query/src/generators/__snapshots__/deletePet/useDeletePet.ts index edd861310..ad704e4ff 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/deletePet/useDeletePet.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/deletePet/useDeletePet.ts @@ -17,8 +17,7 @@ export function deletePetMutationOptions(config: Partial, DeletePetOptions, TContext>({ mutationKey, mutationFn: async ({ path, headers }) => { - const { data } = await deletePet({ ...config, path, headers, throwOnError: true }) - return data + return deletePet({ ...config, path, headers, throwOnError: true }).unwrap() }, }) } diff --git a/packages/plugin-react-query/src/generators/__snapshots__/findByStatusAllOptional/useFindPetsByStatus.ts b/packages/plugin-react-query/src/generators/__snapshots__/findByStatusAllOptional/useFindPetsByStatus.ts index 12622d602..622840094 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/findByStatusAllOptional/useFindPetsByStatus.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/findByStatusAllOptional/useFindPetsByStatus.ts @@ -23,8 +23,7 @@ export function findPetsByStatusQueryOptions( return queryOptions, FindPetsByStatusStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/packages/plugin-react-query/src/generators/__snapshots__/findByTags/useFindPetsByTags.ts b/packages/plugin-react-query/src/generators/__snapshots__/findByTags/useFindPetsByTags.ts index e08ba0ce5..b66ab985b 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/findByTags/useFindPetsByTags.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/findByTags/useFindPetsByTags.ts @@ -22,8 +22,7 @@ export function findPetsByTagsQueryOptions( return queryOptions, FindPetsByTagsStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/packages/plugin-react-query/src/generators/__snapshots__/findByTags/useFindPetsByTagsInfinite.ts b/packages/plugin-react-query/src/generators/__snapshots__/findByTags/useFindPetsByTagsInfinite.ts index 860876fc0..a012a03c7 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/findByTags/useFindPetsByTagsInfinite.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/findByTags/useFindPetsByTagsInfinite.ts @@ -33,8 +33,7 @@ export function findPetsByTagsInfiniteQueryOptions( ...(query ?? {}), ['pageSize']: pageParam as unknown as FindPetsByTagsQuery['pageSize'], } as FindPetsByTagsQuery - const { data } = await findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, initialPageParam: 0, getNextPageParam: (lastPage, _allPages, lastPageParam) => (Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1), diff --git a/packages/plugin-react-query/src/generators/__snapshots__/findByTags/useFindPetsByTagsSuspense.ts b/packages/plugin-react-query/src/generators/__snapshots__/findByTags/useFindPetsByTagsSuspense.ts index d054f8643..6e98c9671 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/findByTags/useFindPetsByTagsSuspense.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/findByTags/useFindPetsByTagsSuspense.ts @@ -23,8 +23,7 @@ export function findPetsByTagsSuspenseQueryOptions( return queryOptions, FindPetsByTagsStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/packages/plugin-react-query/src/generators/__snapshots__/findByTags/useFindPetsByTagsSuspenseInfinite.ts b/packages/plugin-react-query/src/generators/__snapshots__/findByTags/useFindPetsByTagsSuspenseInfinite.ts index 2aa308155..6b1feae67 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/findByTags/useFindPetsByTagsSuspenseInfinite.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/findByTags/useFindPetsByTagsSuspenseInfinite.ts @@ -33,8 +33,7 @@ export function findPetsByTagsSuspenseInfiniteQueryOptions( ...(query ?? {}), ['pageSize']: pageParam as unknown as FindPetsByTagsQuery['pageSize'], } as FindPetsByTagsQuery - const { data } = await findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, initialPageParam: 0, getNextPageParam: (lastPage, _allPages, lastPageParam) => (Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1), diff --git a/packages/plugin-react-query/src/generators/__snapshots__/findByTagsObject/useFindPetsByTagsInfinite.ts b/packages/plugin-react-query/src/generators/__snapshots__/findByTagsObject/useFindPetsByTagsInfinite.ts index 860876fc0..a012a03c7 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/findByTagsObject/useFindPetsByTagsInfinite.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/findByTagsObject/useFindPetsByTagsInfinite.ts @@ -33,8 +33,7 @@ export function findPetsByTagsInfiniteQueryOptions( ...(query ?? {}), ['pageSize']: pageParam as unknown as FindPetsByTagsQuery['pageSize'], } as FindPetsByTagsQuery - const { data } = await findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, initialPageParam: 0, getNextPageParam: (lastPage, _allPages, lastPageParam) => (Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1), diff --git a/packages/plugin-react-query/src/generators/__snapshots__/findByTagsObject/useFindPetsByTagsSuspense.ts b/packages/plugin-react-query/src/generators/__snapshots__/findByTagsObject/useFindPetsByTagsSuspense.ts index d054f8643..6e98c9671 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/findByTagsObject/useFindPetsByTagsSuspense.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/findByTagsObject/useFindPetsByTagsSuspense.ts @@ -23,8 +23,7 @@ export function findPetsByTagsSuspenseQueryOptions( return queryOptions, FindPetsByTagsStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/packages/plugin-react-query/src/generators/__snapshots__/findByTagsObject/useFindPetsByTagsSuspenseInfinite.ts b/packages/plugin-react-query/src/generators/__snapshots__/findByTagsObject/useFindPetsByTagsSuspenseInfinite.ts index 2aa308155..6b1feae67 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/findByTagsObject/useFindPetsByTagsSuspenseInfinite.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/findByTagsObject/useFindPetsByTagsSuspenseInfinite.ts @@ -33,8 +33,7 @@ export function findPetsByTagsSuspenseInfiniteQueryOptions( ...(query ?? {}), ['pageSize']: pageParam as unknown as FindPetsByTagsQuery['pageSize'], } as FindPetsByTagsQuery - const { data } = await findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, initialPageParam: 0, getNextPageParam: (lastPage, _allPages, lastPageParam) => (Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1), diff --git a/packages/plugin-react-query/src/generators/__snapshots__/findByTagsTemplateString/useFindPetsByTags.ts b/packages/plugin-react-query/src/generators/__snapshots__/findByTagsTemplateString/useFindPetsByTags.ts index e08ba0ce5..b66ab985b 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/findByTagsTemplateString/useFindPetsByTags.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/findByTagsTemplateString/useFindPetsByTags.ts @@ -22,8 +22,7 @@ export function findPetsByTagsQueryOptions( return queryOptions, FindPetsByTagsStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/packages/plugin-react-query/src/generators/__snapshots__/getPetById/useGetPetById.ts b/packages/plugin-react-query/src/generators/__snapshots__/getPetById/useGetPetById.ts index 33bca40e5..ef58494db 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/getPetById/useGetPetById.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/getPetById/useGetPetById.ts @@ -19,8 +19,7 @@ export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Part return queryOptions, GetPetByIdStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/packages/plugin-react-query/src/generators/__snapshots__/getPetIdCamelCase/useGetPetByIdSuspense.ts b/packages/plugin-react-query/src/generators/__snapshots__/getPetIdCamelCase/useGetPetByIdSuspense.ts index f60839fc6..4294619d2 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/getPetIdCamelCase/useGetPetByIdSuspense.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/getPetIdCamelCase/useGetPetByIdSuspense.ts @@ -22,8 +22,7 @@ export function getPetByIdSuspenseQueryOptions( return queryOptions, GetPetByIdStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/packages/plugin-react-query/src/generators/__snapshots__/headersOnly/useRetrieveMyProfile.ts b/packages/plugin-react-query/src/generators/__snapshots__/headersOnly/useRetrieveMyProfile.ts index d8e2b780c..04a7b8b72 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/headersOnly/useRetrieveMyProfile.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/headersOnly/useRetrieveMyProfile.ts @@ -22,8 +22,7 @@ export function retrieveMyProfileQueryOptions( return queryOptions, RetrieveMyProfileStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await retrieveMyProfile({ ...config, headers, signal: config.signal ?? signal, throwOnError: true }) - return data + return retrieveMyProfile({ ...config, headers, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/packages/plugin-react-query/src/generators/__snapshots__/multiContentType/useUploadFile.ts b/packages/plugin-react-query/src/generators/__snapshots__/multiContentType/useUploadFile.ts index acc0e88c6..3a64fb4f0 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/multiContentType/useUploadFile.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/multiContentType/useUploadFile.ts @@ -21,8 +21,7 @@ export function uploadFileMutationOptions( return mutationOptions, UploadFileOptions, TContext>({ mutationKey, mutationFn: async ({ path, body }) => { - const { data } = await uploadFile({ ...config, path, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, body, throwOnError: true }).unwrap() }, }) } diff --git a/packages/plugin-react-query/src/generators/__snapshots__/updatePetById/useUpdatePetWithForm.ts b/packages/plugin-react-query/src/generators/__snapshots__/updatePetById/useUpdatePetWithForm.ts index cd96517c7..db709c4ea 100644 --- a/packages/plugin-react-query/src/generators/__snapshots__/updatePetById/useUpdatePetWithForm.ts +++ b/packages/plugin-react-query/src/generators/__snapshots__/updatePetById/useUpdatePetWithForm.ts @@ -17,8 +17,7 @@ export function updatePetWithFormMutationOptions(config: Par return mutationOptions, UpdatePetWithFormOptions, TContext>({ mutationKey, mutationFn: async ({ path, body }) => { - const { data } = await updatePetWithForm({ ...config, path, body, throwOnError: true }) - return data + return updatePetWithForm({ ...config, path, body, throwOnError: true }).unwrap() }, }) } diff --git a/packages/plugin-swr/src/components/Mutation.tsx b/packages/plugin-swr/src/components/Mutation.tsx index 2506e06c3..541ebf917 100644 --- a/packages/plugin-swr/src/components/Mutation.tsx +++ b/packages/plugin-swr/src/components/Mutation.tsx @@ -61,8 +61,7 @@ export function Mutation({ name, clientName, mutationKeyName, mutationKeyTypeNam const groupedParamsNode = createFunctionParameters({ params: groupedParam ? [groupedParam] : [] }) const argTypeBody = hasMutationParams ? tsResolver.response.options(node) : '' const argBindingStr = hasMutationParams ? (callPrinter.print(groupedParamsNode) ?? '') : '' - const mutationFnBody = `const { data } = await ${buildClientCall(node, { clientName, signal: false })} - return data` + const mutationFnBody = `return ${buildClientCall(node, { clientName, signal: false })}.unwrap()` const generics = [TData, TError, `${mutationKeyTypeName} | null`, mutationArgTypeName] diff --git a/packages/plugin-swr/src/components/QueryOptions.tsx b/packages/plugin-swr/src/components/QueryOptions.tsx index 62445d97a..d54ae2e4f 100644 --- a/packages/plugin-swr/src/components/QueryOptions.tsx +++ b/packages/plugin-swr/src/components/QueryOptions.tsx @@ -17,8 +17,7 @@ const declarationPrinter = functionPrinter({ mode: 'declaration' }) export function QueryOptions({ name, clientName, node, tsResolver }: Props): KubbReactNode { const paramsNode = buildQueryOptionsParams(node, { resolver: tsResolver }) const paramsSignature = declarationPrinter.print(paramsNode) ?? '' - const fetcherBody = `const { data } = await ${buildClientCall(node, { clientName, signal: false })} - return data` + const fetcherBody = `return ${buildClientCall(node, { clientName, signal: false })}.unwrap()` return ( diff --git a/packages/plugin-swr/src/generators/__snapshots__/createPet/useCreatePet.ts b/packages/plugin-swr/src/generators/__snapshots__/createPet/useCreatePet.ts index 5fbfc2163..4dc0ecd6b 100644 --- a/packages/plugin-swr/src/generators/__snapshots__/createPet/useCreatePet.ts +++ b/packages/plugin-swr/src/generators/__snapshots__/createPet/useCreatePet.ts @@ -34,8 +34,7 @@ export function useCreatePet( return useSWRMutation, CreatePetMutationKey | null, CreatePetMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { body } }) => { - const { data } = await createPet({ ...config, body, throwOnError: true }) - return data + return createPet({ ...config, body, throwOnError: true }).unwrap() }, mutationOptions, ) diff --git a/packages/plugin-swr/src/generators/__snapshots__/deletePet/useDeletePet.ts b/packages/plugin-swr/src/generators/__snapshots__/deletePet/useDeletePet.ts index 0fad8915b..33067e884 100644 --- a/packages/plugin-swr/src/generators/__snapshots__/deletePet/useDeletePet.ts +++ b/packages/plugin-swr/src/generators/__snapshots__/deletePet/useDeletePet.ts @@ -34,8 +34,7 @@ export function useDeletePet( return useSWRMutation, DeletePetMutationKey | null, DeletePetMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { path, headers } }) => { - const { data } = await deletePet({ ...config, path, headers, throwOnError: true }) - return data + return deletePet({ ...config, path, headers, throwOnError: true }).unwrap() }, mutationOptions, ) diff --git a/packages/plugin-swr/src/generators/__snapshots__/findByStatusAllOptional/useFindPetsByStatus.ts b/packages/plugin-swr/src/generators/__snapshots__/findByStatusAllOptional/useFindPetsByStatus.ts index 6d5dd7ddd..4fcc3e0b9 100644 --- a/packages/plugin-swr/src/generators/__snapshots__/findByStatusAllOptional/useFindPetsByStatus.ts +++ b/packages/plugin-swr/src/generators/__snapshots__/findByStatusAllOptional/useFindPetsByStatus.ts @@ -21,8 +21,7 @@ export function findPetsByStatusQueryOptions( ) { return { fetcher: async () => { - const { data } = await findPetsByStatus({ ...config, query, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, throwOnError: true }).unwrap() }, } } diff --git a/packages/plugin-swr/src/generators/__snapshots__/findByTags/useFindPetsByTags.ts b/packages/plugin-swr/src/generators/__snapshots__/findByTags/useFindPetsByTags.ts index adf410c83..b470b6baa 100644 --- a/packages/plugin-swr/src/generators/__snapshots__/findByTags/useFindPetsByTags.ts +++ b/packages/plugin-swr/src/generators/__snapshots__/findByTags/useFindPetsByTags.ts @@ -20,8 +20,7 @@ export function findPetsByTagsQueryOptions( ) { return { fetcher: async () => { - const { data } = await findPetsByTags({ ...config, query, throwOnError: true }) - return data + return findPetsByTags({ ...config, query, throwOnError: true }).unwrap() }, } } diff --git a/packages/plugin-swr/src/generators/__snapshots__/getAsMutation/useFindPetsByTags.ts b/packages/plugin-swr/src/generators/__snapshots__/getAsMutation/useFindPetsByTags.ts index acb1dca66..48f3bb70f 100644 --- a/packages/plugin-swr/src/generators/__snapshots__/getAsMutation/useFindPetsByTags.ts +++ b/packages/plugin-swr/src/generators/__snapshots__/getAsMutation/useFindPetsByTags.ts @@ -34,8 +34,7 @@ export function useFindPetsByTags( return useSWRMutation, FindPetsByTagsMutationKey | null, FindPetsByTagsMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { query } }) => { - const { data } = await findPetsByTags({ ...config, query, throwOnError: true }) - return data + return findPetsByTags({ ...config, query, throwOnError: true }).unwrap() }, mutationOptions, ) diff --git a/packages/plugin-swr/src/generators/__snapshots__/getPetById/useGetPetById.ts b/packages/plugin-swr/src/generators/__snapshots__/getPetById/useGetPetById.ts index 48f2d4218..de7c4f45a 100644 --- a/packages/plugin-swr/src/generators/__snapshots__/getPetById/useGetPetById.ts +++ b/packages/plugin-swr/src/generators/__snapshots__/getPetById/useGetPetById.ts @@ -17,8 +17,7 @@ type GetPetByIdQueryKey = ReturnType export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await getPetById({ ...config, path, throwOnError: true }) - return data + return getPetById({ ...config, path, throwOnError: true }).unwrap() }, } } diff --git a/packages/plugin-swr/src/generators/__snapshots__/updatePetWithForm/useUpdatePetWithForm.ts b/packages/plugin-swr/src/generators/__snapshots__/updatePetWithForm/useUpdatePetWithForm.ts index 17271ae10..e86d40a30 100644 --- a/packages/plugin-swr/src/generators/__snapshots__/updatePetWithForm/useUpdatePetWithForm.ts +++ b/packages/plugin-swr/src/generators/__snapshots__/updatePetWithForm/useUpdatePetWithForm.ts @@ -37,8 +37,7 @@ export function useUpdatePetWithForm( return useSWRMutation, UpdatePetWithFormMutationKey | null, UpdatePetWithFormMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { path, body } }) => { - const { data } = await updatePetWithForm({ ...config, path, body, throwOnError: true }) - return data + return updatePetWithForm({ ...config, path, body, throwOnError: true }).unwrap() }, mutationOptions, ) diff --git a/packages/plugin-vue-query/src/components/Mutation.tsx b/packages/plugin-vue-query/src/components/Mutation.tsx index 0733142a6..06f7088fa 100644 --- a/packages/plugin-vue-query/src/components/Mutation.tsx +++ b/packages/plugin-vue-query/src/components/Mutation.tsx @@ -55,8 +55,7 @@ export function Mutation({ name, clientName, node, tsResolver, mutationKeyName } const hasMutationParams = groupedParam !== null const groupedParamsNode = createFunctionParameters({ params: groupedParam ? [groupedParam] : [] }) const argBindingStr = hasMutationParams ? (callPrinter.print(groupedParamsNode) ?? '') : '' - const mutationFnBody = `const { data } = await ${buildVueClientCall(node, { clientName, signal: false })} - return data` + const mutationFnBody = `return ${buildVueClientCall(node, { clientName, signal: false })}.unwrap()` const TRequest = resolveMutationRequestType(node, tsResolver) const generics = [TData, TError, TRequest, 'TContext'].join(', ') diff --git a/packages/plugin-vue-query/src/components/QueryOptions.tsx b/packages/plugin-vue-query/src/components/QueryOptions.tsx index c565e5b0b..c343d156f 100644 --- a/packages/plugin-vue-query/src/components/QueryOptions.tsx +++ b/packages/plugin-vue-query/src/components/QueryOptions.tsx @@ -30,8 +30,7 @@ export function QueryOptions({ name, clientName, node, tsResolver, queryKeyName const paramsNode = getQueryOptionsParams(node, { resolver: tsResolver }) const paramsSignature = declarationPrinter.print(paramsNode) ?? '' - const queryFnBody = `const { data } = await ${buildVueClientCall(node, { clientName, signal: true })} - return data` + const queryFnBody = `return ${buildVueClientCall(node, { clientName, signal: true })}.unwrap()` return ( diff --git a/packages/plugin-vue-query/src/generators/__snapshots__/findByTags/useFindPetsByTags.ts b/packages/plugin-vue-query/src/generators/__snapshots__/findByTags/useFindPetsByTags.ts index 909365883..78bfd6c74 100644 --- a/packages/plugin-vue-query/src/generators/__snapshots__/findByTags/useFindPetsByTags.ts +++ b/packages/plugin-vue-query/src/generators/__snapshots__/findByTags/useFindPetsByTags.ts @@ -25,8 +25,7 @@ export function findPetsByTagsQueryOptions( return queryOptions, FindPetsByTagsStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByTags({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/packages/plugin-vue-query/src/generators/__snapshots__/findByTagsTemplateString/useFindPetsByTags.ts b/packages/plugin-vue-query/src/generators/__snapshots__/findByTagsTemplateString/useFindPetsByTags.ts index 909365883..78bfd6c74 100644 --- a/packages/plugin-vue-query/src/generators/__snapshots__/findByTagsTemplateString/useFindPetsByTags.ts +++ b/packages/plugin-vue-query/src/generators/__snapshots__/findByTagsTemplateString/useFindPetsByTags.ts @@ -25,8 +25,7 @@ export function findPetsByTagsQueryOptions( return queryOptions, FindPetsByTagsStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByTags({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/packages/plugin-vue-query/src/generators/__snapshots__/findByTagsWithCustomQueryKey/useFindPetsByTags.ts b/packages/plugin-vue-query/src/generators/__snapshots__/findByTagsWithCustomQueryKey/useFindPetsByTags.ts index 8d0e19cd3..9a9b50140 100644 --- a/packages/plugin-vue-query/src/generators/__snapshots__/findByTagsWithCustomQueryKey/useFindPetsByTags.ts +++ b/packages/plugin-vue-query/src/generators/__snapshots__/findByTagsWithCustomQueryKey/useFindPetsByTags.ts @@ -25,8 +25,7 @@ export function findPetsByTagsQueryOptions( return queryOptions, FindPetsByTagsStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByTags({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/packages/plugin-vue-query/src/generators/__snapshots__/findInfiniteByTags/useFindPetsByTagsInfinite.ts b/packages/plugin-vue-query/src/generators/__snapshots__/findInfiniteByTags/useFindPetsByTagsInfinite.ts index 6af38f73b..df0e10004 100644 --- a/packages/plugin-vue-query/src/generators/__snapshots__/findInfiniteByTags/useFindPetsByTagsInfinite.ts +++ b/packages/plugin-vue-query/src/generators/__snapshots__/findInfiniteByTags/useFindPetsByTagsInfinite.ts @@ -35,8 +35,7 @@ export function findPetsByTagsInfiniteQueryOptions( ...(query ?? {}), ['pageSize']: pageParam as unknown as FindPetsByTagsQuery['pageSize'], } as FindPetsByTagsQuery - const { data } = await findPetsByTags({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, initialPageParam: 0, getNextPageParam: (lastPage, _allPages, lastPageParam) => (Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1), diff --git a/packages/plugin-vue-query/src/generators/__snapshots__/findInfiniteByTagsCursor/useFindPetsByTagsInfinite.ts b/packages/plugin-vue-query/src/generators/__snapshots__/findInfiniteByTagsCursor/useFindPetsByTagsInfinite.ts index 7354620f9..d775dec7c 100644 --- a/packages/plugin-vue-query/src/generators/__snapshots__/findInfiniteByTagsCursor/useFindPetsByTagsInfinite.ts +++ b/packages/plugin-vue-query/src/generators/__snapshots__/findInfiniteByTagsCursor/useFindPetsByTagsInfinite.ts @@ -35,8 +35,7 @@ export function findPetsByTagsInfiniteQueryOptions( ...(query ?? {}), ['pageSize']: pageParam as unknown as FindPetsByTagsQuery['pageSize'], } as FindPetsByTagsQuery - const { data } = await findPetsByTags({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByTags({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, initialPageParam: 0, getNextPageParam: (lastPage) => lastPage['cursor'], diff --git a/packages/plugin-vue-query/src/generators/__snapshots__/postAsQuery/useUpdatePetWithForm.ts b/packages/plugin-vue-query/src/generators/__snapshots__/postAsQuery/useUpdatePetWithForm.ts index 91ee3d9d8..3417042e5 100644 --- a/packages/plugin-vue-query/src/generators/__snapshots__/postAsQuery/useUpdatePetWithForm.ts +++ b/packages/plugin-vue-query/src/generators/__snapshots__/postAsQuery/useUpdatePetWithForm.ts @@ -40,15 +40,14 @@ export function updatePetWithFormQueryOptions( return queryOptions, UpdatePetWithFormStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await updatePetWithForm({ + return updatePetWithForm({ ...config, path: toValue(path), query: toValue(query), body: toValue(body), signal: config.signal ?? signal, throwOnError: true, - }) - return data + }).unwrap() }, }) } diff --git a/packages/plugin-vue-query/src/generators/__snapshots__/updatePetById/useUpdatePetWithForm.ts b/packages/plugin-vue-query/src/generators/__snapshots__/updatePetById/useUpdatePetWithForm.ts index 24f31e407..4a63e861c 100644 --- a/packages/plugin-vue-query/src/generators/__snapshots__/updatePetById/useUpdatePetWithForm.ts +++ b/packages/plugin-vue-query/src/generators/__snapshots__/updatePetById/useUpdatePetWithForm.ts @@ -29,8 +29,7 @@ export function useUpdatePetWithForm( return useMutation, UpdatePetWithFormOptions, TContext>( { mutationFn: async ({ path, body }) => { - const { data } = await updatePetWithForm({ ...config, path: toValue(path), body: toValue(body), throwOnError: true }) - return data + return updatePetWithForm({ ...config, path: toValue(path), body: toValue(body), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions, diff --git a/packages/plugin-vue-query/src/generators/__snapshots__/updatePetByIdWithCustomMutationKey/useUpdatePetWithForm.ts b/packages/plugin-vue-query/src/generators/__snapshots__/updatePetByIdWithCustomMutationKey/useUpdatePetWithForm.ts index a1fe2e04c..c8993bcac 100644 --- a/packages/plugin-vue-query/src/generators/__snapshots__/updatePetByIdWithCustomMutationKey/useUpdatePetWithForm.ts +++ b/packages/plugin-vue-query/src/generators/__snapshots__/updatePetByIdWithCustomMutationKey/useUpdatePetWithForm.ts @@ -29,8 +29,7 @@ export function useUpdatePetWithForm( return useMutation, UpdatePetWithFormOptions, TContext>( { mutationFn: async ({ path, body }) => { - const { data } = await updatePetWithForm({ ...config, path: toValue(path), body: toValue(body), throwOnError: true }) - return data + return updatePetWithForm({ ...config, path: toValue(path), body: toValue(body), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions, diff --git a/tests/3.0.x/__snapshots__/pluginAxios/default/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginAxios/default/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/default/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/default/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginAxios/default/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginAxios/default/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/default/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/default/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginAxios/default/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginAxios/default/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/default/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/default/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginAxios/default/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginAxios/default/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/default/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/default/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginAxios/default/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginAxios/default/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/default/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/default/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginAxios/default/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginAxios/default/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/default/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/default/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginAxios/default/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginAxios/default/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/default/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/default/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginAxios/default/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginAxios/default/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/default/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/default/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginAxios/paramsCasing/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginAxios/paramsCasing/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/paramsCasing/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/paramsCasing/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginAxios/paramsCasing/clients/updatePet.ts b/tests/3.0.x/__snapshots__/pluginAxios/paramsCasing/clients/updatePet.ts index c8b4a7c7c..8fab7946f 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/paramsCasing/clients/updatePet.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/paramsCasing/clients/updatePet.ts @@ -3,15 +3,15 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UpdatePetOptions, UpdatePetResponses } from '../types/UpdatePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * {@link /pets/:pet_id} */ -export function updatePet(options: Options): Promise> { +export function updatePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pets/{pet_id}', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pets/{pet_id}', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginAxios/sdkClass/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginAxios/sdkClass/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/sdkClass/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/sdkClass/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginAxios/sdkClass/clients/petClient.ts b/tests/3.0.x/__snapshots__/pluginAxios/sdkClass/clients/petClient.ts index 2a7a28beb..869252682 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/sdkClass/clients/petClient.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/sdkClass/clients/petClient.ts @@ -3,13 +3,13 @@ * Do not edit manually. */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from '../.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { createClient } from '../.kubb/client' +import { createClient, withUnwrap } from '../.kubb/client' export class PetClient { private readonly client: ClientInstance @@ -23,10 +23,10 @@ export class PetClient { * @summary Add a new pet to the store * {@link /pet} */ - public addPet(options: Options): Promise> { + public addPet(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } /** @@ -34,10 +34,10 @@ export class PetClient { * @summary Finds Pets by status * {@link /pet/findByStatus} */ - public findPetsByStatus(options: Options = {}): Promise> { + public findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], ...config }) as Promise>) } /** @@ -45,10 +45,10 @@ export class PetClient { * @summary Find pet by ID * {@link /pet/:petId} */ - public getPetById(options: Options): Promise> { + public getPetById(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } /** @@ -56,19 +56,19 @@ export class PetClient { * @summary Deletes a pet * {@link /pet/:petId} */ - public deletePet(options: Options): Promise> { + public deletePet(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ - public uploadFile(options: Options): Promise> { + public uploadFile(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise>) } } diff --git a/tests/3.0.x/__snapshots__/pluginAxios/sdkClass/clients/storeClient.ts b/tests/3.0.x/__snapshots__/pluginAxios/sdkClass/clients/storeClient.ts index 174c15837..c51022005 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/sdkClass/clients/storeClient.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/sdkClass/clients/storeClient.ts @@ -3,10 +3,10 @@ * Do not edit manually. */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from '../.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { createClient } from '../.kubb/client' +import { createClient, withUnwrap } from '../.kubb/client' export class StoreClient { private readonly client: ClientInstance @@ -20,10 +20,10 @@ export class StoreClient { * @summary Returns pet inventories by status * {@link /store/inventory} */ - public getInventory(options: Options = {}): Promise> { + public getInventory(options: Options = {}): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } /** @@ -31,9 +31,9 @@ export class StoreClient { * @summary Place an order for a pet * {@link /store/order} */ - public placeOrder(options: Options): Promise> { + public placeOrder(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } } diff --git a/tests/3.0.x/__snapshots__/pluginAxios/sdkClassWithName/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginAxios/sdkClassWithName/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/sdkClassWithName/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/sdkClassWithName/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginAxios/sdkClassWithName/clients/petClient.ts b/tests/3.0.x/__snapshots__/pluginAxios/sdkClassWithName/clients/petClient.ts index 2a7a28beb..869252682 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/sdkClassWithName/clients/petClient.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/sdkClassWithName/clients/petClient.ts @@ -3,13 +3,13 @@ * Do not edit manually. */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from '../.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { createClient } from '../.kubb/client' +import { createClient, withUnwrap } from '../.kubb/client' export class PetClient { private readonly client: ClientInstance @@ -23,10 +23,10 @@ export class PetClient { * @summary Add a new pet to the store * {@link /pet} */ - public addPet(options: Options): Promise> { + public addPet(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } /** @@ -34,10 +34,10 @@ export class PetClient { * @summary Finds Pets by status * {@link /pet/findByStatus} */ - public findPetsByStatus(options: Options = {}): Promise> { + public findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], ...config }) as Promise>) } /** @@ -45,10 +45,10 @@ export class PetClient { * @summary Find pet by ID * {@link /pet/:petId} */ - public getPetById(options: Options): Promise> { + public getPetById(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } /** @@ -56,19 +56,19 @@ export class PetClient { * @summary Deletes a pet * {@link /pet/:petId} */ - public deletePet(options: Options): Promise> { + public deletePet(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ - public uploadFile(options: Options): Promise> { + public uploadFile(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise>) } } diff --git a/tests/3.0.x/__snapshots__/pluginAxios/sdkClassWithName/clients/storeClient.ts b/tests/3.0.x/__snapshots__/pluginAxios/sdkClassWithName/clients/storeClient.ts index 174c15837..c51022005 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/sdkClassWithName/clients/storeClient.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/sdkClassWithName/clients/storeClient.ts @@ -3,10 +3,10 @@ * Do not edit manually. */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from '../.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { createClient } from '../.kubb/client' +import { createClient, withUnwrap } from '../.kubb/client' export class StoreClient { private readonly client: ClientInstance @@ -20,10 +20,10 @@ export class StoreClient { * @summary Returns pet inventories by status * {@link /store/inventory} */ - public getInventory(options: Options = {}): Promise> { + public getInventory(options: Options = {}): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } /** @@ -31,9 +31,9 @@ export class StoreClient { * @summary Place an order for a pet * {@link /store/order} */ - public placeOrder(options: Options): Promise> { + public placeOrder(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } } diff --git a/tests/3.0.x/__snapshots__/pluginAxios/sdkSingle/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginAxios/sdkSingle/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/sdkSingle/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/sdkSingle/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginAxios/sdkSingle/clients/petStore.ts b/tests/3.0.x/__snapshots__/pluginAxios/sdkSingle/clients/petStore.ts index 2904cdd30..eb6ecc02c 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/sdkSingle/clients/petStore.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/sdkSingle/clients/petStore.ts @@ -3,7 +3,7 @@ * Do not edit manually. */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from '../.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' @@ -11,7 +11,7 @@ import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInv import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { createClient } from '../.kubb/client' +import { createClient, withUnwrap } from '../.kubb/client' export class PetStore { private readonly client: ClientInstance @@ -25,10 +25,10 @@ export class PetStore { * @summary Add a new pet to the store * {@link /pet} */ - public addPet(options: Options): Promise> { + public addPet(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } /** @@ -36,10 +36,10 @@ export class PetStore { * @summary Finds Pets by status * {@link /pet/findByStatus} */ - public findPetsByStatus(options: Options = {}): Promise> { + public findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], ...config }) as Promise>) } /** @@ -47,10 +47,10 @@ export class PetStore { * @summary Find pet by ID * {@link /pet/:petId} */ - public getPetById(options: Options): Promise> { + public getPetById(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } /** @@ -58,20 +58,20 @@ export class PetStore { * @summary Deletes a pet * {@link /pet/:petId} */ - public deletePet(options: Options): Promise> { + public deletePet(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ - public uploadFile(options: Options): Promise> { + public uploadFile(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise>) } /** @@ -79,10 +79,10 @@ export class PetStore { * @summary Returns pet inventories by status * {@link /store/inventory} */ - public getInventory(options: Options = {}): Promise> { + public getInventory(options: Options = {}): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } /** @@ -90,9 +90,9 @@ export class PetStore { * @summary Place an order for a pet * {@link /store/order} */ - public placeOrder(options: Options): Promise> { + public placeOrder(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } } diff --git a/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/addPet.ts index 03d71616a..397c9cc0c 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/addPet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptionsSchemaType, AddPetResponsesSchemaType } from '../zod/addPetSchema' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { addPetResponseSchema, addPetErrorSchema } from '../zod/addPetSchema' /** @@ -13,8 +13,8 @@ import { addPetResponseSchema, addPetErrorSchema } from '../zod/addPetSchema' * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], validator: { response: addPetResponseSchema, error: addPetErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], validator: { response: addPetResponseSchema, error: addPetErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/deletePet.ts index 245a40ec1..71e8cae66 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/deletePet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptionsSchemaType, DeletePetResponsesSchemaType } from '../zod/deletePetSchema' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { deletePetResponseSchema, deletePetErrorSchema } from '../zod/deletePetSchema' /** @@ -13,8 +13,8 @@ import { deletePetResponseSchema, deletePetErrorSchema } from '../zod/deletePetS * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], validator: { response: deletePetResponseSchema, error: deletePetErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], validator: { response: deletePetResponseSchema, error: deletePetErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/findPetsByStatus.ts index 5065d1d14..ee1cf3c80 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/findPetsByStatus.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptionsSchemaType, FindPetsByStatusResponsesSchemaType } from '../zod/findPetsByStatusSchema' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { findPetsByStatusResponseSchema, findPetsByStatusErrorSchema } from '../zod/findPetsByStatusSchema' /** @@ -13,8 +13,8 @@ import { findPetsByStatusResponseSchema, findPetsByStatusErrorSchema } from '../ * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, validator: { response: findPetsByStatusResponseSchema, error: findPetsByStatusErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, validator: { response: findPetsByStatusResponseSchema, error: findPetsByStatusErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/getInventory.ts index 83c566b28..2fdc44f8d 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/getInventory.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptionsSchemaType, GetInventoryResponsesSchemaType } from '../zod/getInventorySchema' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { getInventoryResponseSchema } from '../zod/getInventorySchema' /** @@ -13,8 +13,8 @@ import { getInventoryResponseSchema } from '../zod/getInventorySchema' * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], validator: { response: getInventoryResponseSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], validator: { response: getInventoryResponseSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/getPetById.ts index c79fe3fd8..fab89a848 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/getPetById.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptionsSchemaType, GetPetByIdResponsesSchemaType } from '../zod/getPetByIdSchema' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { getPetByIdResponseSchema, getPetByIdErrorSchema } from '../zod/getPetByIdSchema' /** @@ -13,8 +13,8 @@ import { getPetByIdResponseSchema, getPetByIdErrorSchema } from '../zod/getPetBy * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], validator: { response: getPetByIdResponseSchema, error: getPetByIdErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], validator: { response: getPetByIdResponseSchema, error: getPetByIdErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/placeOrder.ts index 352496d50..1025f08e9 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/placeOrder.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptionsSchemaType, PlaceOrderResponsesSchemaType } from '../zod/placeOrderSchema' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { placeOrderResponseSchema, placeOrderErrorSchema } from '../zod/placeOrderSchema' /** @@ -13,8 +13,8 @@ import { placeOrderResponseSchema, placeOrderErrorSchema } from '../zod/placeOrd * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', validator: { response: placeOrderResponseSchema, error: placeOrderErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', validator: { response: placeOrderResponseSchema, error: placeOrderErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/uploadFile.ts index 3c7bf3cc8..3d9c5e420 100644 --- a/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginAxios/zodTypes/clients/uploadFile.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptionsSchemaType, UploadFileResponsesSchemaType } from '../zod/uploadFileSchema' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { uploadFileResponseSchema } from '../zod/uploadFileSchema' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], validator: { response: uploadFileResponseSchema }, contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], validator: { response: uploadFileResponseSchema }, contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/default/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginFetch/default/.kubb/client.ts index f80182aca..ea6223eff 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/default/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/default/.kubb/client.ts @@ -82,6 +82,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginFetch/default/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginFetch/default/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/default/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/default/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/default/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginFetch/default/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/default/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/default/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/default/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginFetch/default/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/default/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/default/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/default/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginFetch/default/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/default/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/default/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/default/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginFetch/default/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/default/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/default/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/default/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginFetch/default/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/default/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/default/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/default/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginFetch/default/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/default/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/default/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/paramsCasing/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginFetch/paramsCasing/.kubb/client.ts index f80182aca..ea6223eff 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/paramsCasing/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/paramsCasing/.kubb/client.ts @@ -82,6 +82,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginFetch/paramsCasing/clients/updatePet.ts b/tests/3.0.x/__snapshots__/pluginFetch/paramsCasing/clients/updatePet.ts index c8b4a7c7c..8fab7946f 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/paramsCasing/clients/updatePet.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/paramsCasing/clients/updatePet.ts @@ -3,15 +3,15 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UpdatePetOptions, UpdatePetResponses } from '../types/UpdatePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * {@link /pets/:pet_id} */ -export function updatePet(options: Options): Promise> { +export function updatePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pets/{pet_id}', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pets/{pet_id}', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/sdkClass/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginFetch/sdkClass/.kubb/client.ts index f80182aca..ea6223eff 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/sdkClass/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/sdkClass/.kubb/client.ts @@ -82,6 +82,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginFetch/sdkClass/clients/petClient.ts b/tests/3.0.x/__snapshots__/pluginFetch/sdkClass/clients/petClient.ts index 2a7a28beb..869252682 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/sdkClass/clients/petClient.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/sdkClass/clients/petClient.ts @@ -3,13 +3,13 @@ * Do not edit manually. */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from '../.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { createClient } from '../.kubb/client' +import { createClient, withUnwrap } from '../.kubb/client' export class PetClient { private readonly client: ClientInstance @@ -23,10 +23,10 @@ export class PetClient { * @summary Add a new pet to the store * {@link /pet} */ - public addPet(options: Options): Promise> { + public addPet(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } /** @@ -34,10 +34,10 @@ export class PetClient { * @summary Finds Pets by status * {@link /pet/findByStatus} */ - public findPetsByStatus(options: Options = {}): Promise> { + public findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], ...config }) as Promise>) } /** @@ -45,10 +45,10 @@ export class PetClient { * @summary Find pet by ID * {@link /pet/:petId} */ - public getPetById(options: Options): Promise> { + public getPetById(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } /** @@ -56,19 +56,19 @@ export class PetClient { * @summary Deletes a pet * {@link /pet/:petId} */ - public deletePet(options: Options): Promise> { + public deletePet(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ - public uploadFile(options: Options): Promise> { + public uploadFile(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise>) } } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/sdkClass/clients/storeClient.ts b/tests/3.0.x/__snapshots__/pluginFetch/sdkClass/clients/storeClient.ts index 174c15837..c51022005 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/sdkClass/clients/storeClient.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/sdkClass/clients/storeClient.ts @@ -3,10 +3,10 @@ * Do not edit manually. */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from '../.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { createClient } from '../.kubb/client' +import { createClient, withUnwrap } from '../.kubb/client' export class StoreClient { private readonly client: ClientInstance @@ -20,10 +20,10 @@ export class StoreClient { * @summary Returns pet inventories by status * {@link /store/inventory} */ - public getInventory(options: Options = {}): Promise> { + public getInventory(options: Options = {}): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } /** @@ -31,9 +31,9 @@ export class StoreClient { * @summary Place an order for a pet * {@link /store/order} */ - public placeOrder(options: Options): Promise> { + public placeOrder(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/sdkClassWithName/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginFetch/sdkClassWithName/.kubb/client.ts index f80182aca..ea6223eff 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/sdkClassWithName/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/sdkClassWithName/.kubb/client.ts @@ -82,6 +82,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginFetch/sdkClassWithName/clients/petClient.ts b/tests/3.0.x/__snapshots__/pluginFetch/sdkClassWithName/clients/petClient.ts index 2a7a28beb..869252682 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/sdkClassWithName/clients/petClient.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/sdkClassWithName/clients/petClient.ts @@ -3,13 +3,13 @@ * Do not edit manually. */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from '../.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { createClient } from '../.kubb/client' +import { createClient, withUnwrap } from '../.kubb/client' export class PetClient { private readonly client: ClientInstance @@ -23,10 +23,10 @@ export class PetClient { * @summary Add a new pet to the store * {@link /pet} */ - public addPet(options: Options): Promise> { + public addPet(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } /** @@ -34,10 +34,10 @@ export class PetClient { * @summary Finds Pets by status * {@link /pet/findByStatus} */ - public findPetsByStatus(options: Options = {}): Promise> { + public findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], ...config }) as Promise>) } /** @@ -45,10 +45,10 @@ export class PetClient { * @summary Find pet by ID * {@link /pet/:petId} */ - public getPetById(options: Options): Promise> { + public getPetById(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } /** @@ -56,19 +56,19 @@ export class PetClient { * @summary Deletes a pet * {@link /pet/:petId} */ - public deletePet(options: Options): Promise> { + public deletePet(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ - public uploadFile(options: Options): Promise> { + public uploadFile(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise>) } } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/sdkClassWithName/clients/storeClient.ts b/tests/3.0.x/__snapshots__/pluginFetch/sdkClassWithName/clients/storeClient.ts index 174c15837..c51022005 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/sdkClassWithName/clients/storeClient.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/sdkClassWithName/clients/storeClient.ts @@ -3,10 +3,10 @@ * Do not edit manually. */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from '../.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { createClient } from '../.kubb/client' +import { createClient, withUnwrap } from '../.kubb/client' export class StoreClient { private readonly client: ClientInstance @@ -20,10 +20,10 @@ export class StoreClient { * @summary Returns pet inventories by status * {@link /store/inventory} */ - public getInventory(options: Options = {}): Promise> { + public getInventory(options: Options = {}): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } /** @@ -31,9 +31,9 @@ export class StoreClient { * @summary Place an order for a pet * {@link /store/order} */ - public placeOrder(options: Options): Promise> { + public placeOrder(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/sdkSingle/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginFetch/sdkSingle/.kubb/client.ts index f80182aca..ea6223eff 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/sdkSingle/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/sdkSingle/.kubb/client.ts @@ -82,6 +82,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginFetch/sdkSingle/clients/petStore.ts b/tests/3.0.x/__snapshots__/pluginFetch/sdkSingle/clients/petStore.ts index 2904cdd30..eb6ecc02c 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/sdkSingle/clients/petStore.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/sdkSingle/clients/petStore.ts @@ -3,7 +3,7 @@ * Do not edit manually. */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from '../.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' @@ -11,7 +11,7 @@ import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInv import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { createClient } from '../.kubb/client' +import { createClient, withUnwrap } from '../.kubb/client' export class PetStore { private readonly client: ClientInstance @@ -25,10 +25,10 @@ export class PetStore { * @summary Add a new pet to the store * {@link /pet} */ - public addPet(options: Options): Promise> { + public addPet(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } /** @@ -36,10 +36,10 @@ export class PetStore { * @summary Finds Pets by status * {@link /pet/findByStatus} */ - public findPetsByStatus(options: Options = {}): Promise> { + public findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], ...config }) as Promise>) } /** @@ -47,10 +47,10 @@ export class PetStore { * @summary Find pet by ID * {@link /pet/:petId} */ - public getPetById(options: Options): Promise> { + public getPetById(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } /** @@ -58,20 +58,20 @@ export class PetStore { * @summary Deletes a pet * {@link /pet/:petId} */ - public deletePet(options: Options): Promise> { + public deletePet(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ - public uploadFile(options: Options): Promise> { + public uploadFile(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], ...config }) as Promise>) } /** @@ -79,10 +79,10 @@ export class PetStore { * @summary Returns pet inventories by status * {@link /store/inventory} */ - public getInventory(options: Options = {}): Promise> { + public getInventory(options: Options = {}): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } /** @@ -90,9 +90,9 @@ export class PetStore { * @summary Place an order for a pet * {@link /store/order} */ - public placeOrder(options: Options): Promise> { + public placeOrder(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/.kubb/client.ts index f80182aca..ea6223eff 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/.kubb/client.ts @@ -82,6 +82,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/addPet.ts index 03d71616a..397c9cc0c 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/addPet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptionsSchemaType, AddPetResponsesSchemaType } from '../zod/addPetSchema' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { addPetResponseSchema, addPetErrorSchema } from '../zod/addPetSchema' /** @@ -13,8 +13,8 @@ import { addPetResponseSchema, addPetErrorSchema } from '../zod/addPetSchema' * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], validator: { response: addPetResponseSchema, error: addPetErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], validator: { response: addPetResponseSchema, error: addPetErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/deletePet.ts index 245a40ec1..71e8cae66 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/deletePet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptionsSchemaType, DeletePetResponsesSchemaType } from '../zod/deletePetSchema' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { deletePetResponseSchema, deletePetErrorSchema } from '../zod/deletePetSchema' /** @@ -13,8 +13,8 @@ import { deletePetResponseSchema, deletePetErrorSchema } from '../zod/deletePetS * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], validator: { response: deletePetResponseSchema, error: deletePetErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], validator: { response: deletePetResponseSchema, error: deletePetErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/findPetsByStatus.ts index 5065d1d14..ee1cf3c80 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/findPetsByStatus.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptionsSchemaType, FindPetsByStatusResponsesSchemaType } from '../zod/findPetsByStatusSchema' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { findPetsByStatusResponseSchema, findPetsByStatusErrorSchema } from '../zod/findPetsByStatusSchema' /** @@ -13,8 +13,8 @@ import { findPetsByStatusResponseSchema, findPetsByStatusErrorSchema } from '../ * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, validator: { response: findPetsByStatusResponseSchema, error: findPetsByStatusErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, validator: { response: findPetsByStatusResponseSchema, error: findPetsByStatusErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/getInventory.ts index 83c566b28..2fdc44f8d 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/getInventory.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptionsSchemaType, GetInventoryResponsesSchemaType } from '../zod/getInventorySchema' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { getInventoryResponseSchema } from '../zod/getInventorySchema' /** @@ -13,8 +13,8 @@ import { getInventoryResponseSchema } from '../zod/getInventorySchema' * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], validator: { response: getInventoryResponseSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], validator: { response: getInventoryResponseSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/getPetById.ts index c79fe3fd8..fab89a848 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/getPetById.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptionsSchemaType, GetPetByIdResponsesSchemaType } from '../zod/getPetByIdSchema' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { getPetByIdResponseSchema, getPetByIdErrorSchema } from '../zod/getPetByIdSchema' /** @@ -13,8 +13,8 @@ import { getPetByIdResponseSchema, getPetByIdErrorSchema } from '../zod/getPetBy * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], validator: { response: getPetByIdResponseSchema, error: getPetByIdErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], validator: { response: getPetByIdResponseSchema, error: getPetByIdErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/placeOrder.ts index 352496d50..1025f08e9 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/placeOrder.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptionsSchemaType, PlaceOrderResponsesSchemaType } from '../zod/placeOrderSchema' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { placeOrderResponseSchema, placeOrderErrorSchema } from '../zod/placeOrderSchema' /** @@ -13,8 +13,8 @@ import { placeOrderResponseSchema, placeOrderErrorSchema } from '../zod/placeOrd * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', validator: { response: placeOrderResponseSchema, error: placeOrderErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', validator: { response: placeOrderResponseSchema, error: placeOrderErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/uploadFile.ts index 3c7bf3cc8..3d9c5e420 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/zodTypes/clients/uploadFile.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptionsSchemaType, UploadFileResponsesSchemaType } from '../zod/uploadFileSchema' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { uploadFileResponseSchema } from '../zod/uploadFileSchema' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], validator: { response: uploadFileResponseSchema }, contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], validator: { response: uploadFileResponseSchema }, contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/zodTypesSdk/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginFetch/zodTypesSdk/.kubb/client.ts index f80182aca..ea6223eff 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/zodTypesSdk/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/zodTypesSdk/.kubb/client.ts @@ -82,6 +82,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginFetch/zodTypesSdk/clients/petClient.ts b/tests/3.0.x/__snapshots__/pluginFetch/zodTypesSdk/clients/petClient.ts index eef8fd252..ef2560df0 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/zodTypesSdk/clients/petClient.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/zodTypesSdk/clients/petClient.ts @@ -3,13 +3,13 @@ * Do not edit manually. */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from '../.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptionsSchemaType, AddPetResponsesSchemaType } from '../zod/addPetSchema' import type { DeletePetOptionsSchemaType, DeletePetResponsesSchemaType } from '../zod/deletePetSchema' import type { FindPetsByStatusOptionsSchemaType, FindPetsByStatusResponsesSchemaType } from '../zod/findPetsByStatusSchema' import type { GetPetByIdOptionsSchemaType, GetPetByIdResponsesSchemaType } from '../zod/getPetByIdSchema' import type { UploadFileOptionsSchemaType, UploadFileResponsesSchemaType } from '../zod/uploadFileSchema' -import { createClient } from '../.kubb/client' +import { createClient, withUnwrap } from '../.kubb/client' import { addPetResponseSchema } from '../zod/addPetSchema' import { deletePetResponseSchema } from '../zod/deletePetSchema' import { findPetsByStatusResponseSchema } from '../zod/findPetsByStatusSchema' @@ -28,10 +28,10 @@ export class PetClient { * @summary Add a new pet to the store * {@link /pet} */ - public addPet(options: Options): Promise> { + public addPet(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], validator: { response: addPetResponseSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], validator: { response: addPetResponseSchema }, ...config }) as Promise>) } /** @@ -39,10 +39,10 @@ export class PetClient { * @summary Finds Pets by status * {@link /pet/findByStatus} */ - public findPetsByStatus(options: Options = {}): Promise> { + public findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], validator: { response: findPetsByStatusResponseSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], validator: { response: findPetsByStatusResponseSchema }, ...config }) as Promise>) } /** @@ -50,10 +50,10 @@ export class PetClient { * @summary Find pet by ID * {@link /pet/:petId} */ - public getPetById(options: Options): Promise> { + public getPetById(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], validator: { response: getPetByIdResponseSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], validator: { response: getPetByIdResponseSchema }, ...config }) as Promise>) } /** @@ -61,19 +61,19 @@ export class PetClient { * @summary Deletes a pet * {@link /pet/:petId} */ - public deletePet(options: Options): Promise> { + public deletePet(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], validator: { response: deletePetResponseSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], validator: { response: deletePetResponseSchema }, ...config }) as Promise>) } /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ - public uploadFile(options: Options): Promise> { + public uploadFile(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], validator: { response: uploadFileResponseSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], validator: { response: uploadFileResponseSchema }, ...config }) as Promise>) } } diff --git a/tests/3.0.x/__snapshots__/pluginFetch/zodTypesSdk/clients/storeClient.ts b/tests/3.0.x/__snapshots__/pluginFetch/zodTypesSdk/clients/storeClient.ts index 6e71f3733..ef8c4891d 100644 --- a/tests/3.0.x/__snapshots__/pluginFetch/zodTypesSdk/clients/storeClient.ts +++ b/tests/3.0.x/__snapshots__/pluginFetch/zodTypesSdk/clients/storeClient.ts @@ -3,10 +3,10 @@ * Do not edit manually. */ -import type { ClientConfig, ClientInstance, Options, RequestResult } from '../.kubb/client' +import type { ClientConfig, ClientInstance, Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptionsSchemaType, GetInventoryResponsesSchemaType } from '../zod/getInventorySchema' import type { PlaceOrderOptionsSchemaType, PlaceOrderResponsesSchemaType } from '../zod/placeOrderSchema' -import { createClient } from '../.kubb/client' +import { createClient, withUnwrap } from '../.kubb/client' import { getInventoryResponseSchema } from '../zod/getInventorySchema' import { placeOrderResponseSchema } from '../zod/placeOrderSchema' @@ -22,10 +22,10 @@ export class StoreClient { * @summary Returns pet inventories by status * {@link /store/inventory} */ - public getInventory(options: Options = {}): Promise> { + public getInventory(options: Options = {}): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], validator: { response: getInventoryResponseSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], validator: { response: getInventoryResponseSchema }, ...config }) as Promise>) } /** @@ -33,9 +33,9 @@ export class StoreClient { * @summary Place an order for a pet * {@link /store/order} */ - public placeOrder(options: Options): Promise> { + public placeOrder(options: Options): Unwrappable> { const { client: request = this.client, ...config } = options - return request({ method: 'POST', url: '/store/order', validator: { response: placeOrderResponseSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', validator: { response: placeOrderResponseSchema }, ...config }) as Promise>) } } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/excludeByOperationId/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/groupByTag/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/includeByTag/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/paramsCasing/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginMcp/paramsCasing/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/paramsCasing/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/paramsCasing/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginMcp/paramsCasing/clients/updatePet.ts b/tests/3.0.x/__snapshots__/pluginMcp/paramsCasing/clients/updatePet.ts index c8b4a7c7c..8fab7946f 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/paramsCasing/clients/updatePet.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/paramsCasing/clients/updatePet.ts @@ -3,15 +3,15 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UpdatePetOptions, UpdatePetResponses } from '../types/UpdatePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * {@link /pets/:pet_id} */ -export function updatePet(options: Options): Promise> { +export function updatePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pets/{pet_id}', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pets/{pet_id}', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/petStore/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginMcp/petStore/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/petStore/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/petStore/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/petStore/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginMcp/withClientPlugin/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/hooks/useFindPetsByStatus.ts index ac0b21f96..6d3757c4b 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/hooks/useFindPetsByStatus.ts @@ -18,8 +18,7 @@ export function findPetsByStatusQueryOptions({ query }: FindPetsByStatusOptions return queryOptions, FindPetsByStatusStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/hooks/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/hooks/useGetInventory.ts index 429740790..5cb2467b1 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/hooks/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/hooks/useGetInventory.ts @@ -18,8 +18,7 @@ export function getInventoryQueryOptions(config: Partial, GetInventoryStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/hooks/useGetPetById.ts index a6b173e0c..e1c6a2c02 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/hooks/useGetPetById.ts @@ -18,8 +18,7 @@ export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Part return queryOptions, GetPetByIdStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/hooks/usePlaceOrder.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/hooks/usePlaceOrder.ts index f8b691050..0d18d3336 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/hooks/usePlaceOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/hooks/usePlaceOrder.ts @@ -16,8 +16,7 @@ export function placeOrderMutationOptions(config: Partial, PlaceOrderOptions, TContext>({ mutationKey, mutationFn: async({ body }) => { - const { data } = await placeOrder({ ...config, body, throwOnError: true }) - return data + return placeOrder({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/hooks/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/hooks/useUploadFile.ts index 0265a8474..5f8e980ae 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/hooks/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/excludeByOperationId/hooks/useUploadFile.ts @@ -16,8 +16,7 @@ export function uploadFileMutationOptions(config: Partial, UploadFileOptions, TContext>({ mutationKey, mutationFn: async({ path, query, body }) => { - const { data } = await uploadFile({ ...config, path, query, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, query, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/pet/useAddPet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/pet/useAddPet.ts index 2583987c5..099d53ce2 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/pet/useAddPet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/pet/useAddPet.ts @@ -16,8 +16,7 @@ export function addPetMutationOptions(config: Partial, AddPetOptions, TContext>({ mutationKey, mutationFn: async({ body }) => { - const { data } = await addPet({ ...config, body, throwOnError: true }) - return data + return addPet({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/pet/useDeletePet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/pet/useDeletePet.ts index eaf868ef1..f94b1beb9 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/pet/useDeletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/pet/useDeletePet.ts @@ -16,8 +16,7 @@ export function deletePetMutationOptions(config: Partial, DeletePetOptions, TContext>({ mutationKey, mutationFn: async({ path, headers }) => { - const { data } = await deletePet({ ...config, path, headers, throwOnError: true }) - return data + return deletePet({ ...config, path, headers, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/pet/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/pet/useFindPetsByStatus.ts index 55033810b..7f23d48f6 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/pet/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/pet/useFindPetsByStatus.ts @@ -18,8 +18,7 @@ export function findPetsByStatusQueryOptions({ query }: FindPetsByStatusOptions return queryOptions, FindPetsByStatusStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/pet/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/pet/useGetPetById.ts index c75374ee6..2857d9a02 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/pet/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/pet/useGetPetById.ts @@ -18,8 +18,7 @@ export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Part return queryOptions, GetPetByIdStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/pet/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/pet/useUploadFile.ts index 4233bb97a..d4b0b1d0c 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/pet/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/pet/useUploadFile.ts @@ -16,8 +16,7 @@ export function uploadFileMutationOptions(config: Partial, UploadFileOptions, TContext>({ mutationKey, mutationFn: async({ path, query, body }) => { - const { data } = await uploadFile({ ...config, path, query, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, query, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/store/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/store/useGetInventory.ts index af2066ff4..b188ef801 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/store/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/store/useGetInventory.ts @@ -18,8 +18,7 @@ export function getInventoryQueryOptions(config: Partial, GetInventoryStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/store/usePlaceOrder.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/store/usePlaceOrder.ts index 0c3bac1d1..9b2de3e53 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/store/usePlaceOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/groupByTag/hooks/store/usePlaceOrder.ts @@ -16,8 +16,7 @@ export function placeOrderMutationOptions(config: Partial, PlaceOrderOptions, TContext>({ mutationKey, mutationFn: async({ body }) => { - const { data } = await placeOrder({ ...config, body, throwOnError: true }) - return data + return placeOrder({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/hooks/useAddPet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/hooks/useAddPet.ts index 91b9c0487..ee3cceef5 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/hooks/useAddPet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/hooks/useAddPet.ts @@ -16,8 +16,7 @@ export function addPetMutationOptions(config: Partial, AddPetOptions, TContext>({ mutationKey, mutationFn: async({ body }) => { - const { data } = await addPet({ ...config, body, throwOnError: true }) - return data + return addPet({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/hooks/useDeletePet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/hooks/useDeletePet.ts index 45c8431e7..c2213c5f5 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/hooks/useDeletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/hooks/useDeletePet.ts @@ -16,8 +16,7 @@ export function deletePetMutationOptions(config: Partial, DeletePetOptions, TContext>({ mutationKey, mutationFn: async({ path, headers }) => { - const { data } = await deletePet({ ...config, path, headers, throwOnError: true }) - return data + return deletePet({ ...config, path, headers, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/hooks/useFindPetsByStatus.ts index ac0b21f96..6d3757c4b 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/hooks/useFindPetsByStatus.ts @@ -18,8 +18,7 @@ export function findPetsByStatusQueryOptions({ query }: FindPetsByStatusOptions return queryOptions, FindPetsByStatusStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/hooks/useGetPetById.ts index a6b173e0c..e1c6a2c02 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/hooks/useGetPetById.ts @@ -18,8 +18,7 @@ export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Part return queryOptions, GetPetByIdStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/hooks/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/hooks/useUploadFile.ts index 0265a8474..5f8e980ae 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/hooks/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/includeByTag/hooks/useUploadFile.ts @@ -16,8 +16,7 @@ export function uploadFileMutationOptions(config: Partial, UploadFileOptions, TContext>({ mutationKey, mutationFn: async({ path, query, body }) => { - const { data } = await uploadFile({ ...config, path, query, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, query, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useAddPet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useAddPet.ts index 91b9c0487..ee3cceef5 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useAddPet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useAddPet.ts @@ -16,8 +16,7 @@ export function addPetMutationOptions(config: Partial, AddPetOptions, TContext>({ mutationKey, mutationFn: async({ body }) => { - const { data } = await addPet({ ...config, body, throwOnError: true }) - return data + return addPet({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useDeletePet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useDeletePet.ts index 45c8431e7..c2213c5f5 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useDeletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useDeletePet.ts @@ -16,8 +16,7 @@ export function deletePetMutationOptions(config: Partial, DeletePetOptions, TContext>({ mutationKey, mutationFn: async({ path, headers }) => { - const { data } = await deletePet({ ...config, path, headers, throwOnError: true }) - return data + return deletePet({ ...config, path, headers, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useFindPetsByStatus.ts index ac0b21f96..6d3757c4b 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useFindPetsByStatus.ts @@ -18,8 +18,7 @@ export function findPetsByStatusQueryOptions({ query }: FindPetsByStatusOptions return queryOptions, FindPetsByStatusStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useGetInventory.ts index 429740790..5cb2467b1 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useGetInventory.ts @@ -18,8 +18,7 @@ export function getInventoryQueryOptions(config: Partial, GetInventoryStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useGetPetById.ts index a6b173e0c..e1c6a2c02 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useGetPetById.ts @@ -18,8 +18,7 @@ export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Part return queryOptions, GetPetByIdStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/usePlaceOrder.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/usePlaceOrder.ts index f8b691050..0d18d3336 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/usePlaceOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/usePlaceOrder.ts @@ -16,8 +16,7 @@ export function placeOrderMutationOptions(config: Partial, PlaceOrderOptions, TContext>({ mutationKey, mutationFn: async({ body }) => { - const { data } = await placeOrder({ ...config, body, throwOnError: true }) - return data + return placeOrder({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useUploadFile.ts index 0265a8474..5f8e980ae 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/infinite/hooks/useUploadFile.ts @@ -16,8 +16,7 @@ export function uploadFileMutationOptions(config: Partial, UploadFileOptions, TContext>({ mutationKey, mutationFn: async({ path, query, body }) => { - const { data } = await uploadFile({ ...config, path, query, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, query, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/hooks/useFindPetsByStatus.ts index ac0b21f96..6d3757c4b 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/hooks/useFindPetsByStatus.ts @@ -18,8 +18,7 @@ export function findPetsByStatusQueryOptions({ query }: FindPetsByStatusOptions return queryOptions, FindPetsByStatusStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/hooks/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/hooks/useGetInventory.ts index 429740790..5cb2467b1 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/hooks/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/hooks/useGetInventory.ts @@ -18,8 +18,7 @@ export function getInventoryQueryOptions(config: Partial, GetInventoryStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/hooks/useGetPetById.ts index a6b173e0c..e1c6a2c02 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/mutationDisabled/hooks/useGetPetById.ts @@ -18,8 +18,7 @@ export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Part return queryOptions, GetPetByIdStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/paramsCasing/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/paramsCasing/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/paramsCasing/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/paramsCasing/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/paramsCasing/clients/updatePet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/paramsCasing/clients/updatePet.ts index c8b4a7c7c..8fab7946f 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/paramsCasing/clients/updatePet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/paramsCasing/clients/updatePet.ts @@ -3,15 +3,15 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UpdatePetOptions, UpdatePetResponses } from '../types/UpdatePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * {@link /pets/:pet_id} */ -export function updatePet(options: Options): Promise> { +export function updatePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pets/{pet_id}', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pets/{pet_id}', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/paramsCasing/hooks/useUpdatePet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/paramsCasing/hooks/useUpdatePet.ts index 31bed050a..b77184cfb 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/paramsCasing/hooks/useUpdatePet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/paramsCasing/hooks/useUpdatePet.ts @@ -16,8 +16,7 @@ export function updatePetMutationOptions(config: Partial, UpdatePetOptions, TContext>({ mutationKey, mutationFn: async({ path, query, body, headers }) => { - const { data } = await updatePet({ ...config, path, query, body, headers, throwOnError: true }) - return data + return updatePet({ ...config, path, query, body, headers, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/addPet.ts index 7e5366344..179bf621e 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/addPet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { addPetResponseSchema, addPetErrorSchema } from '../zod/addPetSchema' /** @@ -13,8 +13,8 @@ import { addPetResponseSchema, addPetErrorSchema } from '../zod/addPetSchema' * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], validator: { response: addPetResponseSchema, error: addPetErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], validator: { response: addPetResponseSchema, error: addPetErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/deletePet.ts index 3d3119966..e1a6d8774 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/deletePet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { deletePetResponseSchema, deletePetErrorSchema } from '../zod/deletePetSchema' /** @@ -13,8 +13,8 @@ import { deletePetResponseSchema, deletePetErrorSchema } from '../zod/deletePetS * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], validator: { response: deletePetResponseSchema, error: deletePetErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], validator: { response: deletePetResponseSchema, error: deletePetErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/findPetsByStatus.ts index 2d39d3b70..b37a025b5 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/findPetsByStatus.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { findPetsByStatusResponseSchema, findPetsByStatusErrorSchema } from '../zod/findPetsByStatusSchema' /** @@ -13,8 +13,8 @@ import { findPetsByStatusResponseSchema, findPetsByStatusErrorSchema } from '../ * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, validator: { response: findPetsByStatusResponseSchema, error: findPetsByStatusErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, validator: { response: findPetsByStatusResponseSchema, error: findPetsByStatusErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/getInventory.ts index f4ed2eda7..af8922f7d 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/getInventory.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { getInventoryResponseSchema } from '../zod/getInventorySchema' /** @@ -13,8 +13,8 @@ import { getInventoryResponseSchema } from '../zod/getInventorySchema' * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], validator: { response: getInventoryResponseSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], validator: { response: getInventoryResponseSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/getPetById.ts index aacafef77..70ad99207 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/getPetById.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { getPetByIdResponseSchema, getPetByIdErrorSchema } from '../zod/getPetByIdSchema' /** @@ -13,8 +13,8 @@ import { getPetByIdResponseSchema, getPetByIdErrorSchema } from '../zod/getPetBy * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], validator: { response: getPetByIdResponseSchema, error: getPetByIdErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], validator: { response: getPetByIdResponseSchema, error: getPetByIdErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/placeOrder.ts index 17d73dae2..e1ae15784 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/placeOrder.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { placeOrderResponseSchema, placeOrderErrorSchema } from '../zod/placeOrderSchema' /** @@ -13,8 +13,8 @@ import { placeOrderResponseSchema, placeOrderErrorSchema } from '../zod/placeOrd * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', validator: { response: placeOrderResponseSchema, error: placeOrderErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', validator: { response: placeOrderResponseSchema, error: placeOrderErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/uploadFile.ts index 4933c1401..de69d9c74 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/clients/uploadFile.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { uploadFileResponseSchema } from '../zod/uploadFileSchema' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], validator: { response: uploadFileResponseSchema }, contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], validator: { response: uploadFileResponseSchema }, contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useAddPet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useAddPet.ts index 91b9c0487..ee3cceef5 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useAddPet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useAddPet.ts @@ -16,8 +16,7 @@ export function addPetMutationOptions(config: Partial, AddPetOptions, TContext>({ mutationKey, mutationFn: async({ body }) => { - const { data } = await addPet({ ...config, body, throwOnError: true }) - return data + return addPet({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useDeletePet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useDeletePet.ts index 45c8431e7..c2213c5f5 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useDeletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useDeletePet.ts @@ -16,8 +16,7 @@ export function deletePetMutationOptions(config: Partial, DeletePetOptions, TContext>({ mutationKey, mutationFn: async({ path, headers }) => { - const { data } = await deletePet({ ...config, path, headers, throwOnError: true }) - return data + return deletePet({ ...config, path, headers, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useFindPetsByStatus.ts index ac0b21f96..6d3757c4b 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useFindPetsByStatus.ts @@ -18,8 +18,7 @@ export function findPetsByStatusQueryOptions({ query }: FindPetsByStatusOptions return queryOptions, FindPetsByStatusStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useGetInventory.ts index 429740790..5cb2467b1 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useGetInventory.ts @@ -18,8 +18,7 @@ export function getInventoryQueryOptions(config: Partial, GetInventoryStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useGetPetById.ts index a6b173e0c..e1c6a2c02 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useGetPetById.ts @@ -18,8 +18,7 @@ export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Part return queryOptions, GetPetByIdStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/usePlaceOrder.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/usePlaceOrder.ts index f8b691050..0d18d3336 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/usePlaceOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/usePlaceOrder.ts @@ -16,8 +16,7 @@ export function placeOrderMutationOptions(config: Partial, PlaceOrderOptions, TContext>({ mutationKey, mutationFn: async({ body }) => { - const { data } = await placeOrder({ ...config, body, throwOnError: true }) - return data + return placeOrder({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useUploadFile.ts index 0265a8474..5f8e980ae 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/parserZod/hooks/useUploadFile.ts @@ -16,8 +16,7 @@ export function uploadFileMutationOptions(config: Partial, UploadFileOptions, TContext>({ mutationKey, mutationFn: async({ path, query, body }) => { - const { data } = await uploadFile({ ...config, path, query, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, query, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useAddPet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useAddPet.ts index 91b9c0487..ee3cceef5 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useAddPet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useAddPet.ts @@ -16,8 +16,7 @@ export function addPetMutationOptions(config: Partial, AddPetOptions, TContext>({ mutationKey, mutationFn: async({ body }) => { - const { data } = await addPet({ ...config, body, throwOnError: true }) - return data + return addPet({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useDeletePet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useDeletePet.ts index 45c8431e7..c2213c5f5 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useDeletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useDeletePet.ts @@ -16,8 +16,7 @@ export function deletePetMutationOptions(config: Partial, DeletePetOptions, TContext>({ mutationKey, mutationFn: async({ path, headers }) => { - const { data } = await deletePet({ ...config, path, headers, throwOnError: true }) - return data + return deletePet({ ...config, path, headers, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useFindPetsByStatus.ts index ac0b21f96..6d3757c4b 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useFindPetsByStatus.ts @@ -18,8 +18,7 @@ export function findPetsByStatusQueryOptions({ query }: FindPetsByStatusOptions return queryOptions, FindPetsByStatusStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useGetInventory.ts index 429740790..5cb2467b1 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useGetInventory.ts @@ -18,8 +18,7 @@ export function getInventoryQueryOptions(config: Partial, GetInventoryStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useGetPetById.ts index a6b173e0c..e1c6a2c02 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useGetPetById.ts @@ -18,8 +18,7 @@ export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Part return queryOptions, GetPetByIdStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/usePlaceOrder.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/usePlaceOrder.ts index f8b691050..0d18d3336 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/usePlaceOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/usePlaceOrder.ts @@ -16,8 +16,7 @@ export function placeOrderMutationOptions(config: Partial, PlaceOrderOptions, TContext>({ mutationKey, mutationFn: async({ body }) => { - const { data } = await placeOrder({ ...config, body, throwOnError: true }) - return data + return placeOrder({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useUploadFile.ts index 0265a8474..5f8e980ae 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/petStore/hooks/useUploadFile.ts @@ -16,8 +16,7 @@ export function uploadFileMutationOptions(config: Partial, UploadFileOptions, TContext>({ mutationKey, mutationFn: async({ path, query, body }) => { - const { data } = await uploadFile({ ...config, path, query, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, query, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useAddPet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useAddPet.ts index 91b9c0487..ee3cceef5 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useAddPet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useAddPet.ts @@ -16,8 +16,7 @@ export function addPetMutationOptions(config: Partial, AddPetOptions, TContext>({ mutationKey, mutationFn: async({ body }) => { - const { data } = await addPet({ ...config, body, throwOnError: true }) - return data + return addPet({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useDeletePet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useDeletePet.ts index 45c8431e7..c2213c5f5 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useDeletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useDeletePet.ts @@ -16,8 +16,7 @@ export function deletePetMutationOptions(config: Partial, DeletePetOptions, TContext>({ mutationKey, mutationFn: async({ path, headers }) => { - const { data } = await deletePet({ ...config, path, headers, throwOnError: true }) - return data + return deletePet({ ...config, path, headers, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useFindPetsByStatus.ts index ac0b21f96..6d3757c4b 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useFindPetsByStatus.ts @@ -18,8 +18,7 @@ export function findPetsByStatusQueryOptions({ query }: FindPetsByStatusOptions return queryOptions, FindPetsByStatusStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useGetInventory.ts index 429740790..5cb2467b1 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useGetInventory.ts @@ -18,8 +18,7 @@ export function getInventoryQueryOptions(config: Partial, GetInventoryStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useGetPetById.ts index a6b173e0c..e1c6a2c02 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useGetPetById.ts @@ -18,8 +18,7 @@ export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Part return queryOptions, GetPetByIdStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/usePlaceOrder.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/usePlaceOrder.ts index f8b691050..0d18d3336 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/usePlaceOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/usePlaceOrder.ts @@ -16,8 +16,7 @@ export function placeOrderMutationOptions(config: Partial, PlaceOrderOptions, TContext>({ mutationKey, mutationFn: async({ body }) => { - const { data } = await placeOrder({ ...config, body, throwOnError: true }) - return data + return placeOrder({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useUploadFile.ts index 0265a8474..5f8e980ae 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/queryMethods/hooks/useUploadFile.ts @@ -16,8 +16,7 @@ export function uploadFileMutationOptions(config: Partial, UploadFileOptions, TContext>({ mutationKey, mutationFn: async({ path, query, body }) => { - const { data } = await uploadFile({ ...config, path, query, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, query, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useAddPet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useAddPet.ts index 91b9c0487..ee3cceef5 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useAddPet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useAddPet.ts @@ -16,8 +16,7 @@ export function addPetMutationOptions(config: Partial, AddPetOptions, TContext>({ mutationKey, mutationFn: async({ body }) => { - const { data } = await addPet({ ...config, body, throwOnError: true }) - return data + return addPet({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useDeletePet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useDeletePet.ts index 45c8431e7..c2213c5f5 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useDeletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useDeletePet.ts @@ -16,8 +16,7 @@ export function deletePetMutationOptions(config: Partial, DeletePetOptions, TContext>({ mutationKey, mutationFn: async({ path, headers }) => { - const { data } = await deletePet({ ...config, path, headers, throwOnError: true }) - return data + return deletePet({ ...config, path, headers, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useFindPetsByStatus.ts index ac0b21f96..6d3757c4b 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useFindPetsByStatus.ts @@ -18,8 +18,7 @@ export function findPetsByStatusQueryOptions({ query }: FindPetsByStatusOptions return queryOptions, FindPetsByStatusStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useFindPetsByStatusSuspense.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useFindPetsByStatusSuspense.ts index ba215ddf8..1afcf7e82 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useFindPetsByStatusSuspense.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useFindPetsByStatusSuspense.ts @@ -18,8 +18,7 @@ export function findPetsByStatusSuspenseQueryOptions({ query }: FindPetsByStatus return queryOptions, FindPetsByStatusStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useGetInventory.ts index 429740790..5cb2467b1 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useGetInventory.ts @@ -18,8 +18,7 @@ export function getInventoryQueryOptions(config: Partial, GetInventoryStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useGetInventorySuspense.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useGetInventorySuspense.ts index a30938079..73442315e 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useGetInventorySuspense.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useGetInventorySuspense.ts @@ -18,8 +18,7 @@ export function getInventorySuspenseQueryOptions(config: Partial, GetInventoryStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useGetPetById.ts index a6b173e0c..e1c6a2c02 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useGetPetById.ts @@ -18,8 +18,7 @@ export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Part return queryOptions, GetPetByIdStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useGetPetByIdSuspense.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useGetPetByIdSuspense.ts index 3e204f2f7..286049325 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useGetPetByIdSuspense.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useGetPetByIdSuspense.ts @@ -18,8 +18,7 @@ export function getPetByIdSuspenseQueryOptions({ path }: GetPetByIdOptions, conf return queryOptions, GetPetByIdStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/usePlaceOrder.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/usePlaceOrder.ts index f8b691050..0d18d3336 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/usePlaceOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/usePlaceOrder.ts @@ -16,8 +16,7 @@ export function placeOrderMutationOptions(config: Partial, PlaceOrderOptions, TContext>({ mutationKey, mutationFn: async({ body }) => { - const { data } = await placeOrder({ ...config, body, throwOnError: true }) - return data + return placeOrder({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useUploadFile.ts index 0265a8474..5f8e980ae 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/suspense/hooks/useUploadFile.ts @@ -16,8 +16,7 @@ export function uploadFileMutationOptions(config: Partial, UploadFileOptions, TContext>({ mutationKey, mutationFn: async({ path, query, body }) => { - const { data } = await uploadFile({ ...config, path, query, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, query, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useAddPet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useAddPet.ts index 91b9c0487..ee3cceef5 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useAddPet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useAddPet.ts @@ -16,8 +16,7 @@ export function addPetMutationOptions(config: Partial, AddPetOptions, TContext>({ mutationKey, mutationFn: async({ body }) => { - const { data } = await addPet({ ...config, body, throwOnError: true }) - return data + return addPet({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useDeletePet.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useDeletePet.ts index 45c8431e7..c2213c5f5 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useDeletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useDeletePet.ts @@ -16,8 +16,7 @@ export function deletePetMutationOptions(config: Partial, DeletePetOptions, TContext>({ mutationKey, mutationFn: async({ path, headers }) => { - const { data } = await deletePet({ ...config, path, headers, throwOnError: true }) - return data + return deletePet({ ...config, path, headers, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useFindPetsByStatus.ts index ac0b21f96..6d3757c4b 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useFindPetsByStatus.ts @@ -18,8 +18,7 @@ export function findPetsByStatusQueryOptions({ query }: FindPetsByStatusOptions return queryOptions, FindPetsByStatusStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useGetInventory.ts index 429740790..5cb2467b1 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useGetInventory.ts @@ -18,8 +18,7 @@ export function getInventoryQueryOptions(config: Partial, GetInventoryStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useGetPetById.ts index a6b173e0c..e1c6a2c02 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useGetPetById.ts @@ -18,8 +18,7 @@ export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Part return queryOptions, GetPetByIdStatus200, typeof queryKey>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/usePlaceOrder.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/usePlaceOrder.ts index f8b691050..0d18d3336 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/usePlaceOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/usePlaceOrder.ts @@ -16,8 +16,7 @@ export function placeOrderMutationOptions(config: Partial, PlaceOrderOptions, TContext>({ mutationKey, mutationFn: async({ body }) => { - const { data } = await placeOrder({ ...config, body, throwOnError: true }) - return data + return placeOrder({ ...config, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useUploadFile.ts index 0265a8474..5f8e980ae 100644 --- a/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginReactQuery/withClientPlugin/hooks/useUploadFile.ts @@ -16,8 +16,7 @@ export function uploadFileMutationOptions(config: Partial, UploadFileOptions, TContext>({ mutationKey, mutationFn: async({ path, query, body }) => { - const { data } = await uploadFile({ ...config, path, query, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, query, body, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/hooks/useFindPetsByStatus.ts index 320f4ba0a..424d6e458 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/hooks/useFindPetsByStatus.ts @@ -16,8 +16,7 @@ type FindPetsByStatusQueryKey = ReturnType export function findPetsByStatusQueryOptions({ query }: FindPetsByStatusOptions = {}, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await findPetsByStatus({ ...config, query, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/hooks/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/hooks/useGetInventory.ts index 14780be27..d74713298 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/hooks/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/hooks/useGetInventory.ts @@ -16,8 +16,7 @@ type GetInventoryQueryKey = ReturnType export function getInventoryQueryOptions(config: Partial> = {}) { return { fetcher: async () => { - const { data } = await getInventory({ ...config, throwOnError: true }) - return data + return getInventory({ ...config, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/hooks/useGetPetById.ts index a9d8cb949..c88a12d2c 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/hooks/useGetPetById.ts @@ -16,8 +16,7 @@ type GetPetByIdQueryKey = ReturnType export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await getPetById({ ...config, path, throwOnError: true }) - return data + return getPetById({ ...config, path, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/hooks/usePlaceOrder.ts b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/hooks/usePlaceOrder.ts index 2d04c250a..f570f0cdf 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/hooks/usePlaceOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/hooks/usePlaceOrder.ts @@ -31,8 +31,7 @@ export function usePlaceOrder(options: { return useSWRMutation, PlaceOrderMutationKey | null, PlaceOrderMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { body } }) => { - const { data } = await placeOrder({ ...config, body, throwOnError: true }) - return data + return placeOrder({ ...config, body, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/hooks/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/hooks/useUploadFile.ts index 42a941cf6..6416e4644 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/hooks/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/excludeByOperationId/hooks/useUploadFile.ts @@ -30,8 +30,7 @@ export function useUploadFile(options: { return useSWRMutation, UploadFileMutationKey | null, UploadFileMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { path, query, body } }) => { - const { data } = await uploadFile({ ...config, path, query, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, query, body, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/pet/useAddPet.ts b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/pet/useAddPet.ts index b45c36e6e..62b4ab441 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/pet/useAddPet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/pet/useAddPet.ts @@ -31,8 +31,7 @@ export function useAddPet(options: { return useSWRMutation, AddPetMutationKey | null, AddPetMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { body } }) => { - const { data } = await addPet({ ...config, body, throwOnError: true }) - return data + return addPet({ ...config, body, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/pet/useDeletePet.ts b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/pet/useDeletePet.ts index c01248d83..a0f17d1c8 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/pet/useDeletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/pet/useDeletePet.ts @@ -31,8 +31,7 @@ export function useDeletePet(options: { return useSWRMutation, DeletePetMutationKey | null, DeletePetMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { path, headers } }) => { - const { data } = await deletePet({ ...config, path, headers, throwOnError: true }) - return data + return deletePet({ ...config, path, headers, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/pet/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/pet/useFindPetsByStatus.ts index b0d641501..f03d65272 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/pet/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/pet/useFindPetsByStatus.ts @@ -16,8 +16,7 @@ type FindPetsByStatusQueryKey = ReturnType export function findPetsByStatusQueryOptions({ query }: FindPetsByStatusOptions = {}, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await findPetsByStatus({ ...config, query, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/pet/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/pet/useGetPetById.ts index 26e8827e8..7eedcafee 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/pet/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/pet/useGetPetById.ts @@ -16,8 +16,7 @@ type GetPetByIdQueryKey = ReturnType export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await getPetById({ ...config, path, throwOnError: true }) - return data + return getPetById({ ...config, path, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/pet/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/pet/useUploadFile.ts index 7a66dfaaa..b204314bc 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/pet/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/pet/useUploadFile.ts @@ -30,8 +30,7 @@ export function useUploadFile(options: { return useSWRMutation, UploadFileMutationKey | null, UploadFileMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { path, query, body } }) => { - const { data } = await uploadFile({ ...config, path, query, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, query, body, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/store/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/store/useGetInventory.ts index 1c39cd64d..8c6db9b6d 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/store/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/store/useGetInventory.ts @@ -16,8 +16,7 @@ type GetInventoryQueryKey = ReturnType export function getInventoryQueryOptions(config: Partial> = {}) { return { fetcher: async () => { - const { data } = await getInventory({ ...config, throwOnError: true }) - return data + return getInventory({ ...config, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/store/usePlaceOrder.ts b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/store/usePlaceOrder.ts index 48ec6e6dd..f20b861b6 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/store/usePlaceOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/groupByTag/hooks/store/usePlaceOrder.ts @@ -31,8 +31,7 @@ export function usePlaceOrder(options: { return useSWRMutation, PlaceOrderMutationKey | null, PlaceOrderMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { body } }) => { - const { data } = await placeOrder({ ...config, body, throwOnError: true }) - return data + return placeOrder({ ...config, body, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/hooks/useAddPet.ts b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/hooks/useAddPet.ts index 77a3f9e58..28a2a062b 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/hooks/useAddPet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/hooks/useAddPet.ts @@ -31,8 +31,7 @@ export function useAddPet(options: { return useSWRMutation, AddPetMutationKey | null, AddPetMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { body } }) => { - const { data } = await addPet({ ...config, body, throwOnError: true }) - return data + return addPet({ ...config, body, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/hooks/useDeletePet.ts b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/hooks/useDeletePet.ts index 217e93073..938795032 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/hooks/useDeletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/hooks/useDeletePet.ts @@ -31,8 +31,7 @@ export function useDeletePet(options: { return useSWRMutation, DeletePetMutationKey | null, DeletePetMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { path, headers } }) => { - const { data } = await deletePet({ ...config, path, headers, throwOnError: true }) - return data + return deletePet({ ...config, path, headers, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/hooks/useFindPetsByStatus.ts index 320f4ba0a..424d6e458 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/hooks/useFindPetsByStatus.ts @@ -16,8 +16,7 @@ type FindPetsByStatusQueryKey = ReturnType export function findPetsByStatusQueryOptions({ query }: FindPetsByStatusOptions = {}, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await findPetsByStatus({ ...config, query, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/hooks/useGetPetById.ts index a9d8cb949..c88a12d2c 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/hooks/useGetPetById.ts @@ -16,8 +16,7 @@ type GetPetByIdQueryKey = ReturnType export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await getPetById({ ...config, path, throwOnError: true }) - return data + return getPetById({ ...config, path, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/hooks/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/hooks/useUploadFile.ts index 42a941cf6..6416e4644 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/hooks/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/includeByTag/hooks/useUploadFile.ts @@ -30,8 +30,7 @@ export function useUploadFile(options: { return useSWRMutation, UploadFileMutationKey | null, UploadFileMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { path, query, body } }) => { - const { data } = await uploadFile({ ...config, path, query, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, query, body, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/hooks/useFindPetsByStatus.ts index 320f4ba0a..424d6e458 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/hooks/useFindPetsByStatus.ts @@ -16,8 +16,7 @@ type FindPetsByStatusQueryKey = ReturnType export function findPetsByStatusQueryOptions({ query }: FindPetsByStatusOptions = {}, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await findPetsByStatus({ ...config, query, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/hooks/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/hooks/useGetInventory.ts index 14780be27..d74713298 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/hooks/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/hooks/useGetInventory.ts @@ -16,8 +16,7 @@ type GetInventoryQueryKey = ReturnType export function getInventoryQueryOptions(config: Partial> = {}) { return { fetcher: async () => { - const { data } = await getInventory({ ...config, throwOnError: true }) - return data + return getInventory({ ...config, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/hooks/useGetPetById.ts index a9d8cb949..c88a12d2c 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/mutationDisabled/hooks/useGetPetById.ts @@ -16,8 +16,7 @@ type GetPetByIdQueryKey = ReturnType export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await getPetById({ ...config, path, throwOnError: true }) - return data + return getPetById({ ...config, path, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/paramsCasing/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginSwr/paramsCasing/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/paramsCasing/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/paramsCasing/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginSwr/paramsCasing/clients/updatePet.ts b/tests/3.0.x/__snapshots__/pluginSwr/paramsCasing/clients/updatePet.ts index c8b4a7c7c..8fab7946f 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/paramsCasing/clients/updatePet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/paramsCasing/clients/updatePet.ts @@ -3,15 +3,15 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UpdatePetOptions, UpdatePetResponses } from '../types/UpdatePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * {@link /pets/:pet_id} */ -export function updatePet(options: Options): Promise> { +export function updatePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pets/{pet_id}', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pets/{pet_id}', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/paramsCasing/hooks/useUpdatePet.ts b/tests/3.0.x/__snapshots__/pluginSwr/paramsCasing/hooks/useUpdatePet.ts index b99ed503d..f89424974 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/paramsCasing/hooks/useUpdatePet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/paramsCasing/hooks/useUpdatePet.ts @@ -29,8 +29,7 @@ export function useUpdatePet(options: { return useSWRMutation, UpdatePetMutationKey | null, UpdatePetMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { path, query, body, headers } }) => { - const { data } = await updatePet({ ...config, path, query, body, headers, throwOnError: true }) - return data + return updatePet({ ...config, path, query, body, headers, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/addPet.ts index 7e5366344..179bf621e 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/addPet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { addPetResponseSchema, addPetErrorSchema } from '../zod/addPetSchema' /** @@ -13,8 +13,8 @@ import { addPetResponseSchema, addPetErrorSchema } from '../zod/addPetSchema' * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], validator: { response: addPetResponseSchema, error: addPetErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], validator: { response: addPetResponseSchema, error: addPetErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/deletePet.ts index 3d3119966..e1a6d8774 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/deletePet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { deletePetResponseSchema, deletePetErrorSchema } from '../zod/deletePetSchema' /** @@ -13,8 +13,8 @@ import { deletePetResponseSchema, deletePetErrorSchema } from '../zod/deletePetS * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], validator: { response: deletePetResponseSchema, error: deletePetErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], validator: { response: deletePetResponseSchema, error: deletePetErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/findPetsByStatus.ts index 2d39d3b70..b37a025b5 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/findPetsByStatus.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { findPetsByStatusResponseSchema, findPetsByStatusErrorSchema } from '../zod/findPetsByStatusSchema' /** @@ -13,8 +13,8 @@ import { findPetsByStatusResponseSchema, findPetsByStatusErrorSchema } from '../ * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, validator: { response: findPetsByStatusResponseSchema, error: findPetsByStatusErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, validator: { response: findPetsByStatusResponseSchema, error: findPetsByStatusErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/getInventory.ts index f4ed2eda7..af8922f7d 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/getInventory.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { getInventoryResponseSchema } from '../zod/getInventorySchema' /** @@ -13,8 +13,8 @@ import { getInventoryResponseSchema } from '../zod/getInventorySchema' * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], validator: { response: getInventoryResponseSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], validator: { response: getInventoryResponseSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/getPetById.ts index aacafef77..70ad99207 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/getPetById.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { getPetByIdResponseSchema, getPetByIdErrorSchema } from '../zod/getPetByIdSchema' /** @@ -13,8 +13,8 @@ import { getPetByIdResponseSchema, getPetByIdErrorSchema } from '../zod/getPetBy * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], validator: { response: getPetByIdResponseSchema, error: getPetByIdErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], validator: { response: getPetByIdResponseSchema, error: getPetByIdErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/placeOrder.ts index 17d73dae2..e1ae15784 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/placeOrder.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { placeOrderResponseSchema, placeOrderErrorSchema } from '../zod/placeOrderSchema' /** @@ -13,8 +13,8 @@ import { placeOrderResponseSchema, placeOrderErrorSchema } from '../zod/placeOrd * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', validator: { response: placeOrderResponseSchema, error: placeOrderErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', validator: { response: placeOrderResponseSchema, error: placeOrderErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/uploadFile.ts index 4933c1401..de69d9c74 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/clients/uploadFile.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { uploadFileResponseSchema } from '../zod/uploadFileSchema' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], validator: { response: uploadFileResponseSchema }, contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], validator: { response: uploadFileResponseSchema }, contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useAddPet.ts b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useAddPet.ts index 77a3f9e58..28a2a062b 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useAddPet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useAddPet.ts @@ -31,8 +31,7 @@ export function useAddPet(options: { return useSWRMutation, AddPetMutationKey | null, AddPetMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { body } }) => { - const { data } = await addPet({ ...config, body, throwOnError: true }) - return data + return addPet({ ...config, body, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useDeletePet.ts b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useDeletePet.ts index 217e93073..938795032 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useDeletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useDeletePet.ts @@ -31,8 +31,7 @@ export function useDeletePet(options: { return useSWRMutation, DeletePetMutationKey | null, DeletePetMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { path, headers } }) => { - const { data } = await deletePet({ ...config, path, headers, throwOnError: true }) - return data + return deletePet({ ...config, path, headers, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useFindPetsByStatus.ts index 320f4ba0a..424d6e458 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useFindPetsByStatus.ts @@ -16,8 +16,7 @@ type FindPetsByStatusQueryKey = ReturnType export function findPetsByStatusQueryOptions({ query }: FindPetsByStatusOptions = {}, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await findPetsByStatus({ ...config, query, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useGetInventory.ts index 14780be27..d74713298 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useGetInventory.ts @@ -16,8 +16,7 @@ type GetInventoryQueryKey = ReturnType export function getInventoryQueryOptions(config: Partial> = {}) { return { fetcher: async () => { - const { data } = await getInventory({ ...config, throwOnError: true }) - return data + return getInventory({ ...config, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useGetPetById.ts index a9d8cb949..c88a12d2c 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useGetPetById.ts @@ -16,8 +16,7 @@ type GetPetByIdQueryKey = ReturnType export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await getPetById({ ...config, path, throwOnError: true }) - return data + return getPetById({ ...config, path, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/usePlaceOrder.ts b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/usePlaceOrder.ts index 2d04c250a..f570f0cdf 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/usePlaceOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/usePlaceOrder.ts @@ -31,8 +31,7 @@ export function usePlaceOrder(options: { return useSWRMutation, PlaceOrderMutationKey | null, PlaceOrderMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { body } }) => { - const { data } = await placeOrder({ ...config, body, throwOnError: true }) - return data + return placeOrder({ ...config, body, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useUploadFile.ts index 42a941cf6..6416e4644 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/parserZod/hooks/useUploadFile.ts @@ -30,8 +30,7 @@ export function useUploadFile(options: { return useSWRMutation, UploadFileMutationKey | null, UploadFileMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { path, query, body } }) => { - const { data } = await uploadFile({ ...config, path, query, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, query, body, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/petStore/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginSwr/petStore/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/petStore/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/petStore/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/petStore/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useAddPet.ts b/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useAddPet.ts index 77a3f9e58..28a2a062b 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useAddPet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useAddPet.ts @@ -31,8 +31,7 @@ export function useAddPet(options: { return useSWRMutation, AddPetMutationKey | null, AddPetMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { body } }) => { - const { data } = await addPet({ ...config, body, throwOnError: true }) - return data + return addPet({ ...config, body, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useDeletePet.ts b/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useDeletePet.ts index 217e93073..938795032 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useDeletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useDeletePet.ts @@ -31,8 +31,7 @@ export function useDeletePet(options: { return useSWRMutation, DeletePetMutationKey | null, DeletePetMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { path, headers } }) => { - const { data } = await deletePet({ ...config, path, headers, throwOnError: true }) - return data + return deletePet({ ...config, path, headers, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useFindPetsByStatus.ts index 320f4ba0a..424d6e458 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useFindPetsByStatus.ts @@ -16,8 +16,7 @@ type FindPetsByStatusQueryKey = ReturnType export function findPetsByStatusQueryOptions({ query }: FindPetsByStatusOptions = {}, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await findPetsByStatus({ ...config, query, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useGetInventory.ts index 14780be27..d74713298 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useGetInventory.ts @@ -16,8 +16,7 @@ type GetInventoryQueryKey = ReturnType export function getInventoryQueryOptions(config: Partial> = {}) { return { fetcher: async () => { - const { data } = await getInventory({ ...config, throwOnError: true }) - return data + return getInventory({ ...config, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useGetPetById.ts index a9d8cb949..c88a12d2c 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useGetPetById.ts @@ -16,8 +16,7 @@ type GetPetByIdQueryKey = ReturnType export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await getPetById({ ...config, path, throwOnError: true }) - return data + return getPetById({ ...config, path, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/usePlaceOrder.ts b/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/usePlaceOrder.ts index 2d04c250a..f570f0cdf 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/usePlaceOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/usePlaceOrder.ts @@ -31,8 +31,7 @@ export function usePlaceOrder(options: { return useSWRMutation, PlaceOrderMutationKey | null, PlaceOrderMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { body } }) => { - const { data } = await placeOrder({ ...config, body, throwOnError: true }) - return data + return placeOrder({ ...config, body, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useUploadFile.ts index 42a941cf6..6416e4644 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/petStore/hooks/useUploadFile.ts @@ -30,8 +30,7 @@ export function useUploadFile(options: { return useSWRMutation, UploadFileMutationKey | null, UploadFileMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { path, query, body } }) => { - const { data } = await uploadFile({ ...config, path, query, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, query, body, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useAddPet.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useAddPet.ts index 31b3e2db8..90d5686a8 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useAddPet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useAddPet.ts @@ -14,8 +14,7 @@ type AddPetQueryKey = ReturnType export function addPetQueryOptions({ body }: AddPetOptions, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await addPet({ ...config, body, throwOnError: true }) - return data + return addPet({ ...config, body, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useDeletePet.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useDeletePet.ts index 431fdfb3e..a84130118 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useDeletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useDeletePet.ts @@ -14,8 +14,7 @@ type DeletePetQueryKey = ReturnType export function deletePetQueryOptions({ path, headers }: DeletePetOptions, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await deletePet({ ...config, path, headers, throwOnError: true }) - return data + return deletePet({ ...config, path, headers, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useFindPetsByStatus.ts index e655a88ab..c3bd9bea8 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useFindPetsByStatus.ts @@ -14,8 +14,7 @@ type FindPetsByStatusQueryKey = ReturnType export function findPetsByStatusQueryOptions({ query }: FindPetsByStatusOptions = {}, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await findPetsByStatus({ ...config, query, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useGetInventory.ts index 639428a2c..0c20b9f1c 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useGetInventory.ts @@ -13,8 +13,7 @@ type GetInventoryQueryKey = ReturnType export function getInventoryQueryOptions(config: Partial> = {}) { return { fetcher: async () => { - const { data } = await getInventory({ ...config, throwOnError: true }) - return data + return getInventory({ ...config, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useGetPetById.ts index fa07c0824..553feaa03 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useGetPetById.ts @@ -14,8 +14,7 @@ type GetPetByIdQueryKey = ReturnType export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await getPetById({ ...config, path, throwOnError: true }) - return data + return getPetById({ ...config, path, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/usePlaceOrder.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/usePlaceOrder.ts index dec4fbd40..58bd4a7a5 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/usePlaceOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/usePlaceOrder.ts @@ -14,8 +14,7 @@ type PlaceOrderQueryKey = ReturnType export function placeOrderQueryOptions({ body }: PlaceOrderOptions, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await placeOrder({ ...config, body, throwOnError: true }) - return data + return placeOrder({ ...config, body, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useUploadFile.ts index 8a77ca0da..7e9d69c07 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryDisabled/hooks/useUploadFile.ts @@ -14,8 +14,7 @@ type UploadFileQueryKey = ReturnType export function uploadFileQueryOptions({ path, query, body }: UploadFileOptions, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await uploadFile({ ...config, path, query, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, query, body, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useAddPet.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useAddPet.ts index 77a3f9e58..28a2a062b 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useAddPet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useAddPet.ts @@ -31,8 +31,7 @@ export function useAddPet(options: { return useSWRMutation, AddPetMutationKey | null, AddPetMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { body } }) => { - const { data } = await addPet({ ...config, body, throwOnError: true }) - return data + return addPet({ ...config, body, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useDeletePet.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useDeletePet.ts index 217e93073..938795032 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useDeletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useDeletePet.ts @@ -31,8 +31,7 @@ export function useDeletePet(options: { return useSWRMutation, DeletePetMutationKey | null, DeletePetMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { path, headers } }) => { - const { data } = await deletePet({ ...config, path, headers, throwOnError: true }) - return data + return deletePet({ ...config, path, headers, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useFindPetsByStatus.ts index 320f4ba0a..424d6e458 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useFindPetsByStatus.ts @@ -16,8 +16,7 @@ type FindPetsByStatusQueryKey = ReturnType export function findPetsByStatusQueryOptions({ query }: FindPetsByStatusOptions = {}, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await findPetsByStatus({ ...config, query, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useGetInventory.ts index 14780be27..d74713298 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useGetInventory.ts @@ -16,8 +16,7 @@ type GetInventoryQueryKey = ReturnType export function getInventoryQueryOptions(config: Partial> = {}) { return { fetcher: async () => { - const { data } = await getInventory({ ...config, throwOnError: true }) - return data + return getInventory({ ...config, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useGetPetById.ts index a9d8cb949..c88a12d2c 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useGetPetById.ts @@ -16,8 +16,7 @@ type GetPetByIdQueryKey = ReturnType export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await getPetById({ ...config, path, throwOnError: true }) - return data + return getPetById({ ...config, path, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/usePlaceOrder.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/usePlaceOrder.ts index 2d04c250a..f570f0cdf 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/usePlaceOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/usePlaceOrder.ts @@ -31,8 +31,7 @@ export function usePlaceOrder(options: { return useSWRMutation, PlaceOrderMutationKey | null, PlaceOrderMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { body } }) => { - const { data } = await placeOrder({ ...config, body, throwOnError: true }) - return data + return placeOrder({ ...config, body, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useUploadFile.ts index 42a941cf6..6416e4644 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/queryMethods/hooks/useUploadFile.ts @@ -30,8 +30,7 @@ export function useUploadFile(options: { return useSWRMutation, UploadFileMutationKey | null, UploadFileMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { path, query, body } }) => { - const { data } = await uploadFile({ ...config, path, query, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, query, body, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useAddPet.ts b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useAddPet.ts index 77a3f9e58..28a2a062b 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useAddPet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useAddPet.ts @@ -31,8 +31,7 @@ export function useAddPet(options: { return useSWRMutation, AddPetMutationKey | null, AddPetMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { body } }) => { - const { data } = await addPet({ ...config, body, throwOnError: true }) - return data + return addPet({ ...config, body, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useDeletePet.ts b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useDeletePet.ts index 217e93073..938795032 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useDeletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useDeletePet.ts @@ -31,8 +31,7 @@ export function useDeletePet(options: { return useSWRMutation, DeletePetMutationKey | null, DeletePetMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { path, headers } }) => { - const { data } = await deletePet({ ...config, path, headers, throwOnError: true }) - return data + return deletePet({ ...config, path, headers, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useFindPetsByStatus.ts index 320f4ba0a..424d6e458 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useFindPetsByStatus.ts @@ -16,8 +16,7 @@ type FindPetsByStatusQueryKey = ReturnType export function findPetsByStatusQueryOptions({ query }: FindPetsByStatusOptions = {}, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await findPetsByStatus({ ...config, query, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useGetInventory.ts index 14780be27..d74713298 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useGetInventory.ts @@ -16,8 +16,7 @@ type GetInventoryQueryKey = ReturnType export function getInventoryQueryOptions(config: Partial> = {}) { return { fetcher: async () => { - const { data } = await getInventory({ ...config, throwOnError: true }) - return data + return getInventory({ ...config, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useGetPetById.ts index a9d8cb949..c88a12d2c 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useGetPetById.ts @@ -16,8 +16,7 @@ type GetPetByIdQueryKey = ReturnType export function getPetByIdQueryOptions({ path }: GetPetByIdOptions, config: Partial> = {}) { return { fetcher: async () => { - const { data } = await getPetById({ ...config, path, throwOnError: true }) - return data + return getPetById({ ...config, path, throwOnError: true }).unwrap() }, } } diff --git a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/usePlaceOrder.ts b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/usePlaceOrder.ts index 2d04c250a..f570f0cdf 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/usePlaceOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/usePlaceOrder.ts @@ -31,8 +31,7 @@ export function usePlaceOrder(options: { return useSWRMutation, PlaceOrderMutationKey | null, PlaceOrderMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { body } }) => { - const { data } = await placeOrder({ ...config, body, throwOnError: true }) - return data + return placeOrder({ ...config, body, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useUploadFile.ts index 42a941cf6..6416e4644 100644 --- a/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginSwr/withClientPlugin/hooks/useUploadFile.ts @@ -30,8 +30,7 @@ export function useUploadFile(options: { return useSWRMutation, UploadFileMutationKey | null, UploadFileMutationArg>( shouldFetch ? mutationKey : null, async (_url, { arg: { path, query, body } }) => { - const { data } = await uploadFile({ ...config, path, query, body, throwOnError: true }) - return data + return uploadFile({ ...config, path, query, body, throwOnError: true }).unwrap() }, mutationOptions ) diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/hooks/useFindPetsByStatus.ts index 974d33768..67556cf08 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/hooks/useFindPetsByStatus.ts @@ -20,8 +20,7 @@ export function findPetsByStatusQueryOptions({ query }: { query?: MaybeRefOrGett return queryOptions, FindPetsByStatusStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/hooks/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/hooks/useGetInventory.ts index 7c298abb6..e4f96502f 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/hooks/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/hooks/useGetInventory.ts @@ -19,8 +19,7 @@ export function getInventoryQueryOptions(config: Partial, GetInventoryStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/hooks/useGetPetById.ts index 2a572420c..8be38a998 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/hooks/useGetPetById.ts @@ -20,8 +20,7 @@ export function getPetByIdQueryOptions({ path }: { path: MaybeRefOrGetter, GetPetByIdStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/hooks/usePlaceOrder.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/hooks/usePlaceOrder.ts index 20a10e050..2f55e6449 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/hooks/usePlaceOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/hooks/usePlaceOrder.ts @@ -27,8 +27,7 @@ export function usePlaceOrder(options: { return useMutation, PlaceOrderOptions, TContext>({ mutationFn: async({ body }) => { - const { data } = await placeOrder({ ...config, body: toValue(body), throwOnError: true }) - return data + return placeOrder({ ...config, body: toValue(body), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/hooks/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/hooks/useUploadFile.ts index 2a1ca04bc..7247e006e 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/hooks/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/excludeByOperationId/hooks/useUploadFile.ts @@ -26,8 +26,7 @@ export function useUploadFile(options: { return useMutation, UploadFileOptions, TContext>({ mutationFn: async({ path, query, body }) => { - const { data } = await uploadFile({ ...config, path: toValue(path), query: toValue(query), body: toValue(body), throwOnError: true }) - return data + return uploadFile({ ...config, path: toValue(path), query: toValue(query), body: toValue(body), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/pet/useAddPet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/pet/useAddPet.ts index efc2477dc..403155f19 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/pet/useAddPet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/pet/useAddPet.ts @@ -27,8 +27,7 @@ export function useAddPet(options: { return useMutation, AddPetOptions, TContext>({ mutationFn: async({ body }) => { - const { data } = await addPet({ ...config, body: toValue(body), throwOnError: true }) - return data + return addPet({ ...config, body: toValue(body), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/pet/useDeletePet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/pet/useDeletePet.ts index 771a6a436..a7dc75325 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/pet/useDeletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/pet/useDeletePet.ts @@ -27,8 +27,7 @@ export function useDeletePet(options: { return useMutation, DeletePetOptions, TContext>({ mutationFn: async({ path, headers }) => { - const { data } = await deletePet({ ...config, path: toValue(path), headers: toValue(headers), throwOnError: true }) - return data + return deletePet({ ...config, path: toValue(path), headers: toValue(headers), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/pet/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/pet/useFindPetsByStatus.ts index 8b1f509e4..e407b5ff5 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/pet/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/pet/useFindPetsByStatus.ts @@ -20,8 +20,7 @@ export function findPetsByStatusQueryOptions({ query }: { query?: MaybeRefOrGett return queryOptions, FindPetsByStatusStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/pet/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/pet/useGetPetById.ts index eca6542f5..47dd62a37 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/pet/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/pet/useGetPetById.ts @@ -20,8 +20,7 @@ export function getPetByIdQueryOptions({ path }: { path: MaybeRefOrGetter, GetPetByIdStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/pet/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/pet/useUploadFile.ts index 33302d3fe..e7d7fd90d 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/pet/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/pet/useUploadFile.ts @@ -26,8 +26,7 @@ export function useUploadFile(options: { return useMutation, UploadFileOptions, TContext>({ mutationFn: async({ path, query, body }) => { - const { data } = await uploadFile({ ...config, path: toValue(path), query: toValue(query), body: toValue(body), throwOnError: true }) - return data + return uploadFile({ ...config, path: toValue(path), query: toValue(query), body: toValue(body), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/store/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/store/useGetInventory.ts index a95fd9d0c..765cb8645 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/store/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/store/useGetInventory.ts @@ -19,8 +19,7 @@ export function getInventoryQueryOptions(config: Partial, GetInventoryStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/store/usePlaceOrder.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/store/usePlaceOrder.ts index 7851f6f61..7b41a6686 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/store/usePlaceOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/groupByTag/hooks/store/usePlaceOrder.ts @@ -27,8 +27,7 @@ export function usePlaceOrder(options: { return useMutation, PlaceOrderOptions, TContext>({ mutationFn: async({ body }) => { - const { data } = await placeOrder({ ...config, body: toValue(body), throwOnError: true }) - return data + return placeOrder({ ...config, body: toValue(body), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/hooks/useAddPet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/hooks/useAddPet.ts index 60c495127..d009422eb 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/hooks/useAddPet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/hooks/useAddPet.ts @@ -27,8 +27,7 @@ export function useAddPet(options: { return useMutation, AddPetOptions, TContext>({ mutationFn: async({ body }) => { - const { data } = await addPet({ ...config, body: toValue(body), throwOnError: true }) - return data + return addPet({ ...config, body: toValue(body), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/hooks/useDeletePet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/hooks/useDeletePet.ts index e1cdd224e..2f8853419 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/hooks/useDeletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/hooks/useDeletePet.ts @@ -27,8 +27,7 @@ export function useDeletePet(options: { return useMutation, DeletePetOptions, TContext>({ mutationFn: async({ path, headers }) => { - const { data } = await deletePet({ ...config, path: toValue(path), headers: toValue(headers), throwOnError: true }) - return data + return deletePet({ ...config, path: toValue(path), headers: toValue(headers), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/hooks/useFindPetsByStatus.ts index 974d33768..67556cf08 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/hooks/useFindPetsByStatus.ts @@ -20,8 +20,7 @@ export function findPetsByStatusQueryOptions({ query }: { query?: MaybeRefOrGett return queryOptions, FindPetsByStatusStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/hooks/useGetPetById.ts index 2a572420c..8be38a998 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/hooks/useGetPetById.ts @@ -20,8 +20,7 @@ export function getPetByIdQueryOptions({ path }: { path: MaybeRefOrGetter, GetPetByIdStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/hooks/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/hooks/useUploadFile.ts index 2a1ca04bc..7247e006e 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/hooks/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/includeByTag/hooks/useUploadFile.ts @@ -26,8 +26,7 @@ export function useUploadFile(options: { return useMutation, UploadFileOptions, TContext>({ mutationFn: async({ path, query, body }) => { - const { data } = await uploadFile({ ...config, path: toValue(path), query: toValue(query), body: toValue(body), throwOnError: true }) - return data + return uploadFile({ ...config, path: toValue(path), query: toValue(query), body: toValue(body), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useAddPet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useAddPet.ts index 60c495127..d009422eb 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useAddPet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useAddPet.ts @@ -27,8 +27,7 @@ export function useAddPet(options: { return useMutation, AddPetOptions, TContext>({ mutationFn: async({ body }) => { - const { data } = await addPet({ ...config, body: toValue(body), throwOnError: true }) - return data + return addPet({ ...config, body: toValue(body), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useDeletePet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useDeletePet.ts index e1cdd224e..2f8853419 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useDeletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useDeletePet.ts @@ -27,8 +27,7 @@ export function useDeletePet(options: { return useMutation, DeletePetOptions, TContext>({ mutationFn: async({ path, headers }) => { - const { data } = await deletePet({ ...config, path: toValue(path), headers: toValue(headers), throwOnError: true }) - return data + return deletePet({ ...config, path: toValue(path), headers: toValue(headers), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useFindPetsByStatus.ts index 974d33768..67556cf08 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useFindPetsByStatus.ts @@ -20,8 +20,7 @@ export function findPetsByStatusQueryOptions({ query }: { query?: MaybeRefOrGett return queryOptions, FindPetsByStatusStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useGetInventory.ts index 7c298abb6..e4f96502f 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useGetInventory.ts @@ -19,8 +19,7 @@ export function getInventoryQueryOptions(config: Partial, GetInventoryStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useGetPetById.ts index 2a572420c..8be38a998 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useGetPetById.ts @@ -20,8 +20,7 @@ export function getPetByIdQueryOptions({ path }: { path: MaybeRefOrGetter, GetPetByIdStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/usePlaceOrder.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/usePlaceOrder.ts index 20a10e050..2f55e6449 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/usePlaceOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/usePlaceOrder.ts @@ -27,8 +27,7 @@ export function usePlaceOrder(options: { return useMutation, PlaceOrderOptions, TContext>({ mutationFn: async({ body }) => { - const { data } = await placeOrder({ ...config, body: toValue(body), throwOnError: true }) - return data + return placeOrder({ ...config, body: toValue(body), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useUploadFile.ts index 2a1ca04bc..7247e006e 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/infinite/hooks/useUploadFile.ts @@ -26,8 +26,7 @@ export function useUploadFile(options: { return useMutation, UploadFileOptions, TContext>({ mutationFn: async({ path, query, body }) => { - const { data } = await uploadFile({ ...config, path: toValue(path), query: toValue(query), body: toValue(body), throwOnError: true }) - return data + return uploadFile({ ...config, path: toValue(path), query: toValue(query), body: toValue(body), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/hooks/useFindPetsByStatus.ts index 974d33768..67556cf08 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/hooks/useFindPetsByStatus.ts @@ -20,8 +20,7 @@ export function findPetsByStatusQueryOptions({ query }: { query?: MaybeRefOrGett return queryOptions, FindPetsByStatusStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/hooks/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/hooks/useGetInventory.ts index 7c298abb6..e4f96502f 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/hooks/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/hooks/useGetInventory.ts @@ -19,8 +19,7 @@ export function getInventoryQueryOptions(config: Partial, GetInventoryStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/hooks/useGetPetById.ts index 2a572420c..8be38a998 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/mutationDisabled/hooks/useGetPetById.ts @@ -20,8 +20,7 @@ export function getPetByIdQueryOptions({ path }: { path: MaybeRefOrGetter, GetPetByIdStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/paramsCasing/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/paramsCasing/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/paramsCasing/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/paramsCasing/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/paramsCasing/clients/updatePet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/paramsCasing/clients/updatePet.ts index c8b4a7c7c..8fab7946f 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/paramsCasing/clients/updatePet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/paramsCasing/clients/updatePet.ts @@ -3,15 +3,15 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UpdatePetOptions, UpdatePetResponses } from '../types/UpdatePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * {@link /pets/:pet_id} */ -export function updatePet(options: Options): Promise> { +export function updatePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pets/{pet_id}', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pets/{pet_id}', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/paramsCasing/hooks/useUpdatePet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/paramsCasing/hooks/useUpdatePet.ts index 4e218c213..75d1d4aba 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/paramsCasing/hooks/useUpdatePet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/paramsCasing/hooks/useUpdatePet.ts @@ -25,8 +25,7 @@ export function useUpdatePet(options: { return useMutation, UpdatePetOptions, TContext>({ mutationFn: async({ path, query, body, headers }) => { - const { data } = await updatePet({ ...config, path: toValue(path), query: toValue(query), body: toValue(body), headers: toValue(headers), throwOnError: true }) - return data + return updatePet({ ...config, path: toValue(path), query: toValue(query), body: toValue(body), headers: toValue(headers), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/addPet.ts index 7e5366344..179bf621e 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/addPet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { addPetResponseSchema, addPetErrorSchema } from '../zod/addPetSchema' /** @@ -13,8 +13,8 @@ import { addPetResponseSchema, addPetErrorSchema } from '../zod/addPetSchema' * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], validator: { response: addPetResponseSchema, error: addPetErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], validator: { response: addPetResponseSchema, error: addPetErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/deletePet.ts index 3d3119966..e1a6d8774 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/deletePet.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { deletePetResponseSchema, deletePetErrorSchema } from '../zod/deletePetSchema' /** @@ -13,8 +13,8 @@ import { deletePetResponseSchema, deletePetErrorSchema } from '../zod/deletePetS * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], validator: { response: deletePetResponseSchema, error: deletePetErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], validator: { response: deletePetResponseSchema, error: deletePetErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/findPetsByStatus.ts index 2d39d3b70..b37a025b5 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/findPetsByStatus.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { findPetsByStatusResponseSchema, findPetsByStatusErrorSchema } from '../zod/findPetsByStatusSchema' /** @@ -13,8 +13,8 @@ import { findPetsByStatusResponseSchema, findPetsByStatusErrorSchema } from '../ * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, validator: { response: findPetsByStatusResponseSchema, error: findPetsByStatusErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, validator: { response: findPetsByStatusResponseSchema, error: findPetsByStatusErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/getInventory.ts index f4ed2eda7..af8922f7d 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/getInventory.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { getInventoryResponseSchema } from '../zod/getInventorySchema' /** @@ -13,8 +13,8 @@ import { getInventoryResponseSchema } from '../zod/getInventorySchema' * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], validator: { response: getInventoryResponseSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], validator: { response: getInventoryResponseSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/getPetById.ts index aacafef77..70ad99207 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/getPetById.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { getPetByIdResponseSchema, getPetByIdErrorSchema } from '../zod/getPetByIdSchema' /** @@ -13,8 +13,8 @@ import { getPetByIdResponseSchema, getPetByIdErrorSchema } from '../zod/getPetBy * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], validator: { response: getPetByIdResponseSchema, error: getPetByIdErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], validator: { response: getPetByIdResponseSchema, error: getPetByIdErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/placeOrder.ts index 17d73dae2..e1ae15784 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/placeOrder.ts @@ -3,9 +3,9 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { placeOrderResponseSchema, placeOrderErrorSchema } from '../zod/placeOrderSchema' /** @@ -13,8 +13,8 @@ import { placeOrderResponseSchema, placeOrderErrorSchema } from '../zod/placeOrd * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', validator: { response: placeOrderResponseSchema, error: placeOrderErrorSchema }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', validator: { response: placeOrderResponseSchema, error: placeOrderErrorSchema }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/uploadFile.ts index 4933c1401..de69d9c74 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/clients/uploadFile.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' import { uploadFileResponseSchema } from '../zod/uploadFileSchema' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], validator: { response: uploadFileResponseSchema }, contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], validator: { response: uploadFileResponseSchema }, contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useAddPet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useAddPet.ts index 60c495127..d009422eb 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useAddPet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useAddPet.ts @@ -27,8 +27,7 @@ export function useAddPet(options: { return useMutation, AddPetOptions, TContext>({ mutationFn: async({ body }) => { - const { data } = await addPet({ ...config, body: toValue(body), throwOnError: true }) - return data + return addPet({ ...config, body: toValue(body), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useDeletePet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useDeletePet.ts index e1cdd224e..2f8853419 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useDeletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useDeletePet.ts @@ -27,8 +27,7 @@ export function useDeletePet(options: { return useMutation, DeletePetOptions, TContext>({ mutationFn: async({ path, headers }) => { - const { data } = await deletePet({ ...config, path: toValue(path), headers: toValue(headers), throwOnError: true }) - return data + return deletePet({ ...config, path: toValue(path), headers: toValue(headers), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useFindPetsByStatus.ts index 974d33768..67556cf08 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useFindPetsByStatus.ts @@ -20,8 +20,7 @@ export function findPetsByStatusQueryOptions({ query }: { query?: MaybeRefOrGett return queryOptions, FindPetsByStatusStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useGetInventory.ts index 7c298abb6..e4f96502f 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useGetInventory.ts @@ -19,8 +19,7 @@ export function getInventoryQueryOptions(config: Partial, GetInventoryStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useGetPetById.ts index 2a572420c..8be38a998 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useGetPetById.ts @@ -20,8 +20,7 @@ export function getPetByIdQueryOptions({ path }: { path: MaybeRefOrGetter, GetPetByIdStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/usePlaceOrder.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/usePlaceOrder.ts index 20a10e050..2f55e6449 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/usePlaceOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/usePlaceOrder.ts @@ -27,8 +27,7 @@ export function usePlaceOrder(options: { return useMutation, PlaceOrderOptions, TContext>({ mutationFn: async({ body }) => { - const { data } = await placeOrder({ ...config, body: toValue(body), throwOnError: true }) - return data + return placeOrder({ ...config, body: toValue(body), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useUploadFile.ts index 2a1ca04bc..7247e006e 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/parserZod/hooks/useUploadFile.ts @@ -26,8 +26,7 @@ export function useUploadFile(options: { return useMutation, UploadFileOptions, TContext>({ mutationFn: async({ path, query, body }) => { - const { data } = await uploadFile({ ...config, path: toValue(path), query: toValue(query), body: toValue(body), throwOnError: true }) - return data + return uploadFile({ ...config, path: toValue(path), query: toValue(query), body: toValue(body), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useAddPet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useAddPet.ts index 60c495127..d009422eb 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useAddPet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useAddPet.ts @@ -27,8 +27,7 @@ export function useAddPet(options: { return useMutation, AddPetOptions, TContext>({ mutationFn: async({ body }) => { - const { data } = await addPet({ ...config, body: toValue(body), throwOnError: true }) - return data + return addPet({ ...config, body: toValue(body), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useDeletePet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useDeletePet.ts index e1cdd224e..2f8853419 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useDeletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useDeletePet.ts @@ -27,8 +27,7 @@ export function useDeletePet(options: { return useMutation, DeletePetOptions, TContext>({ mutationFn: async({ path, headers }) => { - const { data } = await deletePet({ ...config, path: toValue(path), headers: toValue(headers), throwOnError: true }) - return data + return deletePet({ ...config, path: toValue(path), headers: toValue(headers), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useFindPetsByStatus.ts index 974d33768..67556cf08 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useFindPetsByStatus.ts @@ -20,8 +20,7 @@ export function findPetsByStatusQueryOptions({ query }: { query?: MaybeRefOrGett return queryOptions, FindPetsByStatusStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useGetInventory.ts index 7c298abb6..e4f96502f 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useGetInventory.ts @@ -19,8 +19,7 @@ export function getInventoryQueryOptions(config: Partial, GetInventoryStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useGetPetById.ts index 2a572420c..8be38a998 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useGetPetById.ts @@ -20,8 +20,7 @@ export function getPetByIdQueryOptions({ path }: { path: MaybeRefOrGetter, GetPetByIdStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/usePlaceOrder.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/usePlaceOrder.ts index 20a10e050..2f55e6449 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/usePlaceOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/usePlaceOrder.ts @@ -27,8 +27,7 @@ export function usePlaceOrder(options: { return useMutation, PlaceOrderOptions, TContext>({ mutationFn: async({ body }) => { - const { data } = await placeOrder({ ...config, body: toValue(body), throwOnError: true }) - return data + return placeOrder({ ...config, body: toValue(body), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useUploadFile.ts index 2a1ca04bc..7247e006e 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/petStore/hooks/useUploadFile.ts @@ -26,8 +26,7 @@ export function useUploadFile(options: { return useMutation, UploadFileOptions, TContext>({ mutationFn: async({ path, query, body }) => { - const { data } = await uploadFile({ ...config, path: toValue(path), query: toValue(query), body: toValue(body), throwOnError: true }) - return data + return uploadFile({ ...config, path: toValue(path), query: toValue(query), body: toValue(body), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/.kubb/client.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/.kubb/client.ts index d928c18f3..9ccace88f 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/.kubb/client.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/.kubb/client.ts @@ -84,6 +84,32 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body. + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one. + * + * @example Full result + * `const { data, error } = await getPetById({ path: { petId: 1 } })` + * + * @example Success body only + * `const pet = await getPetById({ path: { petId: 1 } }).unwrap()` + */ +export function withUnwrap(promise: Promise): Unwrappable { + const unwrappable = promise as Unwrappable + unwrappable.unwrap = () => + promise.then((result) => { + if (result.error !== undefined) throw result.error + return result.data as Extract['data'] + }) + return unwrappable +} + /** * The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation. */ diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/addPet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/addPet.ts index 038e01e98..82de77de2 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/addPet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/addPet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { AddPetOptions, AddPetResponses } from '../types/AddPet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Add a new pet to the store * @summary Add a new pet to the store * {@link /pet} */ -export function addPet(options: Options): Promise> { +export function addPet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/deletePet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/deletePet.ts index 2dba7ae21..079067b15 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/deletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/deletePet.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { DeletePetOptions, DeletePetResponses } from '../types/DeletePet' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description delete a pet * @summary Deletes a pet * {@link /pet/:petId} */ -export function deletePet(options: Options): Promise> { +export function deletePet(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'DELETE', url: '/pet/{petId}', security: [{ type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/findPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/findPetsByStatus.ts index c6309f1e6..d04cecf7a 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/findPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/findPetsByStatus.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { FindPetsByStatusOptions, FindPetsByStatusResponses } from '../types/FindPetsByStatus' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status * {@link /pet/findByStatus} */ -export function findPetsByStatus(options: Options = {}): Promise> { +export function findPetsByStatus(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/findByStatus', security: [{ type: 'oauth2' }], styles: { query: { status: { explode: true } } }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/getInventory.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/getInventory.ts index dd8f690d1..8b7b3ad62 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/getInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/getInventory.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetInventoryOptions, GetInventoryResponses } from '../types/GetInventory' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a map of status codes to quantities * @summary Returns pet inventories by status * {@link /store/inventory} */ -export function getInventory(options: Options = {}): Promise> { +export function getInventory(options: Options = {}): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/store/inventory', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/getPetById.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/getPetById.ts index d8f3570c3..13e4bb40c 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/getPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/getPetById.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { GetPetByIdOptions, GetPetByIdResponses } from '../types/GetPetById' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:petId} */ -export function getPetById(options: Options): Promise> { +export function getPetById(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise> + return withUnwrap(request({ method: 'GET', url: '/pet/{petId}', security: [{ type: 'apiKey', name: 'api_key', in: 'header' }, { type: 'oauth2' }], ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/placeOrder.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/placeOrder.ts index 2ec2f2839..9b72c7a19 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/placeOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/placeOrder.ts @@ -3,17 +3,17 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { PlaceOrderOptions, PlaceOrderResponses } from '../types/PlaceOrder' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @description Place a new order in the store * @summary Place an order for a pet * {@link /store/order} */ -export function placeOrder(options: Options): Promise> { +export function placeOrder(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/store/order', ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/store/order', ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/uploadFile.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/uploadFile.ts index 77a4938ef..33aa10f78 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/uploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/clients/uploadFile.ts @@ -3,16 +3,16 @@ * Do not edit manually. */ -import type { Options, RequestResult } from '../.kubb/client' +import type { Options, Unwrappable, RequestResult } from '../.kubb/client' import type { UploadFileOptions, UploadFileResponses } from '../types/UploadFile' -import { client } from '../.kubb/client' +import { client, withUnwrap } from '../.kubb/client' /** * @summary uploads an image * {@link /pet/:petId/uploadImage} */ -export function uploadFile(options: Options): Promise> { +export function uploadFile(options: Options): Unwrappable> { const { client: request = client, ...config } = options - return request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise> + return withUnwrap(request({ method: 'POST', url: '/pet/{petId}/uploadImage', security: [{ type: 'oauth2' }], contentType: { request: 'application/octet-stream' }, ...config }) as Promise>) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useAddPet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useAddPet.ts index 60c495127..d009422eb 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useAddPet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useAddPet.ts @@ -27,8 +27,7 @@ export function useAddPet(options: { return useMutation, AddPetOptions, TContext>({ mutationFn: async({ body }) => { - const { data } = await addPet({ ...config, body: toValue(body), throwOnError: true }) - return data + return addPet({ ...config, body: toValue(body), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useDeletePet.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useDeletePet.ts index e1cdd224e..2f8853419 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useDeletePet.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useDeletePet.ts @@ -27,8 +27,7 @@ export function useDeletePet(options: { return useMutation, DeletePetOptions, TContext>({ mutationFn: async({ path, headers }) => { - const { data } = await deletePet({ ...config, path: toValue(path), headers: toValue(headers), throwOnError: true }) - return data + return deletePet({ ...config, path: toValue(path), headers: toValue(headers), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useFindPetsByStatus.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useFindPetsByStatus.ts index 974d33768..67556cf08 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useFindPetsByStatus.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useFindPetsByStatus.ts @@ -20,8 +20,7 @@ export function findPetsByStatusQueryOptions({ query }: { query?: MaybeRefOrGett return queryOptions, FindPetsByStatusStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await findPetsByStatus({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }) - return data + return findPetsByStatus({ ...config, query: toValue(query), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useGetInventory.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useGetInventory.ts index 7c298abb6..e4f96502f 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useGetInventory.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useGetInventory.ts @@ -19,8 +19,7 @@ export function getInventoryQueryOptions(config: Partial, GetInventoryStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }) - return data + return getInventory({ ...config, signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useGetPetById.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useGetPetById.ts index 2a572420c..8be38a998 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useGetPetById.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useGetPetById.ts @@ -20,8 +20,7 @@ export function getPetByIdQueryOptions({ path }: { path: MaybeRefOrGetter, GetPetByIdStatus200>({ queryKey, queryFn: async ({ signal }) => { - const { data } = await getPetById({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }) - return data + return getPetById({ ...config, path: toValue(path), signal: config.signal ?? signal, throwOnError: true }).unwrap() }, }) } diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/usePlaceOrder.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/usePlaceOrder.ts index 20a10e050..2f55e6449 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/usePlaceOrder.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/usePlaceOrder.ts @@ -27,8 +27,7 @@ export function usePlaceOrder(options: { return useMutation, PlaceOrderOptions, TContext>({ mutationFn: async({ body }) => { - const { data } = await placeOrder({ ...config, body: toValue(body), throwOnError: true }) - return data + return placeOrder({ ...config, body: toValue(body), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions diff --git a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useUploadFile.ts b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useUploadFile.ts index 2a1ca04bc..7247e006e 100644 --- a/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useUploadFile.ts +++ b/tests/3.0.x/__snapshots__/pluginVueQuery/withClientPlugin/hooks/useUploadFile.ts @@ -26,8 +26,7 @@ export function useUploadFile(options: { return useMutation, UploadFileOptions, TContext>({ mutationFn: async({ path, query, body }) => { - const { data } = await uploadFile({ ...config, path: toValue(path), query: toValue(query), body: toValue(body), throwOnError: true }) - return data + return uploadFile({ ...config, path: toValue(path), query: toValue(query), body: toValue(body), throwOnError: true }).unwrap() }, mutationKey, ...mutationOptions