### SDK Example with Module and Setup Configuration Source: https://heyapi.dev/docs/openapi/typescript/plugins/sdk Example demonstrating SDK usage after configuring `moduleName` and `setupName` for practical examples. ```typescript import { PetStore } from '@petstore/client'; const client = new PetStore(); await client.addPet(); ``` -------------------------------- ### SDK Plugin Configuration (Module and Setup) Source: https://heyapi.dev/docs/openapi/typescript/plugins/sdk Configuration for the SDK plugin to specify `moduleName` and `setupName` for example generation. ```javascript export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', plugins: [ // ...other plugins { examples: { moduleName: '@petstore/client', setupName: 'client', }, name: '@hey-api/sdk', operations: { containerName: 'PetStore', strategy: 'single', }, }, ], }; ``` -------------------------------- ### Start @hey-api/openapi-ts Development Source: https://heyapi.dev/docs/openapi/typescript/community/contributing/building Run this command to start the development server for the @hey-api/openapi-ts package. Ensure you have pnpm installed and are in the project root. ```bash pnpm --filter @hey-api/openapi-ts dev ``` -------------------------------- ### Basic SDK Example Source: https://heyapi.dev/docs/openapi/typescript/plugins/sdk A basic example showing how to import and use an SDK method. This is generated with `examples: true`. ```typescript import { PetStore } from 'your-package'; await new PetStore().addPet(); ``` -------------------------------- ### SDK Example with Initialization Configuration Source: https://heyapi.dev/docs/openapi/typescript/plugins/sdk Example showing SDK initialization with an API key, controlled by `importSetup`. ```typescript import { PetStore } from '@petstore/client'; const client = new PetStore({ apiKey: 'YOUR_API_KEY', }); await client.addPet(); ``` -------------------------------- ### Install @hey-api/openapi-python with bun Source: https://heyapi.dev/docs/openapi/python/get-started Install the package as a development dependency using bun. ```sh bun add @hey-api/openapi-python -D -E ``` -------------------------------- ### Install @hey-api/openapi-ts with bun Source: https://heyapi.dev/docs Install the @hey-api/openapi-ts package using bun. ```bash bun add @hey-api/openapi-ts -D ``` -------------------------------- ### SDK Plugin Configuration (Default Examples) Source: https://heyapi.dev/docs/openapi/typescript/plugins/sdk Configuration for the SDK plugin to enable default code examples. ```javascript export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', plugins: [ // ...other plugins { examples: true, name: '@hey-api/sdk', operations: { containerName: 'PetStore', strategy: 'single', }, }, ], }; ``` -------------------------------- ### Example Fetch URL with Organization and Project Source: https://heyapi.dev/docs/openapi/typescript/integrations An example of a fetch URL using a specific organization 'foo' and project 'bar'. ```plaintext https://get.heyapi.dev/foo/bar ``` -------------------------------- ### Install @hey-api/vite-plugin with bun Source: https://heyapi.dev/docs/openapi/typescript/configuration/vite Install the Vite plugin as a development dependency using bun. ```bash bun add @hey-api/vite-plugin -D ``` -------------------------------- ### Update Example Code Source: https://heyapi.dev/docs/openapi/typescript/community/contributing/developing Run this command to regenerate client code in all examples after making changes to core packages. ```sh pnpm examples:update ``` -------------------------------- ### CLI Installation for OFetch Client Source: https://heyapi.dev/docs/openapi/typescript/clients/ofetch Install and configure the ofetch client using the openapi-ts CLI. Ensure you provide the input OpenAPI spec, output directory, and the client-ofetch plugin. ```bash npx @hey-api/openapi-ts \ -i hey-api/backend \ -o src/client \ -c @hey-api/client-ofetch ``` -------------------------------- ### Install @hey-api/openapi-python with npm Source: https://heyapi.dev/docs/openapi/python/get-started Install the package as a development dependency using npm. ```sh npm install @hey-api/openapi-python -D -E ``` -------------------------------- ### SDK Example with Custom Import Style Source: https://heyapi.dev/docs/openapi/typescript/plugins/sdk Example demonstrating SDK usage with a custom import style, using `importName` and `importKind`. ```typescript import CatStore from '@petstore/client'; const client = new CatStore({ apiKey: 'YOUR_API_KEY', }); await client.addPet(); ``` -------------------------------- ### Install @hey-api/openapi-python with pnpm Source: https://heyapi.dev/docs/openapi/python/get-started Install the package as a development dependency using pnpm. ```sh pnpm add @hey-api/openapi-python -D -E ``` -------------------------------- ### Define API with OpenAPI Source: https://heyapi.dev/codegen/openapi/typescript Start by defining your API using the OpenAPI specification. This example shows a basic OpenAPI 3.2.0 structure for an Equity Trading API. ```yaml openapi: 3.2.0 info: title: Equity Trading API version: 1.2.0 paths: /orders: post: operationId: createOrder requestBody: required: true content: application/json: schema: $ref: '#/components/schemas/Order' responses: '201': description: Order placed content: application/json: schema: $ref: '#/components/schemas/Order' ``` -------------------------------- ### Check Example Code Source: https://heyapi.dev/docs/openapi/typescript/community/contributing/developing Use this command to ensure all generated example code is up-to-date before committing. It regenerates code and checks for modifications. ```sh pnpm examples:check ``` -------------------------------- ### Install @hey-api/openapi-python with yarn Source: https://heyapi.dev/docs/openapi/python/get-started Install the package as a development dependency using yarn. ```sh yarn add @hey-api/openapi-python -D -E ``` -------------------------------- ### Install @hey-api/vite-plugin with npm Source: https://heyapi.dev/docs/openapi/typescript/configuration/vite Install the Vite plugin as a development dependency using npm. ```bash npm install @hey-api/vite-plugin -D -E ``` -------------------------------- ### Install @hey-api/vite-plugin with yarn Source: https://heyapi.dev/docs/openapi/typescript/configuration/vite Install the Vite plugin as a development dependency using yarn. ```bash yarn add @hey-api/vite-plugin -D -E ``` -------------------------------- ### SDK Plugin Configuration (Initialization) Source: https://heyapi.dev/docs/openapi/typescript/plugins/sdk Configuration for the SDK plugin to customize SDK initialization in examples using `importSetup`. ```javascript export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', plugins: [ // ...other plugins { examples: { importSetup: ({ $, node }) => $.new( node.name, $.object() .pretty() .prop('apiKey', $.literal('YOUR_API_KEY')), ), moduleName: '@petstore/client', setupName: 'client', }, name: '@hey-api/sdk', operations: { containerName: 'PetStore', strategy: 'single', }, }, ], }; ``` -------------------------------- ### CLI Installation with Axios Plugin Source: https://heyapi.dev/docs/openapi/typescript/clients/axios Install and configure the Axios client using the openapi-ts CLI by specifying the client-axios plugin with the -c flag. ```bash npx @hey-api/openapi-ts \ -i hey-api/backend \ -o src/client \ -c @hey-api/client-axios ``` -------------------------------- ### Install @hey-api/vite-plugin with pnpm Source: https://heyapi.dev/docs/openapi/typescript/configuration/vite Install the Vite plugin as a development dependency using pnpm. ```bash pnpm add @hey-api/vite-plugin -D -E ``` -------------------------------- ### Install @hey-api/openapi-ts with npm Source: https://heyapi.dev/docs Install the @hey-api/openapi-ts package as a development dependency using npm. ```bash npm install @hey-api/openapi-ts -D -E ``` -------------------------------- ### Uninstall Old Client Package Source: https://heyapi.dev/docs/openapi/typescript/migrating In v0.73.0, Hey API clients are bundled by default. Uninstall any previously installed client packages like `@hey-api/client-fetch` to avoid conflicts and simplify your setup. ```sh npm uninstall @hey-api/client-fetch ``` -------------------------------- ### Generate Client with Other CLI Tools Source: https://heyapi.dev/docs/openapi/typescript/integrations Example of using a generic CLI tool to generate a client from a Hey API backend URL. ```sh other-cli \ --input https://get.heyapi.dev/hey-api/backend \ --output refer/to/other/tools/docs ``` -------------------------------- ### Setup MSW Server with All Handlers Source: https://heyapi.dev/docs/openapi/typescript/plugins/msw Use `getAllMocks` to generate handlers for all operations at once. This is useful for quickly setting up a mock server without manually listing each operation. ```typescript import { setupServer } from 'msw/node'; import { getAllMocks } from './client/msw.gen'; const server = setupServer(...getAllMocks()); ``` -------------------------------- ### Install @hey-api/openapi-ts with yarn Source: https://heyapi.dev/docs Install the @hey-api/openapi-ts package as a development dependency using yarn. ```bash yarn add @hey-api/openapi-ts -D -E ``` -------------------------------- ### Install @hey-api/openapi-ts with pnpm Source: https://heyapi.dev/docs Install the @hey-api/openapi-ts package as a development dependency using pnpm. ```bash pnpm add @hey-api/openapi-ts -D -E ``` -------------------------------- ### Example File Header Content Source: https://heyapi.dev/docs/openapi/typescript/configuration/output Illustrates the default content of the header notice in automatically generated files. ```typescript /* eslint-disable */ // This file is auto-generated by @hey-api/openapi-ts /** ... */ ``` -------------------------------- ### SDK Authentication Configuration Source: https://heyapi.dev/docs/openapi/typescript/plugins/sdk Example of configuring authentication for the SDK client. The auth function should return the token or credentials. ```typescript import { client } from './client.gen'; client.setConfig({ auth() { return ''; }, }); ``` ```javascript export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', plugins: [ // ...other plugins { auth: true, name: '@hey-api/sdk', }, ], }; ``` -------------------------------- ### Install OpenAPI TS CLI Source: https://heyapi.dev/docs/openapi/typescript/get-started Install @hey-api/openapi-ts as a development dependency using your preferred package manager. ```bash npm install @hey-api/openapi-ts -D -E ``` ```bash pnpm add @hey-api/openapi-ts -D -E ``` ```bash yarn add @hey-api/openapi-ts -D -E ``` ```bash bun add @hey-api/openapi-ts -D ``` -------------------------------- ### SDK with Flat Parameters Source: https://heyapi.dev/docs/openapi/typescript/plugins/sdk Example of using the SDK with the flat parameter structure. All parameter types are merged into a single object for a more compact syntax. ```typescript import { addPet } from './sdk.gen.ts'; await addPet({ color: 'red', name: 'Fluffy', petId: 1234, type: 'cat', }); ``` ```javascript export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', plugins: [ // ...other plugins { name: '@hey-api/sdk', paramsStructure: 'flat', }, ], }; ``` -------------------------------- ### Install Next.js Client Plugin Source: https://heyapi.dev/docs/openapi/typescript/clients/next-js Add the `@hey-api/client-next` plugin to your openapi-ts configuration file to enable Next.js client generation. ```javascript export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', plugins: ['@hey-api/client-next'], }; ``` -------------------------------- ### Query Key Example with Tags Source: https://heyapi.dev/docs/openapi/typescript/plugins/pinia-colada Example of a generated query key including operation tags for enhanced cache invalidation. ```typescript const key = [ { _id: 'getPetById', baseUrl: 'https://app.heyapi.dev', path: { petId: 1, }, tags: ['pets', 'one', 'get'], }, ]; ``` -------------------------------- ### Generated Query Key Example Source: https://heyapi.dev/docs/openapi/typescript/plugins/pinia-colada Example of a generated query key structure, including normalized parameters and metadata. ```typescript const queryKey = [ { _id: 'getPetById', baseUrl: 'https://app.heyapi.dev', path: { petId: 1, }, }, ]; ``` -------------------------------- ### Install Nuxt Client Dependency Source: https://heyapi.dev/docs/openapi/typescript/clients/nuxt Add the `@hey-api/nuxt` package to your project's dependencies using your preferred package manager. ```bash npm install @hey-api/nuxt ``` ```bash pnpm add @hey-api/nuxt ``` ```bash yarn add @hey-api/nuxt ``` ```bash bun add @hey-api/nuxt ``` -------------------------------- ### Configure service client (v0.52.0 and later) Source: https://heyapi.dev/docs/openapi/typescript/migrating Starting with v0.52.0, services define their own clients. Configure the client by importing it from 'client/services.gen' and using `setConfig()`. ```javascript import { client } from 'client/services.gen'; client.setConfig({ baseUrl: 'https://example.com', }); console.log(client.getConfig().baseUrl); // <-- 'https://example.com' ``` -------------------------------- ### Changeset Format - New Feature Source: https://heyapi.dev/docs/openapi/typescript/community/contributing/developing Example of a changeset for a new feature, including a plugin scope and a description of the enhancement. ```md --- "@hey-api/openapi-ts": minor --- **plugin(zod)**: handle guid string format ``` -------------------------------- ### SDK with Grouped Parameters Source: https://heyapi.dev/docs/openapi/typescript/plugins/sdk Example of using the SDK with the default grouped parameter structure. Parameters are organized into 'query', 'path', and 'body' objects. ```typescript import { addPet } from './sdk.gen.ts'; await addPet({ body: { name: 'Fluffy', type: 'cat', }, path: { petId: 1234, }, query: { color: 'red', }, }); ``` ```javascript export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', plugins: [ // ...other plugins { name: '@hey-api/sdk', paramsStructure: 'grouped', }, ], }; ``` -------------------------------- ### Configure Pinia Colada Plugin Source: https://heyapi.dev/docs/openapi/typescript/plugins/pinia-colada Configuration example for the `@pinia/colada` plugin in `openapi-ts.config.ts`. This enables mutation options generation. ```javascript export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', plugins: [ // ...other plugins { name: '@pinia/colada', mutationOptions: true, }, ], }; ``` -------------------------------- ### Install Fastify Plugin Source: https://heyapi.dev/docs/openapi/typescript/plugins/fastify Add 'fastify' to your plugins in the openapi-ts configuration file to enable generation of Fastify artifacts. ```js export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', plugins: [ // ...other plugins 'fastify', ], }; ``` -------------------------------- ### Install Zod Plugin Source: https://heyapi.dev/docs/openapi/typescript/plugins/zod Add 'zod' to your plugins in the configuration to enable Zod schema generation. ```typescript export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', plugins: [ // ...other plugins 'zod', ], }; ``` -------------------------------- ### Install Valibot Plugin Source: https://heyapi.dev/docs/openapi/typescript/plugins/valibot Add 'valibot' to your plugins array in the openapi-ts configuration file to enable Valibot schema generation. ```typescript export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', plugins: [ // ...other plugins 'valibot', ], }; ``` -------------------------------- ### Generated Plugin Output File Source: https://heyapi.dev/docs/openapi/python/plugins/custom This is an example of the output file (`my-plugin.gen.ts`) generated after successfully applying a custom plugin. ```ts export const user = 'Stan Smith'; ``` -------------------------------- ### Install Transformers Plugin Source: https://heyapi.dev/docs/openapi/typescript/plugins/transformers Add the @hey-api/transformers plugin to your OpenAPI TypeScript configuration to enable transformer generation. ```js export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', plugins: [ // ...other plugins '@hey-api/transformers', ], }; ``` -------------------------------- ### Setting up MSW Server with All Handlers Source: https://heyapi.dev/docs/openapi/typescript/plugins/msw Generates handlers for all operations at once, useful for quickly setting up a mock server. It uses `setupServer` from MSW and `getAllMocks` from the generated client. ```APIDOC ## Setting up MSW Server with All Handlers ### Description Generates handlers for all operations at once, useful for quickly setting up a mock server. It uses `setupServer` from MSW and `getAllMocks` from the generated client. ### Code ```typescript import { setupServer } from 'msw/node'; import { getAllMocks } from './client/msw.gen'; const server = setupServer(...getAllMocks()); ``` ``` -------------------------------- ### Mock Endpoint Using OpenAPI Response Examples (MSW) Source: https://heyapi.dev/docs/openapi/typescript/plugins/msw Generated handlers automatically use response examples from the OpenAPI spec as defaults. You can call the handler without arguments to get the example response, or override it with a custom response. ```typescript import { getFooMock } from './client/msw.gen'; const mockServer = setupServer( // uses the example response from the OpenAPI spec as default getFooMock(), // you can still override with a custom response getFooMock({ result: { name: 'Custom' } }), ); ``` -------------------------------- ### Vite Configuration with Vite-Specific Options Source: https://heyapi.dev/docs/openapi/typescript/configuration/vite Pass Vite Plugin API options, such as 'apply', to control when the plugin runs. This example configures the plugin to run only during development ('serve'). ```javascript import { heyApiPlugin } from '@hey-api/vite-plugin'; import { defineConfig } from 'vite'; export default defineConfig({ plugins: [ heyApiPlugin({ config: { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', }, vite: { apply: 'serve', }, }), ], }); ``` -------------------------------- ### Accessing Query Key from Query Options Source: https://heyapi.dev/docs/openapi/typescript/plugins/pinia-colada Example demonstrating how to access the query key from the result of a query options function. ```typescript const { key } = getPetByIdQuery({ path: { petId: 1, }, }); ``` -------------------------------- ### Example Usage of Generated Query Function Source: https://heyapi.dev/docs/openapi/typescript/plugins/tanstack-query Demonstrates how to use the generated `useQuery` hook with `getPetByIdOptions` to fetch pet data. ```typescript const query = useQuery({ ...getPetByIdOptions({ path: { petId: 1, }, }), }); ``` -------------------------------- ### Configure OpenAPI Specification Filtering Source: https://heyapi.dev/docs/openapi/typescript/migrating Use the `input.include` option with the experimental parser to filter your OpenAPI specification. This example processes only a specific schema and GET operation. ```js export default { client: '@hey-api/client-fetch', experimentalParser: true, input: { include: '^(#/components/schemas/foo|#/paths/api/v1/foo/get)$', path: 'hey-api/backend', // sign up at app.heyapi.dev }, output: 'src/client', }; ``` -------------------------------- ### Include Filters for Operations and Schemas Source: https://heyapi.dev/docs/openapi/typescript/migrating Configure input filters to include specific operations and schemas. This example shows how to match only the schema named `foo` and the `GET` operation for the `/api/v1/foo` path. ```typescript export default { input: { // match only the schema named `foo` and `GET` operation for the `/api/v1/foo` path filters: { operations: { include: ['GET /api/v1/foo'], }, schemas: { include: ['foo'], }, }, path: 'hey-api/backend', // sign up at app.heyapi.dev }, output: 'src/client', plugins: ['@hey-api/client-fetch'], }; ``` -------------------------------- ### Use Generated TypeScript Client Source: https://heyapi.dev/codegen/openapi/typescript Integrate the generated API client into your application. This example demonstrates making a 'createOrder' request and handling the response. ```typescript import { createOrder, zOrder } from './trading-client'; const { data, error } = await createOrder({ body: { symbol: 'AAPL', side: 'buy', type: 'limit', quantity: 10, price: 189.5, }, }); if (error) { console.error('Order rejected:', error.message); return; } const order = zOrder.parse(data); console.log(`Order ${order.id} is ${order.status}`); ``` -------------------------------- ### Exclude Filters for Operations and Schemas Source: https://heyapi.dev/docs/openapi/typescript/migrating Configure input filters to exclude specific operations and schemas. This example shows how to match everything except for the schema named `foo` and the `GET` operation for the `/api/v1/foo` path. ```typescript export default { input: { // match everything except for the schema named `foo` and `GET` operation for the `/api/v1/foo` path filters: { operations: { exclude: ['GET /api/v1/foo'], }, schemas: { exclude: ['foo'], }, }, path: 'hey-api/backend', // sign up at app.heyapi.dev }, output: 'src/client', plugins: ['@hey-api/client-fetch'], }; ``` -------------------------------- ### Use Generated Python Client Source: https://heyapi.dev/codegen/openapi/python Import and use the generated client functions and types to interact with the API. This example demonstrates creating an order and handling the response. ```typescript import { createOrder, zOrder } from './trading-client'; const { data, error } = await createOrder({ body: { symbol: 'AAPL', side: 'buy', type: 'limit', quantity: 10, price: 189.5, }, }); if (error) { console.error('Order rejected:', error.message); return; } const order = zOrder.parse(data); console.log(`Order ${order.id} is ${order.status}`); ``` -------------------------------- ### Set Output Path as String Source: https://heyapi.dev/docs/openapi/python/configuration Configure the output directory for generated files by providing a string path. This is a common setup for specifying the client's destination folder. ```javascript export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', }; ``` -------------------------------- ### Add Pet SDK Client with Valibot Validation Source: https://heyapi.dev/docs/openapi/typescript/plugins/sdk Example of generating an SDK client function for adding a pet, incorporating request and response validation using Valibot. ```typescript import * as v from 'valibot'; export const addPet = (options: Options) => (options.client ?? client).post({ requestValidator: async (data) => await v.parseAsync(vAddPetData, data), responseValidator: async (data) => await v.parseAsync(vAddPetResponse, data), /** ... */ }); ``` -------------------------------- ### Install HTTPX Client via CLI Source: https://heyapi.dev/docs/openapi/python/clients/httpx Use the command-line interface to generate client artifacts with the HTTPX client plugin. This command specifies the input OpenAPI spec, output directory, and the client plugin. ```bash npx @hey-api/openapi-python \ -i hey-api/backend \ -o src/client \ -c @hey-api/client-httpx ``` -------------------------------- ### Python SDK Usage for Creating Order Source: https://heyapi.dev/codegen/openapi/python Demonstrates how to use the generated Python SDK client to create an order and handle potential errors. ```python from client import Client, CreateOrderData client = Client() order, error = client.create_order(CreateOrderData( symbol='AAPL', side='buy', type='limit', quantity=10, price=189.50, )) if error: print(f'Order failed: {error.message}') else: print(f'Order placed: {order.id}') ``` -------------------------------- ### Resolved User Configuration Example Source: https://heyapi.dev/docs/openapi/python/plugins/custom Illustrates the resolved configuration object for the 'user' field after applying coercion rules. Shows how a string input is transformed into the expected object structure. ```json { "user": { "age": 42, "name": "Joe Doe" } } ``` -------------------------------- ### Create a Changeset Source: https://heyapi.dev/docs/openapi/typescript/community/contributing/developing Run this command to initiate the creation of a new changeset for documenting changes and managing releases. ```sh pnpm changeset ``` -------------------------------- ### Generate Query Function Example Source: https://heyapi.dev/docs/openapi/typescript/plugins/pinia-colada Example of using a generated query function from the Pinia Colada plugin. ```typescript const query = useQuery(getPetByIdQuery, () => ({ path: { petId: 1, }, })); ``` -------------------------------- ### Create and Configure Custom ofetch Instance Source: https://heyapi.dev/docs/openapi/typescript/clients/ofetch Demonstrates creating a custom ofetch instance with onRequest and onResponse hooks, and then configuring the client to use this custom instance. ```typescript import { ofetch } from 'ofetch'; import { client } from 'client/client.gen'; const customOFetchInstance = ofetch.create({ onRequest: ({ options }) => { // customize request }, onResponse: ({ response }) => { // customize response }, }); client.setConfig({ ofetch: customOFetchInstance, }); ``` -------------------------------- ### Custom Temporal Date Transformer Example Source: https://heyapi.dev/docs/openapi/typescript/plugins/transformers Example of a custom transformer function using the Temporal API to convert date strings to Temporal.Instant objects. ```ts import { Temporal } from 'temporal-polyfill'; import type { GetFooResponse } from './types.gen'; const quxSchemaResponseTransformer = (data: any) => { if (data.baz) { data.baz = Temporal.Instant.from(data.baz); } return data; }; const bazSchemaResponseTransformer = (data: any) => { data = quxSchemaResponseTransformer(data); data.bar = Temporal.Instant.from(data.bar); return data; }; export const getFooResponseTransformer = async (data: any): Promise => { data = bazSchemaResponseTransformer(data); return data; }; ``` -------------------------------- ### TypeScript SDK Usage for Creating Order Source: https://heyapi.dev/codegen/openapi/python Demonstrates how to use the generated TypeScript SDK to create an order and handle the response. ```typescript import { createOrder } from './sdk.gen'; const { data, error } = await createOrder({ symbol: 'AAPL', side: 'buy', type: 'limit', quantity: 10, price: 189.5, }); if (error) { console.error('Order failed:', error.message); return; } console.log('Order placed:', data.id); ``` -------------------------------- ### Install Faker Plugin Source: https://heyapi.dev/docs/openapi/typescript/plugins/faker Add the Faker plugin to your OpenAPI TypeScript configuration to enable fake data generation. This requires installing the `@faker-js/faker` package. ```typescript export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', plugins: [ // ...other plugins '@faker-js/faker', ], }; ``` -------------------------------- ### Create a Custom Client Instance Source: https://heyapi.dev/docs/openapi/typescript/clients/angular/v19 Use `createClient()` to instantiate a new client. This allows manual request sending or pointing to a different domain. The created instance can override the default client. ```typescript import { createClient } from './client/client'; const myClient = createClient({ baseUrl: 'https://example.com', }); ``` ```typescript const response = await getFoo({ client: myClient, }); ``` -------------------------------- ### Angular HTTP Resource Example Source: https://heyapi.dev/docs/openapi/typescript/plugins/angular/v20 An example of a generated HTTP resource function for the 'addPet' endpoint. It wraps the `addPetRequest` function with `httpResource` to provide a resource-based interface. ```typescript export const addPetResource = (options) => httpResource(() => addPetRequest(options())); ``` -------------------------------- ### Example Response Transformer Implementation Source: https://heyapi.dev/docs/openapi/typescript/plugins/transformers A generated response transformer that parses date strings into Date objects and applies nested transformations. Note that this example has been edited for brevity. ```typescript import type { GetFooResponse } from './types.gen'; const quxSchemaResponseTransformer = (data: any) => { if (data.baz) { data.baz = new Date(data.baz); } return data; }; const bazSchemaResponseTransformer = (data: any) => { data = quxSchemaResponseTransformer(data); data.bar = new Date(data.bar); return data; }; export const getFooResponseTransformer = async (data: any): Promise => { data = bazSchemaResponseTransformer(data); return data; }; ``` -------------------------------- ### Configure MSW Plugin to Disable OpenAPI Examples Source: https://heyapi.dev/docs/openapi/typescript/plugins/msw Configure the MSW plugin in `openapi-ts.config.ts` to disable embedding OpenAPI examples by setting the `source` property to an empty array. ```javascript export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', plugins: [ // ...other plugins { name: 'msw', source: [], }, ], }; ``` -------------------------------- ### Configure HTTPX Client Plugin Source: https://heyapi.dev/docs/openapi/python/clients/httpx Add `@hey-api/client-httpx` to your plugins in the configuration file to enable the HTTPX client generation. This is the default client, so this step is optional. ```typescript export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', plugins: ['@hey-api/client-httpx'], }; ``` -------------------------------- ### Angular HTTP Request Example Source: https://heyapi.dev/docs/openapi/typescript/plugins/angular/v20 An example of a generated HTTP request function for the 'addPet' endpoint. It utilizes `client.requestOptions` to define the request method, response style, and URL. ```typescript export const addPetRequest = (options) => client.requestOptions({ method: 'POST', responseStyle: 'data', url: '/pet', ...options, }); ``` -------------------------------- ### Providing a Custom HTTP Client Source: https://heyapi.dev/docs/openapi/typescript/clients/angular/v19 Replace or extend the default HTTP client instance by providing a custom `httpClient` in the client configuration. This example shows how to inject a custom HTTP client. ```javascript import { client } from 'client/client.gen'; client.setConfig({ httpClient: inject(CustomHttpClient), }); ``` -------------------------------- ### Install Vite Plugin for OpenAPI TS Source: https://heyapi.dev/docs/openapi/typescript/get-started Install the @hey-api/vite-plugin alongside the main package to integrate OpenAPI TS into your Vite build pipeline. Supports Vite versions 5 through 8. ```bash npm install @hey-api/vite-plugin -D -E ``` ```bash pnpm add @hey-api/vite-plugin -D -E ``` ```bash yarn add @hey-api/vite-plugin -D -E ``` ```bash bun add @hey-api/vite-plugin -D ``` -------------------------------- ### Example Faker Factory Functions Source: https://heyapi.dev/docs/openapi/typescript/plugins/faker Example TypeScript code demonstrating how to create faker factory functions for 'Foo' and 'Bar' types. These functions use `@faker-js/faker` to generate mock data for your API types. ```typescript import { faker, type Faker } from '@faker-js/faker'; import type { Bar, Foo } from '../types.gen'; export const fakeFoo = (options?: Options): Foo => ({ name: ensureFaker(options).string.sample(), age: ensureFaker(options).number.int({ min: 1, max: 120 }), }); export const fakeBar = (options?: Options): Bar => ensureFaker(options).helpers.arrayElement(['baz', 'qux']); ``` -------------------------------- ### Run OpenAPI TS CLI Source: https://heyapi.dev/docs/openapi/typescript/get-started Use npx to quickly generate a client from your OpenAPI specification. Specify the input OpenAPI file and the output directory. ```bash npx @hey-api/openapi-ts -i hey-api/backend -o src/client ``` -------------------------------- ### Default Client Initialization Source: https://heyapi.dev/docs/openapi/python/output This file initializes the Fetch client for SDKs. It's consumed by SDKs but can also be used for manual requests or further configuration. ```typescript import { createClient, createConfig } from './client'; export const client = createClient(createConfig()); ``` -------------------------------- ### Configure Input via Path Source: https://heyapi.dev/docs/openapi/python/configuration Specify the input OpenAPI specification using a local file path. ```javascript export default { input: './path/to/openapi.json', }; ``` -------------------------------- ### Install Angular Plugin Source: https://heyapi.dev/docs/openapi/typescript/plugins/angular/v20 Add '@angular/common' to your plugins in the openapi-ts configuration to enable Angular artifact generation. ```javascript export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', plugins: [ // ...other plugins '@angular/common', ], }; ``` -------------------------------- ### Configuration with openapi-ts.config.ts Source: https://heyapi.dev/docs/openapi/typescript/clients/ofetch Configure the openapi-ts CLI to use the ofetch client by specifying the input OpenAPI spec, output directory, and the @hey-api/client-ofetch plugin. ```javascript export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', plugins: ['@hey-api/client-ofetch'], }; ``` -------------------------------- ### Install Pydantic Plugin Source: https://heyapi.dev/docs/openapi/python/plugins/pydantic Add the 'pydantic' plugin to your openapi-python configuration to enable Pydantic model generation. ```js export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', plugins: [ // ...other plugins 'pydantic', ], }; ``` -------------------------------- ### Instance SDK Generation Source: https://heyapi.dev/docs/openapi/typescript/plugins/sdk Use the 'single' strategy for class-based SDKs. This approach results in a larger bundle size but offers a preferred class syntax. Configure this in `openapi-ts.config.ts`. ```typescript import type { AddPetData } from './types.gen'; export class Sdk extends HeyApiClient { public addPet(options: Options) { /** ... */ } } ``` ```javascript export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', plugins: [ // ...other plugins { name: '@hey-api/sdk', operations: { strategy: 'single', }, }, ], }; ``` -------------------------------- ### Compose Output with Plugins Source: https://heyapi.dev/codegen/openapi/typescript Customize the generated code by selecting specific plugins. This example includes plugins for SDK generation, data validation with Zod, and React Query integration. ```typescript export default defineConfig({ input: 'https://api.tradespark.io/openapi.json', output: 'src/trading-client', plugins: ['@hey-api/sdk', 'zod', '@tanstack/react-query'], }); ``` -------------------------------- ### Configure Client with setConfig() Source: https://heyapi.dev/docs/openapi/typescript/clients/angular Use setConfig() to globally configure the client instance. Pass any HttpRequest configuration option. This method can be called anytime to update the configuration. ```typescript import { client } from 'client/client.gen'; client.setConfig({ baseUrl: 'https://example.com', }); ``` -------------------------------- ### Configure default client (v0.51.0 and earlier) Source: https://heyapi.dev/docs/openapi/typescript/migrating In older versions, a default client was created and could be imported and configured globally. This approach is deprecated. ```javascript import { client, createClient } from '@hey-api/client-fetch'; createClient({ baseUrl: 'https://example.com', }); console.log(client.getConfig().baseUrl); // <-- 'https://example.com' ``` -------------------------------- ### Changeset Format - Bug Fix Source: https://heyapi.dev/docs/openapi/typescript/community/contributing/developing Example of a changeset for a bug fix, specifying the package and a brief description of the change. ```md --- "@hey-api/openapi-ts": patch --- **parser**: fix explicit discriminator mapping ``` -------------------------------- ### Configure Fetch API Client with openapi-ts (CLI) Source: https://heyapi.dev/docs/openapi/typescript/clients/fetch Use the npx command to generate client artifacts with the Fetch API client by specifying the plugin with the -c flag. ```bash npx @hey-api/openapi-ts \ -i hey-api/backend \ -o src/client \ -c @hey-api/client-fetch ``` -------------------------------- ### Add Meta to Mutation Options Source: https://heyapi.dev/docs/openapi/typescript/plugins/tanstack-query Example of attaching arbitrary metadata to a mutation option. This can be useful for custom logic or identification. ```typescript const mutationOptions = { // ...other fields meta: { id: 'getPetById', }, }; ``` -------------------------------- ### Build Core Packages Source: https://heyapi.dev/docs/openapi/typescript/community/contributing/developing Build the core packages using pnpm, filtering for packages within the '@hey-api' scope. ```sh pnpm build --filter="@hey-api/**" ``` -------------------------------- ### Set Output Path as Object Source: https://heyapi.dev/docs/openapi/python/configuration Configure the output directory and other settings by providing an object for the output path. This allows for more granular control over the generation process. ```javascript export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: { path: 'src/client', // ...other options }, }; ``` -------------------------------- ### Install Zod Plugin Source: https://heyapi.dev/docs/openapi/typescript/plugins/zod/v3 Add the Zod plugin to your openapi-ts configuration to generate Zod artifacts. Ensure compatibilityVersion is set to 3. ```typescript export default { input: 'hey-api/backend', // sign up at app.heyapi.dev output: 'src/client', plugins: [ // ...other plugins { name: 'zod', compatibilityVersion: 3, }, ], }; ```