diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -87,6 +87,15 @@ '@hey-api/client-axios': specifier: workspace:* version: link:../../packages/client-axios + '@radix-ui/react-form': + specifier: 0.0.3 + version: 0.0.3(@types/react-dom@18.3.0)(@types/react@18.3.3)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + '@radix-ui/react-icons': + specifier: 1.3.0 + version: 1.3.0(react@18.3.1) + '@radix-ui/themes': + specifier: 3.0.5 + version: 3.0.5(@types/react-dom@18.3.0)(@types/react@18.3.3)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) axios: specifier: 1.7.2 version: 1.7.2 @@ -115,6 +124,9 @@ '@vitejs/plugin-react': specifier: 4.3.0 version: 4.3.0(vite@5.2.11(@types/node@20.12.12)(less@4.2.0)(sass@1.71.1)(terser@5.31.0)) + autoprefixer: + specifier: 10.4.19 + version: 10.4.19(postcss@8.4.38) eslint: specifier: 9.3.0 version: 9.3.0 @@ -124,9 +136,15 @@ eslint-plugin-react-refresh: specifier: 0.4.7 version: 0.4.7(eslint@9.3.0) + postcss: + specifier: 8.4.38 + version: 8.4.38 prettier: specifier: 3.2.5 version: 3.2.5 + tailwindcss: + specifier: 3.4.3 + version: 3.4.3(ts-node@10.9.2(@types/node@20.12.12)(typescript@5.4.5)) typescript: specifier: 5.4.5 version: 5.4.5 @@ -10624,7 +10642,7 @@ dom-serializer: 2.0.0 domhandler: 5.0.3 htmlparser2: 8.0.2 - postcss: 8.4.35 + postcss: 8.4.38 postcss-media-query-parser: 0.2.3 cross-spawn@5.1.0: @@ -10641,12 +10659,12 @@ css-loader@6.10.0(webpack@5.90.3(esbuild@0.20.1)): dependencies: - icss-utils: 5.1.0(postcss@8.4.35) - postcss: 8.4.35 - postcss-modules-extract-imports: 3.1.0(postcss@8.4.35) - postcss-modules-local-by-default: 4.0.5(postcss@8.4.35) - postcss-modules-scope: 3.2.0(postcss@8.4.35) - postcss-modules-values: 4.0.0(postcss@8.4.35) + icss-utils: 5.1.0(postcss@8.4.38) + postcss: 8.4.38 + postcss-modules-extract-imports: 3.1.0(postcss@8.4.38) + postcss-modules-local-by-default: 4.0.5(postcss@8.4.38) + postcss-modules-scope: 3.2.0(postcss@8.4.38) + postcss-modules-values: 4.0.0(postcss@8.4.38) postcss-value-parser: 4.2.0 semver: 7.6.2 optionalDependencies: @@ -11661,9 +11679,9 @@ dependencies: safer-buffer: 2.1.2 - icss-utils@5.1.0(postcss@8.4.35): + icss-utils@5.1.0(postcss@8.4.38): dependencies: - postcss: 8.4.35 + postcss: 8.4.38 ieee754@1.2.1: {} @@ -12757,26 +12775,26 @@ postcss-media-query-parser@0.2.3: {} - postcss-modules-extract-imports@3.1.0(postcss@8.4.35): + postcss-modules-extract-imports@3.1.0(postcss@8.4.38): dependencies: - postcss: 8.4.35 + postcss: 8.4.38 - postcss-modules-local-by-default@4.0.5(postcss@8.4.35): + postcss-modules-local-by-default@4.0.5(postcss@8.4.38): dependencies: - icss-utils: 5.1.0(postcss@8.4.35) - postcss: 8.4.35 + icss-utils: 5.1.0(postcss@8.4.38) + postcss: 8.4.38 postcss-selector-parser: 6.0.16 postcss-value-parser: 4.2.0 - postcss-modules-scope@3.2.0(postcss@8.4.35): + postcss-modules-scope@3.2.0(postcss@8.4.38): dependencies: - postcss: 8.4.35 + postcss: 8.4.38 postcss-selector-parser: 6.0.16 - postcss-modules-values@4.0.0(postcss@8.4.35): + postcss-modules-values@4.0.0(postcss@8.4.38): dependencies: - icss-utils: 5.1.0(postcss@8.4.35) - postcss: 8.4.35 + icss-utils: 5.1.0(postcss@8.4.38) + postcss: 8.4.38 postcss-nested@6.0.1(postcss@8.4.38): dependencies: @@ -13080,7 +13098,7 @@ adjust-sourcemap-loader: 4.0.0 convert-source-map: 1.9.0 loader-utils: 2.0.4 - postcss: 8.4.35 + postcss: 8.4.38 source-map: 0.6.1 resolve@1.22.8: @@ -13580,7 +13598,7 @@ is-glob: 4.0.3 jiti: 1.21.0 lilconfig: 2.1.0 - micromatch: 4.0.6 + micromatch: 4.0.7 normalize-path: 3.0.0 object-hash: 3.0.0 picocolors: 1.0.1 @@ -13911,7 +13929,7 @@ vite@5.1.7(@types/node@20.12.12)(less@4.2.0)(sass@1.71.1)(terser@5.29.1): dependencies: esbuild: 0.19.12 - postcss: 8.4.35 + postcss: 8.4.38 rollup: 4.18.0 optionalDependencies: '@types/node': 20.12.12 diff --git a/examples/openapi-ts-axios/index.html b/examples/openapi-ts-axios/index.html --- a/examples/openapi-ts-axios/index.html +++ b/examples/openapi-ts-axios/index.html @@ -2,7 +2,7 @@ - + Hey API + Axios Demo diff --git a/examples/openapi-ts-axios/openapi-ts.config.ts b/examples/openapi-ts-axios/openapi-ts.config.ts --- a/examples/openapi-ts-axios/openapi-ts.config.ts +++ b/examples/openapi-ts-axios/openapi-ts.config.ts @@ -1,9 +1,18 @@ import { defineConfig } from '@hey-api/openapi-ts'; export default defineConfig({ - format: 'prettier', + base: 'https://petstore3.swagger.io/api/v3', + client: '@hey-api/client-axios', input: 'https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml', - lint: 'eslint', - output: './src/client', + // 'https://raw.githubusercontent.com/Redocly/museum-openapi-example/main/openapi.yaml', + // '../../packages/openapi-ts/test/spec/v3.json', + output: { + format: 'prettier', + lint: 'eslint', + path: './src/client', + }, + types: { + enums: 'javascript', + }, }); diff --git a/examples/openapi-ts-axios/package.json b/examples/openapi-ts-axios/package.json --- a/examples/openapi-ts-axios/package.json +++ b/examples/openapi-ts-axios/package.json @@ -13,6 +13,9 @@ }, "dependencies": { "@hey-api/client-axios": "workspace:*", + "@radix-ui/react-form": "0.0.3", + "@radix-ui/react-icons": "1.3.0", + "@radix-ui/themes": "3.0.5", "axios": "1.7.2", "react": "18.3.1", "react-dom": "18.3.1" @@ -24,10 +27,13 @@ "@typescript-eslint/eslint-plugin": "7.10.0", "@typescript-eslint/parser": "7.10.0", "@vitejs/plugin-react": "4.3.0", + "autoprefixer": "10.4.19", "eslint": "9.3.0", "eslint-plugin-react-hooks": "4.6.2", "eslint-plugin-react-refresh": "0.4.7", + "postcss": "8.4.38", "prettier": "3.2.5", + "tailwindcss": "3.4.3", "typescript": "5.4.5", "vite": "5.2.11" } diff --git a/examples/openapi-ts-axios/postcss.config.js b/examples/openapi-ts-axios/postcss.config.js new file mode 100644 --- /dev/null +++ b/examples/openapi-ts-axios/postcss.config.js @@ -0,0 +1,6 @@ +export default { + plugins: { + autoprefixer: {}, + tailwindcss: {}, + }, +}; diff --git a/examples/openapi-ts-axios/tailwind.config.js b/examples/openapi-ts-axios/tailwind.config.js new file mode 100644 --- /dev/null +++ b/examples/openapi-ts-axios/tailwind.config.js @@ -0,0 +1,8 @@ +/** @type {import('tailwindcss').Config} */ +export default { + content: ['./index.html', './src/**/*.{html,js,ts,jsx,tsx}'], + plugins: [], + theme: { + extend: {}, + }, +}; diff --git a/examples/openapi-ts-axios/src/App.css b/examples/openapi-ts-axios/src/App.css --- a/examples/openapi-ts-axios/src/App.css +++ b/examples/openapi-ts-axios/src/App.css @@ -1,61 +1,11 @@ -:root { - font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; - line-height: 1.5; - font-weight: 400; - - color-scheme: light dark; - color: rgba(255, 255, 255, 0.87); - background-color: #242424; - - font-synthesis: none; - text-rendering: optimizeLegibility; - -webkit-font-smoothing: antialiased; - -moz-osx-font-smoothing: grayscale; -} - -.flex { - align-items: center; - display: flex; - padding: 1em 0; -} - -.pet-name { - padding: 0 1em; -} - -.button { - border: 1px solid #444; -} - -#root { - max-width: 1280px; - margin: 0 auto; - padding: 0.5rem 2rem; -} - -body { - margin: 0; - display: flex; - place-items: center; - min-width: 320px; - min-height: 100vh; -} - -.h1 { - font-size: 1.2em; - line-height: 1.1; -} +@tailwind base; +@tailwind components; +@tailwind utilities; .logo { height: 4em; will-change: filter; transition: filter 300ms; -} -.logo:hover { - filter: drop-shadow(0 0 2em #646cffaa); -} -.logo.react:hover { - filter: drop-shadow(0 0 2em #61dafbaa); } @keyframes logo-spin { @@ -64,39 +14,5 @@ } to { transform: rotate(360deg); - } -} - -@media (prefers-reduced-motion: no-preference) { - a:nth-of-type(2) .logo { - animation: logo-spin infinite 20s linear; - } -} - -.card { - padding: 2em; -} - -.container { - align-items: center; - display: flex; - grid-gap: 1em; - text-align: center; -} - -.openapi-ts { - color: #444; -} - -@media (prefers-color-scheme: light) { - :root { - color: #213547; - background-color: #ffffff; - } - a:hover { - color: #747bff; - } - button { - background-color: #f9f9f9; } } diff --git a/examples/openapi-ts-axios/src/App.tsx b/examples/openapi-ts-axios/src/App.tsx --- a/examples/openapi-ts-axios/src/App.tsx +++ b/examples/openapi-ts-axios/src/App.tsx @@ -1,58 +1,247 @@ import './App.css'; -import { client, createClient } from '@hey-api/client-axios'; +import { createClient } from '@hey-api/client-axios'; +import * as Form from '@radix-ui/react-form'; +import { DownloadIcon, PlusIcon, ReloadIcon } from '@radix-ui/react-icons'; +import { + Avatar, + Box, + Button, + Card, + Container, + Flex, + Heading, + Section, + Text, + TextField, +} from '@radix-ui/themes'; import { useState } from 'react'; import { $Pet } from './client/schemas.gen'; -import { PetService } from './client/services.gen'; +import { addPet, getPetById, updatePet } from './client/services.gen'; +import type { Pet } from './client/types.gen'; createClient({ + // set default base url for requests baseUrl: 'https://petstore3.swagger.io/api/v3', + // set default headers for requests + headers: { + Authorization: 'Bearer ', + }, }); -// OpenAPI.interceptors.response.use((response) => { -// if (response.status === 200) { -// console.log(`request to ${response.url} was successful`); -// } -// return response; -// }); +const localClient = createClient({ + // set default base url for requests made by this client + baseUrl: 'https://petstore3.swagger.io/api/v3', + global: false, + /** + * Set default headers only for requests made by this client. This is to + * demonstrate local clients and their configuration taking precedence over + * global configuration. + */ + headers: { + Authorization: 'Bearer ', + }, +}); + +localClient.interceptors.request.use((request, options) => { + // Middleware is great for adding authorization tokens to requests made to + // protected paths. Headers are set randomly here to allow surfacing the + // default headers, too. + if ( + options.url === '/pet/{petId}' && + options.method === 'GET' && + Math.random() < 0.5 + ) { + request.headers.set('Authorization', 'Bearer '); + } + return request; +}); function App() { - const [pet, setPet] = - useState>>(); + const [pet, setPet] = useState(); + const [isRequiredNameError, setIsRequiredNameError] = useState(false); - const onFetchPet = async () => { - client.get('/pet/{petId}'); - const pet = await PetService.getPetById({ - // random id 1-10 - petId: Math.floor(Math.random() * (10 - 1 + 1) + 1), + const onAddPet = async (formData: FormData) => { + // simple form field validation to demonstrate using schemas + if ($Pet.required.includes('name') && !formData.get('name')) { + setIsRequiredNameError(true); + return; + } + + const { data, error } = await addPet({ + body: { + category: { + id: 0, + name: formData.get('category') as string, + }, + id: 0, + name: formData.get('name') as string, + photoUrls: ['string'], + status: 'available', + tags: [ + { + id: 0, + name: 'string', + }, + ], + }, }); - setPet(pet); + if (error) { + console.log(error); + return; + } + setPet(data!); + setIsRequiredNameError(false); + }; + + const onGetPetById = async () => { + const { data, error } = await getPetById({ + client: localClient, + path: { + // random id 1-10 + petId: Math.floor(Math.random() * (10 - 1 + 1) + 1), + }, + }); + if (error) { + console.log(error); + return; + } + setPet(data!); + }; + + const onUpdatePet = async () => { + const { data, error } = await updatePet({ + body: { + category: { + id: 0, + name: 'Cats', + }, + id: 2, + name: 'Updated Kitty', + photoUrls: ['string'], + status: 'available', + tags: [ + { + id: 0, + name: 'string', + }, + ], + }, + // setting headers per request + headers: { + Authorization: 'Bearer ', + }, + }); + if (error) { + console.log(error); + return; + } + setPet(data!); }; return ( - <> -
- - Hey API logo - -

@hey-api/openapi-ts 🤝 Axios

-
-
- - Fetched pet's name: {pet?.name} -
-
- {"import { $Pet } from './client/schemas.gen'"} -
{JSON.stringify($Pet, null, 2)}
-
- + + +
+ + + Hey API logo + + @hey-api/openapi-ts 🤝 Axios + +
+ + + + + + + + Name: {pet?.name ?? 'N/A'} + + + Category: {pet?.category?.name ?? 'N/A'} + + + + + + + +
+ + { + event.preventDefault(); + onAddPet(new FormData(event.currentTarget)); + }} + > + +
+ + Name + + {isRequiredNameError && ( + + Please enter a name + + )} +
+ + + +
+ +
+ + Category + + + Please enter a category + +
+ + + +
+ + + + + + +
+
+
+ + ); } diff --git a/examples/openapi-ts-axios/src/index.css b/examples/openapi-ts-axios/src/index.css deleted file mode 100644 --- a/examples/openapi-ts-axios/src/index.css +++ /dev/null @@ -1,68 +0,0 @@ -:root { - font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; - line-height: 1.5; - font-weight: 400; - - color-scheme: light dark; - color: rgba(255, 255, 255, 0.87); - background-color: #242424; - - font-synthesis: none; - text-rendering: optimizeLegibility; - -webkit-font-smoothing: antialiased; - -moz-osx-font-smoothing: grayscale; -} - -a { - font-weight: 500; - color: #646cff; - text-decoration: inherit; -} -a:hover { - color: #535bf2; -} - -body { - margin: 0; - display: flex; - place-items: center; - min-width: 320px; - min-height: 100vh; -} - -h1 { - font-size: 3.2em; - line-height: 1.1; -} - -button { - border-radius: 8px; - border: 1px solid transparent; - padding: 0.6em 1.2em; - font-size: 1em; - font-weight: 500; - font-family: inherit; - background-color: #1a1a1a; - cursor: pointer; - transition: border-color 0.25s; -} -button:hover { - border-color: #646cff; -} -button:focus, -button:focus-visible { - outline: 4px auto -webkit-focus-ring-color; -} - -@media (prefers-color-scheme: light) { - :root { - color: #213547; - background-color: #ffffff; - } - a:hover { - color: #747bff; - } - button { - background-color: #f9f9f9; - } -} diff --git a/examples/openapi-ts-axios/src/main.tsx b/examples/openapi-ts-axios/src/main.tsx --- a/examples/openapi-ts-axios/src/main.tsx +++ b/examples/openapi-ts-axios/src/main.tsx @@ -1,5 +1,6 @@ -import './index.css'; +import '@radix-ui/themes/styles.css'; +import { Theme } from '@radix-ui/themes'; import React from 'react'; import ReactDOM from 'react-dom/client'; @@ -7,6 +8,8 @@ ReactDOM.createRoot(document.getElementById('root')!).render( - + + + , ); diff --git a/examples/openapi-ts-axios/src/client/services.gen.ts b/examples/openapi-ts-axios/src/client/services.gen.ts --- a/examples/openapi-ts-axios/src/client/services.gen.ts +++ b/examples/openapi-ts-axios/src/client/services.gen.ts @@ -1,444 +1,238 @@ // This file is auto-generated by @hey-api/openapi-ts -import type { CancelablePromise } from '@hey-api/client-axios'; -import { OpenAPI } from '@hey-api/client-axios'; -import { request as __request } from '@hey-api/client-axios'; +import { client, type Options } from '@hey-api/client-axios'; -import type { $OpenApiTs } from './types.gen'; +import type { + AddPetData, + AddPetError, + AddPetResponse, + CreateUserData, + CreateUserError, + CreateUserResponse, + CreateUsersWithListInputData, + CreateUsersWithListInputError, + CreateUsersWithListInputResponse, + DeleteOrderData, + DeletePetData, + DeleteUserData, + FindPetsByStatusData, + FindPetsByStatusError, + FindPetsByStatusResponse, + FindPetsByTagsData, + FindPetsByTagsError, + FindPetsByTagsResponse, + GetInventoryError, + GetInventoryResponse, + GetOrderByIdData, + GetOrderByIdError, + GetOrderByIdResponse, + GetPetByIdData, + GetPetByIdError, + GetPetByIdResponse, + GetUserByNameData, + GetUserByNameError, + GetUserByNameResponse, + LoginUserData, + LoginUserError, + LoginUserResponse, + LogoutUserError, + LogoutUserResponse, + PlaceOrderData, + PlaceOrderError, + PlaceOrderResponse, + UpdatePetData, + UpdatePetError, + UpdatePetResponse, + UpdatePetWithFormData, + UpdateUserData, + UpdateUserError, + UpdateUserResponse, + UploadFileData, + UploadFileError, + UploadFileResponse, +} from './types.gen'; -export class PetService { - /** - * Add a new pet to the store - * Add a new pet to the store - * @param data The data for the request. - * @param data.requestBody Create a new pet in the store - * @returns Pet Successful operation - * @throws ApiError - */ - public static addPet( - data: $OpenApiTs['/pet']['post']['req'], - ): CancelablePromise<$OpenApiTs['/pet']['post']['res'][200]> { - return __request(OpenAPI, { - body: data.requestBody, - errors: { - 405: 'Invalid input', - }, - mediaType: 'application/json', - method: 'POST', - url: '/pet', - }); - } +/** + * Add a new pet to the store + * Add a new pet to the store + */ +export const addPet = (options: Options) => (options?.client ?? client).post({ + ...options, + url: '/pet', + }); - /** - * Update an existing pet - * Update an existing pet by Id - * @param data The data for the request. - * @param data.requestBody Update an existent pet in the store - * @returns Pet Successful operation - * @throws ApiError - */ - public static updatePet( - data: $OpenApiTs['/pet']['put']['req'], - ): CancelablePromise<$OpenApiTs['/pet']['put']['res'][200]> { - return __request(OpenAPI, { - body: data.requestBody, - errors: { - 400: 'Invalid ID supplied', - 404: 'Pet not found', - 405: 'Validation exception', - }, - mediaType: 'application/json', - method: 'PUT', - url: '/pet', - }); - } +/** + * Update an existing pet + * Update an existing pet by Id + */ +export const updatePet = (options: Options) => (options?.client ?? client).put({ + ...options, + url: '/pet', + }); - /** - * Finds Pets by status - * Multiple status values can be provided with comma separated strings - * @param data The data for the request. - * @param data.status Status values that need to be considered for filter - * @returns Pet successful operation - * @throws ApiError - */ - public static findPetsByStatus( - data: $OpenApiTs['/pet/findByStatus']['get']['req'] = {}, - ): CancelablePromise<$OpenApiTs['/pet/findByStatus']['get']['res'][200]> { - return __request(OpenAPI, { - errors: { - 400: 'Invalid status value', - }, - method: 'GET', - query: { - status: data.status, - }, - url: '/pet/findByStatus', - }); - } +/** + * Finds Pets by status + * Multiple status values can be provided with comma separated strings + */ +export const findPetsByStatus = (options?: Options) => (options?.client ?? client).get< + FindPetsByStatusResponse, + FindPetsByStatusError + >({ + ...options, + url: '/pet/findByStatus', + }); - /** - * Finds Pets by tags - * Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. - * @param data The data for the request. - * @param data.tags Tags to filter by - * @returns Pet successful operation - * @throws ApiError - */ - public static findPetsByTags( - data: $OpenApiTs['/pet/findByTags']['get']['req'] = {}, - ): CancelablePromise<$OpenApiTs['/pet/findByTags']['get']['res'][200]> { - return __request(OpenAPI, { - errors: { - 400: 'Invalid tag value', - }, - method: 'GET', - query: { - tags: data.tags, - }, - url: '/pet/findByTags', - }); - } +/** + * Finds Pets by tags + * Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. + */ +export const findPetsByTags = (options?: Options) => (options?.client ?? client).get< + FindPetsByTagsResponse, + FindPetsByTagsError + >({ + ...options, + url: '/pet/findByTags', + }); - /** - * Find pet by ID - * Returns a single pet - * @param data The data for the request. - * @param data.petId ID of pet to return - * @returns Pet successful operation - * @throws ApiError - */ - public static getPetById( - data: $OpenApiTs['/pet/{petId}']['get']['req'], - ): CancelablePromise<$OpenApiTs['/pet/{petId}']['get']['res'][200]> { - return __request(OpenAPI, { - errors: { - 400: 'Invalid ID supplied', - 404: 'Pet not found', - }, - method: 'GET', - path: { - petId: data.petId, - }, - url: '/pet/{petId}', - }); - } +/** + * Find pet by ID + * Returns a single pet + */ +export const getPetById = (options: Options) => (options?.client ?? client).get({ + ...options, + url: '/pet/{petId}', + }); - /** - * Updates a pet in the store with form data - * @param data The data for the request. - * @param data.petId ID of pet that needs to be updated - * @param data.name Name of pet that needs to be updated - * @param data.status Status of pet that needs to be updated - * @throws ApiError - */ - public static updatePetWithForm( - data: $OpenApiTs['/pet/{petId}']['post']['req'], - ): CancelablePromise { - return __request(OpenAPI, { - errors: { - 405: 'Invalid input', - }, - method: 'POST', - path: { - petId: data.petId, - }, - query: { - name: data.name, - status: data.status, - }, - url: '/pet/{petId}', - }); - } +/** + * Updates a pet in the store with form data + */ +export const updatePetWithForm = (options: Options) => (options?.client ?? client).post({ + ...options, + url: '/pet/{petId}', + }); - /** - * Deletes a pet - * @param data The data for the request. - * @param data.petId Pet id to delete - * @param data.apiKey - * @throws ApiError - */ - public static deletePet( - data: $OpenApiTs['/pet/{petId}']['delete']['req'], - ): CancelablePromise { - return __request(OpenAPI, { - errors: { - 400: 'Invalid pet value', - }, - headers: { - api_key: data.apiKey, - }, - method: 'DELETE', - path: { - petId: data.petId, - }, - url: '/pet/{petId}', - }); - } +/** + * Deletes a pet + */ +export const deletePet = (options: Options) => (options?.client ?? client).delete({ + ...options, + url: '/pet/{petId}', + }); - /** - * uploads an image - * @param data The data for the request. - * @param data.petId ID of pet to update - * @param data.additionalMetadata Additional Metadata - * @param data.requestBody - * @returns ApiResponse successful operation - * @throws ApiError - */ - public static uploadFile( - data: $OpenApiTs['/pet/{petId}/uploadImage']['post']['req'], - ): CancelablePromise< - $OpenApiTs['/pet/{petId}/uploadImage']['post']['res'][200] - > { - return __request(OpenAPI, { - body: data.requestBody, - mediaType: 'application/octet-stream', - method: 'POST', - path: { - petId: data.petId, - }, - query: { - additionalMetadata: data.additionalMetadata, - }, - url: '/pet/{petId}/uploadImage', - }); - } -} +/** + * uploads an image + */ +export const uploadFile = (options: Options) => (options?.client ?? client).post({ + ...options, + url: '/pet/{petId}/uploadImage', + }); -export class StoreService { - /** - * Returns pet inventories by status - * Returns a map of status codes to quantities - * @returns number successful operation - * @throws ApiError - */ - public static getInventory(): CancelablePromise< - $OpenApiTs['/store/inventory']['get']['res'][200] - > { - return __request(OpenAPI, { - method: 'GET', - url: '/store/inventory', - }); - } +/** + * Returns pet inventories by status + * Returns a map of status codes to quantities + */ +export const getInventory = (options?: Options) => (options?.client ?? client).get< + GetInventoryResponse, + GetInventoryError + >({ + ...options, + url: '/store/inventory', + }); - /** - * Place an order for a pet - * Place a new order in the store - * @param data The data for the request. - * @param data.requestBody - * @returns Order successful operation - * @throws ApiError - */ - public static placeOrder( - data: $OpenApiTs['/store/order']['post']['req'] = {}, - ): CancelablePromise<$OpenApiTs['/store/order']['post']['res'][200]> { - return __request(OpenAPI, { - body: data.requestBody, - errors: { - 405: 'Invalid input', - }, - mediaType: 'application/json', - method: 'POST', - url: '/store/order', - }); - } +/** + * Place an order for a pet + * Place a new order in the store + */ +export const placeOrder = (options?: Options) => (options?.client ?? client).post({ + ...options, + url: '/store/order', + }); - /** - * Find purchase order by ID - * For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions. - * @param data The data for the request. - * @param data.orderId ID of order that needs to be fetched - * @returns Order successful operation - * @throws ApiError - */ - public static getOrderById( - data: $OpenApiTs['/store/order/{orderId}']['get']['req'], - ): CancelablePromise< - $OpenApiTs['/store/order/{orderId}']['get']['res'][200] - > { - return __request(OpenAPI, { - errors: { - 400: 'Invalid ID supplied', - 404: 'Order not found', - }, - method: 'GET', - path: { - orderId: data.orderId, - }, - url: '/store/order/{orderId}', - }); - } +/** + * Find purchase order by ID + * For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions. + */ +export const getOrderById = (options: Options) => (options?.client ?? client).get< + GetOrderByIdResponse, + GetOrderByIdError + >({ + ...options, + url: '/store/order/{orderId}', + }); - /** - * Delete purchase order by ID - * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors - * @param data The data for the request. - * @param data.orderId ID of the order that needs to be deleted - * @throws ApiError - */ - public static deleteOrder( - data: $OpenApiTs['/store/order/{orderId}']['delete']['req'], - ): CancelablePromise { - return __request(OpenAPI, { - errors: { - 400: 'Invalid ID supplied', - 404: 'Order not found', - }, - method: 'DELETE', - path: { - orderId: data.orderId, - }, - url: '/store/order/{orderId}', - }); - } -} +/** + * Delete purchase order by ID + * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors + */ +export const deleteOrder = (options: Options) => (options?.client ?? client).delete({ + ...options, + url: '/store/order/{orderId}', + }); -export class UserService { - /** - * Create user - * This can only be done by the logged in user. - * @param data The data for the request. - * @param data.requestBody Created user object - * @returns User successful operation - * @throws ApiError - */ - public static createUser( - data: $OpenApiTs['/user']['post']['req'] = {}, - ): CancelablePromise<$OpenApiTs['/user']['post']['res'][200]> { - return __request(OpenAPI, { - body: data.requestBody, - mediaType: 'application/json', - method: 'POST', - url: '/user', - }); - } +/** + * Create user + * This can only be done by the logged in user. + */ +export const createUser = (options?: Options) => (options?.client ?? client).post({ + ...options, + url: '/user', + }); - /** - * Creates list of users with given input array - * Creates list of users with given input array - * @param data The data for the request. - * @param data.requestBody - * @returns User Successful operation - * @returns unknown successful operation - * @throws ApiError - */ - public static createUsersWithListInput( - data: $OpenApiTs['/user/createWithList']['post']['req'] = {}, - ): CancelablePromise< - | $OpenApiTs['/user/createWithList']['post']['res'][200] - | $OpenApiTs['/user/createWithList']['post']['res'][200] - > { - return __request(OpenAPI, { - body: data.requestBody, - mediaType: 'application/json', - method: 'POST', - url: '/user/createWithList', - }); - } +/** + * Creates list of users with given input array + * Creates list of users with given input array + */ +export const createUsersWithListInput = ( + options?: Options, +) => (options?.client ?? client).post< + CreateUsersWithListInputResponse, + CreateUsersWithListInputError + >({ + ...options, + url: '/user/createWithList', + }); - /** - * Logs user into the system - * @param data The data for the request. - * @param data.username The user name for login - * @param data.password The password for login in clear text - * @returns string successful operation - * @throws ApiError - */ - public static loginUser( - data: $OpenApiTs['/user/login']['get']['req'] = {}, - ): CancelablePromise<$OpenApiTs['/user/login']['get']['res'][200]> { - return __request(OpenAPI, { - errors: { - 400: 'Invalid username/password supplied', - }, - method: 'GET', - query: { - password: data.password, - username: data.username, - }, - url: '/user/login', - }); - } +/** + * Logs user into the system + */ +export const loginUser = (options?: Options) => (options?.client ?? client).get({ + ...options, + url: '/user/login', + }); - /** - * Logs out current logged in user session - * @returns unknown successful operation - * @throws ApiError - */ - public static logoutUser(): CancelablePromise< - $OpenApiTs['/user/logout']['get']['res'][200] - > { - return __request(OpenAPI, { - method: 'GET', - url: '/user/logout', - }); - } +/** + * Logs out current logged in user session + */ +export const logoutUser = (options?: Options) => (options?.client ?? client).get({ + ...options, + url: '/user/logout', + }); - /** - * Get user by user name - * @param data The data for the request. - * @param data.username The name that needs to be fetched. Use user1 for testing. - * @returns User successful operation - * @throws ApiError - */ - public static getUserByName( - data: $OpenApiTs['/user/{username}']['get']['req'], - ): CancelablePromise<$OpenApiTs['/user/{username}']['get']['res'][200]> { - return __request(OpenAPI, { - errors: { - 400: 'Invalid username supplied', - 404: 'User not found', - }, - method: 'GET', - path: { - username: data.username, - }, - url: '/user/{username}', - }); - } +/** + * Get user by user name + */ +export const getUserByName = (options: Options) => (options?.client ?? client).get< + GetUserByNameResponse, + GetUserByNameError + >({ + ...options, + url: '/user/{username}', + }); - /** - * Update user - * This can only be done by the logged in user. - * @param data The data for the request. - * @param data.username name that needs to be updated - * @param data.requestBody Update an existent user in the store - * @returns unknown successful operation - * @throws ApiError - */ - public static updateUser( - data: $OpenApiTs['/user/{username}']['put']['req'], - ): CancelablePromise<$OpenApiTs['/user/{username}']['put']['res'][200]> { - return __request(OpenAPI, { - body: data.requestBody, - mediaType: 'application/json', - method: 'PUT', - path: { - username: data.username, - }, - url: '/user/{username}', - }); - } +/** + * Update user + * This can only be done by the logged in user. + */ +export const updateUser = (options: Options) => (options?.client ?? client).put({ + ...options, + url: '/user/{username}', + }); - /** - * Delete user - * This can only be done by the logged in user. - * @param data The data for the request. - * @param data.username The name that needs to be deleted - * @throws ApiError - */ - public static deleteUser( - data: $OpenApiTs['/user/{username}']['delete']['req'], - ): CancelablePromise { - return __request(OpenAPI, { - errors: { - 400: 'Invalid username supplied', - 404: 'User not found', - }, - method: 'DELETE', - path: { - username: data.username, - }, - url: '/user/{username}', - }); - } -} +/** + * Delete user + * This can only be done by the logged in user. + */ +export const deleteUser = (options: Options) => (options?.client ?? client).delete({ + ...options, + url: '/user/{username}', + }); diff --git a/examples/openapi-ts-axios/src/client/types.gen.ts b/examples/openapi-ts-axios/src/client/types.gen.ts --- a/examples/openapi-ts-axios/src/client/types.gen.ts +++ b/examples/openapi-ts-axios/src/client/types.gen.ts @@ -12,6 +12,20 @@ complete?: boolean; }; +/** + * Order Status + */ +export type status = 'placed' | 'approved' | 'delivered'; + +/** + * Order Status + */ +export const status = { + APPROVED: 'approved', + DELIVERED: 'delivered', + PLACED: 'placed', +} as const; + export type Customer = { id?: number; username?: string; @@ -61,178 +75,365 @@ status?: 'available' | 'pending' | 'sold'; }; +/** + * pet status in the store + */ +export type status2 = 'available' | 'pending' | 'sold'; + +/** + * pet status in the store + */ +export const status2 = { + AVAILABLE: 'available', + PENDING: 'pending', + SOLD: 'sold', +} as const; + export type ApiResponse = { code?: number; type?: string; message?: string; }; +export type AddPetData = { + /** + * Create a new pet in the store + */ + body: Pet; +}; + +export type AddPetResponse = Pet; + +export type AddPetError = unknown; + +export type UpdatePetData = { + /** + * Update an existent pet in the store + */ + body: Pet; +}; + +export type UpdatePetResponse = Pet; + +export type UpdatePetError = unknown; + +export type FindPetsByStatusData = { + query?: { + /** + * Status values that need to be considered for filter + */ + status?: 'available' | 'pending' | 'sold'; + }; +}; + +export type FindPetsByStatusResponse = Array; + +export type FindPetsByStatusError = unknown; + +export type FindPetsByTagsData = { + query?: { + /** + * Tags to filter by + */ + tags?: Array; + }; +}; + +export type FindPetsByTagsResponse = Array; + +export type FindPetsByTagsError = unknown; + +export type GetPetByIdData = { + path: { + /** + * ID of pet to return + */ + petId: number; + }; +}; + +export type GetPetByIdResponse = Pet; + +export type GetPetByIdError = unknown; + +export type UpdatePetWithFormData = { + path: { + /** + * ID of pet that needs to be updated + */ + petId: number; + }; + query?: { + /** + * Name of pet that needs to be updated + */ + name?: string; + /** + * Status of pet that needs to be updated + */ + status?: string; + }; +}; + +export type DeletePetData = { + headers?: { + api_key?: string; + }; + path: { + /** + * Pet id to delete + */ + petId: number; + }; +}; + +export type UploadFileData = { + body?: Blob | File; + path: { + /** + * ID of pet to update + */ + petId: number; + }; + query?: { + /** + * Additional Metadata + */ + additionalMetadata?: string; + }; +}; + +export type UploadFileResponse = ApiResponse; + +export type UploadFileError = unknown; + +export type GetInventoryResponse = { + [key: string]: number; +}; + +export type GetInventoryError = unknown; + +export type PlaceOrderData = { + body?: Order; +}; + +export type PlaceOrderResponse = Order; + +export type PlaceOrderError = unknown; + +export type GetOrderByIdData = { + path: { + /** + * ID of order that needs to be fetched + */ + orderId: number; + }; +}; + +export type GetOrderByIdResponse = Order; + +export type GetOrderByIdError = unknown; + +export type DeleteOrderData = { + path: { + /** + * ID of the order that needs to be deleted + */ + orderId: number; + }; +}; + +export type CreateUserData = { + /** + * Created user object + */ + body?: User; +}; + +export type CreateUserResponse = User; + +export type CreateUserError = unknown; + +export type CreateUsersWithListInputData = { + body?: Array; +}; + +export type CreateUsersWithListInputResponse = User; + +export type CreateUsersWithListInputError = unknown; + +export type LoginUserData = { + query?: { + /** + * The password for login in clear text + */ + password?: string; + /** + * The user name for login + */ + username?: string; + }; +}; + +export type LoginUserResponse = string; + +export type LoginUserError = unknown; + +export type LogoutUserResponse = unknown; + +export type LogoutUserError = unknown; + +export type GetUserByNameData = { + path: { + /** + * The name that needs to be fetched. Use user1 for testing. + */ + username: string; + }; +}; + +export type GetUserByNameResponse = User; + +export type GetUserByNameError = unknown; + +export type UpdateUserData = { + /** + * Update an existent user in the store + */ + body?: User; + path: { + /** + * name that needs to be updated + */ + username: string; + }; +}; + +export type UpdateUserResponse = unknown; + +export type UpdateUserError = unknown; + +export type DeleteUserData = { + path: { + /** + * The name that needs to be deleted + */ + username: string; + }; +}; + export type $OpenApiTs = { '/pet': { post: { - req: { - /** - * Create a new pet in the store - */ - requestBody: Pet; - }; + req: AddPetData; res: { /** * Successful operation */ - 200: Pet; + '200': Pet; /** * Invalid input */ - 405: unknown; + '405': unknown; }; }; put: { - req: { - /** - * Update an existent pet in the store - */ - requestBody: Pet; - }; + req: UpdatePetData; res: { /** * Successful operation */ - 200: Pet; + '200': Pet; /** * Invalid ID supplied */ - 400: unknown; + '400': unknown; /** * Pet not found */ - 404: unknown; + '404': unknown; /** * Validation exception */ - 405: unknown; + '405': unknown; }; }; }; '/pet/findByStatus': { get: { - req: { - /** - * Status values that need to be considered for filter - */ - status?: 'available' | 'pending' | 'sold'; - }; + req: FindPetsByStatusData; res: { /** * successful operation */ - 200: Array; + '200': Array; /** * Invalid status value */ - 400: unknown; + '400': unknown; }; }; }; '/pet/findByTags': { get: { - req: { - /** - * Tags to filter by - */ - tags?: Array; - }; + req: FindPetsByTagsData; res: { /** * successful operation */ - 200: Array; + '200': Array; /** * Invalid tag value */ - 400: unknown; + '400': unknown; }; }; }; '/pet/{petId}': { get: { - req: { - /** - * ID of pet to return - */ - petId: number; - }; + req: GetPetByIdData; res: { /** * successful operation */ - 200: Pet; + '200': Pet; /** * Invalid ID supplied */ - 400: unknown; + '400': unknown; /** * Pet not found */ - 404: unknown; + '404': unknown; }; }; post: { - req: { - /** - * Name of pet that needs to be updated - */ - name?: string; - /** - * ID of pet that needs to be updated - */ - petId: number; - /** - * Status of pet that needs to be updated - */ - status?: string; - }; + req: UpdatePetWithFormData; res: { /** * Invalid input */ - 405: unknown; + '405': unknown; }; }; delete: { - req: { - apiKey?: string; - /** - * Pet id to delete - */ - petId: number; - }; + req: DeletePetData; res: { /** * Invalid pet value */ - 400: unknown; + '400': unknown; }; }; }; '/pet/{petId}/uploadImage': { post: { - req: { - /** - * Additional Metadata - */ - additionalMetadata?: string; - /** - * ID of pet to update - */ - petId: number; - requestBody?: Blob | File; - }; + req: UploadFileData; res: { /** * successful operation */ - 200: ApiResponse; + '200': ApiResponse; }; }; }; @@ -242,7 +443,7 @@ /** * successful operation */ - 200: { + '200': { [key: string]: number; }; }; @@ -250,113 +451,89 @@ }; '/store/order': { post: { - req: { - requestBody?: Order; - }; + req: PlaceOrderData; res: { /** * successful operation */ - 200: Order; + '200': Order; /** * Invalid input */ - 405: unknown; + '405': unknown; }; }; }; '/store/order/{orderId}': { get: { - req: { - /** - * ID of order that needs to be fetched - */ - orderId: number; - }; + req: GetOrderByIdData; res: { /** * successful operation */ - 200: Order; + '200': Order; /** * Invalid ID supplied */ - 400: unknown; + '400': unknown; /** * Order not found */ - 404: unknown; + '404': unknown; }; }; delete: { - req: { - /** - * ID of the order that needs to be deleted - */ - orderId: number; - }; + req: DeleteOrderData; res: { /** * Invalid ID supplied */ - 400: unknown; + '400': unknown; /** * Order not found */ - 404: unknown; + '404': unknown; }; }; }; '/user': { post: { - req: { - /** - * Created user object - */ - requestBody?: User; - }; + req: CreateUserData; res: { /** * successful operation */ - 200: User; + default: User; }; }; }; '/user/createWithList': { post: { - req: { - requestBody?: Array; - }; + req: CreateUsersWithListInputData; res: { + /** + * Successful operation + */ + '200': User; /** * successful operation */ - 200: unknown; + default: unknown; }; }; }; '/user/login': { get: { - req: { - /** - * The password for login in clear text - */ - password?: string; - /** - * The user name for login - */ - username?: string; - }; + req: LoginUserData; res: { /** * successful operation */ - 200: string; + '200': string; /** * Invalid username/password supplied */ - 400: unknown; + '400': unknown; }; }; }; @@ -366,67 +543,48 @@ /** * successful operation */ - 200: unknown; + default: unknown; }; }; }; '/user/{username}': { get: { - req: { - /** - * The name that needs to be fetched. Use user1 for testing. - */ - username: string; - }; + req: GetUserByNameData; res: { /** * successful operation */ - 200: User; + '200': User; /** * Invalid username supplied */ - 400: unknown; + '400': unknown; /** * User not found */ - 404: unknown; + '404': unknown; }; }; put: { - req: { - /** - * Update an existent user in the store - */ - requestBody?: User; - /** - * name that needs to be updated - */ - username: string; - }; + req: UpdateUserData; res: { /** * successful operation */ - 200: unknown; + default: unknown; }; }; delete: { - req: { - /** - * The name that needs to be deleted - */ - username: string; - }; + req: DeleteUserData; res: { /** * Invalid username supplied */ - 400: unknown; + '400': unknown; /** * User not found */ - 404: unknown; + '404': unknown; }; }; };