@upstash/search
Version:
An HTTP/REST based AI Search client built on top of Upstash REST API.
63 lines (61 loc) • 2.24 kB
JavaScript
import {
HttpClient,
Search,
UpstashError,
VERSION,
getRuntime
} from "./chunk-4TNZ3BQM.mjs";
// src/platforms/nodejs.ts
var Search2 = class _Search extends Search {
/**
* Creates a new Search instance.
*
* @param vectorIndex - The underlying index used for search operations.
*/
constructor(params) {
const environment = typeof process === "undefined" ? {} : process.env;
const token = params?.token ?? environment.NEXT_PUBLIC_UPSTASH_SEARCH_REST_TOKEN ?? environment.UPSTASH_SEARCH_REST_TOKEN;
const url = params?.url ?? environment.NEXT_PUBLIC_UPSTASH_SEARCH_REST_URL ?? environment.UPSTASH_SEARCH_REST_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 enableTelemetry = environment.UPSTASH_DISABLE_TELEMETRY ? false : params?.enableTelemetry ?? true;
const telemetryHeaders = enableTelemetry ? {
"Upstash-Telemetry-Sdk": `upstash-search-js@${VERSION}`,
"Upstash-Telemetry-Platform": environment.VERCEL ? "vercel" : environment.AWS_REGION ? "aws" : "unknown",
"Upstash-Telemetry-Runtime": getRuntime()
} : {};
const client = new HttpClient({
baseUrl: url,
retry: params?.retry,
headers: { authorization: `Bearer ${token}`, ...telemetryHeaders },
cache: params?.cache === false ? void 0 : params?.cache || "no-store"
});
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
};