A type-safe API endpoint framework with file-based routing and multi-runtime support.
@asgard/heimdall
is a modern TypeScript framework for building type-safe API endpoints with a focus on developer experience and cross-platform compatibility. Named after Heimdall, the Norse god who guards the rainbow bridge to Asgard, this package creates a bridge between your file structure and various cloud platforms.
@asgard/hermod
for service discovery# Using npm npm install @asgard/heimdall # Using pnpm pnpm add @asgard/heimdall # Using yarn yarn add @asgard/heimdall # Using JSR npx jsr add @asgard/heimdall
import { HeimdallEndpoint } from '@asgard/heimdall'; import { z } from 'zod'; import { UserService, LoggerService } from '../services'; // Define your endpoint const getUserEndpoint = new HeimdallEndpoint({ path: '/users/:id', method: 'GET', // Define parameter validation with Zod params: z.object({ id: z.string() }), // Define response schema with Zod response: z.object({ id: z.string(), name: z.string(), email: z.string().email() }), // Inject services from @asgard/hermod services: [UserService, LoggerService], // Implement the handler handler: async ({ params, services }) => { const { userService, logger } = services; logger.info(`Fetching user with id ${params.id}`); const user = await userService.findById(params.id); if (!user) { return { statusCode: 404, body: { message: `User with id ${params.id} not found` } }; } return { statusCode: 200, body: user }; } }); // Register with your Heimdall server server.registerEndpoint(getUserEndpoint);
Endpoints are the building blocks of your API. Each endpoint combines:
@asgard/heimdall
integrates with @asgard/hermod
for service discovery and dependency injection:
// Define the services you need const endpoint = new HeimdallEndpoint({ // ... services: [DatabaseService, LoggerService, AuthService], handler: async ({ services }) => { // Services are available and fully typed const { databaseService, loggerService, authService } = services; // Use your services const result = await databaseService.query('...'); return { statusCode: 200, body: result }; } });
@asgard/heimdall
supports multiple validation libraries through StandardSchema specification:
import { z } from 'zod'; const createUserEndpoint = new HeimdallEndpoint({ path: '/users', method: 'POST', // Use Zod for validation body: z.object({ name: z.string(), email: z.string().email(), age: z.number().min(18), roles: z.array(z.string()) }), // ...handler implementation });
import { type } from 'arktype'; const createUserEndpoint = new HeimdallEndpoint({ path: '/users', method: 'POST', // Use Arktype for validation body: type({ name: 'string', email: 'string:email', age: 'number>=18', roles: 'string[]' }), // ...handler implementation });
@asgard/heimdall
provides first-class support for AWS Lambda and API Gateway v2:
import { HeimdallEndpoint } from '@asgard/heimdall'; import { HeimdallAWSAPIGatewayV2Handler } from '@asgard/heimdall/aws'; import { type } from 'arktype'; import { UserService } from '../services'; // Define your endpoint const getUserEndpoint = new HeimdallEndpoint({ path: '/users/:id', method: 'GET', // Use Arktype for validation params: type({ id: 'string' }), response: type({ id: 'string', name: 'string', email: 'string:email' }), services: [UserService], handler: async ({ params, services }) => { const { userService } = services; const user = await userService.findById(params.id); if (!user) { return { statusCode: 404, body: { message: 'User not found' } }; } return { statusCode: 200, body: user }; } }); // Create Lambda handler const lambdaHandler = new HeimdallAWSAPIGatewayV2Handler(getUserEndpoint); // Export the handler function for AWS Lambda export const handler = lambdaHandler.handler;
The AWS adapter:
The main class for defining endpoints.
new HeimdallEndpoint({ path: string; // The endpoint path (e.g., '/users/:id') method: HttpMethod; // 'GET', 'POST', 'PUT', 'DELETE', or 'PATCH' body?: Schema; // Request body validation schema response?: Schema; // Response body schema search?: Schema; // Query parameters schema params?: Schema; // Path parameters schema services: Service[]; // Array of service constructors handler: Function; // Handler implementation })
MIT © Lebogang Mabala
Add Package
deno add jsr:@asgard/heimdall
Import symbol
import * as heimdall from "@asgard/heimdall";
Import directly with a jsr specifier
import * as heimdall from "jsr:@asgard/heimdall";
Add Package
pnpm i jsr:@asgard/heimdall
pnpm dlx jsr add @asgard/heimdall
Import symbol
import * as heimdall from "@asgard/heimdall";
Add Package
yarn add jsr:@asgard/heimdall
yarn dlx jsr add @asgard/heimdall
Import symbol
import * as heimdall from "@asgard/heimdall";
Add Package
npx jsr add @asgard/heimdall
Import symbol
import * as heimdall from "@asgard/heimdall";
Add Package
bunx jsr add @asgard/heimdall
Import symbol
import * as heimdall from "@asgard/heimdall";