From bd4d45a0ae9e9efeb4444e6396783d64f13629d8 Mon Sep 17 00:00:00 2001 From: joelchu Date: Tue, 26 Nov 2019 20:34:35 +0800 Subject: [PATCH 1/9] verify the new jsonql-resolver is working correctly --- packages/@jsonql/koa/package.json | 4 +-- packages/@jsonql/koa/tests/ms.test.js | 4 +-- packages/ws-server/package.json | 2 +- packages/ws-server/src/core/ws-setup.js | 5 ++-- packages/ws-server/src/share/add-property.js | 27 ++++++++++++------- .../ws-server/src/share/resolve-method.js | 18 +++++++------ 6 files changed, 35 insertions(+), 25 deletions(-) diff --git a/packages/@jsonql/koa/package.json b/packages/@jsonql/koa/package.json index 30fcfdf6..e7dfe930 100644 --- a/packages/@jsonql/koa/package.json +++ b/packages/@jsonql/koa/package.json @@ -52,8 +52,8 @@ "dependencies": { "debug": "^4.1.1", "fs-extra": "^8.1.0", - "jsonql-constants": "^1.8.10", - "jsonql-koa": "^1.4.17", + "jsonql-constants": "^1.8.11", + "jsonql-koa": "^1.4.18", "jsonql-params-validator": "^1.4.11", "koa": "^2.11.0", "koa-bodyparser": "^4.2.1", diff --git a/packages/@jsonql/koa/tests/ms.test.js b/packages/@jsonql/koa/tests/ms.test.js index 108cefbf..1425b9c5 100644 --- a/packages/@jsonql/koa/tests/ms.test.js +++ b/packages/@jsonql/koa/tests/ms.test.js @@ -50,7 +50,7 @@ test(`It should able to connect to server B directly`, async t => { }) -test(`It should able to connect to another service via the internal nodeClient`, async t => { +test.only(`It should able to connect to another service via the internal nodeClient`, async t => { const client1 = await getClientA() const msg = await client1.query.getSomething() @@ -60,7 +60,7 @@ test(`It should able to connect to another service via the internal nodeClient`, t.truthy(msg) }) -test.cb.only(`It should able to connect to the another client via socket connection`, t => { +test.cb(`It should able to connect to the another client via socket connection`, t => { t.plan(2) getClientA() .then(client => { diff --git a/packages/ws-server/package.json b/packages/ws-server/package.json index 4ad3d165..e2bc640c 100755 --- a/packages/ws-server/package.json +++ b/packages/ws-server/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-ws-server", - "version": "1.4.3", + "version": "1.4.4", "description": "Setup WebSocket server for the jsonql to run on the same host, automatic generate public / private channel using contract", "main": "index.js", "files": [ diff --git a/packages/ws-server/src/core/ws-setup.js b/packages/ws-server/src/core/ws-setup.js index 382768cf..7aa05ef9 100644 --- a/packages/ws-server/src/core/ws-setup.js +++ b/packages/ws-server/src/core/ws-setup.js @@ -35,8 +35,7 @@ const fnHandler = (ws, payload, resolverName, params, opts, userdata) => { } // @NOTE the params.params is from the contract return validateAsync(payload.args, params.params, true) - .then( args => { - return resolveMethod( + .then(args => resolveMethod( resolverName, args, // this is the clear value from validateAsync params, @@ -44,7 +43,7 @@ const fnHandler = (ws, payload, resolverName, params, opts, userdata) => { ws, userdata ) - }) + )) .then(result => { debug('result', result) // decide if we need to call the cb or not here diff --git a/packages/ws-server/src/share/add-property.js b/packages/ws-server/src/share/add-property.js index c70c1218..9d9f0b80 100644 --- a/packages/ws-server/src/share/add-property.js +++ b/packages/ws-server/src/share/add-property.js @@ -1,14 +1,15 @@ // add required properties to the resolver -const _ = require('lodash') const { EMIT_REPLY_TYPE, SEND_MSG_PROP_NAME, ON_MESSAGE_PROP_NAME, - JS_WS_NAME + JS_WS_NAME, + INIT_CLIENT_PROP_KEY } = require('jsonql-constants') // @BUG it's weird this file is not here but no error was throw const { objDefineProps, injectToFn } = require('jsonql-utils') const { provideUserdata } = require('jsonql-jwt') +const { injectNodeClient } = require('jsonql-resolver') const { nil, createWsReply, getDebug } = require('../share/helpers') const debug = getDebug(`addProperty`) @@ -24,26 +25,34 @@ resolver = addHandlerProperty(resolver, ON_MESSAGE_PROP_NAME, function(handler) /** * using the serverType to provide different addProperty method to this + * change to return a promise on 1.4.4 * @param {function} fn the resolver function * @param {string} resolverName resolver name * @param {object} ws the different context object * @param {object|boolean} userdata false when there is none + * @param {object} opts configuration added in 1.4.4 for the client configs * @return {function} the applied function */ -const addProperty = (fn, resolverName, ws, userdata) => { - return _(injectToFn(fn, JS_WS_NAME, ws)) - .chain() - .thru(resolver => { +const addProperty = (fn, resolverName, ws, userdata, opts) => { + return Promise + .resolve(injectToFn(fn, JS_WS_NAME, ws)) + // define the send method + .then(resolver => { debug(`add ${SEND_MSG_PROP_NAME} to ${resolverName}`) return objDefineProps(fn, SEND_MSG_PROP_NAME, function(prop) { // @TODO should this get validate as well? ws.send(createWsReply(EMIT_REPLY_TYPE, resolverName, prop)) }, nil) }) - .thru(resolver => { - return userdata ? provideUserdata(resolver, userdata) : resolver; + .then(resolver => userdata ? provideUserdata(resolver, userdata) : resolver) + .then(resolver => { + if (opts[INIT_CLIENT_PROP_KEY] && opts[INIT_CLIENT_PROP_KEY].then) { + return opts[INIT_CLIENT_PROP_KEY] + .then(clients => injectNodeClient(resolver, clients)) + } + // @TODO what if we run this standalone and we want to inject clients to it? + return resolver; }) - .value() } module.exports = { addProperty } diff --git a/packages/ws-server/src/share/resolve-method.js b/packages/ws-server/src/share/resolve-method.js index a3729852..596f9215 100644 --- a/packages/ws-server/src/share/resolve-method.js +++ b/packages/ws-server/src/share/resolve-method.js @@ -32,18 +32,20 @@ const debug = getDebug('resolve-method') * @param {object} opts for search later * @param {object} ws the WebSocket instance * @param {object} [userdata=false] userdata - * @return {mixed} depends on the contract + * @return {promise} depends on the contract */ const resolveMethod = function(resolverName, args, params, opts, ws, userdata = false) { // the contract is always part of the options here const fn = getResolver(resolverName, SOCKET_NAME, opts.contract, opts) - const tfn = addProperty(fn, resolverName, ws, userdata) - try { - return Reflect.apply(tfn, null, args) - } catch(e) { - debug(`resolveMethod Error`, e) - throw new JsonqlResolverAppError(resolverName, e) - } + return addProperty(fn, resolverName, ws, userdata, opts) + .then(tfn => { + try { + return Reflect.apply(tfn, null, args) + } catch(e) { + debug(`resolveMethod Error`, e) + throw new JsonqlResolverAppError(resolverName, e) + } + }) } // we only need to export one method -- Gitee From 665e8a5f8f279301abdb719ebdffa85f90b32d86 Mon Sep 17 00:00:00 2001 From: joelchu Date: Tue, 26 Nov 2019 20:36:05 +0800 Subject: [PATCH 2/9] new promise interface for resolve-method is working as expect in jsonql-ws-server --- packages/ws-server/src/core/ws-setup.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ws-server/src/core/ws-setup.js b/packages/ws-server/src/core/ws-setup.js index 7aa05ef9..f3f460e9 100644 --- a/packages/ws-server/src/core/ws-setup.js +++ b/packages/ws-server/src/core/ws-setup.js @@ -43,7 +43,7 @@ const fnHandler = (ws, payload, resolverName, params, opts, userdata) => { ws, userdata ) - )) + ) .then(result => { debug('result', result) // decide if we need to call the cb or not here -- Gitee From 942e68737cf225bdb13663d4f8250a16b7c70965 Mon Sep 17 00:00:00 2001 From: joelchu Date: Tue, 26 Nov 2019 20:38:06 +0800 Subject: [PATCH 3/9] jsonql-ws-server to 1.4.4 --- packages/ws-server/package.json | 6 +++--- packages/ws-server/src/share/add-property.js | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/ws-server/package.json b/packages/ws-server/package.json index e2bc640c..18e4636d 100755 --- a/packages/ws-server/package.json +++ b/packages/ws-server/package.json @@ -32,12 +32,12 @@ "debug": "^4.1.1", "esm": "^3.2.25", "fs-extra": "^8.1.0", - "jsonql-constants": "^1.8.10", + "jsonql-constants": "^1.8.11", "jsonql-errors": "^1.1.6", "jsonql-jwt": "^1.3.4", "jsonql-params-validator": "^1.4.11", - "jsonql-resolver": "^0.9.6", - "jsonql-utils": "^0.8.4", + "jsonql-resolver": "^0.9.9", + "jsonql-utils": "^0.8.5", "lodash": "^4.17.15", "ws": "^7.2.0" }, diff --git a/packages/ws-server/src/share/add-property.js b/packages/ws-server/src/share/add-property.js index 9d9f0b80..a55a11f0 100644 --- a/packages/ws-server/src/share/add-property.js +++ b/packages/ws-server/src/share/add-property.js @@ -47,6 +47,7 @@ const addProperty = (fn, resolverName, ws, userdata, opts) => { .then(resolver => userdata ? provideUserdata(resolver, userdata) : resolver) .then(resolver => { if (opts[INIT_CLIENT_PROP_KEY] && opts[INIT_CLIENT_PROP_KEY].then) { + debug(`using INIT_CLIENT_PROP_KEY to add clients to the resolver`) return opts[INIT_CLIENT_PROP_KEY] .then(clients => injectNodeClient(resolver, clients)) } -- Gitee From a61da39a36770c1face18249c7212c34f8a1c346 Mon Sep 17 00:00:00 2001 From: joelchu Date: Tue, 26 Nov 2019 20:43:02 +0800 Subject: [PATCH 4/9] the final test passed for @jsonql/koa --- packages/@jsonql/koa/package.json | 2 +- packages/@jsonql/koa/tests/ms.test.js | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/@jsonql/koa/package.json b/packages/@jsonql/koa/package.json index e7dfe930..e3c2712c 100644 --- a/packages/@jsonql/koa/package.json +++ b/packages/@jsonql/koa/package.json @@ -61,7 +61,7 @@ "yargs": "^15.0.2" }, "optionalDependencies": { - "jsonql-ws-server": "^1.4.3" + "jsonql-ws-server": "^1.4.4" }, "bin": { "jsonql-koa-cli": "./cli.js" diff --git a/packages/@jsonql/koa/tests/ms.test.js b/packages/@jsonql/koa/tests/ms.test.js index 1425b9c5..acf19a95 100644 --- a/packages/@jsonql/koa/tests/ms.test.js +++ b/packages/@jsonql/koa/tests/ms.test.js @@ -30,6 +30,8 @@ test.after(t => { t.context.serverA.stop() t.context.serverB.stop() + fsx.removeSync(contractDirA) + fsx.removeSync(contractDirB) }) test(`It should able to connect to service A directly`, async t => { @@ -50,7 +52,7 @@ test(`It should able to connect to server B directly`, async t => { }) -test.only(`It should able to connect to another service via the internal nodeClient`, async t => { +test(`It should able to connect to another service via the internal nodeClient`, async t => { const client1 = await getClientA() const msg = await client1.query.getSomething() -- Gitee From 96d21df8b7b7f83f0e0d9696ed2f2cdea6908b34 Mon Sep 17 00:00:00 2001 From: joelchu Date: Tue, 26 Nov 2019 20:44:04 +0800 Subject: [PATCH 5/9] Add the files field to the package for @jsonql/koa --- packages/@jsonql/koa/package.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/@jsonql/koa/package.json b/packages/@jsonql/koa/package.json index e3c2712c..6f2163be 100644 --- a/packages/@jsonql/koa/package.json +++ b/packages/@jsonql/koa/package.json @@ -1,8 +1,13 @@ { "name": "@jsonql/koa", - "version": "0.5.0", + "version": "0.5.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": [ + "cli.js", + "index.js", + "src" + ], "scripts": { "test": "ava --verbose", "test:basic": "DEBUG=jsonql-koa* ava --verbose ./tests/basic.test.js", -- Gitee From 38732db96d943c63ac0eaaf1cc424c5f3ba08669 Mon Sep 17 00:00:00 2001 From: joelchu Date: Tue, 26 Nov 2019 22:28:48 +0800 Subject: [PATCH 6/9] got the test passed --- packages/@jsonql/koa/index.js | 8 ++++---- packages/@jsonql/koa/package.json | 1 + packages/@jsonql/koa/src/init-server.js | 5 ++--- packages/@jsonql/koa/tests/auth.test.js | 4 ++-- packages/@jsonql/koa/tests/basic.test.js | 6 ++++-- packages/@jsonql/koa/tests/socket.test.js | 6 +++--- 6 files changed, 16 insertions(+), 14 deletions(-) diff --git a/packages/@jsonql/koa/index.js b/packages/@jsonql/koa/index.js index a8f3e3d7..5e033648 100644 --- a/packages/@jsonql/koa/index.js +++ b/packages/@jsonql/koa/index.js @@ -20,16 +20,16 @@ class JsonqlKoaServer { this.app = app; this.ws = ws; this.started = false; - if (this.opts.autoStart) { + if (this.opts.autoStart && this.opts.port) { this.start() } } // start the server - start() { + start(port) { if (!this.started) { - console.info(`Server version: ${version} start on ${this.opts.port}`) - this.server.listen(this.opts.port) + console.info(`Server version: ${version} start on ${port || this.opts.port}`) + this.server.listen(port || this.opts.port) this.started = true; } } diff --git a/packages/@jsonql/koa/package.json b/packages/@jsonql/koa/package.json index 6f2163be..2c24d9f1 100644 --- a/packages/@jsonql/koa/package.json +++ b/packages/@jsonql/koa/package.json @@ -60,6 +60,7 @@ "jsonql-constants": "^1.8.11", "jsonql-koa": "^1.4.18", "jsonql-params-validator": "^1.4.11", + "jsonql-utils": "^0.8.5", "koa": "^2.11.0", "koa-bodyparser": "^4.2.1", "koa-cors": "0.0.16", diff --git a/packages/@jsonql/koa/src/init-server.js b/packages/@jsonql/koa/src/init-server.js index 46547543..4037edb6 100644 --- a/packages/@jsonql/koa/src/init-server.js +++ b/packages/@jsonql/koa/src/init-server.js @@ -5,6 +5,7 @@ const bodyparser = require('koa-bodyparser') const cors = require('koa-cors') // const { jsonqlKoa } = require('../../../koa/main') const { jsonqlKoa } = require('jsonql-koa') +// const { chainFns } = require('jsonql-utils') const { getSocketServer } = require('./get-socket-server') // const debug = require('debug')('jsonql-koa:init-server') @@ -18,7 +19,7 @@ function initServer(config, middlewares) { const app = new Koa() // apply default middlewares app.use(bodyparser()) - if (config.cors === true) { // default is true + if (config.cors === true) { // default is true app.use(cors()) } // init jsonqlKoa @@ -30,8 +31,6 @@ function initServer(config, middlewares) { const server = http.createServer(app.callback()) - // debug('config for socket server', config) - const ws = getSocketServer(config, server) // return it return { server, app, ws } diff --git a/packages/@jsonql/koa/tests/auth.test.js b/packages/@jsonql/koa/tests/auth.test.js index 27aa6b55..13356421 100644 --- a/packages/@jsonql/koa/tests/auth.test.js +++ b/packages/@jsonql/koa/tests/auth.test.js @@ -10,13 +10,13 @@ const contractDir = join(contractBaseDir, AUTH_DIR) const port = 8082; test.before(t => { - const { stop } = jsonqlKoaServer({ + const jsonqlKoaInt = jsonqlKoaServer({ port: port, autoStart: true, enableAuth: true }) - t.context.stop = stop; + t.context.stop = () => jsonqlKoaInt.stop(); }) test.after( t => { diff --git a/packages/@jsonql/koa/tests/basic.test.js b/packages/@jsonql/koa/tests/basic.test.js index 4db99eb0..1113f9a7 100644 --- a/packages/@jsonql/koa/tests/basic.test.js +++ b/packages/@jsonql/koa/tests/basic.test.js @@ -12,12 +12,14 @@ const contractDir = join(contractBaseDir, BASIC_DIR) const jsonqlKoaServer = require('./fixtures/test-server') test.before(t => { - const { stop } = jsonqlKoaServer({ + const jsonqlKoaInt = jsonqlKoaServer({ port: 8001, autoStart: true }) - t.context.stop = stop; + // debug(jsonqlKoaInt) + + t.context.stop = () => jsonqlKoaInt.stop(); }) test.after(t => { diff --git a/packages/@jsonql/koa/tests/socket.test.js b/packages/@jsonql/koa/tests/socket.test.js index 8ecc71b9..b4f5d6ad 100644 --- a/packages/@jsonql/koa/tests/socket.test.js +++ b/packages/@jsonql/koa/tests/socket.test.js @@ -11,7 +11,7 @@ const keysDir = join(contractBaseDir, [SOCKET_DIR, 'keys'].join('-')) const port = 8083; test.before(t => { - const { stop, start } = jsonqlKoaServer({ + const jsonqlKoaInt = jsonqlKoaServer({ port, keysDir, contractDir, @@ -19,9 +19,9 @@ test.before(t => { serverType: JS_WS_NAME }) - t.context.stop = stop; + t.context.stop = () => jsonqlKoaInt.stop(); - start() + jsonqlKoaInt.start() }) -- Gitee From edfd4924dc42a647ea3b1f3a405fe3c9d2eff65e Mon Sep 17 00:00:00 2001 From: joelchu Date: Tue, 26 Nov 2019 22:32:18 +0800 Subject: [PATCH 7/9] not gonna to remove the client folder then --- packages/@jsonql/koa/tests/ms.test.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/@jsonql/koa/tests/ms.test.js b/packages/@jsonql/koa/tests/ms.test.js index acf19a95..8e4f0add 100644 --- a/packages/@jsonql/koa/tests/ms.test.js +++ b/packages/@jsonql/koa/tests/ms.test.js @@ -15,7 +15,7 @@ const createMsTestServer = require('./fixtures/ms-test-servers') const getClientA = async () => ( await jsonqlNodeClient({ hostname: `http://localhost:${PORT_A}`, - contractDir: join(__dirname, 'fixtures', 'contract', 'client1'), + contractDir: join(contractBaseDir, 'client1'), serverType: 'ws' }) ) @@ -32,6 +32,9 @@ test.after(t => { fsx.removeSync(contractDirA) fsx.removeSync(contractDirB) + + // fsx.removeSync(join(contractBaseDir, 'client1')) + // fsx.removeSync(join(contractBaseDir, 'client2')) }) test(`It should able to connect to service A directly`, async t => { @@ -45,7 +48,7 @@ test(`It should able to connect to service A directly`, async t => { test(`It should able to connect to server B directly`, async t => { const client2 = await jsonqlNodeClient({ hostname: `http://localhost:${PORT_B}`, - contractDir: join(__dirname, 'fixtures', 'contract', 'client2') + contractDir: join(contractBaseDir, 'client2') }) const res2 = await client2.query.helloWorld() t.is(res2, HELLO) -- Gitee From 77389eafd87a26a8a2e59eb2369a667afb729183 Mon Sep 17 00:00:00 2001 From: joelchu Date: Wed, 27 Nov 2019 09:27:13 +0800 Subject: [PATCH 8/9] @jsonql/koa to 0.5.2 fixed all the test --- packages/@jsonql/koa/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@jsonql/koa/package.json b/packages/@jsonql/koa/package.json index 2c24d9f1..e32cf583 100644 --- a/packages/@jsonql/koa/package.json +++ b/packages/@jsonql/koa/package.json @@ -1,6 +1,6 @@ { "name": "@jsonql/koa", - "version": "0.5.1", + "version": "0.5.2", "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": [ -- Gitee From 2e907204e5b9043239f2350040bd10869785ace9 Mon Sep 17 00:00:00 2001 From: joelchu Date: Wed, 27 Nov 2019 09:42:10 +0800 Subject: [PATCH 9/9] add TIMESTAMP_PARAM_NAME --- packages/constants/README.md | 1 + packages/constants/constants.json | 1 + packages/constants/main.js | 1 + packages/constants/module.js | 3 ++- packages/constants/package.json | 2 +- packages/utils/package.json | 2 +- packages/utils/src/params-api.js | 19 +++++++++++++++++-- 7 files changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/constants/README.md b/packages/constants/README.md index 2bd7caf9..5f90c19e 100755 --- a/packages/constants/README.md +++ b/packages/constants/README.md @@ -36,6 +36,7 @@ non-javascript to develop your tool. You can also use the included `constants.js - CONDITION_PARAM_NAME - RESOLVER_PARAM_NAME - QUERY_ARG_NAME +- TIMESTAMP_PARAM_NAME - MUTATION_ARGS - JSONP_CALLBACK_NAME - API_REQUEST_METHODS diff --git a/packages/constants/constants.json b/packages/constants/constants.json index 7cc280c3..15d4bfba 100644 --- a/packages/constants/constants.json +++ b/packages/constants/constants.json @@ -32,6 +32,7 @@ "CONDITION_PARAM_NAME": "condition", "RESOLVER_PARAM_NAME": "resolverName", "QUERY_ARG_NAME": "args", + "TIMESTAMP_PARAM_NAME": "TS", "MUTATION_ARGS": [ "resolverName", "payload", diff --git a/packages/constants/main.js b/packages/constants/main.js index 6b493cd5..c3ec08fd 100644 --- a/packages/constants/main.js +++ b/packages/constants/main.js @@ -32,6 +32,7 @@ module.exports = { "CONDITION_PARAM_NAME": "condition", "RESOLVER_PARAM_NAME": "resolverName", "QUERY_ARG_NAME": "args", + "TIMESTAMP_PARAM_NAME": "TS", "MUTATION_ARGS": [ "resolverName", "payload", diff --git a/packages/constants/module.js b/packages/constants/module.js index 3c5213db..8f5d2a10 100644 --- a/packages/constants/module.js +++ b/packages/constants/module.js @@ -39,10 +39,11 @@ export const SOCKET_NAME = 'socket'; export const CONTRACT_NAME = 'contract'; export const RESOLVER_TYPES = [QUERY_NAME, MUTATION_NAME, SOCKET_NAME]; // for calling the mutation -export const PAYLOAD_PARAM_NAME = 'payload'; +export const PAYLOAD_PARAM_NAME = 'payload'; // @TODO shortern them export const CONDITION_PARAM_NAME = 'condition'; export const RESOLVER_PARAM_NAME = 'resolverName'; export const QUERY_ARG_NAME = 'args'; +export const TIMESTAMP_PARAM_NAME = 'TS'; export const MUTATION_ARGS = [RESOLVER_PARAM_NAME, PAYLOAD_PARAM_NAME, CONDITION_PARAM_NAME]; // new jsonp export const JSONP_CALLBACK_NAME = 'jsonqlJsonpCallback'; diff --git a/packages/constants/package.json b/packages/constants/package.json index e77c5e21..7b8a8ac0 100755 --- a/packages/constants/package.json +++ b/packages/constants/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-constants", - "version": "1.8.11", + "version": "1.8.12", "description": "All the share constants for json:ql tools", "main": "main.js", "module": "module.js", diff --git a/packages/utils/package.json b/packages/utils/package.json index 945b07b4..cd023d7e 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-utils", - "version": "0.8.5", + "version": "0.8.6", "description": "This is a jsonql dependency module, not for generate use.", "main": "main.js", "module": "index.js", diff --git a/packages/utils/src/params-api.js b/packages/utils/src/params-api.js index a7ab0542..72eda760 100644 --- a/packages/utils/src/params-api.js +++ b/packages/utils/src/params-api.js @@ -13,6 +13,8 @@ import isArray from 'lodash-es/isArray' import isPlainObject from 'lodash-es/isPlainObject' import isString from 'lodash-es/isString' +import { timestamp } from './generic' + /** * make sure it's an object (it was call formatPayload but it doesn't make sense) * @param {*} payload the object comes in could be string based @@ -37,6 +39,19 @@ export function getNameFromPayload(payload) { return Object.keys(payload)[0] } +/** + * wrapper method to add the timestamp as well + * @param {string} resolverName + * @param {*} payload + * @return {object} delierable + */ +export function createDeliverable(resolverName, payload) { + return { + [resolverName]: payload, + timestamp: [ timestamp() ] + } +} + /** * @param {string} resolverName name of function * @param {array} [args=[]] from the ...args @@ -49,7 +64,7 @@ export function createQuery(resolverName, args = [], jsonp = false) { if (jsonp === true) { return payload; } - return { [resolverName]: payload } + return createDeliverable(resolverName, payload) } throw new JsonqlValidationError(`[createQuery] expect resolverName to be string and args to be array!`, { resolverName, args }) } @@ -78,7 +93,7 @@ export function createMutation(resolverName, payload, condition = {}, jsonp = fa return _payload; } if (isString(resolverName)) { - return { [resolverName]: _payload } + return createDeliverable(resolverName, _payload) } throw new JsonqlValidationError(`[createMutation] expect resolverName to be string!`, { resolverName, payload, condition }) } -- Gitee