@diephp/vue-apix 0.1.3 → 0.1.4
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 +41 -3
- package/dist/model.d.ts +10 -1
- package/dist/vue-apix.cjs +1 -1
- package/dist/vue-apix.js +107 -93
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -228,7 +228,8 @@ const submit = () =>
|
|
|
228
228
|
| `model.setErrors(errors)` / `model.clearErrors(keys?)` | manage errors manually |
|
|
229
229
|
| `model.reset(keys?)` | restore the snapshot: first server response, else `default()`; `reset(['name'])` for specific paths |
|
|
230
230
|
| `model.post/put/patch/delete(url?, opts?)` | submit `model.data` (or `opts.data` / `opts.only: ['name']`), returns the response Promise |
|
|
231
|
-
| `model.reload(url?)` | re-run the fill request |
|
|
231
|
+
| `model.reload(url?)` | re-run the fill request as configured (fresh load — ignores watch payload) |
|
|
232
|
+
| `model.refresh()` | re-send the fill request with the CURRENT state — a manual watch trigger for a "Search" button |
|
|
232
233
|
| `model.watch(paths?, opts?)` / `model.unwatch()` | auto-refetch on change, see below |
|
|
233
234
|
| `model.transformResponse(fn)` / `model.transformRequest(fn)` | reshape server data ↔ model data, see below |
|
|
234
235
|
|
|
@@ -239,8 +240,42 @@ treated as validation errors — only `model.message` is surfaced.
|
|
|
239
240
|
|
|
240
241
|
**Reserved names.** These model members always win over data keys:
|
|
241
242
|
`data, errors, message, processing, hasErrors, isDirty, get, error, setData, setErrors,
|
|
242
|
-
clearErrors, reset, post, put, patch, delete, reload, watch, unwatch,
|
|
243
|
-
transformResponse`. A data field with such a name is still there —
|
|
243
|
+
clearErrors, reset, post, put, patch, delete, reload, refresh, watch, unwatch,
|
|
244
|
+
transformRequest, transformResponse`. A data field with such a name is still there —
|
|
245
|
+
via `model.data.<key>`.
|
|
246
|
+
|
|
247
|
+
## Passing a model to a child component
|
|
248
|
+
|
|
249
|
+
A model is a plain object reference — hand it to a child and every mutation stays shared
|
|
250
|
+
and reactive. Two equivalent ways:
|
|
251
|
+
|
|
252
|
+
```ts
|
|
253
|
+
// ChildForm.vue — via defineModel (parent: <ChildForm v-model="model" />)
|
|
254
|
+
import type { Model } from '@diephp/vue-apix';
|
|
255
|
+
|
|
256
|
+
// type it through the macro GENERIC — not a cast on the left side,
|
|
257
|
+
// otherwise .value loses the model members (errors, processing, data…)
|
|
258
|
+
const form = defineModel<Model<AttrDto>>({ required: true });
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
```ts
|
|
262
|
+
// ChildForm.vue — via a plain prop (parent: <ChildForm :form="model" />)
|
|
263
|
+
import type { Model } from '@diephp/vue-apix';
|
|
264
|
+
|
|
265
|
+
const props = defineProps<{ form: Model<AttrDto> }>();
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
```html
|
|
269
|
+
<!-- child template is identical either way -->
|
|
270
|
+
<el-form-item label="Name" :error="form.errors.name">
|
|
271
|
+
<el-input v-model="form.name" clearable />
|
|
272
|
+
</el-form-item>
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
One rule with `defineModel`: never REPLACE the model from the child
|
|
276
|
+
(`form.value = ...`) — mutate it instead (`form.value.setData(...)`, `form.value.reset()`).
|
|
277
|
+
The child mutates the same model instance the parent owns, so a separate `:errors` prop
|
|
278
|
+
is unnecessary — `form.errors` is already reactive inside the child.
|
|
244
279
|
|
|
245
280
|
## watch() — filters, pagination, live search
|
|
246
281
|
|
|
@@ -335,6 +370,9 @@ How `watch()` behaves:
|
|
|
335
370
|
- **Debounce** (`debounce: 300`) collapses fast typing into one request.
|
|
336
371
|
- `reset()` restores the first-response snapshot; if that actually changes watched data,
|
|
337
372
|
the request is re-sent — reset filters, the table refreshes itself.
|
|
373
|
+
- **A "Search" button is `model.refresh()`** — it re-sends the request with the current
|
|
374
|
+
watched values (and cancels a pending debounce). `model.reload()` is different: it
|
|
375
|
+
re-runs the ORIGINAL fill request without the watch payload, i.e. it resets the view.
|
|
338
376
|
- **Watch server-owned fields precisely**: `['filters', 'paginator.page']`, not
|
|
339
377
|
`['paginator']` — a watched path is preserved from responses, so watching the whole
|
|
340
378
|
`paginator` would freeze `paginator.total` at its first value.
|
package/dist/model.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ interface ModelState {
|
|
|
14
14
|
* Reserved members (always win over data keys, use `model.data.<key>` for those):
|
|
15
15
|
* data, errors, message, processing, hasErrors, isDirty,
|
|
16
16
|
* get, error, setData, setErrors, clearErrors, reset,
|
|
17
|
-
* post, put, patch, delete, reload, watch, unwatch,
|
|
17
|
+
* post, put, patch, delete, reload, refresh, watch, unwatch,
|
|
18
18
|
* transformRequest, transformResponse.
|
|
19
19
|
*/
|
|
20
20
|
export declare class ApixModel<T = any> {
|
|
@@ -71,6 +71,13 @@ export declare class ApixModel<T = any> {
|
|
|
71
71
|
delete(url?: string, options?: SubmitOptions): Promise<any>;
|
|
72
72
|
/** Re-run the fill request (or set a new source URL and run it). */
|
|
73
73
|
reload(url?: string): Promise<any>;
|
|
74
|
+
/**
|
|
75
|
+
* Re-send the fill request with the CURRENT state — a manual watch trigger
|
|
76
|
+
* (a "Search" button next to filters). Sends the watched paths as payload and
|
|
77
|
+
* preserves them in the response, exactly like a watch-triggered request; any
|
|
78
|
+
* pending debounce is cancelled. Without an active watch() it equals reload().
|
|
79
|
+
*/
|
|
80
|
+
refresh(): Promise<any>;
|
|
74
81
|
/**
|
|
75
82
|
* Map server data into the model shape. Runs on every fill (initial, reload,
|
|
76
83
|
* watch) AFTER path extraction and BEFORE the reactive data is touched — so it
|
|
@@ -104,6 +111,8 @@ export declare class ApixModel<T = any> {
|
|
|
104
111
|
private _submit;
|
|
105
112
|
private _onWatchTrigger;
|
|
106
113
|
private _sendWatchRequest;
|
|
114
|
+
/** Build the fill() extra for a watch-shaped request from the current state. */
|
|
115
|
+
private _watchExtra;
|
|
107
116
|
private _payloadJson;
|
|
108
117
|
private _syncLastSent;
|
|
109
118
|
}
|
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 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;
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const d=require("vue");function c(i){const t=d.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 _(i){return i&&typeof i=="object"&&typeof i.message=="string"?i.message:null}function T(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(_(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 E(i){return i instanceof Error&&i.name==="AbortError"}function S(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 R{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=d.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)."))}refresh(){return this._source?this._stopWatch?(this._debounceTimer&&(clearTimeout(this._debounceTimer),this._debounceTimer=null),this._lastSent=this._payloadJson(),this._fill(this._watchExtra())):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=d.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 p=T(l,this._options.path);if(this._transformResponse){const g=c(p);p=this._transformResponse(g,l)??g}return this._applyFill(p,t.preserve??null),this._state.message=_(l),this._state.errors={},(n=(a=this._options).onSuccess)==null||n.call(a,l,this._proxy),l}catch(l){throw E(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&&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=_(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=_(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(){this._debounceTimer=null,this._source&&(this._lastSent=this._payloadJson(),this._fill(this._watchExtra()).catch(()=>{}))}_watchExtra(){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);const r=this._watchOptions.refreshWatched?null:t;return this._source.method==="get"?{params:s,preserve:r}:{data:s,preserve:r}}_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 R(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 v{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=S(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 k(h);if(this.bodyTransformer&&(u=this.bodyTransformer(u,n)??u),!h.ok)throw new y(h.status,u,h);return u}catch(h){throw E(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 v(i)}const q=O();exports.Apix=v;exports.ApixError=y;exports.ApixModel=R;exports.apix=q;exports.createApix=O;exports.default=q;
|
package/dist/vue-apix.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { toRaw as
|
|
2
|
-
function c(
|
|
3
|
-
const t =
|
|
1
|
+
import { toRaw as E, reactive as v, watch as A } from "vue";
|
|
2
|
+
function c(i) {
|
|
3
|
+
const t = E(i);
|
|
4
4
|
if (t === null || typeof t != "object") return t;
|
|
5
5
|
if (Array.isArray(t)) return t.map((s) => c(s));
|
|
6
6
|
if (t instanceof Date) return new Date(t.getTime());
|
|
@@ -12,62 +12,62 @@ function c(o) {
|
|
|
12
12
|
}
|
|
13
13
|
return t;
|
|
14
14
|
}
|
|
15
|
-
function y(
|
|
16
|
-
if (
|
|
17
|
-
const t = Object.getPrototypeOf(
|
|
15
|
+
function y(i) {
|
|
16
|
+
if (i === null || typeof i != "object") return !1;
|
|
17
|
+
const t = Object.getPrototypeOf(i);
|
|
18
18
|
return t === Object.prototype || t === null;
|
|
19
19
|
}
|
|
20
|
-
function x(
|
|
20
|
+
function x(i, t) {
|
|
21
21
|
if (!y(t))
|
|
22
|
-
return
|
|
23
|
-
if (
|
|
24
|
-
if (!y(
|
|
22
|
+
return i === void 0 ? c(t) : i;
|
|
23
|
+
if (i == null) return c(t);
|
|
24
|
+
if (!y(i)) return i;
|
|
25
25
|
for (const e of Object.keys(t))
|
|
26
|
-
|
|
27
|
-
return
|
|
26
|
+
i[e] = e in i ? x(i[e], t[e]) : c(t[e]);
|
|
27
|
+
return i;
|
|
28
28
|
}
|
|
29
|
-
function f(
|
|
30
|
-
if (
|
|
31
|
-
let e =
|
|
29
|
+
function f(i, t) {
|
|
30
|
+
if (i == null) return;
|
|
31
|
+
let e = i;
|
|
32
32
|
for (const s of t.split(".")) {
|
|
33
33
|
if (e == null) return;
|
|
34
34
|
e = e[s];
|
|
35
35
|
}
|
|
36
36
|
return e;
|
|
37
37
|
}
|
|
38
|
-
function d(
|
|
38
|
+
function d(i, t, e) {
|
|
39
39
|
const s = t.split(".");
|
|
40
|
-
let r =
|
|
41
|
-
for (let
|
|
42
|
-
const a = s[
|
|
40
|
+
let r = i;
|
|
41
|
+
for (let o = 0; o < s.length - 1; o++) {
|
|
42
|
+
const a = s[o];
|
|
43
43
|
(r[a] === null || typeof r[a] != "object") && (r[a] = {}), r = r[a];
|
|
44
44
|
}
|
|
45
45
|
r[s[s.length - 1]] = e;
|
|
46
46
|
}
|
|
47
|
-
function g(
|
|
47
|
+
function g(i, t) {
|
|
48
48
|
const e = {};
|
|
49
49
|
for (const s of t) {
|
|
50
|
-
const r = f(
|
|
50
|
+
const r = f(i, s);
|
|
51
51
|
r !== void 0 && d(e, s, c(r));
|
|
52
52
|
}
|
|
53
53
|
return e;
|
|
54
54
|
}
|
|
55
|
-
function b(
|
|
55
|
+
function b(i) {
|
|
56
56
|
const t = {};
|
|
57
|
-
for (const [e, s] of Object.entries(
|
|
57
|
+
for (const [e, s] of Object.entries(i ?? {}))
|
|
58
58
|
s != null && (t[e] = Array.isArray(s) ? String(s[0]) : String(s));
|
|
59
59
|
return t;
|
|
60
60
|
}
|
|
61
|
-
function
|
|
62
|
-
return
|
|
61
|
+
function _(i) {
|
|
62
|
+
return i && typeof i == "object" && typeof i.message == "string" ? i.message : null;
|
|
63
63
|
}
|
|
64
|
-
function O(
|
|
65
|
-
const e =
|
|
64
|
+
function O(i, t) {
|
|
65
|
+
const e = i && typeof i == "object" && !Array.isArray(i) && "data" in i ? i.data : i;
|
|
66
66
|
return t ? f(e, t) : e;
|
|
67
67
|
}
|
|
68
68
|
class j extends Error {
|
|
69
69
|
constructor(t, e, s) {
|
|
70
|
-
super(
|
|
70
|
+
super(_(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(i) {
|
|
82
|
+
return i instanceof Error && i.name === "AbortError";
|
|
83
83
|
}
|
|
84
|
-
function q(
|
|
85
|
-
const t = [], e = (r,
|
|
86
|
-
t.push(`${encodeURIComponent(r)}=${encodeURIComponent(
|
|
87
|
-
}, s = (r,
|
|
84
|
+
function q(i) {
|
|
85
|
+
const t = [], e = (r, o) => {
|
|
86
|
+
t.push(`${encodeURIComponent(r)}=${encodeURIComponent(o)}`);
|
|
87
|
+
}, s = (r, o) => {
|
|
88
88
|
if (r != null) {
|
|
89
|
-
if (typeof r == "boolean") return e(
|
|
90
|
-
if (r instanceof Date) return e(
|
|
89
|
+
if (typeof r == "boolean") return e(o, r ? "1" : "0");
|
|
90
|
+
if (r instanceof Date) return e(o, r.toISOString());
|
|
91
91
|
if (Array.isArray(r)) {
|
|
92
|
-
r.forEach((a, n) => s(a, `${
|
|
92
|
+
r.forEach((a, n) => s(a, `${o}[${n}]`));
|
|
93
93
|
return;
|
|
94
94
|
}
|
|
95
95
|
if (typeof r == "object") {
|
|
96
|
-
for (const [a, n] of Object.entries(r)) s(n, `${
|
|
96
|
+
for (const [a, n] of Object.entries(r)) s(n, `${o}[${a}]`);
|
|
97
97
|
return;
|
|
98
98
|
}
|
|
99
|
-
e(
|
|
99
|
+
e(o, String(r));
|
|
100
100
|
}
|
|
101
101
|
};
|
|
102
|
-
for (const [r,
|
|
102
|
+
for (const [r, o] of Object.entries(i)) s(o, r);
|
|
103
103
|
return t.join("&");
|
|
104
104
|
}
|
|
105
|
-
async function
|
|
106
|
-
if (
|
|
107
|
-
const t = await
|
|
105
|
+
async function T(i) {
|
|
106
|
+
if (i.status === 204) return null;
|
|
107
|
+
const t = await i.text();
|
|
108
108
|
if (!t) return null;
|
|
109
109
|
try {
|
|
110
110
|
return JSON.parse(t);
|
|
@@ -214,6 +214,17 @@ class S {
|
|
|
214
214
|
new Error("[vue-apix] The model has no source URL. Use apix.getForm(url) or model.reload(url).")
|
|
215
215
|
);
|
|
216
216
|
}
|
|
217
|
+
/**
|
|
218
|
+
* Re-send the fill request with the CURRENT state — a manual watch trigger
|
|
219
|
+
* (a "Search" button next to filters). Sends the watched paths as payload and
|
|
220
|
+
* preserves them in the response, exactly like a watch-triggered request; any
|
|
221
|
+
* pending debounce is cancelled. Without an active watch() it equals reload().
|
|
222
|
+
*/
|
|
223
|
+
refresh() {
|
|
224
|
+
return this._source ? this._stopWatch ? (this._debounceTimer && (clearTimeout(this._debounceTimer), this._debounceTimer = null), this._lastSent = this._payloadJson(), this._fill(this._watchExtra())) : this._fill() : Promise.reject(
|
|
225
|
+
new Error("[vue-apix] The model has no source URL. Use apix.getForm(url) or model.reload(url).")
|
|
226
|
+
);
|
|
227
|
+
}
|
|
217
228
|
// --- transformers -------------------------------------------------------------------
|
|
218
229
|
/**
|
|
219
230
|
* Map server data into the model shape. Runs on every fill (initial, reload,
|
|
@@ -243,8 +254,8 @@ class S {
|
|
|
243
254
|
watch(t, e) {
|
|
244
255
|
let s = null, r;
|
|
245
256
|
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
|
|
247
|
-
return this._stopWatch =
|
|
257
|
+
const o = s && s.length ? s.map((a) => () => f(this._state.data, a)) : () => this._state.data;
|
|
258
|
+
return this._stopWatch = A(o, () => this._onWatchTrigger(), {
|
|
248
259
|
deep: r.deep ?? !0
|
|
249
260
|
}), this._proxy;
|
|
250
261
|
}
|
|
@@ -255,21 +266,21 @@ class S {
|
|
|
255
266
|
// --- internals ----------------------------------------------------------------------
|
|
256
267
|
/** @internal */
|
|
257
268
|
async _fill(t = {}) {
|
|
258
|
-
var
|
|
269
|
+
var o, a, n, h, u;
|
|
259
270
|
const e = this._source;
|
|
260
|
-
(
|
|
271
|
+
(o = this._fillAbort) == null || o.abort();
|
|
261
272
|
const s = new AbortController();
|
|
262
273
|
this._fillAbort = s, this._state.processing = !0;
|
|
263
274
|
const r = { signal: s.signal };
|
|
264
275
|
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
276
|
try {
|
|
266
277
|
const l = await this._apix.request(e.method, e.url, r);
|
|
267
|
-
let
|
|
278
|
+
let p = O(l, this._options.path);
|
|
268
279
|
if (this._transformResponse) {
|
|
269
|
-
const m = c(
|
|
270
|
-
|
|
280
|
+
const m = c(p);
|
|
281
|
+
p = this._transformResponse(m, l) ?? m;
|
|
271
282
|
}
|
|
272
|
-
return this._applyFill(
|
|
283
|
+
return this._applyFill(p, t.preserve ?? null), this._state.message = _(l), this._state.errors = {}, (n = (a = this._options).onSuccess) == null || n.call(a, l, this._proxy), l;
|
|
273
284
|
} catch (l) {
|
|
274
285
|
throw R(l) || (this._applyError(l), (u = (h = this._options).onError) == null || u.call(h, l, this._proxy)), l;
|
|
275
286
|
} finally {
|
|
@@ -281,33 +292,33 @@ class S {
|
|
|
281
292
|
this._options.mergeDefaults !== !1 && this._options.default && (s = x(s, this._options.default()));
|
|
282
293
|
const r = this._snapshotLocked ? null : c(s);
|
|
283
294
|
if (e && e.length && s && typeof s == "object" && this._state.data && typeof this._state.data == "object")
|
|
284
|
-
for (const
|
|
285
|
-
const a = f(this._state.data,
|
|
286
|
-
a !== void 0 && d(s,
|
|
295
|
+
for (const o of e) {
|
|
296
|
+
const a = f(this._state.data, o);
|
|
297
|
+
a !== void 0 && d(s, o, c(a));
|
|
287
298
|
}
|
|
288
299
|
this._state.data = s, this._snapshotLocked || (this._snapshot = r, this._snapshotLocked = !0), this._syncLastSent();
|
|
289
300
|
}
|
|
290
301
|
_applyError(t) {
|
|
291
|
-
t instanceof j && (this._state.message =
|
|
302
|
+
t instanceof j && (this._state.message = _(t.body) ?? t.message, t.isValidationError && (this._state.errors = b(t.errors)));
|
|
292
303
|
}
|
|
293
304
|
async _submit(t, e, s = {}) {
|
|
294
305
|
var a;
|
|
295
306
|
const r = e ?? ((a = this._source) == null ? void 0 : a.url);
|
|
296
307
|
if (!r)
|
|
297
308
|
throw new Error(`[vue-apix] The model has no URL. Pass one explicitly: model.${t}(url).`);
|
|
298
|
-
let
|
|
299
|
-
if (s.data === void 0 && this._transformRequest &&
|
|
300
|
-
const n = c(
|
|
301
|
-
|
|
309
|
+
let o = s.data !== void 0 ? s.data : s.only ? g(this._state.data, s.only) : this._state.data ?? void 0;
|
|
310
|
+
if (s.data === void 0 && this._transformRequest && o !== void 0) {
|
|
311
|
+
const n = c(o);
|
|
312
|
+
o = this._transformRequest(n) ?? n;
|
|
302
313
|
}
|
|
303
314
|
this._state.processing = !0, this._state.errors = {}, this._state.message = null;
|
|
304
315
|
try {
|
|
305
316
|
const n = await this._apix.request(t, r, {
|
|
306
|
-
data:
|
|
317
|
+
data: o,
|
|
307
318
|
params: s.params,
|
|
308
319
|
headers: s.headers
|
|
309
320
|
});
|
|
310
|
-
return this._state.message =
|
|
321
|
+
return this._state.message = _(n), n;
|
|
311
322
|
} catch (n) {
|
|
312
323
|
throw this._applyError(n), n;
|
|
313
324
|
} finally {
|
|
@@ -321,13 +332,16 @@ class S {
|
|
|
321
332
|
t > 0 ? this._debounceTimer = setTimeout(() => this._sendWatchRequest(), t) : this._sendWatchRequest();
|
|
322
333
|
}
|
|
323
334
|
_sendWatchRequest() {
|
|
324
|
-
|
|
335
|
+
this._debounceTimer = null, this._source && (this._lastSent = this._payloadJson(), this._fill(this._watchExtra()).catch(() => {
|
|
336
|
+
}));
|
|
337
|
+
}
|
|
338
|
+
/** Build the fill() extra for a watch-shaped request from the current state. */
|
|
339
|
+
_watchExtra() {
|
|
325
340
|
const t = this._watched, e = this._watchOptions.payload ?? t;
|
|
326
341
|
let s = e && e.length ? g(this._state.data, e) : c(this._state.data);
|
|
327
|
-
this._transformRequest && (s = this._transformRequest(s) ?? s)
|
|
328
|
-
const r = this._watchOptions.refreshWatched ? null : t
|
|
329
|
-
this.
|
|
330
|
-
});
|
|
342
|
+
this._transformRequest && (s = this._transformRequest(s) ?? s);
|
|
343
|
+
const r = this._watchOptions.refreshWatched ? null : t;
|
|
344
|
+
return this._source.method === "get" ? { params: s, preserve: r } : { data: s, preserve: r };
|
|
331
345
|
}
|
|
332
346
|
_payloadJson() {
|
|
333
347
|
const t = this._watched, e = t && t.length ? t.map((s) => f(this._state.data, s)) : this._state.data;
|
|
@@ -337,28 +351,28 @@ class S {
|
|
|
337
351
|
this._lastSent = this._payloadJson();
|
|
338
352
|
}
|
|
339
353
|
}
|
|
340
|
-
function w(
|
|
341
|
-
const s = new S(
|
|
342
|
-
get(
|
|
343
|
-
if (typeof a == "symbol" || a in
|
|
344
|
-
const n =
|
|
354
|
+
function w(i, t = {}, e = null) {
|
|
355
|
+
const s = new S(i, t, e), r = new Proxy(s, {
|
|
356
|
+
get(o, a) {
|
|
357
|
+
if (typeof a == "symbol" || a in o) return Reflect.get(o, a);
|
|
358
|
+
const n = o._state.data;
|
|
345
359
|
if (n && typeof n == "object") return n[a];
|
|
346
360
|
},
|
|
347
|
-
set(
|
|
348
|
-
if (typeof a == "symbol" || a in
|
|
349
|
-
const h =
|
|
350
|
-
return h && typeof h == "object" ? (h[a] = n, !0) : (
|
|
361
|
+
set(o, a, n) {
|
|
362
|
+
if (typeof a == "symbol" || a in o) return Reflect.set(o, a, n);
|
|
363
|
+
const h = o._state.data;
|
|
364
|
+
return h && typeof h == "object" ? (h[a] = n, !0) : (o._state.data = { [a]: n }, !0);
|
|
351
365
|
},
|
|
352
|
-
has(
|
|
353
|
-
if (a in
|
|
354
|
-
const n =
|
|
366
|
+
has(o, a) {
|
|
367
|
+
if (a in o) return !0;
|
|
368
|
+
const n = o._state.data;
|
|
355
369
|
return !!n && typeof n == "object" && a in n;
|
|
356
370
|
}
|
|
357
371
|
});
|
|
358
372
|
return s._proxy = r, e && t.immediate !== !1 && r.reload().catch(() => {
|
|
359
373
|
}), r;
|
|
360
374
|
}
|
|
361
|
-
class
|
|
375
|
+
class k {
|
|
362
376
|
constructor(t = {}) {
|
|
363
377
|
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
378
|
}
|
|
@@ -404,23 +418,23 @@ class T {
|
|
|
404
418
|
const h = q(s.params);
|
|
405
419
|
h && (r += (r.includes("?") ? "&" : "?") + h);
|
|
406
420
|
}
|
|
407
|
-
const
|
|
421
|
+
const o = {
|
|
408
422
|
accept: "application/json",
|
|
409
423
|
"x-requested-with": "XMLHttpRequest"
|
|
410
424
|
};
|
|
411
425
|
for (const [h, u] of Object.entries(this.headers)) {
|
|
412
426
|
const l = typeof u == "function" ? u() : u;
|
|
413
|
-
l != null && (
|
|
427
|
+
l != null && (o[h.toLowerCase()] = l);
|
|
414
428
|
}
|
|
415
429
|
for (const [h, u] of Object.entries(s.headers ?? {}))
|
|
416
|
-
|
|
417
|
-
const a = { method: t.toUpperCase(), headers:
|
|
418
|
-
this.credentials && (a.credentials = this.credentials), s.data !== void 0 && t !== "get" && (typeof FormData < "u" && s.data instanceof FormData ? (delete
|
|
430
|
+
o[h.toLowerCase()] = u;
|
|
431
|
+
const a = { method: t.toUpperCase(), headers: o, signal: s.signal };
|
|
432
|
+
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)));
|
|
419
433
|
const n = { method: t, url: r, options: s };
|
|
420
434
|
this.trackStart();
|
|
421
435
|
try {
|
|
422
436
|
const h = await fetch(r, a);
|
|
423
|
-
let u = await
|
|
437
|
+
let u = await T(h);
|
|
424
438
|
if (this.bodyTransformer && (u = this.bodyTransformer(u, n) ?? u), !h.ok) throw new j(h.status, u, h);
|
|
425
439
|
return u;
|
|
426
440
|
} catch (h) {
|
|
@@ -481,22 +495,22 @@ class T {
|
|
|
481
495
|
let s = t;
|
|
482
496
|
for (const r of this.errorHooks)
|
|
483
497
|
try {
|
|
484
|
-
const
|
|
485
|
-
|
|
498
|
+
const o = r(s, e);
|
|
499
|
+
o instanceof Error && (s = o);
|
|
486
500
|
} catch {
|
|
487
501
|
}
|
|
488
502
|
return s;
|
|
489
503
|
}
|
|
490
504
|
}
|
|
491
|
-
function U(
|
|
492
|
-
return new
|
|
505
|
+
function U(i = {}) {
|
|
506
|
+
return new k(i);
|
|
493
507
|
}
|
|
494
|
-
const
|
|
508
|
+
const F = U();
|
|
495
509
|
export {
|
|
496
|
-
|
|
510
|
+
k as Apix,
|
|
497
511
|
j as ApixError,
|
|
498
512
|
S as ApixModel,
|
|
499
|
-
|
|
513
|
+
F as apix,
|
|
500
514
|
U as createApix,
|
|
501
|
-
|
|
515
|
+
F as default
|
|
502
516
|
};
|