What are Type Safe APIs
Type-safe APIs are designed to ensure that data passed between components or services strictly adheres to predefined data types, reducing the risk of type-related runtime errors. Type safety helps catch and prevent data type mismatches and provides a high level of confidence in the correctness and integrity of the data being processed.
Basically APIs that validate and serialize incoming and outgoing data.
Type Safe APIs with Typescript
In the world of Node.js server frameworks, the choices seem limitless. Among the myriad options, mion is a new contender that offers a unique approach to building APIs, with a strong emphasis on TypeScript type safety.
In this article, we will compare Mion to popular server frameworks like Fastify, Elysia.js, tRPC, and Express.js.
Mion: A Quick Overview
Mion is a JavaScript framework designed to create type-safe APIs by leveraging TypeScript's type system. It achieves this by compiling type metadata and making it available at runtime.
Here are some of mion's standout features:
- RPC-Like Architecture:. Mion employs an RPC-like architecture, which simplifies API consumption. You can use remote methods just like local asynchronous functions.
import {Routes} from '@mionkit/router';
const routes = {
sayHello: (ctx, name: string): string => {
return `Hello ${name}.`;
},
greetings: (ctx, name1: string, name2: string): string => {
return `Hello ${name1} and ${name2}.`;
},
} satisfies Routes;
- Fully Typed Client: One of Mion's strengths is its fully typed client. This means that you can enjoy static type checking, autocompletion in the Front-End.
import {initClient} from '@mionkit/client';
// importing only the RemoteApi type from server
import type {MyApi} from './server.routes';
const port = 8076;
const baseURL = `http://localhost:${port}`;
const {routes} = initClient<MyApi>({baseURL});
// calls sayHello route in the server
const sayHello = await routes.sayHello('John').call();
console.log(sayHello); // Hello John
// calls greetings route in the server
const sayHello = await routes.greetings('John', 'Anne').call();
console.log(sayHello); // Hello John and Anne
Exceptional Performance:.
Mion offers fast cold starts and utilizes a straightforward in-memory map for route lookup. This makes it exceptionally fast, which is crucial for any server framework, especially in production environments.
Our goal is to have similar performance to fastify which is considered the gold standard in node.
But mion also supports bun for even greater performance. For more benchmarks check out mion's benchmarks page
Automatic Parameters Validation & Serialization.
mion provides automatic validation and serialization out of the box based in your Typescript types, no need to use scehma o validation library like Joy or Zod.
All parameters declared on mions routes are automatically deserialized & validated, without any other extra step.
Bellow example will always validate thatname
is a string andbornDate
is a Date before calling thesayHello
route.
The type information is directly extracted from typescript.
import {Routes} from '@mionkit/router';
const routes = {
sayHello: (ctx, name: string, bornDate: Date): string => {
return `Hello ${name}, you were born on ${bornDate.toString()}`;
},
} satisfies Routes;
End-to-End Type Safety:.
Mion ensures end-to-end type safety, meaning that you can easily refactor your API without fearing breaking changes. The client will seamlessly adapt to these changes, providing confidence in your API's stability.
Validation is also available directly in the client without any need to query the server just to validate.
Bellow Animation shows how easy is to refactor APIs using mion and how the Client picks the changes.
Write Once, Run Everywhere:.
Mion APIs can run on various platforms, including Node.js, Bum, and serverless platforms like AWS Lambda, Google Cloud Functions, and Azure Functions. This adaptability offers flexibility in choosing your hosting environment.
Comparing Mion to Other Frameworks:
Now, let's take a closer look at how Mion stacks up against other popular server frameworks:
Mion vs. Fastify:
Fastify is known for its speed and low overhead. However, Mion's automatic validation and serialization features provide a unique advantage, enhancing code reliability and eliminating a common source of runtime errors.Mion vs. Elysia.js:
Elysia.js focuses on making the development of web APIs efficient. Mion offers the same efficiency while bolstering type safety out of the box thanks to runtime types vs elisia that uses Typebox for validation.Mion vs. tRPC:
tRPC is another framework that emphasizes type safety. Mion shares this focus but stands out with its automatic validation and serialization out of the box and superior performance.Mion vs. Express.js:
Express.js is a classic choice for building APIs in Node.js. However it is very poor in terms of performance and does not offer any feature related to type safety. Mion takes TypeScript type safety to the next level and simplifies the development.
Conclusion:
Mion is an exciting addition to the world of Node.js server frameworks. Its unique focus on TypeScript type safety with automatic validation and serialization, along good performance and flexibility to run in multiple environments sets it apart from its peers.
The framework offers a fast and efficient way to build APIs with confidence, reducing the chances of runtime errors and simplifying the development process. Whether you're building a small web application or a complex serverless API, Mion is a worthy candidate to consider for your next project.
Top comments (0)