Skip to content

Commit ea5bc70

Browse files
committed
Added additionalHeaders option
This allows arbitrary headers to be sent along on requests.
1 parent bde3d42 commit ea5bc70

File tree

6 files changed

+90
-30
lines changed

6 files changed

+90
-30
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,14 @@ var client = SwiftypeAppSearch.createClient({
8080

8181
List of configuration options:
8282

83-
| Option | Required | Description |
84-
| -------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
85-
| hostIdentifier | No | Your **Host Identifier**, should start with `host-`. Required unless explicitly setting `endpointBase` |
86-
| searchKey | Yes | Your **Public Search Key**. It should start with `search-`. |
87-
| engineName | Yes | |
88-
| endpointBase | No | Overrides the base of the Swiftype API endpoint completely. Useful when proxying the Swiftype API, developing against a local server, or a Managed Deploy. Ex. "http://localhost:3002" |
89-
| cacheResponses | No | Whether or not API responses should be cached. Default: `true`. |
83+
| Option | Required | Description |
84+
| ----------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
85+
| hostIdentifier | No | Your **Host Identifier**, should start with `host-`. Required unless explicitly setting `endpointBase` |
86+
| searchKey | Yes | Your **Public Search Key**. It should start with `search-`. |
87+
| engineName | Yes | |
88+
| endpointBase | No | Overrides the base of the Swiftype API endpoint completely. Useful when proxying the Swiftype API, developing against a local server, or a Managed Deploy. Ex. "http://localhost:3002" |
89+
| cacheResponses | No | Whether or not API responses should be cached. Default: `true`. |
90+
| additionalHeaders | No | An Object with keys and values that will be converted to header names and values on all API requests |
9091

9192
### Using with App Search Managed Deploys
9293

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
POST /api/as/v1/engines/node-modules/search.json
2+
authorization: Bearer api-hean6g8dmxnm2shqqiag757a
3+
content-type: bogus/format
4+
x-swiftype-client: swiftype-app-search-javascript
5+
x-swiftype-client-version: 2.3.1
6+
accept: */*
7+
accept-encoding: gzip,deflate
8+
body: {\"query\":\"cat\"}
9+
10+
HTTP/1.1 200 OK
11+
date: Mon, 09 Jul 2018 14:14:38 GMT
12+
content-type: application/json; charset=utf-8
13+
transfer-encoding: chunked
14+
connection: close
15+
vary: Accept-Encoding, Origin
16+
status: 200 OK
17+
x-frame-options: SAMEORIGIN
18+
x-xss-protection: 1; mode=block
19+
x-content-type-options: nosniff
20+
etag: W/"a4200d33b39a6f91d97871fd280d0e97"
21+
cache-control: max-age=0, private, must-revalidate
22+
x-request-id: 8bedbf97e0a9ed0e9335665938502c27
23+
x-runtime: 0.274941
24+
x-swiftype-datacenter: dal05
25+
x-swiftype-frontend-node: web02.dal05
26+
x-swiftype-edge-node: web02.dal05
27+
28+
{"meta":{"warnings":[],"page":{"current":1,"total_pages":65,"total_results":642,"size":10},"request_id":"8bedbf97e0a9ed0e9335665938502c27"},"results":[{"license":{"raw":["BSD"]},"name":{"raw":"source-map-cat"},"repository":{"raw":null},"created":{"raw":"2013-01-23T04:51:39.995Z"},"dependencies":{"raw":["coffee-script","argparse","source-map"]},"keywords":{"raw":null},"description":{"raw":"WIP cat for JS source maps."},"modified":{"raw":"2013-01-23T04:51:42.782Z"},"id":{"raw":"source-map-cat"},"version":{"raw":"0.0.0"},"owners":{"raw":["gregg@aweber.com"]},"_meta":{"score":19.10181}},{"license":{"raw":null},"name":{"raw":"Cat4D"},"repository":{"raw":["https://github.com/Cat4D/Cat4D"]},"homepage":{"raw":"http://www.dumb.cat"},"created":{"raw":"2012-04-12T06:27:43.515Z"},"dependencies":{"raw":null},"keywords":{"raw":["Cat4D","dumb","cat"]},"description":{"raw":"Cat4D Framework Implementation"},"modified":{"raw":"2012-04-12T06:39:23.334Z"},"id":{"raw":"Cat4D"},"version":{"raw":"0.0.2"},"owners":{"raw":["Cat@Dumb.Cat"]},"_meta":{"score":18.906376}},{"license":{"raw":null},"name":{"raw":"response.require"},"repository":{"raw":["https://github.com/kapetan/response.require"]},"created":{"raw":"2013-01-06T13:43:21.012Z"},"dependencies":{"raw":["rex","cat","htmlparser"]},"keywords":{"raw":["root","rex","require"]},"description":{"raw":"Rex Javascript compiling for root"},"modified":{"raw":"2013-01-06T13:43:23.680Z"},"id":{"raw":"response.require"},"version":{"raw":"0.1.0"},"owners":{"raw":["mirza.kapetanovic@gmail.com"]},"_meta":{"score":17.527254}},{"license":{"raw":null},"name":{"raw":"rex-cli"},"repository":{"raw":["https://github.com/gett/rex"]},"created":{"raw":"2012-05-01T21:32:09.544Z"},"dependencies":{"raw":["common","rex","optimist","cat"]},"keywords":{"raw":["browser","commonjs","cli","require"]},"description":{"raw":"rex-cli is the cli equivivalent of rex"},"modified":{"raw":"2013-01-02T12:48:38.182Z"},"id":{"raw":"rex-cli"},"version":{"raw":"0.3.2"},"owners":{"raw":["mathiasbuus@gmail.com"]},"_meta":{"score":15.753918}},{"license":{"raw":["BSD"]},"name":{"raw":"cats"},"repository":{"raw":["https://github.com/luciferous/cats"]},"homepage":{"raw":"http://lcfrs.org/cats"},"created":{"raw":"2012-10-15T23:26:48.468Z"},"dependencies":{"raw":null},"keywords":{"raw":["monad","functor","monoid","async"]},"description":{"raw":"Categories for Javascript"},"modified":{"raw":"2012-10-18T16:14:10.680Z"},"id":{"raw":"cats"},"version":{"raw":"0.0.2"},"owners":{"raw":["neuman.vong@gmail.com"]},"_meta":{"score":12.959391}},{"license":{"raw":null},"name":{"raw":"cts"},"repository":{"raw":["https://github.com/cts/cts-js"]},"homepage":{"raw":"http://www.treesheets.org"},"created":{"raw":"2013-01-28T16:50:08.471Z"},"dependencies":{"raw":null},"keywords":{"raw":["treesheets","cts","cats","structure","cascading treesheets","tree"]},"description":{"raw":"Declarative structural remapping for the web."},"modified":{"raw":"2013-01-28T16:50:09.891Z"},"id":{"raw":"cts"},"version":{"raw":"0.5.0"},"owners":{"raw":["eob@csail.mit.edu"]},"_meta":{"score":7.3216166}},{"license":{"raw":null},"name":{"raw":"redis-user"},"repository":{"raw":null},"homepage":{"raw":"https://github.com/CatChen/redis-user"},"created":{"raw":"2011-06-24T15:38:51.745Z"},"dependencies":{"raw":["jshelpers"]},"keywords":{"raw":null},"description":{"raw":"A simple user and role system for Node.js and Redis."},"modified":{"raw":"2011-07-20T04:00:12.987Z"},"id":{"raw":"redis-user"},"version":{"raw":"0.2.4"},"owners":{"raw":["cathsfz@gmail.com"]},"_meta":{"score":3.2000966}},{"license":{"raw":null},"name":{"raw":"traceurl"},"repository":{"raw":null},"homepage":{"raw":"https://github.com/CatChen/traceurl"},"created":{"raw":"2011-07-16T16:31:00.287Z"},"dependencies":{"raw":["jshelpers"]},"keywords":{"raw":null},"description":{"raw":"A JavaScript utility to trace the original url of a shortened url."},"modified":{"raw":"2011-12-16T16:38:49.255Z"},"id":{"raw":"traceurl"},"version":{"raw":"0.2.8"},"owners":{"raw":["cathsfz@gmail.com"]},"_meta":{"score":2.8856053}},{"license":{"raw":["MIT"]},"name":{"raw":"catcher"},"repository":{"raw":["https://github.com/stuartpb/catcher"]},"created":{"raw":"2013-01-10T01:48:15.743Z"},"dependencies":{"raw":null},"keywords":{"raw":["error","handling"]},"description":{"raw":"microscopic Node error helpers"},"modified":{"raw":"2013-01-10T01:48:16.381Z"},"id":{"raw":"catcher"},"version":{"raw":"0.1.0"},"owners":{"raw":["stuart@testtrack4.com"]},"_meta":{"score":0.73055637}},{"license":{"raw":null},"name":{"raw":"catwalk"},"repository":{"raw":null},"created":{"raw":"2012-12-20T21:05:36.757Z"},"dependencies":{"raw":null},"keywords":{"raw":null},"description":{"raw":"models++"},"modified":{"raw":"2012-12-20T21:05:38.476Z"},"id":{"raw":"catwalk"},"version":{"raw":"0.0.1"},"owners":{"raw":["a@so.hn"]},"_meta":{"score":0.7191726}}]}

src/client.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ export default class Client {
4141
hostIdentifier,
4242
searchKey,
4343
engineName,
44-
{ endpointBase = "", cacheResponses = true } = {}
44+
{ endpointBase = "", cacheResponses = true, additionalHeaders } = {}
4545
) {
46+
this.additionalHeaders = additionalHeaders;
4647
this.searchKey = searchKey;
4748
this.cacheResponses = cacheResponses;
4849
this.engineName = engineName;
@@ -70,7 +71,8 @@ export default class Client {
7071
this.apiEndpoint,
7172
this.querySuggestionPath,
7273
params,
73-
this.cacheResponses
74+
this.cacheResponses,
75+
{ additionalHeaders: this.additionalHeaders }
7476
).then(handleErrorResponse);
7577
}
7678

@@ -207,7 +209,8 @@ export default class Client {
207209
this.apiEndpoint,
208210
`${searchPath}.json`,
209211
params,
210-
this.cacheResponses
212+
this.cacheResponses,
213+
{ additionalHeaders: this.additionalHeaders }
211214
).then(handleErrorResponse);
212215
}
213216

@@ -233,7 +236,8 @@ export default class Client {
233236
this.apiEndpoint,
234237
`${this.clickPath}.json`,
235238
params,
236-
this.cacheResponses
239+
this.cacheResponses,
240+
{ additionalHeaders: this.additionalHeaders }
237241
).then(handleErrorResponse);
238242
}
239243
}

src/request.js

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import { version, name } from "../package.json";
22
import QueryCache from "./query_cache";
33
const cache = new QueryCache();
44

5-
export function request(searchKey, apiEndpoint, path, params, cacheResponses) {
5+
export function request(
6+
searchKey,
7+
apiEndpoint,
8+
path,
9+
params,
10+
cacheResponses,
11+
{ additionalHeaders } = {}
12+
) {
613
const method = "POST";
714
const key = cache.getKey(method, apiEndpoint + path, params);
815
if (cacheResponses) {
@@ -12,28 +19,36 @@ export function request(searchKey, apiEndpoint, path, params, cacheResponses) {
1219
}
1320
}
1421

15-
return _request(method, searchKey, apiEndpoint, path, params).then(
16-
response => {
17-
return response
18-
.json()
19-
.then(json => {
20-
const result = { response: response, json: json };
21-
if (cacheResponses) cache.store(key, result);
22-
return result;
23-
})
24-
.catch(() => {
25-
return { response: response, json: {} };
26-
});
27-
}
28-
);
22+
return _request(method, searchKey, apiEndpoint, path, params, {
23+
additionalHeaders
24+
}).then(response => {
25+
return response
26+
.json()
27+
.then(json => {
28+
const result = { response: response, json: json };
29+
if (cacheResponses) cache.store(key, result);
30+
return result;
31+
})
32+
.catch(() => {
33+
return { response: response, json: {} };
34+
});
35+
});
2936
}
3037

31-
function _request(method, searchKey, apiEndpoint, path, params) {
38+
function _request(
39+
method,
40+
searchKey,
41+
apiEndpoint,
42+
path,
43+
params,
44+
{ additionalHeaders } = {}
45+
) {
3246
const headers = new Headers({
3347
Authorization: `Bearer ${searchKey}`,
3448
"Content-Type": "application/json",
3549
"X-Swiftype-Client": name,
36-
"X-Swiftype-Client-Version": version
50+
"X-Swiftype-Client-Version": version,
51+
...additionalHeaders
3752
});
3853

3954
return fetch(`${apiEndpoint}${path}`, {

src/swiftype_app_search.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ export function createClient({
99
searchKey,
1010
engineName,
1111
endpointBase,
12-
cacheResponses
12+
cacheResponses,
13+
additionalHeaders
1314
}) {
1415
hostIdentifier = hostIdentifier || accountHostKey; // accountHostKey is deprecated
1516
searchKey = searchKey || apiKey; //apiKey is deprecated
1617
return new Client(hostIdentifier, searchKey, engineName, {
1718
endpointBase,
18-
cacheResponses
19+
cacheResponses,
20+
additionalHeaders
1921
});
2022
}

tests/client.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,5 +399,15 @@ describe("Client", () => {
399399
expect(e).toEqual(new Error("[404]"));
400400
}
401401
});
402+
403+
// Fixture: additional_headers
404+
it.only("should pass along additional headers", async () => {
405+
const headerClient = new Client(hostIdentifier, searchKey, engineName, {
406+
additionalHeaders: { "Content-Type": "bogus/format" }
407+
});
408+
await headerClient.search("cat", {});
409+
// REPLAY will fail this spec if the additional helper is not sent
410+
expect(true).toBe(true);
411+
});
402412
});
403413
});

0 commit comments

Comments
 (0)