weaviate-client
Version:
JS/TS client for Weaviate
86 lines (85 loc) • 3.04 kB
JavaScript
import { WeaviateStartUpError } from '../errors.js';
import { AuthAccessTokenCredentials, AuthClientCredentials, AuthUserPasswordCredentials, } from './auth.js';
export function connectToWeaviateCloud(clusterURL, clientMaker, options) {
// check if the URL is set
if (!clusterURL)
throw new Error('Missing `clusterURL` parameter');
if (!clusterURL.startsWith('http')) {
clusterURL = `https://${clusterURL}`;
}
const url = new URL(clusterURL);
let grpcHost;
if (url.hostname.endsWith('.weaviate.network')) {
const [ident, ...rest] = url.hostname.split('.');
grpcHost = `${ident}.grpc.${rest.join('.')}`;
}
else {
grpcHost = `grpc-${url.hostname}`;
}
const { authCredentials: auth, headers, ...rest } = options || {};
if ([AuthAccessTokenCredentials, AuthClientCredentials, AuthUserPasswordCredentials].some((c) => auth instanceof c)) {
console.warn('Connecting to Weaviate Cloud (WCD) using OIDC is deprecated and will be removed in August 2025. Please use API keys instead.');
}
return clientMaker({
connectionParams: {
http: {
secure: true,
host: url.hostname,
port: 443,
},
grpc: {
secure: true,
host: grpcHost,
port: 443,
},
},
auth,
headers: options?.headers,
...rest,
}).catch((e) => {
throw new WeaviateStartUpError(`Weaviate failed to startup with message: ${e.message}`);
});
}
export function connectToLocal(clientMaker, options) {
const { host, port, grpcPort, authCredentials: auth, ...rest } = options || {};
return clientMaker({
connectionParams: {
http: {
secure: false,
host: host || 'localhost',
port: port || 8080,
},
grpc: {
secure: false,
host: host || 'localhost',
port: grpcPort || 50051,
},
},
auth,
...rest,
}).catch((e) => {
throw new WeaviateStartUpError(`Weaviate failed to startup with message: ${e.message}`);
});
}
export function connectToCustom(clientMaker, options) {
const { httpHost, httpPath, httpPort, httpSecure, grpcHost, grpcPort, grpcSecure, authCredentials: auth, ...rest } = options || {};
return clientMaker({
connectionParams: {
http: {
secure: httpSecure || false,
host: httpHost || 'localhost',
path: httpPath || '',
port: httpPort || 8080,
},
grpc: {
secure: grpcSecure || false,
host: grpcHost || 'localhost',
port: grpcPort || 50051,
},
},
auth,
...rest,
}).catch((e) => {
throw new WeaviateStartUpError(`Weaviate failed to startup with message: ${e.message}`);
});
}