Skip to content

Instantly share code, notes, and snippets.

@Fredx87
Created January 6, 2024 22:23
Show Gist options
  • Save Fredx87/68b2d86caa309d96f27884a4ae9ff71f to your computer and use it in GitHub Desktop.
Save Fredx87/68b2d86caa309d96f27884a4ae9ff71f to your computer and use it in GitHub Desktop.
@effect/platform @effect/schema Cloudflare Worker example
import * as Http from "@effect/platform/HttpServer";
import { Effect } from "effect";
import * as S from "@effect/schema/Schema";
const HttpLive = Http.router.empty.pipe(
Http.router.get("/", Http.response.text("Hello World")),
Http.router.get(
"/todo/:id",
Effect.gen(function* ($) {
const { id } = yield* $(
Http.router.schemaPathParams(S.struct({ id: S.NumberFromString })),
);
return yield* $(Http.response.text(`Todo ${id}`));
}),
),
Http.router.all("*", Http.response.empty({ status: 404 })),
Http.router.catchAll((e) => {
console.log(e);
return Http.response.empty({ status: 400 });
}),
Http.app.toWebHandler,
);
export default {
async fetch(request: Request) {
return await HttpLive(request);
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment