Skip to content

Instantly share code, notes, and snippets.

@raymcdermott
Created February 1, 2015 21:39
Show Gist options
  • Save raymcdermott/f4ec1166bf48d02fb68e to your computer and use it in GitHub Desktop.
Save raymcdermott/f4ec1166bf48d02fb68e to your computer and use it in GitHub Desktop.
request-promise fn composition
var express = require('express'),
reqpromise = require('request-promise');
function register(req, res, next) {
var user = {};
user.username = req.body.username;
user.password = req.body.password;
user.firstName = req.body.firstName;
user.lastName = req.body.lastName;
user.email = req.body.username;
getAdminToken(next)
.then(registerWithTaas)
.then(registerWithCustomerPortal)
.then(sendResponse);
function registerWithTaas(adminToken) {
var url = config.taasUrl + config.register,
options = {
json: {
'username': user.username,
'password': user.password,
'groups': [],
'active': true,
'adminToken': adminToken
}
};
return reqpromise.post(url, options).then(function (registeredUser) {
user.uuid = registeredUser.uuid;
}).catch(next);
}
function registerWithCustomerPortal() {
var url = config.customerDomain + config.customerRegister,
options = {
json: {
'uuid': user.uuid,
'username': user.username,
'email': user.email,
'firstName': user.firstName,
'lastName': user.lastName
}
};
return reqpromise.post(url, options).then(function (registeredUser) {
return registeredUser;
}).catch(next);
}
function sendResponse(data) {
res.status(200).send(data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment