@player-ui/make-flow 0.0.1-next.1
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 +84 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.esm.js +79 -0
- package/package.json +16 -0
- package/src/identify.ts +28 -0
- package/src/index.ts +92 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
exports.ObjType = void 0;
|
|
6
|
+
(function(ObjType2) {
|
|
7
|
+
ObjType2[ObjType2["FLOW"] = 0] = "FLOW";
|
|
8
|
+
ObjType2[ObjType2["ASSET"] = 1] = "ASSET";
|
|
9
|
+
ObjType2[ObjType2["ASSET_WRAPPER"] = 2] = "ASSET_WRAPPER";
|
|
10
|
+
ObjType2[ObjType2["UNKNOWN"] = 3] = "UNKNOWN";
|
|
11
|
+
})(exports.ObjType || (exports.ObjType = {}));
|
|
12
|
+
function identify(obj) {
|
|
13
|
+
if ("id" in obj && "type" in obj) {
|
|
14
|
+
return 1;
|
|
15
|
+
}
|
|
16
|
+
if ("asset" in obj && identify(obj.asset) === 1) {
|
|
17
|
+
return 2;
|
|
18
|
+
}
|
|
19
|
+
if ("navigation" in obj || "schema" in obj || "views" in obj) {
|
|
20
|
+
return 0;
|
|
21
|
+
}
|
|
22
|
+
return 3;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function unwrapJSend(obj) {
|
|
26
|
+
const isJSend = "status" in obj && "data" in obj;
|
|
27
|
+
if (isJSend) {
|
|
28
|
+
return obj.data;
|
|
29
|
+
}
|
|
30
|
+
return obj;
|
|
31
|
+
}
|
|
32
|
+
function makeFlow(obj) {
|
|
33
|
+
const objified = unwrapJSend(typeof obj === "string" ? JSON.parse(obj) : obj);
|
|
34
|
+
if (Array.isArray(objified)) {
|
|
35
|
+
const collection = {
|
|
36
|
+
id: "collection",
|
|
37
|
+
type: "collection",
|
|
38
|
+
values: objified.map((v) => {
|
|
39
|
+
const type2 = identify(v);
|
|
40
|
+
if (type2 === exports.ObjType.ASSET) {
|
|
41
|
+
return { asset: v };
|
|
42
|
+
}
|
|
43
|
+
return v;
|
|
44
|
+
})
|
|
45
|
+
};
|
|
46
|
+
return makeFlow(collection);
|
|
47
|
+
}
|
|
48
|
+
const type = identify(obj);
|
|
49
|
+
if (type === exports.ObjType.UNKNOWN) {
|
|
50
|
+
throw new Error("No clue how to convert this into a flow. Just do it yourself");
|
|
51
|
+
}
|
|
52
|
+
if (type === exports.ObjType.FLOW) {
|
|
53
|
+
return obj;
|
|
54
|
+
}
|
|
55
|
+
if (type === exports.ObjType.ASSET_WRAPPER) {
|
|
56
|
+
return makeFlow(obj.asset);
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
id: "generated-flow",
|
|
60
|
+
views: [obj],
|
|
61
|
+
data: {},
|
|
62
|
+
navigation: {
|
|
63
|
+
BEGIN: "FLOW_1",
|
|
64
|
+
FLOW_1: {
|
|
65
|
+
startState: "VIEW_1",
|
|
66
|
+
VIEW_1: {
|
|
67
|
+
state_type: "VIEW",
|
|
68
|
+
ref: obj.id,
|
|
69
|
+
transitions: {
|
|
70
|
+
"*": "END_Done"
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
END_Done: {
|
|
74
|
+
state_type: "END",
|
|
75
|
+
outcome: "done"
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
exports.identify = identify;
|
|
83
|
+
exports.makeFlow = makeFlow;
|
|
84
|
+
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Flow } 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
|
+
/**
|
|
13
|
+
* Take any given object and try to convert it to a flow
|
|
14
|
+
*/
|
|
15
|
+
declare function makeFlow(obj: any): Flow;
|
|
16
|
+
|
|
17
|
+
export { ObjType, identify, makeFlow };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
var ObjType;
|
|
2
|
+
(function(ObjType2) {
|
|
3
|
+
ObjType2[ObjType2["FLOW"] = 0] = "FLOW";
|
|
4
|
+
ObjType2[ObjType2["ASSET"] = 1] = "ASSET";
|
|
5
|
+
ObjType2[ObjType2["ASSET_WRAPPER"] = 2] = "ASSET_WRAPPER";
|
|
6
|
+
ObjType2[ObjType2["UNKNOWN"] = 3] = "UNKNOWN";
|
|
7
|
+
})(ObjType || (ObjType = {}));
|
|
8
|
+
function identify(obj) {
|
|
9
|
+
if ("id" in obj && "type" in obj) {
|
|
10
|
+
return 1;
|
|
11
|
+
}
|
|
12
|
+
if ("asset" in obj && identify(obj.asset) === 1) {
|
|
13
|
+
return 2;
|
|
14
|
+
}
|
|
15
|
+
if ("navigation" in obj || "schema" in obj || "views" in obj) {
|
|
16
|
+
return 0;
|
|
17
|
+
}
|
|
18
|
+
return 3;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function unwrapJSend(obj) {
|
|
22
|
+
const isJSend = "status" in obj && "data" in obj;
|
|
23
|
+
if (isJSend) {
|
|
24
|
+
return obj.data;
|
|
25
|
+
}
|
|
26
|
+
return obj;
|
|
27
|
+
}
|
|
28
|
+
function makeFlow(obj) {
|
|
29
|
+
const objified = unwrapJSend(typeof obj === "string" ? JSON.parse(obj) : obj);
|
|
30
|
+
if (Array.isArray(objified)) {
|
|
31
|
+
const collection = {
|
|
32
|
+
id: "collection",
|
|
33
|
+
type: "collection",
|
|
34
|
+
values: objified.map((v) => {
|
|
35
|
+
const type2 = identify(v);
|
|
36
|
+
if (type2 === ObjType.ASSET) {
|
|
37
|
+
return { asset: v };
|
|
38
|
+
}
|
|
39
|
+
return v;
|
|
40
|
+
})
|
|
41
|
+
};
|
|
42
|
+
return makeFlow(collection);
|
|
43
|
+
}
|
|
44
|
+
const type = identify(obj);
|
|
45
|
+
if (type === ObjType.UNKNOWN) {
|
|
46
|
+
throw new Error("No clue how to convert this into a flow. Just do it yourself");
|
|
47
|
+
}
|
|
48
|
+
if (type === ObjType.FLOW) {
|
|
49
|
+
return obj;
|
|
50
|
+
}
|
|
51
|
+
if (type === ObjType.ASSET_WRAPPER) {
|
|
52
|
+
return makeFlow(obj.asset);
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
id: "generated-flow",
|
|
56
|
+
views: [obj],
|
|
57
|
+
data: {},
|
|
58
|
+
navigation: {
|
|
59
|
+
BEGIN: "FLOW_1",
|
|
60
|
+
FLOW_1: {
|
|
61
|
+
startState: "VIEW_1",
|
|
62
|
+
VIEW_1: {
|
|
63
|
+
state_type: "VIEW",
|
|
64
|
+
ref: obj.id,
|
|
65
|
+
transitions: {
|
|
66
|
+
"*": "END_Done"
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
END_Done: {
|
|
70
|
+
state_type: "END",
|
|
71
|
+
outcome: "done"
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export { ObjType, identify, makeFlow };
|
|
79
|
+
//# sourceMappingURL=index.esm.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@player-ui/make-flow",
|
|
3
|
+
"version": "0.0.1-next.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"registry": "https://registry.npmjs.org"
|
|
7
|
+
},
|
|
8
|
+
"peerDependencies": {},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@player-ui/types": "0.0.1-next.1",
|
|
11
|
+
"@babel/runtime": "7.15.4"
|
|
12
|
+
},
|
|
13
|
+
"main": "dist/index.cjs.js",
|
|
14
|
+
"module": "dist/index.esm.js",
|
|
15
|
+
"typings": "dist/index.d.ts"
|
|
16
|
+
}
|
package/src/identify.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { AssetWrapper } from '@player-ui/types';
|
|
2
|
+
|
|
3
|
+
export enum ObjType {
|
|
4
|
+
FLOW,
|
|
5
|
+
ASSET,
|
|
6
|
+
ASSET_WRAPPER,
|
|
7
|
+
UNKNOWN,
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Try to identify any object as an Asset or Flow */
|
|
11
|
+
export default function identify(obj: object): ObjType {
|
|
12
|
+
if ('id' in obj && 'type' in obj) {
|
|
13
|
+
return ObjType.ASSET;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (
|
|
17
|
+
'asset' in obj &&
|
|
18
|
+
identify((obj as AssetWrapper).asset) === ObjType.ASSET
|
|
19
|
+
) {
|
|
20
|
+
return ObjType.ASSET_WRAPPER;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if ('navigation' in obj || 'schema' in obj || 'views' in obj) {
|
|
24
|
+
return ObjType.FLOW;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return ObjType.UNKNOWN;
|
|
28
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { Flow, Asset, AssetWrapper } from '@player-ui/types';
|
|
2
|
+
import identify, { ObjType } from './identify';
|
|
3
|
+
|
|
4
|
+
export * from './identify';
|
|
5
|
+
export { identify };
|
|
6
|
+
|
|
7
|
+
interface JSend<T> {
|
|
8
|
+
/** The status of the JSEND wrapper */
|
|
9
|
+
status: string;
|
|
10
|
+
/** The data we care about */
|
|
11
|
+
data: T;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface CollectionAsset extends Asset<'collection'> {
|
|
15
|
+
/** The values of the collection. Used when there are an array of assets passed to the makeFlow fn */
|
|
16
|
+
values: Array<AssetWrapper>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Check an object for the JSEND wrapper and remove it if needed */
|
|
20
|
+
function unwrapJSend(obj: object) {
|
|
21
|
+
const isJSend = 'status' in obj && 'data' in obj;
|
|
22
|
+
|
|
23
|
+
if (isJSend) {
|
|
24
|
+
return (obj as JSend<object>).data;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return obj;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Take any given object and try to convert it to a flow
|
|
32
|
+
*/
|
|
33
|
+
export function makeFlow(obj: any): Flow {
|
|
34
|
+
const objified = unwrapJSend(typeof obj === 'string' ? JSON.parse(obj) : obj);
|
|
35
|
+
|
|
36
|
+
if (Array.isArray(objified)) {
|
|
37
|
+
const collection: CollectionAsset = {
|
|
38
|
+
id: 'collection',
|
|
39
|
+
type: 'collection',
|
|
40
|
+
values: objified.map((v) => {
|
|
41
|
+
const type = identify(v);
|
|
42
|
+
|
|
43
|
+
if (type === ObjType.ASSET) {
|
|
44
|
+
return { asset: v };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return v;
|
|
48
|
+
}),
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
return makeFlow(collection);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const type = identify(obj);
|
|
55
|
+
|
|
56
|
+
if (type === ObjType.UNKNOWN) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
'No clue how to convert this into a flow. Just do it yourself'
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (type === ObjType.FLOW) {
|
|
63
|
+
return obj;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (type === ObjType.ASSET_WRAPPER) {
|
|
67
|
+
return makeFlow(obj.asset);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
id: 'generated-flow',
|
|
72
|
+
views: [obj],
|
|
73
|
+
data: {},
|
|
74
|
+
navigation: {
|
|
75
|
+
BEGIN: 'FLOW_1',
|
|
76
|
+
FLOW_1: {
|
|
77
|
+
startState: 'VIEW_1',
|
|
78
|
+
VIEW_1: {
|
|
79
|
+
state_type: 'VIEW',
|
|
80
|
+
ref: obj.id,
|
|
81
|
+
transitions: {
|
|
82
|
+
'*': 'END_Done',
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
END_Done: {
|
|
86
|
+
state_type: 'END',
|
|
87
|
+
outcome: 'done',
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|