diff --git a/packages/rx-client/README.md b/packages/@jsonql/rx/README.md similarity index 100% rename from packages/rx-client/README.md rename to packages/@jsonql/rx/README.md diff --git a/packages/rx-client/package.json b/packages/@jsonql/rx/package.json similarity index 100% rename from packages/rx-client/package.json rename to packages/@jsonql/rx/package.json diff --git a/packages/rx-client/rollup.config.js b/packages/@jsonql/rx/rollup.config.js similarity index 100% rename from packages/rx-client/rollup.config.js rename to packages/@jsonql/rx/rollup.config.js diff --git a/packages/rx-client/src/base-client.js b/packages/@jsonql/rx/src/base-client.js similarity index 100% rename from packages/rx-client/src/base-client.js rename to packages/@jsonql/rx/src/base-client.js diff --git a/packages/rx-client/src/fetch-class.js b/packages/@jsonql/rx/src/fetch-class.js similarity index 100% rename from packages/rx-client/src/fetch-class.js rename to packages/@jsonql/rx/src/fetch-class.js diff --git a/packages/rx-client/src/generator.js b/packages/@jsonql/rx/src/generator.js similarity index 100% rename from packages/rx-client/src/generator.js rename to packages/@jsonql/rx/src/generator.js diff --git a/packages/rx-client/src/index.js b/packages/@jsonql/rx/src/index.js similarity index 100% rename from packages/rx-client/src/index.js rename to packages/@jsonql/rx/src/index.js diff --git a/packages/rx-client/src/lib/validator/index.js b/packages/@jsonql/rx/src/lib/validator/index.js similarity index 100% rename from packages/rx-client/src/lib/validator/index.js rename to packages/@jsonql/rx/src/lib/validator/index.js diff --git a/packages/rx-client/src/lib/validator/src/any.js b/packages/@jsonql/rx/src/lib/validator/src/any.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/any.js rename to packages/@jsonql/rx/src/lib/validator/src/any.js diff --git a/packages/rx-client/src/lib/validator/src/array.js b/packages/@jsonql/rx/src/lib/validator/src/array.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/array.js rename to packages/@jsonql/rx/src/lib/validator/src/array.js diff --git a/packages/rx-client/src/lib/validator/src/boolean.js b/packages/@jsonql/rx/src/lib/validator/src/boolean.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/boolean.js rename to packages/@jsonql/rx/src/lib/validator/src/boolean.js diff --git a/packages/rx-client/src/lib/validator/src/check-options.js b/packages/@jsonql/rx/src/lib/validator/src/check-options.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/check-options.js rename to packages/@jsonql/rx/src/lib/validator/src/check-options.js diff --git a/packages/rx-client/src/lib/validator/src/combine.js b/packages/@jsonql/rx/src/lib/validator/src/combine.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/combine.js rename to packages/@jsonql/rx/src/lib/validator/src/combine.js diff --git a/packages/rx-client/src/lib/validator/src/constants.js b/packages/@jsonql/rx/src/lib/validator/src/constants.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/constants.js rename to packages/@jsonql/rx/src/lib/validator/src/constants.js diff --git a/packages/rx-client/src/lib/validator/src/construct-config.js b/packages/@jsonql/rx/src/lib/validator/src/construct-config.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/construct-config.js rename to packages/@jsonql/rx/src/lib/validator/src/construct-config.js diff --git a/packages/rx-client/src/lib/validator/src/debug.js b/packages/@jsonql/rx/src/lib/validator/src/debug.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/debug.js rename to packages/@jsonql/rx/src/lib/validator/src/debug.js diff --git a/packages/rx-client/src/lib/validator/src/errors/checker-error.js b/packages/@jsonql/rx/src/lib/validator/src/errors/checker-error.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/errors/checker-error.js rename to packages/@jsonql/rx/src/lib/validator/src/errors/checker-error.js diff --git a/packages/rx-client/src/lib/validator/src/errors/enum-error.js b/packages/@jsonql/rx/src/lib/validator/src/errors/enum-error.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/errors/enum-error.js rename to packages/@jsonql/rx/src/lib/validator/src/errors/enum-error.js diff --git a/packages/rx-client/src/lib/validator/src/errors/index.js b/packages/@jsonql/rx/src/lib/validator/src/errors/index.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/errors/index.js rename to packages/@jsonql/rx/src/lib/validator/src/errors/index.js diff --git a/packages/rx-client/src/lib/validator/src/errors/type-error.js b/packages/@jsonql/rx/src/lib/validator/src/errors/type-error.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/errors/type-error.js rename to packages/@jsonql/rx/src/lib/validator/src/errors/type-error.js diff --git a/packages/rx-client/src/lib/validator/src/index.js b/packages/@jsonql/rx/src/lib/validator/src/index.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/index.js rename to packages/@jsonql/rx/src/lib/validator/src/index.js diff --git a/packages/rx-client/src/lib/validator/src/not-empty.js b/packages/@jsonql/rx/src/lib/validator/src/not-empty.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/not-empty.js rename to packages/@jsonql/rx/src/lib/validator/src/not-empty.js diff --git a/packages/rx-client/src/lib/validator/src/number.js b/packages/@jsonql/rx/src/lib/validator/src/number.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/number.js rename to packages/@jsonql/rx/src/lib/validator/src/number.js diff --git a/packages/rx-client/src/lib/validator/src/object.js b/packages/@jsonql/rx/src/lib/validator/src/object.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/object.js rename to packages/@jsonql/rx/src/lib/validator/src/object.js diff --git a/packages/rx-client/src/lib/validator/src/options/check-options-async.js b/packages/@jsonql/rx/src/lib/validator/src/options/check-options-async.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/options/check-options-async.js rename to packages/@jsonql/rx/src/lib/validator/src/options/check-options-async.js diff --git a/packages/rx-client/src/lib/validator/src/options/check-options-sync.js b/packages/@jsonql/rx/src/lib/validator/src/options/check-options-sync.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/options/check-options-sync.js rename to packages/@jsonql/rx/src/lib/validator/src/options/check-options-sync.js diff --git a/packages/rx-client/src/lib/validator/src/options/construct-config.js b/packages/@jsonql/rx/src/lib/validator/src/options/construct-config.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/options/construct-config.js rename to packages/@jsonql/rx/src/lib/validator/src/options/construct-config.js diff --git a/packages/rx-client/src/lib/validator/src/options/index.js b/packages/@jsonql/rx/src/lib/validator/src/options/index.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/options/index.js rename to packages/@jsonql/rx/src/lib/validator/src/options/index.js diff --git a/packages/rx-client/src/lib/validator/src/options/utils.js b/packages/@jsonql/rx/src/lib/validator/src/options/utils.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/options/utils.js rename to packages/@jsonql/rx/src/lib/validator/src/options/utils.js diff --git a/packages/rx-client/src/lib/validator/src/string.js b/packages/@jsonql/rx/src/lib/validator/src/string.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/string.js rename to packages/@jsonql/rx/src/lib/validator/src/string.js diff --git a/packages/rx-client/src/lib/validator/src/validator.js b/packages/@jsonql/rx/src/lib/validator/src/validator.js similarity index 100% rename from packages/rx-client/src/lib/validator/src/validator.js rename to packages/@jsonql/rx/src/lib/validator/src/validator.js diff --git a/packages/rx-client/src/options.js b/packages/@jsonql/rx/src/options.js similarity index 100% rename from packages/rx-client/src/options.js rename to packages/@jsonql/rx/src/options.js diff --git a/packages/rx-client/src/rx-client.js b/packages/@jsonql/rx/src/rx-client.js similarity index 100% rename from packages/rx-client/src/rx-client.js rename to packages/@jsonql/rx/src/rx-client.js diff --git a/packages/rx-client/src/socket-client-generator.js b/packages/@jsonql/rx/src/socket-client-generator.js similarity index 100% rename from packages/rx-client/src/socket-client-generator.js rename to packages/@jsonql/rx/src/socket-client-generator.js diff --git a/packages/express/package.json b/packages/express/package.json new file mode 100644 index 0000000000000000000000000000000000000000..6c5947be8f914cf41390ad8cb543f3d087e261a8 --- /dev/null +++ b/packages/express/package.json @@ -0,0 +1,16 @@ +{ + "name": "jsonql-express", + "version": "0.0.1", + "description": "jsonql express middleware", + "main": "index.js", + "scripts": { + "test": "ava" + }, + "keywords": [ + "jsonql", + "express", + "node" + ], + "author": "Joel Chu ", + "license": "ISC" +} diff --git a/packages/koa-ts/README.md b/packages/koa-ts/README.md deleted file mode 100644 index 61fbf29e52b64082ddadb6d7c6d442e8f0686f4c..0000000000000000000000000000000000000000 --- a/packages/koa-ts/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# ~~jsonql Koa middleware using Typescript~~ - -This project will no longer continue, instead we develop a Deno.js port using Typescript diff --git a/packages/koa-ts/client.ts b/packages/koa-ts/client.ts deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/packages/koa-ts/index.ts b/packages/koa-ts/index.ts deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/packages/koa-ts/package.json b/packages/koa-ts/package.json deleted file mode 100644 index 095757abe558b17bf14ba569b8b42d0062199e43..0000000000000000000000000000000000000000 --- a/packages/koa-ts/package.json +++ /dev/null @@ -1,75 +0,0 @@ -{ - "name": "jsonql-koa-ts", - "version": "0.0.1", - "description": "jsonql Koa middleware using Typescript", - "main": "index.ts", - "scripts": { - "test": "nyc ava", - "dev": "rollup -w", - "build": "rollup" - }, - "keywords": [ - "Typescript", - "Koa", - "Node.js", - "jsonql" - ], - "author": "NEWBRAN LTD ", - "license": "MIT", - "dependencies": { - "fs-extra": "^7.0.1", - "jsonql-constants": "^1.2.1", - "koa": "^2.7.0", - "koa-bodyparser": "^4.2.1", - "koa-compose": "^4.1.0", - "koa-json": "^2.0.2", - "koa-logger": "^3.2.0", - "koa-router": "^7.4.0", - "lodash-es": "^4.17.11", - "lodash.merge": "^4.6.1", - "lodash.trim": "^4.5.1" - }, - "devDependencies": { - "@types/koa": "^2.0.48", - "@types/koa-bodyparser": "^4.2.2", - "@types/koa-json": "^2.0.18", - "@types/koa-logger": "^3.1.1", - "@types/koa-router": "^7.0.40", - "@types/lodash-es": "^4.17.3", - "@types/lodash.merge": "^4.6.6", - "@types/lodash.trim": "^4.5.6", - "@types/node": "^11.13.0", - "ava": "^1.4.1", - "debug": "^4.1.1", - "nyc": "^13.3.0", - "rollup": "^1.9.0", - "rollup-plugin-typescript": "^1.0.1", - "superkoa": "^1.0.3", - "ts-node": "^8.0.3", - "tslib": "^1.9.3", - "typescript": "^3.4.2" - }, - "ava": { - "files": [ - "tests/*.test.ts", - "!tests/helpers/*.*", - "!tests/fixtures/*.*" - ], - "cache": true, - "concurrency": 5, - "failFast": true, - "failWithoutAssertions": false, - "tap": false, - "compileEnhancements": false, - "extensions": [ - "ts" - ], - "require": [ - "ts-node/register" - ] - }, - "repository": { - "type": "git", - "url": "git+ssh://git@gitee.com:to1source/jsonql.git" - } -} diff --git a/packages/koa-ts/rollup.config.js b/packages/koa-ts/rollup.config.js deleted file mode 100644 index 6e0332ad05ae308b10c5ec30c3b336b3b924095d..0000000000000000000000000000000000000000 --- a/packages/koa-ts/rollup.config.js +++ /dev/null @@ -1,8 +0,0 @@ -import typescript from 'rollup-plugin-typescript'; - -export default { - input: 'src/index.ts', - plugins: [ - typescript() - ] -} diff --git a/packages/koa-ts/src/auth.ts b/packages/koa-ts/src/auth.ts deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/packages/koa-ts/src/client.ts b/packages/koa-ts/src/client.ts deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/packages/koa-ts/src/contract.ts b/packages/koa-ts/src/contract.ts deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/packages/koa-ts/src/core.ts b/packages/koa-ts/src/core.ts deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/packages/koa-ts/src/hello.ts b/packages/koa-ts/src/hello.ts deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/packages/koa-ts/src/index.ts b/packages/koa-ts/src/index.ts deleted file mode 100644 index 8ec9fbe64b0610b1efee3b348fb9d2cf47b375f5..0000000000000000000000000000000000000000 --- a/packages/koa-ts/src/index.ts +++ /dev/null @@ -1,7 +0,0 @@ -// top level exports -export * from './lib/options.ts'; -export * from './auth.ts'; -export * from './core.ts'; -export * from './contract.ts'; -export * from './hello.ts'; -export * from './client.ts'; diff --git a/packages/koa-ts/src/lib/index.ts b/packages/koa-ts/src/lib/index.ts deleted file mode 100644 index 0e9e1ee825ea4112d23760f71792f234cc0074ac..0000000000000000000000000000000000000000 --- a/packages/koa-ts/src/lib/index.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { - headerParser, - getDocLen, - packError, - packResult, - printError, - isJsonqlRequest, - getCallMethod, - isHeaderPresent -} from './utils'; -import getFunction from './search'; -import { contractFn, readContract } from './contract'; -import ResolverNotFoundError from './resolver-not-found-error'; -import ResolverApplicationError from './resolver-application-error'; -// export -export { - headerParser, - getDocLen, - packError, - packResult, - printError, - // individual import - getFunction, - isJsonqlRequest, - getCallMethod, - isHeaderPresent, - contractFn, - readContract, - ResolverNotFoundError, - ResolverApplicationError -}; diff --git a/packages/koa-ts/src/lib/search.ts b/packages/koa-ts/src/lib/search.ts deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/packages/koa-ts/src/lib/utils.ts b/packages/koa-ts/src/lib/utils.ts deleted file mode 100644 index a31e953b312b3b36ba7ad40a953eb61a93c49c3b..0000000000000000000000000000000000000000 --- a/packages/koa-ts/src/lib/utils.ts +++ /dev/null @@ -1,153 +0,0 @@ -// util libraries -import * as fs from 'fs'; -import { join } from 'path'; -import { inspect } from 'util'; - -/** - * From underscore.string library - * @BUG if it's not a camel case such as FileToDash - * it will return -file-to-dash (with the extra - at the beginning) - * @param {string} str string - * @return {string} dasherize string - */ -export const dasherize = function(str: string): string { - return str.trim() - .replace(/([A-Z])/g, '-$1') - .replace(/[-_\s]+/g, '-') - .toLowerCase(); -}; - -/** - * Get document (string) byte length for use in header - * @param {string} doc to calculate - * @return {number} length - */ -export const getDocLen = function(doc: string): number { - return Buffer.byteLength(doc, 'utf8'); -}; - -/** - * The koa ctx object is not returning what it said on the documentation - * So I need to write a custom parser to check the request content-type - * @param {object} req the ctx.request - * @param {string} type (optional) to check against - * @return {mixed} Array or Boolean (cheat to use any type for now) - */ -export const headerParser = function(req: any, type?: string): any { - try { - const headers = req.headers.accept.split(','); - return type ? headers.filter(h => h === type) - : headers; - } catch (e) { - // When Chrome dev tool activate the headers become empty - return []; - } -}; - -/** - * wrapper of above method to make it easier to use - * @param {object} req ctx.request - * @param {string} type of header - * @return {boolean} - */ -export const isHeaderPresent = function(req: any, type: string): boolean { - const headers = headerParser(req, type); - return !!headers.length; -}; - -/** - * combine two check in one and save time - * @param {object} ctx koa - * @param {object} opts config - * @return {boolean} check result - */ -export const isJsonqlRequest = function(ctx: any, opts: any): boolean { - const header = isHeaderPresent(ctx.request, opts.contentType); - return header ? ctx.path === opts.jsonqlPath : false; -} - -/** - * getting what is calling after the above check - * @param {string} method of call - * @return {mixed} false on failed - */ -export const getCallMethod = function(method: string): string|boolean { - switch (method) { - case 'POST': - return 'query'; - case 'PUT': - return 'mutation'; - default: - return false; - } -} - -/** - * @param {string} name - * @param {string} type - * @param {object} opts - * @return {function} on success || boolean false on fail - */ -export const getPathToFn = function(name: string, type: string, opts: any): any { - const dir = opts.resolverDir; - const fileName = dasherize(name); - let paths = []; - if (opts.contract && opts.contract[type] && opts.contract[type].path) { - paths.push(opts.contract[type].path); - } - paths.push( join(dir, type, fileName, 'index.ts') ); - paths.push( join(dir, type, fileName + '.ts') ); - const ctn = paths.length; - for (let i=0; i { - t.is(await dasherize('fileToDash'), 'file-to-dash'); -}); - -test('Testing getDocLen', async t => { - t.truthy(await getDocLen('Just a test')); -}); diff --git a/packages/koa-ts/tsconfig.json b/packages/koa-ts/tsconfig.json deleted file mode 100644 index 7798006679768aa6d33d3e6257193964bdfef05d..0000000000000000000000000000000000000000 --- a/packages/koa-ts/tsconfig.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "compilerOptions": { - "outDir": "./dist", - "lib": ["es5", "es6"], - "target": "es6", - "module": "commonjs", - "moduleResolution": "node", - "emitDecoratorMetadata": true, - "experimentalDecorators": true - }, - "include": ["./src/**/*"] -} diff --git a/packages/koa/client.js b/packages/koa/client.js deleted file mode 100755 index 93b4925e5469b477241db1db1c3ebab08eab60bb..0000000000000000000000000000000000000000 --- a/packages/koa/client.js +++ /dev/null @@ -1,24 +0,0 @@ -const { join } = require('path') -const fs = require('fs') -const fsx = require('fs-extra') -const merge = require('lodash.merge') -const debug = require('debug')('jsonql-koa:client') -const jsonqlNodeClient = require('jsonql-node-client') - -/** - * This is for the resolvers to include and get their node-client - * @param {string} name this client - it get automatically generate if the user didn't provide one - * @param {object} config configuration - * @return {promise} to resolve the node client instance - */ -module.exports = function createNodeClient(name, config) { - if (!name) { - throw new Error('Name is required!') - } - if (!config.contractDir) { - throw new Error('contractDir is required') - } - config.contractDir = join(config.contractDir, name) - - return jsonqlNodeClient(config) -} diff --git a/packages/koa/index.js b/packages/koa/index.js index 24a2cc261cd08117eaee6d857b46d72f409f32e5..a02666e619e59ee28a26b225657006789e7615d3 100755 --- a/packages/koa/index.js +++ b/packages/koa/index.js @@ -12,7 +12,6 @@ const { authMiddleware, contractMiddleware, helloMiddleware, - clientGenerator, consoleMiddleware, publicMethodMiddleware, errorsHandlerMiddleware, diff --git a/packages/koa/package.json b/packages/koa/package.json index 6957c70992f394498d3329a9c3cb1555939a10d7..71fe3dbae405667311b36ebe7df0397a7a4dbe5e 100755 --- a/packages/koa/package.json +++ b/packages/koa/package.json @@ -1,12 +1,11 @@ { "name": "jsonql-koa", - "version": "1.3.7", + "version": "1.3.8", "description": "jsonql Koa middleware", "main": "index.js", "files": [ "src", "index.js", - "client.js", "contract.js" ], "scripts": { diff --git a/packages/koa/src/client-generator.js b/packages/koa/src/client-generator.js deleted file mode 100755 index cbadf720cdf484b9c60fef57bd0850d930f07f17..0000000000000000000000000000000000000000 --- a/packages/koa/src/client-generator.js +++ /dev/null @@ -1,47 +0,0 @@ -const { join } = require('path') -const fs = require('fs') -const fsx = require('fs-extra') -const contractApi = require('jsonql-contract') -const debug = require('debug')('jsonql-koa:clientGenerator') -const { CLIENT_CONFIG_FILE } = require('jsonql-constants') -// const config = require('config'); - -// @TODO move this to the node-client instead - -/** - * this will construct the client for resolvers to use - * The idea is simple we pass the client config to the node-client - * it generate the contract files, and when the developer need to call it - * just simply include the ./client using the name and it will locate it for them - * @param {object} opts the full configuration - * @return {undefined} nothing - */ -module.exports = function(opts) { - let clientConfig = []; - let clientLocalConfig = {}; - const nodeClient = opts.nodeClient; - const contractDir = opts.contractDir; - for (let name in nodeClient) { - if (nodeClient.hostname) { - let obj = {}; - obj.remote = nodeClient.hostname; - if (nodeClient.contractKey) { - obj.remote = '?' + (nodeClient.contractKeyName || opts.contractKeyName) + '=' + opts.contractKey; - } - let cdir = join(contractDir, name); - obj.out = cdir; - clientConfig.push(obj); - // this is for keeping a record - clientLocalConfig[name] = { - contractDir: cdir, - hostname: nodeClient.hostname - }; - } else { - debug(`${name} node client is missing hostname! skipped`) - } - } - return Promise.all(clientopts.map(contractApi)).then(contracts => { - // create a .clients.json in the contract folder - fsx.outputJsonSync(join(process.cwd(), CLIENT_CONFIG_FILE), clientLocalConfig) - }); -} diff --git a/packages/koa/src/index.js b/packages/koa/src/index.js index 91f95d302a3f795b5d0fd222c34bda360b346454..ef7bdf98758548b4a249cd76bc509be0de86c399 100755 --- a/packages/koa/src/index.js +++ b/packages/koa/src/index.js @@ -4,7 +4,7 @@ const authMiddleware = require('./auth-middleware') const coreMiddleware = require('./core-middleware') const contractMiddleware = require('./contract-middleware') const helloMiddleware = require('./hello-middleware') -const clientGenerator = require('./client-generator') + const consoleMiddleware = require('./console-middleware') const publicMethodMiddleware = require('./public-method-middleware') @@ -20,7 +20,7 @@ module.exports = { coreMiddleware, contractMiddleware, helloMiddleware, - clientGenerator, + publicMethodMiddleware, errorsHandlerMiddleware, initMiddleware diff --git a/packages/koa/src/lib/config-check/index.js b/packages/koa/src/lib/config-check/index.js index 9570e25d1910f028455bea211f499edeff3c5823..f166f154eb6eb6165522e729de65975b372a57e2 100644 --- a/packages/koa/src/lib/config-check/index.js +++ b/packages/koa/src/lib/config-check/index.js @@ -9,35 +9,6 @@ const { getContract, isContractJson, chainFns, getDebug, inArray } = require('.. const debug = getDebug('config-check') -/** - * Double check if the client config is correct or not also we could inject some of the properties here - * @param {object} config configuration - * @return {object} with additional properties - */ -const validateClientConfig = function(config) { - let ctn = config.clientConfig.length - if (ctn) { - let names = [] - let clients = [] - for (let i = 0; i < ctn; ++i) { - let client = config.clientConfig[i] - if (!client.hostname) { - throw new Error(`Missing hostname in client config ${i}`) - } - if (!client.name) { - client.name = `nodeClient${i}` - } - if (inArray(client.name, names)) { - throw new Error(`[${i}] ${client.name} already existed, can not have duplicated!`) - } - names.push(client.name) - clients.push(client) - } - config.clientConfig = clients; - } - return config; -} - /** * break out from the applyAuthOptions because it's not suppose to be there * @param {object} config configuration @@ -93,6 +64,6 @@ const applyAuthOptions = function(config) { * @api public */ module.exports = function configCheck(config) { - const fn = chainFns(checkConfig, validateClientConfig, applyGetContract, applyAuthOptions) + const fn = chainFns(checkConfig, applyGetContract, applyAuthOptions) return fn(config, appProps, constProps) } diff --git a/packages/node-client/package.json b/packages/node-client/package.json index d9e8d209ad891c0631b16b529678efa5ae59b2a4..44b99fa07910634e6415cec5e5d6477db65695d2 100755 --- a/packages/node-client/package.json +++ b/packages/node-client/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-node-client", - "version": "1.1.4", + "version": "1.1.5", "description": "jsonql node.js client using request", "main": "index.js", "scripts": { diff --git a/packages/node-client/src/check-options.js b/packages/node-client/src/check-options.js index ac6246cfbb7cdacbd969293f82d5314dea93569a..67afb0192c47358a0eff6bcba54b80b7b2987210 100755 --- a/packages/node-client/src/check-options.js +++ b/packages/node-client/src/check-options.js @@ -59,7 +59,7 @@ const appProps = { contractKey: constructConfig(false, [BOOLEAN_TYPE, STRING_TYPE]), // if the server side is lock by the key you need this contractKeyName: constructConfig(CONTRACT_KEY_NAME, STRING_TYPE),// same as above they go in pairs // we don't really need to check this, it will get generated - contractDir: constructConfig( join(BASE_DIR, DEFAULT_RESOLVER_DIR), STRING_TYPE), + contractDir: constructConfig( join(BASE_DIR, DEFAULT_CONTRACT_DIR), STRING_TYPE), contractFileName: constructConfig(PUBLIC_CONTRACT_FILE_NAME, STRING_TYPE), // functions storeAuthToken: constructConfig(false, BOOLEAN_TYPE), diff --git a/packages/node-client/src/jsonql/get-remote-contract.js b/packages/node-client/src/jsonql/get-remote-contract.js index 3b6ef192ec836e45741298d35f6c4f63303f8abc..8efecfe77e04a54167eb7b2ec1c2afbc91f251d6 100755 --- a/packages/node-client/src/jsonql/get-remote-contract.js +++ b/packages/node-client/src/jsonql/get-remote-contract.js @@ -34,7 +34,9 @@ module.exports = function getRemoteContract(args) { return rejecter(json.error || 'Server return wrong contract format') } const contract = json.data; - const outFile = join( resolve(args.out) || process.cwd(), PUBLIC_CONTRACT_FILE_NAME ) + const outDir = args.out ? resolve(args.out) : process.cwd() + const outFile = join(outDir, PUBLIC_CONTRACT_FILE_NAME) + debug(`outFile to: ${outFile}`) // v1.1.4 add raw output if (args.raw) { return resolver(contract) diff --git a/packages/node-client/src/jsonql/jsonql-base-cls.js b/packages/node-client/src/jsonql/jsonql-base-cls.js index ec813291c0b4c6562ea548233bfa090714ed936f..8d9e9373bbc3a7917be13ed8e1e26c240ba64ce4 100755 --- a/packages/node-client/src/jsonql/jsonql-base-cls.js +++ b/packages/node-client/src/jsonql/jsonql-base-cls.js @@ -156,6 +156,7 @@ class JsonqlClient { if (this.opts.contractKey !== false) { authHeader = {[this.opts.contractKeyName]: this.opts.contractKey} } + debug(`contract out: ${this.opts.contractDir}`) // use the remote method to get it return getRemote(Object.assign({ remote: this.__url__, diff --git a/packages/node-client/src/jsonql/request-client.js b/packages/node-client/src/jsonql/request-client.js index 6f125963c9c8023e2c41507148ad030fa226059f..ffc5c01d2fe11c262b1aa913f606fca7a8fb4c71 100755 --- a/packages/node-client/src/jsonql/request-client.js +++ b/packages/node-client/src/jsonql/request-client.js @@ -26,7 +26,7 @@ class JsonqlRequestClient extends JsonqlClient { return fsx.readJsonSync(result); } return result; - }); + }) } /** @@ -60,7 +60,7 @@ class JsonqlRequestClient extends JsonqlClient { debug('error happens here!', e) rejecter(e) } - }); + }) } /** diff --git a/packages/resolver/index.js b/packages/resolver/index.js index 9ef78303fc8a4727a34b3e125eacef3cf7c87675..e98a3cabcd11eb883365ed5b70c184c943e8b8a8 100644 --- a/packages/resolver/index.js +++ b/packages/resolver/index.js @@ -1,11 +1,13 @@ +const searchResolvers = require('./src/search-resolvers') +const validateAndCall = require('./src/validate-and-call') const { - getLocalValidator, executeResolver, resolverRenderHandler } = require('./src/resolve-methods') -const searchResolvers = require('./src/search-resolvers') -const validateAndCall = require('./src/validate-and-call') -const handleAuthMethods = require('./src/handle-auth-methods') +const { + getLocalValidator, + handleAuthMethods +}= require('./src/handle-auth-methods') // @TODO use the same for the jsonql-ws-server as well module.exports = { diff --git a/packages/resolver/package.json b/packages/resolver/package.json index 45b0d74380d674c690972a46d6c023f1d8a53dea..b78bb76fe2003f2eb77fca574cca36dc4d7eac7b 100644 --- a/packages/resolver/package.json +++ b/packages/resolver/package.json @@ -8,7 +8,8 @@ "index.js" ], "scripts": { - "test": "ava", + "test": "ava --verbose", + "test:clients": "DEBUG=jsonql-resolver* ava --verbose ./tests/clients.test.js", "contract": "jsonql-contract create ./tests/fixtures/resolvers ./tests/fixtures/contract" }, "keywords": [ @@ -21,11 +22,15 @@ "jsonql-constants": "^1.7.9", "jsonql-errors": "^1.1.1", "jsonql-jwt": "^1.2.5", - "jsonql-params-validator": "^1.4.3" + "jsonql-node-client": "^1.1.4", + "jsonql-params-validator": "^1.4.3", + "lodash.merge": "^4.6.2" }, "devDependencies": { "ava": "^2.2.0", - "jsonql-contract": "^1.7.7" + "jsonql-contract": "^1.7.7", + "jsonql-koa": "^1.3.7", + "server-io-core": "^1.2.0" }, "ava": { "files": [ diff --git a/packages/resolver/src/client/clients-generator.js b/packages/resolver/src/client/clients-generator.js new file mode 100755 index 0000000000000000000000000000000000000000..c23fd85782f994352519ede95771b1ce4de3938b --- /dev/null +++ b/packages/resolver/src/client/clients-generator.js @@ -0,0 +1,18 @@ +const jsonqlNodeClient = require('jsonql-node-client') +/** + * create the node client using the already passed configurations + * @param {object} opts the clients configuration + * @return {promise} resolve to the node client + */ +module.exports = function(opts) { + return Promise.all( + opts.map(opt => { + let name = opt.name; + return jsonqlNodeClient(opt) + .then(client => { + client.name = name; + return client; + }) + }) + ) +} diff --git a/packages/resolver/src/client/index.js b/packages/resolver/src/client/index.js new file mode 100755 index 0000000000000000000000000000000000000000..62defabc87bdb454f4f0448bdd9621d0be4b387a --- /dev/null +++ b/packages/resolver/src/client/index.js @@ -0,0 +1,10 @@ +// import export +const injectNodeClient = require('./inject-node-clients') +const validateClientConfig = require('./validate-client-config') +const clientsGenerator = require('./clients-generator') +// export +module.exports = { + injectNodeClient, + validateClientConfig, + clientsGenerator +} diff --git a/packages/resolver/src/client/inject-node-clients.js b/packages/resolver/src/client/inject-node-clients.js new file mode 100644 index 0000000000000000000000000000000000000000..d8abe5c6cd4fb1b46dd1945c4a51944c4d84ac0a --- /dev/null +++ b/packages/resolver/src/client/inject-node-clients.js @@ -0,0 +1,45 @@ +const { getDebug } = require('../utils') +const debug = getDebug(`inject-node-clients`) +/** + * kind of curry the function back to inject into the resolver + * @param {array} clients the node clients + * @return {function} to the end resolver to use + */ +function resolveClients(clients) { + /** + * When pass as number then check this index, if its name then + * find with the name property, all failed then return false + * @param {number|string} + * @return {boolean|object} false when failed + */ + return function(name) { + debug(`resolveClients ${name}`, clients) + if (typeof name === 'string') { + return clients + .filter(client => client.name === name) + .reduce((base, result) => { + return result || base; + }, false) + } + if (clients[name]) { + return clients[name] + } + return false; + } +} + +/** + * inject the clients to the resolver + * @param {function} resolver the resolver to get injection + * @param {array} clients the jsonql node clients + * @return {function} the injected resolver + */ +module.exports = function injectNodeClient(resolver, clients) { + if (Object.getOwnPropertyDescriptor(resolver, 'clients') === undefined) { + Object.defineProperty(resolver, 'clients', { + value: resolveClients(clients), + writable: false // make this immutatble + }) + } + return resolver +} diff --git a/packages/resolver/src/client/validate-client-config.js b/packages/resolver/src/client/validate-client-config.js new file mode 100644 index 0000000000000000000000000000000000000000..eca0d2cad81da116a713612f0f1bf52438654c76 --- /dev/null +++ b/packages/resolver/src/client/validate-client-config.js @@ -0,0 +1,38 @@ +const { join } = require('path') +const { getDebug, inArray } = require('../utils') +const debug = getDebug('validate-client-config') +/** + * Double check if the client config is correct or not also we could inject some of the properties here + * @param {object} config configuration + * @return {object|boolean} false when there is none, with additional properties + */ +const validateClientConfig = function(config) { + const ctn = config.clientConfig.length; + // take the contractDir from the main + const contractDir = config.contractDir; + if (ctn) { + let names = [] + let clients = [] + for (let i = 0; i < ctn; ++i) { + let client = config.clientConfig[i] + if (!client.hostname) { + throw new Error(`Missing hostname in client config ${i}`) + } + debug(`name: ${client.name}`) + if (!client.name) { + client.name = `nodeClient${i}` + } + if (inArray(names, client.name)) { + throw new Error(`[${i}] ${client.name} already existed, can not have duplicated!`) + } + names.push(client.name) + // next we need to create contract dir path using the name + clients.contractDir = join(contractDir, client.name) + clients.push(client) + } + return clients; + } + return false; +} + +module.exports = validateClientConfig diff --git a/packages/resolver/src/handle-auth-methods.js b/packages/resolver/src/handle-auth-methods.js index 531a8cc40737857fe0944def3483845efe4a3dab..33e846e5cd2854e1fb9fc9cc825985da3cabef01 100644 --- a/packages/resolver/src/handle-auth-methods.js +++ b/packages/resolver/src/handle-auth-methods.js @@ -24,8 +24,25 @@ const { getErrorNameByInstance, UNKNOWN_ERROR } = require('jsonql-errors') + const debug = getDebug('public-method-middleware') +/** + * if useJwt = true then use the jsonql-jwt version + * @param {object} config configuration + * @param {string} type type of call + * @param {object} contract contract.json + * @return {function} the correct handler + */ +const getLocalValidator = (config, type, contract) => { + // if you explicitly set it to false then it will be ignore + if (config.validatorHandlerName === false) { + return false; + } + let validatorFnPath = searchResolvers(config.validatorHandlerName, type, config, contract) + return require(validatorFnPath) +} + /** * This is move from the auth middleware to handle the ISSUER_NAME and LOGOUT_NAME * They both are always publicly available @@ -39,8 +56,8 @@ const handleAuthMethods = async function(ctx, resolverName, payload, opts, contr try { const { loginHandlerName } = opts; // @TODO here we intercept the result and add extra output if the useJwt is enabled - const issuerFnPath = searchResolvers(loginHandlerName, AUTH_TYPE, opts, contract); - const issuerFn = require(issuerFnPath); + const issuerFnPath = searchResolvers(loginHandlerName, AUTH_TYPE, opts, contract) + const issuerFn = require(issuerFnPath) const result = await validateAndCall( issuerFn, @@ -70,4 +87,7 @@ const handleAuthMethods = async function(ctx, resolverName, payload, opts, contr } } -module.exports = handleAuthMethods; +module.exports = { + getLocalValidator, + handleAuthMethods +} diff --git a/packages/resolver/src/provide-node-clients.js b/packages/resolver/src/provide-node-clients.js new file mode 100644 index 0000000000000000000000000000000000000000..d35f96cd37a76bd865cf8f2ac89265dd300c2ed1 --- /dev/null +++ b/packages/resolver/src/provide-node-clients.js @@ -0,0 +1,39 @@ +// setup and inject the clients into resolver +const { + injectNodeClient, + validateClientConfig, + clientsGenerator +} = require('./client') +const { + getDebug +} = require('./utils') +const debug = getDebug('provide-node-clients') +let clients = [] +let hasClientConfig; + +/** + * The top level methods to inject the clients into the resolver + * @param {function} resolver the function to get injection + * @param {object} config configuration + * @return {function} the resolver with injection if any + */ +module.exports = async function provideNodeClients(resolver, config) { + if (hasClientConfig === false) { + debug(`nothing to inject`) + return resolver; // nothing to do + } + // if there is cache clients + if (clients.length) { + debug(`inject client from cache`) + return injectNodeClient(resolver, clients) + } + hasClientConfig = validateClientConfig(config) + if (hasClientConfig === false) { + debug(`check and nothing to inject`, config) + return resolver; // nothing to do + } + debug(`run init clients`) + // run init client once + clients = await clientsGenerator(hasClientConfig) + return injectNodeClient(resolver, clients) +} diff --git a/packages/resolver/src/resolve-methods.js b/packages/resolver/src/resolve-methods.js index 6b9d4236fc65f616aeb6ec954ee75f08bd63edce..bcfa0d134a4a7cf5dbeeff5e200a0293a65dc856 100644 --- a/packages/resolver/src/resolve-methods.js +++ b/packages/resolver/src/resolve-methods.js @@ -21,26 +21,10 @@ const { } = require('./utils') const searchResolvers = require('./search-resolvers') const validateAndCall = require('./validate-and-call') +const provideNodeClients = require('./provide-node-clients') const debug = getDebug('resolve-method') - -/** - * if useJwt = true then use the jsonql-jwt version - * @param {object} config configuration - * @param {string} type type of call - * @param {object} contract contract.json - * @return {function} the correct handler - */ -const getLocalValidator = (config, type, contract) => { - // if you explicitly set it to false then it will be ignore - if (config.validatorHandlerName === false) { - return false; - } - let validatorFnPath = searchResolvers(config.validatorHandlerName, type, config, contract) - return require(validatorFnPath) -} - /** * A new method breaking out the get resolver and prepare code * then make the original resolveMethod as a koa render handler @@ -62,6 +46,8 @@ const executeResolver = async (opts, type, resolverName, payload, contract, user fn = require(searchResolvers(resolverName, type, opts, contract)) } const args = extractArgsFromPayload(payload, type) + // inject the node client if any + fn = await provideNodeClients(fn, opts) // here we could apply the userdata to the method const result = await validateAndCall( provideUserdata(fn, userdata), // always call it @TODO need to provide the nodeClient as well @@ -112,7 +98,6 @@ const resolverRenderHandler = async (ctx, type, opts, contract) => { // named export instead module.exports = { - getLocalValidator, executeResolver, resolverRenderHandler } diff --git a/packages/resolver/src/search-resolvers.js b/packages/resolver/src/search-resolvers.js index 072bbf2dc47734a22de039bb71367dfff036cdd9..b986b10ba760cb82f376c140c56ebaf26019354f 100644 --- a/packages/resolver/src/search-resolvers.js +++ b/packages/resolver/src/search-resolvers.js @@ -42,6 +42,7 @@ module.exports = function searchResolvers(name, type, opts, contract) { if (search !== false) { return search; } + debug(`contract is not contract?`, contract) // search by running const filePath = getPathToFn(name, type, opts) if (filePath) { diff --git a/packages/resolver/src/utils.js b/packages/resolver/src/utils.js index 5a1ab6b9b1aefded538539cc54ac9d7a37f3bf00..eb4681c490c3476951e5252749506585657fe28d 100644 --- a/packages/resolver/src/utils.js +++ b/packages/resolver/src/utils.js @@ -22,6 +22,19 @@ const { DEFAULT_RESOLVER_IMPORT_FILE_NAME, } = require('jsonql-constants') +/** + * From underscore.string library + * @BUG there is a bug here with the non-standard name start with _ + * @param {string} str string + * @return {string} dasherize string + */ +const dasherize = str => ( + trim(str) + .replace(/([A-Z])/g, '-$1') + .replace(/[-_\s]+/g, '-') + .toLowerCase() +) + // export a create debug method const debug = require('debug') /** @@ -189,6 +202,19 @@ function importFromModule(resolverDir, type, resolverName) { return resolvers[type + resolverName] } +/** + * @param {array} clients node clients + * @param {string} name of the client + * @return {object|false} the client, false when failed + */ +function findClient(clients, name) { + return clients + .filter(client => client.name === name) + .reduce((base, result) => { + return result || base; + }, false) +} + // export module.exports = { inArray, @@ -201,5 +227,6 @@ module.exports = { isObject, handleOutput, extractArgsFromPayload, - importFromModule + importFromModule, + findClient } diff --git a/packages/resolver/tests/base.test.js b/packages/resolver/tests/base.test.js index ca0c4e3166b3987b6aca9b65b42622ce2f807242..eccddc8310a5af4da03221bcb3c8a07187d547e1 100644 --- a/packages/resolver/tests/base.test.js +++ b/packages/resolver/tests/base.test.js @@ -1,19 +1,9 @@ const test = require('ava') -const fs = require('fs') + const { join } = require('path') const { executeResolver } = require('../') const { createQuery } = require('jsonql-params-validator') - -const readJson = file => { - return new Promise((resolver, rejecter) => { - fs.readFile(file, 'utf8', function (err, data) { - if (err) { - return rejecter(err) - } - resolver(JSON.parse(data)) - }) - }) -} +const readJson = require('./fixtures/read-json') const contractPath = join(__dirname, 'fixtures', 'contract', 'contract.json') @@ -24,7 +14,7 @@ test.before(async t => { contractDir: join(__dirname, 'fixtures', 'contract') } }) - + test(`It should able to call a resolver`, async t => { const resolverName = 'getSomething'; const payload = createQuery(resolverName, ['Joel']) diff --git a/packages/resolver/tests/clients.test.js b/packages/resolver/tests/clients.test.js new file mode 100644 index 0000000000000000000000000000000000000000..011043d73bca06884a9a06c2817106bd09c587fe --- /dev/null +++ b/packages/resolver/tests/clients.test.js @@ -0,0 +1,69 @@ +// this is for testing the inject client +const test = require('ava') +const debug = require('debug')('jsonql-resolver:test:clients') +const anotherServer = require('./fixtures/another-server') +const nodeClient = require('jsonql-node-client') +const { findClient } = require('../src/utils') +const { join } = require('path') +const { executeResolver } = require('../') +const readJson = require('./fixtures/read-json') +const { createQuery } = require('jsonql-params-validator') +const baseDir = join(__dirname, 'fixtures') + +const contractPath = join(baseDir, 'contract', 'contract.json') + +test.before(async t => { + const { stop, app } = anotherServer() + t.context.stop = stop + t.context.app = app + + t.context.clientConfig = { + hostname: 'http://localhost:8001' + } + + t.context.contract = await readJson(contractPath) + t.context.opts = { + resolverDir: join(baseDir, 'resolvers'), + contractDir: join(baseDir, 'contract') + } + +}) + +test.after(t => { + t.context.stop() +}) + +test(`Just testing the filter / reduce method`, t => { + let name = 'name0' + let clients = [{ name }] + let result = findClient(clients, name) + t.not(false, result) + let result1 = findClient([], name) + t.is(false, result1) +}) + +test(`Test the nodeClient is working correctly with the configuration`, async t => { + const client = await nodeClient(t.context.clientConfig) + const result = await client.query.anotherService(`One`) + t.truthy(result.indexOf('another')) +}) + +test.only(`The callMsService resolver should able to call another jsonql service`, async t => { + + const resolverName = 'callMsService'; + const payload = createQuery(resolverName, ['Joel']) + + let opts = t.context.opts; + opts.clientConfig = [t.context.clientConfig]; + + const result = await executeResolver( + opts, + 'query', + resolverName, + payload[resolverName], + t.context.contract + ) + + t.truthy(result.indexOf('another service')) + +}) diff --git a/packages/resolver/tests/fixtures/another-server.js b/packages/resolver/tests/fixtures/another-server.js new file mode 100644 index 0000000000000000000000000000000000000000..7c01e161a0ecbdcdc696a9f18fc46e851465ee58 --- /dev/null +++ b/packages/resolver/tests/fixtures/another-server.js @@ -0,0 +1,19 @@ +// this is the ms run on 8001 + +const serverIoCore = require('server-io-core') +const koa = require('jsonql-koa') +const { join } = require('path') +// export the return for use later +module.exports = () => serverIoCore({ + webroot: ['./dummy'], + port: 8001, + debugger: false, + open: false, + socket: false, + middlewares: [ + koa({ + resolverDir: join(__dirname, 'resolvers'), + contractDir: join(__dirname, 'contract', 'another') + }) + ] +}) diff --git a/packages/resolver/tests/fixtures/contract/another/contract.json b/packages/resolver/tests/fixtures/contract/another/contract.json new file mode 100644 index 0000000000000000000000000000000000000000..94ca58699eb95f99093024a9c31c503672d6da1f --- /dev/null +++ b/packages/resolver/tests/fixtures/contract/another/contract.json @@ -0,0 +1,70 @@ +{ + "query": { + "anotherService": { + "file": "/home/joel/projects/open-source/jsonql/packages/resolver/tests/fixtures/resolvers/query/another-service.js", + "description": false, + "params": [ + { + "type": [ + "string" + ], + "name": "msg", + "description": "input" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "msg output" + } + ] + }, + "callMsService": { + "file": "/home/joel/projects/open-source/jsonql/packages/resolver/tests/fixtures/resolvers/query/call-ms-service.js", + "description": "pass a txt value then process else where", + "params": [ + { + "type": [ + "string" + ], + "name": "txt" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "transformted txt" + } + ] + }, + "getSomething": { + "file": "/home/joel/projects/open-source/jsonql/packages/resolver/tests/fixtures/resolvers/query/get-something.js", + "description": false, + "params": [ + { + "type": [ + "string" + ], + "name": "name", + "description": "pass a name" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "name with prefix" + } + ] + } + }, + "mutation": {}, + "auth": {}, + "timestamp": 1566096001328, + "sourceType": "script" +} diff --git a/packages/resolver/tests/fixtures/contract/another/public-contract.json b/packages/resolver/tests/fixtures/contract/another/public-contract.json new file mode 100644 index 0000000000000000000000000000000000000000..162f44941e5df8652428b3913d6894ba8505982a --- /dev/null +++ b/packages/resolver/tests/fixtures/contract/another/public-contract.json @@ -0,0 +1,76 @@ +{ + "query": { + "helloWorld": { + "description": "This is the stock resolver for testing purpose", + "params": [], + "returns": [ + { + "type": "string", + "description": "stock message" + } + ] + }, + "anotherService": { + "description": false, + "params": [ + { + "type": [ + "string" + ], + "name": "msg", + "description": "input" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "msg output" + } + ] + }, + "callMsService": { + "description": "pass a txt value then process else where", + "params": [ + { + "type": [ + "string" + ], + "name": "txt" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "transformted txt" + } + ] + }, + "getSomething": { + "description": false, + "params": [ + { + "type": [ + "string" + ], + "name": "name", + "description": "pass a name" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "name with prefix" + } + ] + } + }, + "mutation": {}, + "auth": {}, + "timestamp": 1566096001328 +} diff --git a/packages/resolver/tests/fixtures/contract/contract.json b/packages/resolver/tests/fixtures/contract/contract.json index 995ddff7103abb8eab8145f12f1f0b6e65a89917..ccb1840ad3c6d5afb77d67afe14a2f521c420a9f 100644 --- a/packages/resolver/tests/fixtures/contract/contract.json +++ b/packages/resolver/tests/fixtures/contract/contract.json @@ -1,5 +1,46 @@ { "query": { + "anotherService": { + "file": "/home/joel/projects/open-source/jsonql/packages/resolver/tests/fixtures/resolvers/query/another-service.js", + "description": false, + "params": [ + { + "type": [ + "string" + ], + "name": "msg", + "description": "input" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "msg output" + } + ] + }, + "callMsService": { + "file": "/home/joel/projects/open-source/jsonql/packages/resolver/tests/fixtures/resolvers/query/call-ms-service.js", + "description": "pass a txt value then process else where", + "params": [ + { + "type": [ + "string" + ], + "name": "txt" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "transformted txt" + } + ] + }, "getSomething": { "file": "/home/joel/projects/open-source/jsonql/packages/resolver/tests/fixtures/resolvers/query/get-something.js", "description": false, @@ -24,6 +65,6 @@ }, "mutation": {}, "auth": {}, - "timestamp": 1565785436688, + "timestamp": 1566054454129, "sourceType": "script" } diff --git a/packages/resolver/tests/fixtures/dummy/index.html b/packages/resolver/tests/fixtures/dummy/index.html new file mode 100644 index 0000000000000000000000000000000000000000..d1fdcefb8fae3b9a5759efe6bdaad4f1dfabd1ba --- /dev/null +++ b/packages/resolver/tests/fixtures/dummy/index.html @@ -0,0 +1,9 @@ + + + + dummy + + + dummy + + diff --git a/packages/resolver/tests/fixtures/read-json.js b/packages/resolver/tests/fixtures/read-json.js new file mode 100644 index 0000000000000000000000000000000000000000..499cf6b0dbb2eaac00e3cabc75ef1f9c7f465b7a --- /dev/null +++ b/packages/resolver/tests/fixtures/read-json.js @@ -0,0 +1,14 @@ +const fs = require('fs') + +const readJson = file => { + return new Promise((resolver, rejecter) => { + fs.readFile(file, 'utf8', function (err, data) { + if (err) { + return rejecter(err) + } + resolver(JSON.parse(data)) + }) + }) +} + +module.exports = readJson diff --git a/packages/resolver/tests/fixtures/resolvers/query/another-service.js b/packages/resolver/tests/fixtures/resolvers/query/another-service.js new file mode 100644 index 0000000000000000000000000000000000000000..41c09f9dc28430a6a524501b733aed7e70c3de71 --- /dev/null +++ b/packages/resolver/tests/fixtures/resolvers/query/another-service.js @@ -0,0 +1,9 @@ +// this will get call from a different setup + +/** + * @param {string} msg input + * @return {string} msg output + */ +module.exports = function anotherService(msg) { + return msg + ' pass another service'; +} diff --git a/packages/resolver/tests/fixtures/resolvers/query/call-ms-service.js b/packages/resolver/tests/fixtures/resolvers/query/call-ms-service.js new file mode 100644 index 0000000000000000000000000000000000000000..db4be926d64a2e1babb32cf520a70db9501b8f8f --- /dev/null +++ b/packages/resolver/tests/fixtures/resolvers/query/call-ms-service.js @@ -0,0 +1,15 @@ +const debug = require('debug')('jsonql-resolver:call-ms-service') +/** + * pass a txt value then process else where + * @param {string} txt + * @return {string} transformted txt + */ +module.exports = async function callMsService(txt) { + // call another jsonql service + const client0 = await callMsService.clients('nodeClient0') + + debug(client0) + + let txt0 = ['callMsService', txt].join(' ') + return await client0.query.anotherService(txt0) +}