vespa-ts
Version:
A reusable TypeScript package for interacting with Vespa search engine with dependency injection support
122 lines • 4.6 kB
JavaScript
// Production server client for Vespa operations
// This client proxies requests to a production server instead of directly calling Vespa
export class ProductionVespaClient {
constructor(baseUrl, apiKey) {
this.baseUrl = baseUrl;
this.apiKey = apiKey;
}
// Method to update the API key
updateApiKey(newApiKey) {
this.apiKey = newApiKey;
}
// Helper function to safely serialize objects by removing circular references
safeStringify(obj) {
const seen = new WeakSet();
return JSON.stringify(obj, (key, value) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return "[Circular Reference]";
}
seen.add(value);
}
return value;
});
}
async makeApiCall(endpoint, payload) {
const url = `${this.baseUrl}/api/vespa/${endpoint}`;
const headers = {
"Content-Type": "application/json",
"x-api-key": this.apiKey,
};
try {
const response = await fetch(url, {
method: "POST",
headers,
body: this.safeStringify(payload),
});
const isJson = response.headers
.get("content-type")
?.includes("application/json");
if (!response.ok) {
const errorBody = isJson ? await response.json() : await response.text();
const errorMessage = `Production server error: ${response.status} ${response.statusText} - ${JSON.stringify(errorBody)}`;
console.error(errorMessage, `API Call failed: POST ${url}`, {
endpoint,
});
throw new Error(errorMessage);
}
console.debug(`Production server success for ${endpoint}`);
return (await response.json());
}
catch (err) {
const message = err instanceof Error ? err.message : String(err);
console.error(`Production server request failed for ${endpoint}: ${message}`, { endpoint, error: err });
throw new Error(`Production server request failed for ${endpoint}: ${message}`);
}
}
// Proxy method for search
async search(payload) {
return this.makeApiCall("search", payload);
}
// Proxy method for autocomplete
async autoComplete(payload) {
return this.makeApiCall("autocomplete", payload);
}
// Proxy method for group search
async groupSearch(payload) {
return this.makeApiCall("group-search", payload);
}
// Proxy method for getItems
async getItems(payload) {
return this.makeApiCall("get-items", payload);
}
// Method overloads for getChatContainerIdByChannelName
async getChatContainerIdByChannelName(channelName) {
return this.makeApiCall("chat-container-by-channel", { channelName });
}
// Method overloads for getChatUserByEmail
async getChatUserByEmail(email) {
return this.makeApiCall("chat-user-by-email", { email });
}
// Proxy method for getDocument
async getDocument(options) {
return this.makeApiCall("get-document", options);
}
// Proxy method for getDocumentsByOnlyDocIds
async getDocumentsByOnlyDocIds(options) {
return this.makeApiCall("get-documents-by-ids", options);
}
// Proxy method for getUsersByNamesAndEmails
async getUsersByNamesAndEmails(payload) {
return this.makeApiCall("get-users-by-names-and-emails", payload);
}
// Proxy method for getDocumentsBythreadId
async getDocumentsBythreadId(threadIds) {
return this.makeApiCall("get-documents-by-thread-id", { threadIds });
}
// Proxy method for getEmailsByThreadIds
async getEmailsByThreadIds(threadIds, email) {
return this.makeApiCall("get-emails-by-thread-ids", {
threadIds,
email,
});
}
// Proxy method for getDocumentsWithField
async getDocumentsWithField(fieldName, options, limit = 100, offset = 0) {
return this.makeApiCall("get-documents-with-field", {
fieldName,
options,
limit,
offset,
});
}
// Proxy method for getRandomDocument
async getRandomDocument(namespace, schema, cluster) {
return this.makeApiCall("get-random-document", {
namespace,
schema,
cluster,
});
}
}
//# sourceMappingURL=productionVespaClient.js.map