feat: return undefined if not found
This commit is contained in:
parent
fcc12812df
commit
fcfc3cb107
3 changed files with 19 additions and 8 deletions
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "wirekvs-js",
|
"name": "wirekvs-js",
|
||||||
"version": "2.0.0",
|
"version": "3.0.0",
|
||||||
"description": "A Node.js client for the WireKVS database service",
|
"description": "A Node.js client for the WireKVS database service",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
"types": "src/index.d.ts",
|
"types": "src/index.d.ts",
|
||||||
|
|
2
src/index.d.ts
vendored
2
src/index.d.ts
vendored
|
@ -49,7 +49,7 @@ export class WireKVSDatabase extends EventEmitter {
|
||||||
disconnect(): void;
|
disconnect(): void;
|
||||||
|
|
||||||
getAllEntries(): Promise<DatabaseEntry[]>;
|
getAllEntries(): Promise<DatabaseEntry[]>;
|
||||||
get<T extends object>(key: string): Promise<T>;
|
get<T extends object>(key: string): Promise<T | undefined>;
|
||||||
set(key: string, value: any): Promise<void>;
|
set(key: string, value: any): Promise<void>;
|
||||||
update(key: string, value: any): Promise<void>;
|
update(key: string, value: any): Promise<void>;
|
||||||
delete(key: string): Promise<void>;
|
delete(key: string): Promise<void>;
|
||||||
|
|
23
src/index.js
23
src/index.js
|
@ -121,13 +121,24 @@ class WireKVSDatabase extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
async get(key) {
|
async get(key) {
|
||||||
const response = await axios.get(
|
try {
|
||||||
`${API_BASE_URL}/database/${this.id}/${key}`,
|
const response = await axios.get(
|
||||||
{
|
`${API_BASE_URL}/database/${this.id}/${key}`,
|
||||||
headers: { Authorization: this.accessKey },
|
{
|
||||||
|
headers: { Authorization: this.accessKey },
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return response.data;
|
||||||
|
} catch (error) {
|
||||||
|
if (
|
||||||
|
axios.isAxiosError(error) &&
|
||||||
|
(error.response?.status === 404 ||
|
||||||
|
error.response?.data?.message === "Key not found")
|
||||||
|
) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
);
|
throw error;
|
||||||
return response.data;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async json(key) {
|
async json(key) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue