UNPKG

jsql-superagent-plugin

Version:

React and Vue plugin with SuperAgent library to change sql queries to hash for JSQL.

1 lines 7.41 kB
"use strict";if(window.JSQL)throw new Error("JSQL: Main object conflict");window.JSQL=function(config){this.host=null,this.path=null,this.querySet={},this.headers={Accept:"application/json","Content-Type":"application/json"},this.hideErrors=!1,this.rxjs=!1,this.setConfig(config)},JSQL.prototype.isFunction=function(obj){return"function"==typeof obj||!1},JSQL.prototype.throw=function(error){if(!this.hideErrors)throw new Error(error)},JSQL.prototype.setConfig=function(config){null!=config&&0!==Object.keys(config).length||this.throw("JSQL: No options provided"),void 0!==config.host&&null!==config.host&&"string"==typeof config.host&&0!==config.host.trim().length&&""!==config.host||this.throw("JSQL: Invalid host"),void 0!==config.host&&null!==config.host&&"string"==typeof config.host&&0!==config.host.trim().length&&""!==config.host||(config.path="/"),"/"===config.host.substring(config.host.length-1,config.host.length)&&"/"===config.path.substring(0,1)&&(config.path=config.path.substring(1,config.path.length)),"/"!==config.host.substring(config.host.length-1,config.host.length)&&config.path&&"/"!==config.path.substring(0,1)&&(config.path="/"+config.path),void 0!==config.path&&null!==config.path&&"/"!==config.path.substring(config.path.length-1,config.path.length)&&(config.path=config.path+"/"),void 0!==config.hideErrors&&null!==config.hideErrors&&(this.hideErrors=config.hideErrors),this.host=config.host,this.path=config.path?config.path:"/",this.url=this.host+this.path,this.rxjs=void 0!==config.rxjs&&config.rxjs},JSQL.prototype.request=function(){this.throw("JSQL: No request implementation")},JSQL.prototype.wrap=function(){this.throw("JSQL: No wrap implementation")},JSQL.prototype.each=function(obj,iterator){for(var prop in obj)obj.hasOwnProperty(prop)&&iterator(prop,obj[prop])},JSQL.prototype.isEmptyObject=function(obj){var isEmpty=!0;for(var prop in obj)if(obj.hasOwnProperty(prop)&&void 0!==obj[prop]){isEmpty=!1;break}return isEmpty},JSQL.prototype.isArray=function(obj){return void 0===Array.isArray?"[object Array]"===Object.prototype.toString.call(obj):Array.isArray(obj)},JSQL.prototype.construct=function(token,type,options){var _JSQL=this;(null==token||"string"!=typeof token&&!this.isArray(token))&&this.throw("JSQL: Unable to execute with unset token");var defaultOptions={throwSelectOneError:!1,ignoreSelectOneMoreResults:"selectOne"===type,headers:{},successName:"success",errorName:"error",alwaysName:"always"};if(null==options)options=defaultOptions;else for(var option in defaultOptions)defaultOptions.hasOwnProperty(option)&&(void 0!==options[option]&&null!==options[option]||(options[option]=defaultOptions[option]));for(var header in void 0!==options.headers&&null!==options.headers||(options.headers={}),_JSQL.headers)_JSQL.headers.hasOwnProperty(header)&&(void 0!==options.headers[header]&&null!==options.headers[header]||(options.headers[header]=_JSQL.headers[header]));var promise={xhrPromise:null,type:type,isUsedParamsArray:!1,headers:options.headers,options:options,data:{token:token,params:{}},querySet:[],catchCallback:function(){},param:function(paramName,paramValue){return promise.isUsedParamsArray&&_JSQL.throw("JSQL: Cannot mix params array and single params"),null!=paramName&&"string"==typeof paramName&&paramValue?(void 0!==promise.data.params[paramName]&&console.warn('JSQL: Parameter "'+paramName+'" already exist, will be replaced'),promise.data.params[paramName]=paramValue):_JSQL.throw('JSQL: "param" function accept args: [paramName:string, paramValue:primitive]'),promise},params:function(paramsArrayOrParamsObject){return _JSQL.isArray(paramsArrayOrParamsObject)?(_JSQL.isEmptyObject(promise.data.params)||_JSQL.throw("JSQL: Cannot mix params array and object params"),promise.isUsedParamsArray=!0,promise.data.params=paramsArrayOrParamsObject):_JSQL.each(paramsArrayOrParamsObject,function(paramName,paramValue){promise.data.params[paramName]&&console.warn('JSQL: Parameter "'+paramName+'" already exist, will be replaced'),promise.data.params[paramName]=paramValue}),promise},successResultCallback:function(result,callBack){if("selectOne"===promise.type&&!promise.options.ignoreSelectOneMoreResults&&1<result.length)promise.options.throwSelectOneError=!0;else if(_JSQL.isArray(result)){if(_JSQL.rxjs)return result;"selectOne"===promise.type&&promise.options.ignoreSelectOneMoreResults?callBack(0<result.length?result[0]:null):callBack(result)}else{if(_JSQL.rxjs)return result;callBack(result)}},then:function(callBack){return promise.checkAndCreateXhrPromise(),promise.createAlways(),promise.xhrPromise[promise.options.successName](function(result){promise.successResultCallback(result,callBack)}),promise},thenRxjs:function(result){return promise.successResultCallback(result)},catch:function(callBack){return promise.checkAndCreateXhrPromise(),promise.createAlways(),promise.xhrPromise[promise.options.errorName](function(error){callBack(error)}),promise.catchCallback=callBack,promise},catchRxjs:function(error){return error},createAlways:function(){promise.xhrPromise[promise.options.alwaysName]||(promise.xhrPromise[promise.options.alwaysName]=function(){promise.options.throwSelectOneError&&promise.catchCallback("JSQL: More than one result not allowed with selectOne")})},checkAndCreateXhrPromise:function(){promise.xhrPromise||(promise.xhrPromise=_JSQL.request(_JSQL.url+("selectOne"===promise.type?"select":promise.type),promise.data,promise.headers,promise))}};return promise},JSQL.prototype.select=function(token){return this.wrap(token,"select",this)},JSQL.prototype.selectOne=function(token){return this.wrap(token,"selectOne",this)},JSQL.prototype.update=function(token){return this.wrap(token,"update",this)},JSQL.prototype.insert=function(token){return this.wrap(token,"insert",this)},JSQL.prototype.remove=function(token){return this.wrap(token,"delete",this)},JSQL.prototype.delete=function(token){return this.wrap(token,"delete",this)},JSQL.prototype.querySet={},JSQL.prototype.repoSet={},JSQL.prototype.repo=function(name){if(!name)return this.repoSet;var repoSet={};name&&(this.repoSet[name]||(this.repoSet[name]={}),repoSet=this.repoSet[name]);var self=this,repoObj={namedQuery:function(queryName,sqlQueryFunction){return self.set(queryName,sqlQueryFunction),this}};return Object.assign(repoObj,repoSet)},JSQL.prototype.get=function(queryName){var set=this.querySet[queryName];if(this.isFunction(set)){for(var args=[],i=1;i<arguments.length;i++)args.push(arguments[i]);return set.apply(this,args).build()}return set},JSQL.prototype.set=function(queryName,sqlQuery){return this.querySet[queryName]=sqlQuery,this},JSQL.prototype.query=function(sqlQuery){var self=this,setName="set_"+Object.keys(this.querySet).length;return this.querySet[setName]=[sqlQuery],{__isStructure:!0,__setName:setName,build:function(){return self.querySet[this.__setName]},append:function(sqlQuery){return self.querySet[this.__setName].push(" "+sqlQuery+" "),this}}},void 0!==window.module&&(module.exports=JSQL);import superagent from"superagent";export default class JsqlService{constructor(config){window.JSQL.prototype.request=function(requestUrl,requestData,requestHeaders){return superagent.post(requestUrl,requestHeaders).send(requestData)},window.JSQL.prototype.wrap=function(token,queryType){return this.construct(token,queryType,{successName:"then",errorName:"catch",alwaysName:"finally"})},this.jsql=new window.JSQL(config)}};