mobx-tanstack-query-api 0.33.1 → 0.34.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.
- package/index.cjs +203 -190
- package/index.cjs.map +1 -1
- package/index.d.ts +56 -48
- package/index.js +203 -190
- package/index.js.map +1 -1
- package/package.json +1 -1
package/index.cjs
CHANGED
|
@@ -423,213 +423,65 @@ class EndpointMutation extends mobxTanstackQuery.Mutation {
|
|
|
423
423
|
});
|
|
424
424
|
}
|
|
425
425
|
}
|
|
426
|
-
const ContentType = {
|
|
427
|
-
Json: "application/json",
|
|
428
|
-
FormData: "multipart/form-data",
|
|
429
|
-
UrlEncoded: "application/x-www-form-urlencoded",
|
|
430
|
-
Text: "text/plain",
|
|
431
|
-
Binary: "application/octet-stream"
|
|
432
|
-
};
|
|
433
|
-
const isHttpResponse = (response, status) => !!response && typeof response === "object" && response instanceof Response && "data" in response && (!status || response.status === status);
|
|
434
|
-
const isHttpBadResponse = (response) => {
|
|
435
|
-
return isHttpResponse(response) && (!response.ok || !!response.error);
|
|
436
|
-
};
|
|
437
426
|
const emptyStatusCodesSet = /* @__PURE__ */ new Set([204, 205, 304]);
|
|
438
|
-
class
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
this.
|
|
447
|
-
this.
|
|
448
|
-
this.
|
|
449
|
-
this.
|
|
450
|
-
this.
|
|
451
|
-
this.
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
Object.assign(this.config, update);
|
|
469
|
-
if (update.baseApiParams) {
|
|
470
|
-
Object.assign(this.baseApiParams, update.baseApiParams);
|
|
471
|
-
}
|
|
472
|
-
if (update.contentFormatters) {
|
|
473
|
-
Object.assign(this.contentFormatters, update.contentFormatters);
|
|
474
|
-
}
|
|
475
|
-
if (update.fetch) {
|
|
476
|
-
this.fetch = update.fetch;
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
setMeta = (data) => {
|
|
480
|
-
this.meta = data;
|
|
481
|
-
};
|
|
482
|
-
setBadResponse = (response) => {
|
|
483
|
-
this.badResponse = response;
|
|
484
|
-
};
|
|
485
|
-
contentFormatters = {
|
|
486
|
-
"application/json": (input) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
|
|
487
|
-
"text/plain": (input) => input !== null && typeof input !== "string" ? JSON.stringify(input) : input,
|
|
488
|
-
"multipart/form-data": (input) => Object.keys(input || {}).reduce((formData, key) => {
|
|
489
|
-
const property = input[key];
|
|
490
|
-
if (property instanceof Blob) {
|
|
491
|
-
formData.append(key, property);
|
|
492
|
-
} else if (typeof property === "object" && property !== null) {
|
|
493
|
-
formData.append(key, JSON.stringify(property));
|
|
494
|
-
} else {
|
|
495
|
-
formData.append(key, `${property}`);
|
|
496
|
-
}
|
|
497
|
-
return formData;
|
|
498
|
-
}, new FormData()),
|
|
499
|
-
"application/x-www-form-urlencoded": (input) => this.toQueryString(input),
|
|
500
|
-
"application/octet-stream": (input) => input
|
|
501
|
-
};
|
|
502
|
-
mergeRequestParams(params1, params2) {
|
|
503
|
-
return {
|
|
504
|
-
...this.baseApiParams,
|
|
505
|
-
...params1,
|
|
506
|
-
...params2,
|
|
507
|
-
headers: {
|
|
508
|
-
...this.baseApiParams.headers,
|
|
509
|
-
...params1.headers,
|
|
510
|
-
...params2?.headers
|
|
511
|
-
}
|
|
512
|
-
};
|
|
513
|
-
}
|
|
514
|
-
isEmptyResponseBody(response) {
|
|
515
|
-
if (emptyStatusCodesSet.has(response.status)) {
|
|
427
|
+
class HttpResponse {
|
|
428
|
+
constructor(originalResponse, request) {
|
|
429
|
+
this.originalResponse = originalResponse;
|
|
430
|
+
this.request = request;
|
|
431
|
+
this.headers = originalResponse.headers;
|
|
432
|
+
this.ok = originalResponse.ok;
|
|
433
|
+
this.body = originalResponse.body;
|
|
434
|
+
this.redirected = originalResponse.redirected;
|
|
435
|
+
this.status = originalResponse.status;
|
|
436
|
+
this.statusText = originalResponse.statusText;
|
|
437
|
+
this.type = originalResponse.type;
|
|
438
|
+
this.url = originalResponse.url;
|
|
439
|
+
this.data = null;
|
|
440
|
+
this.error = null;
|
|
441
|
+
}
|
|
442
|
+
headers;
|
|
443
|
+
ok;
|
|
444
|
+
redirected;
|
|
445
|
+
statusText;
|
|
446
|
+
type;
|
|
447
|
+
url;
|
|
448
|
+
body;
|
|
449
|
+
data;
|
|
450
|
+
error;
|
|
451
|
+
status;
|
|
452
|
+
clone() {
|
|
453
|
+
return new HttpResponse(this.originalResponse.clone(), this.request);
|
|
454
|
+
}
|
|
455
|
+
isEmpty() {
|
|
456
|
+
if (emptyStatusCodesSet.has(this.status)) {
|
|
516
457
|
return true;
|
|
517
458
|
}
|
|
518
|
-
const contentLength =
|
|
459
|
+
const contentLength = this.headers.get("content-length");
|
|
519
460
|
if (contentLength !== null && contentLength === "0") {
|
|
520
461
|
return true;
|
|
521
462
|
}
|
|
522
|
-
if (
|
|
463
|
+
if (this.body === null) {
|
|
523
464
|
return true;
|
|
524
465
|
}
|
|
525
466
|
return false;
|
|
526
467
|
}
|
|
527
|
-
|
|
528
|
-
* Some custom fetch implementations expose read-only accessors (e.g. `data`),
|
|
529
|
-
* so plain assignment can throw in strict mode.
|
|
530
|
-
*/
|
|
531
|
-
setResponseField = (response, key, value) => {
|
|
468
|
+
async resolveBody(responseFormat) {
|
|
532
469
|
try {
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
}
|
|
537
|
-
Object.defineProperty(response, key, {
|
|
538
|
-
value,
|
|
539
|
-
writable: true,
|
|
540
|
-
configurable: true,
|
|
541
|
-
enumerable: true
|
|
542
|
-
});
|
|
543
|
-
};
|
|
544
|
-
async createResponse(responseFormat = "json", raw, url, params) {
|
|
545
|
-
const response = raw;
|
|
546
|
-
this.setResponseField(response, "request", { url, params });
|
|
547
|
-
this.setResponseField(response, "data", null);
|
|
548
|
-
this.setResponseField(response, "error", null);
|
|
549
|
-
if (this.isEmptyResponseBody(response)) {
|
|
550
|
-
return response;
|
|
551
|
-
}
|
|
552
|
-
try {
|
|
553
|
-
const formatted = await response[responseFormat]();
|
|
554
|
-
if (response.ok) {
|
|
555
|
-
this.setResponseField(response, "data", formatted);
|
|
470
|
+
const formatted = await this.originalResponse[responseFormat]();
|
|
471
|
+
if (this.ok) {
|
|
472
|
+
this.data = formatted;
|
|
556
473
|
} else {
|
|
557
|
-
this.
|
|
474
|
+
this.error = formatted;
|
|
558
475
|
}
|
|
559
476
|
} catch (error) {
|
|
560
|
-
this.
|
|
561
|
-
}
|
|
562
|
-
if (!response.ok || response.error) {
|
|
563
|
-
this.setBadResponse(response);
|
|
564
|
-
}
|
|
565
|
-
return response;
|
|
566
|
-
}
|
|
567
|
-
buildUrl = (params) => {
|
|
568
|
-
const baseUrl = params.baseUrl ?? this.baseUrl ?? "";
|
|
569
|
-
const path = params.path;
|
|
570
|
-
const queryString = params.query && this.toQueryString(params.query);
|
|
571
|
-
const query = queryString ? `?${queryString}` : "";
|
|
572
|
-
if (this.config.buildUrl) {
|
|
573
|
-
return this.config.buildUrl(params, { baseUrl, path, query }, this.meta);
|
|
574
|
-
}
|
|
575
|
-
const url = baseUrl + path + query;
|
|
576
|
-
return url;
|
|
577
|
-
};
|
|
578
|
-
async request(fullParams, endpoint) {
|
|
579
|
-
this.setBadResponse(null);
|
|
580
|
-
const { body, contentType, format, ...params } = fullParams;
|
|
581
|
-
let requestParams = this.mergeRequestParams(params);
|
|
582
|
-
if (this.config.interceptor) {
|
|
583
|
-
requestParams = await this.config.interceptor(requestParams, this.meta, endpoint) ?? requestParams;
|
|
584
|
-
}
|
|
585
|
-
const responseFormat = format || requestParams.format;
|
|
586
|
-
const url = this.buildUrl(fullParams);
|
|
587
|
-
let headers;
|
|
588
|
-
if (requestParams.headers instanceof Headers) {
|
|
589
|
-
headers = requestParams.headers;
|
|
590
|
-
} else if (Array.isArray(requestParams.headers)) {
|
|
591
|
-
headers = new Headers(requestParams.headers);
|
|
592
|
-
} else {
|
|
593
|
-
headers = new Headers(requestParams.headers);
|
|
594
|
-
}
|
|
595
|
-
let bodyToSend;
|
|
596
|
-
if (contentType) {
|
|
597
|
-
if (contentType !== ContentType.FormData && !headers.has("Content-Type")) {
|
|
598
|
-
headers.set("Content-Type", contentType);
|
|
599
|
-
}
|
|
600
|
-
const payloadFormatter = this.contentFormatters[contentType];
|
|
601
|
-
if (body == null) {
|
|
602
|
-
bodyToSend = null;
|
|
603
|
-
} else if (payloadFormatter) {
|
|
604
|
-
bodyToSend = payloadFormatter(body);
|
|
605
|
-
} else {
|
|
606
|
-
bodyToSend = body;
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
const fetchUrl = url;
|
|
610
|
-
const fetchParams = {
|
|
611
|
-
...requestParams,
|
|
612
|
-
headers,
|
|
613
|
-
body: bodyToSend
|
|
614
|
-
};
|
|
615
|
-
let response;
|
|
616
|
-
try {
|
|
617
|
-
response = await this.fetch(fetchUrl, fetchParams);
|
|
618
|
-
} catch (error) {
|
|
619
|
-
response = error;
|
|
620
|
-
}
|
|
621
|
-
const httpResponse = await this.createResponse(
|
|
622
|
-
responseFormat,
|
|
623
|
-
response,
|
|
624
|
-
fetchUrl,
|
|
625
|
-
fetchParams
|
|
626
|
-
);
|
|
627
|
-
if (!httpResponse.ok || httpResponse.error) {
|
|
628
|
-
throw httpResponse;
|
|
477
|
+
this.error = error;
|
|
629
478
|
}
|
|
630
|
-
return httpResponse;
|
|
631
479
|
}
|
|
632
480
|
}
|
|
481
|
+
const isHttpResponse = (response, status) => typeof response === "object" && response instanceof HttpResponse && "data" in response && (!status || response.status === status);
|
|
482
|
+
const isHttpBadResponse = (response) => {
|
|
483
|
+
return isHttpResponse(response) && (!response.ok || !!response.error);
|
|
484
|
+
};
|
|
633
485
|
class Endpoint {
|
|
634
486
|
constructor(configuration, queryClient, httpClient) {
|
|
635
487
|
this.configuration = configuration;
|
|
@@ -847,12 +699,173 @@ const applyStringFilter = (filter, value) => {
|
|
|
847
699
|
}
|
|
848
700
|
return values.includes(filter);
|
|
849
701
|
};
|
|
702
|
+
const ContentType = {
|
|
703
|
+
Json: "application/json",
|
|
704
|
+
FormData: "multipart/form-data",
|
|
705
|
+
UrlEncoded: "application/x-www-form-urlencoded",
|
|
706
|
+
Text: "text/plain",
|
|
707
|
+
Binary: "application/octet-stream"
|
|
708
|
+
};
|
|
709
|
+
class HttpClient {
|
|
710
|
+
config;
|
|
711
|
+
fetch;
|
|
712
|
+
meta;
|
|
713
|
+
baseApiParams;
|
|
714
|
+
badResponse;
|
|
715
|
+
toQueryString;
|
|
716
|
+
constructor(config) {
|
|
717
|
+
this.config = config ?? {};
|
|
718
|
+
this.badResponse = null;
|
|
719
|
+
this.meta = config?.meta ?? null;
|
|
720
|
+
this.fetch = config?.fetch ?? ((...fetchParams) => globalThis.fetch(...fetchParams));
|
|
721
|
+
this.toQueryString = config?.toQueryString ?? ((query) => qs.stringify(query, config?.queryStringifyOptions));
|
|
722
|
+
this.baseApiParams = {
|
|
723
|
+
credentials: "same-origin",
|
|
724
|
+
headers: {},
|
|
725
|
+
redirect: "follow",
|
|
726
|
+
referrerPolicy: "no-referrer"
|
|
727
|
+
};
|
|
728
|
+
this.updateConfig(this.config);
|
|
729
|
+
mobx.observable.ref(this, "badResponse");
|
|
730
|
+
mobx.observable.ref(this, "meta");
|
|
731
|
+
mobx.action(this, "setMeta");
|
|
732
|
+
mobx.action(this, "setBadResponse");
|
|
733
|
+
mobx.makeObservable(this);
|
|
734
|
+
}
|
|
735
|
+
get baseUrl() {
|
|
736
|
+
return this.config.baseUrl ?? "";
|
|
737
|
+
}
|
|
738
|
+
updateConfig(update) {
|
|
739
|
+
Object.assign(this.config, update);
|
|
740
|
+
if (update.baseApiParams) {
|
|
741
|
+
Object.assign(this.baseApiParams, update.baseApiParams);
|
|
742
|
+
}
|
|
743
|
+
if (update.contentFormatters) {
|
|
744
|
+
Object.assign(this.contentFormatters, update.contentFormatters);
|
|
745
|
+
}
|
|
746
|
+
if (update.fetch) {
|
|
747
|
+
this.fetch = update.fetch;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
setMeta = (data) => {
|
|
751
|
+
this.meta = data;
|
|
752
|
+
};
|
|
753
|
+
setBadResponse = (response) => {
|
|
754
|
+
this.badResponse = response;
|
|
755
|
+
};
|
|
756
|
+
contentFormatters = {
|
|
757
|
+
"application/json": (input) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
|
|
758
|
+
"text/plain": (input) => input !== null && typeof input !== "string" ? JSON.stringify(input) : input,
|
|
759
|
+
"multipart/form-data": (input) => Object.keys(input || {}).reduce((formData, key) => {
|
|
760
|
+
const property = input[key];
|
|
761
|
+
if (property instanceof Blob) {
|
|
762
|
+
formData.append(key, property);
|
|
763
|
+
} else if (typeof property === "object" && property !== null) {
|
|
764
|
+
formData.append(key, JSON.stringify(property));
|
|
765
|
+
} else {
|
|
766
|
+
formData.append(key, `${property}`);
|
|
767
|
+
}
|
|
768
|
+
return formData;
|
|
769
|
+
}, new FormData()),
|
|
770
|
+
"application/x-www-form-urlencoded": (input) => this.toQueryString(input),
|
|
771
|
+
"application/octet-stream": (input) => input
|
|
772
|
+
};
|
|
773
|
+
mergeRequestParams(params1, params2) {
|
|
774
|
+
return {
|
|
775
|
+
...this.baseApiParams,
|
|
776
|
+
...params1,
|
|
777
|
+
...params2,
|
|
778
|
+
headers: {
|
|
779
|
+
...this.baseApiParams.headers,
|
|
780
|
+
...params1.headers,
|
|
781
|
+
...params2?.headers
|
|
782
|
+
}
|
|
783
|
+
};
|
|
784
|
+
}
|
|
785
|
+
async createResponse(responseFormat = "json", raw, url, params) {
|
|
786
|
+
const response = new HttpResponse(raw, { url, params });
|
|
787
|
+
if (response.isEmpty()) {
|
|
788
|
+
return response;
|
|
789
|
+
}
|
|
790
|
+
await response.resolveBody(responseFormat);
|
|
791
|
+
if (!response.ok || response.error) {
|
|
792
|
+
this.setBadResponse(response);
|
|
793
|
+
}
|
|
794
|
+
return response;
|
|
795
|
+
}
|
|
796
|
+
buildUrl = (params) => {
|
|
797
|
+
const baseUrl = params.baseUrl ?? this.baseUrl ?? "";
|
|
798
|
+
const path = params.path;
|
|
799
|
+
const queryString = params.query && this.toQueryString(params.query);
|
|
800
|
+
const query = queryString ? `?${queryString}` : "";
|
|
801
|
+
if (this.config.buildUrl) {
|
|
802
|
+
return this.config.buildUrl(params, { baseUrl, path, query }, this.meta);
|
|
803
|
+
}
|
|
804
|
+
const url = baseUrl + path + query;
|
|
805
|
+
return url;
|
|
806
|
+
};
|
|
807
|
+
async request(fullParams, endpoint) {
|
|
808
|
+
this.setBadResponse(null);
|
|
809
|
+
const { body, contentType, format, ...params } = fullParams;
|
|
810
|
+
let requestParams = this.mergeRequestParams(params);
|
|
811
|
+
if (this.config.interceptor) {
|
|
812
|
+
requestParams = await this.config.interceptor(requestParams, this.meta, endpoint) ?? requestParams;
|
|
813
|
+
}
|
|
814
|
+
const responseFormat = format || requestParams.format;
|
|
815
|
+
const url = this.buildUrl(fullParams);
|
|
816
|
+
let headers;
|
|
817
|
+
if (requestParams.headers instanceof Headers) {
|
|
818
|
+
headers = requestParams.headers;
|
|
819
|
+
} else if (Array.isArray(requestParams.headers)) {
|
|
820
|
+
headers = new Headers(requestParams.headers);
|
|
821
|
+
} else {
|
|
822
|
+
headers = new Headers(requestParams.headers);
|
|
823
|
+
}
|
|
824
|
+
let bodyToSend;
|
|
825
|
+
if (contentType) {
|
|
826
|
+
if (contentType !== ContentType.FormData && !headers.has("Content-Type")) {
|
|
827
|
+
headers.set("Content-Type", contentType);
|
|
828
|
+
}
|
|
829
|
+
const payloadFormatter = this.contentFormatters[contentType];
|
|
830
|
+
if (body == null) {
|
|
831
|
+
bodyToSend = null;
|
|
832
|
+
} else if (payloadFormatter) {
|
|
833
|
+
bodyToSend = payloadFormatter(body);
|
|
834
|
+
} else {
|
|
835
|
+
bodyToSend = body;
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
const fetchUrl = url;
|
|
839
|
+
const fetchParams = {
|
|
840
|
+
...requestParams,
|
|
841
|
+
headers,
|
|
842
|
+
body: bodyToSend
|
|
843
|
+
};
|
|
844
|
+
let response;
|
|
845
|
+
try {
|
|
846
|
+
response = await this.fetch(fetchUrl, fetchParams);
|
|
847
|
+
} catch (error) {
|
|
848
|
+
response = error;
|
|
849
|
+
}
|
|
850
|
+
const httpResponse = await this.createResponse(
|
|
851
|
+
responseFormat,
|
|
852
|
+
response,
|
|
853
|
+
fetchUrl,
|
|
854
|
+
fetchParams
|
|
855
|
+
);
|
|
856
|
+
if (!httpResponse.ok || httpResponse.error) {
|
|
857
|
+
throw httpResponse;
|
|
858
|
+
}
|
|
859
|
+
return httpResponse;
|
|
860
|
+
}
|
|
861
|
+
}
|
|
850
862
|
exports.ContentType = ContentType;
|
|
851
863
|
exports.Endpoint = Endpoint;
|
|
852
864
|
exports.EndpointMutation = EndpointMutation;
|
|
853
865
|
exports.EndpointQuery = EndpointQuery;
|
|
854
866
|
exports.EndpointQueryClient = EndpointQueryClient;
|
|
855
867
|
exports.HttpClient = HttpClient;
|
|
868
|
+
exports.HttpResponse = HttpResponse;
|
|
856
869
|
exports.buildOptionsFromParams = buildOptionsFromParams;
|
|
857
870
|
exports.emptyStatusCodesSet = emptyStatusCodesSet;
|
|
858
871
|
exports.getParamsFromContext = getParamsFromContext;
|