@player-ui/make-flow 0.7.4-next.4 → 0.7.5--canary.434.14868
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/MakeFlow.native.js +234 -0
- package/dist/MakeFlow.native.js.map +1 -0
- package/dist/cjs/index.cjs +152 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/{index.esm.js → index.legacy-esm.js} +31 -43
- package/dist/{index.cjs.js → index.mjs} +31 -48
- package/dist/index.mjs.map +1 -0
- package/package.json +20 -57
- package/src/__tests__/identify.test.ts +32 -0
- package/src/__tests__/index.test.ts +100 -0
- package/src/identify.ts +4 -4
- package/src/index.ts +28 -28
- package/types/identify.d.ts +9 -0
- package/types/index.d.ts +20 -0
- package/dist/index.d.ts +0 -28
- package/dist/make-flow.dev.js +0 -251
- package/dist/make-flow.prod.js +0 -1
|
@@ -1,46 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
exports.ObjType = void 0;
|
|
6
|
-
(function(ObjType2) {
|
|
1
|
+
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/core/make-flow/src/identify.ts
|
|
2
|
+
var ObjType = /* @__PURE__ */ ((ObjType2) => {
|
|
7
3
|
ObjType2[ObjType2["FLOW"] = 0] = "FLOW";
|
|
8
4
|
ObjType2[ObjType2["ASSET"] = 1] = "ASSET";
|
|
9
5
|
ObjType2[ObjType2["ASSET_WRAPPER"] = 2] = "ASSET_WRAPPER";
|
|
10
6
|
ObjType2[ObjType2["UNKNOWN"] = 3] = "UNKNOWN";
|
|
11
|
-
|
|
7
|
+
return ObjType2;
|
|
8
|
+
})(ObjType || {});
|
|
12
9
|
function identify(obj) {
|
|
13
10
|
if ("id" in obj && "type" in obj) {
|
|
14
|
-
return 1
|
|
11
|
+
return 1 /* ASSET */;
|
|
15
12
|
}
|
|
16
|
-
if ("asset" in obj && identify(obj.asset) === 1) {
|
|
17
|
-
return 2
|
|
13
|
+
if ("asset" in obj && identify(obj.asset) === 1 /* ASSET */) {
|
|
14
|
+
return 2 /* ASSET_WRAPPER */;
|
|
18
15
|
}
|
|
19
16
|
if ("navigation" in obj || "schema" in obj || "views" in obj) {
|
|
20
|
-
return 0
|
|
17
|
+
return 0 /* FLOW */;
|
|
21
18
|
}
|
|
22
|
-
return 3
|
|
19
|
+
return 3 /* UNKNOWN */;
|
|
23
20
|
}
|
|
24
21
|
|
|
25
|
-
|
|
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));
|
|
22
|
+
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/core/make-flow/src/index.ts
|
|
44
23
|
function unwrapJSend(obj) {
|
|
45
24
|
const isJSend = "status" in obj && "data" in obj;
|
|
46
25
|
if (isJSend) {
|
|
@@ -48,14 +27,13 @@ function unwrapJSend(obj) {
|
|
|
48
27
|
}
|
|
49
28
|
return obj;
|
|
50
29
|
}
|
|
51
|
-
|
|
52
|
-
var _a, _b;
|
|
30
|
+
var createDefaultNav = (flow, options) => {
|
|
53
31
|
if ((flow.navigation === void 0 || flow.navigation === null) && Array.isArray(flow.views) && flow.views.length === 1) {
|
|
54
32
|
const navFlow = {
|
|
55
33
|
startState: "VIEW_0",
|
|
56
34
|
VIEW_0: {
|
|
57
35
|
state_type: "VIEW",
|
|
58
|
-
ref:
|
|
36
|
+
ref: flow.views[0].id ?? `${flow.id}-views-0`,
|
|
59
37
|
transitions: {
|
|
60
38
|
"*": "END_done",
|
|
61
39
|
Prev: "END_back"
|
|
@@ -63,25 +41,26 @@ const createDefaultNav = (flow, options) => {
|
|
|
63
41
|
},
|
|
64
42
|
END_done: {
|
|
65
43
|
state_type: "END",
|
|
66
|
-
outcome:
|
|
44
|
+
outcome: options?.outcome ?? "doneWithFlow"
|
|
67
45
|
},
|
|
68
46
|
END_back: {
|
|
69
47
|
state_type: "END",
|
|
70
48
|
outcome: "BACK"
|
|
71
49
|
}
|
|
72
50
|
};
|
|
73
|
-
if (
|
|
51
|
+
if (options?.onStart !== void 0) {
|
|
74
52
|
navFlow.onStart = options.onStart;
|
|
75
53
|
}
|
|
76
|
-
if (
|
|
54
|
+
if (options?.onEnd !== void 0) {
|
|
77
55
|
navFlow.onEnd = options.onEnd;
|
|
78
56
|
}
|
|
79
|
-
return
|
|
57
|
+
return {
|
|
58
|
+
...flow,
|
|
80
59
|
navigation: {
|
|
81
60
|
BEGIN: "Flow",
|
|
82
61
|
Flow: navFlow
|
|
83
62
|
}
|
|
84
|
-
}
|
|
63
|
+
};
|
|
85
64
|
}
|
|
86
65
|
return flow;
|
|
87
66
|
};
|
|
@@ -93,7 +72,7 @@ function makeFlow(obj, args) {
|
|
|
93
72
|
type: "collection",
|
|
94
73
|
values: objified.map((v) => {
|
|
95
74
|
const type2 = identify(v);
|
|
96
|
-
if (type2 ===
|
|
75
|
+
if (type2 === 1 /* ASSET */) {
|
|
97
76
|
return { asset: v };
|
|
98
77
|
}
|
|
99
78
|
return v;
|
|
@@ -102,13 +81,15 @@ function makeFlow(obj, args) {
|
|
|
102
81
|
return makeFlow(collection);
|
|
103
82
|
}
|
|
104
83
|
const type = identify(obj);
|
|
105
|
-
if (type ===
|
|
106
|
-
throw new Error(
|
|
84
|
+
if (type === 3 /* UNKNOWN */) {
|
|
85
|
+
throw new Error(
|
|
86
|
+
"No clue how to convert this into a flow. Just do it yourself"
|
|
87
|
+
);
|
|
107
88
|
}
|
|
108
|
-
if (type ===
|
|
89
|
+
if (type === 0 /* FLOW */) {
|
|
109
90
|
return createDefaultNav(obj, args);
|
|
110
91
|
}
|
|
111
|
-
if (type ===
|
|
92
|
+
if (type === 2 /* ASSET_WRAPPER */) {
|
|
112
93
|
return makeFlow(obj.asset);
|
|
113
94
|
}
|
|
114
95
|
return {
|
|
@@ -134,7 +115,9 @@ function makeFlow(obj, args) {
|
|
|
134
115
|
}
|
|
135
116
|
};
|
|
136
117
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
118
|
+
export {
|
|
119
|
+
ObjType,
|
|
120
|
+
identify,
|
|
121
|
+
makeFlow
|
|
122
|
+
};
|
|
123
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/core/make-flow/src/identify.ts","../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/core/make-flow/src/index.ts"],"sourcesContent":["import type { AssetWrapper } from \"@player-ui/types\";\n\nexport enum ObjType {\n FLOW,\n ASSET,\n ASSET_WRAPPER,\n UNKNOWN,\n}\n\n/** Try to identify any object as an Asset or Flow */\nexport default function identify(obj: object): ObjType {\n if (\"id\" in obj && \"type\" in obj) {\n return ObjType.ASSET;\n }\n\n if (\n \"asset\" in obj &&\n identify((obj as AssetWrapper).asset) === ObjType.ASSET\n ) {\n return ObjType.ASSET_WRAPPER;\n }\n\n if (\"navigation\" in obj || \"schema\" in obj || \"views\" in obj) {\n return ObjType.FLOW;\n }\n\n return ObjType.UNKNOWN;\n}\n","import type {\n Flow,\n Asset,\n AssetWrapper,\n NavigationFlow,\n NavigationFlowEndState,\n} from \"@player-ui/types\";\nimport identify, { ObjType } from \"./identify\";\n\nexport * from \"./identify\";\nexport { identify };\n\ninterface JSend<T> {\n /** The status of the JSEND wrapper */\n status: string;\n /** The data we care about */\n data: T;\n}\n\ninterface CollectionAsset extends Asset<\"collection\"> {\n /** The values of the collection. Used when there are an array of assets passed to the makeFlow fn */\n values: Array<AssetWrapper>;\n}\n\n/** Check an object for the JSEND wrapper and remove it if needed */\nfunction unwrapJSend(obj: object) {\n const isJSend = \"status\" in obj && \"data\" in obj;\n\n if (isJSend) {\n return (obj as JSend<object>).data;\n }\n\n return obj;\n}\n\ninterface NavOptions {\n /** An optional expression to run when this Flow starts */\n onStart?: NavigationFlow[\"onStart\"];\n /** An optional expression to run when this Flow ends */\n onEnd?: NavigationFlow[\"onEnd\"];\n /**\n * A description of _how_ the flow ended.\n * If this is a flow started from another flow, the outcome determines the flow transition\n */\n outcome?: NavigationFlowEndState[\"outcome\"];\n}\n\n/**\n * create a default navigation if the flow was exactly one view and there is no navigation already\n */\nconst createDefaultNav = (flow: Flow, options?: NavOptions): Flow => {\n if (\n (flow.navigation === undefined || flow.navigation === null) &&\n Array.isArray(flow.views) &&\n flow.views.length === 1\n ) {\n const navFlow: NavigationFlow = {\n startState: \"VIEW_0\",\n VIEW_0: {\n state_type: \"VIEW\",\n ref: flow.views[0].id ?? `${flow.id}-views-0`,\n transitions: {\n \"*\": \"END_done\",\n Prev: \"END_back\",\n },\n },\n END_done: {\n state_type: \"END\",\n outcome: options?.outcome ?? \"doneWithFlow\",\n },\n END_back: {\n state_type: \"END\",\n outcome: \"BACK\",\n },\n };\n\n if (options?.onStart !== undefined) {\n navFlow.onStart = options.onStart;\n }\n\n if (options?.onEnd !== undefined) {\n navFlow.onEnd = options.onEnd;\n }\n\n return {\n ...flow,\n navigation: {\n BEGIN: \"Flow\",\n Flow: navFlow,\n },\n };\n }\n\n return flow;\n};\n\n/**\n * Take any given object and try to convert it to a flow\n */\nexport function makeFlow(obj: any, args?: NavOptions): Flow {\n const objified = unwrapJSend(typeof obj === \"string\" ? JSON.parse(obj) : obj);\n\n if (Array.isArray(objified)) {\n const collection: CollectionAsset = {\n id: \"collection\",\n type: \"collection\",\n values: objified.map((v) => {\n const type = identify(v);\n\n if (type === ObjType.ASSET) {\n return { asset: v };\n }\n\n return v;\n }),\n };\n\n return makeFlow(collection);\n }\n\n const type = identify(obj);\n\n if (type === ObjType.UNKNOWN) {\n throw new Error(\n \"No clue how to convert this into a flow. Just do it yourself\",\n );\n }\n\n if (type === ObjType.FLOW) {\n return createDefaultNav(obj, args);\n }\n\n if (type === ObjType.ASSET_WRAPPER) {\n return makeFlow(obj.asset);\n }\n\n return {\n id: \"generated-flow\",\n views: [obj],\n data: {},\n navigation: {\n BEGIN: \"FLOW_1\",\n FLOW_1: {\n startState: \"VIEW_1\",\n VIEW_1: {\n state_type: \"VIEW\",\n ref: obj.id,\n transitions: {\n \"*\": \"END_Done\",\n },\n },\n END_Done: {\n state_type: \"END\",\n outcome: \"done\",\n },\n },\n },\n };\n}\n"],"mappings":";AAEO,IAAK,UAAL,kBAAKA,aAAL;AACL,EAAAA,kBAAA;AACA,EAAAA,kBAAA;AACA,EAAAA,kBAAA;AACA,EAAAA,kBAAA;AAJU,SAAAA;AAAA,GAAA;AAQG,SAAR,SAA0B,KAAsB;AACrD,MAAI,QAAQ,OAAO,UAAU,KAAK;AAChC,WAAO;AAAA,EACT;AAEA,MACE,WAAW,OACX,SAAU,IAAqB,KAAK,MAAM,eAC1C;AACA,WAAO;AAAA,EACT;AAEA,MAAI,gBAAgB,OAAO,YAAY,OAAO,WAAW,KAAK;AAC5D,WAAO;AAAA,EACT;AAEA,SAAO;AACT;;;ACFA,SAAS,YAAY,KAAa;AAChC,QAAM,UAAU,YAAY,OAAO,UAAU;AAE7C,MAAI,SAAS;AACX,WAAQ,IAAsB;AAAA,EAChC;AAEA,SAAO;AACT;AAiBA,IAAM,mBAAmB,CAAC,MAAY,YAA+B;AACnE,OACG,KAAK,eAAe,UAAa,KAAK,eAAe,SACtD,MAAM,QAAQ,KAAK,KAAK,KACxB,KAAK,MAAM,WAAW,GACtB;AACA,UAAM,UAA0B;AAAA,MAC9B,YAAY;AAAA,MACZ,QAAQ;AAAA,QACN,YAAY;AAAA,QACZ,KAAK,KAAK,MAAM,CAAC,EAAE,MAAM,GAAG,KAAK,EAAE;AAAA,QACnC,aAAa;AAAA,UACX,KAAK;AAAA,UACL,MAAM;AAAA,QACR;AAAA,MACF;AAAA,MACA,UAAU;AAAA,QACR,YAAY;AAAA,QACZ,SAAS,SAAS,WAAW;AAAA,MAC/B;AAAA,MACA,UAAU;AAAA,QACR,YAAY;AAAA,QACZ,SAAS;AAAA,MACX;AAAA,IACF;AAEA,QAAI,SAAS,YAAY,QAAW;AAClC,cAAQ,UAAU,QAAQ;AAAA,IAC5B;AAEA,QAAI,SAAS,UAAU,QAAW;AAChC,cAAQ,QAAQ,QAAQ;AAAA,IAC1B;AAEA,WAAO;AAAA,MACL,GAAG;AAAA,MACH,YAAY;AAAA,QACV,OAAO;AAAA,QACP,MAAM;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;AAKO,SAAS,SAAS,KAAU,MAAyB;AAC1D,QAAM,WAAW,YAAY,OAAO,QAAQ,WAAW,KAAK,MAAM,GAAG,IAAI,GAAG;AAE5E,MAAI,MAAM,QAAQ,QAAQ,GAAG;AAC3B,UAAM,aAA8B;AAAA,MAClC,IAAI;AAAA,MACJ,MAAM;AAAA,MACN,QAAQ,SAAS,IAAI,CAAC,MAAM;AAC1B,cAAMC,QAAO,SAAS,CAAC;AAEvB,YAAIA,yBAAwB;AAC1B,iBAAO,EAAE,OAAO,EAAE;AAAA,QACpB;AAEA,eAAO;AAAA,MACT,CAAC;AAAA,IACH;AAEA,WAAO,SAAS,UAAU;AAAA,EAC5B;AAEA,QAAM,OAAO,SAAS,GAAG;AAEzB,MAAI,0BAA0B;AAC5B,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAEA,MAAI,uBAAuB;AACzB,WAAO,iBAAiB,KAAK,IAAI;AAAA,EACnC;AAEA,MAAI,gCAAgC;AAClC,WAAO,SAAS,IAAI,KAAK;AAAA,EAC3B;AAEA,SAAO;AAAA,IACL,IAAI;AAAA,IACJ,OAAO,CAAC,GAAG;AAAA,IACX,MAAM,CAAC;AAAA,IACP,YAAY;AAAA,MACV,OAAO;AAAA,MACP,QAAQ;AAAA,QACN,YAAY;AAAA,QACZ,QAAQ;AAAA,UACN,YAAY;AAAA,UACZ,KAAK,IAAI;AAAA,UACT,aAAa;AAAA,YACX,KAAK;AAAA,UACP;AAAA,QACF;AAAA,QACA,UAAU;AAAA,UACR,YAAY;AAAA,UACZ,SAAS;AAAA,QACX;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;","names":["ObjType","type"]}
|
package/package.json
CHANGED
|
@@ -1,65 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@player-ui/make-flow",
|
|
3
|
-
"version": "0.7.
|
|
4
|
-
"
|
|
5
|
-
"publishConfig": {
|
|
6
|
-
"registry": "https://registry.npmjs.org"
|
|
7
|
-
},
|
|
8
|
-
"peerDependencies": {},
|
|
3
|
+
"version": "0.7.5--canary.434.14868",
|
|
4
|
+
"main": "dist/cjs/index.cjs",
|
|
9
5
|
"dependencies": {
|
|
10
|
-
"@player-ui/types": "0.7.
|
|
11
|
-
"
|
|
6
|
+
"@player-ui/types": "0.7.5--canary.434.14868",
|
|
7
|
+
"tslib": "^2.6.2"
|
|
12
8
|
},
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
9
|
+
"module": "dist/index.legacy-esm.js",
|
|
10
|
+
"types": "types/index.d.ts",
|
|
11
|
+
"bundle": "dist/MakeFlow.native.js",
|
|
16
12
|
"sideEffects": false,
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
},
|
|
25
|
-
"homepage": "https://player-ui.github.io",
|
|
26
|
-
"contributors": [
|
|
27
|
-
{
|
|
28
|
-
"name": "Adam Dierkens",
|
|
29
|
-
"url": "https://github.com/adierkens"
|
|
30
|
-
},
|
|
31
|
-
{
|
|
32
|
-
"name": "Spencer Hamm",
|
|
33
|
-
"url": "https://github.com/spentacular"
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
"name": "Harris Borawski",
|
|
37
|
-
"url": "https://github.com/hborawski"
|
|
38
|
-
},
|
|
39
|
-
{
|
|
40
|
-
"name": "Jeremiah Zucker",
|
|
41
|
-
"url": "https://github.com/sugarmanz"
|
|
42
|
-
},
|
|
43
|
-
{
|
|
44
|
-
"name": "Ketan Reddy",
|
|
45
|
-
"url": "https://github.com/KetanReddy"
|
|
46
|
-
},
|
|
47
|
-
{
|
|
48
|
-
"name": "Brocollie08",
|
|
49
|
-
"url": "https://github.com/brocollie08"
|
|
50
|
-
},
|
|
51
|
-
{
|
|
52
|
-
"name": "Kelly Harrop",
|
|
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"
|
|
13
|
+
"exports": {
|
|
14
|
+
"./package.json": "./package.json",
|
|
15
|
+
"./dist/index.css": "./dist/index.css",
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./types/index.d.ts",
|
|
18
|
+
"import": "./dist/index.mjs",
|
|
19
|
+
"default": "./dist/cjs/index.cjs"
|
|
62
20
|
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"src",
|
|
25
|
+
"types"
|
|
63
26
|
],
|
|
64
|
-
"
|
|
27
|
+
"peerDependencies": {}
|
|
65
28
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { test, expect } from "vitest";
|
|
2
|
+
import identify, { ObjType } from "../identify";
|
|
3
|
+
|
|
4
|
+
test("finds an asset", () => {
|
|
5
|
+
expect(
|
|
6
|
+
identify({
|
|
7
|
+
id: "some-id",
|
|
8
|
+
type: "text",
|
|
9
|
+
value: "I am a text asset",
|
|
10
|
+
}),
|
|
11
|
+
).toBe(ObjType.ASSET);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test("finds an asset wrapper", () => {
|
|
15
|
+
expect(
|
|
16
|
+
identify({
|
|
17
|
+
asset: {
|
|
18
|
+
id: "some-id",
|
|
19
|
+
type: "text",
|
|
20
|
+
value: "I am a text asset",
|
|
21
|
+
},
|
|
22
|
+
}),
|
|
23
|
+
).toBe(ObjType.ASSET_WRAPPER);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("finds a flow", () => {
|
|
27
|
+
expect(identify({ id: "foo", navigation: {} })).toBe(ObjType.FLOW);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("panics", () => {
|
|
31
|
+
expect(identify([])).toBe(ObjType.UNKNOWN);
|
|
32
|
+
});
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { test, expect } from "vitest";
|
|
2
|
+
import { makeFlow } from "..";
|
|
3
|
+
|
|
4
|
+
test("makes a flow from an asset", () => {
|
|
5
|
+
expect(
|
|
6
|
+
makeFlow({
|
|
7
|
+
id: "some-id",
|
|
8
|
+
type: "text",
|
|
9
|
+
value: "I am a text asset",
|
|
10
|
+
}),
|
|
11
|
+
).toStrictEqual({
|
|
12
|
+
id: "generated-flow",
|
|
13
|
+
views: [
|
|
14
|
+
{
|
|
15
|
+
id: "some-id",
|
|
16
|
+
type: "text",
|
|
17
|
+
value: "I am a text asset",
|
|
18
|
+
},
|
|
19
|
+
],
|
|
20
|
+
data: {},
|
|
21
|
+
navigation: {
|
|
22
|
+
BEGIN: "FLOW_1",
|
|
23
|
+
FLOW_1: {
|
|
24
|
+
startState: "VIEW_1",
|
|
25
|
+
VIEW_1: {
|
|
26
|
+
state_type: "VIEW",
|
|
27
|
+
ref: "some-id",
|
|
28
|
+
transitions: {
|
|
29
|
+
"*": "END_Done",
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
END_Done: {
|
|
33
|
+
state_type: "END",
|
|
34
|
+
outcome: "done",
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("makes a flow using an array of assets", () => {
|
|
42
|
+
expect(
|
|
43
|
+
makeFlow([
|
|
44
|
+
{
|
|
45
|
+
id: "some-id",
|
|
46
|
+
type: "text",
|
|
47
|
+
value: "I am a text asset",
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
asset: {
|
|
51
|
+
id: "some-id-2",
|
|
52
|
+
type: "text",
|
|
53
|
+
value: "I am a text asset",
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
]),
|
|
57
|
+
).toStrictEqual({
|
|
58
|
+
id: "generated-flow",
|
|
59
|
+
views: [
|
|
60
|
+
{
|
|
61
|
+
id: "collection",
|
|
62
|
+
type: "collection",
|
|
63
|
+
values: [
|
|
64
|
+
{
|
|
65
|
+
asset: {
|
|
66
|
+
id: "some-id",
|
|
67
|
+
type: "text",
|
|
68
|
+
value: "I am a text asset",
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
asset: {
|
|
73
|
+
id: "some-id-2",
|
|
74
|
+
type: "text",
|
|
75
|
+
value: "I am a text asset",
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
data: {},
|
|
82
|
+
navigation: {
|
|
83
|
+
BEGIN: "FLOW_1",
|
|
84
|
+
FLOW_1: {
|
|
85
|
+
startState: "VIEW_1",
|
|
86
|
+
VIEW_1: {
|
|
87
|
+
state_type: "VIEW",
|
|
88
|
+
ref: "collection",
|
|
89
|
+
transitions: {
|
|
90
|
+
"*": "END_Done",
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
END_Done: {
|
|
94
|
+
state_type: "END",
|
|
95
|
+
outcome: "done",
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
});
|
package/src/identify.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AssetWrapper } from
|
|
1
|
+
import type { AssetWrapper } from "@player-ui/types";
|
|
2
2
|
|
|
3
3
|
export enum ObjType {
|
|
4
4
|
FLOW,
|
|
@@ -9,18 +9,18 @@ export enum ObjType {
|
|
|
9
9
|
|
|
10
10
|
/** Try to identify any object as an Asset or Flow */
|
|
11
11
|
export default function identify(obj: object): ObjType {
|
|
12
|
-
if (
|
|
12
|
+
if ("id" in obj && "type" in obj) {
|
|
13
13
|
return ObjType.ASSET;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
if (
|
|
17
|
-
|
|
17
|
+
"asset" in obj &&
|
|
18
18
|
identify((obj as AssetWrapper).asset) === ObjType.ASSET
|
|
19
19
|
) {
|
|
20
20
|
return ObjType.ASSET_WRAPPER;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
if (
|
|
23
|
+
if ("navigation" in obj || "schema" in obj || "views" in obj) {
|
|
24
24
|
return ObjType.FLOW;
|
|
25
25
|
}
|
|
26
26
|
|
package/src/index.ts
CHANGED
|
@@ -4,10 +4,10 @@ import type {
|
|
|
4
4
|
AssetWrapper,
|
|
5
5
|
NavigationFlow,
|
|
6
6
|
NavigationFlowEndState,
|
|
7
|
-
} from
|
|
8
|
-
import identify, { ObjType } from
|
|
7
|
+
} from "@player-ui/types";
|
|
8
|
+
import identify, { ObjType } from "./identify";
|
|
9
9
|
|
|
10
|
-
export * from
|
|
10
|
+
export * from "./identify";
|
|
11
11
|
export { identify };
|
|
12
12
|
|
|
13
13
|
interface JSend<T> {
|
|
@@ -17,14 +17,14 @@ interface JSend<T> {
|
|
|
17
17
|
data: T;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
interface CollectionAsset extends Asset<
|
|
20
|
+
interface CollectionAsset extends Asset<"collection"> {
|
|
21
21
|
/** The values of the collection. Used when there are an array of assets passed to the makeFlow fn */
|
|
22
22
|
values: Array<AssetWrapper>;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
/** Check an object for the JSEND wrapper and remove it if needed */
|
|
26
26
|
function unwrapJSend(obj: object) {
|
|
27
|
-
const isJSend =
|
|
27
|
+
const isJSend = "status" in obj && "data" in obj;
|
|
28
28
|
|
|
29
29
|
if (isJSend) {
|
|
30
30
|
return (obj as JSend<object>).data;
|
|
@@ -35,14 +35,14 @@ function unwrapJSend(obj: object) {
|
|
|
35
35
|
|
|
36
36
|
interface NavOptions {
|
|
37
37
|
/** An optional expression to run when this Flow starts */
|
|
38
|
-
onStart?: NavigationFlow[
|
|
38
|
+
onStart?: NavigationFlow["onStart"];
|
|
39
39
|
/** An optional expression to run when this Flow ends */
|
|
40
|
-
onEnd?: NavigationFlow[
|
|
40
|
+
onEnd?: NavigationFlow["onEnd"];
|
|
41
41
|
/**
|
|
42
42
|
* A description of _how_ the flow ended.
|
|
43
43
|
* If this is a flow started from another flow, the outcome determines the flow transition
|
|
44
44
|
*/
|
|
45
|
-
outcome?: NavigationFlowEndState[
|
|
45
|
+
outcome?: NavigationFlowEndState["outcome"];
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
/**
|
|
@@ -55,22 +55,22 @@ const createDefaultNav = (flow: Flow, options?: NavOptions): Flow => {
|
|
|
55
55
|
flow.views.length === 1
|
|
56
56
|
) {
|
|
57
57
|
const navFlow: NavigationFlow = {
|
|
58
|
-
startState:
|
|
58
|
+
startState: "VIEW_0",
|
|
59
59
|
VIEW_0: {
|
|
60
|
-
state_type:
|
|
60
|
+
state_type: "VIEW",
|
|
61
61
|
ref: flow.views[0].id ?? `${flow.id}-views-0`,
|
|
62
62
|
transitions: {
|
|
63
|
-
|
|
64
|
-
Prev:
|
|
63
|
+
"*": "END_done",
|
|
64
|
+
Prev: "END_back",
|
|
65
65
|
},
|
|
66
66
|
},
|
|
67
67
|
END_done: {
|
|
68
|
-
state_type:
|
|
69
|
-
outcome: options?.outcome ??
|
|
68
|
+
state_type: "END",
|
|
69
|
+
outcome: options?.outcome ?? "doneWithFlow",
|
|
70
70
|
},
|
|
71
71
|
END_back: {
|
|
72
|
-
state_type:
|
|
73
|
-
outcome:
|
|
72
|
+
state_type: "END",
|
|
73
|
+
outcome: "BACK",
|
|
74
74
|
},
|
|
75
75
|
};
|
|
76
76
|
|
|
@@ -85,7 +85,7 @@ const createDefaultNav = (flow: Flow, options?: NavOptions): Flow => {
|
|
|
85
85
|
return {
|
|
86
86
|
...flow,
|
|
87
87
|
navigation: {
|
|
88
|
-
BEGIN:
|
|
88
|
+
BEGIN: "Flow",
|
|
89
89
|
Flow: navFlow,
|
|
90
90
|
},
|
|
91
91
|
};
|
|
@@ -98,12 +98,12 @@ const createDefaultNav = (flow: Flow, options?: NavOptions): Flow => {
|
|
|
98
98
|
* Take any given object and try to convert it to a flow
|
|
99
99
|
*/
|
|
100
100
|
export function makeFlow(obj: any, args?: NavOptions): Flow {
|
|
101
|
-
const objified = unwrapJSend(typeof obj ===
|
|
101
|
+
const objified = unwrapJSend(typeof obj === "string" ? JSON.parse(obj) : obj);
|
|
102
102
|
|
|
103
103
|
if (Array.isArray(objified)) {
|
|
104
104
|
const collection: CollectionAsset = {
|
|
105
|
-
id:
|
|
106
|
-
type:
|
|
105
|
+
id: "collection",
|
|
106
|
+
type: "collection",
|
|
107
107
|
values: objified.map((v) => {
|
|
108
108
|
const type = identify(v);
|
|
109
109
|
|
|
@@ -122,7 +122,7 @@ export function makeFlow(obj: any, args?: NavOptions): Flow {
|
|
|
122
122
|
|
|
123
123
|
if (type === ObjType.UNKNOWN) {
|
|
124
124
|
throw new Error(
|
|
125
|
-
|
|
125
|
+
"No clue how to convert this into a flow. Just do it yourself",
|
|
126
126
|
);
|
|
127
127
|
}
|
|
128
128
|
|
|
@@ -135,23 +135,23 @@ export function makeFlow(obj: any, args?: NavOptions): Flow {
|
|
|
135
135
|
}
|
|
136
136
|
|
|
137
137
|
return {
|
|
138
|
-
id:
|
|
138
|
+
id: "generated-flow",
|
|
139
139
|
views: [obj],
|
|
140
140
|
data: {},
|
|
141
141
|
navigation: {
|
|
142
|
-
BEGIN:
|
|
142
|
+
BEGIN: "FLOW_1",
|
|
143
143
|
FLOW_1: {
|
|
144
|
-
startState:
|
|
144
|
+
startState: "VIEW_1",
|
|
145
145
|
VIEW_1: {
|
|
146
|
-
state_type:
|
|
146
|
+
state_type: "VIEW",
|
|
147
147
|
ref: obj.id,
|
|
148
148
|
transitions: {
|
|
149
|
-
|
|
149
|
+
"*": "END_Done",
|
|
150
150
|
},
|
|
151
151
|
},
|
|
152
152
|
END_Done: {
|
|
153
|
-
state_type:
|
|
154
|
-
outcome:
|
|
153
|
+
state_type: "END",
|
|
154
|
+
outcome: "done",
|
|
155
155
|
},
|
|
156
156
|
},
|
|
157
157
|
},
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Flow, NavigationFlow, NavigationFlowEndState } from "@player-ui/types";
|
|
2
|
+
import identify from "./identify";
|
|
3
|
+
export * from "./identify";
|
|
4
|
+
export { identify };
|
|
5
|
+
interface NavOptions {
|
|
6
|
+
/** An optional expression to run when this Flow starts */
|
|
7
|
+
onStart?: NavigationFlow["onStart"];
|
|
8
|
+
/** An optional expression to run when this Flow ends */
|
|
9
|
+
onEnd?: NavigationFlow["onEnd"];
|
|
10
|
+
/**
|
|
11
|
+
* A description of _how_ the flow ended.
|
|
12
|
+
* If this is a flow started from another flow, the outcome determines the flow transition
|
|
13
|
+
*/
|
|
14
|
+
outcome?: NavigationFlowEndState["outcome"];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Take any given object and try to convert it to a flow
|
|
18
|
+
*/
|
|
19
|
+
export declare function makeFlow(obj: any, args?: NavOptions): Flow;
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { Flow, NavigationFlow, NavigationFlowEndState } from '@player-ui/types';
|
|
2
|
-
|
|
3
|
-
declare enum ObjType {
|
|
4
|
-
FLOW = 0,
|
|
5
|
-
ASSET = 1,
|
|
6
|
-
ASSET_WRAPPER = 2,
|
|
7
|
-
UNKNOWN = 3
|
|
8
|
-
}
|
|
9
|
-
/** Try to identify any object as an Asset or Flow */
|
|
10
|
-
declare function identify(obj: object): ObjType;
|
|
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
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Take any given object and try to convert it to a flow
|
|
25
|
-
*/
|
|
26
|
-
declare function makeFlow(obj: any, args?: NavOptions): Flow;
|
|
27
|
-
|
|
28
|
-
export { ObjType, identify, makeFlow };
|