@diephp/vue-apix 0.1.2 → 0.1.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 +6 -0
- package/dist/vue-apix.cjs +1 -1
- package/dist/vue-apix.js +202 -201
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -41,6 +41,12 @@ apix.setHeader({
|
|
|
41
41
|
'x-app': 'admin',
|
|
42
42
|
});
|
|
43
43
|
|
|
44
|
+
// Header names are case-insensitive ('Content-Type' and 'content-type' are the same key).
|
|
45
|
+
// Content-Type is auto-set to application/json only when a request has a JSON body and
|
|
46
|
+
// you haven't configured it yourself; configure it explicitly to send it on every request
|
|
47
|
+
// (including GET). For FormData bodies any content-type is dropped so the browser can
|
|
48
|
+
// set the multipart boundary.
|
|
49
|
+
|
|
44
50
|
// fires with true when the first request starts, false when the last one ends —
|
|
45
51
|
// perfect for a global progress bar / spinner
|
|
46
52
|
apix.onProcessing((active) => progressBar.toggle(active));
|
package/dist/vue-apix.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const _=require("vue");function c(i){const t=_.toRaw(i);if(t===null||typeof t!="object")return t;if(Array.isArray(t))return t.map(s=>c(s));if(t instanceof Date)return new Date(t.getTime());const e=Object.getPrototypeOf(t);if(e===Object.prototype||e===null){const s={};for(const r of Object.keys(t))s[r]=c(t[r]);return s}return t}function b(i){if(i===null||typeof i!="object")return!1;const t=Object.getPrototypeOf(i);return t===Object.prototype||t===null}function A(i,t){if(!b(t))return i===void 0?c(t):i;if(i==null)return c(t);if(!b(i))return i;for(const e of Object.keys(t))i[e]=e in i?A(i[e],t[e]):c(t[e]);return i}function f(i,t){if(i==null)return;let e=i;for(const s of t.split(".")){if(e==null)return;e=e[s]}return e}function m(i,t,e){const s=t.split(".");let r=i;for(let o=0;o<s.length-1;o++){const a=s[o];(r[a]===null||typeof r[a]!="object")&&(r[a]={}),r=r[a]}r[s[s.length-1]]=e}function w(i,t){const e={};for(const s of t){const r=f(i,s);r!==void 0&&m(e,s,c(r))}return e}function x(i){const t={};for(const[e,s]of Object.entries(i??{}))s!=null&&(t[e]=Array.isArray(s)?String(s[0]):String(s));return t}function p(i){return i&&typeof i=="object"&&typeof i.message=="string"?i.message:null}function S(i,t){const e=i&&typeof i=="object"&&!Array.isArray(i)&&"data"in i?i.data:i;return t?f(e,t):e}class y extends Error{constructor(t,e,s){super(p(e)??`Request failed with status ${t}`),this.name="ApixError",this.status=t,this.body=e,this.response=s,this.errors=e&&typeof e=="object"&&e.errors&&typeof e.errors=="object"?e.errors:{}}get isValidationError(){return this.status>=400&&this.status<500&&Object.keys(this.errors).length>0}}function R(i){return i instanceof Error&&i.name==="AbortError"}function T(i){const t=[],e=(r,o)=>{t.push(`${encodeURIComponent(r)}=${encodeURIComponent(o)}`)},s=(r,o)=>{if(r!=null){if(typeof r=="boolean")return e(o,r?"1":"0");if(r instanceof Date)return e(o,r.toISOString());if(Array.isArray(r)){r.forEach((a,n)=>s(a,`${o}[${n}]`));return}if(typeof r=="object"){for(const[a,n]of Object.entries(r))s(n,`${o}[${a}]`);return}e(o,String(r))}};for(const[r,o]of Object.entries(i))s(o,r);return t.join("&")}async function k(i){if(i.status===204)return null;const t=await i.text();if(!t)return null;try{return JSON.parse(t)}catch{return t}}class v{constructor(t,e={},s=null){this._snapshot=null,this._snapshotLocked=!1,this._stopWatch=null,this._debounceTimer=null,this._watched=null,this._watchOptions={},this._lastSent=null,this._fillAbort=null,this._transformResponse=null,this._transformRequest=null,this._apix=t,this._options=e,this._source=s,this._transformResponse=e.transformResponse??null,this._transformRequest=e.transformRequest??null;const r=e.default?e.default():null;this._state=_.reactive({data:r,errors:{},message:null,processing:!1}),this._snapshot=c(r)}get data(){return this._state.data}set data(t){this._state.data=t}get errors(){return this._state.errors}get message(){return this._state.message}get processing(){return this._state.processing}get hasErrors(){return Object.keys(this._state.errors).length>0}get isDirty(){return JSON.stringify(this._state.data)!==JSON.stringify(this._snapshot)}get(t){return c(f(this._state.data,t))}setData(t){return this._state.data=c(t),this._proxy}error(t){const e=this._state.errors;if(e[t]!==void 0)return e[t];const s=`${t}.`;for(const r of Object.keys(e))if(r.startsWith(s))return e[r]}setErrors(t){return this._state.errors=x(t),this._proxy}clearErrors(t){if(!t)return this._state.errors={},this._proxy;for(const e of t){const s=`${e}.`;for(const r of Object.keys(this._state.errors))(r===e||r.startsWith(s))&&delete this._state.errors[r]}return this._proxy}reset(t){if(t===void 0||Array.isArray(t)&&t.length===0)return this._state.data=c(this._snapshot),this._state.errors={},this._state.message=null,this._proxy;const e=Array.isArray(t)?t:[t];if(this._state.data&&typeof this._state.data=="object")for(const s of e)m(this._state.data,s,c(f(this._snapshot,s)));return this.clearErrors(e),this._proxy}post(t,e){return this._submit("post",t,e)}put(t,e){return this._submit("put",t,e)}patch(t,e){return this._submit("patch",t,e)}delete(t,e){return this._submit("delete",t,e)}reload(t){var e;return t&&(this._source={url:t,method:((e=this._source)==null?void 0:e.method)??"get"}),this._source?this._fill():Promise.reject(new Error("[vue-apix] The model has no source URL. Use apix.getForm(url) or model.reload(url)."))}transformResponse(t){return this._transformResponse=t,this._proxy}transformRequest(t){return this._transformRequest=t,this._proxy}watch(t,e){let s=null,r;Array.isArray(t)?(s=t,r=e??{}):r=t??e??{},this._source||console.warn("[vue-apix] model.watch() has no effect until the model has a source URL (getForm/postForm/reload)."),this.unwatch(),this._watched=s,this._watchOptions=r,this._syncLastSent();const o=s&&s.length?s.map(a=>()=>f(this._state.data,a)):()=>this._state.data;return this._stopWatch=_.watch(o,()=>this._onWatchTrigger(),{deep:r.deep??!0}),this._proxy}unwatch(){var t;return(t=this._stopWatch)==null||t.call(this),this._stopWatch=null,this._debounceTimer&&(clearTimeout(this._debounceTimer),this._debounceTimer=null),this._proxy}async _fill(t={}){var o,a,n,h,l;const e=this._source;(o=this._fillAbort)==null||o.abort();const s=new AbortController;this._fillAbort=s,this._state.processing=!0;const r={signal:s.signal};e.method==="get"?r.params={...this._options.params,...t.params}:(r.params=this._options.params,r.data=t.data!==void 0?t.data:this._options.data);try{const u=await this._apix.request(e.method,e.url,r);let d=S(u,this._options.path);if(this._transformResponse){const g=c(d);d=this._transformResponse(g,u)??g}return this._applyFill(d,t.preserve??null),this._state.message=p(u),this._state.errors={},(n=(a=this._options).onSuccess)==null||n.call(a,u,this._proxy),u}catch(u){throw R(u)||(this._applyError(u),(l=(h=this._options).onError)==null||l.call(h,u,this._proxy)),u}finally{this._fillAbort===s&&(this._fillAbort=null,this._state.processing=!1)}}_applyFill(t,e){let s=c(t);this._options.mergeDefaults!==!1&&this._options.default&&(s=A(s,this._options.default()));const r=this._snapshotLocked?null:c(s);if(e&&e.length&&s&&typeof s=="object"&&this._state.data&&typeof this._state.data=="object")for(const o of e){const a=f(this._state.data,o);a!==void 0&&m(s,o,c(a))}this._state.data=s,this._snapshotLocked||(this._snapshot=r,this._snapshotLocked=!0),this._syncLastSent()}_applyError(t){t instanceof y&&(this._state.message=p(t.body)??t.message,t.isValidationError&&(this._state.errors=x(t.errors)))}async _submit(t,e,s={}){var a;const r=e??((a=this._source)==null?void 0:a.url);if(!r)throw new Error(`[vue-apix] The model has no URL. Pass one explicitly: model.${t}(url).`);let o=s.data!==void 0?s.data:s.only?w(this._state.data,s.only):this._state.data??void 0;if(s.data===void 0&&this._transformRequest&&o!==void 0){const n=c(o);o=this._transformRequest(n)??n}this._state.processing=!0,this._state.errors={},this._state.message=null;try{const n=await this._apix.request(t,r,{data:o,params:s.params,headers:s.headers});return this._state.message=p(n),n}catch(n){throw this._applyError(n),n}finally{this._state.processing=!1}}_onWatchTrigger(){if(this._payloadJson()===this._lastSent)return;this._debounceTimer&&clearTimeout(this._debounceTimer);const t=this._watchOptions.debounce??0;t>0?this._debounceTimer=setTimeout(()=>this._sendWatchRequest(),t):this._sendWatchRequest()}_sendWatchRequest(){if(this._debounceTimer=null,!this._source)return;const t=this._watched,e=this._watchOptions.payload??t;let s=e&&e.length?w(this._state.data,e):c(this._state.data);this._transformRequest&&(s=this._transformRequest(s)??s),this._lastSent=this._payloadJson();const r=this._watchOptions.refreshWatched?null:t,o=this._source.method==="get"?{params:s,preserve:r}:{data:s,preserve:r};this._fill(o).catch(()=>{})}_payloadJson(){const t=this._watched,e=t&&t.length?t.map(s=>f(this._state.data,s)):this._state.data;return JSON.stringify(e)??"undefined"}_syncLastSent(){this._lastSent=this._payloadJson()}}function j(i,t={},e=null){const s=new v(i,t,e),r=new Proxy(s,{get(o,a){if(typeof a=="symbol"||a in o)return Reflect.get(o,a);const n=o._state.data;if(n&&typeof n=="object")return n[a]},set(o,a,n){if(typeof a=="symbol"||a in o)return Reflect.set(o,a,n);const h=o._state.data;return h&&typeof h=="object"?(h[a]=n,!0):(o._state.data={[a]:n},!0)},has(o,a){if(a in o)return!0;const n=o._state.data;return!!n&&typeof n=="object"&&a in n}});return s._proxy=r,e&&t.immediate!==!1&&r.reload().catch(()=>{}),r}class E{constructor(t={}){this.baseUrl="",this.headers={},this.processingHooks=[],this.errorHooks=[],this.bodyTransformer=null,this.active=0,t.baseUrl&&this.setBaseUrl(t.baseUrl),t.headers&&this.setHeader(t.headers),t.credentials&&(this.credentials=t.credentials)}setBaseUrl(t){return this.baseUrl=t.replace(/\/+$/,""),this}setHeader(t){return Object.assign(this.headers,t),this}setCredentials(t){return this.credentials=t,this}onProcessing(t){return this.processingHooks.push(t),()=>{this.processingHooks=this.processingHooks.filter(e=>e!==t)}}onError(t){return this.errorHooks.push(t),()=>{this.errorHooks=this.errorHooks.filter(e=>e!==t)}}transformResponse(t){return this.bodyTransformer=t,this}async request(t,e,s={}){let r=/^https?:\/\//i.test(e)?e:this.baseUrl+e;if(s.params){const h=T(s.params);h&&(r+=(r.includes("?")?"&":"?")+h)}const o={Accept:"application/json","X-Requested-With":"XMLHttpRequest"};for(const[h,l]of Object.entries(this.headers)){const u=typeof l=="function"?l():l;u!=null&&(o[h]=u)}Object.assign(o,s.headers);const a={method:t.toUpperCase(),headers:o,signal:s.signal};this.credentials&&(a.credentials=this.credentials),s.data!==void 0&&t!=="get"&&(typeof FormData<"u"&&s.data instanceof FormData?a.body=s.data:(o["Content-Type"]="application/json",a.body=JSON.stringify(s.data)));const n={method:t,url:r,options:s};this.trackStart();try{const h=await fetch(r,a);let l=await k(h);if(this.bodyTransformer&&(l=this.bodyTransformer(l,n)??l),!h.ok)throw new y(h.status,l,h);return l}catch(h){throw R(h)?h:this.emitError(h,n)}finally{this.trackEnd()}}get(t,e,s={}){return this.request("get",t,e?{...s,params:e}:s)}post(t,e,s={}){return this.request("post",t,e!==void 0?{...s,data:e}:s)}put(t,e,s={}){return this.request("put",t,e!==void 0?{...s,data:e}:s)}patch(t,e,s={}){return this.request("patch",t,e!==void 0?{...s,data:e}:s)}delete(t,e,s={}){return this.request("delete",t,e!==void 0?{...s,data:e}:s)}form(t,e={}){return j(this,{...e,default:typeof t=="function"?t:()=>c(t)})}getForm(t,e){return this.createFormModel("get",t,e)}postForm(t,e){return this.createFormModel("post",t,e)}createFormModel(t,e,s){const r=typeof s=="string"?{path:s}:{...s??{}};return j(this,r,{url:e,method:t})}trackStart(){++this.active===1&&this.emitProcessing(!0)}trackEnd(){--this.active===0&&this.emitProcessing(!1)}emitProcessing(t){for(const e of this.processingHooks)try{e(t)}catch{}}emitError(t,e){let s=t;for(const r of this.errorHooks)try{const o=r(s,e);o instanceof Error&&(s=o)}catch{}return s}}function O(i={}){return new E(i)}const q=O();exports.Apix=E;exports.ApixError=y;exports.ApixModel=v;exports.apix=q;exports.createApix=O;exports.default=q;
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const _=require("vue");function c(i){const t=_.toRaw(i);if(t===null||typeof t!="object")return t;if(Array.isArray(t))return t.map(s=>c(s));if(t instanceof Date)return new Date(t.getTime());const e=Object.getPrototypeOf(t);if(e===Object.prototype||e===null){const s={};for(const r of Object.keys(t))s[r]=c(t[r]);return s}return t}function b(i){if(i===null||typeof i!="object")return!1;const t=Object.getPrototypeOf(i);return t===Object.prototype||t===null}function A(i,t){if(!b(t))return i===void 0?c(t):i;if(i==null)return c(t);if(!b(i))return i;for(const e of Object.keys(t))i[e]=e in i?A(i[e],t[e]):c(t[e]);return i}function f(i,t){if(i==null)return;let e=i;for(const s of t.split(".")){if(e==null)return;e=e[s]}return e}function y(i,t,e){const s=t.split(".");let r=i;for(let o=0;o<s.length-1;o++){const a=s[o];(r[a]===null||typeof r[a]!="object")&&(r[a]={}),r=r[a]}r[s[s.length-1]]=e}function w(i,t){const e={};for(const s of t){const r=f(i,s);r!==void 0&&y(e,s,c(r))}return e}function x(i){const t={};for(const[e,s]of Object.entries(i??{}))s!=null&&(t[e]=Array.isArray(s)?String(s[0]):String(s));return t}function p(i){return i&&typeof i=="object"&&typeof i.message=="string"?i.message:null}function S(i,t){const e=i&&typeof i=="object"&&!Array.isArray(i)&&"data"in i?i.data:i;return t?f(e,t):e}class m extends Error{constructor(t,e,s){super(p(e)??`Request failed with status ${t}`),this.name="ApixError",this.status=t,this.body=e,this.response=s,this.errors=e&&typeof e=="object"&&e.errors&&typeof e.errors=="object"?e.errors:{}}get isValidationError(){return this.status>=400&&this.status<500&&Object.keys(this.errors).length>0}}function R(i){return i instanceof Error&&i.name==="AbortError"}function k(i){const t=[],e=(r,o)=>{t.push(`${encodeURIComponent(r)}=${encodeURIComponent(o)}`)},s=(r,o)=>{if(r!=null){if(typeof r=="boolean")return e(o,r?"1":"0");if(r instanceof Date)return e(o,r.toISOString());if(Array.isArray(r)){r.forEach((a,n)=>s(a,`${o}[${n}]`));return}if(typeof r=="object"){for(const[a,n]of Object.entries(r))s(n,`${o}[${a}]`);return}e(o,String(r))}};for(const[r,o]of Object.entries(i))s(o,r);return t.join("&")}async function T(i){if(i.status===204)return null;const t=await i.text();if(!t)return null;try{return JSON.parse(t)}catch{return t}}class v{constructor(t,e={},s=null){this._snapshot=null,this._snapshotLocked=!1,this._stopWatch=null,this._debounceTimer=null,this._watched=null,this._watchOptions={},this._lastSent=null,this._fillAbort=null,this._transformResponse=null,this._transformRequest=null,this._apix=t,this._options=e,this._source=s,this._transformResponse=e.transformResponse??null,this._transformRequest=e.transformRequest??null;const r=e.default?e.default():null;this._state=_.reactive({data:r,errors:{},message:null,processing:!1}),this._snapshot=c(r)}get data(){return this._state.data}set data(t){this._state.data=t}get errors(){return this._state.errors}get message(){return this._state.message}get processing(){return this._state.processing}get hasErrors(){return Object.keys(this._state.errors).length>0}get isDirty(){return JSON.stringify(this._state.data)!==JSON.stringify(this._snapshot)}get(t){return c(f(this._state.data,t))}setData(t){return this._state.data=c(t),this._proxy}error(t){const e=this._state.errors;if(e[t]!==void 0)return e[t];const s=`${t}.`;for(const r of Object.keys(e))if(r.startsWith(s))return e[r]}setErrors(t){return this._state.errors=x(t),this._proxy}clearErrors(t){if(!t)return this._state.errors={},this._proxy;for(const e of t){const s=`${e}.`;for(const r of Object.keys(this._state.errors))(r===e||r.startsWith(s))&&delete this._state.errors[r]}return this._proxy}reset(t){if(t===void 0||Array.isArray(t)&&t.length===0)return this._state.data=c(this._snapshot),this._state.errors={},this._state.message=null,this._proxy;const e=Array.isArray(t)?t:[t];if(this._state.data&&typeof this._state.data=="object")for(const s of e)y(this._state.data,s,c(f(this._snapshot,s)));return this.clearErrors(e),this._proxy}post(t,e){return this._submit("post",t,e)}put(t,e){return this._submit("put",t,e)}patch(t,e){return this._submit("patch",t,e)}delete(t,e){return this._submit("delete",t,e)}reload(t){var e;return t&&(this._source={url:t,method:((e=this._source)==null?void 0:e.method)??"get"}),this._source?this._fill():Promise.reject(new Error("[vue-apix] The model has no source URL. Use apix.getForm(url) or model.reload(url)."))}transformResponse(t){return this._transformResponse=t,this._proxy}transformRequest(t){return this._transformRequest=t,this._proxy}watch(t,e){let s=null,r;Array.isArray(t)?(s=t,r=e??{}):r=t??e??{},this._source||console.warn("[vue-apix] model.watch() has no effect until the model has a source URL (getForm/postForm/reload)."),this.unwatch(),this._watched=s,this._watchOptions=r,this._syncLastSent();const o=s&&s.length?s.map(a=>()=>f(this._state.data,a)):()=>this._state.data;return this._stopWatch=_.watch(o,()=>this._onWatchTrigger(),{deep:r.deep??!0}),this._proxy}unwatch(){var t;return(t=this._stopWatch)==null||t.call(this),this._stopWatch=null,this._debounceTimer&&(clearTimeout(this._debounceTimer),this._debounceTimer=null),this._proxy}async _fill(t={}){var o,a,n,h,u;const e=this._source;(o=this._fillAbort)==null||o.abort();const s=new AbortController;this._fillAbort=s,this._state.processing=!0;const r={signal:s.signal};e.method==="get"?r.params={...this._options.params,...t.params}:(r.params=this._options.params,r.data=t.data!==void 0?t.data:this._options.data);try{const l=await this._apix.request(e.method,e.url,r);let d=S(l,this._options.path);if(this._transformResponse){const g=c(d);d=this._transformResponse(g,l)??g}return this._applyFill(d,t.preserve??null),this._state.message=p(l),this._state.errors={},(n=(a=this._options).onSuccess)==null||n.call(a,l,this._proxy),l}catch(l){throw R(l)||(this._applyError(l),(u=(h=this._options).onError)==null||u.call(h,l,this._proxy)),l}finally{this._fillAbort===s&&(this._fillAbort=null,this._state.processing=!1)}}_applyFill(t,e){let s=c(t);this._options.mergeDefaults!==!1&&this._options.default&&(s=A(s,this._options.default()));const r=this._snapshotLocked?null:c(s);if(e&&e.length&&s&&typeof s=="object"&&this._state.data&&typeof this._state.data=="object")for(const o of e){const a=f(this._state.data,o);a!==void 0&&y(s,o,c(a))}this._state.data=s,this._snapshotLocked||(this._snapshot=r,this._snapshotLocked=!0),this._syncLastSent()}_applyError(t){t instanceof m&&(this._state.message=p(t.body)??t.message,t.isValidationError&&(this._state.errors=x(t.errors)))}async _submit(t,e,s={}){var a;const r=e??((a=this._source)==null?void 0:a.url);if(!r)throw new Error(`[vue-apix] The model has no URL. Pass one explicitly: model.${t}(url).`);let o=s.data!==void 0?s.data:s.only?w(this._state.data,s.only):this._state.data??void 0;if(s.data===void 0&&this._transformRequest&&o!==void 0){const n=c(o);o=this._transformRequest(n)??n}this._state.processing=!0,this._state.errors={},this._state.message=null;try{const n=await this._apix.request(t,r,{data:o,params:s.params,headers:s.headers});return this._state.message=p(n),n}catch(n){throw this._applyError(n),n}finally{this._state.processing=!1}}_onWatchTrigger(){if(this._payloadJson()===this._lastSent)return;this._debounceTimer&&clearTimeout(this._debounceTimer);const t=this._watchOptions.debounce??0;t>0?this._debounceTimer=setTimeout(()=>this._sendWatchRequest(),t):this._sendWatchRequest()}_sendWatchRequest(){if(this._debounceTimer=null,!this._source)return;const t=this._watched,e=this._watchOptions.payload??t;let s=e&&e.length?w(this._state.data,e):c(this._state.data);this._transformRequest&&(s=this._transformRequest(s)??s),this._lastSent=this._payloadJson();const r=this._watchOptions.refreshWatched?null:t,o=this._source.method==="get"?{params:s,preserve:r}:{data:s,preserve:r};this._fill(o).catch(()=>{})}_payloadJson(){const t=this._watched,e=t&&t.length?t.map(s=>f(this._state.data,s)):this._state.data;return JSON.stringify(e)??"undefined"}_syncLastSent(){this._lastSent=this._payloadJson()}}function j(i,t={},e=null){const s=new v(i,t,e),r=new Proxy(s,{get(o,a){if(typeof a=="symbol"||a in o)return Reflect.get(o,a);const n=o._state.data;if(n&&typeof n=="object")return n[a]},set(o,a,n){if(typeof a=="symbol"||a in o)return Reflect.set(o,a,n);const h=o._state.data;return h&&typeof h=="object"?(h[a]=n,!0):(o._state.data={[a]:n},!0)},has(o,a){if(a in o)return!0;const n=o._state.data;return!!n&&typeof n=="object"&&a in n}});return s._proxy=r,e&&t.immediate!==!1&&r.reload().catch(()=>{}),r}class E{constructor(t={}){this.baseUrl="",this.headers={},this.processingHooks=[],this.errorHooks=[],this.bodyTransformer=null,this.active=0,t.baseUrl&&this.setBaseUrl(t.baseUrl),t.headers&&this.setHeader(t.headers),t.credentials&&(this.credentials=t.credentials)}setBaseUrl(t){return this.baseUrl=t.replace(/\/+$/,""),this}setHeader(t){return Object.assign(this.headers,t),this}setCredentials(t){return this.credentials=t,this}onProcessing(t){return this.processingHooks.push(t),()=>{this.processingHooks=this.processingHooks.filter(e=>e!==t)}}onError(t){return this.errorHooks.push(t),()=>{this.errorHooks=this.errorHooks.filter(e=>e!==t)}}transformResponse(t){return this.bodyTransformer=t,this}async request(t,e,s={}){let r=/^https?:\/\//i.test(e)?e:this.baseUrl+e;if(s.params){const h=k(s.params);h&&(r+=(r.includes("?")?"&":"?")+h)}const o={accept:"application/json","x-requested-with":"XMLHttpRequest"};for(const[h,u]of Object.entries(this.headers)){const l=typeof u=="function"?u():u;l!=null&&(o[h.toLowerCase()]=l)}for(const[h,u]of Object.entries(s.headers??{}))o[h.toLowerCase()]=u;const a={method:t.toUpperCase(),headers:o,signal:s.signal};this.credentials&&(a.credentials=this.credentials),s.data!==void 0&&t!=="get"&&(typeof FormData<"u"&&s.data instanceof FormData?(delete o["content-type"],a.body=s.data):(o["content-type"]??(o["content-type"]="application/json"),a.body=JSON.stringify(s.data)));const n={method:t,url:r,options:s};this.trackStart();try{const h=await fetch(r,a);let u=await T(h);if(this.bodyTransformer&&(u=this.bodyTransformer(u,n)??u),!h.ok)throw new m(h.status,u,h);return u}catch(h){throw R(h)?h:this.emitError(h,n)}finally{this.trackEnd()}}get(t,e,s={}){return this.request("get",t,e?{...s,params:e}:s)}post(t,e,s={}){return this.request("post",t,e!==void 0?{...s,data:e}:s)}put(t,e,s={}){return this.request("put",t,e!==void 0?{...s,data:e}:s)}patch(t,e,s={}){return this.request("patch",t,e!==void 0?{...s,data:e}:s)}delete(t,e,s={}){return this.request("delete",t,e!==void 0?{...s,data:e}:s)}form(t,e={}){return j(this,{...e,default:typeof t=="function"?t:()=>c(t)})}getForm(t,e){return this.createFormModel("get",t,e)}postForm(t,e){return this.createFormModel("post",t,e)}createFormModel(t,e,s){const r=typeof s=="string"?{path:s}:{...s??{}};return j(this,r,{url:e,method:t})}trackStart(){++this.active===1&&this.emitProcessing(!0)}trackEnd(){--this.active===0&&this.emitProcessing(!1)}emitProcessing(t){for(const e of this.processingHooks)try{e(t)}catch{}}emitError(t,e){let s=t;for(const r of this.errorHooks)try{const o=r(s,e);o instanceof Error&&(s=o)}catch{}return s}}function O(i={}){return new E(i)}const q=O();exports.Apix=E;exports.ApixError=m;exports.ApixModel=v;exports.apix=q;exports.createApix=O;exports.default=q;
|
package/dist/vue-apix.js
CHANGED
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
import { toRaw as A, reactive as
|
|
2
|
-
function c(
|
|
3
|
-
const t = A(
|
|
1
|
+
import { toRaw as A, reactive as v, watch as E } from "vue";
|
|
2
|
+
function c(o) {
|
|
3
|
+
const t = A(o);
|
|
4
4
|
if (t === null || typeof t != "object") return t;
|
|
5
|
-
if (Array.isArray(t)) return t.map((
|
|
5
|
+
if (Array.isArray(t)) return t.map((s) => c(s));
|
|
6
6
|
if (t instanceof Date) return new Date(t.getTime());
|
|
7
|
-
const
|
|
8
|
-
if (
|
|
9
|
-
const
|
|
10
|
-
for (const r of Object.keys(t))
|
|
11
|
-
return
|
|
7
|
+
const e = Object.getPrototypeOf(t);
|
|
8
|
+
if (e === Object.prototype || e === null) {
|
|
9
|
+
const s = {};
|
|
10
|
+
for (const r of Object.keys(t)) s[r] = c(t[r]);
|
|
11
|
+
return s;
|
|
12
12
|
}
|
|
13
13
|
return t;
|
|
14
14
|
}
|
|
15
|
-
function y(
|
|
16
|
-
if (
|
|
17
|
-
const t = Object.getPrototypeOf(
|
|
15
|
+
function y(o) {
|
|
16
|
+
if (o === null || typeof o != "object") return !1;
|
|
17
|
+
const t = Object.getPrototypeOf(o);
|
|
18
18
|
return t === Object.prototype || t === null;
|
|
19
19
|
}
|
|
20
|
-
function x(
|
|
20
|
+
function x(o, t) {
|
|
21
21
|
if (!y(t))
|
|
22
|
-
return
|
|
23
|
-
if (
|
|
24
|
-
if (!y(
|
|
25
|
-
for (const
|
|
26
|
-
|
|
27
|
-
return
|
|
22
|
+
return o === void 0 ? c(t) : o;
|
|
23
|
+
if (o == null) return c(t);
|
|
24
|
+
if (!y(o)) return o;
|
|
25
|
+
for (const e of Object.keys(t))
|
|
26
|
+
o[e] = e in o ? x(o[e], t[e]) : c(t[e]);
|
|
27
|
+
return o;
|
|
28
28
|
}
|
|
29
|
-
function f(
|
|
30
|
-
if (
|
|
31
|
-
let
|
|
32
|
-
for (const
|
|
33
|
-
if (
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
return
|
|
29
|
+
function f(o, t) {
|
|
30
|
+
if (o == null) return;
|
|
31
|
+
let e = o;
|
|
32
|
+
for (const s of t.split(".")) {
|
|
33
|
+
if (e == null) return;
|
|
34
|
+
e = e[s];
|
|
35
|
+
}
|
|
36
|
+
return e;
|
|
37
37
|
}
|
|
38
|
-
function d(
|
|
39
|
-
const
|
|
40
|
-
let r =
|
|
41
|
-
for (let
|
|
42
|
-
const a =
|
|
38
|
+
function d(o, t, e) {
|
|
39
|
+
const s = t.split(".");
|
|
40
|
+
let r = o;
|
|
41
|
+
for (let i = 0; i < s.length - 1; i++) {
|
|
42
|
+
const a = s[i];
|
|
43
43
|
(r[a] === null || typeof r[a] != "object") && (r[a] = {}), r = r[a];
|
|
44
44
|
}
|
|
45
|
-
r[
|
|
45
|
+
r[s[s.length - 1]] = e;
|
|
46
46
|
}
|
|
47
|
-
function g(
|
|
48
|
-
const
|
|
49
|
-
for (const
|
|
50
|
-
const r = f(
|
|
51
|
-
r !== void 0 && d(
|
|
47
|
+
function g(o, t) {
|
|
48
|
+
const e = {};
|
|
49
|
+
for (const s of t) {
|
|
50
|
+
const r = f(o, s);
|
|
51
|
+
r !== void 0 && d(e, s, c(r));
|
|
52
52
|
}
|
|
53
|
-
return
|
|
53
|
+
return e;
|
|
54
54
|
}
|
|
55
|
-
function b(
|
|
55
|
+
function b(o) {
|
|
56
56
|
const t = {};
|
|
57
|
-
for (const [
|
|
58
|
-
|
|
57
|
+
for (const [e, s] of Object.entries(o ?? {}))
|
|
58
|
+
s != null && (t[e] = Array.isArray(s) ? String(s[0]) : String(s));
|
|
59
59
|
return t;
|
|
60
60
|
}
|
|
61
|
-
function p(
|
|
62
|
-
return
|
|
61
|
+
function p(o) {
|
|
62
|
+
return o && typeof o == "object" && typeof o.message == "string" ? o.message : null;
|
|
63
63
|
}
|
|
64
|
-
function
|
|
65
|
-
const
|
|
66
|
-
return t ? f(
|
|
64
|
+
function O(o, t) {
|
|
65
|
+
const e = o && typeof o == "object" && !Array.isArray(o) && "data" in o ? o.data : o;
|
|
66
|
+
return t ? f(e, t) : e;
|
|
67
67
|
}
|
|
68
68
|
class j extends Error {
|
|
69
|
-
constructor(t,
|
|
70
|
-
super(p(
|
|
69
|
+
constructor(t, e, s) {
|
|
70
|
+
super(p(e) ?? `Request failed with status ${t}`), this.name = "ApixError", this.status = t, this.body = e, this.response = s, this.errors = e && typeof e == "object" && e.errors && typeof e.errors == "object" ? e.errors : {};
|
|
71
71
|
}
|
|
72
72
|
/**
|
|
73
73
|
* True for any 4xx response whose body carries an `errors` object
|
|
@@ -78,33 +78,33 @@ class j extends Error {
|
|
|
78
78
|
return this.status >= 400 && this.status < 500 && Object.keys(this.errors).length > 0;
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
|
-
function R(
|
|
82
|
-
return
|
|
81
|
+
function R(o) {
|
|
82
|
+
return o instanceof Error && o.name === "AbortError";
|
|
83
83
|
}
|
|
84
|
-
function
|
|
85
|
-
const t = [],
|
|
86
|
-
t.push(`${encodeURIComponent(r)}=${encodeURIComponent(
|
|
87
|
-
},
|
|
84
|
+
function q(o) {
|
|
85
|
+
const t = [], e = (r, i) => {
|
|
86
|
+
t.push(`${encodeURIComponent(r)}=${encodeURIComponent(i)}`);
|
|
87
|
+
}, s = (r, i) => {
|
|
88
88
|
if (r != null) {
|
|
89
|
-
if (typeof r == "boolean") return
|
|
90
|
-
if (r instanceof Date) return
|
|
89
|
+
if (typeof r == "boolean") return e(i, r ? "1" : "0");
|
|
90
|
+
if (r instanceof Date) return e(i, r.toISOString());
|
|
91
91
|
if (Array.isArray(r)) {
|
|
92
|
-
r.forEach((a, n) =>
|
|
92
|
+
r.forEach((a, n) => s(a, `${i}[${n}]`));
|
|
93
93
|
return;
|
|
94
94
|
}
|
|
95
95
|
if (typeof r == "object") {
|
|
96
|
-
for (const [a, n] of Object.entries(r))
|
|
96
|
+
for (const [a, n] of Object.entries(r)) s(n, `${i}[${a}]`);
|
|
97
97
|
return;
|
|
98
98
|
}
|
|
99
|
-
|
|
99
|
+
e(i, String(r));
|
|
100
100
|
}
|
|
101
101
|
};
|
|
102
|
-
for (const [r,
|
|
102
|
+
for (const [r, i] of Object.entries(o)) s(i, r);
|
|
103
103
|
return t.join("&");
|
|
104
104
|
}
|
|
105
|
-
async function
|
|
106
|
-
if (
|
|
107
|
-
const t = await
|
|
105
|
+
async function k(o) {
|
|
106
|
+
if (o.status === 204) return null;
|
|
107
|
+
const t = await o.text();
|
|
108
108
|
if (!t) return null;
|
|
109
109
|
try {
|
|
110
110
|
return JSON.parse(t);
|
|
@@ -112,11 +112,11 @@ async function S(i) {
|
|
|
112
112
|
return t;
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
|
-
class
|
|
116
|
-
constructor(t,
|
|
117
|
-
this._snapshot = null, this._snapshotLocked = !1, this._stopWatch = null, this._debounceTimer = null, this._watched = null, this._watchOptions = {}, this._lastSent = null, this._fillAbort = null, this._transformResponse = null, this._transformRequest = null, this._apix = t, this._options =
|
|
118
|
-
const r =
|
|
119
|
-
this._state =
|
|
115
|
+
class S {
|
|
116
|
+
constructor(t, e = {}, s = null) {
|
|
117
|
+
this._snapshot = null, this._snapshotLocked = !1, this._stopWatch = null, this._debounceTimer = null, this._watched = null, this._watchOptions = {}, this._lastSent = null, this._fillAbort = null, this._transformResponse = null, this._transformRequest = null, this._apix = t, this._options = e, this._source = s, this._transformResponse = e.transformResponse ?? null, this._transformRequest = e.transformRequest ?? null;
|
|
118
|
+
const r = e.default ? e.default() : null;
|
|
119
|
+
this._state = v({ data: r, errors: {}, message: null, processing: !1 }), this._snapshot = c(r);
|
|
120
120
|
}
|
|
121
121
|
// --- reactive state ---------------------------------------------------------
|
|
122
122
|
get data() {
|
|
@@ -160,11 +160,11 @@ class T {
|
|
|
160
160
|
* so `error('name')` also surfaces `name.en.label` messages.
|
|
161
161
|
*/
|
|
162
162
|
error(t) {
|
|
163
|
-
const
|
|
164
|
-
if (
|
|
165
|
-
const
|
|
166
|
-
for (const r of Object.keys(
|
|
167
|
-
if (r.startsWith(
|
|
163
|
+
const e = this._state.errors;
|
|
164
|
+
if (e[t] !== void 0) return e[t];
|
|
165
|
+
const s = `${t}.`;
|
|
166
|
+
for (const r of Object.keys(e))
|
|
167
|
+
if (r.startsWith(s)) return e[r];
|
|
168
168
|
}
|
|
169
169
|
setErrors(t) {
|
|
170
170
|
return this._state.errors = b(t), this._proxy;
|
|
@@ -173,10 +173,10 @@ class T {
|
|
|
173
173
|
clearErrors(t) {
|
|
174
174
|
if (!t)
|
|
175
175
|
return this._state.errors = {}, this._proxy;
|
|
176
|
-
for (const
|
|
177
|
-
const
|
|
176
|
+
for (const e of t) {
|
|
177
|
+
const s = `${e}.`;
|
|
178
178
|
for (const r of Object.keys(this._state.errors))
|
|
179
|
-
(r ===
|
|
179
|
+
(r === e || r.startsWith(s)) && delete this._state.errors[r];
|
|
180
180
|
}
|
|
181
181
|
return this._proxy;
|
|
182
182
|
}
|
|
@@ -188,29 +188,29 @@ class T {
|
|
|
188
188
|
reset(t) {
|
|
189
189
|
if (t === void 0 || Array.isArray(t) && t.length === 0)
|
|
190
190
|
return this._state.data = c(this._snapshot), this._state.errors = {}, this._state.message = null, this._proxy;
|
|
191
|
-
const
|
|
191
|
+
const e = Array.isArray(t) ? t : [t];
|
|
192
192
|
if (this._state.data && typeof this._state.data == "object")
|
|
193
|
-
for (const
|
|
194
|
-
d(this._state.data,
|
|
195
|
-
return this.clearErrors(
|
|
193
|
+
for (const s of e)
|
|
194
|
+
d(this._state.data, s, c(f(this._snapshot, s)));
|
|
195
|
+
return this.clearErrors(e), this._proxy;
|
|
196
196
|
}
|
|
197
197
|
// --- HTTP ------------------------------------------------------------------------
|
|
198
|
-
post(t,
|
|
199
|
-
return this._submit("post", t,
|
|
198
|
+
post(t, e) {
|
|
199
|
+
return this._submit("post", t, e);
|
|
200
200
|
}
|
|
201
|
-
put(t,
|
|
202
|
-
return this._submit("put", t,
|
|
201
|
+
put(t, e) {
|
|
202
|
+
return this._submit("put", t, e);
|
|
203
203
|
}
|
|
204
|
-
patch(t,
|
|
205
|
-
return this._submit("patch", t,
|
|
204
|
+
patch(t, e) {
|
|
205
|
+
return this._submit("patch", t, e);
|
|
206
206
|
}
|
|
207
|
-
delete(t,
|
|
208
|
-
return this._submit("delete", t,
|
|
207
|
+
delete(t, e) {
|
|
208
|
+
return this._submit("delete", t, e);
|
|
209
209
|
}
|
|
210
210
|
/** Re-run the fill request (or set a new source URL and run it). */
|
|
211
211
|
reload(t) {
|
|
212
|
-
var
|
|
213
|
-
return t && (this._source = { url: t, method: ((
|
|
212
|
+
var e;
|
|
213
|
+
return t && (this._source = { url: t, method: ((e = this._source) == null ? void 0 : e.method) ?? "get" }), this._source ? this._fill() : Promise.reject(
|
|
214
214
|
new Error("[vue-apix] The model has no source URL. Use apix.getForm(url) or model.reload(url).")
|
|
215
215
|
);
|
|
216
216
|
}
|
|
@@ -240,11 +240,11 @@ class T {
|
|
|
240
240
|
* The response updates everything except the watched paths (see WatchOptions).
|
|
241
241
|
* A new trigger aborts the previous in-flight watch request.
|
|
242
242
|
*/
|
|
243
|
-
watch(t,
|
|
244
|
-
let
|
|
245
|
-
Array.isArray(t) ? (
|
|
246
|
-
const
|
|
247
|
-
return this._stopWatch =
|
|
243
|
+
watch(t, e) {
|
|
244
|
+
let s = null, r;
|
|
245
|
+
Array.isArray(t) ? (s = t, r = e ?? {}) : r = t ?? e ?? {}, this._source || console.warn("[vue-apix] model.watch() has no effect until the model has a source URL (getForm/postForm/reload)."), this.unwatch(), this._watched = s, this._watchOptions = r, this._syncLastSent();
|
|
246
|
+
const i = s && s.length ? s.map((a) => () => f(this._state.data, a)) : () => this._state.data;
|
|
247
|
+
return this._stopWatch = E(i, () => this._onWatchTrigger(), {
|
|
248
248
|
deep: r.deep ?? !0
|
|
249
249
|
}), this._proxy;
|
|
250
250
|
}
|
|
@@ -255,57 +255,57 @@ class T {
|
|
|
255
255
|
// --- internals ----------------------------------------------------------------------
|
|
256
256
|
/** @internal */
|
|
257
257
|
async _fill(t = {}) {
|
|
258
|
-
var
|
|
259
|
-
const
|
|
260
|
-
(
|
|
261
|
-
const
|
|
262
|
-
this._fillAbort =
|
|
263
|
-
const r = { signal:
|
|
264
|
-
|
|
258
|
+
var i, a, n, h, u;
|
|
259
|
+
const e = this._source;
|
|
260
|
+
(i = this._fillAbort) == null || i.abort();
|
|
261
|
+
const s = new AbortController();
|
|
262
|
+
this._fillAbort = s, this._state.processing = !0;
|
|
263
|
+
const r = { signal: s.signal };
|
|
264
|
+
e.method === "get" ? r.params = { ...this._options.params, ...t.params } : (r.params = this._options.params, r.data = t.data !== void 0 ? t.data : this._options.data);
|
|
265
265
|
try {
|
|
266
|
-
const
|
|
267
|
-
let _ =
|
|
266
|
+
const l = await this._apix.request(e.method, e.url, r);
|
|
267
|
+
let _ = O(l, this._options.path);
|
|
268
268
|
if (this._transformResponse) {
|
|
269
269
|
const m = c(_);
|
|
270
|
-
_ = this._transformResponse(m,
|
|
270
|
+
_ = this._transformResponse(m, l) ?? m;
|
|
271
271
|
}
|
|
272
|
-
return this._applyFill(_, t.preserve ?? null), this._state.message = p(
|
|
273
|
-
} catch (
|
|
274
|
-
throw R(
|
|
272
|
+
return this._applyFill(_, t.preserve ?? null), this._state.message = p(l), this._state.errors = {}, (n = (a = this._options).onSuccess) == null || n.call(a, l, this._proxy), l;
|
|
273
|
+
} catch (l) {
|
|
274
|
+
throw R(l) || (this._applyError(l), (u = (h = this._options).onError) == null || u.call(h, l, this._proxy)), l;
|
|
275
275
|
} finally {
|
|
276
|
-
this._fillAbort ===
|
|
276
|
+
this._fillAbort === s && (this._fillAbort = null, this._state.processing = !1);
|
|
277
277
|
}
|
|
278
278
|
}
|
|
279
|
-
_applyFill(t,
|
|
280
|
-
let
|
|
281
|
-
this._options.mergeDefaults !== !1 && this._options.default && (
|
|
282
|
-
const r = this._snapshotLocked ? null : c(
|
|
283
|
-
if (
|
|
284
|
-
for (const
|
|
285
|
-
const a = f(this._state.data,
|
|
286
|
-
a !== void 0 && d(
|
|
279
|
+
_applyFill(t, e) {
|
|
280
|
+
let s = c(t);
|
|
281
|
+
this._options.mergeDefaults !== !1 && this._options.default && (s = x(s, this._options.default()));
|
|
282
|
+
const r = this._snapshotLocked ? null : c(s);
|
|
283
|
+
if (e && e.length && s && typeof s == "object" && this._state.data && typeof this._state.data == "object")
|
|
284
|
+
for (const i of e) {
|
|
285
|
+
const a = f(this._state.data, i);
|
|
286
|
+
a !== void 0 && d(s, i, c(a));
|
|
287
287
|
}
|
|
288
|
-
this._state.data =
|
|
288
|
+
this._state.data = s, this._snapshotLocked || (this._snapshot = r, this._snapshotLocked = !0), this._syncLastSent();
|
|
289
289
|
}
|
|
290
290
|
_applyError(t) {
|
|
291
291
|
t instanceof j && (this._state.message = p(t.body) ?? t.message, t.isValidationError && (this._state.errors = b(t.errors)));
|
|
292
292
|
}
|
|
293
|
-
async _submit(t,
|
|
293
|
+
async _submit(t, e, s = {}) {
|
|
294
294
|
var a;
|
|
295
|
-
const r =
|
|
295
|
+
const r = e ?? ((a = this._source) == null ? void 0 : a.url);
|
|
296
296
|
if (!r)
|
|
297
297
|
throw new Error(`[vue-apix] The model has no URL. Pass one explicitly: model.${t}(url).`);
|
|
298
|
-
let
|
|
299
|
-
if (
|
|
300
|
-
const n = c(
|
|
301
|
-
|
|
298
|
+
let i = s.data !== void 0 ? s.data : s.only ? g(this._state.data, s.only) : this._state.data ?? void 0;
|
|
299
|
+
if (s.data === void 0 && this._transformRequest && i !== void 0) {
|
|
300
|
+
const n = c(i);
|
|
301
|
+
i = this._transformRequest(n) ?? n;
|
|
302
302
|
}
|
|
303
303
|
this._state.processing = !0, this._state.errors = {}, this._state.message = null;
|
|
304
304
|
try {
|
|
305
305
|
const n = await this._apix.request(t, r, {
|
|
306
|
-
data:
|
|
307
|
-
params:
|
|
308
|
-
headers:
|
|
306
|
+
data: i,
|
|
307
|
+
params: s.params,
|
|
308
|
+
headers: s.headers
|
|
309
309
|
});
|
|
310
310
|
return this._state.message = p(n), n;
|
|
311
311
|
} catch (n) {
|
|
@@ -322,43 +322,43 @@ class T {
|
|
|
322
322
|
}
|
|
323
323
|
_sendWatchRequest() {
|
|
324
324
|
if (this._debounceTimer = null, !this._source) return;
|
|
325
|
-
const t = this._watched,
|
|
326
|
-
let
|
|
327
|
-
this._transformRequest && (
|
|
328
|
-
const r = this._watchOptions.refreshWatched ? null : t,
|
|
329
|
-
this._fill(
|
|
325
|
+
const t = this._watched, e = this._watchOptions.payload ?? t;
|
|
326
|
+
let s = e && e.length ? g(this._state.data, e) : c(this._state.data);
|
|
327
|
+
this._transformRequest && (s = this._transformRequest(s) ?? s), this._lastSent = this._payloadJson();
|
|
328
|
+
const r = this._watchOptions.refreshWatched ? null : t, i = this._source.method === "get" ? { params: s, preserve: r } : { data: s, preserve: r };
|
|
329
|
+
this._fill(i).catch(() => {
|
|
330
330
|
});
|
|
331
331
|
}
|
|
332
332
|
_payloadJson() {
|
|
333
|
-
const t = this._watched,
|
|
334
|
-
return JSON.stringify(
|
|
333
|
+
const t = this._watched, e = t && t.length ? t.map((s) => f(this._state.data, s)) : this._state.data;
|
|
334
|
+
return JSON.stringify(e) ?? "undefined";
|
|
335
335
|
}
|
|
336
336
|
_syncLastSent() {
|
|
337
337
|
this._lastSent = this._payloadJson();
|
|
338
338
|
}
|
|
339
339
|
}
|
|
340
|
-
function w(
|
|
341
|
-
const
|
|
342
|
-
get(
|
|
343
|
-
if (typeof a == "symbol" || a in
|
|
344
|
-
const n =
|
|
340
|
+
function w(o, t = {}, e = null) {
|
|
341
|
+
const s = new S(o, t, e), r = new Proxy(s, {
|
|
342
|
+
get(i, a) {
|
|
343
|
+
if (typeof a == "symbol" || a in i) return Reflect.get(i, a);
|
|
344
|
+
const n = i._state.data;
|
|
345
345
|
if (n && typeof n == "object") return n[a];
|
|
346
346
|
},
|
|
347
|
-
set(
|
|
348
|
-
if (typeof a == "symbol" || a in
|
|
349
|
-
const h =
|
|
350
|
-
return h && typeof h == "object" ? (h[a] = n, !0) : (
|
|
347
|
+
set(i, a, n) {
|
|
348
|
+
if (typeof a == "symbol" || a in i) return Reflect.set(i, a, n);
|
|
349
|
+
const h = i._state.data;
|
|
350
|
+
return h && typeof h == "object" ? (h[a] = n, !0) : (i._state.data = { [a]: n }, !0);
|
|
351
351
|
},
|
|
352
|
-
has(
|
|
353
|
-
if (a in
|
|
354
|
-
const n =
|
|
352
|
+
has(i, a) {
|
|
353
|
+
if (a in i) return !0;
|
|
354
|
+
const n = i._state.data;
|
|
355
355
|
return !!n && typeof n == "object" && a in n;
|
|
356
356
|
}
|
|
357
357
|
});
|
|
358
|
-
return
|
|
358
|
+
return s._proxy = r, e && t.immediate !== !1 && r.reload().catch(() => {
|
|
359
359
|
}), r;
|
|
360
360
|
}
|
|
361
|
-
class
|
|
361
|
+
class T {
|
|
362
362
|
constructor(t = {}) {
|
|
363
363
|
this.baseUrl = "", this.headers = {}, this.processingHooks = [], this.errorHooks = [], this.bodyTransformer = null, this.active = 0, t.baseUrl && this.setBaseUrl(t.baseUrl), t.headers && this.setHeader(t.headers), t.credentials && (this.credentials = t.credentials);
|
|
364
364
|
}
|
|
@@ -375,7 +375,7 @@ class k {
|
|
|
375
375
|
/** Global hook: fires with true when the first request starts, false when the last one ends. */
|
|
376
376
|
onProcessing(t) {
|
|
377
377
|
return this.processingHooks.push(t), () => {
|
|
378
|
-
this.processingHooks = this.processingHooks.filter((
|
|
378
|
+
this.processingHooks = this.processingHooks.filter((e) => e !== t);
|
|
379
379
|
};
|
|
380
380
|
}
|
|
381
381
|
/**
|
|
@@ -385,7 +385,7 @@ class k {
|
|
|
385
385
|
*/
|
|
386
386
|
onError(t) {
|
|
387
387
|
return this.errorHooks.push(t), () => {
|
|
388
|
-
this.errorHooks = this.errorHooks.filter((
|
|
388
|
+
this.errorHooks = this.errorHooks.filter((e) => e !== t);
|
|
389
389
|
};
|
|
390
390
|
}
|
|
391
391
|
/**
|
|
@@ -398,68 +398,69 @@ class k {
|
|
|
398
398
|
return this.bodyTransformer = t, this;
|
|
399
399
|
}
|
|
400
400
|
// --- plain requests (Promise, no model) -----------------------------------
|
|
401
|
-
async request(t,
|
|
402
|
-
let r = /^https?:\/\//i.test(
|
|
403
|
-
if (
|
|
404
|
-
const h =
|
|
401
|
+
async request(t, e, s = {}) {
|
|
402
|
+
let r = /^https?:\/\//i.test(e) ? e : this.baseUrl + e;
|
|
403
|
+
if (s.params) {
|
|
404
|
+
const h = q(s.params);
|
|
405
405
|
h && (r += (r.includes("?") ? "&" : "?") + h);
|
|
406
406
|
}
|
|
407
|
-
const
|
|
408
|
-
|
|
409
|
-
"
|
|
407
|
+
const i = {
|
|
408
|
+
accept: "application/json",
|
|
409
|
+
"x-requested-with": "XMLHttpRequest"
|
|
410
410
|
};
|
|
411
|
-
for (const [h,
|
|
412
|
-
const
|
|
413
|
-
|
|
411
|
+
for (const [h, u] of Object.entries(this.headers)) {
|
|
412
|
+
const l = typeof u == "function" ? u() : u;
|
|
413
|
+
l != null && (i[h.toLowerCase()] = l);
|
|
414
414
|
}
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
415
|
+
for (const [h, u] of Object.entries(s.headers ?? {}))
|
|
416
|
+
i[h.toLowerCase()] = u;
|
|
417
|
+
const a = { method: t.toUpperCase(), headers: i, signal: s.signal };
|
|
418
|
+
this.credentials && (a.credentials = this.credentials), s.data !== void 0 && t !== "get" && (typeof FormData < "u" && s.data instanceof FormData ? (delete i["content-type"], a.body = s.data) : (i["content-type"] ?? (i["content-type"] = "application/json"), a.body = JSON.stringify(s.data)));
|
|
419
|
+
const n = { method: t, url: r, options: s };
|
|
419
420
|
this.trackStart();
|
|
420
421
|
try {
|
|
421
422
|
const h = await fetch(r, a);
|
|
422
|
-
let
|
|
423
|
-
if (this.bodyTransformer && (
|
|
424
|
-
return
|
|
423
|
+
let u = await k(h);
|
|
424
|
+
if (this.bodyTransformer && (u = this.bodyTransformer(u, n) ?? u), !h.ok) throw new j(h.status, u, h);
|
|
425
|
+
return u;
|
|
425
426
|
} catch (h) {
|
|
426
427
|
throw R(h) ? h : this.emitError(h, n);
|
|
427
428
|
} finally {
|
|
428
429
|
this.trackEnd();
|
|
429
430
|
}
|
|
430
431
|
}
|
|
431
|
-
get(t,
|
|
432
|
-
return this.request("get", t,
|
|
432
|
+
get(t, e, s = {}) {
|
|
433
|
+
return this.request("get", t, e ? { ...s, params: e } : s);
|
|
433
434
|
}
|
|
434
|
-
post(t,
|
|
435
|
-
return this.request("post", t,
|
|
435
|
+
post(t, e, s = {}) {
|
|
436
|
+
return this.request("post", t, e !== void 0 ? { ...s, data: e } : s);
|
|
436
437
|
}
|
|
437
|
-
put(t,
|
|
438
|
-
return this.request("put", t,
|
|
438
|
+
put(t, e, s = {}) {
|
|
439
|
+
return this.request("put", t, e !== void 0 ? { ...s, data: e } : s);
|
|
439
440
|
}
|
|
440
|
-
patch(t,
|
|
441
|
-
return this.request("patch", t,
|
|
441
|
+
patch(t, e, s = {}) {
|
|
442
|
+
return this.request("patch", t, e !== void 0 ? { ...s, data: e } : s);
|
|
442
443
|
}
|
|
443
444
|
/** DELETE with an optional body. */
|
|
444
|
-
delete(t,
|
|
445
|
-
return this.request("delete", t,
|
|
445
|
+
delete(t, e, s = {}) {
|
|
446
|
+
return this.request("delete", t, e !== void 0 ? { ...s, data: e } : s);
|
|
446
447
|
}
|
|
447
448
|
// --- form models -----------------------------------------------------------
|
|
448
449
|
/** Create a form model without any request (like Inertia's useForm). */
|
|
449
|
-
form(t,
|
|
450
|
-
return w(this, { ...
|
|
450
|
+
form(t, e = {}) {
|
|
451
|
+
return w(this, { ...e, default: typeof t == "function" ? t : () => c(t) });
|
|
451
452
|
}
|
|
452
453
|
/** Create a form model and fill it with a GET request. */
|
|
453
|
-
getForm(t,
|
|
454
|
-
return this.createFormModel("get", t,
|
|
454
|
+
getForm(t, e) {
|
|
455
|
+
return this.createFormModel("get", t, e);
|
|
455
456
|
}
|
|
456
457
|
/** Same as getForm, but the fill request is a POST (complex search endpoints etc.). */
|
|
457
|
-
postForm(t,
|
|
458
|
-
return this.createFormModel("post", t,
|
|
458
|
+
postForm(t, e) {
|
|
459
|
+
return this.createFormModel("post", t, e);
|
|
459
460
|
}
|
|
460
|
-
createFormModel(t,
|
|
461
|
-
const r = typeof
|
|
462
|
-
return w(this, r, { url:
|
|
461
|
+
createFormModel(t, e, s) {
|
|
462
|
+
const r = typeof s == "string" ? { path: s } : { ...s ?? {} };
|
|
463
|
+
return w(this, r, { url: e, method: t });
|
|
463
464
|
}
|
|
464
465
|
// --- internals --------------------------------------------------------------
|
|
465
466
|
trackStart() {
|
|
@@ -469,33 +470,33 @@ class k {
|
|
|
469
470
|
--this.active === 0 && this.emitProcessing(!1);
|
|
470
471
|
}
|
|
471
472
|
emitProcessing(t) {
|
|
472
|
-
for (const
|
|
473
|
+
for (const e of this.processingHooks)
|
|
473
474
|
try {
|
|
474
|
-
|
|
475
|
+
e(t);
|
|
475
476
|
} catch {
|
|
476
477
|
}
|
|
477
478
|
}
|
|
478
479
|
/** Runs the hooks; a hook returning an Error replaces it for the rest of the chain. */
|
|
479
|
-
emitError(t,
|
|
480
|
-
let
|
|
480
|
+
emitError(t, e) {
|
|
481
|
+
let s = t;
|
|
481
482
|
for (const r of this.errorHooks)
|
|
482
483
|
try {
|
|
483
|
-
const
|
|
484
|
-
|
|
484
|
+
const i = r(s, e);
|
|
485
|
+
i instanceof Error && (s = i);
|
|
485
486
|
} catch {
|
|
486
487
|
}
|
|
487
|
-
return
|
|
488
|
+
return s;
|
|
488
489
|
}
|
|
489
490
|
}
|
|
490
|
-
function U(
|
|
491
|
-
return new
|
|
491
|
+
function U(o = {}) {
|
|
492
|
+
return new T(o);
|
|
492
493
|
}
|
|
493
|
-
const
|
|
494
|
+
const L = U();
|
|
494
495
|
export {
|
|
495
|
-
|
|
496
|
+
T as Apix,
|
|
496
497
|
j as ApixError,
|
|
497
|
-
|
|
498
|
-
|
|
498
|
+
S as ApixModel,
|
|
499
|
+
L as apix,
|
|
499
500
|
U as createApix,
|
|
500
|
-
|
|
501
|
+
L as default
|
|
501
502
|
};
|