Add IdentityApi to plugin-search
Signed-off-by: Daniel Johansson <daniel.johansson@acast.com>
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-search': minor
|
||||
---
|
||||
|
||||
add IdentityApi support
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@backstage/plugin-search",
|
||||
"version": "0.4.0",
|
||||
"version": "0.5.0",
|
||||
"main": "src/index.ts",
|
||||
"types": "src/index.ts",
|
||||
"license": "Apache-2.0",
|
||||
|
||||
@@ -26,8 +26,20 @@ describe('apis', () => {
|
||||
|
||||
const baseUrl = 'https://base-url.com/';
|
||||
const getBaseUrl = jest.fn().mockResolvedValue(baseUrl);
|
||||
|
||||
const token = 'AUTHTOKEN';
|
||||
const withToken = jest.fn().mockResolvedValue(token);
|
||||
const withoutToken = jest.fn().mockResolvedValue(undefined);
|
||||
const createIdentityApiMock = (getIdToken: any) => ({
|
||||
getIdToken,
|
||||
getUserId: jest.fn(),
|
||||
getProfile: jest.fn(),
|
||||
signOut: jest.fn(),
|
||||
});
|
||||
|
||||
const client = new SearchClient({
|
||||
discoveryApi: { getBaseUrl },
|
||||
identityApi: createIdentityApiMock(withoutToken),
|
||||
});
|
||||
|
||||
const json = jest.fn();
|
||||
@@ -41,7 +53,21 @@ describe('apis', () => {
|
||||
it('Fetch is called with expected URL (including stringified Q params)', async () => {
|
||||
await client.query(query);
|
||||
expect(getBaseUrl).toHaveBeenLastCalledWith('search/query');
|
||||
expect(fetch).toHaveBeenLastCalledWith(`${baseUrl}?term=&pageCursor=`);
|
||||
expect(fetch).toHaveBeenLastCalledWith(`${baseUrl}?term=&pageCursor=`, {
|
||||
headers: {},
|
||||
});
|
||||
});
|
||||
|
||||
it('Sets Authorization if token is available', async () => {
|
||||
const authedClient = new SearchClient({
|
||||
discoveryApi: { getBaseUrl },
|
||||
identityApi: createIdentityApiMock(withToken),
|
||||
});
|
||||
await authedClient.query(query);
|
||||
expect(getBaseUrl).toHaveBeenLastCalledWith('search/query');
|
||||
expect(fetch).toHaveBeenLastCalledWith(`${baseUrl}?term=&pageCursor=`, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
});
|
||||
|
||||
it('Resolves JSON from fetch response', async () => {
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { createApiRef, DiscoveryApi } from '@backstage/core';
|
||||
import { createApiRef, DiscoveryApi, IdentityApi } from '@backstage/core';
|
||||
import { SearchQuery, SearchResultSet } from '@backstage/search-common';
|
||||
import qs from 'qs';
|
||||
|
||||
@@ -29,17 +29,25 @@ export interface SearchApi {
|
||||
|
||||
export class SearchClient implements SearchApi {
|
||||
private readonly discoveryApi: DiscoveryApi;
|
||||
private readonly identityApi: IdentityApi;
|
||||
|
||||
constructor(options: { discoveryApi: DiscoveryApi }) {
|
||||
constructor(options: {
|
||||
discoveryApi: DiscoveryApi;
|
||||
identityApi: IdentityApi;
|
||||
}) {
|
||||
this.discoveryApi = options.discoveryApi;
|
||||
this.identityApi = options.identityApi;
|
||||
}
|
||||
|
||||
async query(query: SearchQuery): Promise<SearchResultSet> {
|
||||
const token = await this.identityApi.getIdToken();
|
||||
const queryString = qs.stringify(query);
|
||||
const url = `${await this.discoveryApi.getBaseUrl(
|
||||
'search/query',
|
||||
)}?${queryString}`;
|
||||
const response = await fetch(url);
|
||||
const response = await fetch(url, {
|
||||
headers: token ? { Authorization: `Bearer ${token}` } : {},
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
createRoutableExtension,
|
||||
discoveryApiRef,
|
||||
createComponentExtension,
|
||||
identityApiRef,
|
||||
} from '@backstage/core';
|
||||
import { SearchClient, searchApiRef } from './apis';
|
||||
|
||||
@@ -38,9 +39,9 @@ export const searchPlugin = createPlugin({
|
||||
apis: [
|
||||
createApiFactory({
|
||||
api: searchApiRef,
|
||||
deps: { discoveryApi: discoveryApiRef },
|
||||
factory: ({ discoveryApi }) => {
|
||||
return new SearchClient({ discoveryApi });
|
||||
deps: { discoveryApi: discoveryApiRef, identityApi: identityApiRef },
|
||||
factory: ({ discoveryApi, identityApi }) => {
|
||||
return new SearchClient({ discoveryApi, identityApi });
|
||||
},
|
||||
}),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user