@miso.ai/client-sdk-dev-tool 1.3.0-beta.2

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,141 @@
1
+ const TAG = '%cMiso';
2
+ const STYLE = 'color: #fff; background-color: #334cbb; padding: 2px 2px 1px 4px;';
3
+ const ID = 'std:debug';
4
+
5
+ const _log = function (path, name) {
6
+ for (var _len = arguments.length, data = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
7
+ data[_key - 2] = arguments[_key];
8
+ }
9
+
10
+ return console.log(TAG, STYLE, _path(path), _name(name), ...data);
11
+ }; // format path
12
+
13
+
14
+ function _path(path) {
15
+ return "<".concat(typeof path === 'string' ? path : path.filter(v => v).join('/'), ">");
16
+ } // format event name
17
+
18
+
19
+ function _name(name) {
20
+ return "[".concat(name, "]");
21
+ }
22
+
23
+ export default class DebugPlugin {
24
+ constructor() {
25
+ this._log = console.log.bind(console, TAG, STYLE);
26
+ }
27
+
28
+ static get id() {
29
+ return ID;
30
+ } // TODO: config({ active })
31
+
32
+
33
+ install(MisoClient) {
34
+ this._injectComponents(MisoClient);
35
+ }
36
+
37
+ _injectComponents(component) {
38
+ let treePath = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
39
+ component.on('child', c => this._injectComponents(c, treePath));
40
+ component.on('subtree', c => this._injectComponents(c, treePath.concat(component.meta.path)));
41
+ component.on('*', (data, meta) => this._handleEvent(component, meta, data, treePath));
42
+ }
43
+
44
+ _handleEvent(component, _ref, data) {
45
+ let {
46
+ name
47
+ } = _ref;
48
+ let treePath = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
49
+
50
+ if (name === 'child' || name === 'subtree') {
51
+ return;
52
+ }
53
+
54
+ const path = treePath.concat(component.meta.path);
55
+
56
+ if (!path.length) {
57
+ switch (name) {
58
+ case 'create':
59
+ this._handleCreateClient(name, data);
60
+
61
+ return;
62
+ }
63
+ }
64
+
65
+ switch (path[0]) {
66
+ case 'client':
67
+ switch (path[1]) {
68
+ case 'api':
69
+ this._handleApiEvent(name, { ...data,
70
+ groupName: path[2]
71
+ });
72
+
73
+ return;
74
+ }
75
+
76
+ break;
77
+
78
+ case 'plugins':
79
+ if (path.length === 1) {
80
+ this._handlePluginsEvent(name, data);
81
+ } else {
82
+ this._handlePluginSpecificEvent(path[1], path.slice(2), name, data);
83
+ }
84
+
85
+ return;
86
+ }
87
+
88
+ _log(path.join('.'), name, [data]);
89
+ }
90
+
91
+ _handleCreateClient(eventName, data) {
92
+ _log('client', eventName, [data]);
93
+ }
94
+
95
+ _handleApiEvent(eventName, _ref2) {
96
+ let {
97
+ groupName,
98
+ apiName,
99
+ url,
100
+ bulk,
101
+ ...data
102
+ } = _ref2;
103
+ const pathname = new URL(url).pathname;
104
+ const args = ['api', eventName];
105
+ bulk && args.push("(bulk ".concat(bulk.bulkId, ")"));
106
+ args.push("POST ".concat(pathname));
107
+
108
+ if (groupName === 'interactions') {
109
+ // TODO: handle multiple interactions
110
+ const {
111
+ type
112
+ } = data.payload.data[0];
113
+ args.push(type);
114
+ }
115
+
116
+ args.push([{ ...data,
117
+ url
118
+ }]);
119
+
120
+ _log.apply(undefined, args);
121
+ }
122
+
123
+ _handlePluginsEvent(eventName, plugin) {
124
+ _log('plugins', eventName, "".concat(plugin.id || '(anonymous)'), [plugin]);
125
+ }
126
+
127
+ _handlePluginSpecificEvent(pluginId, path, name, data) {
128
+ _log([pluginId, path.join('.')], name, [data]);
129
+ }
130
+
131
+ _getPath(component) {
132
+ const path = [];
133
+
134
+ for (let c = component; c; c = c.meta.parent) {
135
+ c.meta.name && path.push(c.meta.name);
136
+ }
137
+
138
+ return path.reverse();
139
+ }
140
+
141
+ }
@@ -0,0 +1,42 @@
1
+ const ID = 'std:dry-run';
2
+ export default class DryRunPlugin {
3
+ constructor() {
4
+ let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
5
+ this._options = options;
6
+ this.id = 'std:dry-run';
7
+ this.name = 'dry-run';
8
+ }
9
+
10
+ static get id() {
11
+ return ID;
12
+ } // TODO: config({ active })
13
+
14
+
15
+ install(_, context) {
16
+ const self = this;
17
+
18
+ context.classes.api.ApiBase.prototype._send = async function (_ref) {
19
+ let {
20
+ url
21
+ } = _ref;
22
+
23
+ this._events.emit('dry-run-send', {
24
+ url
25
+ });
26
+
27
+ return self._mockResponse();
28
+ };
29
+ }
30
+
31
+ _mockResponse() {
32
+ // TODO: take api type
33
+ return {
34
+ data: {
35
+ products: [],
36
+ attributes: [],
37
+ completions: {}
38
+ }
39
+ };
40
+ }
41
+
42
+ }
@@ -0,0 +1,2 @@
1
+ export { default as DebugPlugin } from './debug';
2
+ export { default as DryRunPlugin } from './dry-run';
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@miso.ai/client-sdk-dev-tool",
3
+ "description": "Dev tool plugins for Miso JavaScript client SDK.",
4
+ "main": "dist/es/index.js",
5
+ "module": "dist/es/index.js",
6
+ "files": [
7
+ "dist/*"
8
+ ],
9
+ "publishConfig": {
10
+ "access": "public"
11
+ },
12
+ "scripts": {
13
+ "start": "npx babel src --root-mode upward --out-dir dist/es --watch",
14
+ "test": "echo later",
15
+ "build:clean": "npx rimraf dist",
16
+ "build:esm": "npx babel src --root-mode upward --out-dir dist/es",
17
+ "build": "npm run build:clean && npm run build:esm"
18
+ },
19
+ "repository": "MisoAI/miso-client-js-sdk",
20
+ "homepage": "https://misoai.github.io/miso-client-js-sdk",
21
+ "license": "MIT",
22
+ "contributors": [
23
+ "simonpai <simon.pai@askmiso.com>"
24
+ ],
25
+ "bugs": {
26
+ "url": "https://github.com/MisoAI/miso-client-js-sdk/issues"
27
+ },
28
+ "dependencies": {
29
+ "@miso.ai/commons": "1.3.0-beta.2"
30
+ },
31
+ "version": "1.3.0-beta.2"
32
+ }