Skip to content

Commit

Permalink
feat: fetch entry data from static assets
Browse files Browse the repository at this point in the history
Note that this doesn't work in vite dev, but workaround is yet to be
found.
  • Loading branch information
yudai-nkt committed Oct 28, 2023
1 parent 7d6eac7 commit 7e6ec61
Showing 1 changed file with 7 additions and 22 deletions.
29 changes: 7 additions & 22 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { type Context } from "hono";
import { getContentFromKVAsset } from "hono/utils/cloudflare";
import { $array, $object, $string, type Validator } from "lizod";
// @ts-expect-error
import manifestJSON from "__STATIC_CONTENT_MANIFEST";

import { type Entry } from "./components/Entries";

Expand Down Expand Up @@ -36,30 +33,18 @@ export const categories = [
},
];

const parseJSONFromKVAsset = async (
path: string,
context: Context<{ Bindings: Env }>
) => {
const asset = await getContentFromKVAsset(path, {
manifest: manifestJSON,
namespace: context.env.__STATIC_CONTENT,
});

// Only known keys under the developers' control are passed to this function,
// so use of non-nullish assertion would be justified for the time being.
return JSON.parse(new TextDecoder().decode(asset!));
};

export const validateEntries = async <T>(
category: string,
context: Context<{ Bindings: Env }>,
context: Context,
// TODO: make subScheme optional w/o compile error
subSchema: Validator<T>
): Promise<Entry<T>[]> => {
const entries = await parseJSONFromKVAsset(
`static/data/${category}.json`,
context
);
const env = context.env as { ASSETS: { fetch: typeof fetch } };
const entries = await env.ASSETS.fetch(
// Path has to be absolute here.
// cf. https://github.com/cloudflare/workers-sdk/issues/165#issuecomment-1290538864
new URL(`/assets/data/${category}.json`, context.req.url)
).then((r) => r.json());

const schema = $array(
$object({
Expand Down

0 comments on commit 7e6ec61

Please sign in to comment.