import {Routes, route} from '@mionkit/router';
import {memoryStoreService as db} from './full-example.app.ts';
export const routes = {
sayHello: route((ctx, name1: string, name2: string): string => {
return `Hello ${name1} and ${name2}.`;
}),
getSomeData: route(async (ctx, id: string): Data | RpcError<'data-not-found'> => {
const data = await db.getData(id);
return data || new RpcError({publicMessage: 'Data not found', type: 'data-not-found'});
}),
} satisfies Routes;
MiddleFns
Execution Chain
mion uses @mionkit/run-types for automatic validation and serialization. When TypeScript is compiled, type metadata is extracted and used at runtime — no schemas to define or maintain.
What happens automatically:
import {Routes, route} from '@mionkit/router';
import {memoryStoreService} from './full-example.app.ts';
// Your TypeScript types ARE the validation schema
interface User {
id: string;
email: string;
age: number;
birthDate: Date;
tags: Set<string>;
}
type NewUser = Omit<User, 'id'>;
// mion automatically:
// 1. Restores Date and Set from JSON
// 2. Validates user parameter
const routes = {
createUser: route((ctx, user: NewUser): User => {
// user is already validated and types are restored
console.log(user.birthDate instanceof Date); // true
console.log(user.tags instanceof Set); // true
return memoryStoreService.createUser(user);
}),
} satisfies Routes;
Some advantages of an RPC architecture:
For more detailed insights into different API types and their pros and cons, check out Nate Barbettini's presentation (API Throw-down: RPC vs REST vs GraphQL)