@defjs/core 0.0.0-test-004 → 0.1.0

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.
Files changed (4) hide show
  1. package/index.d.ts +219 -292
  2. package/index.js +201 -149
  3. package/index.min.js +1 -587
  4. package/package.json +4 -1
package/index.min.js CHANGED
@@ -1,587 +1 @@
1
- const G = new Error("ERR_ABORTED"), W = new Error("ERR_TIMEOUT"), me = new Error("ERR_NETWORK"), X = new Error("ERR_NOT_FOUND_HANDLER"), z = new Error("ERR_OBSERVE"), P = new Error("ERR_NOT_SET_ALIAS"), Y = new Error("ERR_UNSUPPORTED_FIELD_TYPE"), Z = new Error("ERR_INVALID_CLIENT"), q = new Error("ERR_NOT_FOUND_GLOBAL_CLIENT"), B = new Error("ERR_INVALID_HTTP_CONTEXT_TOKEN"), Ee = new Error("ERR_UNKNOWN");
2
- class T extends Error {
3
- status;
4
- statusText;
5
- url;
6
- headers;
7
- body;
8
- constructor(t) {
9
- const r = t?.status ?? 0, n = t?.statusText ?? "", c = r >= 200 && r < 300, i = t?.headers ?? new Headers(), h = t?.url ?? "", b = t?.body ?? null;
10
- let o;
11
- c ? o = `Http failure during parsing for ${h}` : (o = `Http failure response for ${h || "(unknown url)"}: ${r}`, n && (o += ` - ${n}`)), super(o, { cause: t?.error }), super.name = "HttpErrorResponse", this.statusText = n, this.url = h, this.headers = i, this.status = r, this.body = b;
12
- }
13
- }
14
- function ee(e) {
15
- const t = e.get("Content-Length");
16
- if (!t)
17
- return 0;
18
- const r = Number(t);
19
- return isNaN(r) ? 0 : r;
20
- }
21
- function te(e) {
22
- return e.get("Content-Type") || "";
23
- }
24
- function re(e) {
25
- const { request: t, content: r, contentType: n } = e;
26
- switch (t.responseType) {
27
- case "json": {
28
- const i = new TextDecoder().decode(r);
29
- return i === "" ? null : JSON.parse(i);
30
- }
31
- case "text":
32
- return new TextDecoder().decode(r);
33
- case "blob":
34
- return new Blob([r], { type: n });
35
- case "arraybuffer":
36
- return r.buffer;
37
- default:
38
- return null;
39
- }
40
- }
41
- function ne(e, t) {
42
- const r = new Uint8Array(t);
43
- let n = 0;
44
- for (const c of e)
45
- r.set(c, n), n += c.length;
46
- return r;
47
- }
48
- const $ = Symbol("HttpContextToken");
49
- function _e(e) {
50
- return Object.defineProperty(e, $, { value: e }), e;
51
- }
52
- function H(e) {
53
- return typeof e == "function" && $ in e;
54
- }
55
- const U = Symbol("HttpContext");
56
- function oe(e) {
57
- return typeof e == "object" && e !== null && U in e;
58
- }
59
- function se(e) {
60
- const t = /* @__PURE__ */ new Map();
61
- switch (!0) {
62
- case Array.isArray(e): {
63
- for (const [r, n] of e)
64
- H(r) && t.set(r, n);
65
- break;
66
- }
67
- case oe(e): {
68
- for (const [r, n] of e[U])
69
- t.set(r, n);
70
- break;
71
- }
72
- }
73
- return {
74
- set(r, n) {
75
- if (!H(r))
76
- throw B;
77
- return t.set(r, n), this;
78
- },
79
- get(r) {
80
- if (!H(r))
81
- throw B;
82
- return t.has(r) || t.set(r, r()), t.get(r);
83
- },
84
- del(r) {
85
- return t.delete(r), this;
86
- },
87
- has(r) {
88
- return t.has(r);
89
- },
90
- keys() {
91
- return t.keys();
92
- },
93
- get length() {
94
- return Array.from(t.keys()).length;
95
- },
96
- [U]: t
97
- };
98
- }
99
- const x = Symbol("field");
100
- var u = /* @__PURE__ */ ((e) => (e[e.Json = 0] = "Json", e[e.Query = 1] = "Query", e[e.Param = 2] = "Param", e[e.Header = 3] = "Header", e[e.Form = 4] = "Form", e[e.Body = 5] = "Body", e[e.UrlForm = 6] = "UrlForm", e))(u || {});
101
- function Re(e) {
102
- const t = {
103
- // todo
104
- required: typeof e < "u",
105
- alias: /* @__PURE__ */ new Map(),
106
- validators: [],
107
- asyncValidator: []
108
- }, r = () => e;
109
- return Object.defineProperty(r, x, {
110
- value: t
111
- }), r.withJson = (n) => (t.alias.set(0, n), r), r.withForm = (n) => (t.alias.set(4, n), r), r.withQuery = (n) => (t.alias.set(1, n), r), r.withParam = (n) => (t.alias.set(2, n), r), r.withHeader = (n) => (t.alias.set(3, n), r), r.withUrlForm = (n) => (t.alias.set(6, n), r), r.withBody = () => (t.alias.set(5, void 0), r), r.withValidators = (...n) => (t.validators = n, r), r.withAsyncValidators = (...n) => (t.asyncValidator = n, r), r;
112
- }
113
- function j(e) {
114
- return e[x];
115
- }
116
- async function V(e, t) {
117
- await Promise.all(e.map((r) => r(t)));
118
- }
119
- function I(e) {
120
- return !!e && typeof e == "function" && x in e;
121
- }
122
- function J(e) {
123
- return !!e && typeof e == "object" && Object.entries(e).every(([, t]) => I(t));
124
- }
125
- let ae;
126
- const ce = () => ae;
127
- function ie(e) {
128
- return e.reduceRight(
129
- (t, r) => (n, c) => r(n, (i) => t(i, c)),
130
- (t, r) => r(t)
131
- );
132
- }
133
- function K(e) {
134
- const t = e?.status ?? 0, r = e?.statusText ?? "", n = e?.url ?? "", c = e?.headers ?? new Headers(), i = e?.body ?? null;
135
- return {
136
- status: t,
137
- statusText: r,
138
- url: n,
139
- headers: c,
140
- body: i
141
- };
142
- }
143
- function ue(e) {
144
- switch (!0) {
145
- case e instanceof FormData:
146
- case e instanceof Blob:
147
- case e instanceof ArrayBuffer:
148
- case e instanceof URLSearchParams:
149
- case typeof e == "string":
150
- return e;
151
- case typeof e == "object":
152
- case typeof e == "boolean":
153
- case typeof e == "number":
154
- case Array.isArray(e):
155
- return JSON.stringify(e);
156
- default:
157
- return null;
158
- }
159
- }
160
- function le(e) {
161
- switch (!0) {
162
- case e instanceof FormData:
163
- return "multipart/form-data";
164
- case e instanceof ArrayBuffer:
165
- return "application/octet-stream";
166
- case e instanceof Blob:
167
- return e.type || "application/octet-stream";
168
- case e instanceof URLSearchParams:
169
- return "application/x-www-form-urlencoded;charset=UTF-8";
170
- case typeof e == "string":
171
- return "text/plain";
172
- case (typeof e == "object" && e !== null):
173
- case typeof e == "number":
174
- case typeof e == "boolean":
175
- return "application/json";
176
- default:
177
- return null;
178
- }
179
- }
180
- function fe(e) {
181
- if (I(e))
182
- return e();
183
- if (J(e)) {
184
- const t = {};
185
- for (const [r, n] of Object.entries(e))
186
- t[r] = n();
187
- return t;
188
- }
189
- }
190
- function ge(e, t) {
191
- let r = !1, n, c = [], i = "json", h, b = "body", o = !1, l = [], f;
192
- const a = () => {
193
- let s, p, m;
194
- return {
195
- doRequest: async (...E) => {
196
- let g, w;
197
- switch (E.length) {
198
- case 1: {
199
- w = E[0];
200
- break;
201
- }
202
- case 2: {
203
- g = E[0], w = E[1];
204
- break;
205
- }
206
- }
207
- if (r && !g)
208
- throw new Error("Because the request has input, the first argument must be the input value");
209
- if (l.length > 0)
210
- for (const k of l)
211
- k(g);
212
- const y = w?.timeout || 0;
213
- w?.abort ? m = w.abort : y > 0 && (m = AbortSignal.timeout(y));
214
- const v = M(w?.client) ? w.client : ye(), S = Q(v), L = {
215
- host: S?.host,
216
- method: e,
217
- endpoint: t,
218
- queryParams: new URLSearchParams(),
219
- headers: new Headers(),
220
- body: void 0,
221
- withCredentials: o,
222
- responseType: i,
223
- context: h || se(),
224
- observe: b,
225
- uploadProgress: s,
226
- downloadProgress: p,
227
- timeout: y,
228
- abort: m
229
- }, D = w?.handler || S?.handler || ce();
230
- if (!D)
231
- throw X;
232
- r && n && await de(L, n, g);
233
- let C = await ie([...c])(L, D);
234
- if (typeof f == "function")
235
- try {
236
- C = K({
237
- ...C,
238
- body: f(C)
239
- });
240
- } catch (k) {
241
- throw new T({ error: k });
242
- }
243
- switch (L.observe) {
244
- case "body":
245
- return C.body;
246
- case "response":
247
- return C;
248
- default:
249
- throw z;
250
- }
251
- },
252
- getInitValue: () => fe(n),
253
- setUploadProgress: (E) => {
254
- s = E;
255
- },
256
- setDownloadProgress: (E) => {
257
- p = E;
258
- }
259
- };
260
- };
261
- return a.withField = (s) => (n = s, r = !0, a), a.withInterceptors = (s) => (c = s, a), a.withContext = (s) => (h = s, a), a.withCredentials = (s) => (o = s, a), a.withValidators = (s) => (l = s, a), a.withTransformResponse = (s) => (f = s, a), a.withObserve = (s) => (b = s, a), a.withResponseType = (s) => (i = s, a), a;
262
- }
263
- function F(e, t) {
264
- return e.replace(/:([^\/]+)/g, (r, n) => t.get(n) ?? "undefined");
265
- }
266
- async function de(e, t, r) {
267
- function n(o) {
268
- return typeof o == "object" && o !== null;
269
- }
270
- function c(o) {
271
- switch (!0) {
272
- case Array.isArray(o):
273
- case (typeof o == "object" && o !== null):
274
- return JSON.stringify(o);
275
- case (typeof o == "object" && o === null):
276
- case typeof o == "boolean":
277
- case typeof o == "number":
278
- case typeof o == "string":
279
- return String(o);
280
- default:
281
- throw new Error("Unsupported value type");
282
- }
283
- }
284
- function i(o, l, f) {
285
- if (Array.isArray(f))
286
- for (const a of f)
287
- o.append(l, c(a));
288
- else
289
- o.set(l, c(f));
290
- return o;
291
- }
292
- function h(o, l, f) {
293
- if (Array.isArray(f))
294
- for (const a of f)
295
- h(o, l, c(a));
296
- return o.set(l, c(f)), o;
297
- }
298
- function b(o, l) {
299
- switch (!0) {
300
- case typeof o > "u":
301
- return l;
302
- case (typeof o == "object" && o === null):
303
- return o;
304
- default:
305
- return o;
306
- }
307
- }
308
- if (I(t)) {
309
- let o, l, f, a;
310
- const s = b(r, t()), p = j(t);
311
- await V([...p.validators, ...p.asyncValidator], s);
312
- for (const [m, d] of p.alias)
313
- switch (m) {
314
- case u.Query: {
315
- if (!d)
316
- throw P;
317
- l = i(new URLSearchParams(), d, s);
318
- break;
319
- }
320
- case u.Param: {
321
- if (!d)
322
- throw P;
323
- o = h(/* @__PURE__ */ new Map(), d, s);
324
- break;
325
- }
326
- case u.Header: {
327
- if (!d)
328
- throw P;
329
- f = i(new Headers(), d, s);
330
- break;
331
- }
332
- case u.Form: {
333
- if (!d)
334
- throw P;
335
- a = i(new FormData(), d, s);
336
- break;
337
- }
338
- case u.UrlForm: {
339
- if (!d)
340
- throw P;
341
- a = i(new URLSearchParams(), d, s);
342
- break;
343
- }
344
- case u.Json:
345
- case u.Body:
346
- a = s;
347
- break;
348
- }
349
- o && (e.endpoint = F(e.endpoint, o)), e.queryParams = l, e.headers = f, e.body = a;
350
- return;
351
- }
352
- if (J(t) && n(r)) {
353
- let o = /* @__PURE__ */ new Map(), l = new URLSearchParams(), f = new Headers(), a = new URLSearchParams(), s = new FormData();
354
- const p = {};
355
- let m, d;
356
- for (const [N, _] of Object.entries(t)) {
357
- const R = j(_);
358
- for (const [E, g] of R.alias) {
359
- const w = g || N, y = b(r[N], _());
360
- switch (await V([...R.validators, ...R.asyncValidator], y), E) {
361
- case u.Json: {
362
- Object.defineProperty(p, w, {
363
- value: y,
364
- writable: !0,
365
- enumerable: !0
366
- }), d = u.Json;
367
- break;
368
- }
369
- case u.Query:
370
- l = i(l, w, y);
371
- break;
372
- case u.Param:
373
- o = h(o, w, y);
374
- break;
375
- case u.Header:
376
- f = i(f, w, y);
377
- break;
378
- case u.Form: {
379
- s = i(s, w, y), d = u.Form;
380
- break;
381
- }
382
- case u.UrlForm: {
383
- a = i(a, w, y), d = u.UrlForm;
384
- break;
385
- }
386
- case u.Body: {
387
- m = y, d = u.Body;
388
- break;
389
- }
390
- }
391
- }
392
- }
393
- switch (e.endpoint = F(e.endpoint, o), e.queryParams = l, e.headers = f, d) {
394
- case u.Body:
395
- e.body = m;
396
- break;
397
- case u.Form:
398
- e.body = s;
399
- break;
400
- case u.UrlForm:
401
- e.body = a;
402
- break;
403
- case u.Json:
404
- e.body = p;
405
- break;
406
- }
407
- return;
408
- }
409
- throw Y;
410
- }
411
- function he(e) {
412
- const t = new URL(e.endpoint, e.host);
413
- e.queryParams && (t.search = e.queryParams.toString());
414
- const r = e.headers ?? new Headers();
415
- if (!r.has("Content-Type")) {
416
- const c = le(e.body);
417
- c && r.set("Content-Type", c);
418
- }
419
- r.has("Accept") || r.set("Accept", "application/json, text/plain, */*");
420
- const n = e.withCredentials ? "include" : void 0;
421
- return new Request(t, {
422
- headers: r,
423
- method: e.method,
424
- body: ue(e.body),
425
- signal: e.abort,
426
- credentials: n
427
- });
428
- }
429
- async function we(e) {
430
- const t = e.downloadProgress, r = he(e), n = e.abort;
431
- let c;
432
- try {
433
- c = await globalThis.fetch(r);
434
- } catch (p) {
435
- if (n?.aborted && n.reason instanceof Error)
436
- switch (!0) {
437
- case n.reason.name === "AbortError":
438
- throw new T({ error: G });
439
- case n.reason.name === "TimeoutError":
440
- throw new T({ error: W });
441
- }
442
- throw new T({ error: p });
443
- }
444
- const { ok: i, headers: h, status: b, statusText: o, url: l } = c, f = ee(h), a = te(h);
445
- let s = null;
446
- if (c.body) {
447
- const p = [], m = c.body.getReader();
448
- let d = 0;
449
- for (; ; ) {
450
- const { done: _, value: R } = await m.read();
451
- if (_)
452
- break;
453
- p.push(R), d += R.length, t?.({
454
- lengthComputable: f > 0,
455
- loaded: d,
456
- total: f
457
- });
458
- }
459
- const N = ne(p, d);
460
- try {
461
- s = re({
462
- request: e,
463
- content: N,
464
- contentType: a
465
- });
466
- } catch (_) {
467
- throw new T({
468
- error: _,
469
- status: b,
470
- statusText: o,
471
- headers: h,
472
- url: l
473
- });
474
- }
475
- }
476
- if (!i)
477
- throw new T({
478
- status: b,
479
- statusText: o,
480
- headers: h,
481
- url: l,
482
- body: s
483
- });
484
- return K({
485
- url: l,
486
- status: b,
487
- statusText: o,
488
- headers: h,
489
- body: s
490
- });
491
- }
492
- const A = Symbol("Client");
493
- let O;
494
- function M(e) {
495
- return typeof e == "object" && e !== null && A in e;
496
- }
497
- function Q(e) {
498
- if (!M(e))
499
- throw Z;
500
- return e[A];
501
- }
502
- function pe(e) {
503
- const t = {
504
- host: e.host,
505
- strict: e.strict ?? !0,
506
- interceptors: e.interceptors ?? [],
507
- handler: e.handler || we
508
- };
509
- return {
510
- [A]: t
511
- };
512
- }
513
- function Te(e, t) {
514
- const n = {
515
- ...Q(e),
516
- ...t
517
- };
518
- return {
519
- [A]: n
520
- };
521
- }
522
- function ye() {
523
- if (!O)
524
- throw q;
525
- return O;
526
- }
527
- function Ne(e) {
528
- O = e;
529
- }
530
- function Ce() {
531
- O = void 0;
532
- }
533
- function Pe(e) {
534
- O = pe(e);
535
- }
536
- function Oe(e, t) {
537
- let r = t?.encode;
538
- if (!r) {
539
- const n = globalThis.btoa;
540
- if (typeof n != "function")
541
- throw new Error("BasicAuthInterceptor is not supported in this environment");
542
- r = (c) => n(`${c.username}:${c.password}`);
543
- }
544
- return (n, c) => {
545
- const i = n.headers || new Headers();
546
- return i.set("Authorization", `Basic ${r(e())}`), n.headers = i, c(n);
547
- };
548
- }
549
- const Ae = (e) => e ? null : new Error("required"), Le = (e) => typeof e != "boolean" || !e ? new Error("requiredTrue") : null, ke = (e) => (t) => typeof t != "string" ? new Error("invalid string") : t.length < e ? new Error(`minLength ${e}`) : null, He = (e) => (t) => typeof t != "string" ? new Error("invalid string") : t.length > e ? new Error(`maxLength ${e}`) : null, Ue = (e) => (t) => typeof t != "number" || isNaN(t) ? new Error("invalid number") : t < e ? new Error(`minLength ${e}`) : null, xe = (e) => (t) => typeof t != "number" || isNaN(t) ? new Error("invalid number") : t > e ? new Error(`maxLength ${e}`) : null, Ie = (e) => (t) => typeof t != "string" ? new Error("invalid string") : e.test(t) ? null : new Error("pattern");
550
- export {
551
- G as ERR_ABORTED,
552
- Z as ERR_INVALID_CLIENT,
553
- B as ERR_INVALID_HTTP_CONTEXT_TOKEN,
554
- me as ERR_NETWORK,
555
- q as ERR_NOT_FOUND_GLOBAL_CLIENT,
556
- X as ERR_NOT_FOUND_HANDLER,
557
- P as ERR_NOT_SET_ALIAS,
558
- z as ERR_OBSERVE,
559
- W as ERR_TIMEOUT,
560
- Ee as ERR_UNKNOWN,
561
- Y as ERR_UNSUPPORTED_FIELD_TYPE,
562
- T as HttpErrorResponse,
563
- Oe as basicAuthInterceptor,
564
- Te as cloneClient,
565
- pe as createClient,
566
- Pe as createGlobalClient,
567
- ge as defineRequest,
568
- V as doValid,
569
- we as fetchHandler,
570
- Re as field,
571
- ye as getGlobalClient,
572
- M as isClient,
573
- I as isField,
574
- J as isFieldGroup,
575
- H as isHttpContextToken,
576
- se as makeHttpContext,
577
- _e as makeHttpContextToken,
578
- xe as max,
579
- He as maxLength,
580
- Ue as min,
581
- ke as minLength,
582
- Ie as pattern,
583
- Ae as required,
584
- Le as requiredTrue,
585
- Ce as restGlobalClient,
586
- Ne as setGlobalClient
587
- };
1
+ var X=new Error("ERR_ABORTED"),W=new Error("ERR_TIMEOUT"),wt=new Error("ERR_NETWORK"),J=new Error("ERR_NOT_FOUND_HANDLER"),j=new Error("ERR_OBSERVE"),h=new Error("ERR_NOT_SET_ALIAS"),Q=new Error("ERR_UNSUPPORTED_FIELD_TYPE"),Y=new Error("ERR_INVALID_CLIENT"),Z=new Error("ERR_NOT_FOUND_GLOBAL_CLIENT"),B=new Error("ERR_INVALID_HTTP_CONTEXT_TOKEN"),it=new Error("ERR_UNKNOWN");class m extends Error{status;statusText;url;headers;body;constructor(t){const n=t?.status??0,r=t?.statusText??"",o=n>=200&&n<300,T=t?.headers??new Headers,H=t?.url??"",f=t?.body??null;let R;if(o)R=`Http failure during parsing for ${H}`;else if(R=`Http failure response for ${H||"(unknown url)"}: ${n}`,r)R+=` - ${r}`;super(R,{cause:t?.error});super.name="HttpErrorResponse",this.statusText=r,this.url=H,this.headers=T,this.status=n,this.body=f}}function tt(t){const n=t.get("Content-Length");if(!n)return 0;const r=Number(n);if(isNaN(r))return 0;return r}function nt(t){return t.get("Content-Type")||""}function rt(t){const{request:n,content:r,contentType:o}=t;switch(n.responseType){case"json":{const H=new TextDecoder().decode(r);if(H==="")return null;return JSON.parse(H)}case"text":return new TextDecoder().decode(r);case"blob":return new Blob([r],{type:o});case"arraybuffer":return r.buffer;default:return null}}function ot(t,n){const r=new Uint8Array(n);let o=0;for(let T of t)r.set(T,o),o+=T.length;return r}function ft(t){return Object.defineProperty(t,et,{value:t}),t}function D(t){return typeof t==="function"&&et in t}function Rt(t){return typeof t==="object"&&t!==null&&u in t}function G(t){const n=new Map;switch(!0){case Array.isArray(t):{for(let[r,o]of t)if(D(r))n.set(r,o);break}case Rt(t):{for(let[r,o]of t[u])n.set(r,o);break}}return{set(r,o){if(!D(r))throw B;return n.set(r,o),this},get(r){if(!D(r))throw B;if(!n.has(r))n.set(r,r());return n.get(r)},del(r){return n.delete(r),this},has(r){return n.has(r)},keys(){return n.keys()},get length(){return Array.from(n.keys()).length},[u]:n}}var et=Symbol("HttpContextToken"),u=Symbol("HttpContext");function kt(t){const n={required:typeof t!=="undefined",alias:new Map,validators:[],asyncValidator:[]},r=()=>t;return Object.defineProperty(r,z,{value:n}),r.withJson=(o)=>{return n.alias.set(0,o),r},r.withForm=(o)=>{return n.alias.set(4,o),r},r.withQuery=(o)=>{return n.alias.set(1,o),r},r.withParam=(o)=>{return n.alias.set(2,o),r},r.withHeader=(o)=>{return n.alias.set(3,o),r},r.withUrlForm=(o)=>{return n.alias.set(6,o),r},r.withBody=()=>{return n.alias.set(5,void 0),r},r.withValidators=(...o)=>{return n.validators=o,r},r.withAsyncValidators=(...o)=>{return n.asyncValidator=o,r},r}function a(t){return t[z]}async function L(t,n){await Promise.all(t.map((r)=>r(n)))}function F(t){return!!t&&typeof t==="function"&&z in t}function P(t){return!!t&&typeof t==="object"&&Object.entries(t).every(([,n])=>F(n))}var z=Symbol("field");var Ct=void 0;var xt=()=>Ct;function Tt(t){return t.reduceRight((n,r)=>{return(o,T)=>r(o,(H)=>n(H,T))},(n,r)=>r(n))}function y(t){const n=t?.status??0,r=t?.statusText??"",o=t?.url??"",T=t?.headers??new Headers,H=t?.body??null;return{status:n,statusText:r,url:o,headers:T,body:H}}function Ht(t){switch(!0){case t instanceof FormData:case t instanceof Blob:case t instanceof ArrayBuffer:case t instanceof URLSearchParams:case typeof t==="string":return t;case typeof t==="object":case typeof t==="boolean":case typeof t==="number":case Array.isArray(t):return JSON.stringify(t);default:return null}}function pt(t){switch(!0){case t instanceof FormData:return"multipart/form-data";case t instanceof ArrayBuffer:return"application/octet-stream";case t instanceof Blob:return t.type||"application/octet-stream";case t instanceof URLSearchParams:return"application/x-www-form-urlencoded;charset=UTF-8";case typeof t==="string":return"text/plain";case(typeof t==="object"&&t!==null):case typeof t==="number":case typeof t==="boolean":return"application/json";default:return null}}function mt(t){if(F(t))return t();if(P(t)){const n={};for(let[r,o]of Object.entries(t))n[r]=o();return n}return}function Nt(...t){let n,r,o=!1,T,H=[],f="json",R,e="body",E=!1,s=[],w;switch(t.length){case 1:{n="GET",r=t[0];break}case 2:{n=t[0],r=t[1];break}default:throw new Error("Invalid arguments")}const x=()=>{let c,N,p;return{doRequest:async(...C)=>{let k,i;switch(C.length){case 1:{if(o)k=C[0];else i=C[0];break}case 2:{k=C[0],i=C[1];break}}if(o&&!k)throw new Error("Because the request has input, the first argument must be the input value");if(s.length>0)for(let K of s)K(k);const $=i?.timeout||0;if(i?.abort)p=i.abort;else if($>0)p=AbortSignal.timeout($);const st=V(i?.client)?i.client:b(),l=S(st),g={host:l?.host,method:n,endpoint:r,queryParams:new URLSearchParams,headers:new Headers,body:void 0,withCredentials:E,responseType:f,context:R||G(),observe:e,uploadProgress:c,downloadProgress:N,timeout:$,abort:p},v=i?.handler||l?.handler||xt();if(!v)throw J;if(o&&T)await _t(g,T,k);let I=await Tt([...H])(g,v);if(typeof w==="function")try{I=y({...I,body:w(I)})}catch(K){throw new m({error:K})}switch(g.observe){case"body":return I.body;case"response":return I;default:throw j}},getInitValue:()=>mt(T),setUploadProgress:(C)=>{c=C},setDownloadProgress:(C)=>{N=C}}};return x.withField=(c)=>{return T=c,o=!0,x},x.withInterceptors=(c)=>{return H=c,x},x.withContext=(c)=>{return R=c,x},x.withCredentials=(c)=>{return E=c,x},x.withValidators=(c)=>{return s=c,x},x.withTransformResponse=(c)=>{return w=c,x},x.withObserve=(c)=>{return e=c,x},x.withResponseType=(c)=>{return f=c,x},x}function ct(t,n){return t.replace(/:([^\/]+)/g,(r,o)=>{return n.get(o)??"undefined"})}async function _t(t,n,r){function o(e){return typeof e==="object"&&e!==null}function T(e){switch(!0){case Array.isArray(e):case(typeof e==="object"&&e!==null):return JSON.stringify(e);case(typeof e==="object"&&e===null):case typeof e==="boolean":case typeof e==="number":case typeof e==="string":return String(e);default:throw new Error("Unsupported value type")}}function H(e,E,s){if(Array.isArray(s))for(let w of s)e.append(E,T(w));else e.set(E,T(s));return e}function f(e,E,s){if(Array.isArray(s))for(let w of s)f(e,E,T(w));return e.set(E,T(s)),e}function R(e,E){switch(!0){case typeof e==="undefined":return E;case(typeof e==="object"&&e===null):return e;default:return e}}if(F(n)){let e,E,s,w=void 0;const x=R(r,n()),c=a(n);await L([...c.validators,...c.asyncValidator],x);for(let[N,p]of c.alias)switch(N){case 1:{if(!p)throw h;E=H(new URLSearchParams,p,x);break}case 2:{if(!p)throw h;e=f(new Map,p,x);break}case 3:{if(!p)throw h;s=H(new Headers,p,x);break}case 4:{if(!p)throw h;w=H(new FormData,p,x);break}case 6:{if(!p)throw h;w=H(new URLSearchParams,p,x);break}case 0:case 5:w=x;break}if(e)t.endpoint=ct(t.endpoint,e);t.queryParams=E,t.headers=s,t.body=w;return}if(P(n)&&o(r)){let e=new Map,E=new URLSearchParams,s=new Headers,w=new URLSearchParams,x=new FormData;const c={};let N=void 0,p;for(let[O,_]of Object.entries(n)){const A=a(_);for(let[q,C]of A.alias){const k=C||O,i=R(r[O],_());switch(await L([...A.validators,...A.asyncValidator],i),q){case 0:{Object.defineProperty(c,k,{value:i,writable:!0,enumerable:!0}),p=0;break}case 1:E=H(E,k,i);break;case 2:e=f(e,k,i);break;case 3:s=H(s,k,i);break;case 4:{x=H(x,k,i),p=4;break}case 6:{w=H(w,k,i),p=6;break}case 5:{N=i,p=5;break}}}}switch(t.endpoint=ct(t.endpoint,e),t.queryParams=E,t.headers=s,p){case 5:t.body=N;break;case 4:t.body=x;break;case 6:t.body=w;break;case 0:t.body=c;break}return}throw Q}function At(t){const n=new URL(t.endpoint,t.host);if(t.queryParams)n.search=t.queryParams.toString();const r=t.headers??new Headers;if(!r.has("Content-Type")){const T=pt(t.body);if(T)r.set("Content-Type",T)}if(!r.has("Accept"))r.set("Accept","application/json, text/plain, */*");const o=t.withCredentials?"include":void 0;return new Request(n,{headers:r,method:t.method,body:Ht(t.body),signal:t.abort,credentials:o})}async function d(t){const n=t.downloadProgress,r=At(t),o=t.abort;let T;try{T=await globalThis.fetch(r)}catch(c){if(o?.aborted&&o.reason instanceof Error)switch(!0){case o.reason.name==="AbortError":throw new m({error:X});case o.reason.name==="TimeoutError":throw new m({error:W})}throw new m({error:c})}const{ok:H,headers:f,status:R,statusText:e,url:E}=T,s=tt(f),w=nt(f);let x=null;if(T.body){const c=[],N=T.body.getReader();let p=0;while(!0){const{done:_,value:A}=await N.read();if(_)break;c.push(A),p+=A.length,n?.({lengthComputable:s>0,loaded:p,total:s})}const O=ot(c,p);try{x=rt({request:t,content:O,contentType:w})}catch(_){throw new m({error:_,status:R,statusText:e,headers:f,url:E})}}if(!H)throw new m({status:R,statusText:e,headers:f,url:E,body:x});return y({url:E,status:R,statusText:e,headers:f,body:x})}function V(t){return typeof t==="object"&&t!==null&&M in t}function S(t){if(!V(t))throw Y;return t[M]}function Et(t){const n={host:t.host,strict:t.strict??!0,interceptors:t.interceptors??[],handler:t.handler||d};return{[M]:n}}function ht(t,n){const o={...S(t),...n};return{[M]:o}}function b(){if(!U)throw Z;return U}function Ot(t){U=t}function It(){U=void 0}function Ft(t){U=Et(t)}var M=Symbol("Client"),U;function Ut(t,n){let r=n?.encode;if(!r){const o=globalThis.btoa;if(typeof o!=="function")throw new Error("BasicAuthInterceptor is not supported in this environment");r=(T)=>o(`${T.username}:${T.password}`)}return(o,T)=>{const H=o.headers||new Headers;return H.set("Authorization",`Basic ${r(t())}`),o.headers=H,T(o)}}var Bt=(t)=>{if(!t)return new Error("required");return null},Dt=(t)=>{if(typeof t!=="boolean"||!t)return new Error("requiredTrue");return null},Lt=(t)=>{return(n)=>{if(typeof n!=="string")return new Error("invalid string");if(n.length<t)return new Error(`minLength ${t}`);return null}},Pt=(t)=>{return(n)=>{if(typeof n!=="string")return new Error("invalid string");if(n.length>t)return new Error(`maxLength ${t}`);return null}},yt=(t)=>{return(n)=>{if(typeof n!=="number"||isNaN(n))return new Error("invalid number");if(n<t)return new Error(`minLength ${t}`);return null}},Vt=(t)=>{return(n)=>{if(typeof n!=="number"||isNaN(n))return new Error("invalid number");if(n>t)return new Error(`maxLength ${t}`);return null}},Mt=(t)=>{return(n)=>{if(typeof n!=="string")return new Error("invalid string");if(!t.test(n))return new Error("pattern");return null}};export{Ot as setGlobalClient,It as restGlobalClient,Dt as requiredTrue,Bt as required,Mt as pattern,Lt as minLength,yt as min,Pt as maxLength,Vt as max,ft as makeHttpContextToken,G as makeHttpContext,D as isHttpContextToken,P as isFieldGroup,F as isField,V as isClient,b as getGlobalClient,kt as field,d as fetchHandler,L as doValid,Nt as defineRequest,Ft as createGlobalClient,Et as createClient,ht as cloneClient,Ut as basicAuthInterceptor,m as HttpErrorResponse,Q as ERR_UNSUPPORTED_FIELD_TYPE,it as ERR_UNKNOWN,W as ERR_TIMEOUT,j as ERR_OBSERVE,h as ERR_NOT_SET_ALIAS,J as ERR_NOT_FOUND_HANDLER,Z as ERR_NOT_FOUND_GLOBAL_CLIENT,wt as ERR_NETWORK,B as ERR_INVALID_HTTP_CONTEXT_TOKEN,Y as ERR_INVALID_CLIENT,X as ERR_ABORTED};
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "@defjs/core",
3
- "version": "0.0.0-test-004",
3
+ "version": "0.1.0",
4
4
  "type": "module",
5
5
  "module": "index.js",
6
6
  "typings": "index.d.ts",
7
7
  "license": "MIT",
8
+ "publishConfig": {
9
+ "directory": "dist"
10
+ },
8
11
  "repository": {
9
12
  "type": "git",
10
13
  "url": "git+https://github.com/defjs/defjs.git"