Introduction

Manual Install

How to manually configure a mion app.

Server Side

Install the router and Configure tsConfig.json to enable runTypes metadata.

npm i @mionjs/core @mionjs/router
npm i -D @mionjs/devtools
tsConfig.json
{
  "compilerOptions": {
    // typescript compiler options ...
  },
  // enables runtime type metadata for mion's validation and serialization
  "reflection": true
}

Install dependencies depending on where you running mion.
For local development use Node or Bun.

npm install @mionjs/platform-node

Write your first mion API.

import {HeadersSubset, RpcError} from '@mionjs/core';
import {RouterOptions, initMionRouter, headersFn, middleFn, route} from '@mionjs/router';

export type User = {id: string; name: string; surname: string};

// set options and init router
export const routerOptions: Partial<RouterOptions> = {prefix: 'api/v1'};
export const myApi = await initMionRouter(
    // all function parameters will be automatically validated before the function is called
    {
        auth: headersFn((ctx, h: HeadersSubset<'Authorization'>): void | RpcError<'not-authorized'> => {
            const token = h.headers.Authorization;
            if (!token) return new RpcError<'not-authorized'>({publicMessage: 'Not Authorized', type: 'not-authorized'});
        }),
        users: {
            sayHello: route((ctx, user: User): string => `Hello ${user.name} ${user.surname}`),
        },
        log: middleFn((ctx): void => console.log(Date.now(), ctx.path, ctx.response.statusCode), {runOnError: true}),
    },
    routerOptions
);

// Export the type of the Api (used by the client)
export type MyApi = typeof myApi;

Write a start script depending on where you running mion.

import {NodeHttpOptions, startNodeServer} from '@mionjs/platform-node';
import './myApi.routes';

// init a http server with options specific for node
const nodeOptions: Partial<NodeHttpOptions> = {port: 3000};
startNodeServer(nodeOptions);



Client Side

npm install @mionjs/client

Make sure to only import the types of our mion API, this way no code is actually imported.

import {initClient} from '@mionjs/client';
// importing only the RemoteApi type from server
import type {MyApi} from './myApi.routes.ts';

const john = {id: '123', name: 'John', surname: 'Doe'};
const {routes, middleFns} = initClient<MyApi>({baseURL: 'http://localhost:3000'});

// prefills auth token for any future requests, value is stored in localStorage by default
await middleFns.auth({headers: {Authorization: 'myToken-XYZ'}}).prefill();

// calls sayHello route in the server
const [hello] = await routes.users.sayHello(john).call();
console.log(hello); // Hello John Doe

// validate parameters locally without calling the server (await still required as typeErrors is async)
const [_, error] = await routes.users.sayHello(john).typeErrors();
console.log(error); // {hasErrors: false, totalErrors: 0, errors: []}
The mion Vite plugin with AOT compilation can pre-compile all server routes and JIT functions, enabling deployment to secure environments and faster startup times.