@magicfeedback/native 1.0.8 → 1.1.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.
- package/README.md +68 -16
- package/dist/magicfeedback-sdk.browser.js +1 -1
- package/dist/magicfeedback-sdk.node.js +1 -1
- package/dist/types/src/index.d.ts +1 -1
- package/dist/types/src/main.d.ts +2 -2
- package/dist/types/src/models/form.d.ts +45 -6
- package/dist/types/src/models/formData.d.ts +17 -0
- package/dist/types/src/models/types.d.ts +10 -5
- package/dist/types/src/services/paths.d.ts +2 -0
- package/dist/types/src/services/questions.service.d.ts +5 -0
- package/dist/types/src/services/request.service.d.ts +4 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -64,9 +64,27 @@ let form = window.magicfeedback.form(
|
|
|
64
64
|
form.generate(
|
|
65
65
|
"demo_form_div",
|
|
66
66
|
{
|
|
67
|
-
addButton: true | false // Default false
|
|
68
|
-
beforeSubmitEvent:
|
|
69
|
-
|
|
67
|
+
addButton: true | false, // Default false
|
|
68
|
+
beforeSubmitEvent: ({
|
|
69
|
+
loading: boolean,
|
|
70
|
+
progress: number,
|
|
71
|
+
total: number
|
|
72
|
+
}) => {
|
|
73
|
+
}, //Function to execute before send the form
|
|
74
|
+
afterSubmitEvent: ({
|
|
75
|
+
loading: boolean,
|
|
76
|
+
progress: number,
|
|
77
|
+
total: number,
|
|
78
|
+
response: string, // Response of the server if everything is ok
|
|
79
|
+
error: string, // Error of the server if something is wrong
|
|
80
|
+
}) => {
|
|
81
|
+
}, //Function to execute after send the form with the response
|
|
82
|
+
onLoadedEvent: ({
|
|
83
|
+
loading: boolean,
|
|
84
|
+
progress: number,
|
|
85
|
+
total: number,
|
|
86
|
+
}) => {
|
|
87
|
+
} //Function to execute after load the form
|
|
70
88
|
}
|
|
71
89
|
)
|
|
72
90
|
```
|
|
@@ -83,6 +101,7 @@ this example). You can customize the form generation by including the optional p
|
|
|
83
101
|
is submitted.
|
|
84
102
|
* **afterSubmitEvent**: An optional function that you can define to execute actions after the form is submitted. This
|
|
85
103
|
function receives the server response as a parameter.
|
|
104
|
+
* **onLoadedEvent**: An optional function that you can define to execute actions after the form is loaded.
|
|
86
105
|
|
|
87
106
|
Finally, to send the form, you can use the form.send() function.
|
|
88
107
|
|
|
@@ -98,28 +117,61 @@ By following these steps and including the appropriate HTML and JavaScript code
|
|
|
98
117
|
display feedback forms on your website using the magicfeedback service.
|
|
99
118
|
|
|
100
119
|
### B. Send feedback directly
|
|
101
|
-
|
|
102
|
-
This section provides an overview of how to use
|
|
120
|
+
|
|
121
|
+
With this option you can send feedback directly without generate a form. This section provides an overview of how to use
|
|
122
|
+
this feature and the necessary code snippets.
|
|
103
123
|
|
|
104
124
|
To send feedback directly, you need to include the following JavaScript code snippet in your application:
|
|
105
125
|
|
|
106
126
|
```js
|
|
107
127
|
window.magicfeedback.send(
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
]
|
|
116
|
-
)
|
|
128
|
+
"$_APP_ID",
|
|
129
|
+
"$_PUBLIC_KEY",
|
|
130
|
+
feedbackData,
|
|
131
|
+
completed, // Default true
|
|
132
|
+
"$_ID", // Optional
|
|
133
|
+
"$_PRIVATE_KEY", // Optional
|
|
134
|
+
)
|
|
117
135
|
```
|
|
118
136
|
|
|
119
|
-
In this code snippet, you need to replace $_APP_ID with the actual ID of your feedback application and the $_PUBLIC_KEY
|
|
120
|
-
|
|
137
|
+
In this code snippet, you need to replace $_APP_ID with the actual ID of your feedback application and the $_PUBLIC_KEY
|
|
138
|
+
with the public key of your feedback application. This ID and key is provided by the magicfeedback service.
|
|
139
|
+
|
|
140
|
+
###### FeedbackData
|
|
141
|
+
|
|
142
|
+
Then, you can include the feedback data in an object with the following structure:
|
|
143
|
+
|
|
144
|
+
{
|
|
145
|
+
text: "string", // Optional
|
|
146
|
+
answers: [
|
|
147
|
+
{
|
|
148
|
+
key: 'string',
|
|
149
|
+
value: ["string"]
|
|
150
|
+
},
|
|
151
|
+
],
|
|
152
|
+
metadata: [
|
|
153
|
+
{
|
|
154
|
+
key: 'string',
|
|
155
|
+
value: "string"
|
|
156
|
+
},
|
|
157
|
+
],
|
|
158
|
+
metrics: [
|
|
159
|
+
{
|
|
160
|
+
key: 'string',
|
|
161
|
+
value: "string"
|
|
162
|
+
},
|
|
163
|
+
],
|
|
164
|
+
profile: [
|
|
165
|
+
{
|
|
166
|
+
key: 'string',
|
|
167
|
+
value: "string"
|
|
168
|
+
},
|
|
169
|
+
],
|
|
170
|
+
}
|
|
121
171
|
|
|
122
172
|
* **key**: This setting determines the key of the feedback data.
|
|
123
173
|
* **value**: This setting determines the value of the feedback data.
|
|
124
174
|
|
|
175
|
+
Not all the fields are required. You can send only the fields that you need. But you need to send one of that minimal.
|
|
176
|
+
|
|
125
177
|
Finally, to send the feedback, you can use the magicfeedback.send() function.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.magicfeedback=t():e.magicfeedback=t()}(self,(()=>{return e={98:function(e,t){var r="undefined"!=typeof self?self:this,n=function(){function e(){this.fetch=!1,this.DOMException=r.DOMException}return e.prototype=r,new e}();!function(e){!function(t){var r="URLSearchParams"in e,n="Symbol"in e&&"iterator"in Symbol,o="FileReader"in e&&"Blob"in e&&function(){try{return new Blob,!0}catch(e){return!1}}(),i="FormData"in e,s="ArrayBuffer"in e;if(s)var a=["[object Int8Array]","[object Uint8Array]","[object Uint8ClampedArray]","[object Int16Array]","[object Uint16Array]","[object Int32Array]","[object Uint32Array]","[object Float32Array]","[object Float64Array]"],c=ArrayBuffer.isView||function(e){return e&&a.indexOf(Object.prototype.toString.call(e))>-1};function d(e){if("string"!=typeof e&&(e=String(e)),/[^a-z0-9\-#$%&'*+.^_`|~]/i.test(e))throw new TypeError("Invalid character in header field name");return e.toLowerCase()}function u(e){return"string"!=typeof e&&(e=String(e)),e}function l(e){var t={next:function(){var t=e.shift();return{done:void 0===t,value:t}}};return n&&(t[Symbol.iterator]=function(){return t}),t}function f(e){this.map={},e instanceof f?e.forEach((function(e,t){this.append(t,e)}),this):Array.isArray(e)?e.forEach((function(e){this.append(e[0],e[1])}),this):e&&Object.getOwnPropertyNames(e).forEach((function(t){this.append(t,e[t])}),this)}function p(e){if(e.bodyUsed)return Promise.reject(new TypeError("Already read"));e.bodyUsed=!0}function h(e){return new Promise((function(t,r){e.onload=function(){t(e.result)},e.onerror=function(){r(e.error)}}))}function b(e){var t=new FileReader,r=h(t);return t.readAsArrayBuffer(e),r}function y(e){if(e.slice)return e.slice(0);var t=new Uint8Array(e.byteLength);return t.set(new Uint8Array(e)),t.buffer}function m(){return this.bodyUsed=!1,this._initBody=function(e){var t;this._bodyInit=e,e?"string"==typeof e?this._bodyText=e:o&&Blob.prototype.isPrototypeOf(e)?this._bodyBlob=e:i&&FormData.prototype.isPrototypeOf(e)?this._bodyFormData=e:r&&URLSearchParams.prototype.isPrototypeOf(e)?this._bodyText=e.toString():s&&o&&(t=e)&&DataView.prototype.isPrototypeOf(t)?(this._bodyArrayBuffer=y(e.buffer),this._bodyInit=new Blob([this._bodyArrayBuffer])):s&&(ArrayBuffer.prototype.isPrototypeOf(e)||c(e))?this._bodyArrayBuffer=y(e):this._bodyText=e=Object.prototype.toString.call(e):this._bodyText="",this.headers.get("content-type")||("string"==typeof e?this.headers.set("content-type","text/plain;charset=UTF-8"):this._bodyBlob&&this._bodyBlob.type?this.headers.set("content-type",this._bodyBlob.type):r&&URLSearchParams.prototype.isPrototypeOf(e)&&this.headers.set("content-type","application/x-www-form-urlencoded;charset=UTF-8"))},o&&(this.blob=function(){var e=p(this);if(e)return e;if(this._bodyBlob)return Promise.resolve(this._bodyBlob);if(this._bodyArrayBuffer)return Promise.resolve(new Blob([this._bodyArrayBuffer]));if(this._bodyFormData)throw new Error("could not read FormData body as blob");return Promise.resolve(new Blob([this._bodyText]))},this.arrayBuffer=function(){return this._bodyArrayBuffer?p(this)||Promise.resolve(this._bodyArrayBuffer):this.blob().then(b)}),this.text=function(){var e,t,r,n=p(this);if(n)return n;if(this._bodyBlob)return e=this._bodyBlob,r=h(t=new FileReader),t.readAsText(e),r;if(this._bodyArrayBuffer)return Promise.resolve(function(e){for(var t=new Uint8Array(e),r=new Array(t.length),n=0;n<t.length;n++)r[n]=String.fromCharCode(t[n]);return r.join("")}(this._bodyArrayBuffer));if(this._bodyFormData)throw new Error("could not read FormData body as text");return Promise.resolve(this._bodyText)},i&&(this.formData=function(){return this.text().then(w)}),this.json=function(){return this.text().then(JSON.parse)},this}f.prototype.append=function(e,t){e=d(e),t=u(t);var r=this.map[e];this.map[e]=r?r+", "+t:t},f.prototype.delete=function(e){delete this.map[d(e)]},f.prototype.get=function(e){return e=d(e),this.has(e)?this.map[e]:null},f.prototype.has=function(e){return this.map.hasOwnProperty(d(e))},f.prototype.set=function(e,t){this.map[d(e)]=u(t)},f.prototype.forEach=function(e,t){for(var r in this.map)this.map.hasOwnProperty(r)&&e.call(t,this.map[r],r,this)},f.prototype.keys=function(){var e=[];return this.forEach((function(t,r){e.push(r)})),l(e)},f.prototype.values=function(){var e=[];return this.forEach((function(t){e.push(t)})),l(e)},f.prototype.entries=function(){var e=[];return this.forEach((function(t,r){e.push([r,t])})),l(e)},n&&(f.prototype[Symbol.iterator]=f.prototype.entries);var g=["DELETE","GET","HEAD","OPTIONS","POST","PUT"];function v(e,t){var r,n,o=(t=t||{}).body;if(e instanceof v){if(e.bodyUsed)throw new TypeError("Already read");this.url=e.url,this.credentials=e.credentials,t.headers||(this.headers=new f(e.headers)),this.method=e.method,this.mode=e.mode,this.signal=e.signal,o||null==e._bodyInit||(o=e._bodyInit,e.bodyUsed=!0)}else this.url=String(e);if(this.credentials=t.credentials||this.credentials||"same-origin",!t.headers&&this.headers||(this.headers=new f(t.headers)),this.method=(n=(r=t.method||this.method||"GET").toUpperCase(),g.indexOf(n)>-1?n:r),this.mode=t.mode||this.mode||null,this.signal=t.signal||this.signal,this.referrer=null,("GET"===this.method||"HEAD"===this.method)&&o)throw new TypeError("Body not allowed for GET or HEAD requests");this._initBody(o)}function w(e){var t=new FormData;return e.trim().split("&").forEach((function(e){if(e){var r=e.split("="),n=r.shift().replace(/\+/g," "),o=r.join("=").replace(/\+/g," ");t.append(decodeURIComponent(n),decodeURIComponent(o))}})),t}function k(e,t){t||(t={}),this.type="default",this.status=void 0===t.status?200:t.status,this.ok=this.status>=200&&this.status<300,this.statusText="statusText"in t?t.statusText:"OK",this.headers=new f(t.headers),this.url=t.url||"",this._initBody(e)}v.prototype.clone=function(){return new v(this,{body:this._bodyInit})},m.call(v.prototype),m.call(k.prototype),k.prototype.clone=function(){return new k(this._bodyInit,{status:this.status,statusText:this.statusText,headers:new f(this.headers),url:this.url})},k.error=function(){var e=new k(null,{status:0,statusText:""});return e.type="error",e};var E=[301,302,303,307,308];k.redirect=function(e,t){if(-1===E.indexOf(t))throw new RangeError("Invalid status code");return new k(null,{status:t,headers:{location:e}})},t.DOMException=e.DOMException;try{new t.DOMException}catch(e){t.DOMException=function(e,t){this.message=e,this.name=t;var r=Error(e);this.stack=r.stack},t.DOMException.prototype=Object.create(Error.prototype),t.DOMException.prototype.constructor=t.DOMException}function x(e,r){return new Promise((function(n,i){var s=new v(e,r);if(s.signal&&s.signal.aborted)return i(new t.DOMException("Aborted","AbortError"));var a=new XMLHttpRequest;function c(){a.abort()}a.onload=function(){var e,t,r={status:a.status,statusText:a.statusText,headers:(e=a.getAllResponseHeaders()||"",t=new f,e.replace(/\r?\n[\t ]+/g," ").split(/\r?\n/).forEach((function(e){var r=e.split(":"),n=r.shift().trim();if(n){var o=r.join(":").trim();t.append(n,o)}})),t)};r.url="responseURL"in a?a.responseURL:r.headers.get("X-Request-URL");var o="response"in a?a.response:a.responseText;n(new k(o,r))},a.onerror=function(){i(new TypeError("Network request failed"))},a.ontimeout=function(){i(new TypeError("Network request failed"))},a.onabort=function(){i(new t.DOMException("Aborted","AbortError"))},a.open(s.method,s.url,!0),"include"===s.credentials?a.withCredentials=!0:"omit"===s.credentials&&(a.withCredentials=!1),"responseType"in a&&o&&(a.responseType="blob"),s.headers.forEach((function(e,t){a.setRequestHeader(t,e)})),s.signal&&(s.signal.addEventListener("abort",c),a.onreadystatechange=function(){4===a.readyState&&s.signal.removeEventListener("abort",c)}),a.send(void 0===s._bodyInit?null:s._bodyInit)}))}x.polyfill=!0,e.fetch||(e.fetch=x,e.Headers=f,e.Request=v,e.Response=k),t.Headers=f,t.Request=v,t.Response=k,t.fetch=x,Object.defineProperty(t,"__esModule",{value:!0})}({})}(n),n.fetch.ponyfill=!0,delete n.fetch.polyfill;var o=n;(t=o.fetch).default=o.fetch,t.fetch=o.fetch,t.Headers=o.Headers,t.Request=o.Request,t.Response=o.Response,e.exports=t},607:function(e,t,r){"use strict";var n=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const o=n(r(519));let i=null;i||(i=(0,o.default)()),t.default=i},519:function(e,t,r){"use strict";var n=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function s(e){try{c(n.next(e))}catch(e){i(e)}}function a(e){try{c(n.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(s,a)}c((n=n.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0});const o=r(954),i=r(712),s=r(5),a=r(341);t.default=function(){const e=new i.Config;let t;return{init:function(r){(null==r?void 0:r.url)&&e.set("url",null==r?void 0:r.url),(null==r?void 0:r.debug)&&e.set("debug",null==r?void 0:r.debug),t=new s.Log(e),t.log("Initialized Magicfeedback",e)},send:function(r,o,i,s){return n(this,void 0,void 0,(function*(){r||t.err("No appID provided"),o||t.err("No publicKey provided"),i||t.err("No answers provided"),0==i.length&&t.err("Answers are empty");const n=e.get("url"),c={integration:r,publicKey:o,feedback:{answers:i,profile:s}};try{const e=yield(0,a.sendFeedback)(n,c,t);return t.log("sent native feedback"),e}catch(e){return t.err("error native feedback",e),!1}}))},form:function(r,n){return r||t.err("No appID provided"),n||t.err("No publicKey provided"),new o.Form(e,r,n)}}}},712:(e,t)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.Config=void 0,t.Config=class{constructor(){this.variables={},this.variables.url="https://mvp-dot-feedbackai-380013.lm.r.appspot.com/",this.variables.debug=!1}get(e){return this.variables[e]}set(e,t){this.variables[e]=t}}},954:function(e,t,r){"use strict";var n=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function s(e){try{c(n.next(e))}catch(e){i(e)}}function a(e){try{c(n.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(s,a)}c((n=n.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.Form=void 0;const o=r(5),i=r(341);t.Form=class{constructor(e,t,r){this.config=e,this.log=new o.Log(e),this.appId=t,this.publicKey=r}generate(e,t={}){this.log.log("Generating form for app",this.appId);const r=this.config.get("url");(0,i.getQuestions)(r,this.appId,this.publicKey,this.log).then((r=>{void 0!==r&&r?this.generateForm(this.appId,r,e,t):this.log.err(`No questions for app ${this.appId}`)}))}generateForm(e,t,r,o={}){const i=document.getElementById(r);if(!i)return void this.log.err(`Element with ID '${r}' not found.`);i.classList.add("magicfeedback-container");const s=document.createElement("form");if(s.classList.add("magicfeedback-form"),s.id="magicfeedback-"+e,t.forEach((e=>{const{id:t,title:r,type:n,ref:o,require:i,value:a,defaultValue:c}=e;let d,u,l=document.createElement("div");switch(l.classList.add("magicfeedback-div"),n){case"TEXT":d=document.createElement("input"),d.type="text",u="magicfeedback-text";break;case"LONGTEXT":d=document.createElement("textarea"),d.rows=3,u="magicfeedback-longtext";break;case"NUMBER":d=document.createElement("input"),d.type="number",u="magicfeedback-number",a.length&&(a.sort(((e,t)=>Number(e)-Number(t))),d.max=a[a.length-1],d.min=a[0],d.value=a[0]);break;case"RADIO":case"MULTIPLECHOICE":d=document.createElement("div"),u="magicfeedback-"+("RADIO"===n?"radio":"checkbox"),a.forEach((e=>{const t=document.createElement("div");t.classList.add(`magicfeedback-${"RADIO"===n?"radio":"checkbox"}-container`);const r=document.createElement("label"),s=document.createElement("input");s.type="RADIO"===n?"radio":"checkbox",s.name=o,s.value=e,s.required=i,s.classList.add(u),s.classList.add("magicfeedback-input"),e===c&&(s.checked=!0),r.textContent=e,t.appendChild(s),t.appendChild(r),d.appendChild(t)}));break;case"SELECT":d=document.createElement("select"),u="magicfeedback-select",a.forEach((e=>{const t=document.createElement("option");t.value=e,t.text=e,d.appendChild(t)}));break;case"DATE":d=document.createElement("input"),d.type="date",u="magicfeedback-date";break;case"BOOLEAN":d=document.createElement("input"),d.type="checkbox",u="magicfeedback-boolean";break;default:return}d.id=`magicfeedback-${t}`,d.setAttribute("name",o),void 0!==c&&(d.value=c);const f=document.createElement("label");f.setAttribute("for",`magicfeedback-${t}`),f.textContent=r,f.classList.add("magicfeedback-label"),l.appendChild(f),d.classList.add(u),"RADIO"!=n&&"MULTIPLECHOICE"!=n&&(d.classList.add("magicfeedback-input"),d.required=i),l.appendChild(d),s.appendChild(l)})),o.addButton){const e=document.createElement("button");e.type="submit",e.textContent="Submit",e.classList.add("magicfeedback-submit"),s.appendChild(e)}i.appendChild(s),s.addEventListener("submit",(e=>n(this,void 0,void 0,(function*(){e.preventDefault();try{o.beforeSubmitEvent&&(yield o.beforeSubmitEvent());const e=yield this.send();return o.afterSubmitEvent&&(yield o.afterSubmitEvent(e)),e}catch(e){return this.log.err(`An error occurred while submitting the form ${this.appId}:`,e),e}}))))}answer(){const e=document.getElementById("magicfeedback-"+this.appId);if(!e)return this.log.err(`Form "${e}" not found.`),[];const t=[];return e.querySelectorAll(".magicfeedback-input").forEach((e=>{const r=e.type,n={key:e.name,value:[]},o=e.value;"radio"===r||"checkbox"===r?e.checked&&(n.value.push(o),t.push(n)):(n.value.push(o),t.push(n))})),t}send(){return n(this,void 0,void 0,(function*(){try{const e=this.answer();if(0===e.length)throw new Error("No answers provided");const t=this.config.get("url"),r={integration:this.appId,publicKey:this.publicKey,feedback:{answers:e}};return yield(0,i.sendFeedback)(t,r,this.log)}catch(e){throw this.log.err(`An error occurred while submitting the form ${this.appId}:`,e),e}}))}}},374:(e,t)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.endpoints=void 0,t.endpoints={sdk:{app:(e,t)=>`sdk/app/${e}/${t}`,feedback:"sdk/feedback"}}},341:function(e,t,r){"use strict";var n=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function s(e){try{c(n.next(e))}catch(e){i(e)}}function a(e){try{c(n.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(s,a)}c((n=n.apply(e,t||[])).next())}))},o=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.sendFeedback=t.getQuestions=void 0;const i=o(r(98)),s=o(r(147)),a=r(374),c={Accept:"application/json","Magicfeedback-Sdk-Version":s.default.version};t.getQuestions=function(e,t,r,o){return n(this,void 0,void 0,(function*(){try{const n=yield(0,i.default)(e+a.endpoints.sdk.app(t,r),{method:"GET",headers:c});if(n.ok){const e=yield n.json();return o.log(`Received questions for app ${t}`,e),e}throw o.err(`Failed to get questions for app ${t}:`,n.status,n.statusText),new Error("[MagicFeedback] Bad response from server")}catch(e){return console.error(e),[]}}))},t.sendFeedback=function(e,t,r){return n(this,void 0,void 0,(function*(){try{const n=yield(0,i.default)(e+a.endpoints.sdk.feedback,{method:"POST",headers:Object.assign({"Content-Type":"application/json"},c),body:JSON.stringify(t)});if(n.ok)return r.log(`Form ${t.integration} submitted successfully!`),!0;throw r.err(`Failed to submit form ${t.integration}:`,n.status,n.statusText),new Error(n.statusText)}catch(e){return console.error(e),!1}}))}},5:(e,t)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.Log=void 0,t.Log=class{constructor(e){this.config=e}log(...e){this.config.get("debug")&&console.log("[MagicFeedback]:",...e)}err(...e){console.error("[MagicFeedback]:",...e)}}},147:e=>{"use strict";e.exports=JSON.parse('{"name":"@magicfeedback/native","version":"1.0.8","main":"./dist/magicfeedback-sdk.node.js","browser":"./dist/magicfeedback-sdk.browser.js","types":"./dist/types/src/index.d.ts","repository":{"type":"git","url":"git+ssh://git@github.com/MagicFeedback/magicfeedback-sdk.git"},"author":"farias@magicfeedback.io","license":"MIT","private":false,"scripts":{"dev":"vite","build":"webpack","build:watch":"webpack --watch --mode development","test":"jest","test:watch":"jest --watchAll","coverage":"vitest run --coverage"},"files":["dist"],"devDependencies":{"@babel/preset-typescript":"^7.22.5","@types/node":"^17.0.21","@types/webpack":"^5.28.0","@types/webpack-node-externals":"^2.5.3","c8":"^7.11.0","jest":"^29.5.0","jest-environment-jsdom":"^29.5.0","jest-fetch-mock":"^3.0.3","nock":"^13.2.4","ts-jest":"^29.1.0","ts-loader":"^9.2.7","ts-node":"^10.7.0","typescript":"^4.6.2","vite":"^2.8.0","vite-plugin-dts":"^0.9.9","vitest":"^0.5.9","webpack":"^5.70.0","webpack-cli":"^4.9.2","webpack-node-externals":"^3.0.0"},"dependencies":{"cross-fetch":"^3.1.5","is-bundling-for-browser-or-node":"^1.1.1"},"description":"MagicFeedbackAI JavaScript Library for [MagicFeedback.io](https://magicfeedback.io/)","bugs":{"url":"https://github.com/MagicFeedback/magicfeedback-sdk/issues"},"homepage":"https://github.com/MagicFeedback/magicfeedback-sdk#readme","directories":{"example":"examples","test":"test"}}')}},t={},function r(n){var o=t[n];if(void 0!==o)return o.exports;var i=t[n]={exports:{}};return e[n].call(i.exports,i,i.exports,r),i.exports}(607).default;var e,t}));
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.magicfeedback=t():e.magicfeedback=t()}(self,(()=>{return e={98:function(e,t){var r="undefined"!=typeof self?self:this,o=function(){function e(){this.fetch=!1,this.DOMException=r.DOMException}return e.prototype=r,new e}();!function(e){!function(t){var r="URLSearchParams"in e,o="Symbol"in e&&"iterator"in Symbol,i="FileReader"in e&&"Blob"in e&&function(){try{return new Blob,!0}catch(e){return!1}}(),s="FormData"in e,n="ArrayBuffer"in e;if(n)var a=["[object Int8Array]","[object Uint8Array]","[object Uint8ClampedArray]","[object Int16Array]","[object Uint16Array]","[object Int32Array]","[object Uint32Array]","[object Float32Array]","[object Float64Array]"],d=ArrayBuffer.isView||function(e){return e&&a.indexOf(Object.prototype.toString.call(e))>-1};function c(e){if("string"!=typeof e&&(e=String(e)),/[^a-z0-9\-#$%&'*+.^_`|~]/i.test(e))throw new TypeError("Invalid character in header field name");return e.toLowerCase()}function u(e){return"string"!=typeof e&&(e=String(e)),e}function l(e){var t={next:function(){var t=e.shift();return{done:void 0===t,value:t}}};return o&&(t[Symbol.iterator]=function(){return t}),t}function h(e){this.map={},e instanceof h?e.forEach((function(e,t){this.append(t,e)}),this):Array.isArray(e)?e.forEach((function(e){this.append(e[0],e[1])}),this):e&&Object.getOwnPropertyNames(e).forEach((function(t){this.append(t,e[t])}),this)}function p(e){if(e.bodyUsed)return Promise.reject(new TypeError("Already read"));e.bodyUsed=!0}function f(e){return new Promise((function(t,r){e.onload=function(){t(e.result)},e.onerror=function(){r(e.error)}}))}function b(e){var t=new FileReader,r=f(t);return t.readAsArrayBuffer(e),r}function y(e){if(e.slice)return e.slice(0);var t=new Uint8Array(e.byteLength);return t.set(new Uint8Array(e)),t.buffer}function m(){return this.bodyUsed=!1,this._initBody=function(e){var t;this._bodyInit=e,e?"string"==typeof e?this._bodyText=e:i&&Blob.prototype.isPrototypeOf(e)?this._bodyBlob=e:s&&FormData.prototype.isPrototypeOf(e)?this._bodyFormData=e:r&&URLSearchParams.prototype.isPrototypeOf(e)?this._bodyText=e.toString():n&&i&&(t=e)&&DataView.prototype.isPrototypeOf(t)?(this._bodyArrayBuffer=y(e.buffer),this._bodyInit=new Blob([this._bodyArrayBuffer])):n&&(ArrayBuffer.prototype.isPrototypeOf(e)||d(e))?this._bodyArrayBuffer=y(e):this._bodyText=e=Object.prototype.toString.call(e):this._bodyText="",this.headers.get("content-type")||("string"==typeof e?this.headers.set("content-type","text/plain;charset=UTF-8"):this._bodyBlob&&this._bodyBlob.type?this.headers.set("content-type",this._bodyBlob.type):r&&URLSearchParams.prototype.isPrototypeOf(e)&&this.headers.set("content-type","application/x-www-form-urlencoded;charset=UTF-8"))},i&&(this.blob=function(){var e=p(this);if(e)return e;if(this._bodyBlob)return Promise.resolve(this._bodyBlob);if(this._bodyArrayBuffer)return Promise.resolve(new Blob([this._bodyArrayBuffer]));if(this._bodyFormData)throw new Error("could not read FormData body as blob");return Promise.resolve(new Blob([this._bodyText]))},this.arrayBuffer=function(){return this._bodyArrayBuffer?p(this)||Promise.resolve(this._bodyArrayBuffer):this.blob().then(b)}),this.text=function(){var e,t,r,o=p(this);if(o)return o;if(this._bodyBlob)return e=this._bodyBlob,r=f(t=new FileReader),t.readAsText(e),r;if(this._bodyArrayBuffer)return Promise.resolve(function(e){for(var t=new Uint8Array(e),r=new Array(t.length),o=0;o<t.length;o++)r[o]=String.fromCharCode(t[o]);return r.join("")}(this._bodyArrayBuffer));if(this._bodyFormData)throw new Error("could not read FormData body as text");return Promise.resolve(this._bodyText)},s&&(this.formData=function(){return this.text().then(w)}),this.json=function(){return this.text().then(JSON.parse)},this}h.prototype.append=function(e,t){e=c(e),t=u(t);var r=this.map[e];this.map[e]=r?r+", "+t:t},h.prototype.delete=function(e){delete this.map[c(e)]},h.prototype.get=function(e){return e=c(e),this.has(e)?this.map[e]:null},h.prototype.has=function(e){return this.map.hasOwnProperty(c(e))},h.prototype.set=function(e,t){this.map[c(e)]=u(t)},h.prototype.forEach=function(e,t){for(var r in this.map)this.map.hasOwnProperty(r)&&e.call(t,this.map[r],r,this)},h.prototype.keys=function(){var e=[];return this.forEach((function(t,r){e.push(r)})),l(e)},h.prototype.values=function(){var e=[];return this.forEach((function(t){e.push(t)})),l(e)},h.prototype.entries=function(){var e=[];return this.forEach((function(t,r){e.push([r,t])})),l(e)},o&&(h.prototype[Symbol.iterator]=h.prototype.entries);var g=["DELETE","GET","HEAD","OPTIONS","POST","PUT"];function v(e,t){var r,o,i=(t=t||{}).body;if(e instanceof v){if(e.bodyUsed)throw new TypeError("Already read");this.url=e.url,this.credentials=e.credentials,t.headers||(this.headers=new h(e.headers)),this.method=e.method,this.mode=e.mode,this.signal=e.signal,i||null==e._bodyInit||(i=e._bodyInit,e.bodyUsed=!0)}else this.url=String(e);if(this.credentials=t.credentials||this.credentials||"same-origin",!t.headers&&this.headers||(this.headers=new h(t.headers)),this.method=(o=(r=t.method||this.method||"GET").toUpperCase(),g.indexOf(o)>-1?o:r),this.mode=t.mode||this.mode||null,this.signal=t.signal||this.signal,this.referrer=null,("GET"===this.method||"HEAD"===this.method)&&i)throw new TypeError("Body not allowed for GET or HEAD requests");this._initBody(i)}function w(e){var t=new FormData;return e.trim().split("&").forEach((function(e){if(e){var r=e.split("="),o=r.shift().replace(/\+/g," "),i=r.join("=").replace(/\+/g," ");t.append(decodeURIComponent(o),decodeURIComponent(i))}})),t}function k(e,t){t||(t={}),this.type="default",this.status=void 0===t.status?200:t.status,this.ok=this.status>=200&&this.status<300,this.statusText="statusText"in t?t.statusText:"OK",this.headers=new h(t.headers),this.url=t.url||"",this._initBody(e)}v.prototype.clone=function(){return new v(this,{body:this._bodyInit})},m.call(v.prototype),m.call(k.prototype),k.prototype.clone=function(){return new k(this._bodyInit,{status:this.status,statusText:this.statusText,headers:new h(this.headers),url:this.url})},k.error=function(){var e=new k(null,{status:0,statusText:""});return e.type="error",e};var E=[301,302,303,307,308];k.redirect=function(e,t){if(-1===E.indexOf(t))throw new RangeError("Invalid status code");return new k(null,{status:t,headers:{location:e}})},t.DOMException=e.DOMException;try{new t.DOMException}catch(e){t.DOMException=function(e,t){this.message=e,this.name=t;var r=Error(e);this.stack=r.stack},t.DOMException.prototype=Object.create(Error.prototype),t.DOMException.prototype.constructor=t.DOMException}function x(e,r){return new Promise((function(o,s){var n=new v(e,r);if(n.signal&&n.signal.aborted)return s(new t.DOMException("Aborted","AbortError"));var a=new XMLHttpRequest;function d(){a.abort()}a.onload=function(){var e,t,r={status:a.status,statusText:a.statusText,headers:(e=a.getAllResponseHeaders()||"",t=new h,e.replace(/\r?\n[\t ]+/g," ").split(/\r?\n/).forEach((function(e){var r=e.split(":"),o=r.shift().trim();if(o){var i=r.join(":").trim();t.append(o,i)}})),t)};r.url="responseURL"in a?a.responseURL:r.headers.get("X-Request-URL");var i="response"in a?a.response:a.responseText;o(new k(i,r))},a.onerror=function(){s(new TypeError("Network request failed"))},a.ontimeout=function(){s(new TypeError("Network request failed"))},a.onabort=function(){s(new t.DOMException("Aborted","AbortError"))},a.open(n.method,n.url,!0),"include"===n.credentials?a.withCredentials=!0:"omit"===n.credentials&&(a.withCredentials=!1),"responseType"in a&&i&&(a.responseType="blob"),n.headers.forEach((function(e,t){a.setRequestHeader(t,e)})),n.signal&&(n.signal.addEventListener("abort",d),a.onreadystatechange=function(){4===a.readyState&&n.signal.removeEventListener("abort",d)}),a.send(void 0===n._bodyInit?null:n._bodyInit)}))}x.polyfill=!0,e.fetch||(e.fetch=x,e.Headers=h,e.Request=v,e.Response=k),t.Headers=h,t.Request=v,t.Response=k,t.fetch=x,Object.defineProperty(t,"__esModule",{value:!0})}({})}(o),o.fetch.ponyfill=!0,delete o.fetch.polyfill;var i=o;(t=i.fetch).default=i.fetch,t.fetch=i.fetch,t.Headers=i.Headers,t.Request=i.Request,t.Response=i.Response,e.exports=t},607:function(e,t,r){"use strict";var o=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const i=o(r(519));let s=null;s||(s=(0,i.default)()),t.default=s},519:function(e,t,r){"use strict";var o=this&&this.__awaiter||function(e,t,r,o){return new(r||(r=Promise))((function(i,s){function n(e){try{d(o.next(e))}catch(e){s(e)}}function a(e){try{d(o.throw(e))}catch(e){s(e)}}function d(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(n,a)}d((o=o.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0});const i=r(954),s=r(712),n=r(5),a=r(341);t.default=function(){const e=new s.Config;let t;return{init:function(r){(null==r?void 0:r.url)&&e.set("url",null==r?void 0:r.url),(null==r?void 0:r.debug)&&e.set("debug",null==r?void 0:r.debug),t=new n.Log(e),t.log("Initialized Magicfeedback",e)},send:function(r,i,s,n=!0,d,c){return o(this,void 0,void 0,(function*(){r||t.err("No appID provided"),i||t.err("No publicKey provided"),s||t.err("No feedback provided"),s.answers||s.profile||s.metrics||s.metadata||t.err("No feedback data provided");const o=e.get("url"),u={integration:r,publicKey:i,privateKey:c,completed:n,id:d,feedback:s};try{const e=yield(0,a.sendFeedback)(o,u,t);return t.log("sent native feedback"),e}catch(e){return t.err("error native feedback",e),!1}}))},form:function(r,o){return r||t.err("No appID provided"),o||t.err("No publicKey provided"),new i.Form(e,r,o)}}}},712:(e,t)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.Config=void 0,t.Config=class{constructor(){this.variables={},this.variables.url="https://mvp-dot-feedbackai-380013.lm.r.appspot.com/",this.variables.debug=!1}get(e){return this.variables[e]}set(e,t){this.variables[e]=t}}},954:function(e,t,r){"use strict";var o=this&&this.__awaiter||function(e,t,r,o){return new(r||(r=Promise))((function(i,s){function n(e){try{d(o.next(e))}catch(e){s(e)}}function a(e){try{d(o.throw(e))}catch(e){s(e)}}function d(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(n,a)}d((o=o.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.Form=void 0;const i=r(5),s=r(341),n=r(183);t.Form=class{constructor(e,t,r){this.config=e,this.log=new i.Log(e),this.appId=t,this.publicKey=r,this.url=e.get("url"),this.formData=null,this.questions=[],this.elementQuestions=[],this.questionInProcess=null,this.history={},this.progress=0,this.total=0}generate(e,t={}){return o(this,void 0,void 0,(function*(){try{if(this.formData=yield(0,s.getForm)(this.url,this.appId,this.publicKey,this.log),void 0===this.formData||!this.formData)throw new Error(`No form for app ${this.appId}`);if(this.log.log("Generating form for app",this.appId),this.questions=yield(0,s.getQuestions)(this.url,this.appId,this.publicKey,this.log),void 0===this.questions||!this.questions)throw new Error(`No questions for app ${this.appId}`);this.total=this.questions.length,t.onLoadedEvent&&(yield t.onLoadedEvent({loading:!1,progress:this.progress,total:this.total})),this.generateForm(e,t)}catch(e){return void this.log.err(e)}}))}generateForm(e,t={}){var r,i;try{const s=document.getElementById(e);if(!s)throw new Error(`Element with ID '${e}' not found.`);s.classList.add("magicfeedback-container"),s.innerHTML="";const a=document.createElement("form");a.classList.add("magicfeedback-form"),a.id="magicfeedback-"+this.appId;const d=document.createElement("div");if(d.classList.add("magicfeedback-questions"),d.id="magicfeedback-questions-"+this.appId,this.elementQuestions=(0,n.renderQuestions)(this.questions),"MAGICSURVEY"===(null===(r=this.formData)||void 0===r?void 0:r.identity)?(this.elementQuestions.forEach(((e,t)=>this.history[t]=[e])),this.questionInProcess=this.questions[0],d.appendChild(this.history[0][0])):this.elementQuestions.forEach((e=>d.appendChild(e))),a.appendChild(d),s.appendChild(a),t.addButton){const e=(0,n.renderActions)(null===(i=this.formData)||void 0===i?void 0:i.identity,(()=>this.renderBack(d)));a.appendChild(e)}a.addEventListener("submit",(e=>o(this,void 0,void 0,(function*(){e.preventDefault(),this.sendProcess(t,s,d)}))))}catch(e){return void this.log.err(e)}}sendProcess(e,t,r){var i;return o(this,void 0,void 0,(function*(){try{e.beforeSubmitEvent&&(yield e.beforeSubmitEvent({loading:!0,progress:this.progress,total:this.total}));const o=yield this.send("MAGICSURVEY"!==(null===(i=this.formData)||void 0===i?void 0:i.identity));return this.formData&&(this.formData.id=o),yield this.processNextQuestion(t,r),e.afterSubmitEvent&&(yield e.afterSubmitEvent({response:o,loading:!1,progress:this.progress,total:this.total})),o}catch(t){return this.log.err(`An error occurred while submitting the form ${this.appId}:`,t),e.afterSubmitEvent&&(yield e.afterSubmitEvent({loading:!1,progress:this.progress,total:this.total,error:t})),t}}))}answer(){const e=document.getElementById("magicfeedback-"+this.appId);if(!e)return this.log.err(`Form "${e}" not found.`),[];const t=[];return e.querySelectorAll(".magicfeedback-input").forEach((e=>{const r=e.type,o={key:e.name,value:[]},i=e.value;"radio"===r||"checkbox"===r?e.checked&&(o.value.push(i),t.push(o)):(o.value.push(i),t.push(o))})),t}send(e=!1){var t,r;return o(this,void 0,void 0,(function*(){try{const o=this.answer();if(0===o.length&&!e)throw new Error("No answers provided");const i=this.config.get("url"),n={integration:this.appId,publicKey:this.publicKey,feedback:{answers:o},completed:e};return yield(0,s.sendFeedback)(i,(null===(t=this.formData)||void 0===t?void 0:t.id)?Object.assign(Object.assign({},n),{sessionId:null===(r=this.formData)||void 0===r?void 0:r.id}):n,this.log)}catch(e){throw this.log.err(`An error occurred while submitting the form ${this.appId}:`,e),e}}))}callFollowUpQuestion(e){var t;return o(this,void 0,void 0,(function*(){if(!(null==e?void 0:e.followup))return null;try{const r=this.answer();if(0===r.length)throw new Error("No answers provided");const o=this.config.get("url"),i={answer:r[0].value[0],publicKey:this.publicKey,sessionId:null===(t=this.formData)||void 0===t?void 0:t.id,question:e};return yield(0,s.getFollowUpQuestion)(o,i,this.log)}catch(e){throw this.log.err(`An error occurred while submitting the form ${this.appId}:`,e),e}}))}processNextQuestion(e,t){var r,i;return o(this,void 0,void 0,(function*(){switch(null===(r=this.formData)||void 0===r?void 0:r.identity){case"MAGICFORM":this.total=this.questions.length,this.progress=this.questions.length;break;case"MAGICSURVEY":if(null===(i=this.questionInProcess)||void 0===i?void 0:i.followup){const r=yield this.callFollowUpQuestion(this.questionInProcess);if(r){this.questionInProcess=r,this.questions[this.progress].followupQuestion?this.questions[this.progress].followupQuestion.push(r):this.questions[this.progress].followupQuestion=[r];const e=(0,n.renderQuestions)([r])[0];this.history[this.progress].push(e),t.removeChild(t.childNodes[0]),t.appendChild(e)}else this.renderNext(t,e)}else this.renderNext(t,e);break;default:e.removeChild(e.childNodes[0]);const r=(0,n.renderSuccess)("Thank you for your feedback!");e.appendChild(r)}}))}renderNext(e,t){if(this.progress++,this.progress<this.total)this.questionInProcess=this.questions[this.progress],e.removeChild(e.childNodes[0]),e.appendChild(this.history[this.progress][0]);else{t.removeChild(t.childNodes[0]);const e=(0,n.renderSuccess)("Thank you for your feedback!");t.appendChild(e),this.send(!0)}}renderBack(e){0!==this.progress&&(e.removeChild(e.childNodes[0]),this.history[this.progress].length>1?(this.history[this.progress].pop(),this.questions[this.progress].followupQuestion.pop(),this.questionInProcess=this.questions[this.progress].followupQuestion[this.questions[this.progress].followupQuestion.length-1],e.appendChild(this.history[this.progress][this.history[this.progress].length-1])):(this.progress--,this.questionInProcess=this.questions[this.progress],e.appendChild(this.history[this.progress][0])))}}},374:(e,t)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.endpoints=void 0,t.endpoints={sdk:{app:(e,t)=>`sdk/app/${e}/${t}`,app_info:(e,t)=>`sdk/app/${e}/${t}/info`,feedback:"sdk/feedback",followUpQuestion:"sdk/followUpQuestion"}}},183:(e,t)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.renderSuccess=t.renderError=t.renderActions=t.renderQuestions=void 0,t.renderQuestions=function(e){if(!e)throw new Error("[MagicFeedback] No questions provided");const t=[];return e.forEach((e=>{const{id:r,title:o,type:i,ref:s,require:n,value:a,defaultValue:d}=e;let c,u,l=document.createElement("div");switch(l.classList.add("magicfeedback-div"),i){case"TEXT":c=document.createElement("input"),c.type="text",u="magicfeedback-text";break;case"LONGTEXT":c=document.createElement("textarea"),c.rows=3,u="magicfeedback-longtext";break;case"NUMBER":c=document.createElement("input"),c.type="number",u="magicfeedback-number",a.length&&(a.sort(((e,t)=>Number(e)-Number(t))),c.max=a[a.length-1],c.min=a[0],c.value=a[0]);break;case"RADIO":case"MULTIPLECHOICE":c=document.createElement("div"),u="magicfeedback-"+("RADIO"===i?"radio":"checkbox"),a.forEach((e=>{const t=document.createElement("div");t.classList.add(`magicfeedback-${"RADIO"===i?"radio":"checkbox"}-container`);const r=document.createElement("label"),o=document.createElement("input");o.type="RADIO"===i?"radio":"checkbox",o.name=s,o.value=e,o.required=n,o.classList.add(u),o.classList.add("magicfeedback-input"),e===d&&(o.checked=!0),r.textContent=e,t.appendChild(o),t.appendChild(r),c.appendChild(t)}));break;case"SELECT":c=document.createElement("select"),u="magicfeedback-select",a.forEach((e=>{const t=document.createElement("option");t.value=e,t.text=e,c.appendChild(t)}));break;case"DATE":c=document.createElement("input"),c.type="date",u="magicfeedback-date";break;case"BOOLEAN":c=document.createElement("input"),c.type="checkbox",u="magicfeedback-boolean";break;default:return}c.id=`magicfeedback-${r}`,c.setAttribute("name",s),void 0!==d&&(c.value=d);const h=document.createElement("label");h.setAttribute("for",`magicfeedback-${r}`),h.textContent=o,h.classList.add("magicfeedback-label"),l.appendChild(h),c.classList.add(u),"RADIO"!=i&&"MULTIPLECHOICE"!=i&&(c.classList.add("magicfeedback-input"),c.required=n),l.appendChild(c),t.push(l)})),t},t.renderActions=function(e="",t){const r=document.createElement("div");r.classList.add("magicfeedback-action-container");const o=document.createElement("button");o.id="magicfeedback-submit",o.type="submit",o.classList.add("magicfeedback-submit"),o.textContent="MAGICSURVEY"===e?"Next":"Submit";const i=document.createElement("button");return i.id="magicfeedback-back",i.type="button",i.classList.add("magicfeedback-back"),i.textContent="Back",i.addEventListener("click",t),"MAGICSURVEY"===e&&r.appendChild(i),r.appendChild(o),r},t.renderError=function(e){const t=document.createElement("div");return t.classList.add("magicfeedback-error"),t.textContent=e,t},t.renderSuccess=function(e){const t=document.createElement("div");return t.classList.add("magicfeedback-success"),t.textContent=e,t}},341:function(e,t,r){"use strict";var o=this&&this.__awaiter||function(e,t,r,o){return new(r||(r=Promise))((function(i,s){function n(e){try{d(o.next(e))}catch(e){s(e)}}function a(e){try{d(o.throw(e))}catch(e){s(e)}}function d(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(n,a)}d((o=o.apply(e,t||[])).next())}))},i=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.getFollowUpQuestion=t.sendFeedback=t.getQuestions=t.getForm=void 0;const s=i(r(98)),n=i(r(147)),a=r(374),d={Accept:"application/json","Magicfeedback-Sdk-Version":n.default.version};t.getForm=function(e,t,r,i){return o(this,void 0,void 0,(function*(){try{const o=yield(0,s.default)(e+a.endpoints.sdk.app_info(t,r),{method:"GET",headers:d});if(o.ok){const e=yield o.json();return i.log(`Received form for app ${t}`,e),e}throw i.err(`Failed to get questions for app ${t}:`,o.status,o.statusText),new Error("[MagicFeedback] Bad response from server")}catch(e){return i.err(e),null}}))},t.getQuestions=function(e,t,r,i){return o(this,void 0,void 0,(function*(){try{const o=yield(0,s.default)(e+a.endpoints.sdk.app(t,r),{method:"GET",headers:d});if(o.ok){const e=yield o.json();return i.log(`Received questions for app ${t}`,e),e}throw i.err(`Failed to get questions for app ${t}:`,o.status,o.statusText),new Error("[MagicFeedback] Bad response from server")}catch(e){return i.err(e),[]}}))},t.sendFeedback=function(e,t,r){return o(this,void 0,void 0,(function*(){try{const o=yield(0,s.default)(e+a.endpoints.sdk.feedback,{method:"POST",headers:Object.assign({"Content-Type":"application/json"},d),body:JSON.stringify(t)});if(o.ok)return r.log(`Form ${t.integration} submitted successfully!`),(yield o.json()).sessionId;throw r.err(`Failed to submit form ${t.integration}:`,o.status,o.statusText),new Error(o.statusText)}catch(e){return r.err(e),""}}))},t.getFollowUpQuestion=function(e,t,r){return o(this,void 0,void 0,(function*(){try{const o=yield(0,s.default)(e+a.endpoints.sdk.followUpQuestion,{method:"POST",headers:Object.assign({"Content-Type":"application/json"},d),body:JSON.stringify(t)});if(o.ok)return r.log(`Received follow up question for form ${t.integration}`),yield o.json();throw r.err(`Failed to get follow up question for form ${t.integration}:`,o.status,o.statusText),new Error(o.statusText)}catch(e){return r.err(e),""}}))}},5:(e,t)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.Log=void 0,t.Log=class{constructor(e){this.config=e}log(...e){this.config.get("debug")&&console.log("[MagicFeedback]:",...e)}err(...e){console.error("[MagicFeedback]:",...e)}}},147:e=>{"use strict";e.exports=JSON.parse('{"name":"@magicfeedback/native","version":"1.1.0","main":"./dist/magicfeedback-sdk.node.js","browser":"./dist/magicfeedback-sdk.browser.js","types":"./dist/types/src/index.d.ts","repository":{"type":"git","url":"git+ssh://git@github.com/MagicFeedback/magicfeedback-sdk.git"},"author":"farias@magicfeedback.io","license":"MIT","private":false,"scripts":{"dev":"vite","build":"webpack","build:watch":"webpack --watch --mode development","test":"jest","test:watch":"jest --watchAll","coverage":"vitest run --coverage"},"files":["dist"],"devDependencies":{"@babel/preset-typescript":"^7.22.5","@types/node":"^17.0.21","@types/webpack":"^5.28.0","@types/webpack-node-externals":"^2.5.3","c8":"^7.11.0","jest":"^29.5.0","jest-environment-jsdom":"^29.5.0","jest-fetch-mock":"^3.0.3","nock":"^13.2.4","ts-jest":"^29.1.0","ts-loader":"^9.2.7","ts-node":"^10.7.0","typescript":"^4.6.2","vite":"^2.8.0","vite-plugin-dts":"^0.9.9","vitest":"^0.5.9","webpack":"^5.70.0","webpack-cli":"^4.9.2","webpack-node-externals":"^3.0.0"},"dependencies":{"cross-fetch":"^3.1.5","is-bundling-for-browser-or-node":"^1.1.1"},"description":"MagicFeedbackAI JavaScript Library for [MagicFeedback.io](https://magicfeedback.io/)","bugs":{"url":"https://github.com/MagicFeedback/magicfeedback-sdk/issues"},"homepage":"https://github.com/MagicFeedback/magicfeedback-sdk#readme","directories":{"example":"examples","test":"test"}}')}},t={},function r(o){var i=t[o];if(void 0!==i)return i.exports;var s=t[o]={exports:{}};return e[o].call(s.exports,s,s.exports,r),s.exports}(607).default;var e,t}));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.magicfeedback=t():e.magicfeedback=t()}(global,(()=>(()=>{"use strict";var e={607:function(e,t,n){var i=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const a=i(n(519));let o=null;o||(o=(0,a.default)()),t.default=o},519:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(a,o){function r(e){try{s(i.next(e))}catch(e){o(e)}}function c(e){try{s(i.throw(e))}catch(e){o(e)}}function s(e){var t;e.done?a(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,c)}s((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0});const a=n(954),o=n(712),r=n(5),c=n(341);t.default=function(){const e=new o.Config;let t;return{init:function(n){(null==n?void 0:n.url)&&e.set("url",null==n?void 0:n.url),(null==n?void 0:n.debug)&&e.set("debug",null==n?void 0:n.debug),t=new r.Log(e),t.log("Initialized Magicfeedback",e)},send:function(n,a,o,r){return i(this,void 0,void 0,(function*(){n||t.err("No appID provided"),a||t.err("No publicKey provided"),o||t.err("No answers provided"),0==o.length&&t.err("Answers are empty");const i=e.get("url"),s={integration:n,publicKey:a,feedback:{answers:o,profile:r}};try{const e=yield(0,c.sendFeedback)(i,s,t);return t.log("sent native feedback"),e}catch(e){return t.err("error native feedback",e),!1}}))},form:function(n,i){return n||t.err("No appID provided"),i||t.err("No publicKey provided"),new a.Form(e,n,i)}}}},712:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Config=void 0,t.Config=class{constructor(){this.variables={},this.variables.url="https://mvp-dot-feedbackai-380013.lm.r.appspot.com/",this.variables.debug=!1}get(e){return this.variables[e]}set(e,t){this.variables[e]=t}}},954:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(a,o){function r(e){try{s(i.next(e))}catch(e){o(e)}}function c(e){try{s(i.throw(e))}catch(e){o(e)}}function s(e){var t;e.done?a(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,c)}s((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.Form=void 0;const a=n(5),o=n(341);t.Form=class{constructor(e,t,n){this.config=e,this.log=new a.Log(e),this.appId=t,this.publicKey=n}generate(e,t={}){this.log.log("Generating form for app",this.appId);const n=this.config.get("url");(0,o.getQuestions)(n,this.appId,this.publicKey,this.log).then((n=>{void 0!==n&&n?this.generateForm(this.appId,n,e,t):this.log.err(`No questions for app ${this.appId}`)}))}generateForm(e,t,n,a={}){const o=document.getElementById(n);if(!o)return void this.log.err(`Element with ID '${n}' not found.`);o.classList.add("magicfeedback-container");const r=document.createElement("form");if(r.classList.add("magicfeedback-form"),r.id="magicfeedback-"+e,t.forEach((e=>{const{id:t,title:n,type:i,ref:a,require:o,value:c,defaultValue:s}=e;let d,u,l=document.createElement("div");switch(l.classList.add("magicfeedback-div"),i){case"TEXT":d=document.createElement("input"),d.type="text",u="magicfeedback-text";break;case"LONGTEXT":d=document.createElement("textarea"),d.rows=3,u="magicfeedback-longtext";break;case"NUMBER":d=document.createElement("input"),d.type="number",u="magicfeedback-number",c.length&&(c.sort(((e,t)=>Number(e)-Number(t))),d.max=c[c.length-1],d.min=c[0],d.value=c[0]);break;case"RADIO":case"MULTIPLECHOICE":d=document.createElement("div"),u="magicfeedback-"+("RADIO"===i?"radio":"checkbox"),c.forEach((e=>{const t=document.createElement("div");t.classList.add(`magicfeedback-${"RADIO"===i?"radio":"checkbox"}-container`);const n=document.createElement("label"),r=document.createElement("input");r.type="RADIO"===i?"radio":"checkbox",r.name=a,r.value=e,r.required=o,r.classList.add(u),r.classList.add("magicfeedback-input"),e===s&&(r.checked=!0),n.textContent=e,t.appendChild(r),t.appendChild(n),d.appendChild(t)}));break;case"SELECT":d=document.createElement("select"),u="magicfeedback-select",c.forEach((e=>{const t=document.createElement("option");t.value=e,t.text=e,d.appendChild(t)}));break;case"DATE":d=document.createElement("input"),d.type="date",u="magicfeedback-date";break;case"BOOLEAN":d=document.createElement("input"),d.type="checkbox",u="magicfeedback-boolean";break;default:return}d.id=`magicfeedback-${t}`,d.setAttribute("name",a),void 0!==s&&(d.value=s);const p=document.createElement("label");p.setAttribute("for",`magicfeedback-${t}`),p.textContent=n,p.classList.add("magicfeedback-label"),l.appendChild(p),d.classList.add(u),"RADIO"!=i&&"MULTIPLECHOICE"!=i&&(d.classList.add("magicfeedback-input"),d.required=o),l.appendChild(d),r.appendChild(l)})),a.addButton){const e=document.createElement("button");e.type="submit",e.textContent="Submit",e.classList.add("magicfeedback-submit"),r.appendChild(e)}o.appendChild(r),r.addEventListener("submit",(e=>i(this,void 0,void 0,(function*(){e.preventDefault();try{a.beforeSubmitEvent&&(yield a.beforeSubmitEvent());const e=yield this.send();return a.afterSubmitEvent&&(yield a.afterSubmitEvent(e)),e}catch(e){return this.log.err(`An error occurred while submitting the form ${this.appId}:`,e),e}}))))}answer(){const e=document.getElementById("magicfeedback-"+this.appId);if(!e)return this.log.err(`Form "${e}" not found.`),[];const t=[];return e.querySelectorAll(".magicfeedback-input").forEach((e=>{const n=e.type,i={key:e.name,value:[]},a=e.value;"radio"===n||"checkbox"===n?e.checked&&(i.value.push(a),t.push(i)):(i.value.push(a),t.push(i))})),t}send(){return i(this,void 0,void 0,(function*(){try{const e=this.answer();if(0===e.length)throw new Error("No answers provided");const t=this.config.get("url"),n={integration:this.appId,publicKey:this.publicKey,feedback:{answers:e}};return yield(0,o.sendFeedback)(t,n,this.log)}catch(e){throw this.log.err(`An error occurred while submitting the form ${this.appId}:`,e),e}}))}}},374:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.endpoints=void 0,t.endpoints={sdk:{app:(e,t)=>`sdk/app/${e}/${t}`,feedback:"sdk/feedback"}}},341:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(a,o){function r(e){try{s(i.next(e))}catch(e){o(e)}}function c(e){try{s(i.throw(e))}catch(e){o(e)}}function s(e){var t;e.done?a(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,c)}s((i=i.apply(e,t||[])).next())}))},a=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.sendFeedback=t.getQuestions=void 0;const o=a(n(31)),r=a(n(147)),c=n(374),s={Accept:"application/json","Magicfeedback-Sdk-Version":r.default.version};t.getQuestions=function(e,t,n,a){return i(this,void 0,void 0,(function*(){try{const i=yield(0,o.default)(e+c.endpoints.sdk.app(t,n),{method:"GET",headers:s});if(i.ok){const e=yield i.json();return a.log(`Received questions for app ${t}`,e),e}throw a.err(`Failed to get questions for app ${t}:`,i.status,i.statusText),new Error("[MagicFeedback] Bad response from server")}catch(e){return console.error(e),[]}}))},t.sendFeedback=function(e,t,n){return i(this,void 0,void 0,(function*(){try{const i=yield(0,o.default)(e+c.endpoints.sdk.feedback,{method:"POST",headers:Object.assign({"Content-Type":"application/json"},s),body:JSON.stringify(t)});if(i.ok)return n.log(`Form ${t.integration} submitted successfully!`),!0;throw n.err(`Failed to submit form ${t.integration}:`,i.status,i.statusText),new Error(i.statusText)}catch(e){return console.error(e),!1}}))}},5:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Log=void 0,t.Log=class{constructor(e){this.config=e}log(...e){this.config.get("debug")&&console.log("[MagicFeedback]:",...e)}err(...e){console.error("[MagicFeedback]:",...e)}}},31:e=>{e.exports=require("cross-fetch")},147:e=>{e.exports=JSON.parse('{"name":"@magicfeedback/native","version":"1.0.8","main":"./dist/magicfeedback-sdk.node.js","browser":"./dist/magicfeedback-sdk.browser.js","types":"./dist/types/src/index.d.ts","repository":{"type":"git","url":"git+ssh://git@github.com/MagicFeedback/magicfeedback-sdk.git"},"author":"farias@magicfeedback.io","license":"MIT","private":false,"scripts":{"dev":"vite","build":"webpack","build:watch":"webpack --watch --mode development","test":"jest","test:watch":"jest --watchAll","coverage":"vitest run --coverage"},"files":["dist"],"devDependencies":{"@babel/preset-typescript":"^7.22.5","@types/node":"^17.0.21","@types/webpack":"^5.28.0","@types/webpack-node-externals":"^2.5.3","c8":"^7.11.0","jest":"^29.5.0","jest-environment-jsdom":"^29.5.0","jest-fetch-mock":"^3.0.3","nock":"^13.2.4","ts-jest":"^29.1.0","ts-loader":"^9.2.7","ts-node":"^10.7.0","typescript":"^4.6.2","vite":"^2.8.0","vite-plugin-dts":"^0.9.9","vitest":"^0.5.9","webpack":"^5.70.0","webpack-cli":"^4.9.2","webpack-node-externals":"^3.0.0"},"dependencies":{"cross-fetch":"^3.1.5","is-bundling-for-browser-or-node":"^1.1.1"},"description":"MagicFeedbackAI JavaScript Library for [MagicFeedback.io](https://magicfeedback.io/)","bugs":{"url":"https://github.com/MagicFeedback/magicfeedback-sdk/issues"},"homepage":"https://github.com/MagicFeedback/magicfeedback-sdk#readme","directories":{"example":"examples","test":"test"}}')}},t={},n=function n(i){var a=t[i];if(void 0!==a)return a.exports;var o=t[i]={exports:{}};return e[i].call(o.exports,o,o.exports,n),o.exports}(607);return n.default})()));
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.magicfeedback=t():e.magicfeedback=t()}(global,(()=>(()=>{"use strict";var e={607:function(e,t,i){var s=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const o=s(i(519));let r=null;r||(r=(0,o.default)()),t.default=r},519:function(e,t,i){var s=this&&this.__awaiter||function(e,t,i,s){return new(i||(i=Promise))((function(o,r){function n(e){try{d(s.next(e))}catch(e){r(e)}}function a(e){try{d(s.throw(e))}catch(e){r(e)}}function d(e){var t;e.done?o(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(n,a)}d((s=s.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0});const o=i(954),r=i(712),n=i(5),a=i(341);t.default=function(){const e=new r.Config;let t;return{init:function(i){(null==i?void 0:i.url)&&e.set("url",null==i?void 0:i.url),(null==i?void 0:i.debug)&&e.set("debug",null==i?void 0:i.debug),t=new n.Log(e),t.log("Initialized Magicfeedback",e)},send:function(i,o,r,n=!0,d,c){return s(this,void 0,void 0,(function*(){i||t.err("No appID provided"),o||t.err("No publicKey provided"),r||t.err("No feedback provided"),r.answers||r.profile||r.metrics||r.metadata||t.err("No feedback data provided");const s=e.get("url"),u={integration:i,publicKey:o,privateKey:c,completed:n,id:d,feedback:r};try{const e=yield(0,a.sendFeedback)(s,u,t);return t.log("sent native feedback"),e}catch(e){return t.err("error native feedback",e),!1}}))},form:function(i,s){return i||t.err("No appID provided"),s||t.err("No publicKey provided"),new o.Form(e,i,s)}}}},712:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Config=void 0,t.Config=class{constructor(){this.variables={},this.variables.url="https://mvp-dot-feedbackai-380013.lm.r.appspot.com/",this.variables.debug=!1}get(e){return this.variables[e]}set(e,t){this.variables[e]=t}}},954:function(e,t,i){var s=this&&this.__awaiter||function(e,t,i,s){return new(i||(i=Promise))((function(o,r){function n(e){try{d(s.next(e))}catch(e){r(e)}}function a(e){try{d(s.throw(e))}catch(e){r(e)}}function d(e){var t;e.done?o(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(n,a)}d((s=s.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.Form=void 0;const o=i(5),r=i(341),n=i(183);t.Form=class{constructor(e,t,i){this.config=e,this.log=new o.Log(e),this.appId=t,this.publicKey=i,this.url=e.get("url"),this.formData=null,this.questions=[],this.elementQuestions=[],this.questionInProcess=null,this.history={},this.progress=0,this.total=0}generate(e,t={}){return s(this,void 0,void 0,(function*(){try{if(this.formData=yield(0,r.getForm)(this.url,this.appId,this.publicKey,this.log),void 0===this.formData||!this.formData)throw new Error(`No form for app ${this.appId}`);if(this.log.log("Generating form for app",this.appId),this.questions=yield(0,r.getQuestions)(this.url,this.appId,this.publicKey,this.log),void 0===this.questions||!this.questions)throw new Error(`No questions for app ${this.appId}`);this.total=this.questions.length,t.onLoadedEvent&&(yield t.onLoadedEvent({loading:!1,progress:this.progress,total:this.total})),this.generateForm(e,t)}catch(e){return void this.log.err(e)}}))}generateForm(e,t={}){var i,o;try{const r=document.getElementById(e);if(!r)throw new Error(`Element with ID '${e}' not found.`);r.classList.add("magicfeedback-container"),r.innerHTML="";const a=document.createElement("form");a.classList.add("magicfeedback-form"),a.id="magicfeedback-"+this.appId;const d=document.createElement("div");if(d.classList.add("magicfeedback-questions"),d.id="magicfeedback-questions-"+this.appId,this.elementQuestions=(0,n.renderQuestions)(this.questions),"MAGICSURVEY"===(null===(i=this.formData)||void 0===i?void 0:i.identity)?(this.elementQuestions.forEach(((e,t)=>this.history[t]=[e])),this.questionInProcess=this.questions[0],d.appendChild(this.history[0][0])):this.elementQuestions.forEach((e=>d.appendChild(e))),a.appendChild(d),r.appendChild(a),t.addButton){const e=(0,n.renderActions)(null===(o=this.formData)||void 0===o?void 0:o.identity,(()=>this.renderBack(d)));a.appendChild(e)}a.addEventListener("submit",(e=>s(this,void 0,void 0,(function*(){e.preventDefault(),this.sendProcess(t,r,d)}))))}catch(e){return void this.log.err(e)}}sendProcess(e,t,i){var o;return s(this,void 0,void 0,(function*(){try{e.beforeSubmitEvent&&(yield e.beforeSubmitEvent({loading:!0,progress:this.progress,total:this.total}));const s=yield this.send("MAGICSURVEY"!==(null===(o=this.formData)||void 0===o?void 0:o.identity));return this.formData&&(this.formData.id=s),yield this.processNextQuestion(t,i),e.afterSubmitEvent&&(yield e.afterSubmitEvent({response:s,loading:!1,progress:this.progress,total:this.total})),s}catch(t){return this.log.err(`An error occurred while submitting the form ${this.appId}:`,t),e.afterSubmitEvent&&(yield e.afterSubmitEvent({loading:!1,progress:this.progress,total:this.total,error:t})),t}}))}answer(){const e=document.getElementById("magicfeedback-"+this.appId);if(!e)return this.log.err(`Form "${e}" not found.`),[];const t=[];return e.querySelectorAll(".magicfeedback-input").forEach((e=>{const i=e.type,s={key:e.name,value:[]},o=e.value;"radio"===i||"checkbox"===i?e.checked&&(s.value.push(o),t.push(s)):(s.value.push(o),t.push(s))})),t}send(e=!1){var t,i;return s(this,void 0,void 0,(function*(){try{const s=this.answer();if(0===s.length&&!e)throw new Error("No answers provided");const o=this.config.get("url"),n={integration:this.appId,publicKey:this.publicKey,feedback:{answers:s},completed:e};return yield(0,r.sendFeedback)(o,(null===(t=this.formData)||void 0===t?void 0:t.id)?Object.assign(Object.assign({},n),{sessionId:null===(i=this.formData)||void 0===i?void 0:i.id}):n,this.log)}catch(e){throw this.log.err(`An error occurred while submitting the form ${this.appId}:`,e),e}}))}callFollowUpQuestion(e){var t;return s(this,void 0,void 0,(function*(){if(!(null==e?void 0:e.followup))return null;try{const i=this.answer();if(0===i.length)throw new Error("No answers provided");const s=this.config.get("url"),o={answer:i[0].value[0],publicKey:this.publicKey,sessionId:null===(t=this.formData)||void 0===t?void 0:t.id,question:e};return yield(0,r.getFollowUpQuestion)(s,o,this.log)}catch(e){throw this.log.err(`An error occurred while submitting the form ${this.appId}:`,e),e}}))}processNextQuestion(e,t){var i,o;return s(this,void 0,void 0,(function*(){switch(null===(i=this.formData)||void 0===i?void 0:i.identity){case"MAGICFORM":this.total=this.questions.length,this.progress=this.questions.length;break;case"MAGICSURVEY":if(null===(o=this.questionInProcess)||void 0===o?void 0:o.followup){const i=yield this.callFollowUpQuestion(this.questionInProcess);if(i){this.questionInProcess=i,this.questions[this.progress].followupQuestion?this.questions[this.progress].followupQuestion.push(i):this.questions[this.progress].followupQuestion=[i];const e=(0,n.renderQuestions)([i])[0];this.history[this.progress].push(e),t.removeChild(t.childNodes[0]),t.appendChild(e)}else this.renderNext(t,e)}else this.renderNext(t,e);break;default:e.removeChild(e.childNodes[0]);const i=(0,n.renderSuccess)("Thank you for your feedback!");e.appendChild(i)}}))}renderNext(e,t){if(this.progress++,this.progress<this.total)this.questionInProcess=this.questions[this.progress],e.removeChild(e.childNodes[0]),e.appendChild(this.history[this.progress][0]);else{t.removeChild(t.childNodes[0]);const e=(0,n.renderSuccess)("Thank you for your feedback!");t.appendChild(e),this.send(!0)}}renderBack(e){0!==this.progress&&(e.removeChild(e.childNodes[0]),this.history[this.progress].length>1?(this.history[this.progress].pop(),this.questions[this.progress].followupQuestion.pop(),this.questionInProcess=this.questions[this.progress].followupQuestion[this.questions[this.progress].followupQuestion.length-1],e.appendChild(this.history[this.progress][this.history[this.progress].length-1])):(this.progress--,this.questionInProcess=this.questions[this.progress],e.appendChild(this.history[this.progress][0])))}}},374:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.endpoints=void 0,t.endpoints={sdk:{app:(e,t)=>`sdk/app/${e}/${t}`,app_info:(e,t)=>`sdk/app/${e}/${t}/info`,feedback:"sdk/feedback",followUpQuestion:"sdk/followUpQuestion"}}},183:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.renderSuccess=t.renderError=t.renderActions=t.renderQuestions=void 0,t.renderQuestions=function(e){if(!e)throw new Error("[MagicFeedback] No questions provided");const t=[];return e.forEach((e=>{const{id:i,title:s,type:o,ref:r,require:n,value:a,defaultValue:d}=e;let c,u,l=document.createElement("div");switch(l.classList.add("magicfeedback-div"),o){case"TEXT":c=document.createElement("input"),c.type="text",u="magicfeedback-text";break;case"LONGTEXT":c=document.createElement("textarea"),c.rows=3,u="magicfeedback-longtext";break;case"NUMBER":c=document.createElement("input"),c.type="number",u="magicfeedback-number",a.length&&(a.sort(((e,t)=>Number(e)-Number(t))),c.max=a[a.length-1],c.min=a[0],c.value=a[0]);break;case"RADIO":case"MULTIPLECHOICE":c=document.createElement("div"),u="magicfeedback-"+("RADIO"===o?"radio":"checkbox"),a.forEach((e=>{const t=document.createElement("div");t.classList.add(`magicfeedback-${"RADIO"===o?"radio":"checkbox"}-container`);const i=document.createElement("label"),s=document.createElement("input");s.type="RADIO"===o?"radio":"checkbox",s.name=r,s.value=e,s.required=n,s.classList.add(u),s.classList.add("magicfeedback-input"),e===d&&(s.checked=!0),i.textContent=e,t.appendChild(s),t.appendChild(i),c.appendChild(t)}));break;case"SELECT":c=document.createElement("select"),u="magicfeedback-select",a.forEach((e=>{const t=document.createElement("option");t.value=e,t.text=e,c.appendChild(t)}));break;case"DATE":c=document.createElement("input"),c.type="date",u="magicfeedback-date";break;case"BOOLEAN":c=document.createElement("input"),c.type="checkbox",u="magicfeedback-boolean";break;default:return}c.id=`magicfeedback-${i}`,c.setAttribute("name",r),void 0!==d&&(c.value=d);const h=document.createElement("label");h.setAttribute("for",`magicfeedback-${i}`),h.textContent=s,h.classList.add("magicfeedback-label"),l.appendChild(h),c.classList.add(u),"RADIO"!=o&&"MULTIPLECHOICE"!=o&&(c.classList.add("magicfeedback-input"),c.required=n),l.appendChild(c),t.push(l)})),t},t.renderActions=function(e="",t){const i=document.createElement("div");i.classList.add("magicfeedback-action-container");const s=document.createElement("button");s.id="magicfeedback-submit",s.type="submit",s.classList.add("magicfeedback-submit"),s.textContent="MAGICSURVEY"===e?"Next":"Submit";const o=document.createElement("button");return o.id="magicfeedback-back",o.type="button",o.classList.add("magicfeedback-back"),o.textContent="Back",o.addEventListener("click",t),"MAGICSURVEY"===e&&i.appendChild(o),i.appendChild(s),i},t.renderError=function(e){const t=document.createElement("div");return t.classList.add("magicfeedback-error"),t.textContent=e,t},t.renderSuccess=function(e){const t=document.createElement("div");return t.classList.add("magicfeedback-success"),t.textContent=e,t}},341:function(e,t,i){var s=this&&this.__awaiter||function(e,t,i,s){return new(i||(i=Promise))((function(o,r){function n(e){try{d(s.next(e))}catch(e){r(e)}}function a(e){try{d(s.throw(e))}catch(e){r(e)}}function d(e){var t;e.done?o(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(n,a)}d((s=s.apply(e,t||[])).next())}))},o=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.getFollowUpQuestion=t.sendFeedback=t.getQuestions=t.getForm=void 0;const r=o(i(31)),n=o(i(147)),a=i(374),d={Accept:"application/json","Magicfeedback-Sdk-Version":n.default.version};t.getForm=function(e,t,i,o){return s(this,void 0,void 0,(function*(){try{const s=yield(0,r.default)(e+a.endpoints.sdk.app_info(t,i),{method:"GET",headers:d});if(s.ok){const e=yield s.json();return o.log(`Received form for app ${t}`,e),e}throw o.err(`Failed to get questions for app ${t}:`,s.status,s.statusText),new Error("[MagicFeedback] Bad response from server")}catch(e){return o.err(e),null}}))},t.getQuestions=function(e,t,i,o){return s(this,void 0,void 0,(function*(){try{const s=yield(0,r.default)(e+a.endpoints.sdk.app(t,i),{method:"GET",headers:d});if(s.ok){const e=yield s.json();return o.log(`Received questions for app ${t}`,e),e}throw o.err(`Failed to get questions for app ${t}:`,s.status,s.statusText),new Error("[MagicFeedback] Bad response from server")}catch(e){return o.err(e),[]}}))},t.sendFeedback=function(e,t,i){return s(this,void 0,void 0,(function*(){try{const s=yield(0,r.default)(e+a.endpoints.sdk.feedback,{method:"POST",headers:Object.assign({"Content-Type":"application/json"},d),body:JSON.stringify(t)});if(s.ok)return i.log(`Form ${t.integration} submitted successfully!`),(yield s.json()).sessionId;throw i.err(`Failed to submit form ${t.integration}:`,s.status,s.statusText),new Error(s.statusText)}catch(e){return i.err(e),""}}))},t.getFollowUpQuestion=function(e,t,i){return s(this,void 0,void 0,(function*(){try{const s=yield(0,r.default)(e+a.endpoints.sdk.followUpQuestion,{method:"POST",headers:Object.assign({"Content-Type":"application/json"},d),body:JSON.stringify(t)});if(s.ok)return i.log(`Received follow up question for form ${t.integration}`),yield s.json();throw i.err(`Failed to get follow up question for form ${t.integration}:`,s.status,s.statusText),new Error(s.statusText)}catch(e){return i.err(e),""}}))}},5:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Log=void 0,t.Log=class{constructor(e){this.config=e}log(...e){this.config.get("debug")&&console.log("[MagicFeedback]:",...e)}err(...e){console.error("[MagicFeedback]:",...e)}}},31:e=>{e.exports=require("cross-fetch")},147:e=>{e.exports=JSON.parse('{"name":"@magicfeedback/native","version":"1.1.0","main":"./dist/magicfeedback-sdk.node.js","browser":"./dist/magicfeedback-sdk.browser.js","types":"./dist/types/src/index.d.ts","repository":{"type":"git","url":"git+ssh://git@github.com/MagicFeedback/magicfeedback-sdk.git"},"author":"farias@magicfeedback.io","license":"MIT","private":false,"scripts":{"dev":"vite","build":"webpack","build:watch":"webpack --watch --mode development","test":"jest","test:watch":"jest --watchAll","coverage":"vitest run --coverage"},"files":["dist"],"devDependencies":{"@babel/preset-typescript":"^7.22.5","@types/node":"^17.0.21","@types/webpack":"^5.28.0","@types/webpack-node-externals":"^2.5.3","c8":"^7.11.0","jest":"^29.5.0","jest-environment-jsdom":"^29.5.0","jest-fetch-mock":"^3.0.3","nock":"^13.2.4","ts-jest":"^29.1.0","ts-loader":"^9.2.7","ts-node":"^10.7.0","typescript":"^4.6.2","vite":"^2.8.0","vite-plugin-dts":"^0.9.9","vitest":"^0.5.9","webpack":"^5.70.0","webpack-cli":"^4.9.2","webpack-node-externals":"^3.0.0"},"dependencies":{"cross-fetch":"^3.1.5","is-bundling-for-browser-or-node":"^1.1.1"},"description":"MagicFeedbackAI JavaScript Library for [MagicFeedback.io](https://magicfeedback.io/)","bugs":{"url":"https://github.com/MagicFeedback/magicfeedback-sdk/issues"},"homepage":"https://github.com/MagicFeedback/magicfeedback-sdk#readme","directories":{"example":"examples","test":"test"}}')}},t={},i=function i(s){var o=t[s];if(void 0!==o)return o.exports;var r=t[s]={exports:{}};return e[s].call(r.exports,r,r.exports,i),r.exports}(607);return i.default})()));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
declare const _default: {
|
|
2
2
|
init: (options?: import("./models/types").InitOptions | undefined) => void;
|
|
3
|
-
send: (appId: string, publicKey: string,
|
|
3
|
+
send: (appId: string, publicKey: string, feedback: import("./models/types").NativeFeedback, completed?: boolean, id?: string | undefined, privateKey?: string | undefined) => Promise<string | false>;
|
|
4
4
|
form: (appId: string, publicKey: string) => import("./models/form").Form;
|
|
5
5
|
};
|
|
6
6
|
export default _default;
|
package/dist/types/src/main.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { InitOptions,
|
|
1
|
+
import { InitOptions, NativeFeedback } from "./models/types";
|
|
2
2
|
import { Form } from "./models/form";
|
|
3
3
|
/**
|
|
4
4
|
*
|
|
@@ -6,6 +6,6 @@ import { Form } from "./models/form";
|
|
|
6
6
|
*/
|
|
7
7
|
export default function main(): {
|
|
8
8
|
init: (options?: InitOptions) => void;
|
|
9
|
-
send: (appId: string, publicKey: string,
|
|
9
|
+
send: (appId: string, publicKey: string, feedback: NativeFeedback, completed?: boolean, id?: string, privateKey?: string) => Promise<string | false>;
|
|
10
10
|
form: (appId: string, publicKey: string) => Form;
|
|
11
11
|
};
|
|
@@ -1,13 +1,21 @@
|
|
|
1
|
-
import { generateFormOptions, NativeAnswer } from "./types";
|
|
1
|
+
import { generateFormOptions, NativeAnswer, NativeQuestion } from "./types";
|
|
2
2
|
import { Config } from "./config";
|
|
3
3
|
export declare class Form {
|
|
4
4
|
/**
|
|
5
5
|
* Attributes
|
|
6
6
|
*/
|
|
7
7
|
private config;
|
|
8
|
-
private log;
|
|
9
|
-
private appId;
|
|
10
|
-
private publicKey;
|
|
8
|
+
private readonly log;
|
|
9
|
+
private readonly appId;
|
|
10
|
+
private readonly publicKey;
|
|
11
|
+
private readonly url;
|
|
12
|
+
private formData;
|
|
13
|
+
questions: NativeQuestion[];
|
|
14
|
+
private questionInProcess;
|
|
15
|
+
private history;
|
|
16
|
+
private elementQuestions;
|
|
17
|
+
progress: number;
|
|
18
|
+
total: number;
|
|
11
19
|
/**
|
|
12
20
|
*
|
|
13
21
|
* @param config
|
|
@@ -21,7 +29,7 @@ export declare class Form {
|
|
|
21
29
|
* @param selector
|
|
22
30
|
* @param options
|
|
23
31
|
*/
|
|
24
|
-
generate(selector: string, options?: generateFormOptions): void
|
|
32
|
+
generate(selector: string, options?: generateFormOptions): Promise<void>;
|
|
25
33
|
/**
|
|
26
34
|
* Create
|
|
27
35
|
* @param appId
|
|
@@ -32,6 +40,11 @@ export declare class Form {
|
|
|
32
40
|
* TODO: Add option to generate in <form> or in other <tag>
|
|
33
41
|
*/
|
|
34
42
|
private generateForm;
|
|
43
|
+
/**
|
|
44
|
+
*
|
|
45
|
+
* @private
|
|
46
|
+
*/
|
|
47
|
+
private sendProcess;
|
|
35
48
|
/**
|
|
36
49
|
* Answer
|
|
37
50
|
* @param appId
|
|
@@ -43,5 +56,31 @@ export declare class Form {
|
|
|
43
56
|
* Send
|
|
44
57
|
* @returns
|
|
45
58
|
*/
|
|
46
|
-
send(): Promise<
|
|
59
|
+
send(completed?: boolean): Promise<string>;
|
|
60
|
+
/**
|
|
61
|
+
* Call follow up question
|
|
62
|
+
* @param question
|
|
63
|
+
* @private
|
|
64
|
+
*/
|
|
65
|
+
private callFollowUpQuestion;
|
|
66
|
+
/**
|
|
67
|
+
* Process next question
|
|
68
|
+
* @param container
|
|
69
|
+
* @param form
|
|
70
|
+
* @private
|
|
71
|
+
*/
|
|
72
|
+
private processNextQuestion;
|
|
73
|
+
/**
|
|
74
|
+
* Render next question
|
|
75
|
+
* @param form
|
|
76
|
+
* @param container
|
|
77
|
+
* @private
|
|
78
|
+
*/
|
|
79
|
+
private renderNext;
|
|
80
|
+
/**
|
|
81
|
+
* Render back question
|
|
82
|
+
* @param form
|
|
83
|
+
* @private
|
|
84
|
+
*/
|
|
85
|
+
private renderBack;
|
|
47
86
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare class FormData {
|
|
2
|
+
id: string;
|
|
3
|
+
name: string;
|
|
4
|
+
description: string;
|
|
5
|
+
type: string;
|
|
6
|
+
identity: string;
|
|
7
|
+
status: string;
|
|
8
|
+
createdAt: Date;
|
|
9
|
+
updatedAt: Date;
|
|
10
|
+
externalId?: string | null;
|
|
11
|
+
companyId: string;
|
|
12
|
+
productId: string;
|
|
13
|
+
userId: string;
|
|
14
|
+
setting: Record<string, any>;
|
|
15
|
+
conf: Record<string, any>;
|
|
16
|
+
constructor(id: string, name: string, description: string, type: string, identity: string, status: string, createdAt: Date, updatedAt: Date, externalId: string | null, companyId: string, productId: string, userId: string, setting: Record<string, any>, conf: Record<string, any>);
|
|
17
|
+
}
|
|
@@ -3,11 +3,6 @@ export type InitOptions = {
|
|
|
3
3
|
url?: string;
|
|
4
4
|
debug?: boolean;
|
|
5
5
|
};
|
|
6
|
-
export type NativeFeedback = {
|
|
7
|
-
appId: string;
|
|
8
|
-
answers: NativeFeedbackAnswer[];
|
|
9
|
-
profile?: NativeFeedbackProfile;
|
|
10
|
-
};
|
|
11
6
|
export type NativeFeedbackAnswer = {
|
|
12
7
|
id: string;
|
|
13
8
|
type?: FEEDBACKAPPANSWERTYPE;
|
|
@@ -36,16 +31,26 @@ export type NativeQuestion = {
|
|
|
36
31
|
value: string[];
|
|
37
32
|
defaultValue: string;
|
|
38
33
|
appId: string;
|
|
34
|
+
followup: boolean;
|
|
35
|
+
followupQuestion: NativeQuestion[];
|
|
39
36
|
};
|
|
40
37
|
export type NativeAnswer = {
|
|
41
38
|
key: string;
|
|
42
39
|
value: string[];
|
|
43
40
|
};
|
|
41
|
+
export type NativeFeedback = {
|
|
42
|
+
text: string;
|
|
43
|
+
answers: NativeAnswer[];
|
|
44
|
+
profile: NativeAnswer[];
|
|
45
|
+
metrics: NativeAnswer[];
|
|
46
|
+
metadata: NativeAnswer[];
|
|
47
|
+
};
|
|
44
48
|
export type generateFormOptions = {
|
|
45
49
|
addButton?: boolean;
|
|
46
50
|
tag?: generateFormOptionsTag;
|
|
47
51
|
afterSubmitEvent?: Function;
|
|
48
52
|
beforeSubmitEvent?: Function;
|
|
53
|
+
onLoadedEvent?: Function;
|
|
49
54
|
};
|
|
50
55
|
declare enum generateFormOptionsTag {
|
|
51
56
|
FORM = "form",
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { NativeQuestion } from "../models/types";
|
|
2
|
+
export declare function renderQuestions(appQuestions: NativeQuestion[]): HTMLElement[];
|
|
3
|
+
export declare function renderActions(identity: string | undefined, backAction: () => void): HTMLElement;
|
|
4
|
+
export declare function renderError(error: string): HTMLElement;
|
|
5
|
+
export declare function renderSuccess(success: string): HTMLElement;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { Log } from "../utils/log";
|
|
2
2
|
import { NativeQuestion } from "../models/types";
|
|
3
|
+
import { FormData } from "../models/formData";
|
|
4
|
+
export declare function getForm(url: string, appId: string, publicKey: string, log: Log): Promise<FormData | null>;
|
|
3
5
|
export declare function getQuestions(url: string, appId: string, publicKey: string, log: Log): Promise<NativeQuestion[]>;
|
|
4
|
-
export declare function sendFeedback(url: string, body: any, log: Log): Promise<
|
|
6
|
+
export declare function sendFeedback(url: string, body: any, log: Log): Promise<string>;
|
|
7
|
+
export declare function getFollowUpQuestion(url: string, body: any, log: Log): Promise<any>;
|