diff --git a/packages/contract-cli/README.md b/packages/contract-cli/README.md
index 1fee04f1eb8e2c2629b127feff14b99540c954db..3294e2ef3c4c39b0d590130b39fc7aba02e6a384 100755
--- a/packages/contract-cli/README.md
+++ b/packages/contract-cli/README.md
@@ -219,3 +219,10 @@ We will take the jsdoc output and create an additional temporary contract file (
---
MIT (c) [to1source](https://to1source.cn) & [NEWBRAN LTD](https://newbran.ch)
+
+
+"socket.io-client": "^2.2.0",
+"ts-node": "^8.3.0",
+"typescript": "^3.5.3",
+"ws": "^7.1.2",
+"@types/node": "^12.7.1",
diff --git a/packages/contract-cli/lib/public-contract/index.js b/packages/contract-cli/lib/public-contract/index.js
index 0eb6e281b032251e1d8317b4ec7360fadc37b881..6322cb74cb795f258a2bc1f3a13e62d76a3f7846 100644
--- a/packages/contract-cli/lib/public-contract/index.js
+++ b/packages/contract-cli/lib/public-contract/index.js
@@ -72,6 +72,7 @@ function getEnvContractFile(contractDir) {
function getExpired(config, contractJson) {
const { expired } = config;
const { timestamp } = contractJson;
+ // the timestamp now comes with milsecond ...
if (expired && expired > timestamp) {
return { expired }
}
diff --git a/packages/contract-cli/lib/utils.js b/packages/contract-cli/lib/utils.js
index 910833698a7bb4e4594afc677bb78facfd940ef4..413a57610790d6487d8f8b0a9964360fd283db8f 100644
--- a/packages/contract-cli/lib/utils.js
+++ b/packages/contract-cli/lib/utils.js
@@ -8,8 +8,8 @@ const checkOptions = require('./options')
const { transform } = require('lodash')
const debug = require('debug')('jsonql-contract:utils')
const { JsonqlError } = require('jsonql-errors')
-
-const getTimestamp = () => Math.round(Date.now()/1000)
+// timestamp with mil seconds
+const getTimestamp = () => Date.now()
/**
* check if there is a config file and use that value instead
@@ -92,7 +92,7 @@ const checkFile = config => {
if (config.returnAs === 'file') {
if (!fsx.existsSync(dist)) {
throw new JsonqlError('File is not generated!', dist)
- }
+ }
console.info('Your contract file generated in: %s', dist)
} else {
if (!isObject(dist)) {
diff --git a/packages/contract-cli/package.json b/packages/contract-cli/package.json
index 1f6a12caf012223f6d8914ab452e387128794fda..fa6bc196ec82ba74b19651e68e72747508999946 100755
--- a/packages/contract-cli/package.json
+++ b/packages/contract-cli/package.json
@@ -1,6 +1,6 @@
{
"name": "jsonql-contract",
- "version": "1.7.6",
+ "version": "1.7.7",
"description": "An command line tool to generate the contract.json for jsonql",
"main": "index.js",
"files": [
@@ -38,25 +38,20 @@
"jsonql-contract": "./cli.js"
},
"dependencies": {
- "acorn": "^6.2.1",
+ "acorn": "^7.0.0",
"chokidar": "^3.0.2",
"colors": "^1.3.3",
"fs-extra": "^8.1.0",
"glob": "^7.1.4",
"jsdoc-api": "^5.0.2",
- "jsonql-constants": "^1.7.8",
+ "jsonql-constants": "^1.7.9",
"jsonql-errors": "^1.0.9",
"jsonql-params-validator": "^1.4.3",
"kefir": "^3.8.6",
"lodash": "^4.17.15",
- "socket.io-client": "^2.2.0",
- "ts-node": "^8.3.0",
- "typescript": "^3.5.3",
- "ws": "^7.1.1",
"yargs": "^13.3.0"
},
"devDependencies": {
- "@types/node": "^12.6.8",
"ava": "^2.2.0",
"debug": "^4.1.1",
"nyc": "^14.1.1",
@@ -77,13 +72,7 @@
"failWithoutAssertions": false,
"tap": false,
"verbose": true,
- "compileEnhancements": false,
- "extensions": [
- "ts"
- ],
- "require": [
- "ts-node/register"
- ]
+ "compileEnhancements": false
},
"engine": {
"node": ">=8"
diff --git a/packages/contract-cli/tests/generator.test.js b/packages/contract-cli/tests/generator.test.js
index d3b73926446ab88b8cb72b39eb3f4eb3cf8b7401..7e68a788320848e1f3bb0a8b928a50411309dc26 100755
--- a/packages/contract-cli/tests/generator.test.js
+++ b/packages/contract-cli/tests/generator.test.js
@@ -23,7 +23,7 @@ const esContractDir = join(__dirname, 'fixtures', 'tmp', 'es')
const esResolverDir = join(__dirname, 'fixtures', 'es')
-const expired = Math.round(Date.now()/1000) + 60*365;
+const expired = Date.now() + 60*365*1000;
test.after(async t => {
fsx.removeSync(contractDir)
diff --git a/packages/koa/package.json b/packages/koa/package.json
index 89a76124e64b94141e08a425497798da15595a73..47283e27b87bd45049f06a30c487fa7b88d0a7ea 100755
--- a/packages/koa/package.json
+++ b/packages/koa/package.json
@@ -1,6 +1,6 @@
{
"name": "jsonql-koa",
- "version": "1.3.5",
+ "version": "1.3.6",
"description": "jsonql Koa middleware",
"main": "index.js",
"files": [
@@ -25,7 +25,7 @@
"test:throw": "DEBUG=jsonql-* ava ./tests/throw.test.js",
"test:gen": "DEBUG=jsonql* ava ./tests/contract.test.js",
"test:jsonp": "DEBUG=jsonql* ava --verbose ./tests/jsonp.test.js",
- "web-console": "DEBUG=jsonql-koa* node ./tests/helpers/browser.js",
+ "web-console": "DEBUG=jsonql-koa*,jsonql-web-console* node ./tests/helpers/browser.js",
"contract": "node ./node_modules/jsonql-contract/cmd.js ./tests/fixtures/resolvers ./tests/fixtures/contracts"
},
"keywords": [
@@ -46,7 +46,7 @@
"jsonql-jwt": "^1.2.1",
"jsonql-node-client": "^1.1.3",
"jsonql-params-validator": "^1.4.3",
- "jsonql-web-console": "^0.2.0",
+ "jsonql-web-console": "^0.4.2",
"koa": "^2.7.0",
"koa-compose": "^4.1.0",
"lodash": "^4.17.15",
diff --git a/packages/web-console/koa/dev.js b/packages/web-console/koa/dev.js
deleted file mode 100644
index 1a8e8db33f87db853779532bbf781620c70627b6..0000000000000000000000000000000000000000
--- a/packages/web-console/koa/dev.js
+++ /dev/null
@@ -1,9 +0,0 @@
-const serverIoCore = require('server-io-core')
-const { join } = require('path')
-
-serverIoCore({
- webroot: [
- join(__dirname, 'public'),
- join(__dirname, '..', 'node_modules')
- ]
-})
diff --git a/packages/web-console/koa/index.js b/packages/web-console/koa/index.js
index 3ee2e3720a00bee5ba39a42ae24bdd5d370b72c4..3eda21fe7d3e847a7feec36e382b332e21ff86c5 100644
--- a/packages/web-console/koa/index.js
+++ b/packages/web-console/koa/index.js
@@ -1,125 +1,44 @@
// The main interface to the middleware
-const { join, extname } = require('path')
-const fsx = require('fs-extra')
-const _ = require('lodash')
const {
- DEFAULT_CONTRACT_FILE_NAME,
- PUBLIC_CONTRACT_FILE_NAME
-} = require('jsonql-constants')
-const publicDir = join(__dirname, 'public')
-const assets = [
- '/jsonql-web-console.css'
-]
-// const debug = require('debug')('jsonql-web-console:koa')
+ getContractJson,
+ isWebConsoleAssets,
+ getDocLen,
+ render
+} = require('./utils')
+const debug = require('debug')('jsonql-web-console:middleware')
let contractJson = null;
-/**
- * Just to read the contract json
- * search for the contract file
- * should we check if there is a public-contract first
- * if it's not found then we check if there is a contract.json?
- * also in the case of just start up, it might happen a fight situation
- * @param {object} config configuration
- */
-const getContractJson = config => {
- const searchDir = [
- join(config.contractDir, PUBLIC_CONTRACT_FILE_NAME),
- join(config.contractDir, DEFAULT_CONTRACT_FILE_NAME)
- ]
- return _(searchDir)
- .chain()
- .filter(dir => fsx.existsSync(dir))
- .thru(dirs => {
- if (dirs.length) {
- return fsx.readJsonSync(dirs[0])
- }
- return false;
- })
- .value()
-}
-
-/**
- * check if the path matches our assets
- * @param {object} ctx koa context
- * @return {boolean|string} false when its not
- */
-const isWebConsoleAssets = ctx => {
- return _(assets)
- .chain()
- .filter(path => ctx.path === path)
- .thru(path => {
- if (path.length) {
- return path[0]
- }
- return false;
- })
- .value()
-}
-
-/**
- * Get document (string) byte length for use in header
- * @param {string} doc to calculate
- * @return {number} length
- */
-const getDocLen = doc => Buffer.byteLength(doc, 'utf8')
-
-// dirty hack
-const fix = json => {
- if (!json.socket) {
- json.socket = false;
- }
- if (_.isEqual(json.auth, {})) {
- json.auth = false;
- }
- return json;
-}
-
-/**
- * @param {string} content output string from file
- * @param {object} json contract
- */
-const renderTpl = (content, json) => {
- return _.template(content)(fix(json))
-}
-
-/**
- * Render out the content
- * @param {object} ctx koa context
- * @param {string} file to serve up
- * @return {void} nothing should stop here
- */
-const render = (ctx, file, json = false) => {
- let content = fsx.readFileSync(join(publicDir, file))
- if (json !== false) {
- content = renderTpl(content, json)
- }
- let ext = extname(file)
- if (ext === '.css') {
- ctx.type = 'text/css';
- } else {
- ctx.type = 'text/html';
- }
-
- ctx.size = getDocLen(content)
- // should we try to get the contentType?
- // ctx.type = opts.contentType;
- ctx.status = 200;
- ctx.body = content;
-}
-
/**
* @param {object} config configuration
* @param {function} isJsonqlConsoleUrl to check if it's the top calling methods
*/
module.exports = function jsonqlWebConsole(config, isJsonqlConsoleUrl) {
- if (contractJson === null) {
- contractJson = getContractJson(config)
- }
return async function(ctx, next) {
if (isJsonqlConsoleUrl(ctx, config)) {
- if (contractJson === false) {
+ let error = false;
+ if (!contractJson) {
+ // we should try to check if ctx contain the contract at this point as well
+ if (ctx.state && ctx.state.jsonql) {
+ const { contract } = ctx.state.jsonql;
+ debug('ctx.state.jsonql.contract', contract)
+ if (contract) {
+ contractJson = contract;
+ } else {
+ error = true; // just throw it
+ }
+ }
+
+ if (!contractJson) {
+ contractJson = getContractJson(config)
+ debug('contractJson', contractJson)
+ error = !!contractJson
+ }
+ }
+
+ if (error !== false) {
// render the error page
return render(ctx, 'error.html')
}
+
return render(ctx, 'index.html', contractJson)
} else {
const asset = isWebConsoleAssets(ctx, config)
diff --git a/packages/web-console/koa/public/index.html b/packages/web-console/koa/public/index.html
index 16c410928398a5b36894638d726853237e7a1f47..96df62fbde2e5f2751b71cbe8466789499b3fce2 100644
--- a/packages/web-console/koa/public/index.html
+++ b/packages/web-console/koa/public/index.html
@@ -6,6 +6,11 @@
jsonql web-console
+
-
-
+
- <% if (query) { %>
-
Query
- <% _.forEach(query, function(value, name) { %>
-
-
-
-
- <% if (value.description) { %>
-
<%- value.description %>
- <% } else { %>
-
No description
- <% } %>
-
-
parameters
-
-
- | Name |
- Type |
- Description |
-
-
- <% if (value.params.length) { %>
- <% _.forEach(value.params, function(param) { %>
-
- | <%- param.name %> |
- <% if (param.variable) { %>...<% } %><%- param.type %> |
- <%- param.description %> |
-
- <% }) %>
- <% } else { %>
-
- |
- No parameter required
- |
-
- <% } %>
-
-
-
returns
-
-
- | Type |
- Description |
-
-
- <% _.forEach(value.returns, function(returns) { %>
-
- | <%- returns.type %> |
- <%- returns.description %> |
-
- <% }) %>
-
-
-
-
-
-
- <% }) %>
+ <% if (queryBlock !== false) { %>
+ <%= queryBlock %>
<% } %>
-
- <% if (mutation) { %>
-
Mutation
- <% _.forEach(mutation, function(value, name) { %>
-
-
-
-
- <% if (value.description) { %>
-
<%- value.description %>
- <% } else { %>
-
No description
- <% } %>
-
-
parameters
-
-
- | Name |
- Type |
- Description |
-
-
- <% if (value.params.length) { %>
- <% _.forEach(value.params, function(param) { %>
-
- | <%- param.name %> |
- <% if (param.variable) { %>...<% } %><%- param.type %> |
- <%- param.description %> |
-
- <% }) %>
- <% } else { %>
-
- |
- No parameter required
- |
-
- <% } %>
-
-
-
returns
-
-
- | Type |
- Description |
-
-
- <% _.forEach(value.returns, function(returns) { %>
-
- | <%- returns.type %> |
- <%- returns.description %> |
-
- <% }) %>
-
-
-
-
-
-
- <% }) %>
+ <% if (mutationBlock !== false) { %>
+ <%= mutationBlock %>
<% } %>
-
- <% if (auth) { %>
-
auth
- <% _.forEach(auth, function(value, name) { %>
-
-
-
-
- <% if (value.description) { %>
-
<%- value.description %>
- <% } else { %>
-
No description
- <% } %>
-
-
parameters
-
-
- | Name |
- Type |
- Description |
-
-
- <% if (value.params.length) { %>
- <% _.forEach(value.params, function(param) { %>
-
- | <%- param.name %> |
- <% if (param.variable) { %>...<% } %><%- param.type %> |
- <%- param.description %> |
-
- <% }) %>
- <% } else { %>
-
- |
- No parameter required
- |
-
- <% } %>
-
-
-
returns
-
-
- | Type |
- Description |
-
-
- <% _.forEach(value.returns, function(returns) { %>
-
- | <%- returns.type %> |
- <%- returns.description %> |
-
- <% }) %>
-
-
-
-
-
-
- <% }) %>
+ <% if (socketBlock !== false) { %>
+ <%= socketBlock %>
<% } %>
-
- <% if (socket) { %>
-
socket
- <% _.forEach(socket, function(value, name) { %>
-
-
-
-
- <% if (value.description) { %>
-
<%- value.description %>
- <% } else { %>
-
No description
- <% } %>
-
-
parameters
-
-
- | Name |
- Type |
- Description |
-
-
- <% if (value.params.length) { %>
- <% _.forEach(value.params, function(param) { %>
-
- | <%- param.name %> |
- <% if (param.variable) { %>...<% } %><%- param.type %> |
- <%- param.description %> |
-
- <% }) %>
- <% } else { %>
-
- |
- No parameter required
- |
-
- <% } %>
-
-
-
returns
-
-
- | Type |
- Description |
-
-
- <% _.forEach(value.returns, function(returns) { %>
-
- | <%- returns.type %> |
- <%- returns.description %> |
-
- <% }) %>
-
-
-
-
-
-
- <% }) %>
+ <% if (authBlock) { %>
+ <%= authBlock %>
<% } %>
-
-