From f1c466d3322b35b330c5f7595d5125b53b493e9d Mon Sep 17 00:00:00 2001 From: stijnvanhulle <5904681+stijnvanhulle@users.noreply.github.com> Date: Wed, 2 Sep 2026 11:00:08 +0000 Subject: [PATCH] chore(examples): update generated files --- examples/advanced/src/gen/.kubb/client.ts | 36 +++++++++ .../gen/clients/axios/petService/addFiles.ts | 8 +- .../gen/clients/axios/petService/addPet.ts | 22 +++--- .../gen/clients/axios/petService/deletePet.ts | 22 +++--- .../axios/petService/findPetsByStatus.ts | 22 +++--- .../axios/petService/findPetsByTags.ts | 24 +++--- .../clients/axios/petService/getPetById.ts | 22 +++--- .../gen/clients/axios/petService/updatePet.ts | 22 +++--- .../axios/petService/updatePetWithForm.ts | 22 +++--- .../clients/axios/petService/uploadFile.ts | 22 +++--- .../clients/axios/petsService/createPets.ts | 17 ++-- examples/axios/src/gen/.kubb/client.ts | 36 +++++++++ examples/axios/src/gen/clients/pet/addPet.ts | 10 ++- .../axios/src/gen/clients/pet/deletePet.ts | 12 +-- .../src/gen/clients/pet/findPetsByStatus.ts | 22 +++--- .../src/gen/clients/pet/findPetsByTags.ts | 18 ++--- .../axios/src/gen/clients/pet/getPetById.ts | 17 ++-- .../axios/src/gen/clients/pet/updatePet.ts | 10 ++- .../src/gen/clients/pet/updatePetWithForm.ts | 14 ++-- .../axios/src/gen/clients/pet/uploadFile.ts | 14 ++-- .../src/gen/clients/store/deleteOrder.ts | 8 +- .../src/gen/clients/store/getInventory.ts | 14 ++-- .../src/gen/clients/store/getOrderById.ts | 8 +- .../axios/src/gen/clients/store/placeOrder.ts | 8 +- .../src/gen/clients/store/placeOrderPatch.ts | 8 +- .../axios/src/gen/clients/user/createUser.ts | 8 +- .../clients/user/createUsersWithListInput.ts | 10 ++- .../axios/src/gen/clients/user/deleteUser.ts | 8 +- .../src/gen/clients/user/getUserByName.ts | 8 +- .../axios/src/gen/clients/user/loginUser.ts | 8 +- .../axios/src/gen/clients/user/logoutUser.ts | 8 +- .../axios/src/gen/clients/user/updateUser.ts | 8 +- examples/fetch/src/gen/.kubb/client.ts | 36 +++++++++ examples/fetch/src/gen/clients/pet/addPet.ts | 10 ++- .../fetch/src/gen/clients/pet/deletePet.ts | 12 +-- .../src/gen/clients/pet/findPetsByStatus.ts | 22 +++--- .../src/gen/clients/pet/findPetsByTags.ts | 18 ++--- .../fetch/src/gen/clients/pet/getPetById.ts | 17 ++-- .../fetch/src/gen/clients/pet/updatePet.ts | 10 ++- .../src/gen/clients/pet/updatePetWithForm.ts | 14 ++-- .../fetch/src/gen/clients/pet/uploadFile.ts | 14 ++-- .../src/gen/clients/store/deleteOrder.ts | 8 +- .../src/gen/clients/store/getInventory.ts | 14 ++-- .../src/gen/clients/store/getOrderById.ts | 8 +- .../fetch/src/gen/clients/store/placeOrder.ts | 8 +- .../src/gen/clients/store/placeOrderPatch.ts | 8 +- .../fetch/src/gen/clients/user/createUser.ts | 8 +- .../clients/user/createUsersWithListInput.ts | 10 ++- .../fetch/src/gen/clients/user/deleteUser.ts | 8 +- .../src/gen/clients/user/getUserByName.ts | 8 +- .../fetch/src/gen/clients/user/loginUser.ts | 8 +- .../fetch/src/gen/clients/user/logoutUser.ts | 8 +- .../fetch/src/gen/clients/user/updateUser.ts | 8 +- examples/mcp/src/gen/.kubb/client.ts | 36 +++++++++ examples/mcp/src/gen/clients/addFiles.ts | 8 +- examples/mcp/src/gen/clients/addPet.ts | 10 ++- examples/mcp/src/gen/clients/createPets.ts | 8 +- examples/mcp/src/gen/clients/deleteOrder.ts | 8 +- examples/mcp/src/gen/clients/deletePet.ts | 12 +-- .../mcp/src/gen/clients/findPetsByStatus.ts | 14 ++-- .../mcp/src/gen/clients/findPetsByTags.ts | 18 ++--- examples/mcp/src/gen/clients/getInventory.ts | 14 ++-- examples/mcp/src/gen/clients/getOrderById.ts | 8 +- examples/mcp/src/gen/clients/getPetById.ts | 17 ++-- examples/mcp/src/gen/clients/placeOrder.ts | 8 +- .../mcp/src/gen/clients/placeOrderPatch.ts | 8 +- examples/mcp/src/gen/clients/updatePet.ts | 10 ++- .../mcp/src/gen/clients/updatePetWithForm.ts | 14 ++-- examples/react-query/src/gen/.kubb/client.ts | 36 +++++++++ .../react-query/src/gen/clients/pet/addPet.ts | 10 ++- .../src/gen/clients/pet/deletePet.ts | 12 +-- .../src/gen/clients/pet/findPetsByStatus.ts | 22 +++--- .../src/gen/clients/pet/findPetsByTags.ts | 18 ++--- .../src/gen/clients/pet/getPetById.ts | 17 ++-- .../src/gen/clients/pet/updatePet.ts | 10 ++- .../src/gen/clients/pet/updatePetWithForm.ts | 14 ++-- .../src/gen/clients/pet/uploadFile.ts | 14 ++-- .../src/gen/clients/store/deleteOrder.ts | 8 +- .../src/gen/clients/store/getInventory.ts | 14 ++-- .../src/gen/clients/store/getOrderById.ts | 8 +- .../src/gen/clients/store/placeOrder.ts | 8 +- .../src/gen/clients/store/placeOrderPatch.ts | 8 +- .../src/gen/clients/user/createUser.ts | 8 +- .../clients/user/createUsersWithListInput.ts | 10 ++- .../src/gen/clients/user/deleteUser.ts | 8 +- .../src/gen/clients/user/getUserByName.ts | 8 +- .../src/gen/clients/user/loginUser.ts | 8 +- .../src/gen/clients/user/logoutUser.ts | 8 +- .../src/gen/clients/user/updateUser.ts | 8 +- examples/sdk/src/gen/.kubb/client.ts | 36 +++++++++ examples/sdk/src/gen/sdk/pet/pet.ts | 77 +++++++++++-------- examples/sdk/src/gen/sdk/store/store.ts | 30 ++++---- .../simple-single/src/gen/.kubb/client.ts | 36 +++++++++ .../simple-single/src/gen/clients/addPet.ts | 10 ++- .../src/gen/clients/deleteOrder.ts | 8 +- .../src/gen/clients/deletePet.ts | 12 +-- .../src/gen/clients/findPetsByStatus.ts | 22 +++--- .../src/gen/clients/findPetsByTags.ts | 18 ++--- .../src/gen/clients/getInventory.ts | 14 ++-- .../src/gen/clients/getOrderById.ts | 8 +- .../src/gen/clients/getPetById.ts | 17 ++-- .../src/gen/clients/placeOrder.ts | 8 +- .../src/gen/clients/placeOrderPatch.ts | 8 +- .../src/gen/clients/updatePet.ts | 10 ++- .../src/gen/clients/updatePetWithForm.ts | 14 ++-- .../src/gen/clients/uploadFile.ts | 22 +++--- examples/swr/src/gen/.kubb/client.ts | 36 +++++++++ examples/swr/src/gen/clients/pet/addPet.ts | 10 ++- examples/swr/src/gen/clients/pet/deletePet.ts | 12 +-- .../src/gen/clients/pet/findPetsByStatus.ts | 22 +++--- .../swr/src/gen/clients/pet/findPetsByTags.ts | 18 ++--- .../swr/src/gen/clients/pet/getPetById.ts | 17 ++-- examples/swr/src/gen/clients/pet/updatePet.ts | 10 ++- .../src/gen/clients/pet/updatePetWithForm.ts | 14 ++-- .../swr/src/gen/clients/pet/uploadFile.ts | 14 ++-- .../swr/src/gen/clients/store/deleteOrder.ts | 8 +- .../swr/src/gen/clients/store/getInventory.ts | 14 ++-- .../swr/src/gen/clients/store/getOrderById.ts | 8 +- .../swr/src/gen/clients/store/placeOrder.ts | 8 +- .../src/gen/clients/store/placeOrderPatch.ts | 8 +- .../swr/src/gen/clients/user/createUser.ts | 8 +- .../clients/user/createUsersWithListInput.ts | 10 ++- .../swr/src/gen/clients/user/deleteUser.ts | 8 +- .../swr/src/gen/clients/user/getUserByName.ts | 8 +- .../swr/src/gen/clients/user/loginUser.ts | 8 +- .../swr/src/gen/clients/user/logoutUser.ts | 8 +- .../swr/src/gen/clients/user/updateUser.ts | 8 +- examples/vue-query/src/gen/.kubb/client.ts | 36 +++++++++ .../vue-query/src/gen/clients/pet/addPet.ts | 10 ++- .../src/gen/clients/pet/deletePet.ts | 12 +-- .../src/gen/clients/pet/findPetsByStatus.ts | 22 +++--- .../src/gen/clients/pet/findPetsByTags.ts | 18 ++--- .../src/gen/clients/pet/getPetById.ts | 17 ++-- .../src/gen/clients/pet/updatePet.ts | 10 ++- .../src/gen/clients/pet/updatePetWithForm.ts | 14 ++-- .../src/gen/clients/pet/uploadFile.ts | 14 ++-- .../src/gen/clients/store/deleteOrder.ts | 8 +- .../src/gen/clients/store/getInventory.ts | 14 ++-- .../src/gen/clients/store/getOrderById.ts | 8 +- .../src/gen/clients/store/placeOrder.ts | 8 +- .../src/gen/clients/store/placeOrderPatch.ts | 8 +- .../src/gen/clients/user/createUser.ts | 8 +- .../clients/user/createUsersWithListInput.ts | 10 ++- .../src/gen/clients/user/deleteUser.ts | 8 +- .../src/gen/clients/user/getUserByName.ts | 8 +- .../src/gen/clients/user/loginUser.ts | 8 +- .../src/gen/clients/user/logoutUser.ts | 8 +- .../src/gen/clients/user/updateUser.ts | 8 +- 148 files changed, 1245 insertions(+), 822 deletions(-) diff --git a/examples/advanced/src/gen/.kubb/client.ts b/examples/advanced/src/gen/.kubb/client.ts index 72bedcf64..101818998 100644 --- a/examples/advanced/src/gen/.kubb/client.ts +++ b/examples/advanced/src/gen/.kubb/client.ts @@ -84,6 +84,42 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method, the way Redux Toolkit does it. + * `unwrap()` resolves to the bare success body, or rejects with `error` when the result carried + * one. Only `throwOnError: false` can reach that rejection, because the throwing path never + * resolves an error in the first place. + * + * `Extract` picks the success body by the `error: undefined` discriminant, so an error variant's + * `data: undefined` can never widen it. + * + * @example + * `Unwrappable>` + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a call's result promise. Generated operations wrap every result with this, + * so the same value works as a plain promise and as an unwrap. + * + * @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/axios/src/gen/.kubb/client.ts b/examples/axios/src/gen/.kubb/client.ts index d928c18f3..bc9ab52f3 100644 --- a/examples/axios/src/gen/.kubb/client.ts +++ b/examples/axios/src/gen/.kubb/client.ts @@ -84,6 +84,42 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method, the way Redux Toolkit does it. + * `unwrap()` resolves to the bare success body, or rejects with `error` when the result carried + * one. Only `throwOnError: false` can reach that rejection, because the throwing path never + * resolves an error in the first place. + * + * `Extract` picks the success body by the `error: undefined` discriminant, so an error variant's + * `data: undefined` can never widen it. + * + * @example + * `Unwrappable>` + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a call's result promise. Generated operations wrap every result with this, + * so the same value works as a plain promise and as an unwrap. + * + * @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..52b3b743f 100644 --- a/examples/fetch/src/gen/.kubb/client.ts +++ b/examples/fetch/src/gen/.kubb/client.ts @@ -82,6 +82,42 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method, the way Redux Toolkit does it. + * `unwrap()` resolves to the bare success body, or rejects with `error` when the result carried + * one. Only `throwOnError: false` can reach that rejection, because the throwing path never + * resolves an error in the first place. + * + * `Extract` picks the success body by the `error: undefined` discriminant, so an error variant's + * `data: undefined` can never widen it. + * + * @example + * `Unwrappable>` + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a call's result promise. Generated operations wrap every result with this, + * so the same value works as a plain promise and as an unwrap. + * + * @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..467f05b72 100644 --- a/examples/mcp/src/gen/.kubb/client.ts +++ b/examples/mcp/src/gen/.kubb/client.ts @@ -84,6 +84,42 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method, the way Redux Toolkit does it. + * `unwrap()` resolves to the bare success body, or rejects with `error` when the result carried + * one. Only `throwOnError: false` can reach that rejection, because the throwing path never + * resolves an error in the first place. + * + * `Extract` picks the success body by the `error: undefined` discriminant, so an error variant's + * `data: undefined` can never widen it. + * + * @example + * `Unwrappable>` + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a call's result promise. Generated operations wrap every result with this, + * so the same value works as a plain promise and as an unwrap. + * + * @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..52b3b743f 100644 --- a/examples/react-query/src/gen/.kubb/client.ts +++ b/examples/react-query/src/gen/.kubb/client.ts @@ -82,6 +82,42 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method, the way Redux Toolkit does it. + * `unwrap()` resolves to the bare success body, or rejects with `error` when the result carried + * one. Only `throwOnError: false` can reach that rejection, because the throwing path never + * resolves an error in the first place. + * + * `Extract` picks the success body by the `error: undefined` discriminant, so an error variant's + * `data: undefined` can never widen it. + * + * @example + * `Unwrappable>` + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a call's result promise. Generated operations wrap every result with this, + * so the same value works as a plain promise and as an unwrap. + * + * @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/sdk/src/gen/.kubb/client.ts b/examples/sdk/src/gen/.kubb/client.ts index f80182aca..52b3b743f 100644 --- a/examples/sdk/src/gen/.kubb/client.ts +++ b/examples/sdk/src/gen/.kubb/client.ts @@ -82,6 +82,42 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method, the way Redux Toolkit does it. + * `unwrap()` resolves to the bare success body, or rejects with `error` when the result carried + * one. Only `throwOnError: false` can reach that rejection, because the throwing path never + * resolves an error in the first place. + * + * `Extract` picks the success body by the `error: undefined` discriminant, so an error variant's + * `data: undefined` can never widen it. + * + * @example + * `Unwrappable>` + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a call's result promise. Generated operations wrap every result with this, + * so the same value works as a plain promise and as an unwrap. + * + * @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..52b3b743f 100644 --- a/examples/simple-single/src/gen/.kubb/client.ts +++ b/examples/simple-single/src/gen/.kubb/client.ts @@ -82,6 +82,42 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method, the way Redux Toolkit does it. + * `unwrap()` resolves to the bare success body, or rejects with `error` when the result carried + * one. Only `throwOnError: false` can reach that rejection, because the throwing path never + * resolves an error in the first place. + * + * `Extract` picks the success body by the `error: undefined` discriminant, so an error variant's + * `data: undefined` can never widen it. + * + * @example + * `Unwrappable>` + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a call's result promise. Generated operations wrap every result with this, + * so the same value works as a plain promise and as an unwrap. + * + * @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/swr/src/gen/.kubb/client.ts b/examples/swr/src/gen/.kubb/client.ts index f80182aca..52b3b743f 100644 --- a/examples/swr/src/gen/.kubb/client.ts +++ b/examples/swr/src/gen/.kubb/client.ts @@ -82,6 +82,42 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method, the way Redux Toolkit does it. + * `unwrap()` resolves to the bare success body, or rejects with `error` when the result carried + * one. Only `throwOnError: false` can reach that rejection, because the throwing path never + * resolves an error in the first place. + * + * `Extract` picks the success body by the `error: undefined` discriminant, so an error variant's + * `data: undefined` can never widen it. + * + * @example + * `Unwrappable>` + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a call's result promise. Generated operations wrap every result with this, + * so the same value works as a plain promise and as an unwrap. + * + * @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/vue-query/src/gen/.kubb/client.ts b/examples/vue-query/src/gen/.kubb/client.ts index f80182aca..52b3b743f 100644 --- a/examples/vue-query/src/gen/.kubb/client.ts +++ b/examples/vue-query/src/gen/.kubb/client.ts @@ -82,6 +82,42 @@ export type RequestResult +/** + * A `RequestResult` promise with an extra `unwrap()` method, the way Redux Toolkit does it. + * `unwrap()` resolves to the bare success body, or rejects with `error` when the result carried + * one. Only `throwOnError: false` can reach that rejection, because the throwing path never + * resolves an error in the first place. + * + * `Extract` picks the success body by the `error: undefined` discriminant, so an error variant's + * `data: undefined` can never widen it. + * + * @example + * `Unwrappable>` + */ +export type Unwrappable = Promise & { + unwrap: () => Promise['data']> +} + +/** + * Attaches `unwrap()` to a call's result promise. Generated operations wrap every result with this, + * so the same value works as a plain promise and as an unwrap. + * + * @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>) }