diff --git a/packages/contract-cli/package.json b/packages/contract-cli/package.json index d97879a1b9e1e3fed7ec9eb1cb900a52dc6df425..0d66842bc7ca8dbbe65d8e134fa6e608cba9897c 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.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 aa9a2878b30b54921c09e46d16dc96c29824a5cc..8452614afbd410c6ee35fee42ee7b9529f78aa6c 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 = '>'; -// 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) /** @@ -127,7 +129,7 @@ const explainSync = function(source) { * @return {object} clean result for contract */ const processParams = function(params) { - if (Array.isArray(params)) { + if (params && Array.isArray(params)) { return params.map(param => { // always return array from now on param.type = param.type.names.map(normalizeType) @@ -138,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 @@ -156,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/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 0a08679841738e3b27e20b7a9553771869704a97..cf2af93c8d5e5565cbd84d2314ca03b8f2693721 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/koa/README.md b/packages/koa/README.md index 01f4fdcab9c25cf9b7f404d6bf0051f6706815a1..a39ead37c5e9c477571f14bf4f99f1a2c6695906 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") } ``` diff --git a/packages/koa/package.json b/packages/koa/package.json index c29ef0e29f0b658bd97fbeac8951b76b9c27f508..e489d33521c90a5353c8f6411279a119cc594bad 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", @@ -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" }, 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 diff --git a/packages/resolver/package.json b/packages/resolver/package.json index 653143be24b5f72a155cabf06ea25ddf13792b8f..6d088c06974d87b3ead0800598fcf0f36fe0e0d9 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", @@ -26,15 +27,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/src/client/clients-generator.js b/packages/resolver/src/client/clients-generator.js index 0c33d5de0ce818e2d9966ca04bb4eb4f9929eae5..bc6fcd5b058c01a65638a8ca974fbaebc825dab0 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 62defabc87bdb454f4f0448bdd9621d0be4b387a..d2baaf5b30ca4e3ccb5c0fbc88d2f4763c20fb34 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 6c1aaeb2d4dbd3277e071ef8d027847f9ebb3586..df15b81aa6e421168aa09680df554f80c3096b47 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 d96067ec8e21292c508420f30befe4fc037fef6c..1558d2179bd65064449556d8f2520742a02fbc85 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 6b73a38efed78517c966eb053c768a82c6b2a682..38d416a96af23465a44f10cc4ea789e3abdb01e8 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 0faec158ac25208c456e380f5f10acb67622819a..7ffc907d098ed938198ac0e3c4242e97b6fc4010 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 10ea6a5af8fb23b816d62836349b851d9c912ab8..9df112cfdbb7524527b220d6cb2d930887ee3a6c 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 3f1df90f19a6ce317b38197e2a8a9377c6d66529..3ea03c0206ffc90e21f2937e8479e58289fb95ce 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 254a6963f325e89a179ff0b2f890d84a101ae723..16c7996ce07d3856f8eef9cf969b137b00790887 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 } diff --git a/packages/resolver/tests/fixtures/another-server.js b/packages/resolver/tests/fixtures/another-server.js index eb29589994f91bb5ffa3465b44f04aaf3f4ed4fb..13b109c845585c2238a94f19dc5070a2b3704652 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') }) 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 0000000000000000000000000000000000000000..ad66613efc4034cc1664a2900a8266f295496cdc --- /dev/null +++ b/packages/resolver/tests/fixtures/resolvers/query/to-fail.js @@ -0,0 +1,11 @@ +/** + * This resolver will fail and throw an error + * @return a variable that didn't exist + */ +module.exports = function toFail() { + try { + return 1; + } catch(e) { + throw new Error(e) + } +} diff --git a/packages/resolver/tests/throw.test.js b/packages/resolver/tests/throw.test.js new file mode 100644 index 0000000000000000000000000000000000000000..7d52b6a63d277585439fdf692bf6a49c21d16edc --- /dev/null +++ b/packages/resolver/tests/throw.test.js @@ -0,0 +1,23 @@ +// testing the throw error problem +const test = require('ava') +const { JsonqlResolverAppError } = require('jsonql-errors') + +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 => { + +})