Skip to content

Commit 3bb6290

Browse files
authored
Made search key optional (#30)
1 parent add0012 commit 3bb6290

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@ var client = ElasticAppSearch.createClient({
105105

106106
### List of configuration options:
107107

108-
| Option | Required | Description |
109-
| ----------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
110-
| hostIdentifier | No | Your **Host Identifier**, should start with `host-`. Required unless explicitly setting `endpointBase` |
111-
| searchKey | Yes | Your **Public Search Key**. It should start with `search-`. |
112-
| engineName | Yes | |
113-
| endpointBase | No | Overrides the base of the App Search API endpoint completely. Useful when proxying the App Search API, developing against a local server, or a Self-Managed or Cloud Deployment. Ex. "http://localhost:3002" |
114-
| cacheResponses | No | Whether or not API responses should be cached. Default: `true`. |
115-
| additionalHeaders | No | An Object with keys and values that will be converted to header names and values on all API requests |
108+
| Option | Required | Description |
109+
| ----------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
110+
| hostIdentifier | No | Your **Host Identifier**, should start with `host-`. Required unless explicitly setting `endpointBase` |
111+
| searchKey | No | Your **Public Search Key**. It should start with `search-`.<br/><br/>NOTE: This is not _technically_ required, but in 99% of cases it should be provided. There is a small edge case for not providing this, mainly useful for internal App Search usage, where this can be ommited in order to leverage App Search's session based authentication. |
112+
| engineName | Yes | |
113+
| endpointBase | No | Overrides the base of the App Search API endpoint completely. Useful when proxying the App Search API, developing against a local server, or a Self-Managed or Cloud Deployment. Ex. "http://localhost:3002" |
114+
| cacheResponses | No | Whether or not API responses should be cached. Default: `true`. |
115+
| additionalHeaders | No | An Object with keys and values that will be converted to header names and values on all API requests |
116116

117117
### API Methods
118118

src/request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function _request(
4444
{ additionalHeaders } = {}
4545
) {
4646
const headers = new Headers({
47-
Authorization: `Bearer ${searchKey}`,
47+
...(searchKey && { Authorization: `Bearer ${searchKey}` }),
4848
"Content-Type": "application/json",
4949
"X-Swiftype-Client": "elastic-app-search-javascript",
5050
"X-Swiftype-Client-Version": version,

tests/request.spec.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,34 @@ describe("request", () => {
1919

2020
beforeEach(() => {
2121
global.Headers = Headers;
22+
jest.resetAllMocks();
2223
global.fetch = jest
2324
.fn()
2425
.mockImplementation(() => Promise.resolve(response));
2526
});
2627

27-
beforeEach(() => {
28-
global.fetch.mockClear();
28+
it("can send a fetch request, authenticated by the provided search key", async () => {
29+
const res = await request(searchKey, endpoint, path, params, false);
30+
expect(res.response).toBe(response);
31+
expect(global.fetch.mock.calls.length).toBe(1);
32+
var [_, options] = global.fetch.mock.calls[0];
33+
expect(options.headers.get("Authorization")).toEqual("Bearer api-12345");
2934
});
3035

31-
it("can fetch", async () => {
32-
const res = await request(searchKey, endpoint, path, params, true);
33-
expect(res.response).toBe(response);
36+
// The use case for this is mostly internal to Elastic, where we rely on the logged in user session (via cookies) to authenticate
37+
it("can send an authenticated fetch request, when no search key is provided", async () => {
38+
const res = await request(undefined, endpoint, path, params, false);
3439
expect(global.fetch.mock.calls.length).toBe(1);
40+
var [_, options] = global.fetch.mock.calls[0];
41+
expect(options.headers.has("Authorization")).toBe(false);
3542
});
3643

3744
it("will return a cached response if already called once", async () => {
3845
const res = await request(searchKey, endpoint, path, params, true);
46+
await request(searchKey, endpoint, path, params, true);
47+
await request(searchKey, endpoint, path, params, true);
3948
expect(res.response).toBe(response);
40-
expect(global.fetch.mock.calls.length).toBe(0);
49+
expect(global.fetch.mock.calls.length).toBe(1);
4150
});
4251

4352
it("will not return the cached response if endpoint changes", async () => {

0 commit comments

Comments
 (0)