Introduction

Quick Start

How to get started with mion full stack APIs.

Server Side

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

npm i @mionkit/router
tsConfig.json
{
  "compilerOptions": {
    // typescript compiler options ...
  },
  // custom flag required by @deepkit/type-compiler to emit runtime type metadata
  "reflection": true
}

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

npm install @mionkit/node

Write your first mion API.

import {HeadersSubset, RpcError} from '@mionkit/core';
import {RouterOptions, initMionRouter, headersFn, middleFn, route} from '@mionkit/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 '@mionkit/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 @mionkit/client

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

import {initClient} from '@mionkit/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'});

async function example() {
    // 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: []}
}

example();
The mion Vite plugin with AOT compilation can pre-compile all routes and JIT functions, enabling deployment to secure environments and faster startup times.