Skip to content

Commit

Permalink
feat: Add requests support to @lowdefy/api package
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTolmay committed Nov 2, 2021
1 parent 83b885b commit 86533ee
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 26 deletions.
39 changes: 39 additions & 0 deletions 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;
37 changes: 37 additions & 0 deletions 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;
3 changes: 1 addition & 2 deletions packages/api/src/routes/request/getConnectionHandler.js
Expand Up @@ -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.`
Expand All @@ -29,7 +28,7 @@ function getConnectionHandler({ connections, logger }, { connectionConfig }) {
throw err;
}

return null;
return connectionHandler;
}

export default getConnectionHandler;
3 changes: 2 additions & 1 deletion packages/api/src/routes/request/getRequestHandler.js
Expand Up @@ -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 }) {
Expand All @@ -27,7 +28,7 @@ function getRequestHandler({ logger }, { connectionHandler, requestConfig }) {
throw err;
}

return null;
return requestHandler;
}

export default getRequestHandler;
44 changes: 23 additions & 21 deletions packages/api/src/routes/request/request.js
Expand Up @@ -17,19 +17,21 @@
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';
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 });
Expand All @@ -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,
Expand Down
38 changes: 38 additions & 0 deletions 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;
4 changes: 2 additions & 2 deletions packages/servers/serverDev/src/config.js
Expand Up @@ -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 });
Expand All @@ -35,7 +35,7 @@ function config() {

// TODO: dynamic connections
const connections = {
AxiosHttp,
...ConnectionAxiosHttp,
};

return {
Expand Down

0 comments on commit 86533ee

Please sign in to comment.