@magicfeedback/native 1.0.1 → 1.0.3
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 +58 -10
- package/dist/magicfeedback-sdk.browser.js +146 -1
- package/dist/magicfeedback-sdk.node.js +140 -1
- package/dist/types/src/config.d.ts +6 -1
- package/dist/types/src/form.d.ts +45 -0
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/log.d.ts +22 -0
- package/dist/types/src/main.d.ts +6 -0
- package/dist/types/src/request.d.ts +24 -0
- package/dist/types/src/types.d.ts +29 -1
- package/package.json +9 -3
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# MagicFeedbackAI SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
MagicFeedback AI JavaScript Library for [MagicFeedback.io](https://magicfeedback.io/)
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -12,7 +12,7 @@ npm i @magicfeedback/native
|
|
|
12
12
|
|
|
13
13
|
You can then require the lib like a standard Node.js module:
|
|
14
14
|
|
|
15
|
-
```
|
|
15
|
+
```js
|
|
16
16
|
var magicfeedback = require("@magicfeedback/native");
|
|
17
17
|
|
|
18
18
|
// or
|
|
@@ -25,7 +25,7 @@ import magicfeedback from "@magicfeedback/native";
|
|
|
25
25
|
|
|
26
26
|
This method is optional. You can start actived the debug mode to see on console the messages
|
|
27
27
|
|
|
28
|
-
```
|
|
28
|
+
```js
|
|
29
29
|
magicfeedback.init({
|
|
30
30
|
debug: true | false // Default false
|
|
31
31
|
})
|
|
@@ -35,14 +35,52 @@ magicfeedback.init({
|
|
|
35
35
|
## How to use
|
|
36
36
|
This guide provides instructions for utilizing various features and functionalities of the application. Each section below highlights a specific use case and provides a code snippet to demonstrate its implementation.
|
|
37
37
|
|
|
38
|
-
###
|
|
39
|
-
|
|
38
|
+
### A. Generate feedback forms
|
|
39
|
+
The feedback form generation functionality allows you to easily create and display feedback forms on your website. This section provides an overview of how to use this feature and the necessary code snippets.
|
|
40
|
+
|
|
41
|
+
To generate a feedback form, you need to include the following HTML code snippet in your web page:
|
|
42
|
+
|
|
43
|
+
```html
|
|
44
|
+
<div id="demo_form_div"></div>
|
|
45
|
+
```
|
|
46
|
+
This code snippet creates a placeholder element with the ID "demo_form_div" where the feedback form will be inserted.
|
|
47
|
+
|
|
48
|
+
Next, you need to include the following JavaScript code snippet in your application:
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
let form = window.magicfeedback.form("$_APP_ID");
|
|
52
|
+
form.generate("demo_form_div", {
|
|
53
|
+
addButton: true | false // Default false
|
|
54
|
+
beforeSubmitEvent: function(), //Function to execute before send the form
|
|
55
|
+
afterSubmitEvent: function(response), //Function to execute after send the form with the response
|
|
56
|
+
})
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
In this code snippet, you need to replace $_APP_ID with the actual ID of your feedback application. This ID is provided by the magicfeedback service.
|
|
40
60
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
61
|
+
|
|
62
|
+
The **form.generate()** function generates the feedback form inside the specified container element ("demo_form_div" in this example). You can customize the form generation by including the optional parameters:
|
|
63
|
+
|
|
64
|
+
* **addButton**: This setting determines whether to include a "Submit" button that enables users to submit the form themselves. By default, this value is set to false, indicating that the button will not be displayed.
|
|
65
|
+
* **beforeSubmitEvent**: An optional function that you can define to execute some actions or validations before the form is submitted.
|
|
66
|
+
* **afterSubmitEvent**: An optional function that you can define to execute actions after the form is submitted. This function receives the server response as a parameter.
|
|
67
|
+
|
|
68
|
+
Finally, to send the form, you can use the form.send() function.
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
form.send()
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
This function triggers the submission of the generated feedback form.
|
|
75
|
+
|
|
76
|
+

|
|
77
|
+
|
|
78
|
+
By following these steps and including the appropriate HTML and JavaScript code snippets, you can easily generate and display feedback forms on your website using the magicfeedback service.
|
|
79
|
+
|
|
80
|
+
### B. Send feedback directly
|
|
81
|
+
The "Send feedback directly" functionality allows you to send user feedback data to the server without generating a feedback form. This section provides an overview of how to use this feature and the necessary code snippets.
|
|
82
|
+
|
|
83
|
+
To send feedback directly, you need to include the following JavaScript code snippet in your application:
|
|
46
84
|
|
|
47
85
|
|
|
48
86
|
```js
|
|
@@ -64,3 +102,13 @@ const profile = { email: "farias@magicfeedback.io" };
|
|
|
64
102
|
magicfeedback.send("$_APP_ID", answers, profile);
|
|
65
103
|
|
|
66
104
|
```
|
|
105
|
+
|
|
106
|
+
In this code snippet, you need to replace $_APP_ID with the actual ID of your feedback application. This ID is provided by the magicfeedback service.
|
|
107
|
+
|
|
108
|
+
The answers array stores user feedback answer objects. Each answer object should have an id key, which represents the question or feedback category, and a value key, which holds an array of string values representing the user's response(s) to that question or category. You can add as many answer objects as needed to capture the user's feedback.
|
|
109
|
+
|
|
110
|
+
Additionally, you can create a profile object to store additional information about the user. This object can include any information you want, such as the user's email, name, or any other relevant details.
|
|
111
|
+
|
|
112
|
+
Finally, to send the user feedback data to the server, you can use the magicfeedback.send() function. This function takes three parameters: the App Id, the answers array, and the profile object. The App Id is required and identifies your feedback application within the magicfeedback service.
|
|
113
|
+
|
|
114
|
+
By following these steps and including the appropriate JavaScript code snippet, you can send user feedback data directly to the server using the magicfeedback service.
|
|
@@ -1 +1,146 @@
|
|
|
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,n="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 u(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 d(e){return"string"!=typeof e&&(e=String(e)),e}function f(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 p(e){this.map={},e instanceof p?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 h(e){if(e.bodyUsed)return Promise.reject(new TypeError("Already read"));e.bodyUsed=!0}function l(e){return new Promise((function(t,r){e.onload=function(){t(e.result)},e.onerror=function(){r(e.error)}}))}function y(e){var t=new FileReader,r=l(t);return t.readAsArrayBuffer(e),r}function b(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:n&&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&&n&&(t=e)&&DataView.prototype.isPrototypeOf(t)?(this._bodyArrayBuffer=b(e.buffer),this._bodyInit=new Blob([this._bodyArrayBuffer])):s&&(ArrayBuffer.prototype.isPrototypeOf(e)||c(e))?this._bodyArrayBuffer=b(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"))},n&&(this.blob=function(){var e=h(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?h(this)||Promise.resolve(this._bodyArrayBuffer):this.blob().then(y)}),this.text=function(){var e,t,r,o=h(this);if(o)return o;if(this._bodyBlob)return e=this._bodyBlob,r=l(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)},i&&(this.formData=function(){return this.text().then(g)}),this.json=function(){return this.text().then(JSON.parse)},this}p.prototype.append=function(e,t){e=u(e),t=d(t);var r=this.map[e];this.map[e]=r?r+", "+t:t},p.prototype.delete=function(e){delete this.map[u(e)]},p.prototype.get=function(e){return e=u(e),this.has(e)?this.map[e]:null},p.prototype.has=function(e){return this.map.hasOwnProperty(u(e))},p.prototype.set=function(e,t){this.map[u(e)]=d(t)},p.prototype.forEach=function(e,t){for(var r in this.map)this.map.hasOwnProperty(r)&&e.call(t,this.map[r],r,this)},p.prototype.keys=function(){var e=[];return this.forEach((function(t,r){e.push(r)})),f(e)},p.prototype.values=function(){var e=[];return this.forEach((function(t){e.push(t)})),f(e)},p.prototype.entries=function(){var e=[];return this.forEach((function(t,r){e.push([r,t])})),f(e)},o&&(p.prototype[Symbol.iterator]=p.prototype.entries);var v=["DELETE","GET","HEAD","OPTIONS","POST","PUT"];function w(e,t){var r,o,n=(t=t||{}).body;if(e instanceof w){if(e.bodyUsed)throw new TypeError("Already read");this.url=e.url,this.credentials=e.credentials,t.headers||(this.headers=new p(e.headers)),this.method=e.method,this.mode=e.mode,this.signal=e.signal,n||null==e._bodyInit||(n=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 p(t.headers)),this.method=(o=(r=t.method||this.method||"GET").toUpperCase(),v.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)&&n)throw new TypeError("Body not allowed for GET or HEAD requests");this._initBody(n)}function g(e){var t=new FormData;return e.trim().split("&").forEach((function(e){if(e){var r=e.split("="),o=r.shift().replace(/\+/g," "),n=r.join("=").replace(/\+/g," ");t.append(decodeURIComponent(o),decodeURIComponent(n))}})),t}function _(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 p(t.headers),this.url=t.url||"",this._initBody(e)}w.prototype.clone=function(){return new w(this,{body:this._bodyInit})},m.call(w.prototype),m.call(_.prototype),_.prototype.clone=function(){return new _(this._bodyInit,{status:this.status,statusText:this.statusText,headers:new p(this.headers),url:this.url})},_.error=function(){var e=new _(null,{status:0,statusText:""});return e.type="error",e};var x=[301,302,303,307,308];_.redirect=function(e,t){if(-1===x.indexOf(t))throw new RangeError("Invalid status code");return new _(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 E(e,r){return new Promise((function(o,i){var s=new w(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 p,e.replace(/\r?\n[\t ]+/g," ").split(/\r?\n/).forEach((function(e){var r=e.split(":"),o=r.shift().trim();if(o){var n=r.join(":").trim();t.append(o,n)}})),t)};r.url="responseURL"in a?a.responseURL:r.headers.get("X-Request-URL");var n="response"in a?a.response:a.responseText;o(new _(n,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&&n&&(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)}))}E.polyfill=!0,e.fetch||(e.fetch=E,e.Headers=p,e.Request=w,e.Response=_),t.Headers=p,t.Request=w,t.Response=_,t.fetch=E,Object.defineProperty(t,"__esModule",{value:!0})}({})}(o),o.fetch.ponyfill=!0,delete o.fetch.polyfill;var n=o;(t=n.fetch).default=n.fetch,t.fetch=n.fetch,t.Headers=n.Headers,t.Request=n.Request,t.Response=n.Response,e.exports=t},913:(e,t)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.API_URL=void 0,t.API_URL="https://api.magicfeedback.io/"},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 n=o(r(519));let i=null;i||(i=(0,n.default)()),t.default=i},519:function(e,t,r){"use strict";var o=this&&this.__awaiter||function(e,t,r,o){return new(r||(r=Promise))((function(n,i){function s(e){try{c(o.next(e))}catch(e){i(e)}}function a(e){try{c(o.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?n(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(s,a)}c((o=o.apply(e,t||[])).next())}))},n=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const i=n(r(98)),s=n(r(147)),a=r(913);t.default=function(){let e=a.API_URL,t=!1;function r(...e){t&&console.log("[MagicFeedback]:",...e)}function n(...e){throw t&&console.error("[MagicFeedback]:",...e),new Error(...e)}return{init:function(o){(null==o?void 0:o.url)&&(e=null==o?void 0:o.url),(null==o?void 0:o.debug)&&(t=null==o?void 0:o.debug),r("Initialized Magicfeedback",o)},send:function(t,a,c){return o(this,void 0,void 0,(function*(){t||n("No appID provided"),a||n("No answers provided"),0==a.length&&n("Answers are empty");const u={appId:t,answers:a};c&&(u.profile=c);let d={};try{d=yield function(e,t){return o(this,void 0,void 0,(function*(){return(0,i.default)(e,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json","Magicfeedback-Sdk-Version":s.default.version},body:JSON.stringify(t)})}))}(`${e}/feedback/apps`,u),r("sent native feedback",d)}catch(e){n("error native feedback",e)}return d}))}}}},147:e=>{"use strict";e.exports=JSON.parse('{"name":"@magicfeedback/native","version":"1.0.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","test":"vitest","test:ci":"vitest --reporter=junit --outputFile=junit.xml","coverage":"vitest run --coverage"},"files":["dist"],"devDependencies":{"@types/node":"^17.0.21","@types/webpack":"^5.28.0","@types/webpack-node-externals":"^2.5.3","c8":"^7.11.0","nock":"^13.2.4","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 n=t[o];if(void 0!==n)return n.exports;var i=t[o]={exports:{}};return e[o].call(i.exports,i,i.exports,r),i.exports}(607).default;var e,t}));
|
|
1
|
+
/*
|
|
2
|
+
* ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development").
|
|
3
|
+
* This devtool is neither made for production nor for readable output files.
|
|
4
|
+
* It uses "eval()" calls to create a separate source file in the browser devtools.
|
|
5
|
+
* If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
|
|
6
|
+
* or disable the default devtool with "devtool: false".
|
|
7
|
+
* If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
|
|
8
|
+
*/
|
|
9
|
+
(function webpackUniversalModuleDefinition(root, factory) {
|
|
10
|
+
if(typeof exports === 'object' && typeof module === 'object')
|
|
11
|
+
module.exports = factory();
|
|
12
|
+
else if(typeof define === 'function' && define.amd)
|
|
13
|
+
define([], factory);
|
|
14
|
+
else if(typeof exports === 'object')
|
|
15
|
+
exports["magicfeedback"] = factory();
|
|
16
|
+
else
|
|
17
|
+
root["magicfeedback"] = factory();
|
|
18
|
+
})(self, () => {
|
|
19
|
+
return /******/ (() => { // webpackBootstrap
|
|
20
|
+
/******/ var __webpack_modules__ = ({
|
|
21
|
+
|
|
22
|
+
/***/ "./node_modules/cross-fetch/dist/browser-ponyfill.js":
|
|
23
|
+
/*!***********************************************************!*\
|
|
24
|
+
!*** ./node_modules/cross-fetch/dist/browser-ponyfill.js ***!
|
|
25
|
+
\***********************************************************/
|
|
26
|
+
/***/ (function(module, exports) {
|
|
27
|
+
|
|
28
|
+
eval("var global = typeof self !== 'undefined' ? self : this;\nvar __self__ = (function () {\nfunction F() {\nthis.fetch = false;\nthis.DOMException = global.DOMException\n}\nF.prototype = global;\nreturn new F();\n})();\n(function(self) {\n\nvar irrelevant = (function (exports) {\n\n var support = {\n searchParams: 'URLSearchParams' in self,\n iterable: 'Symbol' in self && 'iterator' in Symbol,\n blob:\n 'FileReader' in self &&\n 'Blob' in self &&\n (function() {\n try {\n new Blob();\n return true\n } catch (e) {\n return false\n }\n })(),\n formData: 'FormData' in self,\n arrayBuffer: 'ArrayBuffer' in self\n };\n\n function isDataView(obj) {\n return obj && DataView.prototype.isPrototypeOf(obj)\n }\n\n if (support.arrayBuffer) {\n var viewClasses = [\n '[object Int8Array]',\n '[object Uint8Array]',\n '[object Uint8ClampedArray]',\n '[object Int16Array]',\n '[object Uint16Array]',\n '[object Int32Array]',\n '[object Uint32Array]',\n '[object Float32Array]',\n '[object Float64Array]'\n ];\n\n var isArrayBufferView =\n ArrayBuffer.isView ||\n function(obj) {\n return obj && viewClasses.indexOf(Object.prototype.toString.call(obj)) > -1\n };\n }\n\n function normalizeName(name) {\n if (typeof name !== 'string') {\n name = String(name);\n }\n if (/[^a-z0-9\\-#$%&'*+.^_`|~]/i.test(name)) {\n throw new TypeError('Invalid character in header field name')\n }\n return name.toLowerCase()\n }\n\n function normalizeValue(value) {\n if (typeof value !== 'string') {\n value = String(value);\n }\n return value\n }\n\n // Build a destructive iterator for the value list\n function iteratorFor(items) {\n var iterator = {\n next: function() {\n var value = items.shift();\n return {done: value === undefined, value: value}\n }\n };\n\n if (support.iterable) {\n iterator[Symbol.iterator] = function() {\n return iterator\n };\n }\n\n return iterator\n }\n\n function Headers(headers) {\n this.map = {};\n\n if (headers instanceof Headers) {\n headers.forEach(function(value, name) {\n this.append(name, value);\n }, this);\n } else if (Array.isArray(headers)) {\n headers.forEach(function(header) {\n this.append(header[0], header[1]);\n }, this);\n } else if (headers) {\n Object.getOwnPropertyNames(headers).forEach(function(name) {\n this.append(name, headers[name]);\n }, this);\n }\n }\n\n Headers.prototype.append = function(name, value) {\n name = normalizeName(name);\n value = normalizeValue(value);\n var oldValue = this.map[name];\n this.map[name] = oldValue ? oldValue + ', ' + value : value;\n };\n\n Headers.prototype['delete'] = function(name) {\n delete this.map[normalizeName(name)];\n };\n\n Headers.prototype.get = function(name) {\n name = normalizeName(name);\n return this.has(name) ? this.map[name] : null\n };\n\n Headers.prototype.has = function(name) {\n return this.map.hasOwnProperty(normalizeName(name))\n };\n\n Headers.prototype.set = function(name, value) {\n this.map[normalizeName(name)] = normalizeValue(value);\n };\n\n Headers.prototype.forEach = function(callback, thisArg) {\n for (var name in this.map) {\n if (this.map.hasOwnProperty(name)) {\n callback.call(thisArg, this.map[name], name, this);\n }\n }\n };\n\n Headers.prototype.keys = function() {\n var items = [];\n this.forEach(function(value, name) {\n items.push(name);\n });\n return iteratorFor(items)\n };\n\n Headers.prototype.values = function() {\n var items = [];\n this.forEach(function(value) {\n items.push(value);\n });\n return iteratorFor(items)\n };\n\n Headers.prototype.entries = function() {\n var items = [];\n this.forEach(function(value, name) {\n items.push([name, value]);\n });\n return iteratorFor(items)\n };\n\n if (support.iterable) {\n Headers.prototype[Symbol.iterator] = Headers.prototype.entries;\n }\n\n function consumed(body) {\n if (body.bodyUsed) {\n return Promise.reject(new TypeError('Already read'))\n }\n body.bodyUsed = true;\n }\n\n function fileReaderReady(reader) {\n return new Promise(function(resolve, reject) {\n reader.onload = function() {\n resolve(reader.result);\n };\n reader.onerror = function() {\n reject(reader.error);\n };\n })\n }\n\n function readBlobAsArrayBuffer(blob) {\n var reader = new FileReader();\n var promise = fileReaderReady(reader);\n reader.readAsArrayBuffer(blob);\n return promise\n }\n\n function readBlobAsText(blob) {\n var reader = new FileReader();\n var promise = fileReaderReady(reader);\n reader.readAsText(blob);\n return promise\n }\n\n function readArrayBufferAsText(buf) {\n var view = new Uint8Array(buf);\n var chars = new Array(view.length);\n\n for (var i = 0; i < view.length; i++) {\n chars[i] = String.fromCharCode(view[i]);\n }\n return chars.join('')\n }\n\n function bufferClone(buf) {\n if (buf.slice) {\n return buf.slice(0)\n } else {\n var view = new Uint8Array(buf.byteLength);\n view.set(new Uint8Array(buf));\n return view.buffer\n }\n }\n\n function Body() {\n this.bodyUsed = false;\n\n this._initBody = function(body) {\n this._bodyInit = body;\n if (!body) {\n this._bodyText = '';\n } else if (typeof body === 'string') {\n this._bodyText = body;\n } else if (support.blob && Blob.prototype.isPrototypeOf(body)) {\n this._bodyBlob = body;\n } else if (support.formData && FormData.prototype.isPrototypeOf(body)) {\n this._bodyFormData = body;\n } else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) {\n this._bodyText = body.toString();\n } else if (support.arrayBuffer && support.blob && isDataView(body)) {\n this._bodyArrayBuffer = bufferClone(body.buffer);\n // IE 10-11 can't handle a DataView body.\n this._bodyInit = new Blob([this._bodyArrayBuffer]);\n } else if (support.arrayBuffer && (ArrayBuffer.prototype.isPrototypeOf(body) || isArrayBufferView(body))) {\n this._bodyArrayBuffer = bufferClone(body);\n } else {\n this._bodyText = body = Object.prototype.toString.call(body);\n }\n\n if (!this.headers.get('content-type')) {\n if (typeof body === 'string') {\n this.headers.set('content-type', 'text/plain;charset=UTF-8');\n } else if (this._bodyBlob && this._bodyBlob.type) {\n this.headers.set('content-type', this._bodyBlob.type);\n } else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) {\n this.headers.set('content-type', 'application/x-www-form-urlencoded;charset=UTF-8');\n }\n }\n };\n\n if (support.blob) {\n this.blob = function() {\n var rejected = consumed(this);\n if (rejected) {\n return rejected\n }\n\n if (this._bodyBlob) {\n return Promise.resolve(this._bodyBlob)\n } else if (this._bodyArrayBuffer) {\n return Promise.resolve(new Blob([this._bodyArrayBuffer]))\n } else if (this._bodyFormData) {\n throw new Error('could not read FormData body as blob')\n } else {\n return Promise.resolve(new Blob([this._bodyText]))\n }\n };\n\n this.arrayBuffer = function() {\n if (this._bodyArrayBuffer) {\n return consumed(this) || Promise.resolve(this._bodyArrayBuffer)\n } else {\n return this.blob().then(readBlobAsArrayBuffer)\n }\n };\n }\n\n this.text = function() {\n var rejected = consumed(this);\n if (rejected) {\n return rejected\n }\n\n if (this._bodyBlob) {\n return readBlobAsText(this._bodyBlob)\n } else if (this._bodyArrayBuffer) {\n return Promise.resolve(readArrayBufferAsText(this._bodyArrayBuffer))\n } else if (this._bodyFormData) {\n throw new Error('could not read FormData body as text')\n } else {\n return Promise.resolve(this._bodyText)\n }\n };\n\n if (support.formData) {\n this.formData = function() {\n return this.text().then(decode)\n };\n }\n\n this.json = function() {\n return this.text().then(JSON.parse)\n };\n\n return this\n }\n\n // HTTP methods whose capitalization should be normalized\n var methods = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT'];\n\n function normalizeMethod(method) {\n var upcased = method.toUpperCase();\n return methods.indexOf(upcased) > -1 ? upcased : method\n }\n\n function Request(input, options) {\n options = options || {};\n var body = options.body;\n\n if (input instanceof Request) {\n if (input.bodyUsed) {\n throw new TypeError('Already read')\n }\n this.url = input.url;\n this.credentials = input.credentials;\n if (!options.headers) {\n this.headers = new Headers(input.headers);\n }\n this.method = input.method;\n this.mode = input.mode;\n this.signal = input.signal;\n if (!body && input._bodyInit != null) {\n body = input._bodyInit;\n input.bodyUsed = true;\n }\n } else {\n this.url = String(input);\n }\n\n this.credentials = options.credentials || this.credentials || 'same-origin';\n if (options.headers || !this.headers) {\n this.headers = new Headers(options.headers);\n }\n this.method = normalizeMethod(options.method || this.method || 'GET');\n this.mode = options.mode || this.mode || null;\n this.signal = options.signal || this.signal;\n this.referrer = null;\n\n if ((this.method === 'GET' || this.method === 'HEAD') && body) {\n throw new TypeError('Body not allowed for GET or HEAD requests')\n }\n this._initBody(body);\n }\n\n Request.prototype.clone = function() {\n return new Request(this, {body: this._bodyInit})\n };\n\n function decode(body) {\n var form = new FormData();\n body\n .trim()\n .split('&')\n .forEach(function(bytes) {\n if (bytes) {\n var split = bytes.split('=');\n var name = split.shift().replace(/\\+/g, ' ');\n var value = split.join('=').replace(/\\+/g, ' ');\n form.append(decodeURIComponent(name), decodeURIComponent(value));\n }\n });\n return form\n }\n\n function parseHeaders(rawHeaders) {\n var headers = new Headers();\n // Replace instances of \\r\\n and \\n followed by at least one space or horizontal tab with a space\n // https://tools.ietf.org/html/rfc7230#section-3.2\n var preProcessedHeaders = rawHeaders.replace(/\\r?\\n[\\t ]+/g, ' ');\n preProcessedHeaders.split(/\\r?\\n/).forEach(function(line) {\n var parts = line.split(':');\n var key = parts.shift().trim();\n if (key) {\n var value = parts.join(':').trim();\n headers.append(key, value);\n }\n });\n return headers\n }\n\n Body.call(Request.prototype);\n\n function Response(bodyInit, options) {\n if (!options) {\n options = {};\n }\n\n this.type = 'default';\n this.status = options.status === undefined ? 200 : options.status;\n this.ok = this.status >= 200 && this.status < 300;\n this.statusText = 'statusText' in options ? options.statusText : 'OK';\n this.headers = new Headers(options.headers);\n this.url = options.url || '';\n this._initBody(bodyInit);\n }\n\n Body.call(Response.prototype);\n\n Response.prototype.clone = function() {\n return new Response(this._bodyInit, {\n status: this.status,\n statusText: this.statusText,\n headers: new Headers(this.headers),\n url: this.url\n })\n };\n\n Response.error = function() {\n var response = new Response(null, {status: 0, statusText: ''});\n response.type = 'error';\n return response\n };\n\n var redirectStatuses = [301, 302, 303, 307, 308];\n\n Response.redirect = function(url, status) {\n if (redirectStatuses.indexOf(status) === -1) {\n throw new RangeError('Invalid status code')\n }\n\n return new Response(null, {status: status, headers: {location: url}})\n };\n\n exports.DOMException = self.DOMException;\n try {\n new exports.DOMException();\n } catch (err) {\n exports.DOMException = function(message, name) {\n this.message = message;\n this.name = name;\n var error = Error(message);\n this.stack = error.stack;\n };\n exports.DOMException.prototype = Object.create(Error.prototype);\n exports.DOMException.prototype.constructor = exports.DOMException;\n }\n\n function fetch(input, init) {\n return new Promise(function(resolve, reject) {\n var request = new Request(input, init);\n\n if (request.signal && request.signal.aborted) {\n return reject(new exports.DOMException('Aborted', 'AbortError'))\n }\n\n var xhr = new XMLHttpRequest();\n\n function abortXhr() {\n xhr.abort();\n }\n\n xhr.onload = function() {\n var options = {\n status: xhr.status,\n statusText: xhr.statusText,\n headers: parseHeaders(xhr.getAllResponseHeaders() || '')\n };\n options.url = 'responseURL' in xhr ? xhr.responseURL : options.headers.get('X-Request-URL');\n var body = 'response' in xhr ? xhr.response : xhr.responseText;\n resolve(new Response(body, options));\n };\n\n xhr.onerror = function() {\n reject(new TypeError('Network request failed'));\n };\n\n xhr.ontimeout = function() {\n reject(new TypeError('Network request failed'));\n };\n\n xhr.onabort = function() {\n reject(new exports.DOMException('Aborted', 'AbortError'));\n };\n\n xhr.open(request.method, request.url, true);\n\n if (request.credentials === 'include') {\n xhr.withCredentials = true;\n } else if (request.credentials === 'omit') {\n xhr.withCredentials = false;\n }\n\n if ('responseType' in xhr && support.blob) {\n xhr.responseType = 'blob';\n }\n\n request.headers.forEach(function(value, name) {\n xhr.setRequestHeader(name, value);\n });\n\n if (request.signal) {\n request.signal.addEventListener('abort', abortXhr);\n\n xhr.onreadystatechange = function() {\n // DONE (success or failure)\n if (xhr.readyState === 4) {\n request.signal.removeEventListener('abort', abortXhr);\n }\n };\n }\n\n xhr.send(typeof request._bodyInit === 'undefined' ? null : request._bodyInit);\n })\n }\n\n fetch.polyfill = true;\n\n if (!self.fetch) {\n self.fetch = fetch;\n self.Headers = Headers;\n self.Request = Request;\n self.Response = Response;\n }\n\n exports.Headers = Headers;\n exports.Request = Request;\n exports.Response = Response;\n exports.fetch = fetch;\n\n Object.defineProperty(exports, '__esModule', { value: true });\n\n return exports;\n\n})({});\n})(__self__);\n__self__.fetch.ponyfill = true;\n// Remove \"polyfill\" property added by whatwg-fetch\ndelete __self__.fetch.polyfill;\n// Choose between native implementation (global) or custom implementation (__self__)\n// var ctx = global.fetch ? global : __self__;\nvar ctx = __self__; // this line disable service worker support temporarily\nexports = ctx.fetch // To enable: import fetch from 'cross-fetch'\nexports[\"default\"] = ctx.fetch // For TypeScript consumers without esModuleInterop.\nexports.fetch = ctx.fetch // To enable: import {fetch} from 'cross-fetch'\nexports.Headers = ctx.Headers\nexports.Request = ctx.Request\nexports.Response = ctx.Response\nmodule.exports = exports\n\n\n//# sourceURL=webpack://magicfeedback/./node_modules/cross-fetch/dist/browser-ponyfill.js?");
|
|
29
|
+
|
|
30
|
+
/***/ }),
|
|
31
|
+
|
|
32
|
+
/***/ "./src/config.ts":
|
|
33
|
+
/*!***********************!*\
|
|
34
|
+
!*** ./src/config.ts ***!
|
|
35
|
+
\***********************/
|
|
36
|
+
/***/ ((__unused_webpack_module, exports) => {
|
|
37
|
+
|
|
38
|
+
"use strict";
|
|
39
|
+
eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.Config = void 0;\nclass Config {\n constructor() {\n this.variables = {};\n this.variables[\"url\"] = \"https://api.magicfeedback.io\";\n this.variables[\"debug\"] = false;\n }\n get(key) {\n return this.variables[key];\n }\n set(key, value) {\n this.variables[key] = value;\n }\n}\nexports.Config = Config;\n\n\n//# sourceURL=webpack://magicfeedback/./src/config.ts?");
|
|
40
|
+
|
|
41
|
+
/***/ }),
|
|
42
|
+
|
|
43
|
+
/***/ "./src/form.ts":
|
|
44
|
+
/*!*********************!*\
|
|
45
|
+
!*** ./src/form.ts ***!
|
|
46
|
+
\*********************/
|
|
47
|
+
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {
|
|
48
|
+
|
|
49
|
+
"use strict";
|
|
50
|
+
eval("\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.Form = void 0;\nconst request_1 = __webpack_require__(/*! ./request */ \"./src/request.ts\");\nconst log_1 = __webpack_require__(/*! ./log */ \"./src/log.ts\");\nclass Form {\n /**\n *\n * @param config\n * @param appId\n */\n constructor(config, appId) {\n // Config\n this.config = config;\n this.request = new request_1.Request();\n this.log = new log_1.Log(config);\n // Attributes\n this.appId = appId;\n }\n /**\n * Generate\n * TODO: Check if is inside of a <form>\n * @param appId\n */\n generate(selector, options = {}) {\n //TODO: Check if already exist the form\n // Request question from the app\n this.request\n .get(`${this.config.get(\"url\")}/apps/${this.appId}/questions`, {})\n .then((appQuestions) => {\n if (appQuestions === undefined || !appQuestions) {\n this.log.err(`No questions for app ${this.appId}`);\n return;\n }\n // Create the from from the JSON\n this.generateForm(this.appId, appQuestions, selector, options);\n });\n }\n /**\n * Create\n * @param appId\n * @param appQuestions\n * @param selector\n * @param options\n *\n * TODO: Add option to generate in <form> or in other <tag>\n */\n generateForm(appId, appQuestions, selector, options = {}) {\n // Select the container\n const container = document.getElementById(selector);\n if (!container) {\n this.log.err(`Element with ID '${selector}' not found.`);\n return;\n }\n container.classList.add(\"magicfeedback-container\");\n // Create the form\n const form = document.createElement(\"form\");\n form.classList.add(\"magicfeedback-form\");\n form.id = \"magicfeedback-\" + appId;\n // Process questions and create in the form\n appQuestions.forEach((question) => {\n const { id, title, type, ref, require, \n //external_id,\n value, defaultValue, } = question;\n let element;\n let elementTypeClass;\n let elementContainer = document.createElement(\"div\");\n elementContainer.classList.add(\"magicfeedback-div\");\n switch (type) {\n case \"TEXT\":\n // Create a text input field\n element = document.createElement(\"input\");\n element.type = \"text\";\n elementTypeClass = \"magicfeedback-text\";\n break;\n case \"LONGTEXT\":\n // Create a textarea element for TEXT and LONGTEXT types\n element = document.createElement(\"textarea\");\n element.rows = 3; // Set the number of rows based on the type\n elementTypeClass = \"magicfeedback-longtext\";\n break;\n case \"NUMBER\":\n // Create an input element with type \"number\" for NUMBER type\n element = document.createElement(\"input\");\n element.type = \"number\";\n elementTypeClass = \"magicfeedback-number\";\n if (value.length) {\n value.sort((a, b) => {\n let aa = Number(a);\n let bb = Number(b);\n return aa - bb;\n });\n element.max = value[value.length - 1];\n element.min = value[0];\n element.value = value[0];\n }\n break;\n case \"RADIO\":\n case \"MULTIPLECHOICE\":\n element = document.createElement(\"div\");\n elementTypeClass =\n \"magicfeedback-\" + (type === \"RADIO\" ? \"radio\" : \"checkbox\");\n value.forEach((option) => {\n const label = document.createElement(\"label\");\n const input = document.createElement(\"input\");\n input.type = type === \"RADIO\" ? \"radio\" : \"checkbox\";\n input.name = ref;\n input.value = option;\n input.required = require;\n input.classList.add(elementTypeClass);\n input.classList.add(\"magicfeedback-input\");\n if (option === defaultValue) {\n input.checked = true;\n }\n label.textContent = option;\n element.appendChild(input);\n element.appendChild(label);\n });\n break;\n case \"SELECT\":\n // Create a select element for RADIO and MULTIPLECHOICE types\n element = document.createElement(\"select\");\n elementTypeClass = \"magicfeedback-select\";\n value.forEach((optionValue) => {\n // Create an option element for each value in the question's value array\n const option = document.createElement(\"option\");\n option.value = optionValue;\n option.text = optionValue;\n element.appendChild(option);\n });\n break;\n case \"DATE\":\n // Create an input element with type \"date\" for DATE type\n element = document.createElement(\"input\");\n element.type = \"date\";\n elementTypeClass = \"magicfeedback-date\";\n break;\n case \"BOOLEAN\":\n // Create an input element with type \"checkbox\" for BOOLEAN type\n element = document.createElement(\"input\");\n element.type = \"checkbox\";\n elementTypeClass = \"magicfeedback-boolean\";\n break;\n default:\n return; // Skip unknown types\n }\n element.id = `magicfeedback-${id}`;\n element.setAttribute(\"name\", ref);\n if (defaultValue !== undefined) {\n element.value = defaultValue;\n }\n // Add the label and input element to the form\n const label = document.createElement(\"label\");\n label.setAttribute(\"for\", `magicfeedback-${id}`);\n label.textContent = title;\n label.classList.add(\"magicfeedback-label\");\n elementContainer.appendChild(label);\n element.classList.add(elementTypeClass);\n if (type != \"RADIO\" && type != \"MULTIPLECHOICE\") {\n element.classList.add(\"magicfeedback-input\");\n element.required = require;\n }\n elementContainer.appendChild(element);\n form.appendChild(elementContainer);\n });\n // Submit button\n if (options.addButton) {\n // Create a submit button if specified in options\n const submitButton = document.createElement(\"button\");\n submitButton.type = \"submit\";\n submitButton.textContent = \"Submit\";\n submitButton.classList.add(\"magicfeedback-submit\");\n form.appendChild(submitButton);\n }\n // Add the form to the specified container\n container.appendChild(form);\n // Submit event\n form.addEventListener(\"submit\", (event) => __awaiter(this, void 0, void 0, function* () {\n event.preventDefault();\n try {\n // BEFORE\n if (options.beforeSubmitEvent) {\n yield options.beforeSubmitEvent();\n }\n // SEND\n const response = yield this.send();\n // AFTER\n if (options.afterSubmitEvent) {\n yield options.afterSubmitEvent(response);\n }\n return response;\n }\n catch (error) {\n // Handle error in beforeSubmitEvent, send(), or afterSubmitEvent\n this.log.err(`An error occurred while submitting the form ${this.appId}:`, error);\n // You can perform error handling logic here if needed\n return error;\n }\n }));\n }\n /**\n * Answer\n * @param appId\n * @returns\n * TODO: Required\n */\n answer() {\n const form = document.getElementById(\"magicfeedback-\" + this.appId);\n if (!form) {\n this.log.err(`Form \"${form}\" not found.`);\n return [];\n }\n const surveyAnswers = [];\n let hasError = false; // Flag to track if an error has occurred\n const inputs = form.querySelectorAll(\".magicfeedback-input\");\n inputs.forEach((input) => {\n const inputType = input.type;\n //const required = (input as HTMLInputElement).required;\n const ans = {\n id: input.name,\n type: inputType,\n value: [],\n };\n const value = input.value;\n if (inputType === \"radio\" || inputType === \"checkbox\") {\n if (input.checked) {\n ans.value.push(value);\n surveyAnswers.push(ans);\n }\n }\n else {\n ans.value.push(value);\n surveyAnswers.push(ans);\n }\n });\n if (hasError) {\n return []; // Stop the process if there's an error\n }\n return surveyAnswers;\n }\n /**\n * Send\n * @returns\n */\n send() {\n return __awaiter(this, void 0, void 0, function* () {\n // Define the URL and request payload\n const url = `${this.config.get(\"url\")}/feedback/apps`;\n try {\n // Get the survey answers from the answer() function\n const surveyAnswers = this.answer();\n if (surveyAnswers.length === 0) {\n throw new Error(\"No answers provided\");\n }\n // Make the AJAX POST request\n const response = yield this.request.post(url, {\n appId: this.appId,\n answers: surveyAnswers,\n });\n if (response.ok) {\n // Handle success response\n this.log.log(`Form ${this.appId} submitted successfully!`);\n // You can perform additional actions here if needed\n }\n else {\n // Handle error response\n this.log.err(`Failed to submit form ${this.appId}:`, response.status, response.statusText);\n throw new Error(response.statusText);\n }\n return response;\n }\n catch (error) {\n // Handle network or request error\n this.log.err(`An error occurred while submitting the form ${this.appId}:`, error);\n // You can perform error handling logic here if needed\n throw error;\n }\n });\n }\n}\nexports.Form = Form;\n\n\n//# sourceURL=webpack://magicfeedback/./src/form.ts?");
|
|
51
|
+
|
|
52
|
+
/***/ }),
|
|
53
|
+
|
|
54
|
+
/***/ "./src/index.ts":
|
|
55
|
+
/*!**********************!*\
|
|
56
|
+
!*** ./src/index.ts ***!
|
|
57
|
+
\**********************/
|
|
58
|
+
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {
|
|
59
|
+
|
|
60
|
+
"use strict";
|
|
61
|
+
eval("\nvar __importDefault = (this && this.__importDefault) || function (mod) {\n return (mod && mod.__esModule) ? mod : { \"default\": mod };\n};\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nconst main_1 = __importDefault(__webpack_require__(/*! ./main */ \"./src/main.ts\"));\nlet instance = null;\nif (!instance) {\n instance = (0, main_1.default)();\n}\nexports[\"default\"] = instance;\n\n\n//# sourceURL=webpack://magicfeedback/./src/index.ts?");
|
|
62
|
+
|
|
63
|
+
/***/ }),
|
|
64
|
+
|
|
65
|
+
/***/ "./src/log.ts":
|
|
66
|
+
/*!********************!*\
|
|
67
|
+
!*** ./src/log.ts ***!
|
|
68
|
+
\********************/
|
|
69
|
+
/***/ ((__unused_webpack_module, exports) => {
|
|
70
|
+
|
|
71
|
+
"use strict";
|
|
72
|
+
eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.Log = void 0;\nclass Log {\n /**\n *\n * @param config\n */\n constructor(config) {\n // Config\n this.config = config;\n }\n /**\n *\n * @param args\n */\n log(...args) {\n if (this.config.get(\"debug\")) {\n console.log(\"[MagicFeedback]:\", ...args);\n }\n }\n /**\n *\n * @param args\n */\n err(...args) {\n console.error(\"[MagicFeedback]:\", ...args);\n }\n}\nexports.Log = Log;\n\n\n//# sourceURL=webpack://magicfeedback/./src/log.ts?");
|
|
73
|
+
|
|
74
|
+
/***/ }),
|
|
75
|
+
|
|
76
|
+
/***/ "./src/main.ts":
|
|
77
|
+
/*!*********************!*\
|
|
78
|
+
!*** ./src/main.ts ***!
|
|
79
|
+
\*********************/
|
|
80
|
+
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {
|
|
81
|
+
|
|
82
|
+
"use strict";
|
|
83
|
+
eval("\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nconst request_1 = __webpack_require__(/*! ./request */ \"./src/request.ts\");\nconst form_1 = __webpack_require__(/*! ./form */ \"./src/form.ts\");\nconst config_1 = __webpack_require__(/*! ./config */ \"./src/config.ts\");\nconst log_1 = __webpack_require__(/*! ./log */ \"./src/log.ts\");\n/**\n *\n * @returns\n */\nfunction main() {\n //===============================================\n // Attributes\n //===============================================\n const config = new config_1.Config();\n const request = new request_1.Request();\n let log;\n //===============================================\n // Private\n //===============================================\n //===============================================\n // Public\n //===============================================\n /**\n *\n * @param options\n */\n function init(options) {\n if (options === null || options === void 0 ? void 0 : options.url)\n config.set(\"url\", options === null || options === void 0 ? void 0 : options.url);\n if (options === null || options === void 0 ? void 0 : options.debug)\n config.set(\"debug\", options === null || options === void 0 ? void 0 : options.debug);\n log = new log_1.Log(config);\n log.log(\"Initialized Magicfeedback\", options);\n }\n /**\n *\n * @param appId\n * @param answers\n * @param profile\n * @returns\n */\n function send(appId, answers, profile) {\n return __awaiter(this, void 0, void 0, function* () {\n if (!appId)\n log.err(\"No appID provided\");\n if (!answers)\n log.err(\"No answers provided\");\n if (answers.length == 0)\n log.err(\"Answers are empty\");\n const payload = {\n appId: appId,\n answers: answers,\n };\n if (profile)\n payload.profile = profile;\n let res = {};\n try {\n res = yield request.post(`${config.get(\"url\")}/feedback/apps`, payload);\n log.log(`sent native feedback`, res);\n }\n catch (e) {\n log.err(`error native feedback`, e);\n }\n return res;\n });\n }\n /**\n *\n * @param appId\n * @returns\n */\n function form(appId) {\n if (!appId)\n log.err(\"No appID provided\");\n return new form_1.Form(config, appId);\n }\n //===============================================\n // Return\n //===============================================\n return {\n // lifecycle\n init,\n // requests\n send,\n form,\n };\n}\nexports[\"default\"] = main;\n\n\n//# sourceURL=webpack://magicfeedback/./src/main.ts?");
|
|
84
|
+
|
|
85
|
+
/***/ }),
|
|
86
|
+
|
|
87
|
+
/***/ "./src/request.ts":
|
|
88
|
+
/*!************************!*\
|
|
89
|
+
!*** ./src/request.ts ***!
|
|
90
|
+
\************************/
|
|
91
|
+
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {
|
|
92
|
+
|
|
93
|
+
"use strict";
|
|
94
|
+
eval("\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nvar __importDefault = (this && this.__importDefault) || function (mod) {\n return (mod && mod.__esModule) ? mod : { \"default\": mod };\n};\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.Request = void 0;\nconst package_json_1 = __importDefault(__webpack_require__(/*! ../package.json */ \"./package.json\"));\nconst cross_fetch_1 = __importDefault(__webpack_require__(/*! cross-fetch */ \"./node_modules/cross-fetch/dist/browser-ponyfill.js\"));\nclass Request {\n /**\n *\n */\n constructor() {\n /**\n * Attributes\n */\n this.headers = {};\n this.headers = {\n Accept: \"application/json\",\n \"Magicfeedback-Sdk-Version\": package_json_1.default.version,\n };\n }\n /**\n *\n * @param url\n * @param body\n * @returns\n */\n post(url, body) {\n return __awaiter(this, void 0, void 0, function* () {\n return (0, cross_fetch_1.default)(url, {\n method: \"POST\",\n headers: Object.assign({ \"Content-Type\": \"application/json\" }, this.headers),\n body: JSON.stringify(body),\n });\n });\n }\n /**\n *\n * @param url\n * @param body\n * @returns\n */\n get(url, params) {\n return __awaiter(this, void 0, void 0, function* () {\n const serializedParams = Object.entries(params)\n .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`)\n .join(\"&\");\n const requestUrl = `${url}?${serializedParams}`;\n return (0, cross_fetch_1.default)(requestUrl, {\n method: \"GET\",\n headers: this.headers,\n }).then((res) => {\n if (res.status >= 400) {\n throw new Error(\"[MagicFeedback] Bad response from server\");\n }\n return res.json();\n });\n });\n }\n}\nexports.Request = Request;\n\n\n//# sourceURL=webpack://magicfeedback/./src/request.ts?");
|
|
95
|
+
|
|
96
|
+
/***/ }),
|
|
97
|
+
|
|
98
|
+
/***/ "./package.json":
|
|
99
|
+
/*!**********************!*\
|
|
100
|
+
!*** ./package.json ***!
|
|
101
|
+
\**********************/
|
|
102
|
+
/***/ ((module) => {
|
|
103
|
+
|
|
104
|
+
"use strict";
|
|
105
|
+
eval("module.exports = JSON.parse('{\"name\":\"@magicfeedback/native\",\"version\":\"1.0.2\",\"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\"}}');\n\n//# sourceURL=webpack://magicfeedback/./package.json?");
|
|
106
|
+
|
|
107
|
+
/***/ })
|
|
108
|
+
|
|
109
|
+
/******/ });
|
|
110
|
+
/************************************************************************/
|
|
111
|
+
/******/ // The module cache
|
|
112
|
+
/******/ var __webpack_module_cache__ = {};
|
|
113
|
+
/******/
|
|
114
|
+
/******/ // The require function
|
|
115
|
+
/******/ function __webpack_require__(moduleId) {
|
|
116
|
+
/******/ // Check if module is in cache
|
|
117
|
+
/******/ var cachedModule = __webpack_module_cache__[moduleId];
|
|
118
|
+
/******/ if (cachedModule !== undefined) {
|
|
119
|
+
/******/ return cachedModule.exports;
|
|
120
|
+
/******/ }
|
|
121
|
+
/******/ // Create a new module (and put it into the cache)
|
|
122
|
+
/******/ var module = __webpack_module_cache__[moduleId] = {
|
|
123
|
+
/******/ // no module.id needed
|
|
124
|
+
/******/ // no module.loaded needed
|
|
125
|
+
/******/ exports: {}
|
|
126
|
+
/******/ };
|
|
127
|
+
/******/
|
|
128
|
+
/******/ // Execute the module function
|
|
129
|
+
/******/ __webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
|
130
|
+
/******/
|
|
131
|
+
/******/ // Return the exports of the module
|
|
132
|
+
/******/ return module.exports;
|
|
133
|
+
/******/ }
|
|
134
|
+
/******/
|
|
135
|
+
/************************************************************************/
|
|
136
|
+
/******/
|
|
137
|
+
/******/ // startup
|
|
138
|
+
/******/ // Load entry module and return exports
|
|
139
|
+
/******/ // This entry module is referenced by other modules so it can't be inlined
|
|
140
|
+
/******/ var __webpack_exports__ = __webpack_require__("./src/index.ts");
|
|
141
|
+
/******/ __webpack_exports__ = __webpack_exports__["default"];
|
|
142
|
+
/******/
|
|
143
|
+
/******/ return __webpack_exports__;
|
|
144
|
+
/******/ })()
|
|
145
|
+
;
|
|
146
|
+
});
|
|
@@ -1 +1,140 @@
|
|
|
1
|
-
|
|
1
|
+
/*
|
|
2
|
+
* ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development").
|
|
3
|
+
* This devtool is neither made for production nor for readable output files.
|
|
4
|
+
* It uses "eval()" calls to create a separate source file in the browser devtools.
|
|
5
|
+
* If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
|
|
6
|
+
* or disable the default devtool with "devtool: false".
|
|
7
|
+
* If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
|
|
8
|
+
*/
|
|
9
|
+
(function webpackUniversalModuleDefinition(root, factory) {
|
|
10
|
+
if(typeof exports === 'object' && typeof module === 'object')
|
|
11
|
+
module.exports = factory();
|
|
12
|
+
else if(typeof define === 'function' && define.amd)
|
|
13
|
+
define([], factory);
|
|
14
|
+
else if(typeof exports === 'object')
|
|
15
|
+
exports["magicfeedback"] = factory();
|
|
16
|
+
else
|
|
17
|
+
root["magicfeedback"] = factory();
|
|
18
|
+
})(global, () => {
|
|
19
|
+
return /******/ (() => { // webpackBootstrap
|
|
20
|
+
/******/ "use strict";
|
|
21
|
+
/******/ var __webpack_modules__ = ({
|
|
22
|
+
|
|
23
|
+
/***/ "./src/config.ts":
|
|
24
|
+
/*!***********************!*\
|
|
25
|
+
!*** ./src/config.ts ***!
|
|
26
|
+
\***********************/
|
|
27
|
+
/***/ ((__unused_webpack_module, exports) => {
|
|
28
|
+
|
|
29
|
+
eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.Config = void 0;\nclass Config {\n constructor() {\n this.variables = {};\n this.variables[\"url\"] = \"https://api.magicfeedback.io\";\n this.variables[\"debug\"] = false;\n }\n get(key) {\n return this.variables[key];\n }\n set(key, value) {\n this.variables[key] = value;\n }\n}\nexports.Config = Config;\n\n\n//# sourceURL=webpack://magicfeedback/./src/config.ts?");
|
|
30
|
+
|
|
31
|
+
/***/ }),
|
|
32
|
+
|
|
33
|
+
/***/ "./src/form.ts":
|
|
34
|
+
/*!*********************!*\
|
|
35
|
+
!*** ./src/form.ts ***!
|
|
36
|
+
\*********************/
|
|
37
|
+
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {
|
|
38
|
+
|
|
39
|
+
eval("\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.Form = void 0;\nconst request_1 = __webpack_require__(/*! ./request */ \"./src/request.ts\");\nconst log_1 = __webpack_require__(/*! ./log */ \"./src/log.ts\");\nclass Form {\n /**\n *\n * @param config\n * @param appId\n */\n constructor(config, appId) {\n // Config\n this.config = config;\n this.request = new request_1.Request();\n this.log = new log_1.Log(config);\n // Attributes\n this.appId = appId;\n }\n /**\n * Generate\n * TODO: Check if is inside of a <form>\n * @param appId\n */\n generate(selector, options = {}) {\n //TODO: Check if already exist the form\n // Request question from the app\n this.request\n .get(`${this.config.get(\"url\")}/apps/${this.appId}/questions`, {})\n .then((appQuestions) => {\n if (appQuestions === undefined || !appQuestions) {\n this.log.err(`No questions for app ${this.appId}`);\n return;\n }\n // Create the from from the JSON\n this.generateForm(this.appId, appQuestions, selector, options);\n });\n }\n /**\n * Create\n * @param appId\n * @param appQuestions\n * @param selector\n * @param options\n *\n * TODO: Add option to generate in <form> or in other <tag>\n */\n generateForm(appId, appQuestions, selector, options = {}) {\n // Select the container\n const container = document.getElementById(selector);\n if (!container) {\n this.log.err(`Element with ID '${selector}' not found.`);\n return;\n }\n container.classList.add(\"magicfeedback-container\");\n // Create the form\n const form = document.createElement(\"form\");\n form.classList.add(\"magicfeedback-form\");\n form.id = \"magicfeedback-\" + appId;\n // Process questions and create in the form\n appQuestions.forEach((question) => {\n const { id, title, type, ref, require, \n //external_id,\n value, defaultValue, } = question;\n let element;\n let elementTypeClass;\n let elementContainer = document.createElement(\"div\");\n elementContainer.classList.add(\"magicfeedback-div\");\n switch (type) {\n case \"TEXT\":\n // Create a text input field\n element = document.createElement(\"input\");\n element.type = \"text\";\n elementTypeClass = \"magicfeedback-text\";\n break;\n case \"LONGTEXT\":\n // Create a textarea element for TEXT and LONGTEXT types\n element = document.createElement(\"textarea\");\n element.rows = 3; // Set the number of rows based on the type\n elementTypeClass = \"magicfeedback-longtext\";\n break;\n case \"NUMBER\":\n // Create an input element with type \"number\" for NUMBER type\n element = document.createElement(\"input\");\n element.type = \"number\";\n elementTypeClass = \"magicfeedback-number\";\n if (value.length) {\n value.sort((a, b) => {\n let aa = Number(a);\n let bb = Number(b);\n return aa - bb;\n });\n element.max = value[value.length - 1];\n element.min = value[0];\n element.value = value[0];\n }\n break;\n case \"RADIO\":\n case \"MULTIPLECHOICE\":\n element = document.createElement(\"div\");\n elementTypeClass =\n \"magicfeedback-\" + (type === \"RADIO\" ? \"radio\" : \"checkbox\");\n value.forEach((option) => {\n const label = document.createElement(\"label\");\n const input = document.createElement(\"input\");\n input.type = type === \"RADIO\" ? \"radio\" : \"checkbox\";\n input.name = ref;\n input.value = option;\n input.required = require;\n input.classList.add(elementTypeClass);\n input.classList.add(\"magicfeedback-input\");\n if (option === defaultValue) {\n input.checked = true;\n }\n label.textContent = option;\n element.appendChild(input);\n element.appendChild(label);\n });\n break;\n case \"SELECT\":\n // Create a select element for RADIO and MULTIPLECHOICE types\n element = document.createElement(\"select\");\n elementTypeClass = \"magicfeedback-select\";\n value.forEach((optionValue) => {\n // Create an option element for each value in the question's value array\n const option = document.createElement(\"option\");\n option.value = optionValue;\n option.text = optionValue;\n element.appendChild(option);\n });\n break;\n case \"DATE\":\n // Create an input element with type \"date\" for DATE type\n element = document.createElement(\"input\");\n element.type = \"date\";\n elementTypeClass = \"magicfeedback-date\";\n break;\n case \"BOOLEAN\":\n // Create an input element with type \"checkbox\" for BOOLEAN type\n element = document.createElement(\"input\");\n element.type = \"checkbox\";\n elementTypeClass = \"magicfeedback-boolean\";\n break;\n default:\n return; // Skip unknown types\n }\n element.id = `magicfeedback-${id}`;\n element.setAttribute(\"name\", ref);\n if (defaultValue !== undefined) {\n element.value = defaultValue;\n }\n // Add the label and input element to the form\n const label = document.createElement(\"label\");\n label.setAttribute(\"for\", `magicfeedback-${id}`);\n label.textContent = title;\n label.classList.add(\"magicfeedback-label\");\n elementContainer.appendChild(label);\n element.classList.add(elementTypeClass);\n if (type != \"RADIO\" && type != \"MULTIPLECHOICE\") {\n element.classList.add(\"magicfeedback-input\");\n element.required = require;\n }\n elementContainer.appendChild(element);\n form.appendChild(elementContainer);\n });\n // Submit button\n if (options.addButton) {\n // Create a submit button if specified in options\n const submitButton = document.createElement(\"button\");\n submitButton.type = \"submit\";\n submitButton.textContent = \"Submit\";\n submitButton.classList.add(\"magicfeedback-submit\");\n form.appendChild(submitButton);\n }\n // Add the form to the specified container\n container.appendChild(form);\n // Submit event\n form.addEventListener(\"submit\", (event) => __awaiter(this, void 0, void 0, function* () {\n event.preventDefault();\n try {\n // BEFORE\n if (options.beforeSubmitEvent) {\n yield options.beforeSubmitEvent();\n }\n // SEND\n const response = yield this.send();\n // AFTER\n if (options.afterSubmitEvent) {\n yield options.afterSubmitEvent(response);\n }\n return response;\n }\n catch (error) {\n // Handle error in beforeSubmitEvent, send(), or afterSubmitEvent\n this.log.err(`An error occurred while submitting the form ${this.appId}:`, error);\n // You can perform error handling logic here if needed\n return error;\n }\n }));\n }\n /**\n * Answer\n * @param appId\n * @returns\n * TODO: Required\n */\n answer() {\n const form = document.getElementById(\"magicfeedback-\" + this.appId);\n if (!form) {\n this.log.err(`Form \"${form}\" not found.`);\n return [];\n }\n const surveyAnswers = [];\n let hasError = false; // Flag to track if an error has occurred\n const inputs = form.querySelectorAll(\".magicfeedback-input\");\n inputs.forEach((input) => {\n const inputType = input.type;\n //const required = (input as HTMLInputElement).required;\n const ans = {\n id: input.name,\n type: inputType,\n value: [],\n };\n const value = input.value;\n if (inputType === \"radio\" || inputType === \"checkbox\") {\n if (input.checked) {\n ans.value.push(value);\n surveyAnswers.push(ans);\n }\n }\n else {\n ans.value.push(value);\n surveyAnswers.push(ans);\n }\n });\n if (hasError) {\n return []; // Stop the process if there's an error\n }\n return surveyAnswers;\n }\n /**\n * Send\n * @returns\n */\n send() {\n return __awaiter(this, void 0, void 0, function* () {\n // Define the URL and request payload\n const url = `${this.config.get(\"url\")}/feedback/apps`;\n try {\n // Get the survey answers from the answer() function\n const surveyAnswers = this.answer();\n if (surveyAnswers.length === 0) {\n throw new Error(\"No answers provided\");\n }\n // Make the AJAX POST request\n const response = yield this.request.post(url, {\n appId: this.appId,\n answers: surveyAnswers,\n });\n if (response.ok) {\n // Handle success response\n this.log.log(`Form ${this.appId} submitted successfully!`);\n // You can perform additional actions here if needed\n }\n else {\n // Handle error response\n this.log.err(`Failed to submit form ${this.appId}:`, response.status, response.statusText);\n throw new Error(response.statusText);\n }\n return response;\n }\n catch (error) {\n // Handle network or request error\n this.log.err(`An error occurred while submitting the form ${this.appId}:`, error);\n // You can perform error handling logic here if needed\n throw error;\n }\n });\n }\n}\nexports.Form = Form;\n\n\n//# sourceURL=webpack://magicfeedback/./src/form.ts?");
|
|
40
|
+
|
|
41
|
+
/***/ }),
|
|
42
|
+
|
|
43
|
+
/***/ "./src/index.ts":
|
|
44
|
+
/*!**********************!*\
|
|
45
|
+
!*** ./src/index.ts ***!
|
|
46
|
+
\**********************/
|
|
47
|
+
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {
|
|
48
|
+
|
|
49
|
+
eval("\nvar __importDefault = (this && this.__importDefault) || function (mod) {\n return (mod && mod.__esModule) ? mod : { \"default\": mod };\n};\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nconst main_1 = __importDefault(__webpack_require__(/*! ./main */ \"./src/main.ts\"));\nlet instance = null;\nif (!instance) {\n instance = (0, main_1.default)();\n}\nexports[\"default\"] = instance;\n\n\n//# sourceURL=webpack://magicfeedback/./src/index.ts?");
|
|
50
|
+
|
|
51
|
+
/***/ }),
|
|
52
|
+
|
|
53
|
+
/***/ "./src/log.ts":
|
|
54
|
+
/*!********************!*\
|
|
55
|
+
!*** ./src/log.ts ***!
|
|
56
|
+
\********************/
|
|
57
|
+
/***/ ((__unused_webpack_module, exports) => {
|
|
58
|
+
|
|
59
|
+
eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.Log = void 0;\nclass Log {\n /**\n *\n * @param config\n */\n constructor(config) {\n // Config\n this.config = config;\n }\n /**\n *\n * @param args\n */\n log(...args) {\n if (this.config.get(\"debug\")) {\n console.log(\"[MagicFeedback]:\", ...args);\n }\n }\n /**\n *\n * @param args\n */\n err(...args) {\n console.error(\"[MagicFeedback]:\", ...args);\n }\n}\nexports.Log = Log;\n\n\n//# sourceURL=webpack://magicfeedback/./src/log.ts?");
|
|
60
|
+
|
|
61
|
+
/***/ }),
|
|
62
|
+
|
|
63
|
+
/***/ "./src/main.ts":
|
|
64
|
+
/*!*********************!*\
|
|
65
|
+
!*** ./src/main.ts ***!
|
|
66
|
+
\*********************/
|
|
67
|
+
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {
|
|
68
|
+
|
|
69
|
+
eval("\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nconst request_1 = __webpack_require__(/*! ./request */ \"./src/request.ts\");\nconst form_1 = __webpack_require__(/*! ./form */ \"./src/form.ts\");\nconst config_1 = __webpack_require__(/*! ./config */ \"./src/config.ts\");\nconst log_1 = __webpack_require__(/*! ./log */ \"./src/log.ts\");\n/**\n *\n * @returns\n */\nfunction main() {\n //===============================================\n // Attributes\n //===============================================\n const config = new config_1.Config();\n const request = new request_1.Request();\n let log;\n //===============================================\n // Private\n //===============================================\n //===============================================\n // Public\n //===============================================\n /**\n *\n * @param options\n */\n function init(options) {\n if (options === null || options === void 0 ? void 0 : options.url)\n config.set(\"url\", options === null || options === void 0 ? void 0 : options.url);\n if (options === null || options === void 0 ? void 0 : options.debug)\n config.set(\"debug\", options === null || options === void 0 ? void 0 : options.debug);\n log = new log_1.Log(config);\n log.log(\"Initialized Magicfeedback\", options);\n }\n /**\n *\n * @param appId\n * @param answers\n * @param profile\n * @returns\n */\n function send(appId, answers, profile) {\n return __awaiter(this, void 0, void 0, function* () {\n if (!appId)\n log.err(\"No appID provided\");\n if (!answers)\n log.err(\"No answers provided\");\n if (answers.length == 0)\n log.err(\"Answers are empty\");\n const payload = {\n appId: appId,\n answers: answers,\n };\n if (profile)\n payload.profile = profile;\n let res = {};\n try {\n res = yield request.post(`${config.get(\"url\")}/feedback/apps`, payload);\n log.log(`sent native feedback`, res);\n }\n catch (e) {\n log.err(`error native feedback`, e);\n }\n return res;\n });\n }\n /**\n *\n * @param appId\n * @returns\n */\n function form(appId) {\n if (!appId)\n log.err(\"No appID provided\");\n return new form_1.Form(config, appId);\n }\n //===============================================\n // Return\n //===============================================\n return {\n // lifecycle\n init,\n // requests\n send,\n form,\n };\n}\nexports[\"default\"] = main;\n\n\n//# sourceURL=webpack://magicfeedback/./src/main.ts?");
|
|
70
|
+
|
|
71
|
+
/***/ }),
|
|
72
|
+
|
|
73
|
+
/***/ "./src/request.ts":
|
|
74
|
+
/*!************************!*\
|
|
75
|
+
!*** ./src/request.ts ***!
|
|
76
|
+
\************************/
|
|
77
|
+
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {
|
|
78
|
+
|
|
79
|
+
eval("\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nvar __importDefault = (this && this.__importDefault) || function (mod) {\n return (mod && mod.__esModule) ? mod : { \"default\": mod };\n};\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.Request = void 0;\nconst package_json_1 = __importDefault(__webpack_require__(/*! ../package.json */ \"./package.json\"));\nconst cross_fetch_1 = __importDefault(__webpack_require__(/*! cross-fetch */ \"cross-fetch\"));\nclass Request {\n /**\n *\n */\n constructor() {\n /**\n * Attributes\n */\n this.headers = {};\n this.headers = {\n Accept: \"application/json\",\n \"Magicfeedback-Sdk-Version\": package_json_1.default.version,\n };\n }\n /**\n *\n * @param url\n * @param body\n * @returns\n */\n post(url, body) {\n return __awaiter(this, void 0, void 0, function* () {\n return (0, cross_fetch_1.default)(url, {\n method: \"POST\",\n headers: Object.assign({ \"Content-Type\": \"application/json\" }, this.headers),\n body: JSON.stringify(body),\n });\n });\n }\n /**\n *\n * @param url\n * @param body\n * @returns\n */\n get(url, params) {\n return __awaiter(this, void 0, void 0, function* () {\n const serializedParams = Object.entries(params)\n .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`)\n .join(\"&\");\n const requestUrl = `${url}?${serializedParams}`;\n return (0, cross_fetch_1.default)(requestUrl, {\n method: \"GET\",\n headers: this.headers,\n }).then((res) => {\n if (res.status >= 400) {\n throw new Error(\"[MagicFeedback] Bad response from server\");\n }\n return res.json();\n });\n });\n }\n}\nexports.Request = Request;\n\n\n//# sourceURL=webpack://magicfeedback/./src/request.ts?");
|
|
80
|
+
|
|
81
|
+
/***/ }),
|
|
82
|
+
|
|
83
|
+
/***/ "cross-fetch":
|
|
84
|
+
/*!******************************!*\
|
|
85
|
+
!*** external "cross-fetch" ***!
|
|
86
|
+
\******************************/
|
|
87
|
+
/***/ ((module) => {
|
|
88
|
+
|
|
89
|
+
module.exports = require("cross-fetch");
|
|
90
|
+
|
|
91
|
+
/***/ }),
|
|
92
|
+
|
|
93
|
+
/***/ "./package.json":
|
|
94
|
+
/*!**********************!*\
|
|
95
|
+
!*** ./package.json ***!
|
|
96
|
+
\**********************/
|
|
97
|
+
/***/ ((module) => {
|
|
98
|
+
|
|
99
|
+
eval("module.exports = JSON.parse('{\"name\":\"@magicfeedback/native\",\"version\":\"1.0.2\",\"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\"}}');\n\n//# sourceURL=webpack://magicfeedback/./package.json?");
|
|
100
|
+
|
|
101
|
+
/***/ })
|
|
102
|
+
|
|
103
|
+
/******/ });
|
|
104
|
+
/************************************************************************/
|
|
105
|
+
/******/ // The module cache
|
|
106
|
+
/******/ var __webpack_module_cache__ = {};
|
|
107
|
+
/******/
|
|
108
|
+
/******/ // The require function
|
|
109
|
+
/******/ function __webpack_require__(moduleId) {
|
|
110
|
+
/******/ // Check if module is in cache
|
|
111
|
+
/******/ var cachedModule = __webpack_module_cache__[moduleId];
|
|
112
|
+
/******/ if (cachedModule !== undefined) {
|
|
113
|
+
/******/ return cachedModule.exports;
|
|
114
|
+
/******/ }
|
|
115
|
+
/******/ // Create a new module (and put it into the cache)
|
|
116
|
+
/******/ var module = __webpack_module_cache__[moduleId] = {
|
|
117
|
+
/******/ // no module.id needed
|
|
118
|
+
/******/ // no module.loaded needed
|
|
119
|
+
/******/ exports: {}
|
|
120
|
+
/******/ };
|
|
121
|
+
/******/
|
|
122
|
+
/******/ // Execute the module function
|
|
123
|
+
/******/ __webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
|
124
|
+
/******/
|
|
125
|
+
/******/ // Return the exports of the module
|
|
126
|
+
/******/ return module.exports;
|
|
127
|
+
/******/ }
|
|
128
|
+
/******/
|
|
129
|
+
/************************************************************************/
|
|
130
|
+
/******/
|
|
131
|
+
/******/ // startup
|
|
132
|
+
/******/ // Load entry module and return exports
|
|
133
|
+
/******/ // This entry module is referenced by other modules so it can't be inlined
|
|
134
|
+
/******/ var __webpack_exports__ = __webpack_require__("./src/index.ts");
|
|
135
|
+
/******/ __webpack_exports__ = __webpack_exports__["default"];
|
|
136
|
+
/******/
|
|
137
|
+
/******/ return __webpack_exports__;
|
|
138
|
+
/******/ })()
|
|
139
|
+
;
|
|
140
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { generateFormOptions, NativeAnswer } from "./types";
|
|
2
|
+
import { Config } from "./config";
|
|
3
|
+
export declare class Form {
|
|
4
|
+
/**
|
|
5
|
+
* Attributes
|
|
6
|
+
*/
|
|
7
|
+
private config;
|
|
8
|
+
private request;
|
|
9
|
+
private log;
|
|
10
|
+
private appId;
|
|
11
|
+
/**
|
|
12
|
+
*
|
|
13
|
+
* @param config
|
|
14
|
+
* @param appId
|
|
15
|
+
*/
|
|
16
|
+
constructor(config: Config, appId: string);
|
|
17
|
+
/**
|
|
18
|
+
* Generate
|
|
19
|
+
* TODO: Check if is inside of a <form>
|
|
20
|
+
* @param appId
|
|
21
|
+
*/
|
|
22
|
+
generate(selector: string, options?: generateFormOptions): void;
|
|
23
|
+
/**
|
|
24
|
+
* Create
|
|
25
|
+
* @param appId
|
|
26
|
+
* @param appQuestions
|
|
27
|
+
* @param selector
|
|
28
|
+
* @param options
|
|
29
|
+
*
|
|
30
|
+
* TODO: Add option to generate in <form> or in other <tag>
|
|
31
|
+
*/
|
|
32
|
+
private generateForm;
|
|
33
|
+
/**
|
|
34
|
+
* Answer
|
|
35
|
+
* @param appId
|
|
36
|
+
* @returns
|
|
37
|
+
* TODO: Required
|
|
38
|
+
*/
|
|
39
|
+
answer(): NativeAnswer[];
|
|
40
|
+
/**
|
|
41
|
+
* Send
|
|
42
|
+
* @returns
|
|
43
|
+
*/
|
|
44
|
+
send(): Promise<Response>;
|
|
45
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
declare const _default: {
|
|
2
2
|
init: (options?: import("./types").InitOptions | undefined) => void;
|
|
3
3
|
send: (appId: string, answers: import("./types").NativeFeedbackAnswer[], profile?: import("./types").NativeFeedbackProfile | undefined) => Promise<{}>;
|
|
4
|
+
form: (appId: string) => import("./form").Form;
|
|
4
5
|
};
|
|
5
6
|
export default _default;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Config } from "./config";
|
|
2
|
+
export declare class Log {
|
|
3
|
+
/**
|
|
4
|
+
* Attributes
|
|
5
|
+
*/
|
|
6
|
+
private config;
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* @param config
|
|
10
|
+
*/
|
|
11
|
+
constructor(config: Config);
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* @param args
|
|
15
|
+
*/
|
|
16
|
+
log(...args: any[]): void;
|
|
17
|
+
/**
|
|
18
|
+
*
|
|
19
|
+
* @param args
|
|
20
|
+
*/
|
|
21
|
+
err(...args: any[]): void;
|
|
22
|
+
}
|
package/dist/types/src/main.d.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { InitOptions, NativeFeedback } from "./types";
|
|
2
|
+
import { Form } from "./form";
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @returns
|
|
6
|
+
*/
|
|
2
7
|
export default function main(): {
|
|
3
8
|
init: (options?: InitOptions) => void;
|
|
4
9
|
send: (appId: NativeFeedback["appId"], answers: NativeFeedback["answers"], profile?: NativeFeedback["profile"]) => Promise<{}>;
|
|
10
|
+
form: (appId: string) => Form;
|
|
5
11
|
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare class Request {
|
|
2
|
+
/**
|
|
3
|
+
* Attributes
|
|
4
|
+
*/
|
|
5
|
+
private headers;
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
constructor();
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
* @param url
|
|
13
|
+
* @param body
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
post(url: string, body: any): Promise<Response>;
|
|
17
|
+
/**
|
|
18
|
+
*
|
|
19
|
+
* @param url
|
|
20
|
+
* @param body
|
|
21
|
+
* @returns
|
|
22
|
+
*/
|
|
23
|
+
get(url: string, params: any): Promise<any>;
|
|
24
|
+
}
|
|
@@ -23,5 +23,33 @@ export declare enum FEEDBACKAPPANSWERTYPE {
|
|
|
23
23
|
RADIO = "RADIO",
|
|
24
24
|
MULTIPLECHOICE = "MULTIPLECHOICE",
|
|
25
25
|
DATE = "DATE",
|
|
26
|
-
BOOLEAN = "BOOLEAN"
|
|
26
|
+
BOOLEAN = "BOOLEAN",
|
|
27
|
+
SELECT = "SELECT"
|
|
27
28
|
}
|
|
29
|
+
export type NativeQuestion = {
|
|
30
|
+
id: string;
|
|
31
|
+
title: string;
|
|
32
|
+
type: FEEDBACKAPPANSWERTYPE;
|
|
33
|
+
ref: string;
|
|
34
|
+
require: boolean;
|
|
35
|
+
external_id: string;
|
|
36
|
+
value: string[];
|
|
37
|
+
defaultValue: string;
|
|
38
|
+
appId: string;
|
|
39
|
+
};
|
|
40
|
+
export type NativeAnswer = {
|
|
41
|
+
id: string;
|
|
42
|
+
type: string;
|
|
43
|
+
value: string[];
|
|
44
|
+
};
|
|
45
|
+
export type generateFormOptions = {
|
|
46
|
+
addButton?: boolean;
|
|
47
|
+
tag?: generateFormOptionsTag;
|
|
48
|
+
afterSubmitEvent?: Function;
|
|
49
|
+
beforeSubmitEvent?: Function;
|
|
50
|
+
};
|
|
51
|
+
declare enum generateFormOptionsTag {
|
|
52
|
+
FORM = "form",
|
|
53
|
+
DIV = "div"
|
|
54
|
+
}
|
|
55
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@magicfeedback/native",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"main": "./dist/magicfeedback-sdk.node.js",
|
|
5
5
|
"browser": "./dist/magicfeedback-sdk.browser.js",
|
|
6
6
|
"types": "./dist/types/src/index.d.ts",
|
|
@@ -14,19 +14,25 @@
|
|
|
14
14
|
"scripts": {
|
|
15
15
|
"dev": "vite",
|
|
16
16
|
"build": "webpack",
|
|
17
|
-
"
|
|
18
|
-
"test
|
|
17
|
+
"build:watch": "webpack --watch --mode development",
|
|
18
|
+
"test": "jest",
|
|
19
|
+
"test:watch": "jest --watchAll",
|
|
19
20
|
"coverage": "vitest run --coverage"
|
|
20
21
|
},
|
|
21
22
|
"files": [
|
|
22
23
|
"dist"
|
|
23
24
|
],
|
|
24
25
|
"devDependencies": {
|
|
26
|
+
"@babel/preset-typescript": "^7.22.5",
|
|
25
27
|
"@types/node": "^17.0.21",
|
|
26
28
|
"@types/webpack": "^5.28.0",
|
|
27
29
|
"@types/webpack-node-externals": "^2.5.3",
|
|
28
30
|
"c8": "^7.11.0",
|
|
31
|
+
"jest": "^29.5.0",
|
|
32
|
+
"jest-environment-jsdom": "^29.5.0",
|
|
33
|
+
"jest-fetch-mock": "^3.0.3",
|
|
29
34
|
"nock": "^13.2.4",
|
|
35
|
+
"ts-jest": "^29.1.0",
|
|
30
36
|
"ts-loader": "^9.2.7",
|
|
31
37
|
"ts-node": "^10.7.0",
|
|
32
38
|
"typescript": "^4.6.2",
|