jeudi 25 novembre 2021

How to use strategy pattern in nodejs

I am making some rest calls in my code based upon the value in a variable. Each of these rest calls takes a different number of parameters but the response remains the same.

if (type=="Membership"){
    reponse=await claimRepo.findDetailsByMembersip(memberNumber)
}
else if (type=="Claim"){
    reponse=await claimRepo.findDetailsByClaim(claimNumber)
}
else if (type=="Date"){
    reponse=await claimRepo.findDetailsByDate(date)
}

Now inside the claimRepo class

async findDetailsByMembersip(memberNumber){
    const data=requests.get('someurl')//make the call
    return {
        data,
        statusCode
    }
}

async findDetailsByClaim(claimNumber){
    const data=requests.get('someurl')//make the call
    return {
        data,
        statusCode
    }
}

async findDetailsByDate(date){
    const data=requests.get('someurl')//make the call
    return {
        data,
        statusCode
    }
}

Is there a way to get rid of these if-else blocks and implement this using the strategy pattern ?

Aucun commentaire:

Enregistrer un commentaire