Validation & Serialization
mion uses Deepkit's runtime types to automatically Validate request params and Serialize/Deserialize request/response data.
For more info please check deepkit's documentation:
Deepkit is also a fully featured Typescript framework but has a different philosophy than mion. mion tries to be lightweight and minimal while deepkit is heavier and oriented towards enterprise applications.
Please note we only use
Please note we only use
@deepkit/type-compiler
for runtime type compilation and @deepkit/type
packages from DeepkitExplicit Types!
Type Inference is not supported so parameter types and especially return types must be explicitly defined.
๐ซ Incorrect route definitions:
const myNullRoute = route(() => null);
const sayHello = route((ctx, name) => `Hello ${name}`);
const getDummyUser =route( () => {
return new User({name: 'James', surname: 'Smith'})
});
const getUser = route(async (ctx, userId) => {
return data.getUserById(userId);
});
โ Correct route definitions:
const myNullRoute = route((): null => null);
const sayHello = route((ctx: Context, name: string): string => `Hello ${name}`);
const getDummyUser = route((): User => {
return new User({name: 'James', surname: 'Smith'})
});
const getUser = route(async (ctx: Context, userId: number): Promise<User> => {
return data.getUserById(userId);
});
Configuring ESLint
It might be a good idea to enforce explicit types in router files, but having to explicitly declare types everywhere can be a bit annoying.
We recommend using a suffix in your router files i.e. .routes.ts
and configure ESLint to enforce explicit types only on those files.
/* file: .eslintrc.js */
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
parserOptions: {
project: [
'./tsconfig.json',
'./packages/*/tsconfig.json' // use only in monorepos
],
},
// adds explicit types rules to .routes.ts files
overrides: [
{
files: ['**/*.routes.ts'],
rules: {
'@typescript-eslint/explicit-function-return-type': 'error',
'@typescript-eslint/no-explicit-any': 'error',
},
},
],
};
Table of Contents