UNPKG

@elbwalker/destination-web-api

Version:

API web destination for walkerOS

1 lines 7.83 kB
"use strict";function _array_like_to_array(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,n=new Array(e);t<e;t++)n[t]=r[t];return n}function _array_with_holes(r){if(Array.isArray(r))return r}function _array_without_holes(r){if(Array.isArray(r))return _array_like_to_array(r)}function asyncGeneratorStep(r,e,t,n,o,a,i){try{var u=r[a](i),c=u.value}catch(r){return void t(r)}u.done?e(c):Promise.resolve(c).then(n,o)}function _async_to_generator(r){return function(){var e=this,t=arguments;return new Promise((function(n,o){var a=r.apply(e,t);function i(r){asyncGeneratorStep(a,n,o,i,u,"next",r)}function u(r){asyncGeneratorStep(a,n,o,i,u,"throw",r)}i(void 0)}))}}function _define_property(r,e,t){return e in r?Object.defineProperty(r,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):r[e]=t,r}function _iterable_to_array(r){if("undefined"!=typeof Symbol&&null!=r[Symbol.iterator]||null!=r["@@iterator"])return Array.from(r)}function _iterable_to_array_limit(r,e){var t=null==r?null:"undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(null!=t){var n,o,a=[],i=!0,u=!1;try{for(t=t.call(r);!(i=(n=t.next()).done)&&(a.push(n.value),!e||a.length!==e);i=!0);}catch(r){u=!0,o=r}finally{try{i||null==t.return||t.return()}finally{if(u)throw o}}return a}}function _non_iterable_rest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _non_iterable_spread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _object_spread(r){for(var e=1;e<arguments.length;e++){var t=null!=arguments[e]?arguments[e]:{},n=Object.keys(t);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(t).filter((function(r){return Object.getOwnPropertyDescriptor(t,r).enumerable})))),n.forEach((function(e){_define_property(r,e,t[e])}))}return r}function _sliced_to_array(r,e){return _array_with_holes(r)||_iterable_to_array_limit(r,e)||_unsupported_iterable_to_array(r,e)||_non_iterable_rest()}function _to_consumable_array(r){return _array_without_holes(r)||_iterable_to_array(r)||_unsupported_iterable_to_array(r)||_non_iterable_spread()}function _type_of(r){return r&&"undefined"!=typeof Symbol&&r.constructor===Symbol?"symbol":typeof r}function _unsupported_iterable_to_array(r,e){if(r){if("string"==typeof r)return _array_like_to_array(r,e);var t=Object.prototype.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(t):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?_array_like_to_array(r,e):void 0}}function _ts_generator(r,e){var t,n,o,a,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return a={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function u(a){return function(u){return function(a){if(t)throw new TypeError("Generator is already executing.");for(;i;)try{if(t=1,n&&(o=2&a[0]?n.return:a[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,a[1])).done)return o;switch(n=0,o&&(a=[2&a[0],o.value]),a[0]){case 0:case 1:o=a;break;case 4:return i.label++,{value:a[1],done:!1};case 5:i.label++,n=a[1],a=[0];continue;case 7:a=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==a[0]&&2!==a[0])){i=0;continue}if(3===a[0]&&(!o||a[1]>o[0]&&a[1]<o[3])){i.label=a[1];break}if(6===a[0]&&i.label<o[1]){i.label=o[1],o=a;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(a);break}o[2]&&i.ops.pop(),i.trys.pop();continue}a=e.call(r,i)}catch(r){a=[6,r],n=0}finally{t=o=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,u])}}}var Destination=function(){var r=function(r){return void 0===r||"string"==typeof r?r:JSON.stringify(r)},e=function(){return function(r){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};t=_object_spread({},_,t);var n=Object.entries(e).reduce((function(e,n){var o=_sliced_to_array(n,2),a=o[0],i=o[1],u=r[a];return t.merge&&Array.isArray(u)&&Array.isArray(i)?e[a]=i.reduce((function(r,e){return r.includes(e)?r:_to_consumable_array(r).concat([e])}),_to_consumable_array(u)):(t.extend||a in r)&&(e[a]=i),e}),{});return t.shallow?_object_spread({},r,n):(Object.assign(r,n),r)}({"Content-Type":"application/json; charset=utf-8"},arguments.length>0&&void 0!==arguments[0]?arguments[0]:{})},t=function(r,e){return function(){for(var t=arguments.length,n=new Array(t),o=0;o<t;o++)n[o]=arguments[o];try{return r.apply(void 0,_to_consumable_array(n))}catch(r){if(!e)return;return e(r)}}},n=function(r,e){var t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{transport:"fetch"};switch(t.transport||"fetch"){case"beacon":return a(r,e);case"xhr":return i(r,e,t);default:return o(r,e,t)}},o=function(r,e){return y.apply(this,arguments)},a=function(e,t){var n=r(t),o=navigator.sendBeacon(e,n);return{ok:o,error:o?void 0:"Failed to send beacon"}},i=function(n,o){var a=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},i=e(a.headers),u=a.method||"POST",c=r(o);return t((function(){var r=new XMLHttpRequest;for(var e in r.open(u,n,!1),i)r.setRequestHeader(e,i[e]);r.send(c);var o=r.status>=200&&r.status<300;return{ok:o,data:t(JSON.parse,(function(){return r.response}))(r.response),error:o?void 0:"".concat(r.status," ").concat(r.statusText)}}),(function(r){return{ok:!1,error:r.message}}))()},u=Object.defineProperty,c=Object.getOwnPropertyDescriptor,s=Object.getOwnPropertyNames,l=Object.prototype.hasOwnProperty,f={};!function(r,e){for(var t in e)u(r,t,{get:e[t],enumerable:!0})}(f,{DestinationWebAPI:function(){return p},default:function(){return h},destinationWebAPI:function(){return b}});var _={merge:!0,shallow:!0,extend:!0};function y(){return y=_async_to_generator((function(t,n){var o,a,i,u,c,s=arguments;return _ts_generator(this,(function(l){return a=e((o=s.length>2&&void 0!==s[2]?s[2]:{}).headers),i=r(n),[2,(u=_async_to_generator((function(){var r,e,n;return _ts_generator(this,(function(u){switch(u.label){case 0:return[4,fetch(t,{method:o.method||"POST",headers:a,keepalive:!0,credentials:o.credentials||"same-origin",mode:o.noCors?"no-cors":"cors",body:i})];case 1:return r=u.sent(),o.noCors?(n="",[3,4]):[3,2];case 2:return[4,r.text()];case 3:n=u.sent(),u.label=4;case 4:return e=n,[2,{ok:r.ok,data:e,error:r.ok?void 0:r.statusText}]}}))})),c=function(r){return{ok:!1,error:r.message}},_async_to_generator((function(){var r,e,t,n,o=arguments;return _ts_generator(this,(function(a){switch(a.label){case 0:for(r=o.length,e=new Array(r),t=0;t<r;t++)e[t]=o[t];a.label=1;case 1:return a.trys.push([1,3,,5]),[4,u.apply(void 0,_to_consumable_array(e))];case 2:return[2,a.sent()];case 3:return n=a.sent(),c?[4,c(n)]:[2];case 4:return[2,a.sent()];case 5:return[2]}}))})))()]}))})),y.apply(this,arguments)}var d,p={},b={type:"api",config:{},push:function(r,e,t){var o=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{},a=e.custom,i=void 0===a?{}:a,u=e.fn,c=i.url,s=i.headers,l=i.method,f=i.transform,_=i.transport,y=void 0===_?"fetch":_;if(c){var d=void 0!==o.data?o.data:r,p=f?f(d,e,t):JSON.stringify(d);(u||n)(c,p,{headers:s,method:l,transport:y})}}},h=b;return d=f,function(r,e,t,n){if(e&&"object"===(void 0===e?"undefined":_type_of(e))||"function"==typeof e){var o=!0,a=!1,i=void 0;try{for(var f,_=function(){var o=f.value;l.call(r,o)||o===t||u(r,o,{get:function(){return e[o]},enumerable:!(n=c(e,o))||n.enumerable})},y=s(e)[Symbol.iterator]();!(o=(f=y.next()).done);o=!0)_()}catch(r){a=!0,i=r}finally{try{o||null==y.return||y.return()}finally{if(a)throw i}}}return r}(u({},"__esModule",{value:!0}),d)}();