@thomasfosterau/effect-sveltekit
v0.2.1
Published
Effect integration for SvelteKit: load/action/endpoint wrappers, remote functions, an effectKit() Vite plugin, and an Effect HTTP deployment adapter
Maintainers
Readme
@thomasfosterau/effect-sveltekit
Effect v4-native integration for SvelteKit: write load functions, form
actions, endpoints, hooks, and remote functions as Effects; move custom types
across the server/client boundary with Effect Schema; and deploy on an Effect
HTTP server.
Part of the effect-svelte
monorepo. Depends on
@thomasfosterau/effect-svelte
for the Svelte runes hooks.
Installation
npm install @thomasfosterau/effect-sveltekit effect@beta svelte @sveltejs/kit
# the Effect deployment adapter additionally needs:
npm install @effect/platform-node@betaEntry points
| Import | What it provides |
| ---------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| @thomasfosterau/effect-sveltekit | universal helpers (load, the Remote call namespace, transport, sveltekitConfig, reroute) |
| …/server | server wrappers (serverLoad, action, actions, handle, endpoint), app runtimes (makeServerRuntime / makeUniversalRuntime), the per-request runtime seam (makeRuntimeAdapter / RequestRuntime), RemoteFunction |
| …/client | browser-only Navigation / State / Remote namespaces and useRemoteQuery |
| …/vite | the effectKit() Vite plugin (auto-wrap bare-Effect route exports) |
| …/adapter | the Effect HTTP deployment adapter |
Example
// vite.config.ts — return Effects directly from route files
import { sveltekit } from "@sveltejs/kit/vite";
import { effectKit } from "@thomasfosterau/effect-sveltekit/vite";
export default { plugins: [effectKit(), sveltekit()] };// +page.server.ts
import { Effect } from "effect";
export const load = (event) => Effect.succeed({ id: event.params.id });What's included
- Effects as route exports — explicit wrappers, app-wide runtimes (services
built once per process), or the
effectKit()plugin. - Bring-your-own per-request runtime —
makeRuntimeAdapter(provider, opts)binds the server wrappers to aRequestRuntimeprovider, so the handler effect's services are your own application catalogue (notSvelteKitServices). Adopt a runtime the consumer stashed onevent.locals(RequestRuntime.fromLocals, resolved per call so a mid-request rebuild is honoured), let the package build + dispose one per request (RequestRuntime.fromLayerPerRequest, withafterResponseto route disposal through Cloudflare Workers'ctx.waitUntil), or reuse the build-once model (fromManagedRuntime/fromLayer).RemoteFunction.withRuntime(provider)gives remote functions the same seam. - Pluggable boundary error translation — pass
translateErrortomakeRuntimeAdapter,makeServerRuntime, orRemoteFunction.withRuntimeto map a failedCauseto SvelteKit control flow (error(404),redirect(), remote-forminvalid()) instead of a generic 500. - Remote functions — define with
RemoteFunction.*(a bare EffectSchemavalidator is accepted and auto-wrapped), consume via theRemotenamespace (Remote.callQuery,Remote.liveQueryStream, …);useRemoteQuerybridges a reactive remote resource to anAsyncResult.RemoteFunction.withRuntimealso takes aformErrorhook to map a domain error into SvelteKit field issues. transport⇄ Effect Schema (Hooks.transport/transporter, plusHooks.markerTransporterfor non-round-tripping wrappers) and$env⇄ EffectConfig.- Client navigation/state — the
Navigation(goto/invalidate/… plusbeforeNavigate/afterNavigate/onNavigatestreams) andState(page) namespaces. - Effect deployment adapter — the generated app is a platform-neutral
Effect
HttpApp(makeHandlerWith): serve it as a production Node HTTP server (effect/unstable/http+@effect/platform-node, the default) or as a Webfetchhandler on Cloudflare Workers (effectAdapter({ target: "fetch" })/toWebHandler). Asset serving is a pluggableAssetSource(AssetSource.fileSystemfor Node,AssetSource.cloudflareAssets(env.ASSETS)for Workers, or any custom source — R2, KV, a CDN passthrough).
See the monorepo README for the full API and a runnable SvelteKit example.
License
MIT
