Skip to content

Commit

Permalink
add weaviate embedding service headers
Browse files Browse the repository at this point in the history
  • Loading branch information
augustas1 committed Nov 27, 2024
1 parent 4cf0637 commit a57ab5d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"test:coverage": "npm run test -- --coverage",
"build": "npm run build:node",
"build:web": "tsup",
"build:cjs": "tsc --module commonjs --outDir dist/node/cjs && touch dist/node/cjs/package.json && echo '{\"type\": \"commonjs\"}' > dist/node/cjs/package.json",
"build:esm": "tsc --module esnext --outDir dist/node/esm && touch dist/node/esm/package.json && echo '{\"type\": \"module\"}' > dist/node/esm/package.json",
"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",
"build:esm": "tsc --outDir dist/node/esm && touch dist/node/esm/package.json && echo '{\"type\": \"module\"}' > dist/node/esm/package.json",
"build:node": "npm run lint && npm run build:cjs && npm run build:esm && prettier --write --no-error-on-unmatched-pattern '**/dist/**/*.{ts,js}'",
"prepack": "npm run build",
"lint": "eslint --ext .ts,.js .",
Expand Down Expand Up @@ -96,7 +96,7 @@
"tsup": "^8.0.2",
"typedoc": "^0.25.12",
"typedoc-plugin-extras": "^3.0.0",
"typescript": "5.1.3",
"typescript": "^5.3.3",
"uuid": "^9.0.1"
},
"lint-staged": {
Expand Down
35 changes: 35 additions & 0 deletions src/connection/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import weaviate from '../index.js';
import { connectToWeaviateCloud } from './helpers.js';

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

describe('adds Weaviate Embedding Service headers', () => {
it('to empty headers', async () => {
const clientMakerMock = jest.fn().mockResolvedValue(undefined);

await connectToWeaviateCloud(WCD_URL, clientMakerMock, {
authCredentials: new weaviate.ApiKey(WCD_KEY),
});

expect(clientMakerMock.mock.calls[0][0]).toMatchObject({
headers: {
'X-Weaviate-Api-Key': WCD_KEY,
'X-Weaviate-Cluster-Url': WCD_URL,
},
});
});

it('to existing headers', async () => {
const clientMakerMock = jest.fn().mockResolvedValue(undefined);

await connectToWeaviateCloud(WCD_URL, clientMakerMock, {
authCredentials: new weaviate.ApiKey(WCD_KEY),
headers: { existingHeader: 'existingValue' },
});

expect(clientMakerMock.mock.calls[0][0]).toMatchObject({
headers: {
existingHeader: 'existingValue',
'X-Weaviate-Api-Key': WCD_KEY,
'X-Weaviate-Cluster-Url': WCD_URL,
},
});
});
});

it('should connect to a local cluster', () => {
return weaviate
.connectToLocal()
Expand Down
16 changes: 14 additions & 2 deletions src/connection/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WeaviateStartUpError } from '../errors.js';
import { ClientParams, WeaviateClient } from '../index.js';
import { AuthCredentials } from './auth.js';
import { AuthCredentials, isApiKey, mapApiKey } from './auth.js';
import { ProxiesParams, TimeoutParams } from './http.js';

/** The options available to the `weaviate.connectToWeaviateCloud` method. */
Expand Down Expand Up @@ -100,7 +100,7 @@ export function connectToWeaviateCloud(
},
},
auth: options?.authCredentials,
headers: options?.headers,
headers: addWeaviateEmbeddingServiceHeaders(clusterURL, options),
}).catch((e) => {
throw new WeaviateStartUpError(`Weaviate failed to startup with message: ${e.message}`);
});
Expand Down Expand Up @@ -155,3 +155,15 @@ export function connectToCustom(
throw new WeaviateStartUpError(`Weaviate failed to startup with message: ${e.message}`);
});
}

function addWeaviateEmbeddingServiceHeaders(clusterURL: string, options?: ConnectToWeaviateCloudOptions) {
if (!isApiKey(options?.authCredentials)) {
return options?.headers;
}

return {
...options.headers,
'X-Weaviate-Api-Key': mapApiKey(options.authCredentials).apiKey,
'X-Weaviate-Cluster-Url': clusterURL,
};
}

0 comments on commit a57ab5d

Please sign in to comment.