web-applicationscustom-backend

How to pass an object to a FatFractal server extension, access from within the extension, and return an object back to client application?


I am using the FatFractal JavaScript SDK and have declared a server extension in my FFDL file as follows:

CREATE EXTENSION /ChangePassword AS javascript:require('scripts/UserAdministration').changePassword();

I am creating a server extension to allow a user to change their login password. From my client application, I want to pass a simple object to /ChangePassword containing the logged in user's username, current (old) password, and new desired password. Assuming I have populated said object, how would I then A) Pass the object the client application to the server extension, B) Get a handle to the passed object in the server extension, and C) Return a confirmation object (preferably) or message from the server extension to the client application once the change is complete?

FFDL:

CREATE OBJECTTYPE ChangePasswordRequest (userName STRING, oldPassword STRING, newPassword STRING)
CREATE COLLECTION /ChangePasswordRequest OBJECTTYPE ChangePasswordRequest

Client application JS code:

    ...

    function ChangePassInfo() {
        this.userName = null;
        this.currentPassword = null;
        this.newPassword = null;
        return this;
    }

    ...

    function changePassword() {
        var uname = ff.loggedInUser().userName;
        var oldPass = $("#input-curr-pass").val();
        var newPass = $("#input-new-pass").val();

        var requestInfo = new ChangePassInfo();
        requestInfo.userName = uname;
        requestInfo.currentPassword = oldPass;
        requestInfo.newPassword = newPass;

        // pass 'requestInfo' to 'ChangePassword' extension
        // acquire handle to confirmation object/message returned from 'ChangePassword'

        ...

    }

Server extension JS code:

var ff = require('ffef/FatFractal');

...

function ChangePasswordRequest() {
    this.clazz = 'ChangePasswordRequest';
    this.createdBy = 'system';
    this.userName = null;
    this.oldPassword = null;
    this.newPassword = null;
    return this;
}

...

function changePassword() {

    var changePassReq; // instance of a 'ChangePasswordRequest' object

    // acquire handle to 'requestInfo' passed from client application to populate 'changePassReq'

    ...

    // return a confirmation message or copy of 'changePassReq' to client application

}

...

exports.changePassword = changePassword;

The commented sections are the problem areas I'm looking to solve. Once those are solved, I should be able to fill in the rest of the implementation. Thanks!


Solution

  • Let's start with backend. Assuming an object has been posted to the extension, here's how you retrieve it in your function:

    var ff = require('ffef/FatFractal');     // FatFractal module
    var data = ff.getExtensionRequestData(); // Get request data
    var member = data.httpContent.member     // Get 'obj' member of passed object
    

    Alright, so now you've done some stuff and want to return something, here's how you do that:

    var r = ff.response();  // Get response object, and populate
    r.result = { member: "Something", otherMember: false, yaMember: 42 };
    r.responseCode = "200";
    r.statusMessage = "This message is useful";
    r.mimeType = "application/json";
    

    Finally, to the client side. Here's how to post an object to the extension:

    var ff = new FatFractal();
    
    // ...
    
    ff.postObjToExtension({ member: "derp" }, "/yourExtension", ... );
    

    Hope that clarifies things!