Skip to content

Commit

Permalink
Docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed May 10, 2023
1 parent 44cae10 commit 9b96013
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
32 changes: 30 additions & 2 deletions packages/esm-worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down Expand Up @@ -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("<h1>Welcome to use esm.sh!</h1>", {
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...
}
});
```
8 changes: 4 additions & 4 deletions packages/esm-worker/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ export default function (
): ExportedHandlerFetchHandler<Env, {}>;

export type Context<Data = Record<string, any>> = {
waitUntil(promise: Promise<any>): void;
withCache(fetch: () => Promise<Response> | Response): Promise<Response>;
cache: Cache;
url: URL;
env: Env;
data: Data;
env: Env;
isDev: boolean;
url: URL;
waitUntil(promise: Promise<any>): void;
withCache(fetch: () => Promise<Response> | Response): Promise<Response>;
};

export interface Middleware {
Expand Down

0 comments on commit 9b96013

Please sign in to comment.