Created
January 6, 2024 22:23
-
-
Save Fredx87/68b2d86caa309d96f27884a4ae9ff71f to your computer and use it in GitHub Desktop.
@effect/platform @effect/schema Cloudflare Worker example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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