Skip to content

Commit

Permalink
Engines: Customise route fn (#924)
Browse files Browse the repository at this point in the history
* engines-connector

* update dependencies

* updates based on PR comments

* support to customise the engine _search url
  • Loading branch information
joemcelroy authored Jan 30, 2023
1 parent a3f818a commit 8ba80d9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,46 @@ describe("EngineTransporter", () => {
expect(response.hits.hits[0]._source.title).toEqual("My title");
});
});

it("perform a request overriding getRoute", () => {
const transporter = new EngineTransporter(
"http://localhost:9200",
"my_engine",
"apikey",
(host, engineName) => `${host}/internal/${engineName}/search`
);

const body = {
query: {
match_all: {}
}
};

nock("http://localhost:9200", {
reqheaders: {
authorization: "ApiKey apikey"
}
})
.post("/internal/my_engine/search")
.reply(200, () => ({
hits: {
hits: [
{
_id: "1",
_source: {
title: "My title"
}
}
]
}
}));

return transporter
.performRequest({
body
} as SearchRequest)
.then((response: SearchResponse) => {
expect(response.hits.hits).toHaveLength(1);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
BaseFilters,
Filter as SKFilter,
GeoDistanceOptionsFacet,
MultiMatchQuery,
MultiQueryOptionsFacet,
RefinementSelectFacet,
SearchkitConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
ResponseState
} from "@elastic/search-ui";
import Searchkit from "@searchkit/sdk";
import { EngineRouteFn } from "../../types";
import { EngineTransporter } from "../transporter";
import buildConfiguration, { buildBaseFilters } from "./Configuration";
import buildRequest from "./Request";
Expand All @@ -15,6 +16,7 @@ interface SearchHandlerConfiguration {
host: string;
engineName: string;
apiKey: string;
engineRouteFn?: EngineRouteFn;
}

export default async function handleRequest(
Expand All @@ -32,7 +34,8 @@ export default async function handleRequest(
const transporter = new EngineTransporter(
host,
engineName,
configuration.apiKey
configuration.apiKey,
configuration.engineRouteFn
);

const request = Searchkit(searchkitConfig, transporter);
Expand Down
26 changes: 14 additions & 12 deletions packages/search-ui-engines-connector/src/handlers/transporter.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import type { SearchkitTransporter } from "@searchkit/sdk";
import { SearchRequest, SearchResponse } from "../types";
import { SearchRequest, SearchResponse, EngineRouteFn } from "../types";

function defaultEngineRoute(host: string, engineName: string): string {
return `${host}/api/engines/${engineName}/_search`;
}

export class EngineTransporter implements SearchkitTransporter {
constructor(
private host: string,
private engineName: string,
private apiKey: string
private apiKey: string,
private getRoute: EngineRouteFn = defaultEngineRoute
) {}

async performRequest(requestBody: SearchRequest): Promise<SearchResponse> {
if (!fetch)
throw new Error("Fetch is not supported in this browser / environment");

const response = await fetch(
this.host + `/api/engines/${this.engineName}/_search`,
{
method: "POST",
body: JSON.stringify(requestBody),
headers: {
"Content-Type": "application/json",
Authorization: `ApiKey ${this.apiKey}`
}
const response = await fetch(this.getRoute(this.host, this.engineName), {
method: "POST",
body: JSON.stringify(requestBody),
headers: {
"Content-Type": "application/json",
Authorization: `ApiKey ${this.apiKey}`
}
);
});
const json = await response.json();
return json;
}
Expand Down
1 change: 1 addition & 0 deletions packages/search-ui-engines-connector/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import type { estypes } from "@elastic/elasticsearch";

export type SearchRequest = estypes.SearchRequest;
export type SearchResponse = estypes.SearchResponse<Record<string, unknown>>;
export type EngineRouteFn = (host: string, engineName: string) => string;

0 comments on commit 8ba80d9

Please sign in to comment.