-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from nrccua/feature/headersLink
Added a New, Optional Apollo Link: HeadersLink
- Loading branch information
Showing
9 changed files
with
197 additions
and
26 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
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,46 @@ | ||
import { NextLink, Operation } from '@apollo/client'; | ||
import HeadersLink from '.'; | ||
|
||
describe('HeadersLink', () => { | ||
const mockForward = jest.fn(); | ||
const mockGetContext = jest.fn(); | ||
const mockOperation = { | ||
getContext: mockGetContext, | ||
} as unknown as Operation; | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
mockForward.mockReturnValue([{}]); | ||
mockGetContext.mockReturnValue({ | ||
restResponses: [{}], | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('can handle requests without headers', () => { | ||
const headersLink = new HeadersLink(); | ||
|
||
const result = headersLink.request(mockOperation, mockForward as unknown as NextLink); | ||
|
||
expect(result).toEqual([{ data: { headers: {} } }]); | ||
}); | ||
|
||
it('can handle requests with headers', () => { | ||
mockGetContext.mockReturnValue({ | ||
restResponses: [ | ||
{ | ||
headers: [['x-api-key', '12345']], | ||
}, | ||
], | ||
}); | ||
|
||
const headersLink = new HeadersLink(); | ||
|
||
const result = headersLink.request(mockOperation, mockForward as unknown as NextLink); | ||
|
||
expect(result).toEqual([{ data: { headers: { xApiKey: '12345' } } }]); | ||
}); | ||
}); |
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,29 @@ | ||
import { ApolloLink, FetchResult, NextLink, Observable, Operation } from '@apollo/client'; | ||
import { camelCase, get } from 'lodash'; | ||
|
||
export class HeadersLink extends ApolloLink { | ||
// eslint-disable-next-line class-methods-use-this | ||
public request(operation: Operation, forward: NextLink): Observable<FetchResult> | null { | ||
return forward(operation).map((response): Record<string, unknown> => { | ||
const context = operation.getContext(); | ||
|
||
const headersMap = (get(context, 'restResponses[0].headers') as Map<string, unknown>) || new Map(); | ||
const headersObj: Record<string, unknown> = Object.fromEntries(headersMap); | ||
|
||
const headersObjCamelCased: Record<string, unknown> = {}; | ||
Object.keys(headersObj).forEach((key): void => { | ||
headersObjCamelCased[camelCase(key)] = headersObj[key]; | ||
}); | ||
|
||
return { | ||
...response, | ||
data: { | ||
...response.data, | ||
headers: headersObjCamelCased, | ||
}, | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
export default HeadersLink; |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './HeadersLink'; | ||
export * from './types'; | ||
export * from './useRestQuery'; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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