javascript - How to rewrite this function in FP (Ramda.js)? -


i have function , wonder, how should 1 write in fp style (with ramda.js).

  1. registeruser(username, password) -> client_id, client_secret;
  2. getuseraccesstoken(username, password, client_id, client_secret) -> access_token, refresh_token, refreshtokenexpiresat;
  3. save storage: client_id, client_secret, access_token, refresh_token, refreshtokenexpiresat.
async function registeruser(username: string, password: string) {   try {     const { client_id, client_secret } = await authapi.registeruser(username, password);      // tokens     const {       access_token,       refresh_token,       refreshtokenexpiresat,     } = await authapi.getuseraccesstoken(username, password, client_id, client_secret);      // save async storage     store.update('user', {       client_id,       client_secret,       access_token,       refresh_token,       refreshtokenexpiresat,     });   } catch (err) {     throw error(err);   } } 

this has nothing ramda or fp. you've got 1 function doing 3 different things (auth, token request, , persistence) , that's problem.

const registered = authapi.registeruser(username, password)   .catch(err => showuserregistererr(err));  const token = registered.then(({client_id, client_secret}) => {   return promise.all([     promise.resolve({client_id, client_secret}),      authapi.getuseraccesstoken(       username,       password,        client_id,        client_secret     )   ]); });  const persisted = token.then((   {client_id, client_secret},   {access_token, refresh_token, refreshtokenexpiresat} ) => {   return store.update('user', {     client_id,     client_secret,     access_token,     refresh_token,     refreshtokenexpiresat,   }); });  persisted.catch(err => { throw err; }); 

now these constant values, not functions, lets fix that:

// no .catch, caller can catch errors if wants. // no more registeruser function, thin layer on // auth api error handling , we'll delegate // caller that. const gettoken = (username, password, registration) => {   return registration.then(({client_id, client_secret}) => {     return promise.all([       username,       password,       client_id,       client_secret,       authapi.getuseraccesstoken(         username,         password,          client_id,          client_secret       )     ]);   }); };  const persist = datastore => ([   client_id,   client_secret,   {     access_token,     refresh_token,     refreshtokenexpiresat   } ]) => {   return store.update('user', {     client_id,     client_secret,     access_token,     refresh_token,     refreshtokenexpiresat,   }); });  const persisttodb = persist(store); const signup = (username, password) => {   return gettoken(username, password, authapi.registeruser(username, password))     .then(persisttodb)     .catch(showuserregistrationerr); }; 

now different pieces independently testable. since registration passed parameter gettoken can test mock promise. likewise persistence function. can mockout authapi.registeruser function.

no state mutated except in persistence function, functions use parameters, etc. parameterized datastore can swap out persistence (or stub tests).


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -