Install the router and Configure tsConfig.json to enable runTypes metadata.
npm i @mionkit/router
{
"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
npm install @mionkit/bun
npm install @mionkit/aws
npm install @mionkit/gcloud
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);
import {BunHttpOptions, startBunServer} from '@mionkit/bun';
import './myApi.routes';
// init a bun server with options specific for bun
const bunOptions: Partial<BunHttpOptions> = {port: 3000};
startBunServer(bunOptions);
import {AwsLambdaOptions, awsLambdaHandler, setAwsLambdaOpts} from '@mionkit/aws';
import './myApi.routes';
// set options specific for aws lambda
const awsOptions: Partial<AwsLambdaOptions> = {};
setAwsLambdaOpts(awsOptions);
// export AWS Lambda Handler
export const handler = awsLambdaHandler;
import {GoogleCFOptions, googleCFHandler, setGoogleCFOpts} from '@mionkit/gcloud';
import './myApi.routes';
// set options specific for GC Cloud Functions
const gcfOptions: Partial<GoogleCFOptions> = {};
setGoogleCFOpts(gcfOptions);
// export Google Cloud Functions Handler
export const handler = googleCFHandler;
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();