|
| 1 | +# 📘️ openapi-typescript-fetch |
| 2 | + |
| 3 | +A typed fetch client for [openapi-typescript](https://github.com/drwpow/openapi-typescript) |
| 4 | + |
| 5 | +**Features** |
| 6 | + |
| 7 | +Supports JSON request and responses |
| 8 | + |
| 9 | +- ✅ [OpenAPI 3.0](https://swagger.io/specification) |
| 10 | +- ✅ [Swagger 2.0](https://swagger.io/specification/v2/) |
| 11 | + |
| 12 | +### Usage |
| 13 | + |
| 14 | +**Generate typescript definition from schema** |
| 15 | + |
| 16 | +```bash |
| 17 | +npx openapi-typescript https://petstore.swagger.io/v2/swagger.json --output petstore.ts |
| 18 | + |
| 19 | +# 🔭 Loading spec from https://petstore.swagger.io/v2/swagger.json… |
| 20 | +# 🚀 https://petstore.swagger.io/v2/swagger.json -> petstore.ts [650ms] |
| 21 | +``` |
| 22 | + |
| 23 | +**Typed fetch client** |
| 24 | + |
| 25 | +```ts |
| 26 | +import 'whatwg-fetch' |
| 27 | + |
| 28 | +import { paths } from './petstore' |
| 29 | + |
| 30 | +const fetcher = Fetcher.for<paths>() |
| 31 | + |
| 32 | +// global configuration |
| 33 | +fetcher.configure({ |
| 34 | + baseUrl: 'https://petstore.swagger.io/v2', |
| 35 | + init: { |
| 36 | + headers: { |
| 37 | + ... |
| 38 | + }, |
| 39 | + }, |
| 40 | + use: [...] // middewares |
| 41 | +}) |
| 42 | + |
| 43 | +// create fetch operations |
| 44 | +const findPetsByStatus = fetcher.path('/pet/findByStatus').method('get').create() |
| 45 | +const addPet = fetcher.path('/pet').method('post').create() |
| 46 | + |
| 47 | +// fetch |
| 48 | +const { status, data: pets } = await findPetsByStatus({ |
| 49 | + status: ['available', 'pending'], |
| 50 | +}) |
| 51 | + |
| 52 | +console.log(pets[0]) |
| 53 | +``` |
| 54 | + |
| 55 | +### Middleware |
| 56 | + |
| 57 | +Middlewares can be used to pre and post process fetch operations (log api calls, add auth headers etc) |
| 58 | + |
| 59 | +```ts |
| 60 | + |
| 61 | +import { Middleware } from 'openapi-typescript-fetch' |
| 62 | + |
| 63 | +const logger: Middleware = async (url, init, next) => { |
| 64 | + console.log(`fetching ${url}`) |
| 65 | + const response = await next(url, init) |
| 66 | + console.log(`fetched ${url}`) |
| 67 | + return response |
| 68 | +} |
| 69 | + |
| 70 | +fetcher.configure({ |
| 71 | + baseUrl: 'https://petstore.swagger.io/v2', |
| 72 | + init: { ... }, |
| 73 | + use: [logger], |
| 74 | +}) |
| 75 | + |
| 76 | +// or |
| 77 | + |
| 78 | +fetcher.use(logger) |
| 79 | +``` |
| 80 | + |
| 81 | +### Utility Types |
| 82 | + |
| 83 | +- `OpArgType` - Infer argument type of an operation |
| 84 | +- `OpReturnType` - Infer return type of an operation |
| 85 | +- `FetchArgType` - Argument type of a typed fetch operation |
| 86 | +- `FetchReturnType` - Return type of a typed fetch operation |
| 87 | + |
| 88 | +```ts |
| 89 | +import { paths, operations } from './petstore' |
| 90 | + |
| 91 | +type Arg = OpArgType<operations['findPetsByStatus']> |
| 92 | +type Ret = OpReturnType<operations['findPetsByStatus']> |
| 93 | + |
| 94 | +type Arg = OpArgType<paths['/pet/findByStatus']['get']> |
| 95 | +type Ret = OpReturnType<paths['/pet/findByStatus']['get']> |
| 96 | + |
| 97 | +const findPetsByStatus = fetcher.path('/pet/findByStatus').method('get').create() |
| 98 | + |
| 99 | +type Arg = FetchArgType<typeof findPetsByStatus> |
| 100 | +type Ret = FetchReturnType<typeof findPetsByStatus> |
| 101 | +``` |
| 102 | +
|
| 103 | +Happy fetching! 👍 |
0 commit comments