Skip to content

Commit

Permalink
fix(http, server): handle missing resources with http 404 (#367)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <pooya@pi0.io>
  • Loading branch information
Aareksio and pi0 authored Jan 12, 2024
1 parent fc9f6a9 commit 78e1568
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/drivers/http.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineDriver } from "./utils";
import { $fetch as _fetch } from "ofetch";
import { type FetchError, $fetch as _fetch } from "ofetch";
import { joinURL } from "ufo";

export interface HTTPOptions {
Expand All @@ -15,6 +15,13 @@ export default defineDriver((opts: HTTPOptions) => {
const rBase = (key: string = "") =>
joinURL(opts.base!, (key || "/").replace(/:/g, "/"), ":");

const catchFetchError = (error: FetchError, fallbackVal: any = null) => {
if (error?.response?.status === 404) {
return fallbackVal;
}
throw error;
};

return {
name: DRIVER_NAME,
options: opts,
Expand All @@ -24,12 +31,12 @@ export default defineDriver((opts: HTTPOptions) => {
headers: { ...opts.headers, ...topts.headers },
})
.then(() => true)
.catch(() => false);
.catch((err) => catchFetchError(err, false));
},
async getItem(key, tops = {}) {
const value = await _fetch(r(key), {
headers: { ...opts.headers, ...tops.headers },
});
}).catch(catchFetchError);
return value;
},
async getItemRaw(key, topts) {
Expand All @@ -39,7 +46,7 @@ export default defineDriver((opts: HTTPOptions) => {
...opts.headers,
...topts.headers,
},
});
}).catch(catchFetchError);
return value;
},
async getMeta(key, topts) {
Expand Down
12 changes: 12 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,23 @@ export function createH3StorageHandler(

const isRaw =
getRequestHeader(event, "accept") === "application/octet-stream";

const checkNotFound = (value: any) => {
if (value === null) {
throw createError({
statusMessage: "KV value not found",
statusCode: 404,
});
}
};

if (isRaw) {
const value = await storage.getItemRaw(key);
checkNotFound(value);
return value;
} else {
const value = await storage.getItem(key);
checkNotFound(value);
return stringify(value);
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ describe("server", () => {
expect(await fetchStorage("foo/bar", { method: "DELETE" })).toBe("OK");
expect(await fetchStorage("foo/bar/", {})).toMatchObject([]);

await expect(
fetchStorage("/non", { method: "GET" }).catch((error) => {
throw error.data;
})
).rejects.toMatchObject({
statusCode: 404,
statusMessage: "KV value not found",
});

await expect(
fetchStorage("private/foo/bar", { method: "GET" }).catch((error) => {
throw error.data;
Expand Down

0 comments on commit 78e1568

Please sign in to comment.