@buddy-technology/offer-component 0.3.2 → 1.0.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.
@@ -0,0 +1,12 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "include": [
4
+ "**/*.ts",
5
+ "**/*.js",
6
+ "**/*.tsx",
7
+ "**/*.jsx",
8
+ ".eslintrc.js",
9
+ "release.config.js",
10
+ "setupTests.ts",
11
+ ],
12
+ }
package/API.md DELETED
@@ -1,174 +0,0 @@
1
- ## Functions
2
-
3
- <dl>
4
- <dt><a href="#BuddyOfferElement">BuddyOfferElement(options)</a> ⇒ <code><a href="#BuddyOfferElementProps">FunctionComponent.&lt;BuddyOfferElementProps&gt;</a></code></dt>
5
- <dd></dd>
6
- </dl>
7
-
8
- ## Typedefs
9
-
10
- <dl>
11
- <dt><a href="#DataObject">DataObject</a> : <code>Object</code></dt>
12
- <dd></dd>
13
- <dt><a href="#AddToCartFunction">AddToCartFunction</a> ⇒ <code><a href="#DataObject">DataObject</a></code></dt>
14
- <dd><p>A callback to be called with a payload object when users opt into the offer.</p>
15
- </dd>
16
- <dt><a href="#RemoveFromCartFunction">RemoveFromCartFunction</a> ⇒ <code><a href="#DataObject">DataObject</a></code></dt>
17
- <dd><p>A callback to be called with a payload object when users opt out of the offer.</p>
18
- </dd>
19
- <dt><a href="#OverridesObject">OverridesObject</a> : <code>Object</code></dt>
20
- <dd></dd>
21
- <dt><a href="#ThemeObject">ThemeObject</a> : <code>Object</code></dt>
22
- <dd></dd>
23
- <dt><a href="#LogoOverride">LogoOverride</a> : <code>Object</code></dt>
24
- <dd></dd>
25
- <dt><a href="#EventObject">EventObject</a> : <code>Object</code></dt>
26
- <dd></dd>
27
- <dt><a href="#OnUserEventCallback">OnUserEventCallback</a> : <code>function</code></dt>
28
- <dd></dd>
29
- <dt><a href="#BuddyOfferElementProps">BuddyOfferElementProps</a> : <code>Object</code></dt>
30
- <dd></dd>
31
- </dl>
32
-
33
- <a name="BuddyOfferElement"></a>
34
-
35
- ## BuddyOfferElement(options) ⇒ [<code>FunctionComponent.&lt;BuddyOfferElementProps&gt;</code>](#BuddyOfferElementProps)
36
- **Kind**: global function
37
-
38
- | Param | Type |
39
- | --- | --- |
40
- | options | [<code>BuddyOfferElementProps</code>](#BuddyOfferElementProps) |
41
-
42
- <a name="DataObject"></a>
43
-
44
- ## DataObject : <code>Object</code>
45
- **Kind**: global typedef
46
- **Properties**
47
-
48
- | Name | Type | Description |
49
- | --- | --- | --- |
50
- | [customer] | <code>Object</code> | customer data |
51
- | [policy] | <code>Object</code> | policy data |
52
-
53
- <a name="AddToCartFunction"></a>
54
-
55
- ## AddToCartFunction ⇒ [<code>DataObject</code>](#DataObject)
56
- A callback to be called with a payload object when users opt into the offer.
57
-
58
- **Kind**: global typedef
59
- **Returns**: [<code>DataObject</code>](#DataObject) - the policy payload
60
- <a name="RemoveFromCartFunction"></a>
61
-
62
- ## RemoveFromCartFunction ⇒ [<code>DataObject</code>](#DataObject)
63
- A callback to be called with a payload object when users opt out of the offer.
64
-
65
- **Kind**: global typedef
66
- **Returns**: [<code>DataObject</code>](#DataObject) - the policy payload
67
- <a name="OverridesObject"></a>
68
-
69
- ## OverridesObject : <code>Object</code>
70
- **Kind**: global typedef
71
- **Properties**
72
-
73
- | Name | Type | Description |
74
- | --- | --- | --- |
75
- | [webFonts] | <code>Array.&lt;String&gt;</code> | array of url strings linking to web fonts. |
76
- | [styles] | <code>Object</code> | Object Styles object for overriding any css. |
77
- | [colors] | <code>Object</code> | Object for overriding individual color options. |
78
-
79
- <a name="ThemeObject"></a>
80
-
81
- ## ThemeObject : <code>Object</code>
82
- **Kind**: global typedef
83
- **Properties**
84
-
85
- | Name | Type | Default | Description |
86
- | --- | --- | --- | --- |
87
- | [baseTheme] | <code>String</code> | <code>&#x27;base&#x27;</code> | The base theme to style with. Can be used simply as a started and overwritten. |
88
- | [palette] | <code>String</code> | <code>&#x27;base&#x27;</code> | The base color scheme to style with. Can be used simply as a started and overwritten. |
89
- | [overrides] | [<code>OverridesObject</code>](#OverridesObject) | | |
90
-
91
- **Example**
92
- ```js
93
- const theme = {
94
- baseTheme: 'base',
95
- palette: 'buddy',
96
- overrides: {
97
- webFonts: [
98
- 'https://fonts.googleapis.com/css2?family=Rubik:wght@700&display=swap',
99
- ],
100
- styles: {
101
- h1: {
102
- color: 'var(--color-text-primary)',
103
- fontFamily: 'Rubik, sans-serif',
104
- fontSize: '2rem',
105
- '@media (min-width: 992px)': {
106
- fontSize: '3rem',
107
- },
108
- '&:hover': {
109
- boxShadow: 'none',
110
- backgroundColor: '#FBF9EF',
111
- },
112
- },
113
- body: { color: '#0A242D' },
114
- '.input-text': {
115
- border: 'none',
116
- color: '#333333',
117
- },
118
- },
119
- colors: { textPrimary: '#0A242D' },
120
- },
121
- };
122
- ```
123
- <a name="LogoOverride"></a>
124
-
125
- ## LogoOverride : <code>Object</code>
126
- **Kind**: global typedef
127
- **Properties**
128
-
129
- | Name | Type | Description |
130
- | --- | --- | --- |
131
- | url | <code>String</code> | destination url when users click the trust badge |
132
- | src | <code>String</code> | src of the img for displaying the trust badge |
133
- | [alt] | <code>String</code> | alt text for trust badge image |
134
-
135
- <a name="EventObject"></a>
136
-
137
- ## EventObject : <code>Object</code>
138
- **Kind**: global typedef
139
-
140
- | Param | Type | Description |
141
- | --- | --- | --- |
142
- | eventType | <code>string</code> | the type of user event (eg:'onCheckout') |
143
- | data | <code>object</code> | data object related to the user event. |
144
- | data.timestamp | <code>object</code> | timestamp of the event. All events have a timestamp property. |
145
-
146
- <a name="OnUserEventCallback"></a>
147
-
148
- ## OnUserEventCallback : <code>function</code>
149
- **Kind**: global typedef
150
-
151
- | Param | Type | Description |
152
- | --- | --- | --- |
153
- | type | [<code>EventObject</code>](#EventObject) | the type of user event (eg:'onCheckout') |
154
-
155
- <a name="BuddyOfferElementProps"></a>
156
-
157
- ## BuddyOfferElementProps : <code>Object</code>
158
- **Kind**: global typedef
159
- **Properties**
160
-
161
- | Name | Type | Default | Description |
162
- | --- | --- | --- | --- |
163
- | ion | <code>String</code> | | The ion id for the offering. |
164
- | partnerID | <code>String</code> | | The partner ID required for instantiating the Offer |
165
- | [stage] | <code>String</code> | <code>&quot;STAGING&quot;</code> | toggle's the environment for the Offer Component. Defaults to STAGING. Must be set to "PRODUCTION" before going live. |
166
- | [viewType] | <code>String</code> | <code>&quot;paginated&quot;</code> | establishes how the offer should display to the user. One of: 'paginated', 'single-form' or 'offer-only.' |
167
- | [theme] | [<code>ThemeObject</code>](#ThemeObject) | | theming object for customizing offer component's styles |
168
- | [data] | [<code>DataObject</code>](#DataObject) | | Any customer or policy data to pre-fill the offer with. Refer to your individual ION for data structure. |
169
- | [onUserEvent] | [<code>OnUserEventCallback</code>](#OnUserEventCallback) | | callback function for tracking user behavioral data. Triggers on user interactions such as input focus/blur, in-app navigation, etc. Refer to the docs for more details. |
170
- | [onAddToCart] | [<code>AddToCartFunction</code>](#AddToCartFunction) | | callback function triggered when users opt into an offer-only offer. |
171
- | [onRemoveFromCart] | [<code>RemoveFromCartFunction</code>](#RemoveFromCartFunction) | | callback function triggered when users opt out of an offer-only offer. |
172
- | [includeCheckout] | <code>boolean</code> | | toggles whether or not to display the card capture checkout view. Defaults to true. When false, an AddToCart callback must be provided. |
173
- | [logoOverride] | [<code>LogoOverride</code>](#LogoOverride) | | object for overriding Buddy's trust badge. |
174
-
package/dist/util.js DELETED
@@ -1,252 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.updateOffer = exports.loadScript = exports.findScript = void 0;
7
-
8
- require("core-js/modules/es.array.includes.js");
9
-
10
- require("core-js/modules/es.string.trim.js");
11
-
12
- require("core-js/modules/es.array.reduce.js");
13
-
14
- require("core-js/modules/es.regexp.exec.js");
15
-
16
- require("core-js/modules/es.regexp.test.js");
17
-
18
- require("core-js/modules/es.promise.js");
19
-
20
- require("core-js/modules/es.string.includes.js");
21
-
22
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
23
-
24
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
25
-
26
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
27
-
28
- const EXISTING_SCRIPT_MESSAGE = 'Buddy Offer Script is already loaded.';
29
- const SCRIPTS = {
30
- LOCAL: {
31
- URL: 'http://localhost:8008/index.js',
32
- REGEX: /^http:\/\/localhost:8008\/index\.js\/?(\?.*)?$/
33
- },
34
- // TODO: Delete this in favor of "testing", once we're sure nothing is using it.
35
- DEVELOPMENT: {
36
- URL: 'https://js.buddy.insure/v2/dev/index.js',
37
- REGEX: /^https:\/\/js\.buddy\.insure\/v2\/dev\/index\.js\/?(\?.*)?$/
38
- },
39
- TESTING: {
40
- URL: 'https://js.buddy.insure/v2/testing/index.js',
41
- REGEX: /^https:\/\/js\.buddy\.insure\/v2\/testing\/index\.js\/?(\?.*)?$/
42
- },
43
- STAGING: {
44
- URL: 'https://js.buddy.insure/v2/staging/index.js',
45
- REGEX: /^https:\/\/js\.buddy\.insure\/v2\/staging\/index\.js\/?(\?.*)?$/
46
- },
47
- PRODUCTION: {
48
- URL: 'https://js.buddy.insure/v2/index.js',
49
- REGEX: /^https:\/\/js\.buddy\.insure\/v2\/index\.js\/?(\?.*)?$/
50
- }
51
- };
52
- const STAGES = Object.keys(SCRIPTS);
53
- /**
54
- * @type {Object}
55
- * @property {string} stage
56
- */
57
-
58
- const defaultOptions = {
59
- stage: 'STAGING'
60
- };
61
- /**
62
- * @function isEmptyOrNil
63
- * @param {*} value
64
- * @returns {Boolean}
65
- */
66
-
67
- const isEmptyOrNil = value => {
68
- const isNil = val => [undefined, null].includes(val);
69
-
70
- const isEmptyStr = str => typeof str === 'string' && str.trim().length === 0;
71
-
72
- const isEmptyObjOrArr = obj => typeof obj === 'object' && obj.length === undefined && Object.keys(obj).length === 0 || Array.isArray(obj) && obj.length === 0;
73
-
74
- return isNil(value) || isEmptyStr(value) || isEmptyObjOrArr(value);
75
- };
76
- /**
77
- * @param {Array<String>} propsToCheck
78
- * @param {Object} obj
79
- * @returns {(null | String)}
80
- */
81
-
82
-
83
- const validatePropertiesOfObject = (propsToCheck, obj) => {
84
- const emptyProps = propsToCheck.filter(prop => isEmptyOrNil(obj[prop]));
85
-
86
- if (emptyProps.length) {
87
- const displayMissingProps = emptyProps.reduce((prev, current, i) => {
88
- const isLastEl = i === emptyProps.length - 1;
89
- return isLastEl ? "".concat(prev).concat(current, ".") : "".concat(prev).concat(current, ", ");
90
- }, '');
91
- return "The following props are required: ".concat(displayMissingProps);
92
- }
93
-
94
- return null;
95
- };
96
- /**
97
- * @function findScript
98
- * @param {String} stage
99
- * @returns {(HTMLElement | undefined)}
100
- */
101
-
102
-
103
- const findScript = function findScript() {
104
- let stage = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultOptions.stage;
105
- const {
106
- URL,
107
- REGEX
108
- } = SCRIPTS[stage];
109
- const scripts = Array.from(document.querySelectorAll("script[src^=\"".concat(URL, "\"]")));
110
- const matchedScript = scripts.find(_ref => {
111
- let {
112
- src
113
- } = _ref;
114
- return REGEX.test(src);
115
- });
116
- return matchedScript;
117
- };
118
- /**
119
- * @function updateOffer
120
- * @param {Object} options
121
- */
122
-
123
-
124
- exports.findScript = findScript;
125
-
126
- const updateOffer = options => {
127
- if (window.Buddy) {
128
- try {
129
- window.Buddy.updateOffer(options);
130
- } catch (error) {
131
- // eslint-disable-next-line no-console
132
- console.error(error);
133
- }
134
- }
135
- };
136
- /**
137
- * @function createOffer
138
- * @param {Object} options
139
- */
140
-
141
-
142
- exports.updateOffer = updateOffer;
143
-
144
- const createOffer = options => {
145
- if (window.Buddy) {
146
- try {
147
- window.Buddy.createOffer(options);
148
- } catch (error) {
149
- // eslint-disable-next-line no-console
150
- console.error(error);
151
- }
152
- }
153
- };
154
- /**
155
- * @function injectScript
156
- * @param {Object} options
157
- * @returns {HTMLElement}
158
- */
159
-
160
-
161
- const injectScript = options => {
162
- const finalOptions = _objectSpread(_objectSpread({}, defaultOptions), options);
163
-
164
- const script = document.createElement('script');
165
- script.src = "".concat(SCRIPTS[finalOptions.stage].URL);
166
- document.body.appendChild(script);
167
-
168
- script.onload = () => createOffer(finalOptions);
169
-
170
- return script;
171
- };
172
- /**
173
- * @type {(Promise | null)}
174
- */
175
-
176
-
177
- let scriptPromise = null;
178
- /**
179
- * @function loadScript
180
- * @param {Object} options
181
- * @returns {Promise}
182
- */
183
-
184
- const loadScript = options => {
185
- // Ensure that we only attempt to script once
186
- if (scriptPromise !== null) {
187
- return scriptPromise;
188
- }
189
-
190
- scriptPromise = new Promise((resolve, reject) => {
191
- if (typeof window === 'undefined') {
192
- // Resolve to null when imported server side. This makes the module
193
- // safe to import in an isomorphic code base.
194
- resolve(null);
195
- return;
196
- }
197
-
198
- if (window.Buddy) {
199
- // eslint-disable-next-line no-console
200
- console.warn(EXISTING_SCRIPT_MESSAGE);
201
- resolve(window.Buddy);
202
- return;
203
- }
204
-
205
- try {
206
- const requiredProps = ['ion', 'partnerID'];
207
- const missingRequiredProps = validatePropertiesOfObject(requiredProps, options);
208
-
209
- if (missingRequiredProps) {
210
- reject(new Error(missingRequiredProps));
211
- return;
212
- }
213
-
214
- if (!(options !== null && options !== void 0 && options.stage)) {
215
- // eslint-disable-next-line no-console
216
- console.warn("No stage passed to BuddyOfferElement. Using default stage: ".concat(defaultOptions.stage));
217
- } // If stage is passed, ensure it is one of the prescribed options.
218
-
219
-
220
- if (options !== null && options !== void 0 && options.stage && !STAGES.includes(options.stage)) {
221
- const msg = "The stage prop must be one of: [".concat(STAGES.join(' , '), "], but received \"").concat(options.stage, ".\"");
222
- reject(new Error(msg));
223
- return;
224
- }
225
-
226
- let script = findScript(options === null || options === void 0 ? void 0 : options.stage);
227
-
228
- if (script) {
229
- // eslint-disable-next-line no-console
230
- console.warn(EXISTING_SCRIPT_MESSAGE);
231
- } else if (!script) {
232
- script = injectScript(options);
233
- }
234
-
235
- script.addEventListener('load', () => {
236
- if (window.Buddy) {
237
- resolve(window.Buddy);
238
- } else {
239
- reject(new Error('Buddy Offer Element not available'));
240
- }
241
- });
242
- script.addEventListener('error', () => {
243
- reject(new Error('Failed to load Buddy Offer Element'));
244
- });
245
- } catch (error) {
246
- reject(error);
247
- }
248
- });
249
- return scriptPromise;
250
- };
251
-
252
- exports.loadScript = loadScript;
package/lib/types.d.ts DELETED
@@ -1,144 +0,0 @@
1
- export = BuddyOfferElement
2
-
3
- /**
4
- * @property [customer] - customer data
5
- * @property [policy] - policy data
6
- */
7
- declare type DataObject = {
8
- customer?: any;
9
- policy?: any;
10
- };
11
-
12
- /**
13
- * A callback to be called with a payload object when users opt into the offer.
14
- */
15
- declare type AddToCartFunction = () => DataObject;
16
-
17
- /**
18
- * A callback to be called with a payload object when users opt out of the offer.
19
- */
20
- declare type RemoveFromCartFunction = () => DataObject;
21
-
22
- /**
23
- * @property [webFonts] - array of url strings linking to web fonts.
24
- * @property [styles] - Object Styles object for overriding any css.
25
- * @property [colors] - Object for overriding individual color options.
26
- */
27
- declare type OverridesObject = {
28
- webFonts?: String[];
29
- styles?: any;
30
- colors?: any;
31
- };
32
-
33
- /**
34
- * @example
35
- * const theme = {
36
- * baseTheme: 'base',
37
- * palette: 'buddy',
38
- * overrides: {
39
- * webFonts: [
40
- * 'https://fonts.googleapis.com/css2?family=Rubik:wght@700&display=swap',
41
- * ],
42
- * styles: {
43
- * h1: {
44
- * color: 'var(--color-text-primary)',
45
- * fontFamily: 'Rubik, sans-serif',
46
- * fontSize: '2rem',
47
- * '@media (min-width: 992px)': {
48
- * fontSize: '3rem',
49
- * },
50
- * '&:hover': {
51
- * boxShadow: 'none',
52
- * backgroundColor: '#FBF9EF',
53
- * },
54
- * },
55
- * body: { color: '#0A242D' },
56
- * '.input-text': {
57
- * border: 'none',
58
- * color: '#333333',
59
- * },
60
- * },
61
- * colors: { textPrimary: '#0A242D' },
62
- * },
63
- * };
64
- * @property [baseTheme = 'base'] - The base theme to style with. Can be used simply as a started and overwritten.
65
- * @property [palette = 'base'] - The base color scheme to style with. Can be used simply as a started and overwritten.
66
- */
67
- declare type ThemeObject = {
68
- baseTheme?: string;
69
- palette?: string;
70
- overrides?: OverridesObject;
71
- };
72
-
73
- /**
74
- * @property url - destination url when users click the trust badge
75
- * @property src - src of the img for displaying the trust badge
76
- * @property [alt] - alt text for trust badge image
77
- */
78
- declare type LogoOverride = {
79
- url: string;
80
- src: string;
81
- alt?: string;
82
- };
83
-
84
- /**
85
- * @param eventType - the type of user event (eg:'onCheckout')
86
- * @param data - data object related to the user event.
87
- * @param data.timestamp - timestamp of the event. All events have a timestamp property.
88
- */
89
- declare type EventObject = any;
90
-
91
- /**
92
- * @param type - the type of user event (eg:'onCheckout')
93
- */
94
- declare type OnUserEventCallback = (type: EventObject) => void;
95
-
96
- /**
97
- * @property ion - The ion id for the offering.
98
- * @property partnerID - The partner ID required for instantiating the Offer
99
- * @property [stage = STAGING] - toggle's the environment for the Offer Component. Defaults to STAGING. Must be set to PRODUCTION before going live.
100
- * @property [viewType = paginated] - establishes how the offer should display to the user. One of: 'paginated', 'single-form' or 'offer-only.'
101
- * @property [theme] - theming object for customizing offer component's styles
102
- * @property [data] - Any customer or policy data to pre-fill the offer with. Refer to your individual ION for data structure.
103
- * @property [onUserEvent] - callback function for tracking user behavioral data. Triggers on user interactions such as input focus/blur, in-app navigation, etc. Refer to the docs for more details.
104
- * @property [onAddToCart] - callback function triggered when users opt into an offer-only offer.
105
- * @property [onRemoveFromCart] - callback function triggered when users opt out of an offer-only offer.
106
- * @property [includeCheckout] - toggles whether or not to display the card capture checkout view. Defaults to true. When false, an AddToCart callback must be provided.
107
- * @property [logoOverride] - object for overriding Buddy's trust badge.
108
- */
109
- declare type BuddyOfferElementProps = {
110
- ion: string;
111
- partnerID: string;
112
- stage?: string;
113
- viewType?: string;
114
- theme?: ThemeObject;
115
- data?: DataObject;
116
- onUserEvent?: OnUserEventCallback;
117
- onAddToCart?: AddToCartFunction;
118
- onRemoveFromCart?: RemoveFromCartFunction;
119
- includeCheckout?: boolean;
120
- logoOverride?: LogoOverride;
121
- };
122
-
123
- declare function BuddyOfferElement(options: BuddyOfferElementProps): FunctionComponent<BuddyOfferElementProps>;
124
-
125
- declare const defaultOptions: {
126
- stage: string;
127
- };
128
-
129
- declare function isEmptyOrNil(value: any): boolean;
130
-
131
- declare function validatePropertiesOfObject(propsToCheck: String[], obj: any): null | string;
132
-
133
- declare function findScript(stage: string): HTMLElement | undefined;
134
-
135
- declare function updateOffer(options: any): void;
136
-
137
- declare function createOffer(options: any): void;
138
-
139
- declare function injectScript(options: any): HTMLElement;
140
-
141
- declare var scriptPromise: Promise | null;
142
-
143
- declare function loadScript(options: any): Promise;
144
-