From b153570c37e5646e1fb7261be01d6ef483938ea1 Mon Sep 17 00:00:00 2001 From: janhommes Date: Wed, 8 Nov 2017 23:02:43 +0100 Subject: [PATCH] Fixed slash issue and added raw property. --- o.js | 10 ++++++---- o.min.js | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/o.js b/o.js index 9479ecb..aa81854 100644 --- a/o.js +++ b/o.js @@ -1,13 +1,13 @@ // +++ -// o.js v0.3.4 +// o.js v0.3.7 // // o.js is a simple oData wrapper for JavaScript. // Currently supporting the following operations: // .get() / .post() / .put() / .delete() / .first() / .take() / .skip() / .filter() / .orderBy() / .orderByDesc() / .count() /.search() / .select() / .any() / .ref() / .deleteRef() // // By Jan Hommes -// Date: 06/07/2017 -// Contributors: Matteo Antony Mistretta (https://github.com/IceOnFire) +// Date: 11/08/2017 +// Contributors: Matteo Antony Mistretta (https://github.com/IceOnFire), // // -------------------- // The MIT License (MIT) @@ -157,6 +157,7 @@ base.inlinecount = null; //if inlinecount is set, here the counting is gold base.param = {}; //this object holds all parameter for a route base.oConfig = config; //the internal config, passed over from the o function + base.raw = null; //holds the data after an callback (raw data, containing also metadata) // ---------------------+++ PUBLICS +++---------------------------- @@ -725,7 +726,7 @@ queryStr += '/'; } - if (typeof res.appending === 'undefined' || res.appending === null) { + if (!res.appending) { queryStr = queryStr.slice(0, -1); } @@ -1470,6 +1471,7 @@ else { if (JSON && response !== '') { var data = JSON.parse(response); + tempBase.raw = data; if (data.hasOwnProperty('value')) { if (isQuery(['$first']) && data.value.length && data.value.length <= 1) { tempBase.data = data.value[0]; diff --git a/o.min.js b/o.min.js index 2ca078a..fb1e821 100644 --- a/o.min.js +++ b/o.min.js @@ -1 +1 @@ -!function(e,n){"function"==typeof define&&define.amd?define(["q"],n):"object"==typeof exports?module.exports=n(require("q")):e.o=n(e.Q)}(this,function(e){function n(e){function n(){for(var e,n={},t=0,r=arguments.length;r>t;t++)for(e in arguments[t])arguments[t].hasOwnProperty(e)&&(n[e]=arguments[t][e]);return n}var r=this;return r.oConfig=r.oConfig||{endpoint:null,format:"json",autoFormat:!0,version:4,strictMode:!0,start:null,ready:null,error:null,headers:[],username:null,password:null,isAsync:!0,isCors:!0,openAjaxRequests:0,isHashRoute:!0,appending:""},r.config=function(e){r.oConfig=n(r.oConfig,e)},r.isEndpoint=function(){return null!==r.oConfig.endpoint},"undefined"==typeof e?r:new t(e,r.oConfig)}function t(n,t){function r(){if("undefined"!=typeof e){var n=e;return n}if("undefined"==typeof window){var n=require("q");return n}return null}function o(e,n,t,r){if(A(n)){var o="",a=[];for(i=0;i0?"?"+e.substring(1):""}function f(e){for(var n=0;n-1||e.toUpperCase().indexOf("HTTPS://")>-1?I=!1:B.oConfig.endpoint||k("You can not use resource query without defining your oData endpoint. Use o().config({endpoint:yourEndpoint}) to define your oData endpoint."),routeName=e,h(e),B)}function v(e){w("$expand")?(U.queryList[U.query.$expand].value+=","+e,U.queryList[U.query.$expand].original=U.queryList[U.query.$expand].value):C("$expand",e,e)}function m(e){var n=e.split("?"),t=e,r="",o={path:[],appending:"",query:{},queryList:[],method:"GET",data:null,progress:null};if(2===n.length){t=n[0],r=n[1];for(var a=r.split("&"),i=0;i0?e:void k(n+": Parameter must be set.")}function O(e,n){if("number"==typeof e)return e;var t=n;return e&&e.length>0&&(isNaN(e)||(t=parseInt(e))),t}function P(e){for(var n=0,t=0;t-1&&n++;return n}function E(e){for(var n=[],t=0;t-1&&n.push(t);for(var t=n.length-1;t>=0;t--)G.splice(n[t],1);G[0]&&(U=G[0])}function b(e){return JSON?JSON.stringify(e):(k("No JSON support."),e)}function A(e){return"undefined"==typeof Array.isArray?"[object Array]"===e.toString():Array.isArray(e)}function R(e,n){return e?-1!==e.indexOf(n,e.length-n.length):!1}function N(e,n){return 0===e.indexOf(n)}function k(e){function n(e){this.message=e,this.name="o.js exception"}if(n.prototype=new Error,B.oConfig.strictMode===!0)throw new n(e);console.log("o.js exception: "+e)}function L(e,n){var t="",r=$(),o=!1;n&&(t+="--batch_"+e+"\n",t+="Content-Type: multipart/mixed; boundary=changeset_"+r+"\n\n");var a=null;null!==B.oConfig.endpoint&&(a=B.oConfig.endpoint.indexOf("://")>-1?B.oConfig.endpoint.split("/")[2]:B.oConfig.endpoint.split("/")[0],a=a.split(":")[0]);for(var i=0;i=200&&e.status<300){if(204!==e.status)if(o){var n,a=[],s=/({[\s\S]*?--batchresponse_)/g;do n=s.exec(e.responseText),n&&(D(n[0].substring(0,n[0].length-16),f),a.push(f.data));while(n);f.data=a}else D(e.responseText,f);X&&X.resolve(f),"function"==typeof t&&t.call(f,f.data,i)}else try{var p=e.responseText;if(JSON&&""!=e.responseText&&(p=JSON.parse(e.responseText)),""!==p&&p["odata.error"]){var l=p["odata.error"].message.value+" | HTTP Status: "+e.status+" | oData Code: "+p["odata.error"].code;k(l)}else k("Request to "+u()+" failed with HTTP status "+(e.status||404)+".")}catch(d){if(H(f,!0,e.status||404,e.responseText),"function"==typeof r)r(e.status||404,d);else{if(!X)throw d;d.status=e.status||404,X.reject(d)}}H(f,!1)}},B.oConfig.username&&B.oConfig.password&&(z&&k("CORS and Basic Auth is not supported for IE <= 9. Try to set isCors:false in the OData config if you do not need CORS support."),e.setRequestHeader("Authorization","Basic "+Y(B.oConfig.username+":"+B.oConfig.password))),!z){if(a)for(var p=0;p0)for(var p=0;p>2,i=(3&t)<<4|r>>4,u=(15&r)<<2|o>>6,s=63&o,isNaN(r)?u=s=64:isNaN(o)&&(s=64),f=f+this._keyStr.charAt(a)+this._keyStr.charAt(i)+this._keyStr.charAt(u)+this._keyStr.charAt(s);return f},_utf8_encode:function(e){e=e.replace(/\r\n/g,"\n");for(var n="",t=0;tr?n+=String.fromCharCode(r):r>127&&2048>r?(n+=String.fromCharCode(r>>6|192),n+=String.fromCharCode(63&r|128)):(n+=String.fromCharCode(r>>12|224),n+=String.fromCharCode(r>>6&63|128),n+=String.fromCharCode(63&r|128))}return n}};return n.encode(e)}var B=this,U=null,G=[],M=[],I=!0,X=null,F=null,z=!1,Q=function(){},W={},K={"==":"eq","===":"eq","!=":"ne","!==":"ne",">":"gt",">=":"ge","<":"lt","<=":"le","&&":"and","||":"or","!":"not","*":"mul","%":"mod"};return B.data=[],B.inlinecount=null,B.param={},B.oConfig=t,B.routes=B.route=function(e,n){A(e)||(e=[e]),"undefined"==typeof window&&k("Routes are only supported in a browser env.");for(var t=window.location.hash,r=0;r=4?U.path.push({resource:"$count",get:null}):(q("$format"),C("$count","count")),B},B.inlineCount=function(e){return B.oConfig.version>=4?(e=e||"true",T("$count")||C("$count",e)):(e=e||"allpages",T("$inlinecount")||C("$inlinecount",e)),B},B.batch=function(e){return h(e),B},B.expand=function(e){return v(e),B},B.loading=function(e,n){return n=n||e,F=e?[e,n]:[function(){},function(){}],B},B.ref=B.link=function(e,n){q("$format"),(null==U||U.get)&&k("You need to define a resource with the find() method to append an navigation property"),B.oConfig.version<4?(U.method="POST",U.path.push("$link"),U.path.push({resource:e,get:null})):(U.method="POST",U.path.push({resource:e,get:null}),U.path.push({resource:"$ref",get:null}));var t=m(e);t.path[t.path.length-1].get=n;var r=u(t);return U.data={"@odata.id":r.substring(0,r.length-1)},B},B.removeRef=B.deleteRef=function(e,n){if(q("$format"),(null==U||U.get)&&k("You need to define a resource with the find() method to append an navigation property"),B.oConfig.version<4?(U.method="POST",U.path.push("$link"),U.path.push({resource:e,get:null})):(U.method="POST",U.path.push({resource:e,get:null}),U.path.push({resource:"$ref",get:null})),n){var t=m(e);t.path[t.path.length-1].get=n;var r=u(t);C("$id",r.substring(0,r.length-1))}return U.method="DELETE",B},B.get=function(e,n){var t=r();return t&&"undefined"==typeof e&&(X=t.defer()),c(e,n,!1),t&&"undefined"==typeof e?X.promise:B},B.save=function(e,n){if("GET"===U.method&&null!==U.data){var t=l(U);t.method="PATCH",t.data=U.data,h(t)}var o=r();return o&&"undefined"==typeof e?(X=o.defer(),c(e,n,!0),X.promise):(c(e,n,!0),B)},B.post=function(e,n){return q("$format"),n&&h(n),U.method="POST",U.data=e,B},B.patch=function(e,n){return n&&h(n),U.path[U.path.length-1]&&U.path[U.path.length-1].get||k("Bulk updates are not supported. You need to query a unique resource with find() to patch/put it."),U.method="PATCH",U.data=e,B},B.put=function(e,n){return n&&h(n),U.path[U.path.length-1]&&U.path[U.path.length-1].get||k("Bulk updates are not supported. You need to query a unique resource with find() to patch/put it."),U.method="PUT",U.data=e,B},B.remove=B["delete"]=function(e){return e&&h(e),U.path[U.path.length-1]&&U.path[U.path.length-1].get||k("Bulk deletes are not supported. You need to query a unique resource with find() to delete it."),U.method="DELETE",B},B.query=function(e){return u(e)},B.search=function(e,n,t,r){var o=a(e,n,t);return 4==B.oConfig.version&&r?T("$search")||C("$search",o,o):T("$filter")||C("$filter",o,o,"$search"),B},B.filterByList=B.exclude=function(e,n){if(!T("$filter")){var t=o(e,n,K["!="],K["&&"]);C("$filter",S(t),t)}return B},B.include=function(e,n){if(!T("$filter")){var t=o(e,n,K["=="],K["||"]);C("$filter",S(t),t)}return B},B.progress=function(e){return null!=U&&(U.progress=e),B},y(n)}return n}); \ No newline at end of file +!function(e,n){"function"==typeof define&&define.amd?define(["q"],n):"object"==typeof exports?module.exports=n(require("q")):e.o=n(e.Q)}(this,function(e){function n(e){function n(){for(var e,n={},t=0,r=arguments.length;r>t;t++)for(e in arguments[t])arguments[t].hasOwnProperty(e)&&(n[e]=arguments[t][e]);return n}var r=this;return r.oConfig=r.oConfig||{endpoint:null,format:"json",autoFormat:!0,version:4,strictMode:!0,start:null,ready:null,error:null,headers:[],username:null,password:null,isAsync:!0,isCors:!0,openAjaxRequests:0,isHashRoute:!0,appending:""},r.config=function(e){r.oConfig=n(r.oConfig,e)},r.isEndpoint=function(){return null!==r.oConfig.endpoint},"undefined"==typeof e?r:new t(e,r.oConfig)}function t(n,t){function r(){if("undefined"!=typeof e){var n=e;return n}if("undefined"==typeof window){var n=require("q");return n}return null}function o(e,n,t,r){if(A(n)){var o="",a=[];for(i=0;i0?"?"+e.substring(1):""}function f(e){for(var n=0;n-1||e.toUpperCase().indexOf("HTTPS://")>-1?I=!1:B.oConfig.endpoint||k("You can not use resource query without defining your oData endpoint. Use o().config({endpoint:yourEndpoint}) to define your oData endpoint."),routeName=e,h(e),B)}function v(e){w("$expand")?(U.queryList[U.query.$expand].value+=","+e,U.queryList[U.query.$expand].original=U.queryList[U.query.$expand].value):C("$expand",e,e)}function m(e){var n=e.split("?"),t=e,r="",o={path:[],appending:"",query:{},queryList:[],method:"GET",data:null,progress:null};if(2===n.length){t=n[0],r=n[1];for(var a=r.split("&"),i=0;i0?e:void k(n+": Parameter must be set.")}function O(e,n){if("number"==typeof e)return e;var t=n;return e&&e.length>0&&(isNaN(e)||(t=parseInt(e))),t}function P(e){for(var n=0,t=0;t-1&&n++;return n}function E(e){for(var n=[],t=0;t-1&&n.push(t);for(var t=n.length-1;t>=0;t--)G.splice(n[t],1);G[0]&&(U=G[0])}function b(e){return JSON?JSON.stringify(e):(k("No JSON support."),e)}function A(e){return"undefined"==typeof Array.isArray?"[object Array]"===e.toString():Array.isArray(e)}function R(e,n){return e?-1!==e.indexOf(n,e.length-n.length):!1}function N(e,n){return 0===e.indexOf(n)}function k(e){function n(e){this.message=e,this.name="o.js exception"}if(n.prototype=new Error,B.oConfig.strictMode===!0)throw new n(e);console.log("o.js exception: "+e)}function L(e,n){var t="",r=$(),o=!1;n&&(t+="--batch_"+e+"\n",t+="Content-Type: multipart/mixed; boundary=changeset_"+r+"\n\n");var a=null;null!==B.oConfig.endpoint&&(a=B.oConfig.endpoint.indexOf("://")>-1?B.oConfig.endpoint.split("/")[2]:B.oConfig.endpoint.split("/")[0],a=a.split(":")[0]);for(var i=0;i=200&&e.status<300){if(204!==e.status)if(o){var n,a=[],s=/({[\s\S]*?--batchresponse_)/g;do n=s.exec(e.responseText),n&&(D(n[0].substring(0,n[0].length-16),f),a.push(f.data));while(n);f.data=a}else D(e.responseText,f);X&&X.resolve(f),"function"==typeof t&&t.call(f,f.data,i)}else try{var p=e.responseText;if(JSON&&""!=e.responseText&&(p=JSON.parse(e.responseText)),""!==p&&p["odata.error"]){var l=p["odata.error"].message.value+" | HTTP Status: "+e.status+" | oData Code: "+p["odata.error"].code;k(l)}else k("Request to "+u()+" failed with HTTP status "+(e.status||404)+".")}catch(d){if(H(f,!0,e.status||404,e.responseText),"function"==typeof r)r(e.status||404,d);else{if(!X)throw d;d.status=e.status||404,X.reject(d)}}H(f,!1)}},B.oConfig.username&&B.oConfig.password&&(z&&k("CORS and Basic Auth is not supported for IE <= 9. Try to set isCors:false in the OData config if you do not need CORS support."),e.setRequestHeader("Authorization","Basic "+Y(B.oConfig.username+":"+B.oConfig.password))),!z){if(a)for(var p=0;p0)for(var p=0;p>2,i=(3&t)<<4|r>>4,u=(15&r)<<2|o>>6,s=63&o,isNaN(r)?u=s=64:isNaN(o)&&(s=64),f=f+this._keyStr.charAt(a)+this._keyStr.charAt(i)+this._keyStr.charAt(u)+this._keyStr.charAt(s);return f},_utf8_encode:function(e){e=e.replace(/\r\n/g,"\n");for(var n="",t=0;tr?n+=String.fromCharCode(r):r>127&&2048>r?(n+=String.fromCharCode(r>>6|192),n+=String.fromCharCode(63&r|128)):(n+=String.fromCharCode(r>>12|224),n+=String.fromCharCode(r>>6&63|128),n+=String.fromCharCode(63&r|128))}return n}};return n.encode(e)}var B=this,U=null,G=[],M=[],I=!0,X=null,F=null,z=!1,Q=function(){},W={},K={"==":"eq","===":"eq","!=":"ne","!==":"ne",">":"gt",">=":"ge","<":"lt","<=":"le","&&":"and","||":"or","!":"not","*":"mul","%":"mod"};return B.data=[],B.inlinecount=null,B.param={},B.oConfig=t,B.raw=null,B.routes=B.route=function(e,n){A(e)||(e=[e]),"undefined"==typeof window&&k("Routes are only supported in a browser env.");for(var t=window.location.hash,r=0;r=4?U.path.push({resource:"$count",get:null}):(q("$format"),C("$count","count")),B},B.inlineCount=function(e){return B.oConfig.version>=4?(e=e||"true",T("$count")||C("$count",e)):(e=e||"allpages",T("$inlinecount")||C("$inlinecount",e)),B},B.batch=function(e){return h(e),B},B.expand=function(e){return v(e),B},B.loading=function(e,n){return n=n||e,F=e?[e,n]:[function(){},function(){}],B},B.ref=B.link=function(e,n){q("$format"),(null==U||U.get)&&k("You need to define a resource with the find() method to append an navigation property"),B.oConfig.version<4?(U.method="POST",U.path.push("$link"),U.path.push({resource:e,get:null})):(U.method="POST",U.path.push({resource:e,get:null}),U.path.push({resource:"$ref",get:null}));var t=m(e);t.path[t.path.length-1].get=n;var r=u(t);return U.data={"@odata.id":r.substring(0,r.length-1)},B},B.removeRef=B.deleteRef=function(e,n){if(q("$format"),(null==U||U.get)&&k("You need to define a resource with the find() method to append an navigation property"),B.oConfig.version<4?(U.method="POST",U.path.push("$link"),U.path.push({resource:e,get:null})):(U.method="POST",U.path.push({resource:e,get:null}),U.path.push({resource:"$ref",get:null})),n){var t=m(e);t.path[t.path.length-1].get=n;var r=u(t);C("$id",r.substring(0,r.length-1))}return U.method="DELETE",B},B.get=function(e,n){var t=r();return t&&"undefined"==typeof e&&(X=t.defer()),c(e,n,!1),t&&"undefined"==typeof e?X.promise:B},B.save=function(e,n){if("GET"===U.method&&null!==U.data){var t=l(U);t.method="PATCH",t.data=U.data,h(t)}var o=r();return o&&"undefined"==typeof e?(X=o.defer(),c(e,n,!0),X.promise):(c(e,n,!0),B)},B.post=function(e,n){return q("$format"),n&&h(n),U.method="POST",U.data=e,B},B.patch=function(e,n){return n&&h(n),U.path[U.path.length-1]&&U.path[U.path.length-1].get||k("Bulk updates are not supported. You need to query a unique resource with find() to patch/put it."),U.method="PATCH",U.data=e,B},B.put=function(e,n){return n&&h(n),U.path[U.path.length-1]&&U.path[U.path.length-1].get||k("Bulk updates are not supported. You need to query a unique resource with find() to patch/put it."),U.method="PUT",U.data=e,B},B.remove=B["delete"]=function(e){return e&&h(e),U.path[U.path.length-1]&&U.path[U.path.length-1].get||k("Bulk deletes are not supported. You need to query a unique resource with find() to delete it."),U.method="DELETE",B},B.query=function(e){return u(e)},B.search=function(e,n,t,r){var o=a(e,n,t);return 4==B.oConfig.version&&r?T("$search")||C("$search",o,o):T("$filter")||C("$filter",o,o,"$search"),B},B.filterByList=B.exclude=function(e,n){if(!T("$filter")){var t=o(e,n,K["!="],K["&&"]);C("$filter",S(t),t)}return B},B.include=function(e,n){if(!T("$filter")){var t=o(e,n,K["=="],K["||"]);C("$filter",S(t),t)}return B},B.progress=function(e){return null!=U&&(U.progress=e),B},y(n)}return n}); \ No newline at end of file