care-fully v1.0.2
Care-Fully
A Chainable way to respond to express request.
This library allows you to have re usable logic in your express application that is able to stack on top of each other.
It manages all of your async code so that you can just ask for what you need and perform validation.
import express from "express"
const app = express();
import jwt from "jsonwebtoken"
import SECRET from "./secret";
import {Respond} from "care-fully"
import ProfileRepo from "./repo/Profile"
//interface
import {User} from "./user-interface"
import {Profile} from "./user-profile-interface";
const userR = Respond.Append({
user({req}){
const user = jwt.verify(req.headers.token,SECRET)
return user as User
}
}).catch(({error,res})=>{
//handle error here, nothing else will respond
res.status(403).send({
message:"fobidden"
})
})
const profileR = userR.append({
/**
* This comes from the previous response and is reuse.
* You will get any type information was passed down.
*/
async profile({user}){
//type checking available
const profile = await ProfileRepo.getUserProfile(user)
if(!profile){
throw new Error("user profile has not been created")
}else{
return profile
}
}
}).correct({
async profile({user}){
//create new profile for user
const profile = await ProfileRepo.createUserProfile(user);
return profile as Profile
}
}).catch(({res})=>{
res.status(500).json({
message:"failed to create user profile"
})
})
app.post("/user/profile",Respond.MergeAll(profileR).data(({profile})=>{
//this is the user profile
return profile;
})
/**
* User response is a 200 response with the user profile
*
* /
This allows you to abstract out all of the things your server has to do so you can reuse them later on.
Getting the user can be user later on as well, in different api response.
Static Methods
MergeAll
one or more handlers can be joined togather to receive all the appended data. Merged handlers are run in parallel
ConcatAll
one or more handlers can be joined togather to receive all the appended data. Concat handlers are run in one after each other. Even if they are async
Good
Automatically respond with a 200 ok message if nothing throws an error
Respond.Append(...info...).good(async({info})=>{
await sendEmail(info)
})
Data
Automatically respond with a 200 message containing what ever data was returned
Respond.Append(...info...).data(({info})=>{
return format(info);
})