-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support oauth2 and apiKey security schemes
- Loading branch information
Showing
59 changed files
with
1,589 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@hey-api/client-axios': patch | ||
'@hey-api/client-fetch': patch | ||
'@hey-api/openapi-ts': patch | ||
--- | ||
|
||
feat: support oauth2 and apiKey security schemes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import { describe, expect, it, vi } from 'vitest'; | ||
|
||
import { getAuthToken, setAuthParams } from '../utils'; | ||
|
||
describe('getAuthToken', () => { | ||
it('returns access token', async () => { | ||
const accessToken = vi.fn().mockReturnValue('foo'); | ||
const apiKey = vi.fn().mockReturnValue('bar'); | ||
const token = await getAuthToken( | ||
{ | ||
fn: 'accessToken', | ||
in: 'header', | ||
name: 'baz', | ||
}, | ||
{ | ||
accessToken, | ||
apiKey, | ||
}, | ||
); | ||
expect(accessToken).toHaveBeenCalled(); | ||
expect(token).toBe('Bearer foo'); | ||
}); | ||
|
||
it('returns nothing when accessToken function is undefined', async () => { | ||
const apiKey = vi.fn().mockReturnValue('bar'); | ||
const token = await getAuthToken( | ||
{ | ||
fn: 'accessToken', | ||
in: 'header', | ||
name: 'baz', | ||
}, | ||
{ | ||
apiKey, | ||
}, | ||
); | ||
expect(token).toBeUndefined(); | ||
}); | ||
|
||
it('returns API key', async () => { | ||
const accessToken = vi.fn().mockReturnValue('foo'); | ||
const apiKey = vi.fn().mockReturnValue('bar'); | ||
const token = await getAuthToken( | ||
{ | ||
fn: 'apiKey', | ||
in: 'header', | ||
name: 'baz', | ||
}, | ||
{ | ||
accessToken, | ||
apiKey, | ||
}, | ||
); | ||
expect(apiKey).toHaveBeenCalled(); | ||
expect(token).toBe('bar'); | ||
}); | ||
|
||
it('returns nothing when apiKey function is undefined', async () => { | ||
const accessToken = vi.fn().mockReturnValue('foo'); | ||
const token = await getAuthToken( | ||
{ | ||
fn: 'apiKey', | ||
in: 'header', | ||
name: 'baz', | ||
}, | ||
{ | ||
accessToken, | ||
}, | ||
); | ||
expect(token).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('setAuthParams', () => { | ||
it('sets access token in headers', async () => { | ||
const accessToken = vi.fn().mockReturnValue('foo'); | ||
const apiKey = vi.fn().mockReturnValue('bar'); | ||
const headers: Record<any, unknown> = {}; | ||
const query: Record<any, unknown> = {}; | ||
await setAuthParams({ | ||
accessToken, | ||
apiKey, | ||
headers, | ||
query, | ||
security: [ | ||
{ | ||
fn: 'accessToken', | ||
in: 'header', | ||
name: 'baz', | ||
}, | ||
], | ||
}); | ||
expect(accessToken).toHaveBeenCalled(); | ||
expect(headers.baz).toBe('Bearer foo'); | ||
expect(Object.keys(query).length).toBe(0); | ||
}); | ||
|
||
it('sets access token in query', async () => { | ||
const accessToken = vi.fn().mockReturnValue('foo'); | ||
const apiKey = vi.fn().mockReturnValue('bar'); | ||
const headers: Record<any, unknown> = {}; | ||
const query: Record<any, unknown> = {}; | ||
await setAuthParams({ | ||
accessToken, | ||
apiKey, | ||
headers, | ||
query, | ||
security: [ | ||
{ | ||
fn: 'accessToken', | ||
in: 'query', | ||
name: 'baz', | ||
}, | ||
], | ||
}); | ||
expect(accessToken).toHaveBeenCalled(); | ||
expect(Object.keys(headers).length).toBe(0); | ||
expect(query.baz).toBe('Bearer foo'); | ||
}); | ||
|
||
it('sets first scheme only', async () => { | ||
const accessToken = vi.fn().mockReturnValue('foo'); | ||
const apiKey = vi.fn().mockReturnValue('bar'); | ||
const headers: Record<any, unknown> = {}; | ||
const query: Record<any, unknown> = {}; | ||
await setAuthParams({ | ||
accessToken, | ||
apiKey, | ||
headers, | ||
query, | ||
security: [ | ||
{ | ||
fn: 'accessToken', | ||
in: 'header', | ||
name: 'baz', | ||
}, | ||
{ | ||
fn: 'accessToken', | ||
in: 'query', | ||
name: 'baz', | ||
}, | ||
], | ||
}); | ||
expect(accessToken).toHaveBeenCalled(); | ||
expect(headers.baz).toBe('Bearer foo'); | ||
expect(Object.keys(query).length).toBe(0); | ||
}); | ||
|
||
it('sets first scheme with token', async () => { | ||
const accessToken = vi.fn().mockReturnValue('foo'); | ||
const apiKey = vi.fn().mockReturnValue(undefined); | ||
const headers: Record<any, unknown> = {}; | ||
const query: Record<any, unknown> = {}; | ||
await setAuthParams({ | ||
accessToken, | ||
apiKey, | ||
headers, | ||
query, | ||
security: [ | ||
{ | ||
fn: 'apiKey', | ||
in: 'header', | ||
name: 'baz', | ||
}, | ||
{ | ||
fn: 'accessToken', | ||
in: 'query', | ||
name: 'baz', | ||
}, | ||
], | ||
}); | ||
expect(accessToken).toHaveBeenCalled(); | ||
expect(Object.keys(headers).length).toBe(0); | ||
expect(query.baz).toBe('Bearer foo'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
packages/client-fetch/test/index.test.ts → .../client-fetch/src/__tests__/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.