@lowdefy/engine 4.0.0-alpha.29 → 4.0.0-alpha.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ function createLogout({ context }) {
16
+ return function logout(params) {
17
+ return context._internal.lowdefy._internal.auth.logout(params);
18
+ };
19
+ }
20
+ export default createLogout;
@@ -0,0 +1,26 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ function createRequest({ actions , arrayIndices , blockId , context , event }) {
16
+ return async function request(params) {
17
+ return await context._internal.Requests.callRequests({
18
+ actions,
19
+ arrayIndices,
20
+ blockId,
21
+ event,
22
+ params
23
+ });
24
+ };
25
+ }
26
+ export default createRequest;
@@ -0,0 +1,22 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import { serializer } from '@lowdefy/helpers';
16
+ function createReset({ context }) {
17
+ return function reset() {
18
+ context._internal.State.resetState();
19
+ context._internal.RootBlocks.reset(serializer.deserializeFromString(context._internal.State.frozenState));
20
+ };
21
+ }
22
+ export default createReset;
@@ -0,0 +1,21 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import getBlockMatcher from '../getBlockMatcher.js';
16
+ function createResetValidation({ context }) {
17
+ return function resetValidation(params) {
18
+ context._internal.RootBlocks.resetValidation(getBlockMatcher(params));
19
+ };
20
+ }
21
+ export default createResetValidation;
@@ -0,0 +1,25 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import { applyArrayIndices, set } from '@lowdefy/helpers';
16
+ const createSetGlobal = ({ arrayIndices , context })=>{
17
+ return function setGlobal(params) {
18
+ Object.keys(params).forEach((key)=>{
19
+ set(context._internal.lowdefy.lowdefyGlobal, applyArrayIndices(arrayIndices, key), params[key]);
20
+ });
21
+ context._internal.RootBlocks.reset();
22
+ context._internal.update();
23
+ };
24
+ };
25
+ export default createSetGlobal;
@@ -0,0 +1,25 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import { applyArrayIndices } from '@lowdefy/helpers';
16
+ function createSetState({ arrayIndices , context }) {
17
+ return function setState(params) {
18
+ Object.keys(params).forEach((key)=>{
19
+ context._internal.State.set(applyArrayIndices(arrayIndices, key), params[key]);
20
+ });
21
+ context._internal.RootBlocks.reset();
22
+ context._internal.update();
23
+ };
24
+ }
25
+ export default createSetState;
@@ -0,0 +1,25 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import getBlockMatcher from '../getBlockMatcher.js';
16
+ function createValidate({ context }) {
17
+ return function validate(params) {
18
+ const validationErrors = context._internal.RootBlocks.validate(getBlockMatcher(params));
19
+ if (validationErrors.length > 0) {
20
+ const error = new Error(`Your input has ${validationErrors.length} validation error${validationErrors.length !== 1 ? 's' : ''}.`);
21
+ throw error;
22
+ }
23
+ };
24
+ }
25
+ export default createValidate;
@@ -0,0 +1,61 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import createCallMethod from './createCallMethod.js';
16
+ import createGetActions from './createGetActions.js';
17
+ import createGetBlockId from './createGetBlockId.js';
18
+ import createGetEvent from './createGetEvent.js';
19
+ import createGetGlobal from './createGetGlobal.js';
20
+ import createGetInput from './createGetInput.js';
21
+ import createGetPageId from './createGetPageId.js';
22
+ import createGetRequestDetails from './createGetRequestDetails.js';
23
+ import createGetState from './createGetState.js';
24
+ import createGetUrlQuery from './createGetUrlQuery.js';
25
+ import createGetUser from './createGetUser.js';
26
+ import createLink from './createLink.js';
27
+ import createLogin from './createLogin.js';
28
+ import createLogout from './createLogout.js';
29
+ import createDisplayMessage from './createDisplayMessage.js';
30
+ import createRequest from './createRequest.js';
31
+ import createReset from './createReset.js';
32
+ import createResetValidation from './createResetValidation.js';
33
+ import createSetGlobal from './createSetGlobal.js';
34
+ import createSetState from './createSetState.js';
35
+ import createValidate from './createValidate.js';
36
+ function getActionMethods(props) {
37
+ return {
38
+ callMethod: createCallMethod(props),
39
+ displayMessage: createDisplayMessage(props),
40
+ getActions: createGetActions(props),
41
+ getBlockId: createGetBlockId(props),
42
+ getEvent: createGetEvent(props),
43
+ getGlobal: createGetGlobal(props),
44
+ getInput: createGetInput(props),
45
+ getPageId: createGetPageId(props),
46
+ getRequestDetails: createGetRequestDetails(props),
47
+ getState: createGetState(props),
48
+ getUrlQuery: createGetUrlQuery(props),
49
+ getUser: createGetUser(props),
50
+ link: createLink(props),
51
+ login: createLogin(props),
52
+ logout: createLogout(props),
53
+ request: createRequest(props),
54
+ reset: createReset(props),
55
+ resetValidation: createResetValidation(props),
56
+ setGlobal: createSetGlobal(props),
57
+ setState: createSetState(props),
58
+ validate: createValidate(props)
59
+ };
60
+ }
61
+ export default getActionMethods;
@@ -0,0 +1,42 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import { applyArrayIndices, get, serializer, type } from '@lowdefy/helpers';
16
+ const getFromObject = ({ arrayIndices , location , method , object , params })=>{
17
+ if (params === true) params = {
18
+ all: true
19
+ };
20
+ if (type.isString(params) || type.isInt(params)) params = {
21
+ key: params
22
+ };
23
+ if (!type.isObject(params)) {
24
+ throw new Error(`Method Error: ${method} params must be of type string, integer, boolean or object. Received: ${JSON.stringify(params)} at ${location}.`);
25
+ }
26
+ if (params.key === null) return get(params, 'default', {
27
+ default: null,
28
+ copy: true
29
+ });
30
+ if (params.all === true) return serializer.copy(object);
31
+ if (!type.isString(params.key) && !type.isInt(params.key)) {
32
+ throw new Error(`Method Error: ${method} params.key must be of type string or integer. Received: ${JSON.stringify(params)} at ${location}.`);
33
+ }
34
+ return get(object, applyArrayIndices(arrayIndices, params.key), {
35
+ default: get(params, 'default', {
36
+ default: null,
37
+ copy: true
38
+ }),
39
+ copy: true
40
+ });
41
+ };
42
+ export default getFromObject;
@@ -0,0 +1,67 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import { type, urlQuery as urlQueryFn } from '@lowdefy/helpers';
16
+ function createLink({ backLink , disabledLink , lowdefy , newOriginLink , noLink , sameOriginLink }) {
17
+ function link(props) {
18
+ if (props.disabled === true) {
19
+ return disabledLink(props);
20
+ }
21
+ if ([
22
+ !props.pageId,
23
+ !props.back,
24
+ !props.home,
25
+ !props.url
26
+ ].filter((v)=>!v).length > 1) {
27
+ throw Error(`Invalid Link: To avoid ambiguity, only one of 'back', 'home', 'pageId' or 'url' can be defined.`);
28
+ }
29
+ if (props.back === true) {
30
+ // Cannot set input or urlQuery on back
31
+ return backLink(props);
32
+ }
33
+ const query = type.isNone(props.urlQuery) ? '' : `${urlQueryFn.stringify(props.urlQuery)}`;
34
+ if (props.home === true) {
35
+ const pathname = `/${lowdefy.home.configured ? '' : lowdefy.home.pageId}`;
36
+ return sameOriginLink({
37
+ ...props,
38
+ pathname,
39
+ query,
40
+ setInput: ()=>{
41
+ lowdefy.inputs[`page:${lowdefy.home.pageId}`] = props.input ?? {};
42
+ }
43
+ });
44
+ }
45
+ if (type.isString(props.pageId)) {
46
+ return sameOriginLink({
47
+ ...props,
48
+ pathname: `/${props.pageId}`,
49
+ query,
50
+ setInput: ()=>{
51
+ lowdefy.inputs[`page:${props.pageId}`] = props.input ?? {};
52
+ }
53
+ });
54
+ }
55
+ if (type.isString(props.url)) {
56
+ const protocol = props.url.includes(':') ? '' : 'https://';
57
+ return newOriginLink({
58
+ ...props,
59
+ url: `${protocol}${props.url}`,
60
+ query
61
+ });
62
+ }
63
+ return noLink(props);
64
+ }
65
+ return link;
66
+ }
67
+ export default createLink;
@@ -0,0 +1,61 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import { type } from '@lowdefy/helpers';
16
+ const getBlockMatcher = (params)=>{
17
+ let testParams = params;
18
+ if (type.isNone(testParams)) return ()=>true;
19
+ if (type.isString(testParams)) {
20
+ testParams = {
21
+ blockIds: [
22
+ testParams
23
+ ]
24
+ };
25
+ }
26
+ if (type.isArray(testParams) || type.isBoolean(testParams)) {
27
+ testParams = {
28
+ blockIds: testParams
29
+ };
30
+ }
31
+ if (!type.isObject(testParams)) {
32
+ throw new Error('Invalid validate params.');
33
+ }
34
+ if (type.isString(testParams.blockIds)) {
35
+ testParams.blockIds = [
36
+ testParams.blockIds
37
+ ];
38
+ }
39
+ if (type.isString(testParams.regex)) {
40
+ testParams.regex = [
41
+ testParams.regex
42
+ ];
43
+ }
44
+ if (type.isArray(testParams.regex)) {
45
+ testParams.regex = testParams.regex.map((regex)=>new RegExp(regex));
46
+ }
47
+ return (id)=>{
48
+ if (testParams.blockIds === true || type.isArray(testParams.blockIds) && testParams.blockIds.includes(id)) {
49
+ return true;
50
+ }
51
+ if (type.isArray(testParams.regex)) {
52
+ for (const regex of testParams.regex){
53
+ if (regex.test(id)) {
54
+ return true;
55
+ }
56
+ }
57
+ }
58
+ return false;
59
+ };
60
+ };
61
+ export default getBlockMatcher;
@@ -0,0 +1,113 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import { WebParser } from '@lowdefy/operators';
16
+ import Actions from './Actions.js';
17
+ import Blocks from './Blocks.js';
18
+ import Requests from './Requests.js';
19
+ import State from './State.js';
20
+ const blockData = ({ areas , blockId , blocks , events , field , id , layout , pageId , properties , requests , required , style , type , validate , visible , })=>({
21
+ areas,
22
+ blockId,
23
+ blocks,
24
+ events,
25
+ field,
26
+ id,
27
+ layout,
28
+ pageId,
29
+ properties,
30
+ requests,
31
+ required,
32
+ style,
33
+ type,
34
+ validate,
35
+ visible
36
+ });
37
+ function getContext({ config , lowdefy , resetContext ={
38
+ reset: false,
39
+ setReset: ()=>undefined
40
+ } , }) {
41
+ if (!config) {
42
+ throw new Error('A page must be provided to get context.');
43
+ }
44
+ const { id } = config;
45
+ if (lowdefy.contexts[id] && !resetContext.reset) {
46
+ // memoize context if already created, eg between page transitions, unless the reset flag is raised
47
+ lowdefy.contexts[id]._internal.update();
48
+ return lowdefy.contexts[id];
49
+ }
50
+ resetContext.setReset(false); // lower context reset flag.
51
+ if (!lowdefy.inputs[id]) {
52
+ lowdefy.inputs[id] = {};
53
+ }
54
+ const ctx = {
55
+ id: id,
56
+ pageId: config.pageId,
57
+ eventLog: [],
58
+ requests: {},
59
+ state: {},
60
+ _internal: {
61
+ lowdefy,
62
+ rootBlock: blockData(config),
63
+ update: ()=>{}
64
+ }
65
+ };
66
+ const _internal = ctx._internal;
67
+ _internal.parser = new WebParser({
68
+ context: ctx,
69
+ operators: lowdefy._internal.operators
70
+ });
71
+ _internal.State = new State(ctx);
72
+ _internal.Actions = new Actions(ctx);
73
+ _internal.Requests = new Requests(ctx);
74
+ _internal.RootBlocks = new Blocks({
75
+ areas: {
76
+ root: {
77
+ blocks: [
78
+ _internal.rootBlock
79
+ ]
80
+ }
81
+ },
82
+ context: ctx
83
+ });
84
+ _internal.RootBlocks.init();
85
+ _internal.update = ()=>{
86
+ _internal.RootBlocks.update();
87
+ };
88
+ _internal.runOnInit = async (progress)=>{
89
+ progress();
90
+ if (!_internal.onInitDone) {
91
+ await _internal.RootBlocks.areas.root.blocks[0].triggerEvent({
92
+ name: 'onInit',
93
+ progress
94
+ });
95
+ _internal.update();
96
+ _internal.State.freezeState();
97
+ _internal.onInitDone = true;
98
+ }
99
+ };
100
+ _internal.runOnInitAsync = async (progress)=>{
101
+ if (_internal.onInitDone && !_internal.onInitAsyncDone) {
102
+ await _internal.RootBlocks.areas.root.blocks[0].triggerEvent({
103
+ name: 'onInitAsync',
104
+ progress
105
+ });
106
+ _internal.onInitAsyncDone = true;
107
+ }
108
+ };
109
+ ctx._internal.update();
110
+ lowdefy.contexts[id] = ctx;
111
+ return ctx;
112
+ }
113
+ export default getContext;
package/dist/index.js ADDED
@@ -0,0 +1,23 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import Actions from './Actions.js';
16
+ import Events from './Events.js';
17
+ import Blocks from './Blocks.js';
18
+ import createLink from './createLink.js';
19
+ import Requests from './Requests.js';
20
+ import State from './State.js';
21
+ import getContext from './getContext.js';
22
+ export { Actions, Events, Blocks, createLink, Requests, State };
23
+ export default getContext;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowdefy/engine",
3
- "version": "4.0.0-alpha.29",
3
+ "version": "4.0.0-alpha.30",
4
4
  "license": "Apache-2.0",
5
5
  "description": "",
6
6
  "homepage": "https://lowdefy.com",
@@ -36,15 +36,15 @@
36
36
  "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
37
37
  },
38
38
  "dependencies": {
39
- "@lowdefy/helpers": "4.0.0-alpha.29",
40
- "@lowdefy/operators": "4.0.0-alpha.29"
39
+ "@lowdefy/helpers": "4.0.0-alpha.30",
40
+ "@lowdefy/operators": "4.0.0-alpha.30"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@jest/globals": "28.1.0",
44
- "@lowdefy/actions-core": "4.0.0-alpha.29",
45
- "@lowdefy/build": "4.0.0-alpha.29",
46
- "@lowdefy/operators-js": "4.0.0-alpha.29",
47
- "@lowdefy/operators-mql": "4.0.0-alpha.29",
44
+ "@lowdefy/actions-core": "4.0.0-alpha.30",
45
+ "@lowdefy/build": "4.0.0-alpha.30",
46
+ "@lowdefy/operators-js": "4.0.0-alpha.30",
47
+ "@lowdefy/operators-mql": "4.0.0-alpha.30",
48
48
  "@swc/cli": "0.1.57",
49
49
  "@swc/core": "1.2.194",
50
50
  "@swc/jest": "0.2.21",
@@ -53,5 +53,5 @@
53
53
  "publishConfig": {
54
54
  "access": "public"
55
55
  },
56
- "gitHead": "621a191ebc0a1569ee6669dc74c12f8be5a8c7f3"
56
+ "gitHead": "c30cc4e28e221d9b73385cca8d339397d60da0e1"
57
57
  }