{"library":"openapi-typescript-fetch","title":"Typed Fetch Client for OpenAPI Schemas","description":"openapi-typescript-fetch provides a robust and type-safe `fetch` client tailored for use with TypeScript definitions generated by `openapi-typescript`. It simplifies API interactions by offering strong typing for request parameters, response bodies, and error structures, directly derived from OpenAPI 3.x and Swagger 2.0 schemas. The current stable version is 2.2.1, with releases occurring incrementally as bug fixes and minor features are introduced. The project differentiates itself by deeply integrating with `openapi-typescript`'s generated types, allowing developers to configure a global fetcher instance, define per-path operations, and handle complex API error responses using discriminated unions, enhancing developer experience and reducing runtime errors. It supports middleware for request/response interception and offers utility types for introspection of operation arguments and return values. This library focuses on providing a secure and reliable way to interact with RESTful APIs in TypeScript environments, primarily for Node.js (>=12) and modern browsers.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install openapi-typescript-fetch"],"cli":null},"imports":["import { Fetcher } from 'openapi-typescript-fetch'","import { Middleware } from 'openapi-typescript-fetch'","import { OpArgType } from 'openapi-typescript-fetch'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { Fetcher } from 'openapi-typescript-fetch';\nimport type { paths } from './petstore'; // Assume this is generated by 'npx openapi-typescript'\n\n// Configure the fetcher with a base URL and optional global settings\nconst fetcher = Fetcher.for<paths>();\nfetcher.configure({\n  baseUrl: 'https://petstore.swagger.io/v2',\n  init: {\n    headers: {\n      'Accept': 'application/json'\n    }\n  },\n  use: [async (url, init, next) => {\n    console.log(`[REQUEST] ${init.method || 'GET'} ${url}`);\n    const response = await next(url, init);\n    console.log(`[RESPONSE] ${url} - Status: ${response.status}`);\n    return response;\n  }]\n});\n\n// Define specific API operations\nconst findPetsByStatus = fetcher.path('/pet/findByStatus').method('get').create();\nconst addPet = fetcher.path('/pet').method('post').create();\n\nasync function runExample() {\n  try {\n    // Execute the findPetsByStatus operation with typed parameters\n    const { status, data: pets } = await findPetsByStatus({\n      status: ['available', 'pending'],\n      // No body for GET request, but type-safe parameters are enforced\n    });\n    console.log(`Found ${pets.length} pets. First pet ID: ${pets[0]?.id}`);\n\n    // Example of a POST request\n    const newPet = {\n      id: 987654321,\n      name: 'Max',\n      status: 'available',\n      category: { id: 1, name: 'Dog' },\n      photoUrls: ['http://example.com/max.jpg']\n    };\n    const addPetResult = await addPet(newPet);\n    console.log(`Added pet with ID: ${addPetResult.data.id}`);\n\n  } catch (e) {\n    // Handle typed errors\n    if (e instanceof addPet.Error) {\n      const error = e.getActualType();\n      console.error(`API Error for addPet (Status: ${error.status}):`, error.data);\n    } else {\n      console.error('An unexpected error occurred:', e);\n    }\n  }\n}\n\nrunExample();","lang":"typescript","description":"This quickstart demonstrates how to configure a typed fetch client, define specific API operations, execute them with type-safe parameters, and handle potential errors using the library's discriminated union error types.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}