I'm using Kinvey as a BaaS (Backend As A Service) for my upcoming app, Kids In Touch. When a user sends an invitation request to a friend, my Kinvey Business Logic onPreSave event needs to validate the invitee's mobile phone number.

Well, the code for that validation is INSANE. I'm using Google's libphonenumber codebase converted to JavaScript thanks to Al Cheetah / albeebe. Of course I don't want this giant code in every onPreSave validation that checks numbers; so, I've put it in a Kinvey Custom End-Point. Now, I can call wherever I need to without duplicating code.

Or.. so I thought. I've spent a stupid amount of time getting this to work. One problem with using any remote service (API, BaaS, PaaS, etc) is that you don't get to see the magic going on in the background. So, if something is not working, you only get to see whatever output that service spits out. You can't jump into THEIR node.js code and put breakpoints on it. That's why solving this took so long - it had nothing at ALL to do with MY coding skills! :-)

Fortunately, the Kinvey folks are pretty awesome for lending a helping hand. Their VP of Engineering (Ivan) gave a few pointers. He also managed to post the answer to my problem just as I was posting the answer to my problem. I spent forever getting there. I should have just sat on the couch and let him answer it for me. http://support.kinvey.com/hc/communities/public/questions/200359896-Working-Example-of-The-Request-Module-Using-POST-

I'm putting the answer here so someone else might find it. The Kinvey community support forum is private and a bit clunky. So, someone struggling with this problem might not find the answer.

Anyway, HERE is how I am calling my Custom End-Point from my onPreSave event in a datastore.

**UPDATED : ** Ivan showed how to "forward" the authorization from the original BL request to the Custom Endpoint. I've updated the example below with that code.

/**
 * Confirm a mobile number is valid based on the mobile number and cc (country code) in the request.
 */
var validateMobile = function( callback ) {

    if( request.body.mobile !== '' ) {

        modules.request.request({
            uri: 'https://baas.kinvey.com/rpc/' + request.appKey + '/custom/ValidatePhoneNumber',
            method: 'POST',
            json: {"mobile" : request.body.mobile, "cc": request.body.cc},
            headers: {
                'Authorization': request.headers.authorization
            }
        }, function ( validateErr, validateResponse, validateBody) {

            if( validateErr ) {
                response.body.debug = {"error" : "Failed to validate mobile number"};
                response.complete(400);
                return;
            }

            if( validateBody.valid !== true ) {
                validationResults.push( {"attr" : "mobile", "valid" : false, "msg": ["Mobile number is not valid"] } );
            } else {
                request.body.mobile = validateBody.e164;
            }

            if( callback ) {
                callback('validateMobile');
            }

        });

    } else {

        // Nothing to check so let it know all done.
        if( callback ) {
            callback('validateMobile');
        }

    }

};