Skip to content

Commit a57ab5d

Browse files
committed
add weaviate embedding service headers
1 parent 4cf0637 commit a57ab5d

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed

package-lock.json

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
"test:coverage": "npm run test -- --coverage",
2525
"build": "npm run build:node",
2626
"build:web": "tsup",
27-
"build:cjs": "tsc --module commonjs --outDir dist/node/cjs && touch dist/node/cjs/package.json && echo '{\"type\": \"commonjs\"}' > dist/node/cjs/package.json",
28-
"build:esm": "tsc --module esnext --outDir dist/node/esm && touch dist/node/esm/package.json && echo '{\"type\": \"module\"}' > dist/node/esm/package.json",
27+
"build:cjs": "tsc --module commonjs --moduleResolution node10 --outDir dist/node/cjs && touch dist/node/cjs/package.json && echo '{\"type\": \"commonjs\"}' > dist/node/cjs/package.json",
28+
"build:esm": "tsc --outDir dist/node/esm && touch dist/node/esm/package.json && echo '{\"type\": \"module\"}' > dist/node/esm/package.json",
2929
"build:node": "npm run lint && npm run build:cjs && npm run build:esm && prettier --write --no-error-on-unmatched-pattern '**/dist/**/*.{ts,js}'",
3030
"prepack": "npm run build",
3131
"lint": "eslint --ext .ts,.js .",
@@ -96,7 +96,7 @@
9696
"tsup": "^8.0.2",
9797
"typedoc": "^0.25.12",
9898
"typedoc-plugin-extras": "^3.0.0",
99-
"typescript": "5.1.3",
99+
"typescript": "^5.3.3",
100100
"uuid": "^9.0.1"
101101
},
102102
"lint-staged": {

src/connection/helpers.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import weaviate from '../index.js';
2+
import { connectToWeaviateCloud } from './helpers.js';
23

34
const WCD_URL = 'https://piblpmmdsiknacjnm1ltla.c1.europe-west3.gcp.weaviate.cloud';
45
const WCD_KEY = 'cy4ua772mBlMdfw3YnclqAWzFhQt0RLIN0sl';
@@ -18,6 +19,40 @@ describe('Testing of the connection helper methods', () => {
1819
});
1920
});
2021

22+
describe('adds Weaviate Embedding Service headers', () => {
23+
it('to empty headers', async () => {
24+
const clientMakerMock = jest.fn().mockResolvedValue(undefined);
25+
26+
await connectToWeaviateCloud(WCD_URL, clientMakerMock, {
27+
authCredentials: new weaviate.ApiKey(WCD_KEY),
28+
});
29+
30+
expect(clientMakerMock.mock.calls[0][0]).toMatchObject({
31+
headers: {
32+
'X-Weaviate-Api-Key': WCD_KEY,
33+
'X-Weaviate-Cluster-Url': WCD_URL,
34+
},
35+
});
36+
});
37+
38+
it('to existing headers', async () => {
39+
const clientMakerMock = jest.fn().mockResolvedValue(undefined);
40+
41+
await connectToWeaviateCloud(WCD_URL, clientMakerMock, {
42+
authCredentials: new weaviate.ApiKey(WCD_KEY),
43+
headers: { existingHeader: 'existingValue' },
44+
});
45+
46+
expect(clientMakerMock.mock.calls[0][0]).toMatchObject({
47+
headers: {
48+
existingHeader: 'existingValue',
49+
'X-Weaviate-Api-Key': WCD_KEY,
50+
'X-Weaviate-Cluster-Url': WCD_URL,
51+
},
52+
});
53+
});
54+
});
55+
2156
it('should connect to a local cluster', () => {
2257
return weaviate
2358
.connectToLocal()

src/connection/helpers.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { WeaviateStartUpError } from '../errors.js';
22
import { ClientParams, WeaviateClient } from '../index.js';
3-
import { AuthCredentials } from './auth.js';
3+
import { AuthCredentials, isApiKey, mapApiKey } from './auth.js';
44
import { ProxiesParams, TimeoutParams } from './http.js';
55

66
/** The options available to the `weaviate.connectToWeaviateCloud` method. */
@@ -100,7 +100,7 @@ export function connectToWeaviateCloud(
100100
},
101101
},
102102
auth: options?.authCredentials,
103-
headers: options?.headers,
103+
headers: addWeaviateEmbeddingServiceHeaders(clusterURL, options),
104104
}).catch((e) => {
105105
throw new WeaviateStartUpError(`Weaviate failed to startup with message: ${e.message}`);
106106
});
@@ -155,3 +155,15 @@ export function connectToCustom(
155155
throw new WeaviateStartUpError(`Weaviate failed to startup with message: ${e.message}`);
156156
});
157157
}
158+
159+
function addWeaviateEmbeddingServiceHeaders(clusterURL: string, options?: ConnectToWeaviateCloudOptions) {
160+
if (!isApiKey(options?.authCredentials)) {
161+
return options?.headers;
162+
}
163+
164+
return {
165+
...options.headers,
166+
'X-Weaviate-Api-Key': mapApiKey(options.authCredentials).apiKey,
167+
'X-Weaviate-Cluster-Url': clusterURL,
168+
};
169+
}

0 commit comments

Comments
 (0)