commandbar 1.3.1 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. package/build/commandbar-js/src/index.d.ts +10 -0
  2. package/build/commandbar-js/src/index.js +1 -0
  3. package/build/commandbar-js/src/init.d.ts +17 -0
  4. package/build/commandbar-js/src/snippet.d.ts +1 -0
  5. package/build/internal/src/client/AddContextOptions.d.ts +57 -0
  6. package/build/internal/src/client/CommandBarClientSDK.d.ts +182 -0
  7. package/build/internal/src/client/CommandBarProxySDK.d.ts +48 -0
  8. package/build/internal/src/client/CommandBarSDK.d.ts +42 -0
  9. package/build/internal/src/client/EventHandler.d.ts +54 -0
  10. package/build/internal/src/client/Reporting.d.ts +1 -0
  11. package/build/internal/src/client/SDKConfig.d.ts +7 -0
  12. package/build/internal/src/client/SentryReporter.d.ts +41 -0
  13. package/build/internal/src/client/proxy.d.ts +15 -0
  14. package/build/internal/src/client/symbols.d.ts +27 -0
  15. package/build/internal/src/middleware/CommandFromClientV.d.ts +128 -0
  16. package/build/internal/src/middleware/ICommandFromClientType.d.ts +3 -0
  17. package/build/internal/src/middleware/IResourceSettings.d.ts +3 -0
  18. package/build/internal/src/middleware/OrganizationV.d.ts +42 -0
  19. package/build/internal/src/middleware/ResourceSettingsV.d.ts +33 -0
  20. package/build/internal/src/middleware/helpers/argument.d.ts +404 -0
  21. package/build/internal/src/middleware/helpers/commandTemplate.d.ts +129 -0
  22. package/build/internal/src/util/Disposable.d.ts +17 -0
  23. package/package.json +12 -14
  24. package/scripts/minify-snippet.ts +36 -0
  25. package/scripts/watch.ts +5 -0
  26. package/src/index.ts +11 -2
  27. package/src/init.ts +29 -88
  28. package/src/snippet.ts +57 -45
  29. package/tsconfig.json +5 -3
  30. package/webpack.config.js +8 -13
  31. package/yarn-error.log +20216 -0
  32. package/build/commandbar.d.ts +0 -67
  33. package/build/commandbar.d.ts.map +0 -1
  34. package/build/commandbar.js +0 -3
  35. package/build/commandbar.js.map +0 -1
  36. package/build/index.d.ts +0 -3
  37. package/build/index.d.ts.map +0 -1
  38. package/build/index.js +0 -27
  39. package/build/index.js.map +0 -1
  40. package/build/init.d.ts +0 -17
  41. package/build/init.d.ts.map +0 -1
  42. package/build/init.js +0 -116
  43. package/build/init.js.map +0 -1
  44. package/build/snippet.d.ts +0 -2
  45. package/build/snippet.d.ts.map +0 -1
  46. package/build/snippet.js +0 -55
  47. package/build/snippet.js.map +0 -1
  48. package/src/commandbar.ts +0 -76
@@ -0,0 +1,129 @@
1
+ /*******************************************************************************/
2
+ import * as t from 'io-ts';
3
+ /*******************************************************************************/
4
+ export declare const TemplateOptionsV: t.IntersectionC<[t.TypeC<{}>, t.PartialC<{
5
+ commandType: t.UnionC<[t.LiteralC<"independent">, t.LiteralC<"object">, t.LiteralC<"help">]>;
6
+ object: t.StringC;
7
+ hoverTooltip: t.BooleanC;
8
+ operation: t.UnionC<[t.LiteralC<"router">, t.LiteralC<"self">, t.LiteralC<"blank">, t.UndefinedC]>;
9
+ }>]>;
10
+ export declare const AdminTemplateV: t.IntersectionC<[t.TypeC<{
11
+ type: t.LiteralC<"admin">;
12
+ value: t.StringC;
13
+ }>, t.IntersectionC<[t.TypeC<{}>, t.PartialC<{
14
+ commandType: t.UnionC<[t.LiteralC<"independent">, t.LiteralC<"object">, t.LiteralC<"help">]>;
15
+ object: t.StringC;
16
+ hoverTooltip: t.BooleanC;
17
+ operation: t.UnionC<[t.LiteralC<"router">, t.LiteralC<"self">, t.LiteralC<"blank">, t.UndefinedC]>;
18
+ }>]>]>;
19
+ export declare const CallbackTemplateV: t.IntersectionC<[t.TypeC<{
20
+ type: t.LiteralC<"callback">;
21
+ value: t.StringC;
22
+ }>, t.IntersectionC<[t.TypeC<{}>, t.PartialC<{
23
+ commandType: t.UnionC<[t.LiteralC<"independent">, t.LiteralC<"object">, t.LiteralC<"help">]>;
24
+ object: t.StringC;
25
+ hoverTooltip: t.BooleanC;
26
+ operation: t.UnionC<[t.LiteralC<"router">, t.LiteralC<"self">, t.LiteralC<"blank">, t.UndefinedC]>;
27
+ }>]>]>;
28
+ export declare const LinkTemplateV: t.IntersectionC<[t.TypeC<{
29
+ type: t.LiteralC<"link">;
30
+ value: t.StringC;
31
+ }>, t.IntersectionC<[t.TypeC<{}>, t.PartialC<{
32
+ commandType: t.UnionC<[t.LiteralC<"independent">, t.LiteralC<"object">, t.LiteralC<"help">]>;
33
+ object: t.StringC;
34
+ hoverTooltip: t.BooleanC;
35
+ operation: t.UnionC<[t.LiteralC<"router">, t.LiteralC<"self">, t.LiteralC<"blank">, t.UndefinedC]>;
36
+ }>]>]>;
37
+ export declare const ClickTemplateV: t.IntersectionC<[t.TypeC<{
38
+ type: t.UnionC<[t.LiteralC<"click">, t.LiteralC<"clickBySelector">, t.LiteralC<"clickByXpath">]>;
39
+ value: t.ArrayC<t.StringC>;
40
+ }>, t.IntersectionC<[t.TypeC<{}>, t.PartialC<{
41
+ commandType: t.UnionC<[t.LiteralC<"independent">, t.LiteralC<"object">, t.LiteralC<"help">]>;
42
+ object: t.StringC;
43
+ hoverTooltip: t.BooleanC;
44
+ operation: t.UnionC<[t.LiteralC<"router">, t.LiteralC<"self">, t.LiteralC<"blank">, t.UndefinedC]>;
45
+ }>]>]>;
46
+ export declare const BuiltInTemplateV: t.IntersectionC<[t.TypeC<{
47
+ type: t.LiteralC<"builtin">;
48
+ value: t.StringC;
49
+ }>, t.IntersectionC<[t.TypeC<{}>, t.PartialC<{
50
+ commandType: t.UnionC<[t.LiteralC<"independent">, t.LiteralC<"object">, t.LiteralC<"help">]>;
51
+ object: t.StringC;
52
+ hoverTooltip: t.BooleanC;
53
+ operation: t.UnionC<[t.LiteralC<"router">, t.LiteralC<"self">, t.LiteralC<"blank">, t.UndefinedC]>;
54
+ }>]>]>;
55
+ export declare const WebhookTemplateV: t.IntersectionC<[t.TypeC<{
56
+ type: t.LiteralC<"webhook">;
57
+ value: t.StringC;
58
+ }>, t.IntersectionC<[t.TypeC<{}>, t.PartialC<{
59
+ commandType: t.UnionC<[t.LiteralC<"independent">, t.LiteralC<"object">, t.LiteralC<"help">]>;
60
+ object: t.StringC;
61
+ hoverTooltip: t.BooleanC;
62
+ operation: t.UnionC<[t.LiteralC<"router">, t.LiteralC<"self">, t.LiteralC<"blank">, t.UndefinedC]>;
63
+ }>]>]>;
64
+ export declare const ScriptTemplateV: t.IntersectionC<[t.TypeC<{
65
+ type: t.LiteralC<"script">;
66
+ value: t.StringC;
67
+ }>, t.IntersectionC<[t.TypeC<{}>, t.PartialC<{
68
+ commandType: t.UnionC<[t.LiteralC<"independent">, t.LiteralC<"object">, t.LiteralC<"help">]>;
69
+ object: t.StringC;
70
+ hoverTooltip: t.BooleanC;
71
+ operation: t.UnionC<[t.LiteralC<"router">, t.LiteralC<"self">, t.LiteralC<"blank">, t.UndefinedC]>;
72
+ }>]>]>;
73
+ export declare const TemplateV: t.UnionC<[t.IntersectionC<[t.TypeC<{
74
+ type: t.LiteralC<"admin">;
75
+ value: t.StringC;
76
+ }>, t.IntersectionC<[t.TypeC<{}>, t.PartialC<{
77
+ commandType: t.UnionC<[t.LiteralC<"independent">, t.LiteralC<"object">, t.LiteralC<"help">]>;
78
+ object: t.StringC;
79
+ hoverTooltip: t.BooleanC;
80
+ operation: t.UnionC<[t.LiteralC<"router">, t.LiteralC<"self">, t.LiteralC<"blank">, t.UndefinedC]>;
81
+ }>]>]>, t.IntersectionC<[t.TypeC<{
82
+ type: t.LiteralC<"callback">;
83
+ value: t.StringC;
84
+ }>, t.IntersectionC<[t.TypeC<{}>, t.PartialC<{
85
+ commandType: t.UnionC<[t.LiteralC<"independent">, t.LiteralC<"object">, t.LiteralC<"help">]>;
86
+ object: t.StringC;
87
+ hoverTooltip: t.BooleanC;
88
+ operation: t.UnionC<[t.LiteralC<"router">, t.LiteralC<"self">, t.LiteralC<"blank">, t.UndefinedC]>;
89
+ }>]>]>, t.IntersectionC<[t.TypeC<{
90
+ type: t.LiteralC<"link">;
91
+ value: t.StringC;
92
+ }>, t.IntersectionC<[t.TypeC<{}>, t.PartialC<{
93
+ commandType: t.UnionC<[t.LiteralC<"independent">, t.LiteralC<"object">, t.LiteralC<"help">]>;
94
+ object: t.StringC;
95
+ hoverTooltip: t.BooleanC;
96
+ operation: t.UnionC<[t.LiteralC<"router">, t.LiteralC<"self">, t.LiteralC<"blank">, t.UndefinedC]>;
97
+ }>]>]>, t.IntersectionC<[t.TypeC<{
98
+ type: t.UnionC<[t.LiteralC<"click">, t.LiteralC<"clickBySelector">, t.LiteralC<"clickByXpath">]>;
99
+ value: t.ArrayC<t.StringC>;
100
+ }>, t.IntersectionC<[t.TypeC<{}>, t.PartialC<{
101
+ commandType: t.UnionC<[t.LiteralC<"independent">, t.LiteralC<"object">, t.LiteralC<"help">]>;
102
+ object: t.StringC;
103
+ hoverTooltip: t.BooleanC;
104
+ operation: t.UnionC<[t.LiteralC<"router">, t.LiteralC<"self">, t.LiteralC<"blank">, t.UndefinedC]>;
105
+ }>]>]>, t.IntersectionC<[t.TypeC<{
106
+ type: t.LiteralC<"builtin">;
107
+ value: t.StringC;
108
+ }>, t.IntersectionC<[t.TypeC<{}>, t.PartialC<{
109
+ commandType: t.UnionC<[t.LiteralC<"independent">, t.LiteralC<"object">, t.LiteralC<"help">]>;
110
+ object: t.StringC;
111
+ hoverTooltip: t.BooleanC;
112
+ operation: t.UnionC<[t.LiteralC<"router">, t.LiteralC<"self">, t.LiteralC<"blank">, t.UndefinedC]>;
113
+ }>]>]>, t.IntersectionC<[t.TypeC<{
114
+ type: t.LiteralC<"webhook">;
115
+ value: t.StringC;
116
+ }>, t.IntersectionC<[t.TypeC<{}>, t.PartialC<{
117
+ commandType: t.UnionC<[t.LiteralC<"independent">, t.LiteralC<"object">, t.LiteralC<"help">]>;
118
+ object: t.StringC;
119
+ hoverTooltip: t.BooleanC;
120
+ operation: t.UnionC<[t.LiteralC<"router">, t.LiteralC<"self">, t.LiteralC<"blank">, t.UndefinedC]>;
121
+ }>]>]>, t.IntersectionC<[t.TypeC<{
122
+ type: t.LiteralC<"script">;
123
+ value: t.StringC;
124
+ }>, t.IntersectionC<[t.TypeC<{}>, t.PartialC<{
125
+ commandType: t.UnionC<[t.LiteralC<"independent">, t.LiteralC<"object">, t.LiteralC<"help">]>;
126
+ object: t.StringC;
127
+ hoverTooltip: t.BooleanC;
128
+ operation: t.UnionC<[t.LiteralC<"router">, t.LiteralC<"self">, t.LiteralC<"blank">, t.UndefinedC]>;
129
+ }>]>]>]>;
@@ -0,0 +1,17 @@
1
+ import { _dispose, _disposed } from '../client/symbols';
2
+ export declare type PublicDisposable = {
3
+ dispose: VoidFunction;
4
+ _disposed?: boolean;
5
+ };
6
+ export declare type PrivateDisposable = {
7
+ [_dispose]: VoidFunction;
8
+ [_disposed]?: boolean;
9
+ };
10
+ export declare type Disposable = undefined | PublicDisposable | PrivateDisposable;
11
+ export declare const isDisposable: (x: unknown) => x is Disposable;
12
+ /**
13
+ * Determines if the provided value is "disposed". A value is considered "disposed" if it is an object with the
14
+ * _disposed property or symbol symbol set to true, or if the actual value is undefined.
15
+ */
16
+ export declare const isDisposed: (x: unknown) => boolean;
17
+ export declare function dispose(x: Disposable): void;
package/package.json CHANGED
@@ -1,27 +1,25 @@
1
1
  {
2
2
  "name": "commandbar",
3
- "version": "1.3.1",
3
+ "version": "1.4.1",
4
4
  "description": "Javascript Utility for CommandBar",
5
- "main": "build/index.js",
6
- "types": "build/index.d.js",
5
+ "main": "build/commandbar-js/src/index.js",
6
+ "types": "build/commandbar-js/src/index.d.ts",
7
7
  "sideEffects": false,
8
8
  "scripts": {
9
9
  "clean": "rimraf build",
10
- "build": "tsc -p tsconfig.json",
10
+ "build": "webpack build && tsc",
11
+ "minify-snippet": "ts-node scripts/minify-snippet.ts",
11
12
  "prepublishOnly": "yarn run clean && yarn run build",
12
13
  "test": "jest --runInBand",
13
- "test:watch": "jest --watch --verbose false --silent false"
14
+ "test:watch": "jest --watch --verbose false --silent false",
15
+ "watch": "scripts/watch.ts"
14
16
  },
15
17
  "keywords": [],
16
18
  "author": "",
17
19
  "license": "ISC",
18
- "dependencies": {},
19
- "devDependencies": {
20
- "@types/jest": "^26.0.22",
21
- "jest": "^26.6.3",
22
- "rimraf": "^3.0.2",
23
- "ts-jest": "^26.5.4",
24
- "ts-node": "^9.1.1",
25
- "typescript": "^3.9"
26
- }
20
+ "dependencies": {
21
+ "fp-ts": "2.10.5",
22
+ "io-ts": "2.2.13"
23
+ },
24
+ "devDependencies": {}
27
25
  }
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ts-node
2
+
3
+ import * as ts from 'typescript';
4
+ import { snippet } from '../src/snippet';
5
+ import { minify } from 'terser';
6
+
7
+ const result = ts.transpileModule(snippet.toString(), {
8
+ compilerOptions: {
9
+ alwaysStrict: false,
10
+ declaration: false,
11
+ lib: ['DOM', 'ES2015'],
12
+ module: ts.ModuleKind.None,
13
+ noEmitOnError: true,
14
+ removeComments: true,
15
+ sourceMap: false,
16
+ strict: true,
17
+ target: ts.ScriptTarget.ES5,
18
+ },
19
+ });
20
+
21
+ if (result.diagnostics && result.diagnostics.length > 0) {
22
+ result.diagnostics?.forEach((d) => console.error(d.messageText));
23
+ process.exit(1);
24
+ }
25
+
26
+ const lines = result.outputText.split('\n');
27
+ const transpiled = ["var org = '<org_id>';"].concat(lines.slice(1, lines.length - 2)).join('\n');
28
+
29
+ minify(transpiled, { mangle: { toplevel: true } })
30
+ .then((result) => {
31
+ console.log(result.code);
32
+ })
33
+ .catch((err) => {
34
+ console.error(err);
35
+ process.exit(1);
36
+ });
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ts-node
2
+
3
+ import orchestrate from '../../scripts/util/orchestrate';
4
+
5
+ orchestrate([['webpack watch', 'tsc -w']], { killOthers: ['failure', 'success'] });
package/src/index.ts CHANGED
@@ -1,3 +1,12 @@
1
- import init, { initProxy, presentSnippet } from './init';
1
+ import { CommandBarClientSDK } from '../../internal/src/client/CommandBarClientSDK';
2
2
 
3
- export { init, initProxy, presentSnippet };
3
+ export { default as init } from './init';
4
+ export { initProxySDK as initProxy } from '../../internal/src/client/proxy';
5
+ export { snippet } from './snippet';
6
+ export { CommandBarClientSDK };
7
+
8
+ declare global {
9
+ interface Window {
10
+ CommandBar: CommandBarClientSDK;
11
+ }
12
+ }
package/src/init.ts CHANGED
@@ -1,67 +1,14 @@
1
- import { snippet } from './snippet';
2
- import { CommandBarClientAPI } from './commandbar';
3
-
4
- declare global {
5
- interface String {
6
- interpolate(templates: { [k: string]: string }): string;
7
- }
8
- }
9
-
10
- // eslint-disable-next-line no-extend-native
11
- String.prototype.interpolate = function (params) {
12
- const names = Object.keys(params);
13
- const vals = Object.values(params);
14
- // eslint-disable-next-line no-new-func
15
- return new Function(...names, `return \`${this}\`;`)(...vals);
16
- };
17
-
18
- const prepareSnippet = (snippetString: string, templates: { [k: string]: string }) => {
19
- let s = snippetString.interpolate(templates);
20
- s = '!(' + s + ')();';
21
- return s;
22
- };
1
+ import 'proxy-polyfill';
2
+ import 'es6-object-assign/auto';
3
+ import 'es6-symbol/implement';
4
+ import { getProxySDK } from '../../internal/src/client/proxy';
5
+ import { _configuration } from '../../internal/src/client/symbols';
23
6
 
24
7
  interface IInitOptions {
25
8
  debug?: boolean;
26
- version?: string;
27
9
  }
28
10
 
29
- type CommandBarProxyApi = Partial<CommandBarClientAPI> & {
30
- q: unknown[];
31
- };
32
-
33
- // eslint-disable-next-line @typescript-eslint/ban-types
34
- const hasProp = <T extends object>(target: T, prop: string | number | symbol): prop is keyof T => prop in target;
35
-
36
- export const initProxy = () => {
37
- if (window.CommandBar === undefined || window.CommandBar._disposed === true) {
38
- // @ts-expect-error: Temporarily ignore CommandBarProxy definition
39
- let existingQueue = Array.isArray(window.CommandBar?.q) ? [...window.CommandBar.q] : [];
40
- existingQueue = existingQueue.filter((item) => {
41
- return item.length > 0 && item[0] !== 'dispose';
42
- });
43
-
44
- const cb: CommandBarProxyApi = {
45
- q: existingQueue,
46
- };
47
-
48
- window.CommandBar = new Proxy(cb, {
49
- get: function (_, prop) {
50
- if (hasProp(cb, prop)) return cb[prop];
51
- return function () {
52
- // eslint-disable-next-line prefer-rest-params
53
- const a = Array.prototype.slice.call(arguments);
54
- a.unshift(prop);
55
- cb.q.push(a);
56
- };
57
- },
58
- }) as CommandBarClientAPI;
59
- }
60
-
61
- return window.CommandBar;
62
- };
63
-
64
- const getSrc = (org: string, version?: string) => {
11
+ const getSrc = (org: string) => {
65
12
  let origin = 'https://api.commandbar.com';
66
13
  const params = [];
67
14
 
@@ -69,6 +16,8 @@ const getSrc = (org: string, version?: string) => {
69
16
  if (lc && lc.includes('local')) {
70
17
  // for debugging purposes
71
18
  origin = 'http://localhost:8000';
19
+ } else if (!lc && org === '10d7dc04') {
20
+ origin = 'https://api-cu.commandbar.com';
72
21
  }
73
22
 
74
23
  let src = origin + '/latest/' + org;
@@ -77,18 +26,20 @@ const getSrc = (org: string, version?: string) => {
77
26
  params.push('lc=' + lc);
78
27
  }
79
28
 
80
- if (!!version) {
81
- params.push('v=' + version);
82
- }
29
+ const version = 2;
30
+ params.push('version=' + version);
83
31
 
84
32
  src = src + '?' + params.join('&');
85
33
  return src;
86
34
  };
87
35
 
88
- // eslint-disable-next-line @typescript-eslint/no-empty-function
89
- let loadOrg = () => {};
90
-
91
36
  const loadLatest = (org: string) => {
37
+ if (document.readyState !== 'complete') {
38
+ // NOTE: `once` is not supported by IE11 and this will cause a (minor!) memory leak.
39
+ window.addEventListener('load', loadLatest.bind(null, org), { capture: false, once: true });
40
+ return;
41
+ }
42
+
92
43
  const src = getSrc(org);
93
44
 
94
45
  const el = document.createElement('script');
@@ -96,12 +47,19 @@ const loadLatest = (org: string) => {
96
47
  el.async = true;
97
48
  el.src = src;
98
49
  document.head.appendChild(el);
99
-
100
- window.removeEventListener('load', loadOrg, false);
101
50
  };
102
51
 
103
- const DEFAULT_OPTS = { debug: false, version: '' };
104
-
52
+ const DEFAULT_OPTS = { debug: false };
53
+
54
+ /**
55
+ * Initializes CommandBar and injects the scripts required to load it into the <head> of the document. After calling
56
+ * this function it will be possible to access window.CommandBar and call the SDK methods, even if the scripts have not
57
+ * finished loading yet.
58
+ *
59
+ * @param {string} org Your organization ID, which can be found [here](https://app.commandbar.com/docs/getting-started/installation#option-b:-use-npm-or-yarn).
60
+ * @param {IInitOptions} [opts] Additional options to use for initialization: `debug` will enable additional logging
61
+ * output.
62
+ */
105
63
  const init = (org: string, opts: IInitOptions = DEFAULT_OPTS) => {
106
64
  const config = { ...DEFAULT_OPTS, ...opts };
107
65
 
@@ -112,25 +70,8 @@ const init = (org: string, opts: IInitOptions = DEFAULT_OPTS) => {
112
70
 
113
71
  config.debug && console.log('CommandBar init...');
114
72
 
115
- initProxy();
116
-
117
- loadOrg = () => loadLatest(org);
118
-
119
- window.addEventListener('load', loadOrg, false);
120
- };
121
-
122
- export const presentSnippet = (org: string) => {
123
- const snippetString = prepareSnippet(snippet.toString(), { org, version: '' });
124
- const trimmedSnippet = snippetString
125
- .split('\n')
126
- .map((line: string) => {
127
- return line.trim();
128
- })
129
- .join('');
130
-
131
- return `<script>
132
- ${trimmedSnippet}
133
- </script>`;
73
+ getProxySDK()[_configuration] = { uuid: org };
74
+ loadLatest(org);
134
75
  };
135
76
 
136
77
  export default init;
package/src/snippet.ts CHANGED
@@ -1,57 +1,69 @@
1
- /* eslint-disable prefer-rest-params */
2
1
  /* eslint-disable no-template-curly-in-string */
3
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
4
- // @ts-nocheck
5
- export const snippet = () => {
6
- const org = '${org}';
7
- const cb = [];
8
- cb.q = [];
9
- window.CommandBar = new Proxy(cb, {
10
- get: function (f, n) {
11
- if (n in f) {
12
- return f[n];
13
- }
14
- return function () {
15
- const a = Array.prototype.slice.call(arguments);
16
- a.unshift(n);
17
- cb.q.push(a);
18
- };
19
- },
20
- });
21
-
22
- const load = function () {
23
- let origin = 'https://api.commandbar.com';
24
- const params = [];
2
+ /* eslint-disable prefer-rest-params */
3
+ /* eslint-disable @typescript-eslint/no-inferrable-types */
4
+ export const snippet = (org: string) => {
5
+ const polyfills = ['Object.assign', 'Symbol', 'Symbol.for'].join('%2C');
6
+ const w = window as unknown as Record<string, unknown>;
25
7
 
26
- const lc = localStorage.getItem('commandbar.lc');
27
- if (lc && lc.includes('local')) {
28
- // for debugging purposes
29
- origin = 'http://localhost:8000';
8
+ function loadScript(src: string, async = false) {
9
+ if (document.readyState !== 'complete') {
10
+ window.addEventListener('load', loadScript.bind(null, src, async), { capture: false, once: true });
30
11
  }
12
+ const el = document.createElement('script');
13
+ el.type = 'text/javascript';
14
+ el.async = async;
15
+ el.src = src;
16
+ document.head.appendChild(el);
17
+ }
31
18
 
32
- let src = origin + '/latest/' + org;
19
+ function bootstrap() {
20
+ if (w.CommandBar !== undefined) return;
21
+ delete w.__CommandBarBootstrap__;
33
22
 
34
- if (lc) {
35
- params.push('lc=' + lc);
36
- }
23
+ const _configuration = Symbol.for('CommandBar::configuration');
24
+ const _orgConfig = Symbol.for('CommandBar::orgConfig');
25
+ const _disposed = Symbol.for('CommandBar::disposed');
26
+ const _isProxy = Symbol.for('CommandBar::isProxy');
27
+ const _queue = Symbol.for('CommandBar::queue');
28
+ const _unwrap = Symbol.for('CommandBar::unwrap');
29
+ const params = [];
30
+ const lc = localStorage.getItem('commandbar.lc');
31
+ const origin = lc && lc.includes('local') ? 'http://localhost:8000' : 'https://api.commandbar.com';
37
32
 
38
- const v = '${version}';
39
- if (!!v) {
40
- params.push('v=' + v);
41
- }
33
+ const proxy = Object.assign(
34
+ {
35
+ [_configuration]: { uuid: org },
36
+ [_orgConfig]: {},
37
+ [_disposed]: false,
38
+ [_isProxy]: true,
39
+ [_queue]: new Array<unknown>(),
40
+ [_unwrap]: () => proxy,
41
+ },
42
+ w.CommandBar,
43
+ );
44
+ w.CommandBar = new Proxy(proxy, {
45
+ get: function (_, prop: keyof typeof proxy) {
46
+ if (prop in proxy) return proxy[prop];
47
+ return function () {
48
+ const a = Array.prototype.slice.call(arguments);
49
+ a.unshift(prop);
50
+ (proxy[_queue] as Array<unknown>).push(a);
51
+ };
52
+ },
53
+ });
42
54
 
43
- src = src + '?' + params.join('&');
55
+ if (lc !== null) params.push(`lc=${lc}`);
56
+ params.push('version=2');
44
57
 
45
- const el = document.createElement('script');
46
- el.type = 'text/javascript';
47
- el.async = true;
48
- el.src = src;
49
- document.head.appendChild(el);
50
- };
58
+ loadScript(`${origin}/latest/${org}?${params.join('&')}`, true);
59
+ }
51
60
 
52
- if (window.attachEvent) {
53
- window.attachEvent('onload', load);
61
+ if (typeof Object.assign === 'undefined' || typeof Symbol === 'undefined' || typeof Symbol.for === 'undefined') {
62
+ w.__CommandBarBootstrap__ = bootstrap;
63
+ loadScript(
64
+ 'https://polyfill.io/v3/polyfill.min.js?version=3.101.0&callback=__CommandBarBootstrap__&features=' + polyfills,
65
+ );
54
66
  } else {
55
- window.addEventListener('load', load, false);
67
+ bootstrap();
56
68
  }
57
69
  };
package/tsconfig.json CHANGED
@@ -2,10 +2,11 @@
2
2
  "include": ["./src"],
3
3
  "compilerOptions": {
4
4
  "declaration": true,
5
- "declarationMap": true,
5
+ "declarationMap": false,
6
+ "emitDeclarationOnly": true,
6
7
  "module": "CommonJS",
7
8
  "outDir": "build",
8
- "rootDir": "src",
9
+ "rootDir": "..",
9
10
  "sourceMap": true,
10
11
  "target": "es5",
11
12
  "lib": ["dom", "esnext"],
@@ -17,6 +18,7 @@
17
18
 
18
19
  "forceConsistentCasingInFileNames": true,
19
20
  "stripInternal": true, // https://www.typescriptlang.org/tsconfig#stripInternal
20
- "types": []
21
+ "types": ["node"],
22
+ "jsx": "react"
21
23
  }
22
24
  }
package/webpack.config.js CHANGED
@@ -1,12 +1,11 @@
1
- const webpack = require('webpack')
2
- const path = require('path')
1
+ const path = require('path');
3
2
 
4
3
  module.exports = {
5
4
  mode: 'production',
6
- entry: './src/index.ts',
5
+ entry: path.resolve(__dirname, 'src', 'index.ts'),
7
6
  output: {
8
- path: path.resolve(__dirname, 'dist/umd'),
9
- filename: 'commandbar.js',
7
+ path: path.resolve(__dirname, 'build', 'commandbar-js', 'src'),
8
+ filename: 'index.js',
10
9
  library: {
11
10
  type: 'umd',
12
11
  name: 'commandbar',
@@ -15,7 +14,7 @@ module.exports = {
15
14
  module: {
16
15
  rules: [
17
16
  {
18
- test: /\.ts$/,
17
+ test: /\.tsx?$/,
19
18
  loader: 'ts-loader',
20
19
  options: {
21
20
  transpileOnly: true,
@@ -24,11 +23,7 @@ module.exports = {
24
23
  ],
25
24
  },
26
25
  resolve: {
27
- extensions: ['.ts', '.js', '.json'],
26
+ extensions: ['.ts', '.js', '.json', '.tsx'],
28
27
  },
29
- plugins: [
30
- new webpack.DefinePlugin({
31
- process: 'process/browser',
32
- }),
33
- ],
34
- }
28
+ stats: 'minimal',
29
+ };