@supabase-kit/react
Version:
> Hooks by management data from supabase and fetch data by react.
3 lines (2 loc) • 13.4 kB
JavaScript
"use strict";var t=require("@tanstack/react-query"),e=require("react"),r=require("@supabase/supabase-js");function n(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}function o(t,e,r,n,o,a,i){try{var u=t[a](i),c=u.value}catch(t){return void r(t)}u.done?e(c):Promise.resolve(c).then(n,o)}function a(t){return function(){var e=this,r=arguments;return new Promise((function(n,a){var i=t.apply(e,r);function u(t){o(i,n,a,u,c,"next",t)}function c(t){o(i,n,a,u,c,"throw",t)}u(void 0)}))}}function i(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(r)return(r=r.call(t)).next.bind(r);if(Array.isArray(t)||(r=function(t,e){if(t){if("string"==typeof t)return n(t,e);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(t,e):void 0}}(t))||e&&t&&"number"==typeof t.length){r&&(t=r);var o=0;return function(){return o>=t.length?{done:!0}:{done:!1,value:t[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function u(){return u=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)({}).hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},u.apply(null,arguments)}function c(t,e){if(null==t)return{};var r={};for(var n in t)if({}.hasOwnProperty.call(t,n)){if(-1!==e.indexOf(n))continue;r[n]=t[n]}return r}function s(){s=function(){return e};var t,e={},r=Object.prototype,n=r.hasOwnProperty,o="function"==typeof Symbol?Symbol:{},a=o.iterator||"@@iterator",i=o.asyncIterator||"@@asyncIterator",u=o.toStringTag||"@@toStringTag";function c(t,e,r,n){return Object.defineProperty(t,e,{value:r,enumerable:!n,configurable:!n,writable:!n})}try{c({},"")}catch(t){c=function(t,e,r){return t[e]=r}}function l(e,r,n,o){var a=Object.create((r&&r.prototype instanceof v?r:v).prototype);return c(a,"_invoke",function(e,r,n){var o=1;return function(a,i){if(3===o)throw Error("Generator is already running");if(4===o){if("throw"===a)throw i;return{value:t,done:!0}}for(n.method=a,n.arg=i;;){var u=n.delegate;if(u){var c=S(u,n);if(c){if(c===h)continue;return c}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(1===o)throw o=4,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=3;var s=f(e,r,n);if("normal"===s.type){if(o=n.done?4:2,s.arg===h)continue;return{value:s.arg,done:n.done}}"throw"===s.type&&(o=4,n.method="throw",n.arg=s.arg)}}}(e,n,new O(o||[])),!0),a}function f(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}e.wrap=l;var h={};function v(){}function p(){}function y(){}var d={};c(d,a,(function(){return this}));var g=Object.getPrototypeOf,b=g&&g(g(j([])));b&&b!==r&&n.call(b,a)&&(d=b);var m=y.prototype=v.prototype=Object.create(d);function w(t){["next","throw","return"].forEach((function(e){c(t,e,(function(t){return this._invoke(e,t)}))}))}function E(t,e){function r(o,a,i,u){var c=f(t[o],t,a);if("throw"!==c.type){var s=c.arg,l=s.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,i,u)}),(function(t){r("throw",t,i,u)})):e.resolve(l).then((function(t){s.value=t,i(s)}),(function(t){return r("throw",t,i,u)}))}u(c.arg)}var o;c(this,"_invoke",(function(t,n){function a(){return new e((function(e,o){r(t,n,e,o)}))}return o=o?o.then(a,a):a()}),!0)}function S(e,r){var n=r.method,o=e.i[n];if(o===t)return r.delegate=null,"throw"===n&&e.i.return&&(r.method="return",r.arg=t,S(e,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),h;var a=f(o,e.i,r.arg);if("throw"===a.type)return r.method="throw",r.arg=a.arg,r.delegate=null,h;var i=a.arg;return i?i.done?(r[e.r]=i.value,r.next=e.n,"return"!==r.method&&(r.method="next",r.arg=t),r.delegate=null,h):i:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,h)}function x(t){this.tryEntries.push(t)}function _(e){var r=e[4]||{};r.type="normal",r.arg=t,e[4]=r}function O(t){this.tryEntries=[[-1]],t.forEach(x,this),this.reset(!0)}function j(e){if(null!=e){var r=e[a];if(r)return r.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var o=-1,i=function r(){for(;++o<e.length;)if(n.call(e,o))return r.value=e[o],r.done=!1,r;return r.value=t,r.done=!0,r};return i.next=i}}throw new TypeError(typeof e+" is not iterable")}return p.prototype=y,c(m,"constructor",y),c(y,"constructor",p),p.displayName=c(y,u,"GeneratorFunction"),e.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===p||"GeneratorFunction"===(e.displayName||e.name))},e.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,y):(t.__proto__=y,c(t,u,"GeneratorFunction")),t.prototype=Object.create(m),t},e.awrap=function(t){return{__await:t}},w(E.prototype),c(E.prototype,i,(function(){return this})),e.AsyncIterator=E,e.async=function(t,r,n,o,a){void 0===a&&(a=Promise);var i=new E(l(t,r,n,o),a);return e.isGeneratorFunction(r)?i:i.next().then((function(t){return t.done?t.value:i.next()}))},w(m),c(m,u,"Generator"),c(m,a,(function(){return this})),c(m,"toString",(function(){return"[object Generator]"})),e.keys=function(t){var e=Object(t),r=[];for(var n in e)r.unshift(n);return function t(){for(;r.length;)if((n=r.pop())in e)return t.value=n,t.done=!1,t;return t.done=!0,t}},e.values=j,O.prototype={constructor:O,reset:function(e){if(this.prev=this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(_),!e)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0][4];if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function n(t){i.type="throw",i.arg=e,r.next=t}for(var o=r.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],i=a[4],u=this.prev,c=a[1],s=a[2];if(-1===a[0])return n("end"),!1;if(!c&&!s)throw Error("try statement without catch or finally");if(null!=a[0]&&a[0]<=u){if(u<c)return this.method="next",this.arg=t,n(c),!0;if(u<s)return n(s),!1}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n[0]>-1&&n[0]<=this.prev&&this.prev<n[2]){var o=n;break}}o&&("break"===t||"continue"===t)&&o[0]<=e&&e<=o[2]&&(o=null);var a=o?o[4]:{};return a.type=t,a.arg=e,o?(this.method="next",this.next=o[2],h):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),h},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r[2]===t)return this.complete(r[4],r[3]),_(r),h}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r[0]===t){var n=r[4];if("throw"===n.type){var o=n.arg;_(r)}return o}}throw Error("illegal catch attempt")},delegateYield:function(e,r,n){return this.delegate={i:j(e),r:r,n:n},"next"===this.method&&(this.arg=t),h}},e}var l=function(t){return[t.table].concat(t.queryKey).join("_")},f=["where"],h={match:function(t,e){return void 0===e&&(e={}),t.match(e)},or:function(t,e){void 0===e&&(e=[]);for(var r,n=i(e);!(r=n()).done;)t=t.or(r.value);return t},filter:function(t,e){void 0===e&&(e={});for(var r=0,n=Object.entries(e);r<n.length;r++){var o=n[r];t.filter.apply(t,[o[0]].concat(o[1]))}return t},not:function(t,e){void 0===e&&(e={});for(var r=0,n=Object.keys(e);r<n.length;r++){var o=n[r],a=e[o];t=t.textSearch(o,a.text,a.options)}return t},limit:function(t,e){return t.limit(e)},single:function(t){return t.single()},maybeSingle:function(t){return t.maybeSingle()},csv:function(t){return t.csv()},explain:function(t,e){return void 0===e&&(e={}),t.explain(e)},order:function(t,e){for(var r=0,n=Object.entries(e);r<n.length;r++){var o=n[r];t=t.order(o[0],o[1])}return t},range:function(t,e){var r=function(e){return t.range(e.from,e.to,e.options)};if(!Array.isArray(e))return r(e);for(var n,o=i(e);!(n=o()).done;)t=r(n.value);return t}},v=function(t,e){for(var r=t.where,n=u({},void 0===r?{}:r,c(t,f)),o=["eq","neq","in","is","lt","lte","gt","gte","like","ilike","contains","containedBy","rangeGt","rangeGte","rangeLt","rangeLte","rangeAdjacent","contains","containedBy","rangeGt","rangeGte","rangeLt","rangeLte","rangeAdjacent","overlaps"],a=0,i=Object.entries(n);a<i.length;a++){var s=i[a],l=s[1],v=s[0],p=h[v];if(p)e=p(e,l);else if(o.includes(v))for(var y=0,d=Object.entries(l);y<d.length;y++){var g=d[y];e=e[v](g[0],g[1])}}return e},p=["table","schema","column","count","options","single","enabled"],y=["queryKey"],d=["table","schema","column","options","enabled","count"],g=["initialData","queryKey"],b=function(e){return{useSupaInfiniteQuery:function(r){var n=r.table,o=r.schema,i=void 0===o?"public":o,f=r.column,h=void 0===f?"*":f,p=r.options,y=r.enabled,b=r.count,m=void 0===b?"exact":b,w=c(r,d),E=function(){var t=a(s().mark((function t(r){var o,a,u,c,l,f,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return o=i?e.schema(i):e,a=o.from(n).select(h,{count:m}),u=v(w,a),t.next=5,u.abortSignal(r);case 5:if(l=(c=t.sent).data,p=c.count,!(f=c.error)){t.next=11;break}throw f;case 11:return t.abrupt("return",{count:null!=p?p:0,payload:null!=l?l:[]});case 12:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}(),S=p.queryKey,x=void 0===S?[]:S,_=c(p,g),O=l({table:n,queryKey:["infinite"].concat(x)});return t.useInfiniteQuery(u({queryKey:[O,w.where,w.limit],queryFn:function(t){return E(t.signal)},enabled:y},_))},useSupaQuery:function(r){var n=r.table,o=r.schema,i=void 0===o?"public":o,f=r.column,h=void 0===f?"*":f,d=r.count,g=r.options,b=void 0===g?{}:g,m=r.single,w=r.enabled,E=c(r,p),S=function(){var t=a(s().mark((function t(r){var o,a,u,c,l,f,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return o=i?e.schema(i):e,a=o.from(n).select(h,{count:d}),u=v(E,a),t.next=5,u.abortSignal(r);case 5:if(l=(c=t.sent).data,p=c.count,!(f=c.error)){t.next=11;break}throw f;case 11:return t.abrupt("return",{count:null!=p?p:0,payload:m?null!=l?l:{}:null!=l?l:[]});case 12:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}(),x=b.queryKey,_=void 0===x?[]:x,O=c(b,y),j=l({table:n,queryKey:_});return t.useQuery(u({queryKey:[j,E.where,E.limit,m,d,n,h,d,b,m,w],queryFn:function(t){return S(t.signal)},enabled:w},O))},QueryBuilder:v}};exports.QueryBuilder=v,exports.createSupabaseQuery=b,exports.createSupabaseTools=function(n){var o=b(n),i=function(n,o){var a=function(t){var r=t.table,o=t.schema,a=t.event,i=void 0===a?"*":a,u=t.where,c=t.type,s=void 0===c?"postgres_changes":c,l=t.channel,f=void 0===l?"general":l,h=t.callback,v=void 0===h?function(t){return console.log(t)}:h,p=e.useMemo((function(){if(!u)return"";var t=(null==u?void 0:u.key)+"="+(null==u?void 0:u.operator);return"in"===(null==u?void 0:u.operator)?t+".("+(null==u?void 0:u.value.toString())+")":t+"."+(null==u?void 0:u.value)}),[u]),y=e.useMemo((function(){return p?{event:i,schema:o,table:r,filter:p}:{event:i,schema:o,table:r}}),[i,o,r,p]);e.useEffect((function(){var t=n.channel(f).on(s,y,v).subscribe();return function(){t.unsubscribe()}}),[v,y,s,f])};return{useSupaSubscription:a,useSupaRealtime:function(n){var i=n.table,u=n.where,c=n.channel,s=void 0===c?"general":c,l=n.schema,f=void 0===l?"public":l,h=t.useQueryClient(),v=e.useMemo((function(){var t,e;return u?((e={})[u.operator]=((t={})[u.key]=u.value,t),e):{}}),[u]),p=[[i,"subscription"].join("_"),v,null,null,"estimated"],y=o({table:i,schema:f,where:v,options:{queryKey:["subscription"]},count:"estimated"});return a({table:i,schema:f,event:"*",where:u,type:"postgres_changes",channel:s,callback:function(t){var e,n=((e={})[r.REALTIME_POSTGRES_CHANGES_LISTEN_EVENT.DELETE]=function(){h.setQueryData(p,(function(t){var e=t.payload.filter((function(t){return t.id!==e.old.id}));return{count:e.length,payload:e}}))},e[r.REALTIME_POSTGRES_CHANGES_LISTEN_EVENT.INSERT]=function(){h.setQueryData(p,(function(e){var r=[].concat(e.payload,[t.new]);return{count:r.length,payload:r}}))},e[r.REALTIME_POSTGRES_CHANGES_LISTEN_EVENT.UPDATE]=function(){h.setQueryData(p,(function(t){var e=t.payload.map((function(t){return t.id===e.old.id?e.new:t}));return{count:e.length,payload:e}}))},e);n[t.eventType]()}}),y}}}(n,o.useSupaQuery);return u({useSupabase:function(){return e.useMemo((function(){return n}),[])},useSupaSession:function(){return t.useQuery({queryKey:["supa_session",n],initialData:null,queryFn:(e=a(s().mark((function t(){var e,r,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,n.auth.getSession();case 2:if(r=(e=t.sent).data,!(o=e.error)){t.next=7;break}throw o;case 7:return t.abrupt("return",r.session);case 8:case"end":return t.stop()}}),t)}))),function(){return e.apply(this,arguments)})});var e},getSupaQueryName:function(t){return l({table:t.table,queryKey:t.queryKey})}},o,i)};
//# sourceMappingURL=react.cjs.production.min.js.map