Skip to content

Commit 64b786f

Browse files
authored
Merge pull request #2480 from dracomithril/add_multiple_headers
add multiple headers in different type
2 parents 091ca65 + d9af1bb commit 64b786f

File tree

176 files changed

+4116
-339
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

176 files changed

+4116
-339
lines changed

.changeset/giant-dodos-lay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hey-api/openapi-ts": patch
3+
---
4+
5+
fix(client): call `auth()` function for every unique security `name`

packages/custom-client/src/__tests__/utils.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,41 @@ describe('setAuthParams', () => {
207207
expect(headers.get('Cookie')).toBe('baz=foo');
208208
expect(query).toEqual({});
209209
});
210+
211+
it('sets only one specific header', async () => {
212+
const auth = vi.fn(({ name }: Auth) => {
213+
if (name === 'baz') {
214+
return 'foo';
215+
}
216+
return 'buz';
217+
});
218+
const headers = new Headers();
219+
const query: Record<any, unknown> = {};
220+
await setAuthParams({
221+
auth,
222+
headers,
223+
query,
224+
security: [
225+
{
226+
name: 'baz',
227+
scheme: 'bearer',
228+
type: 'http',
229+
},
230+
{
231+
name: 'fiz',
232+
type: 'http',
233+
},
234+
{
235+
in: 'query',
236+
name: 'baz',
237+
scheme: 'bearer',
238+
type: 'http',
239+
},
240+
],
241+
});
242+
expect(auth).toHaveBeenCalled();
243+
expect(headers.get('baz')).toBe('Bearer foo');
244+
expect(headers.get('fiz')).toBe('buz');
245+
expect(Object.keys(query).length).toBe(0);
246+
});
210247
});

packages/custom-client/src/utils.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,25 @@ export const getParseAs = (
186186
return;
187187
};
188188

189+
const checkForExistence = (
190+
options: Pick<RequestOptions, 'auth' | 'query'> & {
191+
headers: Headers;
192+
},
193+
name?: string,
194+
): boolean => {
195+
if (!name) {
196+
return false;
197+
}
198+
if (
199+
options.headers.has(name) ||
200+
options.query?.[name] ||
201+
options.headers.get('Cookie')?.includes(`${name}=`)
202+
) {
203+
return true;
204+
}
205+
return false;
206+
};
207+
189208
export const setAuthParams = async ({
190209
security,
191210
...options
@@ -194,6 +213,10 @@ export const setAuthParams = async ({
194213
headers: Headers;
195214
}) => {
196215
for (const auth of security) {
216+
if (checkForExistence(options, auth.name)) {
217+
continue;
218+
}
219+
197220
const token = await getAuthToken(auth, options.auth);
198221

199222
if (!token) {
@@ -217,8 +240,6 @@ export const setAuthParams = async ({
217240
options.headers.set(name, token);
218241
break;
219242
}
220-
221-
return;
222243
}
223244
};
224245

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/body-response-text-plain/client/utils.gen.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,25 @@ export const getParseAs = (
188188
return;
189189
};
190190

191+
const checkForExistence = (
192+
options: Pick<RequestOptions, 'auth' | 'query'> & {
193+
headers: Headers;
194+
},
195+
name?: string,
196+
): boolean => {
197+
if (!name) {
198+
return false;
199+
}
200+
if (
201+
options.headers.has(name) ||
202+
options.query?.[name] ||
203+
options.headers.get('Cookie')?.includes(`${name}=`)
204+
) {
205+
return true;
206+
}
207+
return false;
208+
};
209+
191210
export const setAuthParams = async ({
192211
security,
193212
...options
@@ -196,6 +215,10 @@ export const setAuthParams = async ({
196215
headers: Headers;
197216
}) => {
198217
for (const auth of security) {
218+
if (checkForExistence(options, auth.name)) {
219+
continue;
220+
}
221+
199222
const token = await getAuthToken(auth, options.auth);
200223

201224
if (!token) {
@@ -219,8 +242,6 @@ export const setAuthParams = async ({
219242
options.headers.set(name, token);
220243
break;
221244
}
222-
223-
return;
224245
}
225246
};
226247

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/form-data/client/utils.gen.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,25 @@ export const getParseAs = (
188188
return;
189189
};
190190

191+
const checkForExistence = (
192+
options: Pick<RequestOptions, 'auth' | 'query'> & {
193+
headers: Headers;
194+
},
195+
name?: string,
196+
): boolean => {
197+
if (!name) {
198+
return false;
199+
}
200+
if (
201+
options.headers.has(name) ||
202+
options.query?.[name] ||
203+
options.headers.get('Cookie')?.includes(`${name}=`)
204+
) {
205+
return true;
206+
}
207+
return false;
208+
};
209+
191210
export const setAuthParams = async ({
192211
security,
193212
...options
@@ -196,6 +215,10 @@ export const setAuthParams = async ({
196215
headers: Headers;
197216
}) => {
198217
for (const auth of security) {
218+
if (checkForExistence(options, auth.name)) {
219+
continue;
220+
}
221+
199222
const token = await getAuthToken(auth, options.auth);
200223

201224
if (!token) {
@@ -219,8 +242,6 @@ export const setAuthParams = async ({
219242
options.headers.set(name, token);
220243
break;
221244
}
222-
223-
return;
224245
}
225246
};
226247

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@hey-api/client-fetch/sdk-nested-classes-instance/client/utils.gen.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,25 @@ export const getParseAs = (
188188
return;
189189
};
190190

191+
const checkForExistence = (
192+
options: Pick<RequestOptions, 'auth' | 'query'> & {
193+
headers: Headers;
194+
},
195+
name?: string,
196+
): boolean => {
197+
if (!name) {
198+
return false;
199+
}
200+
if (
201+
options.headers.has(name) ||
202+
options.query?.[name] ||
203+
options.headers.get('Cookie')?.includes(`${name}=`)
204+
) {
205+
return true;
206+
}
207+
return false;
208+
};
209+
191210
export const setAuthParams = async ({
192211
security,
193212
...options
@@ -196,6 +215,10 @@ export const setAuthParams = async ({
196215
headers: Headers;
197216
}) => {
198217
for (const auth of security) {
218+
if (checkForExistence(options, auth.name)) {
219+
continue;
220+
}
221+
199222
const token = await getAuthToken(auth, options.auth);
200223

201224
if (!token) {
@@ -219,8 +242,6 @@ export const setAuthParams = async ({
219242
options.headers.set(name, token);
220243
break;
221244
}
222-
223-
return;
224245
}
225246
};
226247

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@hey-api/client-fetch/sdk-nested-classes/client/utils.gen.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,25 @@ export const getParseAs = (
188188
return;
189189
};
190190

191+
const checkForExistence = (
192+
options: Pick<RequestOptions, 'auth' | 'query'> & {
193+
headers: Headers;
194+
},
195+
name?: string,
196+
): boolean => {
197+
if (!name) {
198+
return false;
199+
}
200+
if (
201+
options.headers.has(name) ||
202+
options.query?.[name] ||
203+
options.headers.get('Cookie')?.includes(`${name}=`)
204+
) {
205+
return true;
206+
}
207+
return false;
208+
};
209+
191210
export const setAuthParams = async ({
192211
security,
193212
...options
@@ -196,6 +215,10 @@ export const setAuthParams = async ({
196215
headers: Headers;
197216
}) => {
198217
for (const auth of security) {
218+
if (checkForExistence(options, auth.name)) {
219+
continue;
220+
}
221+
199222
const token = await getAuthToken(auth, options.auth);
200223

201224
if (!token) {
@@ -219,8 +242,6 @@ export const setAuthParams = async ({
219242
options.headers.set(name, token);
220243
break;
221244
}
222-
223-
return;
224245
}
225246
};
226247

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@hey-api/sdk/default/client/utils.gen.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,25 @@ export const getParseAs = (
188188
return;
189189
};
190190

191+
const checkForExistence = (
192+
options: Pick<RequestOptions, 'auth' | 'query'> & {
193+
headers: Headers;
194+
},
195+
name?: string,
196+
): boolean => {
197+
if (!name) {
198+
return false;
199+
}
200+
if (
201+
options.headers.has(name) ||
202+
options.query?.[name] ||
203+
options.headers.get('Cookie')?.includes(`${name}=`)
204+
) {
205+
return true;
206+
}
207+
return false;
208+
};
209+
191210
export const setAuthParams = async ({
192211
security,
193212
...options
@@ -196,6 +215,10 @@ export const setAuthParams = async ({
196215
headers: Headers;
197216
}) => {
198217
for (const auth of security) {
218+
if (checkForExistence(options, auth.name)) {
219+
continue;
220+
}
221+
199222
const token = await getAuthToken(auth, options.auth);
200223

201224
if (!token) {
@@ -219,8 +242,6 @@ export const setAuthParams = async ({
219242
options.headers.set(name, token);
220243
break;
221244
}
222-
223-
return;
224245
}
225246
};
226247

packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@hey-api/sdk/instance/client/utils.gen.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,25 @@ export const getParseAs = (
188188
return;
189189
};
190190

191+
const checkForExistence = (
192+
options: Pick<RequestOptions, 'auth' | 'query'> & {
193+
headers: Headers;
194+
},
195+
name?: string,
196+
): boolean => {
197+
if (!name) {
198+
return false;
199+
}
200+
if (
201+
options.headers.has(name) ||
202+
options.query?.[name] ||
203+
options.headers.get('Cookie')?.includes(`${name}=`)
204+
) {
205+
return true;
206+
}
207+
return false;
208+
};
209+
191210
export const setAuthParams = async ({
192211
security,
193212
...options
@@ -196,6 +215,10 @@ export const setAuthParams = async ({
196215
headers: Headers;
197216
}) => {
198217
for (const auth of security) {
218+
if (checkForExistence(options, auth.name)) {
219+
continue;
220+
}
221+
199222
const token = await getAuthToken(auth, options.auth);
200223

201224
if (!token) {
@@ -219,8 +242,6 @@ export const setAuthParams = async ({
219242
options.headers.set(name, token);
220243
break;
221244
}
222-
223-
return;
224245
}
225246
};
226247

0 commit comments

Comments
 (0)