Skip to content

Commit

Permalink
add kv list and del requests (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-btc authored Oct 2, 2023
1 parent 5a63e40 commit 22f4566
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
25 changes: 25 additions & 0 deletions examples/kv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { kv } from "../lib";

const clientOptions = {
token: "<Your_Token_Here>",
endpoint: "http://localhost:8080",
};

(async () => {
const key = "test";
const value = "value";

await kv.set(key, value, clientOptions);
const result = await kv.get(key, clientOptions);

console.log(result);
let all = await kv.all(clientOptions);

console.log(all);

await kv.del(key, clientOptions);

all = await kv.all(clientOptions);

console.log(all);
})();
4 changes: 3 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Chat } from "./chats";
import { Memory, createMemory, updateMemory, getAllMemories } from "./memory";
import { splitString, tokenCount } from "./split";
import { usage } from "./user";
import { get as KVGet, set as KVSet } from "./kv";
import { get as KVGet, set as KVSet, del as KVDel, all as KVAll } from "./kv";
import PolyfactClientBuilder from "./client";
import { generateImage } from "./image";
import { TextFileLoader, StringLoader, AudioLoader } from "./dataloader";
Expand All @@ -32,6 +32,8 @@ export * from "./helpers/models";
const kv = {
get: KVGet,
set: KVSet,
del: KVDel,
all: KVAll,
};

// Export methods
Expand Down
48 changes: 48 additions & 0 deletions lib/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,62 @@ export async function get(key: string, clientOptions: InputClientOptions = {}):
}
}

export async function all(clientOptions: InputClientOptions = {}): Promise<string[]> {
try {
const { token, endpoint } = await defaultOptions(clientOptions);

const response = await axios
.get(`${endpoint}/kvs`, {
method: "GET",
headers: {
"X-Access-Token": token,
"Content-Type": "application/json",
},
})
.catch(() => ({
data: [],
}));

return response.data;
} catch (e: unknown) {
if (e instanceof AxiosError) {
throw new ApiError(e?.response?.data as ErrorData);
}
throw e;
}
}

export async function del(key: string, clientOptions: InputClientOptions = {}): Promise<void> {
try {
const { token, endpoint } = await defaultOptions(clientOptions);

await axios.delete(`${endpoint}/kv?key=${key}`, {
method: "DELETE",
headers: {
"X-Access-Token": token,
"Content-Type": "application/json",
},
});
} catch (e: unknown) {
if (e instanceof AxiosError) {
throw new ApiError(e?.response?.data as ErrorData);
}
throw e;
}
}

export type KVClient = {
get: (key: string) => Promise<string>;
set: (key: string, value: string) => Promise<void>;
del: (key: string) => Promise<void>;
all: () => Promise<string[]>;
};

export default function client(clientOptions: InputClientOptions = {}): KVClient {
return {
get: (key: string) => get(key, clientOptions),
set: (key: string, value: string) => set(key, value, clientOptions),
del: (key: string) => del(key, clientOptions),
all: () => all(clientOptions),
};
}

0 comments on commit 22f4566

Please sign in to comment.