@antfly/sdk 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -161,14 +161,87 @@ var AntflyClient = class {
161
161
  },
162
162
  /**
163
163
  * Lookup a specific key in a table
164
+ * @param tableName - Name of the table
165
+ * @param key - Key of the record to lookup
166
+ * @param options - Optional parameters
167
+ * @param options.fields - Comma-separated list of fields to include (e.g., "title,author,metadata.tags")
164
168
  */
165
- lookup: async (tableName, key) => {
169
+ lookup: async (tableName, key, options) => {
166
170
  const { data, error } = await this.client.GET("/tables/{tableName}/lookup/{key}", {
167
- params: { path: { tableName, key } }
171
+ params: {
172
+ path: { tableName, key },
173
+ query: options?.fields ? { fields: options.fields } : void 0
174
+ }
168
175
  });
169
176
  if (error) throw new Error(`Key lookup failed: ${error.error}`);
170
177
  return data;
171
178
  },
179
+ /**
180
+ * Scan keys in a table within a key range
181
+ * Returns documents as an async iterable, streaming results as NDJSON.
182
+ * @param tableName - Name of the table
183
+ * @param request - Scan request with optional key range, field projection, and filtering
184
+ * @returns AsyncGenerator yielding documents with their keys
185
+ */
186
+ scan: (tableName, request) => {
187
+ const config = this.config;
188
+ async function* scanGenerator() {
189
+ const headers = {
190
+ "Content-Type": "application/json",
191
+ Accept: "application/x-ndjson"
192
+ };
193
+ if (config.auth) {
194
+ const auth = btoa(`${config.auth.username}:${config.auth.password}`);
195
+ headers["Authorization"] = `Basic ${auth}`;
196
+ }
197
+ Object.assign(headers, config.headers);
198
+ const response = await fetch(`${config.baseUrl}/tables/${tableName}/lookup`, {
199
+ method: "POST",
200
+ headers,
201
+ body: JSON.stringify(request || {})
202
+ });
203
+ if (!response.ok) {
204
+ const errorText = await response.text();
205
+ throw new Error(`Scan failed: ${response.status} ${errorText}`);
206
+ }
207
+ if (!response.body) {
208
+ throw new Error("Response body is null");
209
+ }
210
+ const reader = response.body.getReader();
211
+ const decoder = new TextDecoder();
212
+ let buffer = "";
213
+ while (true) {
214
+ const { done, value } = await reader.read();
215
+ if (done) break;
216
+ buffer += decoder.decode(value, { stream: true });
217
+ const lines = buffer.split("\n");
218
+ buffer = lines.pop() || "";
219
+ for (const line of lines) {
220
+ if (line.trim()) {
221
+ yield JSON.parse(line);
222
+ }
223
+ }
224
+ }
225
+ if (buffer.trim()) {
226
+ yield JSON.parse(buffer);
227
+ }
228
+ }
229
+ return scanGenerator();
230
+ },
231
+ /**
232
+ * Scan keys in a table and collect all results into an array
233
+ * Convenience method that consumes the scan AsyncGenerator
234
+ * @param tableName - Name of the table
235
+ * @param request - Scan request with optional key range, field projection, and filtering
236
+ * @returns Promise with array of all matching documents
237
+ */
238
+ scanAll: async (tableName, request) => {
239
+ const results = [];
240
+ for await (const doc of this.tables.scan(tableName, request)) {
241
+ results.push(doc);
242
+ }
243
+ return results;
244
+ },
172
245
  /**
173
246
  * RAG (Retrieval-Augmented Generation) query on a specific table with streaming or citations
174
247
  * @param tableName - Name of the table to query