Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

care-fully

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

care-fully

This library allows you to have re usable logic in your express application that is able to stack on top of each other.

  • 1.0.2
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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);
   })

FAQs

Package last updated on 26 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc