@player-ui/make-flow 0.4.0 → 0.4.1-next.0

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.
package/dist/index.cjs.js CHANGED
@@ -22,6 +22,25 @@ function identify(obj) {
22
22
  return 3;
23
23
  }
24
24
 
25
+ var __defProp = Object.defineProperty;
26
+ var __defProps = Object.defineProperties;
27
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
28
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
29
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
30
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
31
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
32
+ var __spreadValues = (a, b) => {
33
+ for (var prop in b || (b = {}))
34
+ if (__hasOwnProp.call(b, prop))
35
+ __defNormalProp(a, prop, b[prop]);
36
+ if (__getOwnPropSymbols)
37
+ for (var prop of __getOwnPropSymbols(b)) {
38
+ if (__propIsEnum.call(b, prop))
39
+ __defNormalProp(a, prop, b[prop]);
40
+ }
41
+ return a;
42
+ };
43
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
44
  function unwrapJSend(obj) {
26
45
  const isJSend = "status" in obj && "data" in obj;
27
46
  if (isJSend) {
@@ -29,7 +48,44 @@ function unwrapJSend(obj) {
29
48
  }
30
49
  return obj;
31
50
  }
32
- function makeFlow(obj) {
51
+ const createDefaultNav = (flow, options) => {
52
+ var _a, _b;
53
+ if ((flow.navigation === void 0 || flow.navigation === null) && Array.isArray(flow.views) && flow.views.length === 1) {
54
+ const navFlow = {
55
+ startState: "VIEW_0",
56
+ VIEW_0: {
57
+ state_type: "VIEW",
58
+ ref: (_a = flow.views[0].id) != null ? _a : `${flow.id}-views-0`,
59
+ transitions: {
60
+ "*": "END_done",
61
+ Prev: "END_back"
62
+ }
63
+ },
64
+ END_done: {
65
+ state_type: "END",
66
+ outcome: (_b = options == null ? void 0 : options.outcome) != null ? _b : "doneWithFlow"
67
+ },
68
+ END_back: {
69
+ state_type: "END",
70
+ outcome: "BACK"
71
+ }
72
+ };
73
+ if ((options == null ? void 0 : options.onStart) !== void 0) {
74
+ navFlow.onStart = options.onStart;
75
+ }
76
+ if ((options == null ? void 0 : options.onEnd) !== void 0) {
77
+ navFlow.onEnd = options.onEnd;
78
+ }
79
+ return __spreadProps(__spreadValues({}, flow), {
80
+ navigation: {
81
+ BEGIN: "Flow",
82
+ Flow: navFlow
83
+ }
84
+ });
85
+ }
86
+ return flow;
87
+ };
88
+ function makeFlow(obj, args) {
33
89
  const objified = unwrapJSend(typeof obj === "string" ? JSON.parse(obj) : obj);
34
90
  if (Array.isArray(objified)) {
35
91
  const collection = {
@@ -50,7 +106,7 @@ function makeFlow(obj) {
50
106
  throw new Error("No clue how to convert this into a flow. Just do it yourself");
51
107
  }
52
108
  if (type === exports.ObjType.FLOW) {
53
- return obj;
109
+ return createDefaultNav(obj, args);
54
110
  }
55
111
  if (type === exports.ObjType.ASSET_WRAPPER) {
56
112
  return makeFlow(obj.asset);
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Flow } from '@player-ui/types';
1
+ import { Flow, NavigationFlow, NavigationFlowEndState } from '@player-ui/types';
2
2
 
3
3
  declare enum ObjType {
4
4
  FLOW = 0,
@@ -9,9 +9,20 @@ declare enum ObjType {
9
9
  /** Try to identify any object as an Asset or Flow */
10
10
  declare function identify(obj: object): ObjType;
11
11
 
12
+ interface NavOptions {
13
+ /** An optional expression to run when this Flow starts */
14
+ onStart?: NavigationFlow['onStart'];
15
+ /** An optional expression to run when this Flow ends */
16
+ onEnd?: NavigationFlow['onEnd'];
17
+ /**
18
+ * A description of _how_ the flow ended.
19
+ * If this is a flow started from another flow, the outcome determines the flow transition
20
+ */
21
+ outcome?: NavigationFlowEndState['outcome'];
22
+ }
12
23
  /**
13
24
  * Take any given object and try to convert it to a flow
14
25
  */
15
- declare function makeFlow(obj: any): Flow;
26
+ declare function makeFlow(obj: any, args?: NavOptions): Flow;
16
27
 
17
28
  export { ObjType, identify, makeFlow };
package/dist/index.esm.js CHANGED
@@ -18,6 +18,25 @@ function identify(obj) {
18
18
  return 3;
19
19
  }
20
20
 
21
+ var __defProp = Object.defineProperty;
22
+ var __defProps = Object.defineProperties;
23
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
24
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
25
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
26
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
27
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
28
+ var __spreadValues = (a, b) => {
29
+ for (var prop in b || (b = {}))
30
+ if (__hasOwnProp.call(b, prop))
31
+ __defNormalProp(a, prop, b[prop]);
32
+ if (__getOwnPropSymbols)
33
+ for (var prop of __getOwnPropSymbols(b)) {
34
+ if (__propIsEnum.call(b, prop))
35
+ __defNormalProp(a, prop, b[prop]);
36
+ }
37
+ return a;
38
+ };
39
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
21
40
  function unwrapJSend(obj) {
22
41
  const isJSend = "status" in obj && "data" in obj;
23
42
  if (isJSend) {
@@ -25,7 +44,44 @@ function unwrapJSend(obj) {
25
44
  }
26
45
  return obj;
27
46
  }
28
- function makeFlow(obj) {
47
+ const createDefaultNav = (flow, options) => {
48
+ var _a, _b;
49
+ if ((flow.navigation === void 0 || flow.navigation === null) && Array.isArray(flow.views) && flow.views.length === 1) {
50
+ const navFlow = {
51
+ startState: "VIEW_0",
52
+ VIEW_0: {
53
+ state_type: "VIEW",
54
+ ref: (_a = flow.views[0].id) != null ? _a : `${flow.id}-views-0`,
55
+ transitions: {
56
+ "*": "END_done",
57
+ Prev: "END_back"
58
+ }
59
+ },
60
+ END_done: {
61
+ state_type: "END",
62
+ outcome: (_b = options == null ? void 0 : options.outcome) != null ? _b : "doneWithFlow"
63
+ },
64
+ END_back: {
65
+ state_type: "END",
66
+ outcome: "BACK"
67
+ }
68
+ };
69
+ if ((options == null ? void 0 : options.onStart) !== void 0) {
70
+ navFlow.onStart = options.onStart;
71
+ }
72
+ if ((options == null ? void 0 : options.onEnd) !== void 0) {
73
+ navFlow.onEnd = options.onEnd;
74
+ }
75
+ return __spreadProps(__spreadValues({}, flow), {
76
+ navigation: {
77
+ BEGIN: "Flow",
78
+ Flow: navFlow
79
+ }
80
+ });
81
+ }
82
+ return flow;
83
+ };
84
+ function makeFlow(obj, args) {
29
85
  const objified = unwrapJSend(typeof obj === "string" ? JSON.parse(obj) : obj);
30
86
  if (Array.isArray(objified)) {
31
87
  const collection = {
@@ -46,7 +102,7 @@ function makeFlow(obj) {
46
102
  throw new Error("No clue how to convert this into a flow. Just do it yourself");
47
103
  }
48
104
  if (type === ObjType.FLOW) {
49
- return obj;
105
+ return createDefaultNav(obj, args);
50
106
  }
51
107
  if (type === ObjType.ASSET_WRAPPER) {
52
108
  return makeFlow(obj.asset);
@@ -128,6 +128,25 @@ function identify(obj) {
128
128
  return 3;
129
129
  }
130
130
 
131
+ var __defProp = Object.defineProperty;
132
+ var __defProps = Object.defineProperties;
133
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
134
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
135
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
136
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
137
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
138
+ var __spreadValues = (a, b) => {
139
+ for (var prop in b || (b = {}))
140
+ if (__hasOwnProp.call(b, prop))
141
+ __defNormalProp(a, prop, b[prop]);
142
+ if (__getOwnPropSymbols)
143
+ for (var prop of __getOwnPropSymbols(b)) {
144
+ if (__propIsEnum.call(b, prop))
145
+ __defNormalProp(a, prop, b[prop]);
146
+ }
147
+ return a;
148
+ };
149
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
131
150
  function unwrapJSend(obj) {
132
151
  const isJSend = "status" in obj && "data" in obj;
133
152
  if (isJSend) {
@@ -135,7 +154,44 @@ function unwrapJSend(obj) {
135
154
  }
136
155
  return obj;
137
156
  }
138
- function makeFlow(obj) {
157
+ const createDefaultNav = (flow, options) => {
158
+ var _a, _b;
159
+ if ((flow.navigation === void 0 || flow.navigation === null) && Array.isArray(flow.views) && flow.views.length === 1) {
160
+ const navFlow = {
161
+ startState: "VIEW_0",
162
+ VIEW_0: {
163
+ state_type: "VIEW",
164
+ ref: (_a = flow.views[0].id) != null ? _a : `${flow.id}-views-0`,
165
+ transitions: {
166
+ "*": "END_done",
167
+ Prev: "END_back"
168
+ }
169
+ },
170
+ END_done: {
171
+ state_type: "END",
172
+ outcome: (_b = options == null ? void 0 : options.outcome) != null ? _b : "doneWithFlow"
173
+ },
174
+ END_back: {
175
+ state_type: "END",
176
+ outcome: "BACK"
177
+ }
178
+ };
179
+ if ((options == null ? void 0 : options.onStart) !== void 0) {
180
+ navFlow.onStart = options.onStart;
181
+ }
182
+ if ((options == null ? void 0 : options.onEnd) !== void 0) {
183
+ navFlow.onEnd = options.onEnd;
184
+ }
185
+ return __spreadProps(__spreadValues({}, flow), {
186
+ navigation: {
187
+ BEGIN: "Flow",
188
+ Flow: navFlow
189
+ }
190
+ });
191
+ }
192
+ return flow;
193
+ };
194
+ function makeFlow(obj, args) {
139
195
  const objified = unwrapJSend(typeof obj === "string" ? JSON.parse(obj) : obj);
140
196
  if (Array.isArray(objified)) {
141
197
  const collection = {
@@ -156,7 +212,7 @@ function makeFlow(obj) {
156
212
  throw new Error("No clue how to convert this into a flow. Just do it yourself");
157
213
  }
158
214
  if (type === ObjType.FLOW) {
159
- return obj;
215
+ return createDefaultNav(obj, args);
160
216
  }
161
217
  if (type === ObjType.ASSET_WRAPPER) {
162
218
  return makeFlow(obj.asset);
@@ -1 +1 @@
1
- !function(e,t){"object"===typeof exports&&"object"===typeof module?module.exports=t():"function"===typeof define&&define.amd?define([],t):"object"===typeof exports?exports.MakeFlow=t():e.MakeFlow=t()}(this,(function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!==typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"===typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){"use strict";var r,o;function i(e){return"id"in e&&"type"in e?1:"asset"in e&&1===i(e.asset)?2:"navigation"in e||"schema"in e||"views"in e?0:3}function u(e){const t=function(e){return"status"in e&&"data"in e?e.data:e}("string"===typeof e?JSON.parse(e):e);if(Array.isArray(t)){return u({id:"collection",type:"collection",values:t.map((e=>i(e)===r.ASSET?{asset:e}:e))})}const n=i(e);if(n===r.UNKNOWN)throw new Error("No clue how to convert this into a flow. Just do it yourself");return n===r.FLOW?e:n===r.ASSET_WRAPPER?u(e.asset):{id:"generated-flow",views:[e],data:{},navigation:{BEGIN:"FLOW_1",FLOW_1:{startState:"VIEW_1",VIEW_1:{state_type:"VIEW",ref:e.id,transitions:{"*":"END_Done"}},END_Done:{state_type:"END",outcome:"done"}}}}}n.r(t),n.d(t,"ObjType",(function(){return r})),n.d(t,"identify",(function(){return i})),n.d(t,"makeFlow",(function(){return u})),(o=r||(r={}))[o.FLOW=0]="FLOW",o[o.ASSET=1]="ASSET",o[o.ASSET_WRAPPER=2]="ASSET_WRAPPER",o[o.UNKNOWN=3]="UNKNOWN"}])}));
1
+ !function(e,t){"object"===typeof exports&&"object"===typeof module?module.exports=t():"function"===typeof define&&define.amd?define([],t):"object"===typeof exports?exports.MakeFlow=t():e.MakeFlow=t()}(this,(function(){return function(e){var t={};function n(o){if(t[o])return t[o].exports;var r=t[o]={i:o,l:!1,exports:{}};return e[o].call(r.exports,r,r.exports,n),r.l=!0,r.exports}return n.m=e,n.c=t,n.d=function(e,t,o){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},n.r=function(e){"undefined"!==typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"===typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(n.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var r in e)n.d(o,r,function(t){return e[t]}.bind(null,r));return o},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){"use strict";var o,r;function i(e){return"id"in e&&"type"in e?1:"asset"in e&&1===i(e.asset)?2:"navigation"in e||"schema"in e||"views"in e?0:3}n.r(t),n.d(t,"ObjType",(function(){return o})),n.d(t,"identify",(function(){return i})),n.d(t,"makeFlow",(function(){return y})),(r=o||(o={}))[r.FLOW=0]="FLOW",r[r.ASSET=1]="ASSET",r[r.ASSET_WRAPPER=2]="ASSET_WRAPPER",r[r.UNKNOWN=3]="UNKNOWN";var a=Object.defineProperty,u=Object.defineProperties,l=Object.getOwnPropertyDescriptors,s=Object.getOwnPropertySymbols,c=Object.prototype.hasOwnProperty,f=Object.prototype.propertyIsEnumerable,d=(e,t,n)=>t in e?a(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;const p=(e,t)=>{var n,o,r;if((void 0===e.navigation||null===e.navigation)&&Array.isArray(e.views)&&1===e.views.length){const i={startState:"VIEW_0",VIEW_0:{state_type:"VIEW",ref:null!=(n=e.views[0].id)?n:`${e.id}-views-0`,transitions:{"*":"END_done",Prev:"END_back"}},END_done:{state_type:"END",outcome:null!=(o=null==t?void 0:t.outcome)?o:"doneWithFlow"},END_back:{state_type:"END",outcome:"BACK"}};return void 0!==(null==t?void 0:t.onStart)&&(i.onStart=t.onStart),void 0!==(null==t?void 0:t.onEnd)&&(i.onEnd=t.onEnd),r=((e,t)=>{for(var n in t||(t={}))c.call(t,n)&&d(e,n,t[n]);if(s)for(var n of s(t))f.call(t,n)&&d(e,n,t[n]);return e})({},e),u(r,l({navigation:{BEGIN:"Flow",Flow:i}}))}return e};function y(e,t){const n=function(e){return"status"in e&&"data"in e?e.data:e}("string"===typeof e?JSON.parse(e):e);if(Array.isArray(n)){return y({id:"collection",type:"collection",values:n.map((e=>i(e)===o.ASSET?{asset:e}:e))})}const r=i(e);if(r===o.UNKNOWN)throw new Error("No clue how to convert this into a flow. Just do it yourself");return r===o.FLOW?p(e,t):r===o.ASSET_WRAPPER?y(e.asset):{id:"generated-flow",views:[e],data:{},navigation:{BEGIN:"FLOW_1",FLOW_1:{startState:"VIEW_1",VIEW_1:{state_type:"VIEW",ref:e.id,transitions:{"*":"END_Done"}},END_Done:{state_type:"END",outcome:"done"}}}}}}])}));
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@player-ui/make-flow",
3
- "version": "0.4.0",
3
+ "version": "0.4.1-next.0",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org"
7
7
  },
8
8
  "peerDependencies": {},
9
9
  "dependencies": {
10
- "@player-ui/types": "0.4.0",
10
+ "@player-ui/types": "0.4.1-next.0",
11
11
  "@babel/runtime": "7.15.4"
12
12
  },
13
13
  "main": "dist/index.cjs.js",
@@ -51,6 +51,14 @@
51
51
  {
52
52
  "name": "Kelly Harrop",
53
53
  "url": "https://github.com/kharrop"
54
+ },
55
+ {
56
+ "name": "Alejandro Fimbres",
57
+ "url": "https://github.com/lexfm"
58
+ },
59
+ {
60
+ "name": "Rafael Campos",
61
+ "url": "https://github.com/rafbcampos"
54
62
  }
55
63
  ],
56
64
  "bundle": "./dist/make-flow.prod.js"
package/src/index.ts CHANGED
@@ -1,4 +1,10 @@
1
- import type { Flow, Asset, AssetWrapper } from '@player-ui/types';
1
+ import type {
2
+ Flow,
3
+ Asset,
4
+ AssetWrapper,
5
+ NavigationFlow,
6
+ NavigationFlowEndState,
7
+ } from '@player-ui/types';
2
8
  import identify, { ObjType } from './identify';
3
9
 
4
10
  export * from './identify';
@@ -27,10 +33,71 @@ function unwrapJSend(obj: object) {
27
33
  return obj;
28
34
  }
29
35
 
36
+ interface NavOptions {
37
+ /** An optional expression to run when this Flow starts */
38
+ onStart?: NavigationFlow['onStart'];
39
+ /** An optional expression to run when this Flow ends */
40
+ onEnd?: NavigationFlow['onEnd'];
41
+ /**
42
+ * A description of _how_ the flow ended.
43
+ * If this is a flow started from another flow, the outcome determines the flow transition
44
+ */
45
+ outcome?: NavigationFlowEndState['outcome'];
46
+ }
47
+
48
+ /**
49
+ * create a default navigation if the flow was exactly one view and there is no navigation already
50
+ */
51
+ const createDefaultNav = (flow: Flow, options?: NavOptions): Flow => {
52
+ if (
53
+ (flow.navigation === undefined || flow.navigation === null) &&
54
+ Array.isArray(flow.views) &&
55
+ flow.views.length === 1
56
+ ) {
57
+ const navFlow: NavigationFlow = {
58
+ startState: 'VIEW_0',
59
+ VIEW_0: {
60
+ state_type: 'VIEW',
61
+ ref: flow.views[0].id ?? `${flow.id}-views-0`,
62
+ transitions: {
63
+ '*': 'END_done',
64
+ Prev: 'END_back',
65
+ },
66
+ },
67
+ END_done: {
68
+ state_type: 'END',
69
+ outcome: options?.outcome ?? 'doneWithFlow',
70
+ },
71
+ END_back: {
72
+ state_type: 'END',
73
+ outcome: 'BACK',
74
+ },
75
+ };
76
+
77
+ if (options?.onStart !== undefined) {
78
+ navFlow.onStart = options.onStart;
79
+ }
80
+
81
+ if (options?.onEnd !== undefined) {
82
+ navFlow.onEnd = options.onEnd;
83
+ }
84
+
85
+ return {
86
+ ...flow,
87
+ navigation: {
88
+ BEGIN: 'Flow',
89
+ Flow: navFlow,
90
+ },
91
+ };
92
+ }
93
+
94
+ return flow;
95
+ };
96
+
30
97
  /**
31
98
  * Take any given object and try to convert it to a flow
32
99
  */
33
- export function makeFlow(obj: any): Flow {
100
+ export function makeFlow(obj: any, args?: NavOptions): Flow {
34
101
  const objified = unwrapJSend(typeof obj === 'string' ? JSON.parse(obj) : obj);
35
102
 
36
103
  if (Array.isArray(objified)) {
@@ -60,7 +127,7 @@ export function makeFlow(obj: any): Flow {
60
127
  }
61
128
 
62
129
  if (type === ObjType.FLOW) {
63
- return obj;
130
+ return createDefaultNav(obj, args);
64
131
  }
65
132
 
66
133
  if (type === ObjType.ASSET_WRAPPER) {