diff --git a/packages/@jsonql/koa/package.json b/packages/@jsonql/koa/package.json index a683b5e3e6456c9afd2bd1c6f3b9a87bdf70391d..13a1aa2efde3376d9b31a86154c77a60ffe2bcf4 100644 --- a/packages/@jsonql/koa/package.json +++ b/packages/@jsonql/koa/package.json @@ -6,7 +6,7 @@ "scripts": { "test": "ava --verbose", "test:basic": "DEBUG=jsonql-koa* ava --verbose ./tests/basic.test.js", - "test:auth": "DEBUG=jsonql-koa* ava --verbose ./tests/auth.test.js", + "test:auth": "DEBUG=jsonql-* ava --verbose ./tests/auth.test.js", "test:socket": "DEBUG=jsonql-koa* ava --verbose ./tests/socket.test.js", "test:ms": "DEBUG=jsonql-koa* ava --verbose ./tests/ms.test.js", "test:cli": "DEBUG=jsonql-koa* node ./cli.js" @@ -53,7 +53,7 @@ "debug": "^4.1.1", "fs-extra": "^8.1.0", "jsonql-constants": "^1.8.10", - "jsonql-koa": "^1.4.9", + "jsonql-koa": "^1.4.10", "jsonql-params-validator": "^1.4.11", "koa": "^2.11.0", "koa-bodyparser": "^4.2.1", diff --git a/packages/@jsonql/koa/src/index.js b/packages/@jsonql/koa/src/index.js index 64d2cd05339eb26d3c44ba1f67e4d4e7f0d8436e..7226257254c637b24612b578e380f0018219074f 100644 --- a/packages/@jsonql/koa/src/index.js +++ b/packages/@jsonql/koa/src/index.js @@ -3,8 +3,8 @@ const http = require('http') const Koa = require('koa') const bodyparser = require('koa-bodyparser') const cors = require('koa-cors') -const { jsonqlKoa } = require('../../../koa/main') -/* const { jsonqlKoa } = require('jsonql-koa') */ +// const { jsonqlKoa } = require('../../../koa/main') +const { jsonqlKoa } = require('jsonql-koa') const { getSocketServer } = require('./get-socket-server') /** diff --git a/packages/errors/dist/jsonql-errors.cjs.js b/packages/errors/dist/jsonql-errors.cjs.js index 14e2ae517f75472ba7cc5b799db15d831fdfc518..6d528598e7455b7ad8ad28eb211baee5c7e702af 100644 --- a/packages/errors/dist/jsonql-errors.cjs.js +++ b/packages/errors/dist/jsonql-errors.cjs.js @@ -1,2 +1,2 @@ -!function(r,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((r=r||self).jsonqlErrors={})}(this,(function(r){"use strict";var t=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 406},e.name.get=function(){return"Jsonql406Error"},Object.defineProperties(t,e),t}(Error),e=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 500},e.name.get=function(){return"Jsonql500Error"},Object.defineProperties(t,e),t}(Error),o=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 401},e.name.get=function(){return"JsonqlAuthorisationError"},Object.defineProperties(t,e),t}(Error),n=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 401},e.name.get=function(){return"JsonqlContractAuthError"},Object.defineProperties(t,e),t}(Error),a=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 500},e.name.get=function(){return"JsonqlResolverAppError"},Object.defineProperties(t,e),t}(Error),c="undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{},s=function(){try{if(window||document)return!0}catch(r){}return!1},i=function(){try{if(!s()&&c)return!0}catch(r){}return!1};var u=function(r){function t(){for(var t=[],e=arguments.length;e--;)t[e]=arguments[e];r.apply(this,t)}return r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t,t.where=function(){return s()?"browser":i()?"node":"unknown"},t}(Error),p=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,Error.captureStackTrace&&Error.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 404},e.name.get=function(){return"JsonqlResolverNotFoundError"},Object.defineProperties(t,e),t}(u),l=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0}};return e.name.get=function(){return"JsonqlEnumError"},Object.defineProperties(t,e),t}(Error),f=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0}};return e.name.get=function(){return"JsonqlTypeError"},Object.defineProperties(t,e),t}(Error),h=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0}};return e.name.get=function(){return"JsonqlCheckerError"},Object.defineProperties(t,e),t}(Error),m=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,Error.captureStackTrace&&Error.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0}};return e.name.get=function(){return"JsonqlValidationError"},Object.defineProperties(t,e),t}(u),d="No message",g=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,Error.captureStackTrace&&Error.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0},statusCode:{configurable:!0}};return e.name.get=function(){return"JsonqlError"},e.statusCode.get=function(){return-1},Object.defineProperties(t,e),t}(u),E=function(r){function t(e,o){r.call(this,o),this.statusCode=e,this.className=t.name}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0}};return e.name.get=function(){return"JsonqlServerError"},Object.defineProperties(t,e),t}(Error),y=Object.freeze({__proto__:null,Jsonql406Error:t,Jsonql500Error:e,JsonqlAuthorisationError:o,JsonqlContractAuthError:n,JsonqlResolverAppError:a,JsonqlResolverNotFoundError:p,JsonqlEnumError:l,JsonqlTypeError:f,JsonqlCheckerError:h,JsonqlValidationError:m,JsonqlError:g,JsonqlServerError:E}),_=g,b=function(r,t){return!!Object.keys(r).filter((function(r){return t===r})).length};var q="unknown";function J(r,t){var e=function(r,t){return r.filter((function(r){return t instanceof r})).map((function(r){return r.name}))}(r,t);return e.length?e[0]:q}r.JSONQL_ERRORS_INFO="version: 1.1.5 module: umd",r.Jsonql406Error=t,r.Jsonql500Error=e,r.JsonqlAuthorisationError=o,r.JsonqlCheckerError=h,r.JsonqlContractAuthError=n,r.JsonqlEnumError=l,r.JsonqlError=g,r.JsonqlResolverAppError=a,r.JsonqlResolverNotFoundError=p,r.JsonqlServerError=E,r.JsonqlTypeError=f,r.JsonqlValidationError=m,r.UNKNOWN_ERROR=q,r.clientErrorsHandler=function(r){if(b(r,"error")){var t=r.error,e=t.className,o=t.name,n=e||o,a=t.message||d,c=t.detail||t;if(n&&y[n])throw new y[e](a,c);throw new _(a,c)}return r},r.finalCatch=function(r){if(Array.isArray(r))throw new m("",r);var c=r.message||d,s=r.detail||r;switch(!0){case r instanceof t:throw new t(c,s);case r instanceof e:throw new e(c,s);case r instanceof o:throw new o(c,s);case r instanceof n:throw new n(c,s);case r instanceof a:throw new a(c,s);case r instanceof p:throw new p(c,s);case r instanceof l:throw new l(c,s);case r instanceof f:throw new f(c,s);case r instanceof h:throw new h(c,s);case r instanceof m:throw new m(c,s);case r instanceof E:throw new E(c,s);default:throw new g(c,s)}},r.getErrorByStatus=function(r,t){switch(void 0===t&&(t=!1),r){case 401:return t?"JsonqlContractAuthError":"JsonqlAuthorisationError";case 403:return"JsonqlForbiddenError";case 404:return"JsonqlResolverNotFoundError";case 406:return"Jsonql406Error";case 500:return"Jsonql500Error";default:return"JsonqlError"}},r.getErrorNameByInstance=J,r.getErrorNameByInstanceWithDefault=function(r,t){var e=J(r,t);return e===q?"JsonqlError":e},Object.defineProperty(r,"__esModule",{value:!0})})); +!function(r,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((r=r||self).jsonqlErrors={})}(this,(function(r){"use strict";var t=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 406},e.name.get=function(){return"Jsonql406Error"},Object.defineProperties(t,e),t}(Error),e=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 500},e.name.get=function(){return"Jsonql500Error"},Object.defineProperties(t,e),t}(Error),o=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 401},e.name.get=function(){return"JsonqlAuthorisationError"},Object.defineProperties(t,e),t}(Error),n=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 401},e.name.get=function(){return"JsonqlContractAuthError"},Object.defineProperties(t,e),t}(Error),a=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 500},e.name.get=function(){return"JsonqlResolverAppError"},Object.defineProperties(t,e),t}(Error),s=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 404},e.name.get=function(){return"JsonqlResolverNotFoundError"},Object.defineProperties(t,e),t}(Error),c=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0}};return e.name.get=function(){return"JsonqlEnumError"},Object.defineProperties(t,e),t}(Error),i=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0}};return e.name.get=function(){return"JsonqlTypeError"},Object.defineProperties(t,e),t}(Error),u=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0}};return e.name.get=function(){return"JsonqlCheckerError"},Object.defineProperties(t,e),t}(Error),p=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0}};return e.name.get=function(){return"JsonqlValidationError"},Object.defineProperties(t,e),t}(Error),l="No message",f=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0},statusCode:{configurable:!0}};return e.name.get=function(){return"JsonqlError"},e.statusCode.get=function(){return-1},Object.defineProperties(t,e),t}(Error),h=function(r){function t(e,o){r.call(this,o),this.statusCode=e,this.className=t.name}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0}};return e.name.get=function(){return"JsonqlServerError"},Object.defineProperties(t,e),t}(Error),m=Object.freeze({__proto__:null,Jsonql406Error:t,Jsonql500Error:e,JsonqlAuthorisationError:o,JsonqlContractAuthError:n,JsonqlResolverAppError:a,JsonqlResolverNotFoundError:s,JsonqlEnumError:c,JsonqlTypeError:i,JsonqlCheckerError:u,JsonqlValidationError:p,JsonqlError:f,JsonqlServerError:h}),g=f,E=function(r,t){return!!Object.keys(r).filter((function(r){return t===r})).length};var d="unknown";function y(r,t){var e=function(r,t){return r.filter((function(r){return t instanceof r})).map((function(r){return r.name}))}(r,t);return e.length?e[0]:d}r.JSONQL_ERRORS_INFO="version: 1.1.6 module: umd",r.Jsonql406Error=t,r.Jsonql500Error=e,r.JsonqlAuthorisationError=o,r.JsonqlCheckerError=u,r.JsonqlContractAuthError=n,r.JsonqlEnumError=c,r.JsonqlError=f,r.JsonqlResolverAppError=a,r.JsonqlResolverNotFoundError=s,r.JsonqlServerError=h,r.JsonqlTypeError=i,r.JsonqlValidationError=p,r.UNKNOWN_ERROR=d,r.clientErrorsHandler=function(r){if(E(r,"error")){var t=r.error,e=t.className,o=t.name,n=e||o,a=t.message||l,s=t.detail||t;if(n&&m[n])throw new m[e](a,s);throw new g(a,s)}return r},r.finalCatch=function(r){if(Array.isArray(r))throw new p("",r);var m=r.message||l,g=r.detail||r;switch(!0){case r instanceof t:throw new t(m,g);case r instanceof e:throw new e(m,g);case r instanceof o:throw new o(m,g);case r instanceof n:throw new n(m,g);case r instanceof a:throw new a(m,g);case r instanceof s:throw new s(m,g);case r instanceof c:throw new c(m,g);case r instanceof i:throw new i(m,g);case r instanceof u:throw new u(m,g);case r instanceof p:throw new p(m,g);case r instanceof h:throw new h(m,g);default:throw new f(m,g)}},r.getErrorByStatus=function(r,t){switch(void 0===t&&(t=!1),r){case 401:return t?"JsonqlContractAuthError":"JsonqlAuthorisationError";case 403:return"JsonqlForbiddenError";case 404:return"JsonqlResolverNotFoundError";case 406:return"Jsonql406Error";case 500:return"Jsonql500Error";default:return"JsonqlError"}},r.getErrorNameByInstance=y,r.getErrorNameByInstanceWithDefault=function(r,t){var e=y(r,t);return e===d?"JsonqlError":e},Object.defineProperty(r,"__esModule",{value:!0})})); //# sourceMappingURL=jsonql-errors.cjs.js.map diff --git a/packages/errors/dist/jsonql-errors.umd.js b/packages/errors/dist/jsonql-errors.umd.js index 984a4b69b086db8e5d4050062989aceed178ebef..78a17b8e5f2c09c2cf2b0e63ec3293386ef42c69 100644 --- a/packages/errors/dist/jsonql-errors.umd.js +++ b/packages/errors/dist/jsonql-errors.umd.js @@ -1,2 +1,2 @@ -!function(r,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((r=r||self).jsonqlErrors={})}(this,(function(r){"use strict";var t=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 406},e.name.get=function(){return"Jsonql406Error"},Object.defineProperties(t,e),t}(Error),e=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 500},e.name.get=function(){return"Jsonql500Error"},Object.defineProperties(t,e),t}(Error),o=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 401},e.name.get=function(){return"JsonqlAuthorisationError"},Object.defineProperties(t,e),t}(Error),n=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 401},e.name.get=function(){return"JsonqlContractAuthError"},Object.defineProperties(t,e),t}(Error),a=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 500},e.name.get=function(){return"JsonqlResolverAppError"},Object.defineProperties(t,e),t}(Error),c="undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{},s=function(){try{if(window||document)return!0}catch(r){}return!1},i=function(){try{if(!s()&&c)return!0}catch(r){}return!1};var u=function(r){function t(){for(var t=[],e=arguments.length;e--;)t[e]=arguments[e];r.apply(this,t)}return r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t,t.where=function(){return s()?"browser":i()?"node":"unknown"},t}(Error),p=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,Error.captureStackTrace&&Error.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 404},e.name.get=function(){return"JsonqlResolverNotFoundError"},Object.defineProperties(t,e),t}(u),l=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0}};return e.name.get=function(){return"JsonqlEnumError"},Object.defineProperties(t,e),t}(Error),f=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0}};return e.name.get=function(){return"JsonqlTypeError"},Object.defineProperties(t,e),t}(Error),h=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0}};return e.name.get=function(){return"JsonqlCheckerError"},Object.defineProperties(t,e),t}(Error),m=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,Error.captureStackTrace&&Error.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0}};return e.name.get=function(){return"JsonqlValidationError"},Object.defineProperties(t,e),t}(u),d="No message",g=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,Error.captureStackTrace&&Error.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0},statusCode:{configurable:!0}};return e.name.get=function(){return"JsonqlError"},e.statusCode.get=function(){return-1},Object.defineProperties(t,e),t}(u),E=function(r){function t(e,o){r.call(this,o),this.statusCode=e,this.className=t.name}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0}};return e.name.get=function(){return"JsonqlServerError"},Object.defineProperties(t,e),t}(Error),y=Object.freeze({__proto__:null,Jsonql406Error:t,Jsonql500Error:e,JsonqlAuthorisationError:o,JsonqlContractAuthError:n,JsonqlResolverAppError:a,JsonqlResolverNotFoundError:p,JsonqlEnumError:l,JsonqlTypeError:f,JsonqlCheckerError:h,JsonqlValidationError:m,JsonqlError:g,JsonqlServerError:E}),_=g,b=function(r,t){return!!Object.keys(r).filter((function(r){return t===r})).length};var q="unknown";function J(r,t){var e=function(r,t){return r.filter((function(r){return t instanceof r})).map((function(r){return r.name}))}(r,t);return e.length?e[0]:q}r.JSONQL_ERRORS_INFO="version: 1.1.5 module: umd",r.Jsonql406Error=t,r.Jsonql500Error=e,r.JsonqlAuthorisationError=o,r.JsonqlCheckerError=h,r.JsonqlContractAuthError=n,r.JsonqlEnumError=l,r.JsonqlError=g,r.JsonqlResolverAppError=a,r.JsonqlResolverNotFoundError=p,r.JsonqlServerError=E,r.JsonqlTypeError=f,r.JsonqlValidationError=m,r.UNKNOWN_ERROR=q,r.clientErrorsHandler=function(r){if(b(r,"error")){var t=r.error,e=t.className,o=t.name,n=e||o,a=t.message||d,c=t.detail||t;if(n&&y[n])throw new y[e](a,c);throw new _(a,c)}return r},r.finalCatch=function(r){if(Array.isArray(r))throw new m("",r);var c=r.message||d,s=r.detail||r;switch(!0){case r instanceof t:throw new t(c,s);case r instanceof e:throw new e(c,s);case r instanceof o:throw new o(c,s);case r instanceof n:throw new n(c,s);case r instanceof a:throw new a(c,s);case r instanceof p:throw new p(c,s);case r instanceof l:throw new l(c,s);case r instanceof f:throw new f(c,s);case r instanceof h:throw new h(c,s);case r instanceof m:throw new m(c,s);case r instanceof E:throw new E(c,s);default:throw new g(c,s)}},r.getErrorByStatus=function(r,t){switch(void 0===t&&(t=!1),r){case 401:return t?"JsonqlContractAuthError":"JsonqlAuthorisationError";case 403:return"JsonqlForbiddenError";case 404:return"JsonqlResolverNotFoundError";case 406:return"Jsonql406Error";case 500:return"Jsonql500Error";default:return"JsonqlError"}},r.getErrorNameByInstance=J,r.getErrorNameByInstanceWithDefault=function(r,t){var e=J(r,t);return e===q?"JsonqlError":e},Object.defineProperty(r,"__esModule",{value:!0})})); +!function(r,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((r=r||self).jsonqlErrors={})}(this,(function(r){"use strict";var t=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 406},e.name.get=function(){return"Jsonql406Error"},Object.defineProperties(t,e),t}(Error),e=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 500},e.name.get=function(){return"Jsonql500Error"},Object.defineProperties(t,e),t}(Error),o=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 401},e.name.get=function(){return"JsonqlAuthorisationError"},Object.defineProperties(t,e),t}(Error),n=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 401},e.name.get=function(){return"JsonqlContractAuthError"},Object.defineProperties(t,e),t}(Error),a=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 500},e.name.get=function(){return"JsonqlResolverAppError"},Object.defineProperties(t,e),t}(Error),s=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={statusCode:{configurable:!0},name:{configurable:!0}};return e.statusCode.get=function(){return 404},e.name.get=function(){return"JsonqlResolverNotFoundError"},Object.defineProperties(t,e),t}(Error),c=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0}};return e.name.get=function(){return"JsonqlEnumError"},Object.defineProperties(t,e),t}(Error),i=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0}};return e.name.get=function(){return"JsonqlTypeError"},Object.defineProperties(t,e),t}(Error),u=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0}};return e.name.get=function(){return"JsonqlCheckerError"},Object.defineProperties(t,e),t}(Error),p=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0}};return e.name.get=function(){return"JsonqlValidationError"},Object.defineProperties(t,e),t}(Error),l="No message",f=function(r){function t(){for(var e=[],o=arguments.length;o--;)e[o]=arguments[o];r.apply(this,e),this.message=e[0],this.detail=e[1],this.className=t.name,r.captureStackTrace&&r.captureStackTrace(this,t)}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0},statusCode:{configurable:!0}};return e.name.get=function(){return"JsonqlError"},e.statusCode.get=function(){return-1},Object.defineProperties(t,e),t}(Error),h=function(r){function t(e,o){r.call(this,o),this.statusCode=e,this.className=t.name}r&&(t.__proto__=r),t.prototype=Object.create(r&&r.prototype),t.prototype.constructor=t;var e={name:{configurable:!0}};return e.name.get=function(){return"JsonqlServerError"},Object.defineProperties(t,e),t}(Error),m=Object.freeze({__proto__:null,Jsonql406Error:t,Jsonql500Error:e,JsonqlAuthorisationError:o,JsonqlContractAuthError:n,JsonqlResolverAppError:a,JsonqlResolverNotFoundError:s,JsonqlEnumError:c,JsonqlTypeError:i,JsonqlCheckerError:u,JsonqlValidationError:p,JsonqlError:f,JsonqlServerError:h}),g=f,E=function(r,t){return!!Object.keys(r).filter((function(r){return t===r})).length};var d="unknown";function y(r,t){var e=function(r,t){return r.filter((function(r){return t instanceof r})).map((function(r){return r.name}))}(r,t);return e.length?e[0]:d}r.JSONQL_ERRORS_INFO="version: 1.1.6 module: umd",r.Jsonql406Error=t,r.Jsonql500Error=e,r.JsonqlAuthorisationError=o,r.JsonqlCheckerError=u,r.JsonqlContractAuthError=n,r.JsonqlEnumError=c,r.JsonqlError=f,r.JsonqlResolverAppError=a,r.JsonqlResolverNotFoundError=s,r.JsonqlServerError=h,r.JsonqlTypeError=i,r.JsonqlValidationError=p,r.UNKNOWN_ERROR=d,r.clientErrorsHandler=function(r){if(E(r,"error")){var t=r.error,e=t.className,o=t.name,n=e||o,a=t.message||l,s=t.detail||t;if(n&&m[n])throw new m[e](a,s);throw new g(a,s)}return r},r.finalCatch=function(r){if(Array.isArray(r))throw new p("",r);var m=r.message||l,g=r.detail||r;switch(!0){case r instanceof t:throw new t(m,g);case r instanceof e:throw new e(m,g);case r instanceof o:throw new o(m,g);case r instanceof n:throw new n(m,g);case r instanceof a:throw new a(m,g);case r instanceof s:throw new s(m,g);case r instanceof c:throw new c(m,g);case r instanceof i:throw new i(m,g);case r instanceof u:throw new u(m,g);case r instanceof p:throw new p(m,g);case r instanceof h:throw new h(m,g);default:throw new f(m,g)}},r.getErrorByStatus=function(r,t){switch(void 0===t&&(t=!1),r){case 401:return t?"JsonqlContractAuthError":"JsonqlAuthorisationError";case 403:return"JsonqlForbiddenError";case 404:return"JsonqlResolverNotFoundError";case 406:return"Jsonql406Error";case 500:return"Jsonql500Error";default:return"JsonqlError"}},r.getErrorNameByInstance=y,r.getErrorNameByInstanceWithDefault=function(r,t){var e=y(r,t);return e===d?"JsonqlError":e},Object.defineProperty(r,"__esModule",{value:!0})})); //# sourceMappingURL=jsonql-errors.umd.js.map diff --git a/packages/errors/package.json b/packages/errors/package.json index 6822c0c373bbae47c37dc57351cfb2d143799c3b..50d0332f776fe4ae1a100d41acf672f715011526 100644 --- a/packages/errors/package.json +++ b/packages/errors/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-errors", - "version": "1.1.5", + "version": "1.1.6", "description": "Jsonql js error classes use across the projects", "main": "dist/jsonql-errors.cjs.js", "module": "index.js", @@ -33,7 +33,7 @@ "fs-extra": "^8.1.0", "koa": "^2.11.0", "koa-bodyparser": "^4.2.1", - "rollup": "^1.26.3", + "rollup": "^1.26.5", "rollup-plugin-alias": "^2.2.0", "rollup-plugin-async": "^1.2.0", "rollup-plugin-buble": "^0.19.8", @@ -76,6 +76,6 @@ "author": "Joel Chu ", "license": "ISC", "dependencies": { - "jsonql-constants": "^1.8.9" + "jsonql-constants": "^1.8.10" } } diff --git a/packages/errors/src/error.js b/packages/errors/src/error.js index 12a0531d34507cea4bb3c0ca82c3dbc4a7429268..f557828140a1e4e0a734566e9878f83c3738cf5a 100644 --- a/packages/errors/src/error.js +++ b/packages/errors/src/error.js @@ -1,14 +1,14 @@ import { NO_STATUS_CODE } from 'jsonql-constants' -import JsonqlBaseError from './error-base' + /** * This is a custom error to throw whenever a error happen inside the jsonql * This help us to capture the right error, due to the call happens in sequence * @param {string} message to tell what happen * @param {mixed} extra things we want to add, 500? */ -export default class JsonqlError extends JsonqlBaseError { +export default class JsonqlError extends Error { constructor(...args) { - super(...args); + super(...args) this.message = args[0]; this.detail = args[1]; diff --git a/packages/errors/src/general.js b/packages/errors/src/general.js index faff2e3f8ad9525a6fd5dd030fe3263f917202af..ea41e5cc118ee5b6dfc77c7d221ccd9ddaaa062d 100644 --- a/packages/errors/src/general.js +++ b/packages/errors/src/general.js @@ -1,8 +1,7 @@ // this is a new Error class that is not part of the Jsonql // but we will use it in other external modules -import JsonqlBaseError from './error-base' -export default class GeneralError extends JsonqlBaseError { +export default class GeneralError extends Error { constructor(...args) { super(...args); diff --git a/packages/errors/src/resolver-not-found-error.js b/packages/errors/src/resolver-not-found-error.js index 14d8d90d2762c447e4c0f7c9c76826641e823f18..68d7c2480db3f33c26ed987a005485f87aadb707 100644 --- a/packages/errors/src/resolver-not-found-error.js +++ b/packages/errors/src/resolver-not-found-error.js @@ -1,11 +1,10 @@ -import JsonqlBaseError from './error-base' /** * This is a custom error to throw when could not find the resolver * This help us to capture the right error, due to the call happens in sequence * @param {string} message to tell what happen * @param {mixed} extra things we want to add, 500? */ -export default class JsonqlResolverNotFoundError extends JsonqlBaseError { +export default class JsonqlResolverNotFoundError extends Error { constructor(...args) { super(...args); diff --git a/packages/errors/src/validation-error.js b/packages/errors/src/validation-error.js index 420948c91b68ad29db472f0069c2c1eb1b5ec713..968763f7cc9df072b057b38a0ba063d02a009f79 100644 --- a/packages/errors/src/validation-error.js +++ b/packages/errors/src/validation-error.js @@ -1,7 +1,6 @@ // custom validation error class -import JsonqlBaseError from './error-base' // when validaton failed -export default class JsonqlValidationError extends JsonqlBaseError { +export default class JsonqlValidationError extends Error { constructor(...args) { super(...args); diff --git a/packages/errors/src/error-base.js b/packages/errors/tests/helpers/error-base.js similarity index 100% rename from packages/errors/src/error-base.js rename to packages/errors/tests/helpers/error-base.js diff --git a/packages/errors/src/where-am-i.js b/packages/errors/tests/helpers/where-am-i.js similarity index 100% rename from packages/errors/src/where-am-i.js rename to packages/errors/tests/helpers/where-am-i.js diff --git a/packages/koa/main.js b/packages/koa/main.js index c89ffeb8dccec79cba0564ae73ef2208f9453c2f..190d8c7449558df76cf6ca8120ffba4d0224c3fa 100644 --- a/packages/koa/main.js +++ b/packages/koa/main.js @@ -1,6 +1,6 @@ // export for CJS modules using esm module -// @TODO remove later -require('debug')('jsonql-koa:main')('calling the jsonql-koa/main') +// 1.4.10 confirm it's import from this file +// require('debug')('jsonql-koa:main')('calling the jsonql-koa/main') // run the esm require = require('esm')(module) module.exports = require('./index.js') diff --git a/packages/koa/package.json b/packages/koa/package.json index 652d52acf4129be5ec6373e4a7696b56b33fc849..282cfd7d44b416dd2f2b2a8b23e7fed4141c19e6 100644 --- a/packages/koa/package.json +++ b/packages/koa/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-koa", - "version": "1.4.10", + "version": "1.4.11", "description": "jsonql Koa middleware", "main": "main.js", "module": "index.js", @@ -17,7 +17,7 @@ "test:debug": "DEBUG=jsonql* ava --verbose", "coverage": "nyc ava --verbose", "test:basic": "DEBUG=jsonql* ava ./tests/koa.test.js", - "test:notfound": "DEBUG=jsonql-koa* ava --verbose ./tests/resolverNotFound.test.js", + "test:notfound": "DEBUG=jsonql-* ava --verbose ./tests/resolverNotFound.test.js", "test:es6": "DEBUG=jsonql-* ava --verbose ./tests/es6-module.test.js", "test:jwt": "DEBUG=jsonql-koa*,jsonql-jwt* ava ./tests/jwt.test.js", "test:jwt-auth": "DEBUG=jsonql-koa* ava ./tests/jwt-auth.test.js", @@ -75,11 +75,11 @@ "fs-extra": "^8.1.0", "jsonql-constants": "^1.8.10", "jsonql-contract": "^1.8.4", - "jsonql-errors": "^1.1.5", + "jsonql-errors": "^1.1.6", "jsonql-jwt": "^1.3.4", "jsonql-node-client": "^1.2.2", "jsonql-params-validator": "^1.4.11", - "jsonql-resolver": "^0.9.4", + "jsonql-resolver": "^0.9.6", "jsonql-utils": "^0.8.3", "jsonql-web-console": "^0.4.3", "koa": "^2.11.0", diff --git a/packages/koa/src/middlewares/auth-middleware.js b/packages/koa/src/middlewares/auth-middleware.js index 690b968140934b850de2fbd240694baf1e54414d..80532b009bb042d03ab698d3c0a03eddf7aa8e57 100644 --- a/packages/koa/src/middlewares/auth-middleware.js +++ b/packages/koa/src/middlewares/auth-middleware.js @@ -1,5 +1,5 @@ // auth middleware -import { AUTH_TYPE, NOT_FOUND_STATUS } from 'jsonql-constants' +import { NOT_FOUND_STATUS } from 'jsonql-constants' import { JsonqlResolverNotFoundError } from 'jsonql-errors' import { getDebug, @@ -33,7 +33,7 @@ export default function authMiddleware(config) { if (token) { debug('got a token', token) // @BUG 1.4.9 this still throw the error when it shouldn't have - let validatorFn = getValidator(config , AUTH_TYPE, contract) + let validatorFn = getValidator(config , contract) let userdata = await validatorFn(token) debug('validatorFn result', userdata) if (isNotEmpty(userdata) && isObject(userdata)) { diff --git a/packages/koa/src/utils/auth-middleware-helpers.js b/packages/koa/src/utils/auth-middleware-helpers.js index 5793943e4e76ba5cabe3a7b8fa0a565fa7d19219..8b3d1b65c3e46e4063c5967358378333c2869e03 100644 --- a/packages/koa/src/utils/auth-middleware-helpers.js +++ b/packages/koa/src/utils/auth-middleware-helpers.js @@ -7,12 +7,12 @@ import { AUTH_CHECK_HEADER, BEARER } from 'jsonql-constants' +import { createTokenValidator } from 'jsonql-jwt' import { chainFns, getDebug, forbiddenHandler, ctxErrorHandler, - createTokenValidator, // import from the jwt isObject } from '../utils' import { @@ -73,33 +73,49 @@ const createJwtValidatorChain = (config, validator = false) => { return chainFns(jwtFn, validator) } +/** + * wrap this together and try to cache that validator methods + * @param {object} config configuration + * @param {*} localValidator could be nothing or a function + * @return {function} validator + */ +const returnValidator = (config, localValidator) => { + if (config.useJwt) { // we always use the jwt opiton now + validatorFn = createJwtValidatorChain(config, localValidator) + } + else if (typeof localValidator === 'function') { + validatorFn = localValidator + } + else { + throw new JsonqlResolverNotFoundError(`[returnValidator] ${config.validatorHandlerName} NOT FOUND!`) + } + return validatorFn; +} + /** * if useJwt = true then use the jsonql-jwt version * @param {object} config configuration - * @param {string} type type of call * @param {object} contract contract.json * @return {function} the correct handler */ -export const getValidator = (config, type, contract) => { - if (validatorFn && typeof validatorFn === 'function') { - debug(`return the cache validatorFn`) - return validatorFn; - } - let localValidator; +export const getValidator = (config, contract) => { + let localValidator try { - localValidator = getLocalValidator(config, type, contract) + if (validatorFn && typeof validatorFn === 'function') { + debug(`return the cache validatorFn`) + return validatorFn; + } + localValidator = getLocalValidator(config, contract) + + return returnValidator(config, localValidator) } catch(e) { // debug('localValidator throw error', e) - const checkErr = e instanceof JsonqlResolverNotFoundError + const checkErr = e.className === 'JsonqlResolverNotFoundError' + || e instanceof JsonqlResolverNotFoundError if (!checkErr) { debug(`Not the JsonqlResolverNotFoundError?`, e) return finalCatch(e) } + return returnValidator(config, localValidator) } - if (config.useJwt) { // we always use the jwt opiton now - debug(`return the jwt validation chain`) - return createJwtValidatorChain(config, localValidator) - } - debug(`return a local validator`, localValidator) - return localValidator; } diff --git a/packages/koa/src/utils/index.js b/packages/koa/src/utils/index.js index fae38776efca522830a097336dc4fb4477a0e9bc..59872159fe7c0748b9966e166e0c12de275612e3 100644 --- a/packages/koa/src/utils/index.js +++ b/packages/koa/src/utils/index.js @@ -1,6 +1,6 @@ // Just group all the imports here then export them in one go import processJwtKeys from '../options/process-jwt-keys' -import { createTokenValidator } from 'jsonql-jwt' + import { isObject, isArray } from 'jsonql-params-validator' import { getDebug } from './utils' @@ -39,8 +39,7 @@ import { // export export { processJwtKeys, - createTokenValidator, - + getDebug, isObject, diff --git a/packages/koa/tests/resolverNotFound.test.js b/packages/koa/tests/resolverNotFound.test.js index 2cd96953673185ada8c5e2a9a4b03527fa32c7a7..563f5a2b3f20233ea6ca7c9b966b24c1f26c18f0 100644 --- a/packages/koa/tests/resolverNotFound.test.js +++ b/packages/koa/tests/resolverNotFound.test.js @@ -16,7 +16,7 @@ test.after( t => { fsx.removeSync(join(__dirname, 'fixtures', 'tmp', 'notfound')) }) -test('it should throw a ResolverNotFoundError', async (t) => { +test('it should throw a JsonqlResolverNotFoundError', async (t) => { let res = await superkoa(t.context.app) .post('/jsonql') .set(headers) @@ -28,7 +28,9 @@ test('it should throw a ResolverNotFoundError', async (t) => { debug(res.body) - t.is(res.body.error.statusCode, 404) + t.is(res.body.error.className, 'JsonqlResolverNotFoundError') + + // t.is(res.body.error.statusCode, 404) }) // @BUG need fixing diff --git a/packages/resolver/package.json b/packages/resolver/package.json index d1d4a55c6d1e5f98f09fc555fa28a0f94860744d..6860065abdfc62a52a2ed9f03c5b29fcebdd647a 100644 --- a/packages/resolver/package.json +++ b/packages/resolver/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-resolver", - "version": "0.9.4", + "version": "0.9.6", "description": "This is NOT for general use, please do not install it directly. This module is part of the jsonql tools supporting modules.", "main": "index.js", "files": [ @@ -19,7 +19,7 @@ "jsonql", "resolver" ], - "author": "Joel Chu ", + "author": "Joel Chu ", "license": "ISC", "homepage": "jsonql.org", "bugs": { @@ -27,18 +27,18 @@ }, "dependencies": { "debug": "^4.1.1", - "jsonql-constants": "^1.8.3", - "jsonql-errors": "^1.1.3", - "jsonql-jwt": "^1.3.2", - "jsonql-node-client": "^1.1.9", + "jsonql-constants": "^1.8.10", + "jsonql-errors": "^1.1.6", + "jsonql-jwt": "^1.3.4", + "jsonql-node-client": "^1.2.2", "jsonql-params-validator": "^1.4.11", - "jsonql-utils": "^0.6.12", + "jsonql-utils": "^0.8.3", "lodash.merge": "^4.6.2" }, "devDependencies": { "ava": "^2.4.0", - "jsonql-contract": "^1.7.21", - "jsonql-koa": "^1.3.9", + "jsonql-contract": "^1.8.4", + "jsonql-koa": "^1.4.10", "server-io-core": "^1.2.0" }, "ava": { diff --git a/packages/resolver/src/handle-auth-methods.js b/packages/resolver/src/handle-auth-methods.js index 33b3d4431afc31fa8c8eecc6e6f4bc5518626c25..1365c15f9ca4f5416a457e1c5074d1ce522d534f 100644 --- a/packages/resolver/src/handle-auth-methods.js +++ b/packages/resolver/src/handle-auth-methods.js @@ -26,16 +26,18 @@ const debug = getDebug('public-method-middleware') /** * if useJwt = true then use the jsonql-jwt version * @param {object} config configuration - * @param {string} type type of call * @param {object} contract contract.json * @return {function} the correct handler */ -const getLocalValidator = (config, type, contract) => { +const getLocalValidator = (config, contract) => { + const { validatorHandlerName } = config; // if you explicitly set it to false then it will be ignore - if (config.validatorHandlerName === false) { + if (!validatorHandlerName) { return false; } - let validatorFnPath = searchResolvers(config.validatorHandlerName, type, config, contract) + // @BUG when this searchResolvers throw error the next call can not use instanceof to check the Error? + // @TODO there is no need to pass the type here, it will always be the AUTH_TYPE + let validatorFnPath = searchResolvers(validatorHandlerName, AUTH_TYPE, config, contract) return require(validatorFnPath) } diff --git a/packages/resolver/src/resolve-methods.js b/packages/resolver/src/resolve-methods.js index 7d2594c83709f1c9a3b69a38d1b28e86c1b02b2a..39fcb9a4146f9296f2ce205944359b21f6ca030c 100644 --- a/packages/resolver/src/resolve-methods.js +++ b/packages/resolver/src/resolve-methods.js @@ -117,7 +117,7 @@ async function resolverRenderHandler(ctx, type, opts, contract) { if (errorName === UNKNOWN_ERROR) { errorName = 'JsonqlResolverAppError' } - + // @BUG the JsonqlResolverNotFoundError should have a statusCode but disappeared??? return ctxErrorHandler(ctx, errorName, e) } } diff --git a/packages/resolver/src/search-resolvers.js b/packages/resolver/src/search-resolvers.js index 0a533b4d8e0298331f1e7b4438652874116cc9e8..99517855a7df5318a60c7615ff4f696c9f00b034 100644 --- a/packages/resolver/src/search-resolvers.js +++ b/packages/resolver/src/search-resolvers.js @@ -1,7 +1,7 @@ // search for the resolver location const { join } = require('path') -const { JsonqlResolverNotFoundError } = require('jsonql-errors') +const { JsonqlResolverNotFoundError, JsonqlError } = require('jsonql-errors') const { getPathToFn, findFromContract } = require('jsonql-utils') const { DEFAULT_RESOLVER_IMPORT_FILE_NAME } = require('jsonql-constants') @@ -41,9 +41,9 @@ function requireEsModule(pathToResolver) { } else if (obj.default && typeof obj.default === 'function') { return obj.default; } - throw new Error(`Unable to import ES module!`) + throw new JsonqlError(`Unable to import ES module!`) } catch (e) { - throw new Error(e) + throw new JsonqlError(e) } finally { // reset the require here? require = oldRequire; // reset @@ -69,7 +69,7 @@ function searchResolvers(name, type, opts, contract) { if (pathToResolver !== false) { return pathToResolver; } - debug(`contract is not contract?`, contract) + debug(`pathToResolver not found in contract`, contract) // search by running pathToResolver = getPathToFn(name, type, opts) if (pathToResolver) { @@ -77,9 +77,10 @@ function searchResolvers(name, type, opts, contract) { } const debugMsg = `${name} not found! [${opts.name}]`; debug('JsonqlResolverNotFoundError', debugMsg) + // @TODO why the hell when throw from here the instanceof is not working??? throw new JsonqlResolverNotFoundError(prod ? 'NOT FOUND!' : debugMsg) } catch(e) { - debug(`throw again?`, e.name) + // debug(`throw again?`, JsonqlResolverNotFoundError) throw new JsonqlResolverNotFoundError(e) } } diff --git a/packages/resolver/tests/fixtures/contract/es/contract.json b/packages/resolver/tests/fixtures/contract/es/contract.json index 8f2d3dd8248988c111d5369fab1e1ea6de366ccb..3f9854b7b5289892e7fe4c840030e809fba0b66d 100644 --- a/packages/resolver/tests/fixtures/contract/es/contract.json +++ b/packages/resolver/tests/fixtures/contract/es/contract.json @@ -43,6 +43,6 @@ } }, "auth": {}, - "timestamp": 1570635866, + "timestamp": 1573458006, "sourceType": "module" } diff --git a/packages/resolver/tests/throw.test.js b/packages/resolver/tests/throw.test.js index b485899464e46cdc3202d0c348e69d8d60325b30..961d918f83388f8e9e7554a9474bfdf0c53e646a 100644 --- a/packages/resolver/tests/throw.test.js +++ b/packages/resolver/tests/throw.test.js @@ -1,5 +1,6 @@ // testing the throw error problem const test = require('ava') +const debug = require('debug')('jsonql-resolver:test:throw') const { JsonqlResolverNotFoundError, JsonqlAuthorisationError, @@ -10,7 +11,7 @@ const { } = require('jsonql-errors') const { join } = require('path') -const { executeResolver } = require('../') +const { executeResolver, getLocalValidator } = require('../') const { createQuery } = require('jsonql-utils') const readJson = require('./fixtures/read-json') @@ -48,7 +49,18 @@ test.cb(`When resolver throw error, it should able to throw back a JsonqlResolve t.is(errorName, UNKNOWN_ERROR) t.end() - + }) +}) + +test(`The getLocalValidator should throw a JsonqlResolverNotFoundError`, t => { + + const error = t.throws(() => { + return getLocalValidator({ + validatorHandlerName: 'validator' + }, t.context.contract) + }, JsonqlResolverNotFoundError, 'There is no validator and should throw error') + + debug(error) })