Skip to content

Commit 092215c

Browse files
authored
Merge pull request #1850 from kelnos/apikey-cookie-support
feat: add support for cookies auth
2 parents 2797d6c + fe43b88 commit 092215c

File tree

25 files changed

+209
-22
lines changed

25 files changed

+209
-22
lines changed

.changeset/silent-camels-explain.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@hey-api/client-axios": patch
3+
"@hey-api/client-core": patch
4+
"@hey-api/client-fetch": patch
5+
"@hey-api/client-next": patch
6+
"@hey-api/client-nuxt": patch
7+
"@hey-api/openapi-ts": patch
8+
---
9+
10+
feat: add support for cookies auth

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,27 @@ describe('setAuthParams', () => {
9090
expect(query).toEqual({});
9191
});
9292

93+
it('sets an API key in a cookie', async () => {
94+
const auth = vi.fn().mockReturnValue('foo');
95+
const headers: Record<any, unknown> = {};
96+
const query: Record<any, unknown> = {};
97+
await setAuthParams({
98+
auth,
99+
headers,
100+
query,
101+
security: [
102+
{
103+
in: 'cookie',
104+
name: 'baz',
105+
type: 'apiKey',
106+
},
107+
],
108+
});
109+
expect(auth).toHaveBeenCalled();
110+
expect(headers.Cookie).toBe('baz=foo');
111+
expect(query).toEqual({});
112+
});
113+
93114
it('sets first scheme only', async () => {
94115
const auth = vi.fn().mockReturnValue('foo');
95116
const headers: Record<any, unknown> = {};

packages/client-axios/src/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ export const setAuthParams = async ({
171171
}
172172
options.query[name] = token;
173173
break;
174+
case 'cookie': {
175+
const value = `${name}=${token}`;
176+
if ('Cookie' in options.headers && options.headers['Cookie']) {
177+
options.headers['Cookie'] = `${options.headers['Cookie']}; ${value}`;
178+
} else {
179+
options.headers['Cookie'] = value;
180+
}
181+
break;
182+
}
174183
case 'header':
175184
default:
176185
options.headers[name] = token;

packages/client-core/src/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export interface Auth {
66
*
77
* @default 'header'
88
*/
9-
in?: 'header' | 'query';
9+
in?: 'header' | 'query' | 'cookie';
1010
/**
1111
* Header or query parameter name.
1212
*

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,25 @@ describe('setAuthParams', () => {
186186
expect(headers.get('baz')).toBeNull();
187187
expect(query.baz).toBe('Bearer foo');
188188
});
189+
190+
it('sets an API key in a cookie', async () => {
191+
const auth = vi.fn().mockReturnValue('foo');
192+
const headers = new Headers();
193+
const query: Record<any, unknown> = {};
194+
await setAuthParams({
195+
auth,
196+
headers,
197+
query,
198+
security: [
199+
{
200+
in: 'cookie',
201+
name: 'baz',
202+
type: 'apiKey',
203+
},
204+
],
205+
});
206+
expect(auth).toHaveBeenCalled();
207+
expect(headers.get('Cookie')).toBe('baz=foo');
208+
expect(query).toEqual({});
209+
});
189210
});

packages/client-fetch/src/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ export const setAuthParams = async ({
217217
}
218218
options.query[name] = token;
219219
break;
220+
case 'cookie':
221+
options.headers.append('Cookie', `${name}=${token}`);
222+
break;
220223
case 'header':
221224
default:
222225
options.headers.set(name, token);

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,25 @@ describe('setAuthParams', () => {
186186
expect(headers.get('baz')).toBeNull();
187187
expect(query.baz).toBe('Bearer foo');
188188
});
189+
190+
it('sets an API key in a cookie', async () => {
191+
const auth = vi.fn().mockReturnValue('foo');
192+
const headers = new Headers();
193+
const query: Record<any, unknown> = {};
194+
await setAuthParams({
195+
auth,
196+
headers,
197+
query,
198+
security: [
199+
{
200+
in: 'cookie',
201+
name: 'baz',
202+
type: 'apiKey',
203+
},
204+
],
205+
});
206+
expect(auth).toHaveBeenCalled();
207+
expect(headers.get('Cookie')).toBe('baz=foo');
208+
expect(query).toEqual({});
209+
});
189210
});

packages/client-next/src/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ export const setAuthParams = async ({
217217
}
218218
options.query[name] = token;
219219
break;
220+
case 'cookie':
221+
options.headers.append('Cookie', `${name}=${token}`);
222+
break;
220223
case 'header':
221224
default:
222225
options.headers.set(name, token);

packages/client-nuxt/src/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ export const setAuthParams = async ({
178178
}
179179
toValue(options.query)[name] = token;
180180
break;
181+
case 'cookie':
182+
options.headers.append('Cookie', `${name}=${token}`);
183+
break;
181184
case 'header':
182185
default:
183186
options.headers.set(name, token);

packages/openapi-ts/src/plugins/@hey-api/sdk/plugin.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export interface Auth {
3737
*
3838
* @default 'header'
3939
*/
40-
in?: 'header' | 'query';
40+
in?: 'header' | 'query' | 'cookie';
4141
/**
4242
* Header or query parameter name.
4343
*
@@ -175,8 +175,10 @@ const securitySchemeObjectToAuthObject = ({
175175
};
176176
}
177177

178-
// TODO: parser - support cookies auth
179-
if (securitySchemeObject.in === 'query') {
178+
if (
179+
securitySchemeObject.in === 'query' ||
180+
securitySchemeObject.in == 'cookie'
181+
) {
180182
return {
181183
in: securitySchemeObject.in,
182184
name: securitySchemeObject.name,

0 commit comments

Comments
 (0)