@upstash/search
Version:
An HTTP/REST based AI Search client built on top of Upstash REST API.
59 lines (57 loc) • 1.75 kB
JavaScript
import {
HttpClient,
Search,
UpstashError,
VERSION
} from "./chunk-4TNZ3BQM.mjs";
// src/platforms/cloudflare.ts
var Search2 = class _Search extends Search {
/**
* Creates a new Search instance.
*
* @param vectorIndex - The underlying index used for search operations.
*/
constructor(params) {
const token = params?.token;
const url = params?.url;
if (!token) {
throw new UpstashError("UPSTASH_SEARCH_REST_TOKEN is missing!");
}
if (!url) {
throw new UpstashError("UPSTASH_SEARCH_REST_URL is missing!");
}
if (url.startsWith(" ") || url.endsWith(" ") || /\r|\n/.test(url)) {
console.warn("The vector url contains whitespace or newline, which can cause errors!");
}
if (token.startsWith(" ") || token.endsWith(" ") || /\r|\n/.test(token)) {
console.warn("The vector token contains whitespace or newline, which can cause errors!");
}
const telemetryHeaders = params.enableTelemetry ?? true ? {
"Upstash-Telemetry-Sdk": `upstash-search-js@${VERSION}`,
"Upstash-Telemetry-Platform": "cloudflare"
} : {};
const client = new HttpClient({
baseUrl: url,
retry: params?.retry,
headers: { authorization: `Bearer ${token}`, ...telemetryHeaders },
cache: params?.cache === false ? void 0 : params?.cache
});
super(client);
}
/**
* Creates a new Search instance using env variables
* `UPSTASH_SEARCH_REST_URL` and
* `UPSTASH_SEARCH_REST_TOKEN`
*
* @param env
* @returns
*/
static fromEnv = (env, config) => {
const url = env?.UPSTASH_SEARCH_REST_URL;
const token = env?.UPSTASH_SEARCH_REST_TOKEN;
return new _Search({ url, token, ...config });
};
};
export {
Search2 as Search
};