Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions packages/js-client-grpc/src/qdrant-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {GrpcClients, createApis} from './api-client.js';
import {QdrantClientConfigError} from './errors.js';
import {ClientVersion, PACKAGE_VERSION} from './client-version.js';
import { GrpcClients, createApis } from './api-client.js';
import { QdrantClientConfigError } from './errors.js';
import { ClientVersion, PACKAGE_VERSION } from './client-version.js';

export type QdrantClientParams = {
port?: number | null;
Expand All @@ -13,6 +13,7 @@ export type QdrantClientParams = {
checkCompatibility?: boolean;
compression?: boolean | 'gzip';
headers?: Record<string, string>;
allowUnsecureConnection?: boolean;
};

export class QdrantClient {
Expand All @@ -35,6 +36,7 @@ export class QdrantClient {
checkCompatibility = true,
compression = true,
headers,
allowUnsecureConnection = false,
}: QdrantClientParams = {}) {
this._https = https ?? typeof apiKey === 'string';
this._scheme = this._https ? 'https' : 'http';
Expand All @@ -49,17 +51,19 @@ export class QdrantClient {
`Only one of \`url\`, \`host\` params can be set. Url is ${url}, host is ${host}`,
);
}

if (host && (host.startsWith('http://') || host.startsWith('https://') || /:\d+$/.test(host))) {
throw new QdrantClientConfigError(
'The `host` param is not expected to contain neither protocol (http:// or https://) nor port (:6333).\n' +
'Try to use the `url` parameter instead.',
'Try to use the `url` parameter instead.',
);
} else if (url) {
if (!(url.startsWith('http://') || url.startsWith('https://'))) {
throw new QdrantClientConfigError(
'The `url` param expected to contain a valid URL starting with a protocol (http:// or https://).',
);
}

const parsedUrl = new URL(url);
this._host = parsedUrl.hostname;
this._port = parsedUrl.port ? Number(parsedUrl.port) : port;
Expand All @@ -68,24 +72,30 @@ export class QdrantClient {
if (this._prefix.length > 0 && parsedUrl.pathname !== '/') {
throw new QdrantClientConfigError(
'Prefix can be set either in `url` or in `prefix`.\n' +
`url is ${url}, prefix is ${parsedUrl.pathname}`,
`url is ${url}, prefix is ${parsedUrl.pathname}`,
);
}
} else {
this._port = port;
this._host = host ?? '127.0.0.1';
}


if (typeof apiKey === 'string') {
if (this._scheme === 'http') {
if (this._scheme === 'http' && !allowUnsecureConnection) {
console.warn('Api key is used with unsecure connection.');
}
}

const address = this._port ? `${this._host}:${this._port}` : this._host;
this._restUri = `${this._scheme}://${address}${this._prefix}`;

this._grcpClients = createApis(this._restUri, {apiKey, timeout, compression, headers});
this._grcpClients = createApis(this._restUri, {
apiKey,
timeout,
compression,
headers,
});

if (checkCompatibility) {
this._grcpClients.service
Expand All @@ -108,11 +118,8 @@ export class QdrantClient {

/**
* API getter
*
* @param string Name of api
* @returns An instance of a namespaced API, generated from grpc services.
*/
api<T extends keyof GrpcClients>(name: T): GrpcClients[T] {
return this._grcpClients[name];
}
}
}
4 changes: 3 additions & 1 deletion packages/js-client-rest/src/qdrant-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type QdrantClientParams = {
* Check compatibility with the server version. Default: `true`
*/
checkCompatibility?: boolean;
allowUnsecureConnection?: boolean;
};

export class QdrantClient {
Expand All @@ -53,6 +54,7 @@ export class QdrantClient {
port = 6333,
timeout = 300_000,
checkCompatibility = true,
allowUnsecureConnection = false,
...args
}: QdrantClientParams = {}) {
this._https = https ?? typeof apiKey === 'string';
Expand Down Expand Up @@ -105,7 +107,7 @@ export class QdrantClient {
});

if (typeof apiKey === 'string') {
if (this._scheme === 'http') {
if (this._scheme === 'http' && !allowUnsecureConnection) {
console.warn('Api key is used with unsecure connection.');
}
headers.set('api-key', apiKey);
Expand Down