Skip to content

Instantly share code, notes, and snippets.

@LucaColonnello
Last active August 31, 2017 17:23
Show Gist options
  • Save LucaColonnello/55ffd0a311bbf64becc1 to your computer and use it in GitHub Desktop.
Save LucaColonnello/55ffd0a311bbf64becc1 to your computer and use it in GitHub Desktop.
Loopback React SDK Proposals
// it could be part of the LoopBackSDK module
// evaluate usage of node http module for ajax call
var http = require( 'http' );
// LoopBack HTTP Interface
// Class that has the responsibility to call
// the LoopBack Application, must be instantiated for each models
function HTTPInterface(
modelName,
urlBase,
actions
) {
// requires check
// ...
// sets
this.modelName = modelName;
this.urlBase = urlBase;
this.actions = actions;
this.actionsUrlGenerators = { };
// parse actions to create for each of them
// a function that compile the url with received params
for( var i in this.actions ) {
this.compileActionUrlGenerator( i, this.actions[ i ] );
this[ i ] = this.makeCall.bind( this, i );
}
}
// this function create the url generator.
// url generators prevent this class to recalc
// the url pattern each time we have to make
// a call to the API for each action
HTTPInterface.prototype.compileActionUrlGenerator = function compileActionUrlGenerator( action, actionConfig ) {
// calc parameters that has to be in the url, like user's id (/user/:id/)
// use a regexp to match pattern like /\:([^\/?]+)/
// so, params contains an array of possible parameters
// like [ "id" ]
var params = actionConfig.url.match( /\:([^\/?]+)/g );
if( params && params.length ) params = params.map( function( v ) { return v.substring( 1, v.length ); } );
// add the function body to params,
// this could be 'return "/users/" + id;', generated with params and actionConfig.url .
// here is it possible to call LoopBackAuth and has the accessToken not to be passed
// but directly taken from LoopBackAuth class
// ...
var genratedFunction = new Function( params );
this.actionsUrlGenerators[ action + "__urlGenerator" ] = genratedFunction;
};
// this function makes the call to the LoopBack Application
// receiving the action name, the parameters that goes
// in the url and the data object (the body of the HTTP Request).
// action parameters is binded automatically into the constructor,
// the parameters must be an array and the data is an optional literal object
HTTPInterface.prototype.makeCall = function makeCall( action, parameters, data ) {
// evaluate params
// evaluate data, if exists
// call LoopBackAuth for setts Header (if the auth mode is by header)
// call the action url generator in order to have the url
// make the http call, use promises (more compatibility to chainable call and other libs)
// ...
};
module.exports = HTTPInterface;
var LoopBackSDK = require( 'loopback-sdk' );
// create user model api
var UserAPI = new LoopBackSDK.HTTPInterface(
// set model
"user",
// evaluate if set a config variable for the url of the app
LoopBackSDK.appUrl + "/users/",
// set actions
{
findById: {
url: ':id',
method: 'GET'
},
create: {
url: '',
method: 'POST',
// evaluate if create a map
// of parameters in the body data
// helpful for validation, maybe
data: {
name: LoopBackSDK.types.String,
lastname: LoopBackSDK.types.String,
email: LoopBackSDK.types.Email,
photo: LoopBackSDK.types.FileImage,
}
},
// ...
}
);
module.exports = UserAPI;
var UserAPI = require('loopback-sdk/apis/user');
UserAPI.create( {
name: 'Luca',
lastname: 'Colonnello',
email: '[email protected]',
photo: 'bytes of the photo to be converted in HTML5 File API multipart/form-data'
} )
.then( function( d ){
UserAPI.findById( [ d.id ] )
.then( ... )
.fail( ... )
.done( )
;
} )
.fail( function( ) { } )
.done( )
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment