From 9b96013acf18bcca78168cf6c7aa2d87cac14cf2 Mon Sep 17 00:00:00 2001 From: X Date: Wed, 10 May 2023 19:39:39 +0800 Subject: [PATCH] Docs --- packages/esm-worker/README.md | 32 ++++++++++++++++++++++++++-- packages/esm-worker/types/index.d.ts | 8 +++---- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/packages/esm-worker/README.md b/packages/esm-worker/README.md index a5504b725..eecc2e3c7 100644 --- a/packages/esm-worker/README.md +++ b/packages/esm-worker/README.md @@ -10,7 +10,7 @@ the edge. ## Getting Started ```bash -npm install esm-worker +npm install esm-worker@0.120 ``` You need to add following configuration to your `wrangler.toml`: @@ -49,12 +49,40 @@ Then, wrap your worker with the `esm-worker` package: ```js import worker from "esm-worker"; +// extend the `Env` interface +declare global { + interface Env { + // your other vars in `wrangler.toml`... + } +} + export default worker((req, ctx) => { - if (ctx.url.pathname === "/") { + const { env, isDev, url } = ctx; + if (url.pathname === "/") { // custom the homepage return new Response("

Welcome to use esm.sh!

", { headers: { "content-type": "text/html" }, }); + + // using cache + return ctx.withCache(() => + new Response("Boom!", { + headers: { "cache-control": "public; max-age=600" }, + }) + ); + } + + // using KV + await env.KV.put("key", "value"); + const value = await env.KV.get("key"); + + // using R2 + await env.R2.put("key", "value"); + const r2obj = await env.R2.get("key"); + + if (isDev) { + // local development + // your code... } }); ``` diff --git a/packages/esm-worker/types/index.d.ts b/packages/esm-worker/types/index.d.ts index 69e506419..308c0ac95 100644 --- a/packages/esm-worker/types/index.d.ts +++ b/packages/esm-worker/types/index.d.ts @@ -21,13 +21,13 @@ export default function ( ): ExportedHandlerFetchHandler; export type Context> = { - waitUntil(promise: Promise): void; - withCache(fetch: () => Promise | Response): Promise; cache: Cache; - url: URL; - env: Env; data: Data; + env: Env; isDev: boolean; + url: URL; + waitUntil(promise: Promise): void; + withCache(fetch: () => Promise | Response): Promise; }; export interface Middleware {