From dd3284f95257e243d0c6cba6c86e81489ee5c776 Mon Sep 17 00:00:00 2001 From: joelchu Date: Thu, 3 Oct 2019 16:32:14 +0800 Subject: [PATCH 1/9] update jsonql-koa deps and the error seems to gone away but need to do individual test --- packages/koa/package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/koa/package.json b/packages/koa/package.json index c29ef0e2..2df467b1 100644 --- a/packages/koa/package.json +++ b/packages/koa/package.json @@ -72,15 +72,15 @@ "esm": "^3.2.25", "fs-extra": "^8.1.0", "jsonql-constants": "^1.8.3", - "jsonql-contract": "^1.7.15", + "jsonql-contract": "^1.7.16", "jsonql-errors": "^1.1.3", "jsonql-jwt": "^1.3.2", "jsonql-node-client": "^1.1.9", "jsonql-params-validator": "^1.4.11", "jsonql-resolver": "^0.8.7", - "jsonql-utils": "^0.6.10", + "jsonql-utils": "^0.6.12", "jsonql-web-console": "^0.4.3", - "koa": "^2.8.1", + "koa": "^2.8.2", "koa-compose": "^4.1.0", "lodash": "^4.17.15" }, -- Gitee From 3fc3a2dc6753b459a1d2468d9a02f9a9f0c52628 Mon Sep 17 00:00:00 2001 From: joelchu Date: Thu, 3 Oct 2019 18:17:54 +0800 Subject: [PATCH 2/9] seems to fix that error now --- packages/koa/package.json | 2 +- packages/koa/tests/{contract._test_.js => contract.test.js} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename packages/koa/tests/{contract._test_.js => contract.test.js} (100%) diff --git a/packages/koa/package.json b/packages/koa/package.json index 2df467b1..e489d335 100644 --- a/packages/koa/package.json +++ b/packages/koa/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-koa", - "version": "1.3.9", + "version": "1.3.10", "description": "jsonql Koa middleware", "main": "main.js", "module": "index.js", diff --git a/packages/koa/tests/contract._test_.js b/packages/koa/tests/contract.test.js similarity index 100% rename from packages/koa/tests/contract._test_.js rename to packages/koa/tests/contract.test.js -- Gitee From 4b64adcc2623279b7e19878bdf10e636792a0b22 Mon Sep 17 00:00:00 2001 From: joelchu Date: Thu, 3 Oct 2019 18:33:58 +0800 Subject: [PATCH 3/9] update readme --- packages/koa/README.md | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/koa/README.md b/packages/koa/README.md index 01f4fdca..a39ead37 100644 --- a/packages/koa/README.md +++ b/packages/koa/README.md @@ -5,9 +5,9 @@ > This is the jsonql middleware previously published as jsonql-koa, and completely rewritten with ES6 -## BREAKING CHANGE @ V1.4.0 +## BREAKING CHANGE @ V1.3.10 -The module now use named export `jsonqlKoa` +The module use named export `jsonqlKoa`, this will affect all existing code base. Please change accordingly. ```js import { jsonqlKoa } from 'jsonql-koa' @@ -33,6 +33,8 @@ $ yarn add jsonql-koa | ----------- |:----------------------| :------------:| :--------------| | resolversDir | Where the resolvers is | `String` | `join(process.cwd(), 'resolvers')` | | contractDir | Where to store the contract | `String` | `join(process.cwd(), 'contract')` | +| enableAuth | if you need to use jwt authorisation | `Boolean` | `false` | + More options to come later @@ -42,13 +44,13 @@ Also you need to install middleware to parse the JSON content. Here we use [koa-bodyparser](https://github.com/koajs/bodyparser). ```js -const Koa = require('koa'); -const { jsonqlKoa } = require('jsonql-koa'); -const bodyparser = require('koa-bodyparser'); -const { join } = require('path'); +const Koa = require('koa') +const { jsonqlKoa } = require('jsonql-koa') +const bodyparser = require('koa-bodyparser') +const { join } = require('path') -const app = new Koa(); -app.use(bodyparser()); +const app = new Koa() +app.use(bodyparser()) app.use(jsonqlKoa({ resolverDir: join(__dirname, 'resolvers') // this is the default value })) @@ -86,7 +88,7 @@ And your resolver should look something like this: * @return {object} modified bunch of stuff */ module.exports = function(params) { - params.modified = parseInt((new Date()).getTime()/1000, 10); + params.modified = parseInt((new Date()).getTime()/1000, 10) // some more things with your params return params; } @@ -149,7 +151,7 @@ For example you have this resolver: * @return {string} UTC Date */ module.exports = function() { - return Date.UTC(); + return Date.UTC() } ``` @@ -163,7 +165,7 @@ If you throw an error inside your function call: * @return {object} just throw */ module.exports = function() { - throw new Error("I don't know"); + throw new Error("I don't know") } ``` -- Gitee From cec51b817059f3f492c49edd2e963cad808bc20d Mon Sep 17 00:00:00 2001 From: joelchu Date: Fri, 4 Oct 2019 15:27:24 +0800 Subject: [PATCH 4/9] changing all the export interfaces to named --- packages/resolver/src/client/clients-generator.js | 4 +++- packages/resolver/src/client/index.js | 6 +++--- packages/resolver/src/client/inject-node-clients.js | 4 +++- packages/resolver/src/client/validate-client-config.js | 2 +- packages/resolver/src/handle-auth-methods.js | 4 ++-- packages/resolver/src/provide-node-clients.js | 4 +++- packages/resolver/src/resolve-methods.js | 8 ++++---- packages/resolver/src/search-resolvers.js | 4 +++- packages/resolver/src/validate-and-call.js | 4 +++- 9 files changed, 25 insertions(+), 15 deletions(-) diff --git a/packages/resolver/src/client/clients-generator.js b/packages/resolver/src/client/clients-generator.js index 0c33d5de..bc6fcd5b 100755 --- a/packages/resolver/src/client/clients-generator.js +++ b/packages/resolver/src/client/clients-generator.js @@ -7,7 +7,7 @@ const jsonqlNodeClient = require('jsonql-node-client') * @param {object} opts the clients configuration * @return {promise} resolve to the node client */ -module.exports = function(opts) { +function clientsGenerator(opts) { return Promise.all( opts.map(opt => { let name = opt.name; @@ -19,3 +19,5 @@ module.exports = function(opts) { }) ) } + +module.exports = { clientsGenerator } diff --git a/packages/resolver/src/client/index.js b/packages/resolver/src/client/index.js index 62defabc..d2baaf5b 100755 --- a/packages/resolver/src/client/index.js +++ b/packages/resolver/src/client/index.js @@ -1,7 +1,7 @@ // import export -const injectNodeClient = require('./inject-node-clients') -const validateClientConfig = require('./validate-client-config') -const clientsGenerator = require('./clients-generator') +const { injectNodeClient } = require('./inject-node-clients') +const { validateClientConfig } = require('./validate-client-config') +const { clientsGenerator } = require('./clients-generator') // export module.exports = { injectNodeClient, diff --git a/packages/resolver/src/client/inject-node-clients.js b/packages/resolver/src/client/inject-node-clients.js index 6c1aaeb2..df15b81a 100644 --- a/packages/resolver/src/client/inject-node-clients.js +++ b/packages/resolver/src/client/inject-node-clients.js @@ -39,6 +39,8 @@ function resolveClients(clients) { * @param {array} clients the jsonql node clients * @return {function} the injected resolver */ -module.exports = function injectNodeClient(resolver, clients) { +function injectNodeClient(resolver, clients) { return injectToFn(resolver, CLIENT_PROP_NAME, resolveClients(clients)) } + +module.exports = { injectNodeClient } diff --git a/packages/resolver/src/client/validate-client-config.js b/packages/resolver/src/client/validate-client-config.js index d96067ec..1558d217 100644 --- a/packages/resolver/src/client/validate-client-config.js +++ b/packages/resolver/src/client/validate-client-config.js @@ -38,4 +38,4 @@ const validateClientConfig = function(config) { return false; } -module.exports = validateClientConfig +module.exports = { validateClientConfig } diff --git a/packages/resolver/src/handle-auth-methods.js b/packages/resolver/src/handle-auth-methods.js index 6b73a38e..38d416a9 100644 --- a/packages/resolver/src/handle-auth-methods.js +++ b/packages/resolver/src/handle-auth-methods.js @@ -1,7 +1,7 @@ // Auth methods handler const { getDebug } = require('./utils') -const searchResolvers = require('./search-resolvers') -const validateAndCall = require('./validate-and-call') +const { searchResolvers } = require('./search-resolvers') +const { validateAndCall } = require('./validate-and-call') const { handleOutput, packResult, ctxErrorHandler } = require('jsonql-utils') const { diff --git a/packages/resolver/src/provide-node-clients.js b/packages/resolver/src/provide-node-clients.js index 0faec158..7ffc907d 100644 --- a/packages/resolver/src/provide-node-clients.js +++ b/packages/resolver/src/provide-node-clients.js @@ -15,7 +15,7 @@ let hasClientConfig; * @param {object} config configuration * @return {function} the resolver with injection if any */ -module.exports = async function provideNodeClients(resolver, config) { +async function provideNodeClients(resolver, config) { if (hasClientConfig === false) { debug(`nothing to inject`) return resolver; // nothing to do @@ -35,3 +35,5 @@ module.exports = async function provideNodeClients(resolver, config) { clients = await clientsGenerator(hasClientConfig) return injectNodeClient(resolver, clients) } + +module.exports = { provideNodeClients } diff --git a/packages/resolver/src/resolve-methods.js b/packages/resolver/src/resolve-methods.js index 10ea6a5a..9df112cf 100644 --- a/packages/resolver/src/resolve-methods.js +++ b/packages/resolver/src/resolve-methods.js @@ -22,9 +22,9 @@ const { extractArgsFromPayload } = require('jsonql-utils') const { getDebug } = require('./utils') -const searchResolvers = require('./search-resolvers') -const validateAndCall = require('./validate-and-call') -const provideNodeClients = require('./provide-node-clients') +const { searchResolvers } = require('./search-resolvers') +const { validateAndCall } = require('./validate-and-call') +const { provideNodeClients } = require('./provide-node-clients') const debug = getDebug('resolve-method') @@ -39,7 +39,7 @@ const debug = getDebug('resolve-method') function importFromModule(resolverDir, type, resolverName) { debug('[importFromModule]', resolverDir, type, resolverName) const resolvers = require( join(resolverDir, DEFAULT_RESOLVER_IMPORT_FILE_NAME) ) - return resolvers[type + resolverName] + return resolvers[ [type, resolverName].join('') ] } /** diff --git a/packages/resolver/src/search-resolvers.js b/packages/resolver/src/search-resolvers.js index 3f1df90f..3ea03c02 100644 --- a/packages/resolver/src/search-resolvers.js +++ b/packages/resolver/src/search-resolvers.js @@ -20,7 +20,7 @@ const prod = process.env.NODE_ENV === 'production'; * @param {object} contract full version * @return {string} the path to function */ -module.exports = function searchResolvers(name, type, opts, contract) { +function searchResolvers(name, type, opts, contract) { try { const json = typeof contract === 'string' ? JSON.parse(contract) : contract; const search = findFromContract(type, name, json) @@ -41,3 +41,5 @@ module.exports = function searchResolvers(name, type, opts, contract) { throw new JsonqlResolverNotFoundError(e) } } + +module.exports = { searchResolvers } diff --git a/packages/resolver/src/validate-and-call.js b/packages/resolver/src/validate-and-call.js index 254a6963..16c7996c 100644 --- a/packages/resolver/src/validate-and-call.js +++ b/packages/resolver/src/validate-and-call.js @@ -52,7 +52,7 @@ const applyJwtMethod = (type, name, opts, contract) => { * @param {object} opts configuration option to use in the future * @return {object} now return a promise that resolve whatever the resolver is going to return and packed */ -module.exports = function validateAndCall(fn, args, contract, type, name, opts) { +function validateAndCall(fn, args, contract, type, name, opts) { const { params } = extractParamsFromContract(contract, type, name) let errors = validateSync(args, params) if (errors.length) { @@ -69,3 +69,5 @@ module.exports = function validateAndCall(fn, args, contract, type, name, opts) throw e; }) */ } + +module.exports = { validateAndCall } -- Gitee From 90b55700f8d98964e5694f41762df79f6696fc27 Mon Sep 17 00:00:00 2001 From: joelchu Date: Fri, 4 Oct 2019 15:27:35 +0800 Subject: [PATCH 5/9] add throw test --- packages/resolver/tests/throw.test.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 packages/resolver/tests/throw.test.js diff --git a/packages/resolver/tests/throw.test.js b/packages/resolver/tests/throw.test.js new file mode 100644 index 00000000..d559ec40 --- /dev/null +++ b/packages/resolver/tests/throw.test.js @@ -0,0 +1,11 @@ +// testing the throw error problem +const test = require('ava') +const { JsonqlResolverAppError } = require('jsonql-errors') +const { + executeResolver, + resolverRenderHandler +} = require('../src/resolve-methods') + +test.skip(`When resolver throw error, it should able to throw back a JsonqlResolverAppError`, t => { + +}) -- Gitee From 7dc4c95b5ac0e396d5116441e414f7a2154fe3a1 Mon Sep 17 00:00:00 2001 From: joelchu Date: Fri, 4 Oct 2019 15:35:35 +0800 Subject: [PATCH 6/9] The old test passed now start updating the internal --- packages/resolver/tests/fixtures/another-server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/resolver/tests/fixtures/another-server.js b/packages/resolver/tests/fixtures/another-server.js index eb295899..13b109c8 100644 --- a/packages/resolver/tests/fixtures/another-server.js +++ b/packages/resolver/tests/fixtures/another-server.js @@ -1,7 +1,7 @@ // this is the ms run on 8001 const serverIoCore = require('server-io-core') -const koa = require('../../../koa') +const { jsonqlKoa } = require('../../../koa') const { join } = require('path') // export the return for use later module.exports = () => serverIoCore({ @@ -11,7 +11,7 @@ module.exports = () => serverIoCore({ open: false, socket: false, middlewares: [ - koa({ + jsonqlKoa({ resolverDir: join(__dirname, 'resolvers'), contractDir: join(__dirname, 'contract', 'another') }) -- Gitee From b6140e1fdf97ca43ac84f174b2db204905ee9519 Mon Sep 17 00:00:00 2001 From: joelchu Date: Fri, 4 Oct 2019 15:51:42 +0800 Subject: [PATCH 7/9] add a to-fail resolver --- .../tests/fixtures/resolvers/query/to-fail.js | 7 ++++++ packages/resolver/tests/throw.test.js | 22 ++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 packages/resolver/tests/fixtures/resolvers/query/to-fail.js diff --git a/packages/resolver/tests/fixtures/resolvers/query/to-fail.js b/packages/resolver/tests/fixtures/resolvers/query/to-fail.js new file mode 100644 index 00000000..74a03b62 --- /dev/null +++ b/packages/resolver/tests/fixtures/resolvers/query/to-fail.js @@ -0,0 +1,7 @@ +/** + * This resolver will fail and throw an error + * @return a variable that didn't exist + */ +module.exports = function toFail() { + return varNotExist; +} diff --git a/packages/resolver/tests/throw.test.js b/packages/resolver/tests/throw.test.js index d559ec40..7d52b6a6 100644 --- a/packages/resolver/tests/throw.test.js +++ b/packages/resolver/tests/throw.test.js @@ -1,11 +1,23 @@ // testing the throw error problem const test = require('ava') const { JsonqlResolverAppError } = require('jsonql-errors') -const { - executeResolver, - resolverRenderHandler -} = require('../src/resolve-methods') -test.skip(`When resolver throw error, it should able to throw back a JsonqlResolverAppError`, t => { +const { join } = require('path') +const { executeResolver } = require('../') +const { createQuery } = require('jsonql-utils') +const readJson = require('./fixtures/read-json') + +const contractPath = join(__dirname, 'fixtures', 'contract', 'contract.json') + +test.before(async t => { + t.context.contract = await readJson(contractPath) + t.context.opts = { + resolverDir: join(__dirname, 'fixtures', 'resolvers'), + contractDir: join(__dirname, 'fixtures', 'contract') + } +}) + + +test.skip(`When resolver throw error, it should able to throw back a JsonqlResolverAppError`, async t => { }) -- Gitee From b4b327bffa655055afb8e0b054c7d1fd2daa6f51 Mon Sep 17 00:00:00 2001 From: joelchu Date: Fri, 4 Oct 2019 16:05:25 +0800 Subject: [PATCH 8/9] another strange error from the jsonql-contract --- packages/contract-cli/package.json | 2 +- packages/contract-cli/src/ast/jsdoc.js | 3 ++- .../koa-resolvers/query/public/always-available.js | 2 +- packages/resolver/package.json | 8 ++++---- .../resolver/tests/fixtures/resolvers/query/to-fail.js | 6 +++++- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/contract-cli/package.json b/packages/contract-cli/package.json index d97879a1..4ee316a7 100755 --- a/packages/contract-cli/package.json +++ b/packages/contract-cli/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-contract", - "version": "1.7.16", + "version": "1.7.17", "description": "JS API / command line tool to generate the contract.json for jsonql", "main": "index.js", "files": [ diff --git a/packages/contract-cli/src/ast/jsdoc.js b/packages/contract-cli/src/ast/jsdoc.js index aa9a2878..61bf06c1 100644 --- a/packages/contract-cli/src/ast/jsdoc.js +++ b/packages/contract-cli/src/ast/jsdoc.js @@ -18,7 +18,7 @@ const { keyBy, some, result, groupBy, size, indexOf } = require('lodash') const OBJECT_TYPE = 'object'; const LFT = 'array.<'; const RHT = '>'; -// smaller helper to output all the debug code in details +// small helper to output all the debug code in details const detailOut = code => inspect(code, false, null) /** @@ -128,6 +128,7 @@ const explainSync = function(source) { */ const processParams = function(params) { if (Array.isArray(params)) { + debug(params) return params.map(param => { // always return array from now on param.type = param.type.names.map(normalizeType) diff --git a/packages/contract-cli/tests/fixtures/koa-resolvers/query/public/always-available.js b/packages/contract-cli/tests/fixtures/koa-resolvers/query/public/always-available.js index 0a086798..cf2af93c 100644 --- a/packages/contract-cli/tests/fixtures/koa-resolvers/query/public/always-available.js +++ b/packages/contract-cli/tests/fixtures/koa-resolvers/query/public/always-available.js @@ -4,4 +4,4 @@ */ module.exports = function() { return 'Hello there'; -}; +} diff --git a/packages/resolver/package.json b/packages/resolver/package.json index 653143be..b6fd70ff 100644 --- a/packages/resolver/package.json +++ b/packages/resolver/package.json @@ -26,15 +26,15 @@ "jsonql-constants": "^1.8.3", "jsonql-errors": "^1.1.3", "jsonql-jwt": "^1.3.2", - "jsonql-node-client": "^1.1.8", + "jsonql-node-client": "^1.1.9", "jsonql-params-validator": "^1.4.11", - "jsonql-utils": "^0.6.10", + "jsonql-utils": "^0.6.12", "lodash.merge": "^4.6.2" }, "devDependencies": { "ava": "^2.4.0", - "jsonql-contract": "^1.7.8", - "jsonql-koa": "^1.3.8", + "jsonql-contract": "^1.7.16", + "jsonql-koa": "^1.3.9", "server-io-core": "^1.2.0" }, "ava": { diff --git a/packages/resolver/tests/fixtures/resolvers/query/to-fail.js b/packages/resolver/tests/fixtures/resolvers/query/to-fail.js index 74a03b62..ad66613e 100644 --- a/packages/resolver/tests/fixtures/resolvers/query/to-fail.js +++ b/packages/resolver/tests/fixtures/resolvers/query/to-fail.js @@ -3,5 +3,9 @@ * @return a variable that didn't exist */ module.exports = function toFail() { - return varNotExist; + try { + return 1; + } catch(e) { + throw new Error(e) + } } -- Gitee From 288fad6a42515d1701249421a9ab7327f343140c Mon Sep 17 00:00:00 2001 From: joelchu Date: Fri, 4 Oct 2019 22:45:39 +0800 Subject: [PATCH 9/9] The strange problem comes back, the strangest part is - the code is correct to check if the res.params is something then short it to the false or empty array, but some how the check is passed the first call which cause the error --- packages/contract-cli/package.json | 2 +- packages/contract-cli/src/ast/jsdoc.js | 25 +++++++++++++++++++------ packages/resolver/package.json | 5 +++-- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/packages/contract-cli/package.json b/packages/contract-cli/package.json index 4ee316a7..0d66842b 100755 --- a/packages/contract-cli/package.json +++ b/packages/contract-cli/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-contract", - "version": "1.7.17", + "version": "1.7.18", "description": "JS API / command line tool to generate the contract.json for jsonql", "main": "index.js", "files": [ diff --git a/packages/contract-cli/src/ast/jsdoc.js b/packages/contract-cli/src/ast/jsdoc.js index 61bf06c1..8452614a 100644 --- a/packages/contract-cli/src/ast/jsdoc.js +++ b/packages/contract-cli/src/ast/jsdoc.js @@ -18,7 +18,9 @@ const { keyBy, some, result, groupBy, size, indexOf } = require('lodash') const OBJECT_TYPE = 'object'; const LFT = 'array.<'; const RHT = '>'; -// small helper to output all the debug code in details +/** + * small helper to output all the debug code in details + */ const detailOut = code => inspect(code, false, null) /** @@ -127,8 +129,7 @@ const explainSync = function(source) { * @return {object} clean result for contract */ const processParams = function(params) { - if (Array.isArray(params)) { - debug(params) + if (params && Array.isArray(params)) { return params.map(param => { // always return array from now on param.type = param.type.names.map(normalizeType) @@ -139,6 +140,16 @@ const processParams = function(params) { return false } +/** + * Taken out from the code below + * @param {object} res from jsdoc + * @return {*} false on nothing + */ +const getParams = res => ( + res.params ? ( foldParams( processParams(res.params) ) || false ) + : ( res.undocumented ? false : [] ) +) + /** * Take the output then search for the comment we need * @param {object} output parsed source @@ -157,14 +168,16 @@ const search = function(output, name = '') { return res.longname === 'module.exports' }).map(res => { let resolverName = res.meta.code.value || res.meta.code.name + debug(`----------------${resolverName}---------------------`) debug('res.meta', detailOut(res.meta)) - debug('res.params', detailOut(res.params)) + debug('res.params', detailOut(res.params), res.params) + return { name: resolverName, description: res.description || false, - params: res.params ? ( foldParams( processParams(res.params) ) || false ) - : ( res.undocumented ? false : [] ), + // @BUG The strange bug happens here, it should not call the processed but still calling it??? + params: getParams(res), returns: processParams(res.returns) || false } }).reduce((first, next) => { diff --git a/packages/resolver/package.json b/packages/resolver/package.json index b6fd70ff..6d088c06 100644 --- a/packages/resolver/package.json +++ b/packages/resolver/package.json @@ -10,10 +10,11 @@ "scripts": { "test": "ava --verbose", "test:clients": "DEBUG=jsonql* ava --verbose ./tests/clients.test.js", - "contract": "jsonql-contract create ./tests/fixtures/resolvers ./tests/fixtures/contract" + "contract": "DEBUG=jsonql-contract* jsonql-contract create ./tests/fixtures/resolvers ./tests/fixtures/contract" }, "keywords": [ - "jsonql" + "jsonql", + "resolver" ], "author": "Joel Chu ", "license": "ISC", -- Gitee