Async App
async-app
is an express wrapper for handling async middlewares, schema
validation, and other stuff.
Installation
npm i --save async-app
Usage ( Basic example)
import {
badRequest,
createApp,
Opts,
errorMiddleware,
} from 'async-app';
interface User {
email: string;
name: string;
password: string;
}
interface Entities extends BaseEntities {
user: User;
}
const options: Opts = {
};
const app = createApp<Entities>(options);
const getUserData = async (user: User) => {
const profile = await fetchProfile(user.email);
if (!profile) {
throw badRequest('USER_NOT_FOUND');
}
return {
...profile,
password: undefined
};
};
app.get(
'/user',
'Returns user data',
`You must be authenticated!`,
authenticated(),
req => getUserData(req.user),
200,
);
app.use(errorMiddleware);