2121
2222/**
2323 * @module FrameworkBuilders
24- * @version 2.6 .0
24+ * @version 2.7 .0
2525 */
2626
2727'use strict' ;
@@ -31,7 +31,7 @@ const DEFAULT_SCHEMA = 'default';
3131const SKIP = { $$schema : true , $$result : true , $$callback : true , $$async : true , $$index : true , $$repository : true , $$can : true , $$controller : true } ;
3232const REGEXP_CLEAN_EMAIL = / \s / g;
3333const REGEXP_CLEAN_PHONE = / \s | \. | \- | \( | \) / g;
34- const REGEXP_NEWOPERATION = / ^ f u n c t i o n ( \s ) ? \( [ a - z A - Z 0 - 9 \$ ] + \) / g ;
34+ const REGEXP_NEWOPERATION = / ^ f u n c t i o n ( \s ) ? \( [ a - z A - Z 0 - 9 \$ ] + \) / ;
3535const hasOwnProperty = Object . prototype . hasOwnProperty ;
3636const Qs = require ( 'querystring' ) ;
3737
@@ -180,9 +180,10 @@ SchemaBuilderEntity.prototype.define = function(name, type, required, custom) {
180180
181181 switch ( this . schema [ name ] . type ) {
182182 case 7 :
183- if ( ! this . dependencies )
184- this . dependencies = [ ] ;
185- this . dependencies . push ( name ) ;
183+ if ( this . dependencies )
184+ this . dependencies . push ( name ) ;
185+ else
186+ this . dependencies = [ name ] ;
186187 break ;
187188 }
188189
@@ -2345,6 +2346,11 @@ SchemaInstance.prototype.$exec = function(name, helper, callback) {
23452346 return this ;
23462347} ;
23472348
2349+ SchemaInstance . prototype . $controller = function ( controller ) {
2350+ this . $$controller = controller ;
2351+ return this ;
2352+ } ;
2353+
23482354SchemaInstance . prototype . $save = function ( helper , callback ) {
23492355 if ( this . $$can && this . $$async )
23502356 this . $push ( 'save' , helper ) ;
@@ -3538,6 +3544,7 @@ function async_wait(arr, onItem, onCallback, index) {
35383544}
35393545
35403546function RESTBuilder ( url ) {
3547+
35413548 this . $url = url ;
35423549 this . $headers = { 'User-Agent' : 'Total.js/v' + F . version_header , Accept : 'application/json, text/plain, text/plain, text/xml' } ;
35433550 this . $method = 'get' ;
@@ -3546,6 +3553,7 @@ function RESTBuilder(url) {
35463553 this . $schema ;
35473554 this . $length = 0 ;
35483555 this . $transform = transforms [ 'restbuilder_default' ] ;
3556+ this . $files = null ;
35493557
35503558 // this.$flags;
35513559 // this.$data = {};
@@ -3591,6 +3599,15 @@ RESTBuilder.prototype.url = function(url) {
35913599 return this ;
35923600} ;
35933601
3602+ RESTBuilder . prototype . file = function ( name , filename , buffer ) {
3603+ var obj = { name : name , filename : filename , buffer : buffer } ;
3604+ if ( this . $files )
3605+ this . $files . push ( obj ) ;
3606+ else
3607+ this . $files = [ obj ] ;
3608+ return this ;
3609+ } ;
3610+
35943611RESTBuilder . prototype . maketransform = function ( obj , data ) {
35953612 if ( this . $transform ) {
35963613 var fn = transforms [ 'restbuilder' ] [ this . $transform ] ;
@@ -3657,6 +3674,22 @@ RESTBuilder.prototype.origin = function(value) {
36573674 return this ;
36583675} ;
36593676
3677+ RESTBuilder . prototype . robot = function ( ) {
3678+ if ( this . $headers [ 'User-Agent' ] )
3679+ this . $headers [ 'User-Agent' ] += ' Bot' ;
3680+ else
3681+ this . $headers [ 'User-Agent' ] = 'Bot' ;
3682+ return this ;
3683+ } ;
3684+
3685+ RESTBuilder . prototype . mobile = function ( ) {
3686+ if ( this . $headers [ 'User-Agent' ] )
3687+ this . $headers [ 'User-Agent' ] += ' iPhone' ;
3688+ else
3689+ this . $headers [ 'User-Agent' ] = 'iPhone' ;
3690+ return this ;
3691+ } ;
3692+
36603693RESTBuilder . prototype . put = function ( data ) {
36613694 if ( this . $method !== 'put' ) {
36623695 this . $flags = null ;
@@ -3814,12 +3847,26 @@ RESTBuilder.prototype.stream = function(callback) {
38143847 return U . download ( self . $url , flags , self . $data , callback , self . $cookies , self . $headers , undefined , self . $timeout ) ;
38153848} ;
38163849
3850+ RESTBuilder . prototype . file = function ( name , filename ) {
3851+ var self = this ;
3852+ var obj = { name : name , filename : filename } ;
3853+ if ( self . $files )
3854+ self . $files . push ( obj ) ;
3855+ else
3856+ self . $files = [ obj ] ;
3857+ return self ;
3858+ } ;
3859+
38173860RESTBuilder . prototype . exec = function ( callback ) {
38183861
38193862 if ( ! callback )
38203863 callback = NOOP ;
38213864
38223865 var self = this ;
3866+
3867+ if ( self . $files && self . $method === 'get' )
3868+ self . $method = 'post' ;
3869+
38233870 var flags = self . $flags ? self . $flags : [ self . $method ] ;
38243871 var key ;
38253872
@@ -3829,14 +3876,19 @@ RESTBuilder.prototype.exec = function(callback) {
38293876 self . $length && flags . push ( '<' + self . $length ) ;
38303877 self . $redirect === false && flags . push ( 'noredirect' ) ;
38313878
3832- switch ( self . $type ) {
3833- case 1 :
3834- flags . push ( 'json' ) ;
3835- break ;
3836- case 3 :
3837- flags . push ( 'xml' ) ;
3838- break ;
3879+ if ( self . $files ) {
3880+ flags . push ( 'upload' ) ;
3881+ } else {
3882+ switch ( self . $type ) {
3883+ case 1 :
3884+ flags . push ( 'json' ) ;
3885+ break ;
3886+ case 3 :
3887+ flags . push ( 'xml' ) ;
3888+ break ;
3889+ }
38393890 }
3891+
38403892 self . $flags = flags ;
38413893 }
38423894
@@ -3886,7 +3938,7 @@ RESTBuilder.prototype.exec = function(callback) {
38863938 callback ( err , self . maketransform ( output . value , output ) , output ) ;
38873939 output . cache = true ;
38883940
3889- } , self . $cookies , self . $headers , undefined , self . $timeout ) ;
3941+ } , self . $cookies , self . $headers , undefined , self . $timeout , self . $files ) ;
38903942} ;
38913943
38923944function exec_removelisteners ( evt ) {
@@ -3988,7 +4040,9 @@ exports.Pagination = Pagination;
39884040exports . Page = Page ;
39894041exports . UrlBuilder = UrlBuilder ;
39904042exports . TransformBuilder = TransformBuilder ;
4043+ exports . SchemaOptions = SchemaOptions ;
39914044global . RESTBuilder = RESTBuilder ;
4045+ global . RESTBuilderResponse = RESTBuilderResponse ;
39924046global . ErrorBuilder = ErrorBuilder ;
39934047global . TransformBuilder = TransformBuilder ;
39944048global . Pagination = Pagination ;
0 commit comments