-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.ts
145 lines (134 loc) · 3.82 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import {
HttpClient,
Requester,
RequesterConfig,
UpstashRequest,
UpstashResponse,
} from "@http";
import * as core from "./src/vector";
export type * from "@commands/types";
export type { Requester, UpstashRequest, UpstashResponse };
/**
* Connection credentials for upstash vector.
* Get them from https://console.upstash.com/vector/<uuid>
*/
export type IndexConfig = {
/**
* UPSTASH_VECTOR_REST_URL
*/
url?: string;
/**
* UPSTASH_VECTOR_REST_TOKEN
*/
token?: string;
/**
* The signal will allow aborting requests on the fly.
* For more check: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
*/
signal?: AbortSignal;
} & RequesterConfig;
/**
* Serverless vector client for upstash.
*/
export class Index extends core.Index {
/**
* Create a new vector client by providing the url and token
*
* @example
* ```typescript
* const index = new Index({
* url: "<UPSTASH_VECTOR_REST_URL>",
* token: "<UPSTASH_VECTOR_REST_TOKEN>",
* });
* ```
* OR
* This will automatically get environment variables from .env file
* ```typescript
* const index = new Index();
* ```
*/
constructor(config?: IndexConfig);
/**
* Create a new vector client by providing a custom `Requester` implementation
*
* @example
* ```ts
*
* import { UpstashRequest, Requester, UpstashResponse, vector } from "@upstash/vector"
*
* const requester: Requester = {
* request: <TResult>(req: UpstashRequest): Promise<UpstashResponse<TResult>> => {
* // ...
* }
* }
*
* const vector = new vector(requester)
* ```
*/
constructor(requesters?: Requester);
constructor(configOrRequester?: IndexConfig | Requester) {
if (
typeof configOrRequester !== "undefined" &&
"request" in configOrRequester
) {
super(configOrRequester);
return;
}
const token =
configOrRequester?.token ??
process.env.NEXT_PUBLIC_UPSTASH_VECTOR_REST_TOKEN ??
process.env.UPSTASH_VECTOR_REST_TOKEN;
const url =
configOrRequester?.url ??
process.env.NEXT_PUBLIC_UPSTASH_VECTOR_REST_URL ??
process.env.UPSTASH_VECTOR_REST_URL;
if (!token) {
throw new Error("UPSTASH_VECTOR_REST_TOKEN is missing!");
}
if (!url) {
throw new Error("UPSTASH_VECTOR_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 client = new HttpClient({
baseUrl: url,
retry: configOrRequester?.retry,
headers: { authorization: `Bearer ${token}` },
cache: configOrRequester?.cache || "no-store",
signal: configOrRequester?.signal,
});
super(client);
}
/**
* Create a new Upstash Vector instance from environment variables.
*
* Use this to automatically load connection secrets from your environment
* variables. For instance when using the Vercel integration.
*
* This tries to load `UPSTASH_VECTOR_REST_URL` and `UPSTASH_VECTOR_REST_TOKEN` from
* your environment using `process.env`.
*/
static fromEnv(config?: Omit<IndexConfig, "url" | "token">): Index {
const url = process?.env.UPSTASH_VECTOR_REST_URL;
if (!url) {
throw new Error(
"Unable to find environment variable: `UPSTASH_VECTOR_REST_URL`"
);
}
const token = process?.env.UPSTASH_VECTOR_REST_TOKEN;
if (!token) {
throw new Error(
"Unable to find environment variable: `UPSTASH_VECTOR_REST_TOKEN`"
);
}
return new Index({ ...config, url, token });
}
}