Validation & Serialization
mion uses Deepkit's runtime types to automatically Validate request params and Serialize/Deserialize request/response data.
Thanks to Deepkit's magic the type information is available at runtime and the data can be "auto-magically" Validated and Serialized.
Deepkit is also a fully featured Typescript framework but has a completely different philosophy than mion. For more information please read Deepkit's documentation!
Please note we only use the @deepkit/type package from Deepkit
Please note we only use the @deepkit/type package from Deepkit
Explicit Types!
Type Inference is not supported so parameter types and specially return types must be explicitly defined.
๐ซ Incorrect route definitions:
const myNullRoute = () => null;
const sayHello = (ctx, name) => `Hello ${name}`;
const getDummyUser = () => {
return new User({name: 'James', surname: 'Smith'})
};
const getUser = async (ctx, userId) => {
return data.getUserById(userId);
}
โ Correct route definitions:
const myNullRoute = (): null => null;
const sayHello = (ctx: Context, name: string): string => `Hello ${name}`;
const getDummyUser = (): User => {
return new User({name: 'James', surname: 'Smith'})
};
const getUser = 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