Skip to content

Commit 90be1c7

Browse files
committed
📝 doc
Signed-off-by: moznion <moznion@mail.moznion.net>
1 parent 87e87da commit 90be1c7

File tree

1 file changed

+81
-47
lines changed

1 file changed

+81
-47
lines changed

README.md

Lines changed: 81 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,29 @@ async function doSomething() {
6767

6868
### Default HTTP Headers
6969

70-
This tool provides a `--default-headers` CLI option to configure the generated client to use default HTTP headers.
70+
Generated clients support a generic type for default HTTP headers.
7171

72-
For example, when you specify:
72+
Example:
7373

74-
```bash
75-
npx openapi-fetch-gen \
76-
--input ./schema.d.ts \
77-
--output ./client.ts \
78-
--default-headers "Authorization, Application-Version"
74+
```typescript
75+
export class Client<HT extends Record<string, string>> {
76+
constructor(clientOptions: ClientOptions, defaultHeaders?: HT) {
77+
this.client = createClient<paths>(clientOptions);
78+
this.defaultHeaders = defaultHeaders ?? ({} as HT);
79+
}
80+
...
81+
}
7982
```
8083

81-
the `Client` constructor accepts those default values:
84+
You can create a client instance with default headers like this:
8285

8386
```typescript
84-
type DefaultHeaders = Record<'Authorization' | 'Application-Version', string>;
85-
86-
export class Client {
87-
constructor(clientOptions: ClientOptions, defaultHeaders: DefaultHeaders) { ... }
88-
}
87+
new Client({}, {"Authorization": "Bearer your-token", "Application-Version": "1.0.0"});
8988
```
9089

91-
Endpoint methods that require those headers no longer need the default values to be passed explicitly.
90+
With this setup, endpoint methods that require these headers no longer need them to be explicitly passed each time.
9291

93-
For example, given this schema:
92+
For example, given the following schema:
9493

9594
```typescript
9695
"/users/bulk/{jobId}": {
@@ -113,35 +112,29 @@ For example, given this schema:
113112
};
114113
```
115114
116-
When `--default-headers` is not specified, the generated client method looks like this:
117-
118-
```typescript
119-
async getUsersBulkJobid(params: {
120-
header: {
121-
Authorization: string;
122-
"Application-Version": string;
123-
"Something-Id": string;
124-
};
125-
path: { jobId: string };
126-
}) {
127-
...
128-
}
129-
```
130-
131-
This requires you to pass all header values explicitly.
132-
133-
When you do specify `--default-headers`, the signature becomes:
115+
This tool generates an endpoint method using a type-level trick like this:
134116
135117
```typescript
136118
async getUsersBulkJobid(
137-
params: keyof Omit<
138-
{
139-
Authorization: string;
140-
"Application-Version": string;
141-
"Something-Id": string;
142-
},
143-
keyof DefaultHeaders
144-
> extends never
119+
params: [
120+
Exclude<
121+
// Missed Header Keys for default headers
122+
keyof {
123+
Authorization: string;
124+
"Application-Version": string;
125+
"Something-Id": string;
126+
},
127+
Extract<
128+
// Provided header keys by default headers' keys
129+
keyof HT,
130+
keyof {
131+
Authorization: string;
132+
"Application-Version": string;
133+
"Something-Id": string;
134+
}
135+
>
136+
>,
137+
] extends [never]
145138
? {
146139
header?: {
147140
Authorization: string;
@@ -152,14 +145,46 @@ async getUsersBulkJobid(
152145
}
153146
: {
154147
header:
155-
| Omit<
148+
| (Pick<
149+
// Pick the header keys that are not in the default headers
156150
{
157151
Authorization: string;
158152
"Application-Version": string;
159153
"Something-Id": string;
160154
},
161-
keyof DefaultHeaders
162-
>
155+
Exclude<
156+
// Missed Header Keys for default headers
157+
keyof {
158+
Authorization: string;
159+
"Application-Version": string;
160+
"Something-Id": string;
161+
},
162+
Extract<
163+
// Provided header keys by default headers' keys
164+
keyof HT,
165+
keyof {
166+
Authorization: string;
167+
"Application-Version": string;
168+
"Something-Id": string;
169+
}
170+
>
171+
>
172+
> &
173+
Partial<
174+
// Disallow default headers' keys to be in the header param
175+
Record<
176+
Extract<
177+
// Provided header keys by default headers' keys
178+
keyof HT,
179+
keyof {
180+
Authorization: string;
181+
"Application-Version": string;
182+
"Something-Id": string;
183+
}
184+
>,
185+
never
186+
>
187+
>)
163188
| {
164189
Authorization: string;
165190
"Application-Version": string;
@@ -168,11 +193,20 @@ async getUsersBulkJobid(
168193
path: { jobId: string };
169194
},
170195
) {
171-
...
196+
return await this.client.GET("/users/bulk/{jobId}", {
197+
params: {
198+
...params,
199+
header: { ...this.defaultHeaders, ...params.header } as {
200+
Authorization: string;
201+
"Application-Version": string;
202+
"Something-Id": string;
203+
},
204+
},
205+
});
172206
}
173207
```
174208
175-
That signature means you can:
209+
This signature allows you to:
176210
177211
- **Omit** the defaulted headers and only pass additional ones (here, `Something-Id`):
178212
@@ -186,7 +220,7 @@ client.getUsersBulkJobid({header: {"Something-Id": "foobar"}, path: {jobId: "123
186220
client.getUsersBulkJobid({header: {"Authorization": "foo", "Application-Version": "bar", "Something-Id": "foobar"}, path: {jobId: "123"}});
187221
```
188222
189-
If your default headers cover **all** required headers for the endpoint (e.g. `--default-headers 'Authorization, Application-Version, Something-Id'`), you can omit the `header` parameter entirely:
223+
If your default headers already include **all** required headers for the endpoint (e.g. `{"Authorization": "Bearer your-token", "Application-Version": "1.0.0", "Something-Id": "123"}` as the second constructor argument), you can omit the `header` parameter entirely:
190224
191225
```typescript
192226
client.getUsersBulkJobid({path: {jobId: "123"}});

0 commit comments

Comments
 (0)