Shared Logic Patterns (Dependency Equivalents)¶
Verified status as of March 28, 2026. Runtime note: FastFN auto-installs function-local dependencies from
requirements.txt/package.json; host runtimes are required infastfn dev --native, whilefastfn devdepends on a running Docker daemon.
Quick View¶
- Complexity: Intermediate
- Typical time: 15-20 minutes
- Outcome: reusable request logic without framework-level dependency injection
FastFN does not use decorator-based dependency injection. The equivalent is explicit composition with helpers/modules shared across function folders.
1. First pattern: pure helper + route handler¶
Recommended neutral structure:
In each runtime, import shared logic and run it before business code.
use serde_json::{json, Value};
pub fn require_api_key(event: &Value) -> Value {
let key = event["headers"]["x-api-key"].as_str().unwrap_or("");
let expected = event["env"]["API_KEY"].as_str().unwrap_or("");
if key != expected {
return json!({"ok": false, "status": 401, "error": "unauthorized"});
}
json!({"ok": true})
}
2. Class/module style reuse¶
If your team prefers class-based encapsulation, keep it local and explicit:
- Construct a service with config from
event.env. - Call service methods from the handler.
- Keep side effects at the edge.
This maps FastAPI "classes as dependencies" to plain language-native modules.
3. Composable helper chains (sub-dependencies equivalent)¶
Compose helpers in sequence:
- Parse identity
- Authorize role/scope
- Validate payload
- Execute business logic
Short runtime-agnostic flow:
Validation¶
- Shared helpers are imported and used by at least two functions.
- Unauthorized request returns
401from helper guard path. - Validation helper returns deterministic
422errors.
Troubleshooting¶
- If helpers cannot be imported, confirm relative paths from function file.
- If behavior diverges between runtimes, keep helper output envelope (
ok,status,error) consistent. - If tests are flaky, isolate helper functions from network and clock dependencies.
Related links¶
Last reviewed:
March 28, 2026
·
Docs on fastfn.dev