Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get and delete indexes by name #61

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "opperai",
"version": "2.2.0",
"version": "2.3.0",
"description": "Typescript SDK for the Opper API",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
110 changes: 110 additions & 0 deletions src/__tests__/opperai-indexes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,116 @@ describe("OpperAIIndexes", () => {
);
});
});

describe("get", () => {
it("should retrieve an index by name", async () => {
const mockIndex = mockIndexes[0];
(global.fetch as jest.Mock).mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve(mockIndex),
});

const index = await opperAIIndexes.get("Test Index 1");

expect(index).toBeInstanceOf(Index);
expect(index?.uuid).toBe(mockIndex.uuid);
expect(index?._index).toEqual(mockIndex);
expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledWith("https://api.opper.ai/v1/indexes/by-name/Test%20Index%201", {
method: "GET",
headers: {
"X-OPPER-API-KEY": "test-api-key",
"User-Agent": "opper-node/0.0.0",
"Content-Type": "application/json",
},
});
});

it("should return null when index is not found", async () => {
(global.fetch as jest.Mock).mockResolvedValueOnce({
ok: false,
status: 404,
statusText: "Not Found",
});

const index = await opperAIIndexes.get("NonExistentIndex");

expect(index).toBeNull();
expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledWith("https://api.opper.ai/v1/indexes/by-name/NonExistentIndex", {
method: "GET",
headers: {
"X-OPPER-API-KEY": "test-api-key",
"User-Agent": "opper-node/0.0.0",
"Content-Type": "application/json",
},
});
});

it("should throw an error for non-404 errors", async () => {
(global.fetch as jest.Mock).mockResolvedValueOnce({
ok: false,
status: 500,
statusText: "Internal Server Error",
});

await expect(opperAIIndexes.get("Test Index 1")).rejects.toThrow(
"500 Failed to fetch request https://api.opper.ai/v1/indexes/by-name/Test%20Index%201: Internal Server Error"
);
});
});

describe("deleteByName", () => {
it("should delete an index by name", async () => {
const mockIndex = mockIndexes[0];
// Mock the get request
(global.fetch as jest.Mock).mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve(mockIndex),
});
// Mock the delete request
(global.fetch as jest.Mock).mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve(true),
});

const result = await opperAIIndexes.deleteByName("Test Index 1");

expect(result).toBe(true);
expect(fetch).toHaveBeenCalledTimes(2);
// Verify get request
expect(fetch).toHaveBeenNthCalledWith(1, "https://api.opper.ai/v1/indexes/by-name/Test%20Index%201", {
method: "GET",
headers: {
"X-OPPER-API-KEY": "test-api-key",
"User-Agent": "opper-node/0.0.0",
"Content-Type": "application/json",
},
});
// Verify delete request
expect(fetch).toHaveBeenNthCalledWith(2, "https://api.opper.ai/v1/indexes/1", {
method: "DELETE",
headers: {
"X-OPPER-API-KEY": "test-api-key",
"User-Agent": "opper-node/0.0.0",
"Content-Type": "application/json",
},
});
});

it("should return false when index is not found", async () => {
(global.fetch as jest.Mock).mockResolvedValueOnce({
ok: false,
status: 404,
statusText: "Not Found",
});

const result = await opperAIIndexes.deleteByName("NonExistentIndex");

expect(result).toBe(false);
expect(fetch).toHaveBeenCalledTimes(1);
});
});
});

describe("OpperAIIndex", () => {
Expand Down
42 changes: 31 additions & 11 deletions src/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from "node:fs";

import { OpperIndexDocument, OpperIndexQuery, OpperIndex, APIClientContext } from "./types";

import { OpperError } from "./errors";
import { OpperError, APIError } from "./errors";
import APIResource from "./api-resource";
import { URLBuilder, BASE_PATHS } from "./utils";

Expand Down Expand Up @@ -144,6 +144,9 @@ class Indexes extends APIResource {
protected calcURLIndexByUUID = (uuid: string) => {
return `${this.baseURL}/v1/indexes/${uuid}`;
};
protected calcURLIndexByName = (name: string) => {
return `${this.baseURL}/v1/indexes/by-name/${encodeURIComponent(name)}`;
};

constructor({ baseURL, apiKey, isUsingAuthorization }: APIClientContext) {
super({ baseURL, apiKey, isUsingAuthorization });
Expand Down Expand Up @@ -180,30 +183,47 @@ class Indexes extends APIResource {
}

/**
* Deletes an index
* Deletes an index by UUID
* @param uuid The uuid of the index to delete.
* @returns A promise that resolves to a boolean indicating success.
*/
public async delete(uuid: string): Promise<boolean> {
const url = this.calcURLIndexByUUID(uuid);
const deleted = await this.doDelete<boolean>(url);

return deleted;
}

/**
* Deletes an index by name
* @param name The name of the index to delete.
* @returns A promise that resolves to a boolean indicating success, or false if index not found.
* @throws {APIError} If there's an API error other than 404 Not Found
*/
public async deleteByName(name: string): Promise<boolean> {
const index = await this.get(name);
if (!index) {
return false;
}
return this.delete(index.uuid);
}

/**
* Retrieves an index by name.
* @param name The name of the index to retrieve.
* @returns A promise that resolves to the index.
* @returns A promise that resolves to the index or null if not found.
* @throws {APIError} If there's an API error other than 404 Not Found
*/
public async get(name: string) {
const list = await this.list();
const index = list.find((index) => index.name === name);

if (!index) {
return null;
public async get(name: string): Promise<Index | null> {
try {
const url = this.calcURLIndexByName(name);
const index = await this.doGet<OpperIndex>(url);
return new Index(index, this);
} catch (error: unknown) {
if (error instanceof APIError && error.status === 404) {
return null;
}
throw error;
}
return new Index(index, this);
}
}

Expand Down