diff --git a/packages/api/src/routes/request/callRequestResolver.js b/packages/api/src/routes/request/callRequestResolver.js new file mode 100644 index 0000000000..3bb9509048 --- /dev/null +++ b/packages/api/src/routes/request/callRequestResolver.js @@ -0,0 +1,39 @@ +/* + Copyright 2020-2021 Lowdefy, Inc + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +import { RequestError } from '../../context/errors'; + +async function callRequestResolver( + { logger }, + { connectionProperties, requestConfig, requestProperties, requestHandler } +) { + try { + const response = await requestHandler.resolver({ + request: requestProperties, + connection: connectionProperties, + }); + return response; + } catch (error) { + const err = new RequestError(error.message); + logger.debug( + { params: { id: requestConfig.requestId, type: requestConfig.type }, err }, + err.message + ); + throw err; + } +} + +export default callRequestResolver; diff --git a/packages/api/src/routes/request/checkConnectionWrite.js b/packages/api/src/routes/request/checkConnectionWrite.js new file mode 100644 index 0000000000..3205b8ce26 --- /dev/null +++ b/packages/api/src/routes/request/checkConnectionWrite.js @@ -0,0 +1,37 @@ +/* + Copyright 2020-2021 Lowdefy, Inc + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +import { ConfigurationError } from '../../context/errors'; + +function checkConnectionWrite( + { logger }, + { connectionConfig, connectionProperties, requestConfig, requestHandler } +) { + if (requestHandler.checkWrite && connectionProperties.write !== true) { + const err = new ConfigurationError( + `Connection "${connectionConfig.connectionId}" does not allow writes.` + ); + logger.debug( + { + params: { connectionId: connectionConfig.connectionId, requestId: requestConfig.requestId }, + err, + }, + err.message + ); + throw err; + } +} + +export default checkConnectionWrite; diff --git a/packages/api/src/routes/request/getConnectionHandler.js b/packages/api/src/routes/request/getConnectionHandler.js index c86100cd33..940ca7222d 100644 --- a/packages/api/src/routes/request/getConnectionHandler.js +++ b/packages/api/src/routes/request/getConnectionHandler.js @@ -17,7 +17,6 @@ import { ConfigurationError } from '../../context/errors'; function getConnectionHandler({ connections, logger }, { connectionConfig }) { const connectionHandler = connections[connectionConfig.type]; - if (!connectionHandler) { const err = new ConfigurationError( `Connection type "${connectionConfig.type}" can not be found.` @@ -29,7 +28,7 @@ function getConnectionHandler({ connections, logger }, { connectionConfig }) { throw err; } - return null; + return connectionHandler; } export default getConnectionHandler; diff --git a/packages/api/src/routes/request/getRequestHandler.js b/packages/api/src/routes/request/getRequestHandler.js index f48fabde32..f01a98239b 100644 --- a/packages/api/src/routes/request/getRequestHandler.js +++ b/packages/api/src/routes/request/getRequestHandler.js @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ + import { ConfigurationError } from '../../context/errors'; function getRequestHandler({ logger }, { connectionHandler, requestConfig }) { @@ -27,7 +28,7 @@ function getRequestHandler({ logger }, { connectionHandler, requestConfig }) { throw err; } - return null; + return requestHandler; } export default getRequestHandler; diff --git a/packages/api/src/routes/request/request.js b/packages/api/src/routes/request/request.js index 96e94c59ef..849e2d3802 100644 --- a/packages/api/src/routes/request/request.js +++ b/packages/api/src/routes/request/request.js @@ -17,6 +17,7 @@ import { serializer } from '@lowdefy/helpers'; import authorizeRequest from './authorizeRequest'; +import callRequestResolver from './callRequestResolver'; import checkConnectionRead from './checkConnectionRead'; import checkConnectionWrite from './checkConnectionWrite'; import evaluateOperators from './evaluateOperators'; @@ -24,12 +25,13 @@ import getConnectionConfig from './getConnectionConfig'; import getConnectionHandler from './getConnectionHandler'; import getRequestConfig from './getRequestConfig'; import getRequestHandler from './getRequestHandler'; +import validateSchemas from './validateSchemas'; async function request(context, { pageId, payload, requestId }) { const { logger } = context; logger.debug({ route: 'request', params: { pageId, payload, requestId } }, 'Started request'); const requestConfig = await getRequestConfig(context, { pageId, requestId }); - const connectionConfig = await getConnectionConfig(context, { pageId, requestId }); + const connectionConfig = await getConnectionConfig(context, { requestConfig }); authorizeRequest(context, { requestConfig }); const connectionHandler = getConnectionHandler(context, { connectionConfig }); @@ -41,31 +43,31 @@ async function request(context, { pageId, payload, requestId }) { requestConfig, }); - checkConnectionRead({ - connectionId: request.connectionId, + checkConnectionRead(context, { + connectionConfig, connectionProperties, - checkRead: requestHandler.checkRead, + requestConfig, + requestHandler, }); - checkConnectionWrite({ - connectionId: request.connectionId, + checkConnectionWrite(context, { + connectionConfig, connectionProperties, - checkWrite: requestHandler.checkWrite, + requestConfig, + requestHandler, + }); + validateSchemas(context, { + connectionHandler, + connectionProperties, + requestConfig, + requestHandler, + requestProperties, }); - // try { - // validate({ schema: connectionDefinition.schema, data: connectionProperties }); - // validate({ schema: requestDefinition.schema, data: requestProperties }); - // } catch (error) { - // throw new ConfigurationError(error.message); - // } - - // const response = await this.callResolver({ - // connectionProperties, - // requestProperties, - // resolver: requestDefinition.resolver, - // }); - - const response = null; + const response = await callRequestResolver(context, { + connectionProperties, + requestProperties, + requestHandler, + }); return { id: request.id, diff --git a/packages/api/src/routes/request/validateSchemas.js b/packages/api/src/routes/request/validateSchemas.js new file mode 100644 index 0000000000..d4f31b0fef --- /dev/null +++ b/packages/api/src/routes/request/validateSchemas.js @@ -0,0 +1,38 @@ +/* + Copyright 2020-2021 Lowdefy, Inc + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +import { validate } from '@lowdefy/ajv'; + +import { ConfigurationError } from '../../context/errors'; + +function validateSchemas( + { logger }, + { connectionHandler, connectionProperties, requestConfig, requestHandler, requestProperties } +) { + try { + validate({ schema: connectionHandler.schema, data: connectionProperties }); + validate({ schema: requestHandler.schema, data: requestProperties }); + } catch (error) { + const err = new ConfigurationError(error.message); + logger.debug( + { params: { id: requestConfig.requestId, type: requestConfig.type }, err }, + err.message + ); + throw err; + } +} + +export default validateSchemas; diff --git a/packages/servers/serverDev/src/config.js b/packages/servers/serverDev/src/config.js index babce23c8a..c4b2ba5e0f 100644 --- a/packages/servers/serverDev/src/config.js +++ b/packages/servers/serverDev/src/config.js @@ -20,7 +20,7 @@ import pino from 'pino'; import { clientDirectory, publicDirectory as defaultPublicDirectory } from '@lowdefy/client'; import { getConfigFromEnv, getSecretsFromEnv } from '@lowdefy/node-utils'; -import AxiosHttp from '@lowdefy/connection-axios-http'; +import ConnectionAxiosHttp from '@lowdefy/connection-axios-http'; function config() { dotenv.config({ silent: true }); @@ -35,7 +35,7 @@ function config() { // TODO: dynamic connections const connections = { - AxiosHttp, + ...ConnectionAxiosHttp, }; return {