Vercel
@mionjs/platform-vercel runs mion as a Vercel Function: re-export its GET, POST, PUT, DELETE, PATCH and OPTIONS handlers from one function file. OPTIONS lets a CORS preflight reach mion.
Installing the Adapter
npm install @mionjs/platform-vercel @mionjs/router
Creating the Handler
import {createVercelHandler} from '@mionjs/platform-vercel';
import './vercel-routes.ts';
export const {GET, POST, PUT, DELETE, PATCH, OPTIONS} = createVercelHandler();
import {createMionRouter, Routes} from '@mionjs/router';
// the catch-all handler is mounted under /api, so the router gets that prefix
const mion = createMionRouter({basePath: '/api'});
const routes = {
sayHello: mion.route((ctx, name: string): string => {
return `Hello ${name}!`;
}),
} satisfies Routes;
// the server entry only imports this file
export const myApi = mion.initRoutes(routes);
export type MyApi = typeof myApi;
One Function for Every Path
A file under api/ serves only its own path. Add a rewrite that sends every path under /api/ to your function. The function still sees the requested path, which mion routes.
{
"rewrites": [{"source": "/api/(.*)", "destination": "/api/mion"}]
}
API Base Path
Set basePath: '/api' on the router and the client, and point the client's baseURL at the origin. Putting /api in baseURL does not work: a route path is absolute, so it replaces the base's path and drops the prefix.
// your routes module
const mion = createMionRouter({basePath: '/api'});
// your client
const {routes} = initClient<MyApi>({baseURL: 'https://your-app.vercel.app', basePath: '/api'});
Handler Options
Pass options such as maxBodySize (128 KB by default) and maxBodySizeCap to createVercelHandler. maxBodySizeCap defaults to the 4.5 MB Vercel allows, and Vercel refuses anything bigger before the function runs; override it if Vercel changes it. Each route has its own body limit, and a body stream past it is cancelled with a 413.
import {createVercelHandler} from '@mionjs/platform-vercel';
import './vercel-routes.ts';
export const {GET, POST, PUT, DELETE, PATCH, OPTIONS} = createVercelHandler({
maxBodySize: 512 * 1024,
});
Request -> Response API, so it runs on both the Node.js and Edge runtimes without changes.new Function() and eval(), so an Edge build must set
runTypes: {emitMode: 'both'} on the mion Vite plugin:mionVitePlugin({
runTypes: {
// required on Edge: no `new Function` at runtime
emitMode: 'both',
},
}),
mion.initRoutes fails with "Code generation from strings disallowed for this context".
See Cloudflare Workers for why, and the bundle-size cost.
Node.js-runtime deployments need no such change.