feat: Requests working on next server

This commit is contained in:
Sam Tolmay 2021-11-17 16:06:35 +02:00
parent 7d02099778
commit 8d6abe27f9
No known key found for this signature in database
GPG Key ID: D004126FCD1A6DF0
31 changed files with 180 additions and 114 deletions

1
.pnp.cjs generated
View File

@ -5736,6 +5736,7 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) {
["@lowdefy/block-utils", "workspace:packages/block-utils"],
["@lowdefy/blocks-antd", "workspace:packages/blocks/blocks-antd"],
["@lowdefy/blocks-basic", "workspace:packages/blocks/blocks-basic"],
["@lowdefy/connection-axios-http", "workspace:packages/plugins/connections/connection-axios-http"],
["@lowdefy/engine", "workspace:packages/engine"],
["@lowdefy/helpers", "workspace:packages/helpers"],
["@lowdefy/layout", "workspace:packages/layout"],

View File

@ -16,14 +16,17 @@
import createReadConfigFile from './readConfigFile.js';
async function createApiContext({ buildDirectory }) {
async function createApiContext({ buildDirectory, connections, logger, secrets }) {
const readConfigFile = createReadConfigFile({ buildDirectory });
const config = await readConfigFile('config.json');
return {
authenticated: false,
authorize: () => true,
config,
connections,
logger,
readConfigFile,
secrets,
};
}

View File

@ -16,7 +16,7 @@
import callRequest from './routes/request/callRequest.js';
import createApiContext from './context/createApiContext.js';
import getHomePageId from './routes/rootConfig/getHomePageId.js';
import getHome from './routes/rootConfig/getHome.js';
import getPageConfig from './routes/page/getPageConfig.js';
import getRootConfig from './routes/rootConfig/getRootConfig.js';
@ -31,7 +31,7 @@ import {
export {
callRequest,
createApiContext,
getHomePageId,
getHome,
getPageConfig,
getRootConfig,
AuthenticationError,

View File

@ -17,7 +17,7 @@
import { serializer } from '@lowdefy/helpers';
import authorizeRequest from './authorizeRequest.js';
import callRequestHandler from './callRequestHandler.js';
import callRequestResolver from './callRequestResolver.js';
import checkConnectionRead from './checkConnectionRead.js';
import checkConnectionWrite from './checkConnectionWrite.js';
import evaluateOperators from './evaluateOperators.js';
@ -53,14 +53,14 @@ async function callRequest(context, { pageId, payload, requestId }) {
requestConfig,
requestHandler,
});
validateSchemas(context, {
connectionHandler,
connectionProperties,
requestConfig,
requestHandler,
requestProperties,
});
const response = await callRequestHandler(context, {
// validateSchemas(context, {
// connectionHandler,
// connectionProperties,
// requestConfig,
// requestHandler,
// requestProperties,
// });
const response = await callRequestResolver(context, {
connectionProperties,
requestConfig,
requestHandler,

View File

@ -19,7 +19,7 @@ function checkConnectionRead(
{ logger },
{ connectionConfig, connectionProperties, requestConfig, requestHandler }
) {
if (requestHandler.checkRead && connectionProperties.read === false) {
if (requestHandler.meta.checkRead && connectionProperties.read === false) {
const err = new ConfigurationError(
`Connection "${connectionConfig.connectionId}" does not allow reads.`
);

View File

@ -19,7 +19,7 @@ function checkConnectionWrite(
{ logger },
{ connectionConfig, connectionProperties, requestConfig, requestHandler }
) {
if (requestHandler.checkWrite && connectionProperties.write !== true) {
if (requestHandler.meta.checkWrite && connectionProperties.write !== true) {
const err = new ConfigurationError(
`Connection "${connectionConfig.connectionId}" does not allow writes.`
);

View File

@ -14,36 +14,43 @@
limitations under the License.
*/
import { NodeParser } from '@lowdefy/operators';
// import { NodeParser } from '@lowdefy/operators';
import { RequestError } from '../../context/errors.js';
// import { RequestError } from '../../context/errors.js';
// async function evaluateOperators({ secrets, user }, { connectionConfig, payload, requestConfig }) {
// const operatorsParser = new NodeParser({
// payload,
// secrets,
// user,
// });
// await operatorsParser.init();
// const { output: connectionProperties, errors: connectionErrors } = operatorsParser.parse({
// input: connectionConfig.properties || {},
// location: connectionConfig.connectionId,
// });
// if (connectionErrors.length > 0) {
// throw new RequestError(connectionErrors[0]);
// }
// const { output: requestProperties, errors: requestErrors } = operatorsParser.parse({
// input: requestConfig.properties || {},
// location: requestConfig.requestId,
// });
// if (requestErrors.length > 0) {
// throw new RequestError(requestErrors[0]);
// }
// return {
// connectionProperties,
// requestProperties,
// };
// }
async function evaluateOperators({ secrets, user }, { connectionConfig, payload, requestConfig }) {
const operatorsParser = new NodeParser({
payload,
secrets,
user,
});
await operatorsParser.init();
const { output: connectionProperties, errors: connectionErrors } = operatorsParser.parse({
input: connectionConfig.properties || {},
location: connectionConfig.connectionId,
});
if (connectionErrors.length > 0) {
throw new RequestError(connectionErrors[0]);
}
const { output: requestProperties, errors: requestErrors } = operatorsParser.parse({
input: requestConfig.properties || {},
location: requestConfig.requestId,
});
if (requestErrors.length > 0) {
throw new RequestError(requestErrors[0]);
}
return {
connectionProperties,
requestProperties,
connectionProperties: connectionConfig.properties || {},
requestProperties: requestConfig.properties || {},
};
}

View File

@ -16,10 +16,10 @@
import { get } from '@lowdefy/helpers';
function findHomePageId({ config }, { menus }) {
function findHome({ config }, { menus }) {
if (get(config, 'homePageId')) {
return {
homePageId: config.homePageId,
pageId: config.homePageId,
configured: true,
};
}
@ -28,18 +28,18 @@ function findHomePageId({ config }, { menus }) {
// eslint-disable-next-line prefer-destructuring
defaultMenu = menus[0];
}
let homePageId = null;
homePageId = get(defaultMenu, 'links.0.pageId', { default: null });
if (!homePageId) {
homePageId = get(defaultMenu, 'links.0.links.0.pageId', { default: null });
let pageId = null;
pageId = get(defaultMenu, 'links.0.pageId', { default: null });
if (!pageId) {
pageId = get(defaultMenu, 'links.0.links.0.pageId', { default: null });
}
if (!homePageId) {
homePageId = get(defaultMenu, 'links.0.links.0.links.0.pageId', { default: null });
if (!pageId) {
pageId = get(defaultMenu, 'links.0.links.0.links.0.pageId', { default: null });
}
return {
homePageId: homePageId,
pageId,
configured: false,
};
}
export default findHomePageId;
export default findHome;

View File

@ -14,10 +14,10 @@
limitations under the License.
*/
import findHomePageId from './findHomePageId.js';
import findHome from './findHome.js';
import testContext from '../../test/testContext.js';
test('findHomePageId, menu with configured home page id', () => {
test('findHome, menu with configured home page id', () => {
const context = testContext({ config: { homePageId: 'homePageId' } });
const menus = [
{
@ -41,11 +41,11 @@ test('findHomePageId, menu with configured home page id', () => {
],
},
];
const res = findHomePageId(context, { menus });
expect(res).toEqual('homePageId');
const res = findHome(context, { menus });
expect(res).toEqual({ configured: true, pageId: 'homePageId' });
});
test('findHomePageId, get homePageId at first level', () => {
test('findHome, get homePageId at first level', () => {
const context = testContext();
const menus = [
{
@ -61,11 +61,11 @@ test('findHomePageId, get homePageId at first level', () => {
],
},
];
const res = findHomePageId(context, { menus });
expect(res).toEqual('page');
const res = findHome(context, { menus });
expect(res).toEqual({ configured: false, pageId: 'page' });
});
test('findHomePageId, get homePageId at second level', () => {
test('findHome, get homePageId at second level', () => {
const context = testContext();
const menus = [
{
@ -89,11 +89,11 @@ test('findHomePageId, get homePageId at second level', () => {
],
},
];
const res = findHomePageId(context, { menus });
expect(res).toEqual('page');
const res = findHome(context, { menus });
expect(res).toEqual({ configured: false, pageId: 'page' });
});
test('findHomePageId, get homePageId at third level', () => {
test('findHome, get homePageId at third level', () => {
const context = testContext();
const menus = [
{
@ -125,11 +125,11 @@ test('findHomePageId, get homePageId at third level', () => {
],
},
];
const res = findHomePageId(context, { menus });
expect(res).toEqual('page');
const res = findHome(context, { menus });
expect(res).toEqual({ configured: false, pageId: 'page' });
});
test('findHomePageId, no default menu, no configured homepage', () => {
test('findHome, no default menu, no configured homepage', () => {
const context = testContext();
const menus = [
{
@ -145,11 +145,11 @@ test('findHomePageId, no default menu, no configured homepage', () => {
],
},
];
const res = findHomePageId(context, { menus });
expect(res).toEqual('page');
const res = findHome(context, { menus });
expect(res).toEqual({ configured: false, pageId: 'page' });
});
test('findHomePageId, more than 1 menu, no configured homepage', () => {
test('findHome, more than 1 menu, no configured homepage', () => {
const context = testContext();
const menus = [
{
@ -177,11 +177,11 @@ test('findHomePageId, more than 1 menu, no configured homepage', () => {
],
},
];
const res = findHomePageId(context, { menus });
expect(res).toEqual('default-page');
const res = findHome(context, { menus });
expect(res).toEqual({ configured: false, pageId: 'default-page' });
});
test('findHomePageId, default menu has no links', () => {
test('findHome, default menu has no links', () => {
const context = testContext();
const menus = [
{
@ -189,6 +189,6 @@ test('findHomePageId, default menu has no links', () => {
links: [],
},
];
const res = findHomePageId(context, { menus });
expect(res).toEqual(null);
const res = findHome(context, { menus });
expect(res).toEqual({ configured: false, pageId: null });
});

View File

@ -14,14 +14,14 @@
limitations under the License.
*/
import findHomePageId from '../rootConfig/findHomePageId.js';
import getMenus from '../rootConfig/menus/getMenus.js';
import findHome from './findHome.js';
import getMenus from './menus/getMenus.js';
async function getHomePageId(context) {
async function getHome(context) {
// TODO: We can optimise here as we don't need to read menus if homepageId is configured
// but not sure if it is worth the added complexity
const menus = await getMenus(context);
return findHomePageId(context, { menus });
return findHome(context, { menus });
}
export default getHomePageId;
export default getHome;

View File

@ -14,8 +14,8 @@
limitations under the License.
*/
import getLowdefyGlobal from './getLowdefyGlobal';
import testContext from '../../test/testContext';
import getLowdefyGlobal from './getLowdefyGlobal.js';
import testContext from '../../test/testContext.js';
const mockReadConfigFile = jest.fn();

View File

@ -14,7 +14,7 @@
limitations under the License.
*/
import findHomePageId from './findHomePageId.js';
import findHome from './findHome.js';
import getLowdefyGlobal from './getLowdefyGlobal.js';
import getMenus from './menus/getMenus.js';
@ -22,7 +22,7 @@ async function getRootConfig(context) {
const [lowdefyGlobal, menus] = await Promise.all([getLowdefyGlobal(context), getMenus(context)]);
return {
authenticated: context.authenticated,
homePageId: findHomePageId(context, { menus }),
home: findHome(context, { menus }),
lowdefyGlobal,
menus,
};

View File

@ -15,7 +15,7 @@
*/
async function Login({ context, params }) {
return context.lowdefy.auth.login(params);
return context._internal.lowdefy._internal.auth.login(params);
}
export default Login;

View File

@ -15,7 +15,7 @@
*/
async function Logout({ context }) {
return context.lowdefy.auth.logout();
return context._internal.lowdefy._internal.auth.logout();
}
export default Logout;

View File

@ -15,7 +15,7 @@
*/
function Message({ context, params = {} }) {
context.lowdefy._internal.displayMessage({
context._internal.lowdefy._internal.displayMessage({
content: params.content || 'Success',
duration: params.duration,
icon: params.icon,

View File

@ -16,12 +16,12 @@
async function ScrollTo({ context, params = {} }) {
if (params.blockId) {
const element = context.lowdefy._internal.document.getElementById(params.blockId);
const element = context._internal.lowdefy._internal.document.getElementById(params.blockId);
if (element) {
element.scrollIntoView(params.options);
}
} else {
context.lowdefy.window.scrollTo(params);
context._internal.lowdefy._internal.window.scrollTo(params);
}
}

View File

@ -24,12 +24,12 @@ function createLink({ backLink, lowdefy, newOriginLink, sameOriginLink }) {
}
const lowdefyUrlQuery = type.isNone(urlQuery) ? '' : `?${urlQueryFn.stringify(urlQuery)}`;
if (home) {
if (lowdefy.homePageId.configured) {
if (lowdefy.home.configured) {
pathname = '';
pageId = lowdefy.homePageId.homePageId;
pageId = lowdefy.home.pageId;
} else {
pathname = lowdefy.homePageId.homePageId;
pageId = lowdefy.homePageId.homePageId;
pathname = lowdefy.home.pageId;
pageId = lowdefy.home.pageId;
}
}
if (!type.isNone(pathname)) {

View File

@ -14,10 +14,11 @@
limitations under the License.
*/
import AxiosHttp from './AxiosHttpRequest/AxiosHttpRequest.js';
import AxiosHttp from './AxiosHttpRequest/index.js';
export default {
import: {
path: 'connections/AxiosHttp/AxiosHttpConnection.js',
schema: 'connections/AxiosHttp/AxiosHttpConnectionSchema.json',
},
requests: {

View File

@ -14,11 +14,13 @@
limitations under the License.
*/
import AxiosHttpRequest from './AxiosHttpRequest.js';
export default {
import: {
path: 'connections/AxiosHttp/AxiosHttpRequest/AxiosHttpRequest.js',
schema: 'connections/AxiosHttp/AxiosHttpConnectionSchema.json',
schema: 'connections/AxiosHttp/AxiosHttpConnection.json',
},
resolver: AxiosHttpRequest,
meta: {
checkRead: false,
checkWrite: false,

View File

@ -12,6 +12,7 @@ module.exports = withLess({
webpack: (config, { isServer }) => {
if (!isServer) {
config.resolve.fallback = {
assert: false,
buffer: false,
crypto: false,
events: false,

View File

@ -40,6 +40,7 @@
"@lowdefy/block-utils": "3.22.0",
"@lowdefy/blocks-antd": "3.22.0",
"@lowdefy/blocks-basic": "3.22.0",
"@lowdefy/connection-axios-http": "3.22.0",
"@lowdefy/engine": "3.22.0",
"@lowdefy/helpers": "3.22.0",
"@lowdefy/layout": "3.22.0",

View File

@ -24,7 +24,7 @@ const Root = ({ children, lowdefy, rootConfig }) => {
setLoading(false);
}, [lowdefy]);
lowdefy.homePageId = rootConfig.homePageId;
lowdefy.home = rootConfig.home;
lowdefy.lowdefyGlobal = rootConfig.lowdefyGlobal;
lowdefy.menus = rootConfig.menus;

View File

@ -25,8 +25,9 @@ const CategorySwitch = ({ block, Blocks, context, lowdefy }) => {
if (!block.eval) return null; // Renderer updates before eval is executed for the first time on lists. See #520
if (block.eval.visible === false)
return <div id={`vs-${block.blockId}`} style={{ display: 'none' }} />;
const Component = lowdefy._internal.blockComponents[block.type];
switch (block.meta.category) {
const Component = lowdefy._internal.blockComponents[block.type].Component;
const meta = lowdefy._internal.blockComponents[block.type].meta;
switch (meta.category) {
case 'list':
return (
<List

View File

@ -14,9 +14,7 @@
limitations under the License.
*/
import createApiContext from '@lowdefy/api/context/createApiContext';
import getPageConfig from '@lowdefy/api/routes/page/getPageConfig';
import getRootConfig from '@lowdefy/api/routes/rootConfig/getRootConfig';
import { createApiContext, getPageConfig, getRootConfig } from '@lowdefy/api';
import Page from '../components/Page.js';

View File

@ -22,7 +22,14 @@ export default async function handler(req, res) {
if (req.method !== 'POST') {
throw new Error('Only POST requests are supported.');
}
const apiContext = await createApiContext({ buildDirectory: './.lowdefy/build', connections });
const apiContext = await createApiContext({
buildDirectory: './.lowdefy/build',
connections,
// TODO
logger: console,
// TODO
secrets: {},
});
const { pageId, requestId } = req.query;
const { payload } = req.body;

View File

@ -13,20 +13,18 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
import createApiContext from '@lowdefy/api/context/createApiContext';
import getHomePageId from '@lowdefy/api/routes/rootConfig/getHomePageId.js';
import getPageConfig from '@lowdefy/api/routes/page/getPageConfig';
import getRootConfig from '@lowdefy/api/routes/rootConfig/getRootConfig';
import { createApiContext, getHome, getPageConfig, getRootConfig } from '@lowdefy/api';
import Page from '../components/Page.js';
export async function getServerSideProps() {
const apiContext = await createApiContext({ buildDirectory: './.lowdefy/build' });
const homePageIdData = await getHomePageId(apiContext);
if (homePageIdData.configured === false) {
const home = await getHome(apiContext);
if (home.configured === false) {
return {
redirect: {
destination: `/${homePageIdData.homePageId}`,
destination: `/${home.pageId}`,
permanent: false,
},
};
@ -34,7 +32,7 @@ export async function getServerSideProps() {
const [rootConfig, pageConfig] = await Promise.all([
getRootConfig(apiContext),
getPageConfig(apiContext, { pageId: homePageIdData.homePageId }),
getPageConfig(apiContext, { pageId: home.pageId }),
]);
if (!pageConfig) {

View File

@ -20,10 +20,48 @@ import Button from '@lowdefy/blocks-antd/blocks/Button/Button.js';
import Html from '@lowdefy/blocks-basic/blocks/Html/Html.js';
const blocks = {
Anchor,
Box,
Button,
Html,
Anchor: {
Component: Anchor,
meta: {
category: 'display',
loading: {
type: 'SkeletonParagraph',
properties: {
lines: 1,
},
},
},
},
Box: {
Component: Box,
meta: {
category: 'container',
loading: false,
},
},
Button: {
Component: Button,
meta: {
category: 'display',
loading: {
type: 'SkeletonButton',
},
},
},
Html: {
Component: Html,
meta: {
category: 'display',
loading: false,
},
},
};
// const blocks = {
// Anchor,
// Box,
// Button,
// Html,
// };
export default blocks;

View File

@ -14,4 +14,8 @@
limitations under the License.
*/
export default {};
import AxiosHttp from '@lowdefy/connection-axios-http/connections/AxiosHttp/AxiosHttpConnection.js';
export default {
AxiosHttp,
};

View File

@ -23,8 +23,11 @@ async function request({ url, method = 'GET', body }) {
body: JSON.stringify(body),
});
if (!res.ok) {
// TODO: check
const body = await res.json();
console.log(res);
throw new Error('Request Error');
console.log(body);
throw new Error(body.message || 'Request error');
}
return res.json();
}

View File

@ -3947,6 +3947,7 @@ __metadata:
"@lowdefy/block-utils": 3.22.0
"@lowdefy/blocks-antd": 3.22.0
"@lowdefy/blocks-basic": 3.22.0
"@lowdefy/connection-axios-http": 3.22.0
"@lowdefy/engine": 3.22.0
"@lowdefy/helpers": 3.22.0
"@lowdefy/layout": 3.22.0