Skip to content

Commit 2c69963

Browse files
committed
readme
1 parent f39a622 commit 2c69963

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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! 👍

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openapi-typescript-fetch",
3-
"description": "A typed fetch wrapper for openapi-typescript",
3+
"description": "A typed fetch client for openapi-typescript",
44
"version": "1.0.0",
55
"engines": {
66
"node": ">= 12.0.0",
@@ -24,6 +24,7 @@
2424
},
2525
"keywords": [
2626
"fetch",
27+
"client",
2728
"swagger",
2829
"typescript",
2930
"ts",

0 commit comments

Comments
 (0)