Skip to content

Commit

Permalink
ensure trailing or leading slashes don't mess server url
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchalef committed Aug 26, 2023
1 parent 37cc8d3 commit d5f6c64
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 70 deletions.
31 changes: 14 additions & 17 deletions src/document_collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
isGetIDocument,
} from "./document_models";
import { ISearchQuery, IUpdateDocumentParams, IZepClient } from "./interfaces";
import { API_BASEURL, handleRequest, isFloat } from "./utils";
import { handleRequest, isFloat } from "./utils";
import { APIError } from "./errors";

const MIN_DOCS_TO_INDEX = 10_000;
Expand Down Expand Up @@ -64,8 +64,8 @@ export default class DocumentCollection extends DocumentCollectionModel {
if (documents.length > LARGE_BATCH_WARNING_LIMIT) {
console.warn(LARGE_BATCH_WARNING);
}
const url = this.getFullUrl(`/collection/${this.name}/document`);
const body = JSON.stringify(docsWithFloatArrayToDocs(documents));
const url = this.client.getFullUrl(`/collection/${this.name}/document`);
const response = await handleRequest(
fetch(url, {
method: "POST",
Expand Down Expand Up @@ -99,7 +99,9 @@ export default class DocumentCollection extends DocumentCollectionModel {
if (!uuid) {
throw new Error("Document must have a uuid");
}
const url = this.getFullUrl(`/collection/${this.name}/document/${uuid}`);
const url = this.client.getFullUrl(
`/collection/${this.name}/document/${uuid}`
);
await handleRequest(
fetch(url, {
method: "PATCH",
Expand Down Expand Up @@ -131,7 +133,7 @@ export default class DocumentCollection extends DocumentCollectionModel {
if (uuid.length === 0) {
throw new Error("Document must have a uuid");
}
const url = this.getFullUrl(
const url = this.client.getFullUrl(
`/collection/${this.name}/document/uuid/${uuid}`
);
await handleRequest(
Expand All @@ -157,7 +159,9 @@ export default class DocumentCollection extends DocumentCollectionModel {
if (uuid.length === 0) {
throw new Error("Document must have a uuid");
}
const url = this.getFullUrl(`/collection/${this.name}/document/${uuid}`);
const url = this.client.getFullUrl(
`/collection/${this.name}/document/${uuid}`
);
const response = await handleRequest(
fetch(url, {
headers: this.client.headers,
Expand Down Expand Up @@ -196,7 +200,9 @@ export default class DocumentCollection extends DocumentCollectionModel {
console.warn(LARGE_BATCH_WARNING);
}

const url = this.getFullUrl(`/collection/${this.name}/document/list/get`);
const url = this.client.getFullUrl(
`/collection/${this.name}/document/list/get`
);
const response = await handleRequest(
fetch(url, {
method: "POST",
Expand Down Expand Up @@ -252,7 +258,7 @@ export default class DocumentCollection extends DocumentCollectionModel {
: query;

const limitParam = limit ? `?limit=${limit}` : "";
const url = this.getFullUrl(
const url = this.client.getFullUrl(
`/collection/${this.name}/search${limitParam}`
);
const response = await handleRequest(
Expand Down Expand Up @@ -325,7 +331,7 @@ export default class DocumentCollection extends DocumentCollectionModel {
`Collection must have at least ${MIN_DOCS_TO_INDEX} documents to index. Use force=true to override.`
);
}
const url = this.getFullUrl(
const url = this.client.getFullUrl(
`/collection/${this.name}/index/create${forceParam}`
);
await handleRequest(
Expand All @@ -338,13 +344,4 @@ export default class DocumentCollection extends DocumentCollectionModel {
})
);
}

/**
* Constructs the full URL for an API endpoint.
* @param {string} endpoint - The endpoint of the API.
* @returns {string} The full URL.
*/
getFullUrl(endpoint: string): string {
return `${this.client.baseURL}${API_BASEURL}${endpoint}`;
}
}
21 changes: 6 additions & 15 deletions src/document_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
IUpdateCollectionParams,
IZepClient,
} from "./interfaces";
import { API_BASEURL, handleRequest } from "./utils";
import { handleRequest } from "./utils";
import DocumentCollection from "./document_collection";

/**
Expand All @@ -22,15 +22,6 @@ export default class DocumentManager {
this.client = client;
}

/**
* Constructs the full URL for an API endpoint.
* @param {string} endpoint - The endpoint of the API.
* @returns {string} The full URL.
*/
getFullUrl(endpoint: string): string {
return `${this.client.baseURL}${API_BASEURL}${endpoint}`;
}

/**
* Adds a new collection to the Zep client.
* @param {IAddCollectionParams} params - The parameters for the new collection.
Expand Down Expand Up @@ -59,7 +50,7 @@ export default class DocumentManager {
});

await handleRequest(
fetch(this.getFullUrl(`/collection/${name}`), {
fetch(this.client.getFullUrl(`/collection/${name}`), {
method: "POST",
headers: {
...this.client.headers,
Expand Down Expand Up @@ -87,7 +78,7 @@ export default class DocumentManager {
}

const response = await handleRequest(
fetch(this.getFullUrl(`/collection/${name}`), {
fetch(this.client.getFullUrl(`/collection/${name}`), {
headers: this.client.headers,
})
);
Expand Down Expand Up @@ -134,7 +125,7 @@ export default class DocumentManager {
});

await handleRequest(
fetch(this.getFullUrl(`/collection/${collection.name}`), {
fetch(this.client.getFullUrl(`/collection/${collection.name}`), {
method: "PATCH",
headers: {
...this.client.headers,
Expand All @@ -155,7 +146,7 @@ export default class DocumentManager {
*/
async listCollections(): Promise<DocumentCollection[]> {
const response = await handleRequest(
fetch(this.getFullUrl("/collection"), {
fetch(this.client.getFullUrl("/collection"), {
headers: this.client.headers,
})
);
Expand Down Expand Up @@ -195,7 +186,7 @@ export default class DocumentManager {
}

await handleRequest(
fetch(this.getFullUrl(`/collection/${collectionName}`), {
fetch(this.client.getFullUrl(`/collection/${collectionName}`), {
method: "DELETE",
headers: this.client.headers,
})
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export interface IZepClient {
baseURL: string;

headers: any;

getFullUrl(endpoint: string): string;
}

export interface IAddCollectionParams {
Expand Down
25 changes: 8 additions & 17 deletions src/memory_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Session,
} from "./memory_models";
import { IZepClient } from "./interfaces";
import { API_BASEURL, handleRequest } from "./utils";
import { handleRequest } from "./utils";

export default class MemoryManager {
client: IZepClient;
Expand All @@ -15,15 +15,6 @@ export default class MemoryManager {
this.client = client;
}

/**
* Constructs the full URL for an API endpoint.
* @param {string} endpoint - The endpoint of the API.
* @returns {string} The full URL.
*/
getFullUrl(endpoint: string): string {
return `${this.client.baseURL}${API_BASEURL}${endpoint}`;
}

/**
* Retrieves a session with the specified ID.
*
Expand All @@ -39,7 +30,7 @@ export default class MemoryManager {
}

const response = await handleRequest(
fetch(this.getFullUrl(`/sessions/${sessionId}`), {
fetch(this.client.getFullUrl(`/sessions/${sessionId}`), {
headers: this.client.headers,
}),
`No session found for session ${sessionId}`
Expand Down Expand Up @@ -69,7 +60,7 @@ export default class MemoryManager {
}

const response = await handleRequest(
fetch(this.getFullUrl(`/sessions`), {
fetch(this.client.getFullUrl(`/sessions`), {
method: "POST",
headers: {
...this.client.headers,
Expand Down Expand Up @@ -105,7 +96,7 @@ export default class MemoryManager {
}

const response = await handleRequest(
fetch(this.getFullUrl(`/sessions/${session.session_id}`), {
fetch(this.client.getFullUrl(`/sessions/${session.session_id}`), {
method: "PATCH",
headers: {
...this.client.headers,
Expand All @@ -130,7 +121,7 @@ export default class MemoryManager {
* @throws {NotFoundError} - If the session is not found.
*/
async getMemory(sessionID: string, lastn?: number): Promise<Memory | null> {
const url = this.getFullUrl(`/sessions/${sessionID}/memory`);
const url = this.client.getFullUrl(`/sessions/${sessionID}/memory`);
const params = lastn !== undefined ? `?lastn=${lastn}` : "";

const response: Response = await handleRequest(
Expand Down Expand Up @@ -160,7 +151,7 @@ export default class MemoryManager {
* @throws {APIError} If the request fails.
*/
async addMemory(sessionID: string, memory: Memory): Promise<string> {
const url = this.getFullUrl(`/sessions/${sessionID}/memory`);
const url = this.client.getFullUrl(`/sessions/${sessionID}/memory`);

const response: Response = await handleRequest(
fetch(url, {
Expand All @@ -187,7 +178,7 @@ export default class MemoryManager {
* @throws {NotFoundError} - If the session is not found.
*/
async deleteMemory(sessionID: string): Promise<string> {
const url = this.getFullUrl(`/sessions/${sessionID}/memory`);
const url = this.client.getFullUrl(`/sessions/${sessionID}/memory`);

const response: Response = await handleRequest(
fetch(url, {
Expand Down Expand Up @@ -215,7 +206,7 @@ export default class MemoryManager {
searchPayload: MemorySearchPayload,
limit?: number
): Promise<Array<MemorySearchResult>> {
const url = this.getFullUrl(`/sessions/${sessionID}/search`);
const url = this.client.getFullUrl(`/sessions/${sessionID}/search`);
const params = limit !== undefined ? `?limit=${limit}` : "";

const response: Response = await handleRequest(
Expand Down
5 changes: 5 additions & 0 deletions src/tests/collection_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
IDocumentCollectionModel,
} from "../document_models";
import { IZepClient } from "../interfaces";
import { API_BASEPATH } from "../utils";

const API_URL = "http://localhost:8000";

Expand Down Expand Up @@ -38,6 +39,10 @@ const mockCollection: IDocumentCollectionModel = {
const mockClient: IZepClient = {
baseURL: API_URL,
headers: {},

getFullUrl(endpoint: string): string {
return `${this.baseURL}${API_BASEPATH}${endpoint}`;
},
};

describe("DocumentCollection", () => {
Expand Down
34 changes: 30 additions & 4 deletions src/tests/utils_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ import {
warnDeprecation,
} from "../utils";
import { FetchMock } from "jest-fetch-mock";
import ZepClient from "../zep-client";

const fetchMock = global.fetch as FetchMock;

describe("Utility functions", () => {
let client: ZepClient;

beforeEach(() => {
client = new ZepClient("http://localhost:3000");
});

test("warnDeprecation", () => {
console.warn = jest.fn();
warnDeprecation("testFunction");
Expand All @@ -25,10 +32,6 @@ describe("Utility functions", () => {
expect(isVersionGreaterOrEqual(null)).toBe(false);
});

test("returns true if version is greater than minimum", () => {
expect(isVersionGreaterOrEqual("0.10.0")).toBe(true);
});

test("returns true if version is equal to minimum", () => {
expect(isVersionGreaterOrEqual(MINIMUM_SERVER_VERSION)).toBe(true);
});
Expand Down Expand Up @@ -102,4 +105,27 @@ describe("Utility functions", () => {
expect(isFloat(undefined)).toBe(false);
});
});

describe("getFullUrl", () => {
const expectedUrl = "http://localhost:3000/api/v1/testEndpoint";
it("should correctly construct a URL without leading or trailing slashes", () => {
const endpoint = "testEndpoint";
expect(client.getFullUrl(endpoint)).toBe(expectedUrl);
});

it("should correctly construct a URL with leading slash in endpoint", () => {
const endpoint = "/testEndpoint";
expect(client.getFullUrl(endpoint)).toBe(expectedUrl);
});

it("should correctly construct a URL with trailing slash in endpoint", () => {
const endpoint = "testEndpoint/";
expect(client.getFullUrl(endpoint)).toBe(expectedUrl);
});

it("should correctly construct a URL with both leading and trailing slashes in endpoint", () => {
const endpoint = "//testEndpoint/";
expect(client.getFullUrl(endpoint)).toBe(expectedUrl);
});
});
});
Loading

0 comments on commit d5f6c64

Please sign in to comment.