From ceee3b00a79a4fcee8f0d848243b3cb693cad1cf Mon Sep 17 00:00:00 2001 From: joelchu Date: Tue, 3 Mar 2020 11:31:29 +0800 Subject: [PATCH 1/3] add default test to check the appDir option --- packages/@jsonql/koa/package.json | 15 ++++---- packages/@jsonql/koa/tests/default.test.js | 40 ++++++++++++++++++++++ 2 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 packages/@jsonql/koa/tests/default.test.js diff --git a/packages/@jsonql/koa/package.json b/packages/@jsonql/koa/package.json index d1b90820..5d741a0b 100644 --- a/packages/@jsonql/koa/package.json +++ b/packages/@jsonql/koa/package.json @@ -1,6 +1,6 @@ { "name": "@jsonql/koa", - "version": "0.9.0", + "version": "0.9.1", "description": "This is the all in one package to start your jsonql project with Koa, jsonql-koa, jsonql-ws-server and more", "main": "index.js", "files": [ @@ -11,11 +11,12 @@ "scripts": { "test": "ava", "prepare": "npm run test", - "test:basic": "DEBUG=jsonql-koa* ava --verbose ./tests/basic.test.js", - "test:auth": "DEBUG=jsonql-* ava --verbose ./tests/auth.test.js", - "test:socket": "DEBUG=jsonql-koa:test:socket ava --verbose ./tests/socket.test.js", - "test:ms": "DEBUG=jsonql-koa:test* ava --verbose ./tests/ms.test.js", - "test:msm": "DEBUG=jsonql-koa:test* ava --verbose ./tests/ms-multi.test.js", + "test:default": "ava ./tests/default.test.js", + "test:basic": "DEBUG=jsonql-koa* ava ./tests/basic.test.js", + "test:auth": "DEBUG=jsonql-* ava ./tests/auth.test.js", + "test:socket": "DEBUG=jsonql-koa:test:socket ava ./tests/socket.test.js", + "test:ms": "DEBUG=jsonql-koa:test* ava ./tests/ms.test.js", + "test:msm": "DEBUG=jsonql-koa:test* ava ./tests/ms-multi.test.js", "test:cli": "DEBUG=jsonql-koa* node ./cli.js" }, "keywords": [ @@ -62,7 +63,7 @@ "debug": "^4.1.1", "fs-extra": "^8.1.0", "jsonql-constants": "^1.8.14", - "jsonql-koa": "^1.6.0", + "jsonql-koa": "^1.6.1", "jsonql-params-validator": "^1.5.2", "jsonql-utils": "^0.9.7", "koa": "^2.11.0", diff --git a/packages/@jsonql/koa/tests/default.test.js b/packages/@jsonql/koa/tests/default.test.js new file mode 100644 index 00000000..fae3ef3a --- /dev/null +++ b/packages/@jsonql/koa/tests/default.test.js @@ -0,0 +1,40 @@ +// test the koa server with appDir options + +const test = require('ava') +const fsx = require('fs-extra') +const { join } = require('path') +const jsonqlNodeClient = require('jsonql-node-client') +const debug = require('debug')('jsonql-koa:test:basic') +const { HELLO } = require('jsonql-constants') + +const basedir = join(__dirname, 'tests', 'fixtures') +const contractDir = join(basedir, 'contract') +const createJsonqlKoaServer = require('../index') +const port = 10086 + +test.before(t => { + t.context.server = createJsonqlKoaServer({ + appDir: join('tests', 'fixtures'), + autoStart: true, + port + }) +}) + +test.after(t => { + t.context.server.stop() + fsx.removeSync(join(contractDir, 'contract.json')) + fsx.removeSync(join(contractDir, 'public-contract.json')) +}) + +test(`It should able to use the appDir prop`, async t => { + + const client = await jsonqlNodeClient({ + hostname: `http://localhost:${port}`, + contractDir + }) + + const result = await client.helloWorld() + + t.is(result, HELLO) +}) + -- Gitee From 9efc42e1700fb856ea6bc8cf7d1e886bf4a475fd Mon Sep 17 00:00:00 2001 From: joelchu Date: Tue, 3 Mar 2020 12:23:20 +0800 Subject: [PATCH 2/3] completely change the way how we are going to construct other modules from now on, make all fn like lego and put them back together as we need --- packages/@jsonql/koa/package.json | 2 +- packages/@jsonql/koa/src/jsonql-koa-server.js | 3 +- packages/@jsonql/koa/src/options/index.js | 5 +- packages/@jsonql/koa/tests/default.test.js | 4 +- packages/koa/index.js | 46 ++++++++++---- packages/koa/package.json | 2 +- packages/koa/src/options/index.js | 60 +++++++------------ packages/koa/tests/config.test.js | 23 +------ 8 files changed, 65 insertions(+), 80 deletions(-) diff --git a/packages/@jsonql/koa/package.json b/packages/@jsonql/koa/package.json index 5d741a0b..ae0892d5 100644 --- a/packages/@jsonql/koa/package.json +++ b/packages/@jsonql/koa/package.json @@ -11,7 +11,7 @@ "scripts": { "test": "ava", "prepare": "npm run test", - "test:default": "ava ./tests/default.test.js", + "test:default": "DEBUG=jsonql-* ava ./tests/default.test.js", "test:basic": "DEBUG=jsonql-koa* ava ./tests/basic.test.js", "test:auth": "DEBUG=jsonql-* ava ./tests/auth.test.js", "test:socket": "DEBUG=jsonql-koa:test:socket ava ./tests/socket.test.js", diff --git a/packages/@jsonql/koa/src/jsonql-koa-server.js b/packages/@jsonql/koa/src/jsonql-koa-server.js index ebebaf8e..25b054f6 100644 --- a/packages/@jsonql/koa/src/jsonql-koa-server.js +++ b/packages/@jsonql/koa/src/jsonql-koa-server.js @@ -15,6 +15,7 @@ class JsonqlKoaServer { // keep a copy of the original config - do we need this anymore? // const originalConfig = Object.assign({}, config) this.opts = checkOptions(config) + debug('JsonqlKoaServer.opts', this.opts) const { server, app, ws } = initServer(this.opts, middlewares) this.server = server this.app = app @@ -26,7 +27,7 @@ class JsonqlKoaServer { } // start the server - start(port) { + start(port = null) { if (!this.started) { console.info(`[jsonql Koa server] version: ${version} start on ${port || this.opts.port}`) this.server.listen(port || this.opts.port) diff --git a/packages/@jsonql/koa/src/options/index.js b/packages/@jsonql/koa/src/options/index.js index 59e3fc35..7906201e 100644 --- a/packages/@jsonql/koa/src/options/index.js +++ b/packages/@jsonql/koa/src/options/index.js @@ -1,7 +1,7 @@ // check options const { options } = require('./options') const { preConfigCheck } = require('jsonql-koa') -// @TODO there also will be the socket.io version or primus in the future +// @TODO there will be the socket.io version or primus in the future // therefore the merge options should be conditional const { wsServerDefaultOptions, wsServerConstProps } = require('jsonql-ws-server') @@ -15,5 +15,6 @@ module.exports = function checkOptions(config) { return preConfigCheck( Object.assign(wsServerDefaultOptions, options), wsServerConstProps, - config) + config + ) } diff --git a/packages/@jsonql/koa/tests/default.test.js b/packages/@jsonql/koa/tests/default.test.js index fae3ef3a..74266378 100644 --- a/packages/@jsonql/koa/tests/default.test.js +++ b/packages/@jsonql/koa/tests/default.test.js @@ -4,10 +4,10 @@ const test = require('ava') const fsx = require('fs-extra') const { join } = require('path') const jsonqlNodeClient = require('jsonql-node-client') -const debug = require('debug')('jsonql-koa:test:basic') +// const debug = require('debug')('jsonql-koa:test:basic') const { HELLO } = require('jsonql-constants') -const basedir = join(__dirname, 'tests', 'fixtures') +const basedir = join(__dirname, 'fixtures') const contractDir = join(basedir, 'contract') const createJsonqlKoaServer = require('../index') const port = 10086 diff --git a/packages/koa/index.js b/packages/koa/index.js index 99e4751d..64e0840e 100644 --- a/packages/koa/index.js +++ b/packages/koa/index.js @@ -2,8 +2,6 @@ * This is the main interface to export the middleware(s) * we will take the config here and export array of middleware using koa-compose */ -import fs from 'fs' -import { merge } from 'lodash' import compose from 'koa-compose' import { coreMiddleware, @@ -14,14 +12,24 @@ import { publicMethodMiddleware, initMiddleware } from './src/middlewares' -import { configCheck, preConfigCheck } from './src/options' +import { configCheck, extendConfigCheck } from './src/options' import { getDebug } from './src/utils' const debug = getDebug('main') -// main -function jsonqlKoa(config = {}) { - // first check the config - const opts = configCheck(config) - debug('[jsonql-koa] init opts', opts) +// @2020-03-03 +/* +The configuration check causing no end of problem +I am going to ditch that whole pre-check bs, and take +apart the config-check and init the actual module +when we need to put this together with other module +we just take it out like a lego and put it back together again +*/ + +/** + * The actual method that compose all the middlewares together + * @param {object} opts configuration + * @return {object} compose together middlewares as one + */ +function composeJsonqlKoa(opts) { // export let middlewares = [ initMiddleware(opts), @@ -29,7 +37,7 @@ function jsonqlKoa(config = {}) { contractMiddleware(opts) ] // only available when enable it - if (config.enableWebConsole) { + if (opts.enableWebConsole) { middlewares.push(consoleMiddleware(opts)) } @@ -46,5 +54,21 @@ function jsonqlKoa(config = {}) { return compose(middlewares) } -// export these two now -export { jsonqlKoa, preConfigCheck } +/** + * Default interface when usig this standalone + * @param {object} config user supply configuration + * @return {object} composed koa middleware + */ +function jsonqlKoa(config = {}) { + // first check the config + const opts = configCheck(config) + debug('[jsonql-koa] init opts', opts) + return composeJsonqlKoa(opts) +} + +// export +export { + jsonqlKoa, + extendConfigCheck, + composeJsonqlKoa +} diff --git a/packages/koa/package.json b/packages/koa/package.json index 98a08aa6..026a8a48 100644 --- a/packages/koa/package.json +++ b/packages/koa/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-koa", - "version": "1.6.1", + "version": "1.6.2", "description": "jsonql Koa middleware", "main": "main.js", "module": "index.js", diff --git a/packages/koa/src/options/index.js b/packages/koa/src/options/index.js index 688b20eb..aa58c9a7 100644 --- a/packages/koa/src/options/index.js +++ b/packages/koa/src/options/index.js @@ -1,12 +1,6 @@ import { merge } from 'lodash' -import { - preConfigCheck as jsonqlUtilsPreConfigCheck, - objHasProp, - chainFns, - injectToFn -} from 'jsonql-utils' +import { chainFns } from 'jsonql-utils' import { checkConfig } from 'jsonql-params-validator' -import { CHECKED_KEY } from 'jsonql-constants' import { appProps, constProps } from './options' import { applyGetContract, @@ -14,9 +8,17 @@ import { initNodeClient, applyDirectories } from './main' -import { getDebug } from '../utils' -const debug = getDebug('config-check:index') +// import { getDebug } from '../utils' +// const debug = getDebug('config-check:index') + +const getConfigFn = () => chainFns( + checkConfig, + applyDirectories, + applyGetContract, + applyAuthOptions, + initNodeClient +) /** * The main method that run before everything else to check the configuration @@ -25,43 +27,21 @@ const debug = getDebug('config-check:index') * @api public */ export function configCheck(config = {}) { - let now = Date.now() - let time - if ((time = objHasProp(config, CHECKED_KEY)) !== undefined) { // @TODO we could check the time if we want ... - debug(`config already checked, pass it through`, time, now - time) - return config - } - const fn = chainFns( - checkConfig, - applyDirectories, - applyGetContract, - applyAuthOptions, - initNodeClient - ) - const opts = fn(config, appProps, constProps) - // return opts; - return injectToFn(opts, CHECKED_KEY, now) + const fn = getConfigFn() + return fn(config, appProps, constProps) } /** * new methods for when using it combine with the jsonql-ws-server - * @param {object} wsDefaultOptions from the other config - * @param {object} wsConstProps from the other config + * @param {object} extraAppProps from the other config + * @param {object} extraConstProps from the other config * @param {object} config supply by the user */ -export function preConfigCheck(wsDefaultOptions, wsConstProps, config = {}) { - const _defaultOptions = merge({}, appProps, wsDefaultOptions) - const _constProps = merge({}, wsConstProps, constProps) +export function extendConfigCheck(extraAppProps, extraConstProps, config = {}) { + const extendAppProps = merge({}, appProps, extraAppProps) + const extendConstProps = merge({}, constProps, extraConstProps) - debug(_defaultOptions) + const fn = getConfigFn() - return jsonqlUtilsPreConfigCheck( - _defaultOptions, - _constProps, // props - checkConfig, - applyDirectories, - applyGetContract, - applyAuthOptions, - initNodeClient // fns - )(config) // @NOTE the above method return a function accept the config as param + return fn(config, extendAppProps, extendConstProps) } diff --git a/packages/koa/tests/config.test.js b/packages/koa/tests/config.test.js index e5f86d6e..b8724250 100644 --- a/packages/koa/tests/config.test.js +++ b/packages/koa/tests/config.test.js @@ -3,12 +3,8 @@ const test = require('ava') const fsx = require('fs-extra') const { join, resolve } = require('path') -const { CHECKED_KEY, BOOLEAN_TYPE, NUMBER_TYPE } = require('jsonql-constants') -const { objHasProp } = require('jsonql-utils') -const { createConfig } = require('jsonql-params-validator') - const { configCheck } = require('../src/options') -const { preConfigCheck } = require('../index') + const processJwtKeysDefault = require('../src/options/process-jwt-keys') const processJwtKeys = processJwtKeysDefault.default @@ -86,20 +82,3 @@ test('It should have privateKey and publicKey when set useJwt = true', async t = t.truthy(opts.privateKey && opts.publicKey) }) - -test(`It should now have an extra property __checked__`, t => { - const defaultOptions = { - optA: createConfig(false, [BOOLEAN_TYPE]), - optB: createConfig(0, [NUMBER_TYPE]) - } - - const constProps = { someStaticOption: 'whatever' } - - const opts = preConfigCheck(defaultOptions, constProps, {appDir: 'tests/fixtures'}) - debug(opts) - const check = objHasProp(opts, CHECKED_KEY) - // debug(check) - t.truthy(check) - t.is(opts.optA, false) - t.is(opts.someStaticOption, 'whatever') -}) -- Gitee From 27f5f3af6e4a9937ab8af42f26cbf89c182dd2ea Mon Sep 17 00:00:00 2001 From: joelchu Date: Tue, 3 Mar 2020 12:29:59 +0800 Subject: [PATCH 3/3] update readme --- packages/koa/README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/koa/README.md b/packages/koa/README.md index e6b34206..8a096734 100644 --- a/packages/koa/README.md +++ b/packages/koa/README.md @@ -35,6 +35,21 @@ or $ yarn add jsonql-koa ``` +This module export three methods: + +- jsonqlKoa(config = {}): this gives you the Koa middleware, just supply the `config` (if needed, see below) +- composeJsonqlKoa(opts): this is **NOT** for general use, since you have no idea what are the options, this is like a lego for us to build with other modules +- extendConfigCheck(extraAppProps, extraConstProps, config): this goes with the `composeJsonqlKoa` to create the `jsonqlKoa`. Again this is not for general use, we use this to build with other modules. + +Basically you only need this: + +```js +import { jsonqlKoa } from 'jsonql-koa' +``` + +In fact, we don't recommend you to use this package directly (unless you want create your own cool modules). +Use [@jsonql/koa](https://npmjs.com/package/@jsonql/koa) instead. + ## Configuration options | Name | Description | Expected Type | Default value | -- Gitee