From 3f8a0dd0d18b0e77cb138ba3c4f48aebe7b239cc Mon Sep 17 00:00:00 2001 From: joelchu Date: Fri, 13 Dec 2019 15:30:28 +0800 Subject: [PATCH] export one more function getContractFromConfig from jsonql-client --- packages/@jsonql/client/package.json | 6 +- packages/@jsonql/client/src/index.js | 119 ++++++++++++++++----------- packages/@jsonql/client/ws.js | 2 +- packages/http-client/module.js | 4 +- packages/http-client/package.json | 2 +- packages/http-client/src/utils.js | 3 +- 6 files changed, 80 insertions(+), 56 deletions(-) diff --git a/packages/@jsonql/client/package.json b/packages/@jsonql/client/package.json index ffbd147d..de329c44 100644 --- a/packages/@jsonql/client/package.json +++ b/packages/@jsonql/client/package.json @@ -46,10 +46,10 @@ "license": "MIT", "dependencies": { "flyio": "^0.6.14", - "jsonql-client": "^1.5.7" + "jsonql-client": "^1.5.8" }, "optionalDependencies": { - "@jsonql/ws": "^1.0.3" + "@jsonql/ws": "^1.0.6" }, "devDependencies": { "@jsonql/koa": "^0.8.1", @@ -62,7 +62,7 @@ "nyc": "^14.1.1", "promise-polyfill": "8.1.3", "qunit": "^2.9.3", - "rollup": "^1.27.10", + "rollup": "^1.27.11", "rollup-plugin-alias": "^2.2.0", "rollup-plugin-analyzer": "^3.2.2", "rollup-plugin-async": "^1.2.0", diff --git a/packages/@jsonql/client/src/index.js b/packages/@jsonql/client/src/index.js index e98187da..39f2644b 100644 --- a/packages/@jsonql/client/src/index.js +++ b/packages/@jsonql/client/src/index.js @@ -1,71 +1,92 @@ -// these was in the http-client +// Combine interface to also init the socket client if it's required +import merge from 'lodash-es/merge' import { isContract } from 'jsonql-utils/src/contract' + import { isObjectHasKey } from 'jsonql-utils/src/generic' import { getContractFromConfig } from './src/utils' + import { SOCKET_NAME } from 'jsonql-constants' import { JsonqlError } from 'jsonql-errors' +// from jsonql-client +import { getPreConfigCheck } from 'jsonql-client/opts' +import { JsonqlBaseClient, getEventEmitter, generator } from 'jsonql-client/module' +/** + * Create the custom check options method + * @param {object} extraDefaultOptions for valdiation + * @param {object} extraConstProps for merge after + * @return {function} resolve the clean configuration + */ +const getCheckConfigFn = function(extraDefaultOptions, extraConstProps) { + const checkAction = getPreConfigCheck(extraDefaultOptions, extraConstProps) + return (config = {}) => Promise.resolve(checkAction(config)) +} /** * Check if the contract has socket field and the socket client is suplied - * @param {object} client the http client - * @param {object} contract the json - * @param {object} config the checked configuration - * @param {object} socketClient from the original config - * @return {object} the completed client + * @param {*} [socketClient=null] from the original config + * @return {function} takes in the extra params then return the client */ -function initSocketClient(client, contract, config, socketClient) { - if (isObjectHasKey(contract, SOCKET_NAME)) { - if (socketClient) { - // pass the contract here one more time in case the constProps overwritten it - config.log = client.getLogger(`jsonql-client:${config.serverType}`) - config.contract = contract - config.eventEmitter = client.eventEmitter - return socketClient(config) - .then(sc => { - client[SOCKET_NAME] = sc - return client - }) - } else { - throw new JsonqlError(`initSocketClient`, `socketClient is missing!`) +function initSocketClient(socketClient = null) { + /** + * @param {object} client the http client + * @param {object} contract the json + * @param {object} config the checked configuration + */ + return (client, contract, config) => { + if (isObjectHasKey(contract, SOCKET_NAME)) { + if (socketClient) { + const constProps = { + contract, + log: client.getLogger(`jsonql-client:${config.serverType}`), + eventEmitter: client.eventEmitter + } + return socketClient(config, constProps) + .then(sc => { + client[SOCKET_NAME] = sc + return client + }) + } else { + throw new JsonqlError(`initSocketClient`, `socketClient is missing!`) + } } + // just return it if there is none + return client } - return client } /** - * Main interface for jsonql fetch api - * @1.4.8 change this to named export to diff this from the other - * and this is only use with the @jsonql/client to construct the client with ws + * Main interface for construct the client and return extra options for continue + * with socket client if any * @param {object} Fly the http engine * @param {object} [config={}] configuration - * @return {object} jsonqlClient + * @return {function} return promise resolve with opts, contract, client */ -function jsonqlClientModule(fly, config = {}) { - const { socketClient, debugOn } = config; - const ee = getEventEmitter(debugOn) - return checkOptionsAsync(config) - .then(opts => ( - { - opts, - baseClient: new JsonqlBaseClient(fly, opts) - } - )) - // make sure the contract is presented - .then(({opts, baseClient}) => getContractFromConfig(baseClient, opts.contract) - .then(contract => ( - { - opts, - contract, - client: generator(baseClient, opts, contract, ee) - } +function getJsonqlClient(fly, extraDefaultOptions = {}, extraConstProps = {}) { + const checkConfigFn = getCheckConfigFn(extraDefaultOptions, extraConstProps) + return (config = {}) => { + return checkConfigFn(config) + .then(opts => ( + { + opts, + baseClient: new JsonqlBaseClient(fly, opts) + } + )) + // make sure the contract is presented + .then(({opts, baseClient}) => getContractFromConfig(baseClient, opts.contract) + .then(contract => ( + { + opts, + contract, + client: generator(baseClient, opts, contract, ee) + } + ) ) ) - ) - // finally generate the websocket client if any - .then(({opts, contract, client}) => ( - initSocketClient(client, contract, opts, socketClient) - )) + // @NOTE we only return the opts, contract, client here + // and allow the client to chain into this to coninue + // finally generate the websocket client if any + } } // export it -export { jsonqlClientModule } +export { getJsonqlClient, initSocketClient } diff --git a/packages/@jsonql/client/ws.js b/packages/@jsonql/client/ws.js index 1b9beeec..e1f5d4db 100644 --- a/packages/@jsonql/client/ws.js +++ b/packages/@jsonql/client/ws.js @@ -22,7 +22,7 @@ export default function createJsonqlHttpWsClient(Fly, config = {}) { // @NOTE it return a function to accept the config const fn = getPreConfigCheck(wsDefaultOptions, wsConstProps) const opts = fn(config) - opts.socketClient = jsonqlWsClient + // init the client return jsonqlClientModule(Fly, opts) } diff --git a/packages/http-client/module.js b/packages/http-client/module.js index f0e7f1db..95c4b65d 100644 --- a/packages/http-client/module.js +++ b/packages/http-client/module.js @@ -5,10 +5,12 @@ import JsonqlBaseClient from './src/base' import getEventEmitter from './src/ee' import generator from './src/core/jsonql-api-generator' import { checkOptionsAsync } from './src/options' +import { getContractFromConfig } from './src/utils' export { JsonqlBaseClient, getEventEmitter, generator, - checkOptionsAsync + checkOptionsAsync, + getContractFromConfig } diff --git a/packages/http-client/package.json b/packages/http-client/package.json index 8001c604..2f3d2ae5 100755 --- a/packages/http-client/package.json +++ b/packages/http-client/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-client", - "version": "1.5.8", + "version": "1.5.9", "description": "jsonql http browser client using Fly.js with full user profile management with jwt and more", "main": "core.js", "module": "index.js", diff --git a/packages/http-client/src/utils.js b/packages/http-client/src/utils.js index 63ce457d..c6ffbaa4 100644 --- a/packages/http-client/src/utils.js +++ b/packages/http-client/src/utils.js @@ -3,6 +3,7 @@ // this way we avoid those that we don't want node.js module got build into the code import { isContract } from 'jsonql-utils/src/contract' import { hashCode2Str } from 'nb-event-service/src/hash-code' + /** * @param {object} jsonqlInstance the init instance of jsonql client * @param {object} contract the static contract @@ -15,7 +16,7 @@ const getContractFromConfig = function(jsonqlInstance, contract = {}) { return jsonqlInstance.getContract() } // wrapper method to make sure it's a string -// just alias now +// just alias now const hashCode = str => hashCode2Str(str) // simple util to check if an object has any properties -- Gitee