@dereekb/util 13.0.6 → 13.0.7
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/fetch/index.cjs.js +274 -33
- package/fetch/index.esm.js +274 -35
- package/fetch/package.json +2 -2
- package/fetch/src/lib/fetch.file.d.ts +67 -20
- package/fetch/src/lib/fetch.page.iterate.d.ts +277 -49
- package/package.json +1 -1
- package/src/lib/file/pdf.d.ts +15 -0
- package/test/package.json +2 -2
package/fetch/index.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BaseError } from 'make-error';
|
|
2
|
-
import { removeTrailingSlashes, asGetter, multiValueMapBuilder, filterMaybeArrayValues, isWebsiteUrlWithPrefix, fixMultiSlashesInSlashPath, isPromiseLike, cachedGetter, FIRST_PAGE, mapIdentityFunction, performTasksFromFactoryInParallelFunction, performAsyncTasks, mergeObjects, filterEmptyPojoValues, useIterableOrValue, isEmptyIterable, fixExtraQueryParameters, forEachInIterable, forEachKeyValue, isIterable } from '@dereekb/util';
|
|
2
|
+
import { removeTrailingSlashes, asGetter, multiValueMapBuilder, filterMaybeArrayValues, isWebsiteUrlWithPrefix, fixMultiSlashesInSlashPath, isPromiseLike, asArray, urlWithoutParameters, cachedGetter, FIRST_PAGE, mapIdentityFunction, performTasksFromFactoryInParallelFunction, performAsyncTasks, mergeObjects, filterEmptyPojoValues, useIterableOrValue, isEmptyIterable, fixExtraQueryParameters, forEachInIterable, forEachKeyValue, isIterable } from '@dereekb/util';
|
|
3
3
|
import { safeParse } from 'fast-content-type-parse';
|
|
4
4
|
|
|
5
5
|
function _assert_this_initialized$3(self) {
|
|
@@ -276,7 +276,7 @@ function _array_like_to_array$2(arr, len) {
|
|
|
276
276
|
function _array_with_holes$2(arr) {
|
|
277
277
|
if (Array.isArray(arr)) return arr;
|
|
278
278
|
}
|
|
279
|
-
function asyncGeneratorStep$
|
|
279
|
+
function asyncGeneratorStep$4(gen, resolve, reject, _next, _throw, key, arg) {
|
|
280
280
|
try {
|
|
281
281
|
var info = gen[key](arg);
|
|
282
282
|
var value = info.value;
|
|
@@ -290,16 +290,16 @@ function asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, key, arg) {
|
|
|
290
290
|
Promise.resolve(value).then(_next, _throw);
|
|
291
291
|
}
|
|
292
292
|
}
|
|
293
|
-
function _async_to_generator$
|
|
293
|
+
function _async_to_generator$4(fn) {
|
|
294
294
|
return function() {
|
|
295
295
|
var self = this, args = arguments;
|
|
296
296
|
return new Promise(function(resolve, reject) {
|
|
297
297
|
var gen = fn.apply(self, args);
|
|
298
298
|
function _next(value) {
|
|
299
|
-
asyncGeneratorStep$
|
|
299
|
+
asyncGeneratorStep$4(gen, resolve, reject, _next, _throw, "next", value);
|
|
300
300
|
}
|
|
301
301
|
function _throw(err) {
|
|
302
|
-
asyncGeneratorStep$
|
|
302
|
+
asyncGeneratorStep$4(gen, resolve, reject, _next, _throw, "throw", err);
|
|
303
303
|
}
|
|
304
304
|
_next(undefined);
|
|
305
305
|
});
|
|
@@ -401,7 +401,7 @@ function _unsupported_iterable_to_array$2(o, minLen) {
|
|
|
401
401
|
if (n === "Map" || n === "Set") return Array.from(n);
|
|
402
402
|
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array$2(o, minLen);
|
|
403
403
|
}
|
|
404
|
-
function _ts_generator$
|
|
404
|
+
function _ts_generator$4(thisArg, body) {
|
|
405
405
|
var f, y, t, _ = {
|
|
406
406
|
label: 0,
|
|
407
407
|
sent: function() {
|
|
@@ -552,9 +552,9 @@ function _ts_generator$3(thisArg, body) {
|
|
|
552
552
|
}
|
|
553
553
|
var makeFetchRequest = fetchRequestFactory(config);
|
|
554
554
|
return function(input, init) {
|
|
555
|
-
return _async_to_generator$
|
|
555
|
+
return _async_to_generator$4(function() {
|
|
556
556
|
var request, response;
|
|
557
|
-
return _ts_generator$
|
|
557
|
+
return _ts_generator$4(this, function(_state) {
|
|
558
558
|
switch(_state.label){
|
|
559
559
|
case 0:
|
|
560
560
|
return [
|
|
@@ -601,9 +601,9 @@ function fetchRequestFactory(config) {
|
|
|
601
601
|
return result;
|
|
602
602
|
} : undefined;
|
|
603
603
|
function asFetchRequest(input) {
|
|
604
|
-
return _async_to_generator$
|
|
604
|
+
return _async_to_generator$4(function() {
|
|
605
605
|
var awaitedInput, _tmp;
|
|
606
|
-
return _ts_generator$
|
|
606
|
+
return _ts_generator$4(this, function(_state) {
|
|
607
607
|
switch(_state.label){
|
|
608
608
|
case 0:
|
|
609
609
|
if (!isPromiseLike(input)) return [
|
|
@@ -641,9 +641,9 @@ function fetchRequestFactory(config) {
|
|
|
641
641
|
})();
|
|
642
642
|
}
|
|
643
643
|
var buildRequestWithFixedUrl = buildUrl ? function(input) {
|
|
644
|
-
return _async_to_generator$
|
|
644
|
+
return _async_to_generator$4(function() {
|
|
645
645
|
var relativeUrl, baseRequest, request, url;
|
|
646
|
-
return _ts_generator$
|
|
646
|
+
return _ts_generator$4(this, function(_state) {
|
|
647
647
|
switch(_state.label){
|
|
648
648
|
case 0:
|
|
649
649
|
if (!(typeof input === 'string')) return [
|
|
@@ -703,9 +703,9 @@ function fetchRequestFactory(config) {
|
|
|
703
703
|
var buildRequestInit;
|
|
704
704
|
if (inputBaseRequest != null || timeout != null) {
|
|
705
705
|
var computeBaseRequest = function computeBaseRequest() {
|
|
706
|
-
return _async_to_generator$
|
|
706
|
+
return _async_to_generator$4(function() {
|
|
707
707
|
var computedBaseRequest, baseRequest;
|
|
708
|
-
return _ts_generator$
|
|
708
|
+
return _ts_generator$4(this, function(_state) {
|
|
709
709
|
switch(_state.label){
|
|
710
710
|
case 0:
|
|
711
711
|
return [
|
|
@@ -726,9 +726,9 @@ function fetchRequestFactory(config) {
|
|
|
726
726
|
})();
|
|
727
727
|
};
|
|
728
728
|
var combineRequestInits = function combineRequestInits(request, requestInit) {
|
|
729
|
-
return _async_to_generator$
|
|
729
|
+
return _async_to_generator$4(function() {
|
|
730
730
|
var baseRequest, merged, _tmp, timeout;
|
|
731
|
-
return _ts_generator$
|
|
731
|
+
return _ts_generator$4(this, function(_state) {
|
|
732
732
|
switch(_state.label){
|
|
733
733
|
case 0:
|
|
734
734
|
return [
|
|
@@ -777,9 +777,9 @@ function fetchRequestFactory(config) {
|
|
|
777
777
|
};
|
|
778
778
|
}
|
|
779
779
|
return function(input, init) {
|
|
780
|
-
return _async_to_generator$
|
|
780
|
+
return _async_to_generator$4(function() {
|
|
781
781
|
var fixedRequest, request, e;
|
|
782
|
-
return _ts_generator$
|
|
782
|
+
return _ts_generator$4(this, function(_state) {
|
|
783
783
|
switch(_state.label){
|
|
784
784
|
case 0:
|
|
785
785
|
_state.trys.push([
|
|
@@ -881,17 +881,200 @@ function isFetchRequest(input) {
|
|
|
881
881
|
return Boolean(input.url);
|
|
882
882
|
}
|
|
883
883
|
|
|
884
|
-
function
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
884
|
+
function asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, key, arg) {
|
|
885
|
+
try {
|
|
886
|
+
var info = gen[key](arg);
|
|
887
|
+
var value = info.value;
|
|
888
|
+
} catch (error) {
|
|
889
|
+
reject(error);
|
|
890
|
+
return;
|
|
891
|
+
}
|
|
892
|
+
if (info.done) {
|
|
893
|
+
resolve(value);
|
|
894
|
+
} else {
|
|
895
|
+
Promise.resolve(value).then(_next, _throw);
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
function _async_to_generator$3(fn) {
|
|
899
|
+
return function() {
|
|
900
|
+
var self = this, args = arguments;
|
|
901
|
+
return new Promise(function(resolve, reject) {
|
|
902
|
+
var gen = fn.apply(self, args);
|
|
903
|
+
function _next(value) {
|
|
904
|
+
asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, "next", value);
|
|
905
|
+
}
|
|
906
|
+
function _throw(err) {
|
|
907
|
+
asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, "throw", err);
|
|
908
|
+
}
|
|
909
|
+
_next(undefined);
|
|
910
|
+
});
|
|
911
|
+
};
|
|
912
|
+
}
|
|
913
|
+
function _ts_generator$3(thisArg, body) {
|
|
914
|
+
var f, y, t, _ = {
|
|
915
|
+
label: 0,
|
|
916
|
+
sent: function() {
|
|
917
|
+
if (t[0] & 1) throw t[1];
|
|
918
|
+
return t[1];
|
|
919
|
+
},
|
|
920
|
+
trys: [],
|
|
921
|
+
ops: []
|
|
922
|
+
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
|
|
923
|
+
return d(g, "next", {
|
|
924
|
+
value: verb(0)
|
|
925
|
+
}), d(g, "throw", {
|
|
926
|
+
value: verb(1)
|
|
927
|
+
}), d(g, "return", {
|
|
928
|
+
value: verb(2)
|
|
929
|
+
}), typeof Symbol === "function" && d(g, Symbol.iterator, {
|
|
930
|
+
value: function() {
|
|
931
|
+
return this;
|
|
893
932
|
}
|
|
894
|
-
});
|
|
933
|
+
}), g;
|
|
934
|
+
function verb(n) {
|
|
935
|
+
return function(v) {
|
|
936
|
+
return step([
|
|
937
|
+
n,
|
|
938
|
+
v
|
|
939
|
+
]);
|
|
940
|
+
};
|
|
941
|
+
}
|
|
942
|
+
function step(op) {
|
|
943
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
944
|
+
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
945
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
946
|
+
if (y = 0, t) op = [
|
|
947
|
+
op[0] & 2,
|
|
948
|
+
t.value
|
|
949
|
+
];
|
|
950
|
+
switch(op[0]){
|
|
951
|
+
case 0:
|
|
952
|
+
case 1:
|
|
953
|
+
t = op;
|
|
954
|
+
break;
|
|
955
|
+
case 4:
|
|
956
|
+
_.label++;
|
|
957
|
+
return {
|
|
958
|
+
value: op[1],
|
|
959
|
+
done: false
|
|
960
|
+
};
|
|
961
|
+
case 5:
|
|
962
|
+
_.label++;
|
|
963
|
+
y = op[1];
|
|
964
|
+
op = [
|
|
965
|
+
0
|
|
966
|
+
];
|
|
967
|
+
continue;
|
|
968
|
+
case 7:
|
|
969
|
+
op = _.ops.pop();
|
|
970
|
+
_.trys.pop();
|
|
971
|
+
continue;
|
|
972
|
+
default:
|
|
973
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
974
|
+
_ = 0;
|
|
975
|
+
continue;
|
|
976
|
+
}
|
|
977
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
978
|
+
_.label = op[1];
|
|
979
|
+
break;
|
|
980
|
+
}
|
|
981
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
982
|
+
_.label = t[1];
|
|
983
|
+
t = op;
|
|
984
|
+
break;
|
|
985
|
+
}
|
|
986
|
+
if (t && _.label < t[2]) {
|
|
987
|
+
_.label = t[2];
|
|
988
|
+
_.ops.push(op);
|
|
989
|
+
break;
|
|
990
|
+
}
|
|
991
|
+
if (t[2]) _.ops.pop();
|
|
992
|
+
_.trys.pop();
|
|
993
|
+
continue;
|
|
994
|
+
}
|
|
995
|
+
op = body.call(thisArg, _);
|
|
996
|
+
} catch (e) {
|
|
997
|
+
op = [
|
|
998
|
+
6,
|
|
999
|
+
e
|
|
1000
|
+
];
|
|
1001
|
+
y = 0;
|
|
1002
|
+
} finally{
|
|
1003
|
+
f = t = 0;
|
|
1004
|
+
}
|
|
1005
|
+
if (op[0] & 5) throw op[1];
|
|
1006
|
+
return {
|
|
1007
|
+
value: op[0] ? op[1] : void 0,
|
|
1008
|
+
done: true
|
|
1009
|
+
};
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
/**
|
|
1013
|
+
* Creates a File object from the given input.
|
|
1014
|
+
*
|
|
1015
|
+
* @example
|
|
1016
|
+
* makeFileForFetch({ content: pdfBuffer, fileName: 'doc.pdf', mimeType: 'application/pdf' })
|
|
1017
|
+
*/ function makeFileForFetch(input) {
|
|
1018
|
+
var options = {};
|
|
1019
|
+
if (input.mimeType) {
|
|
1020
|
+
options.type = input.mimeType;
|
|
1021
|
+
}
|
|
1022
|
+
if (input.lastModified != null) {
|
|
1023
|
+
options.lastModified = input.lastModified;
|
|
1024
|
+
}
|
|
1025
|
+
return new File(asArray(input.content), input.fileName, options);
|
|
1026
|
+
}
|
|
1027
|
+
function fetchFileFromUrl(input, safe) {
|
|
1028
|
+
return _async_to_generator$3(function() {
|
|
1029
|
+
var url, mimeType, inputFetch, useFetch, response, result, _ref, _ref1, _input_fileName, buffer, responseMimeType, fileName, options;
|
|
1030
|
+
return _ts_generator$3(this, function(_state) {
|
|
1031
|
+
switch(_state.label){
|
|
1032
|
+
case 0:
|
|
1033
|
+
url = input.url, mimeType = input.mimeType, inputFetch = input.fetch;
|
|
1034
|
+
useFetch = inputFetch !== null && inputFetch !== void 0 ? inputFetch : fetch;
|
|
1035
|
+
return [
|
|
1036
|
+
4,
|
|
1037
|
+
useFetch(url, {
|
|
1038
|
+
method: 'GET'
|
|
1039
|
+
})
|
|
1040
|
+
];
|
|
1041
|
+
case 1:
|
|
1042
|
+
response = _state.sent();
|
|
1043
|
+
if (!!response.ok) return [
|
|
1044
|
+
3,
|
|
1045
|
+
2
|
|
1046
|
+
];
|
|
1047
|
+
if (!safe) {
|
|
1048
|
+
throw new Error("Failed to fetch file from ".concat(url, ": ").concat(response.status, " ").concat(response.statusText));
|
|
1049
|
+
}
|
|
1050
|
+
return [
|
|
1051
|
+
3,
|
|
1052
|
+
4
|
|
1053
|
+
];
|
|
1054
|
+
case 2:
|
|
1055
|
+
return [
|
|
1056
|
+
4,
|
|
1057
|
+
response.arrayBuffer()
|
|
1058
|
+
];
|
|
1059
|
+
case 3:
|
|
1060
|
+
buffer = _state.sent();
|
|
1061
|
+
responseMimeType = (_ref = mimeType !== null && mimeType !== void 0 ? mimeType : response.headers.get('content-type')) !== null && _ref !== void 0 ? _ref : undefined;
|
|
1062
|
+
fileName = (_ref1 = (_input_fileName = input.fileName) !== null && _input_fileName !== void 0 ? _input_fileName : urlWithoutParameters(url).split('/').pop()) !== null && _ref1 !== void 0 ? _ref1 : 'file';
|
|
1063
|
+
options = responseMimeType ? {
|
|
1064
|
+
type: responseMimeType
|
|
1065
|
+
} : {};
|
|
1066
|
+
result = new File([
|
|
1067
|
+
buffer
|
|
1068
|
+
], fileName, options);
|
|
1069
|
+
_state.label = 4;
|
|
1070
|
+
case 4:
|
|
1071
|
+
return [
|
|
1072
|
+
2,
|
|
1073
|
+
result
|
|
1074
|
+
];
|
|
1075
|
+
}
|
|
1076
|
+
});
|
|
1077
|
+
})();
|
|
895
1078
|
}
|
|
896
1079
|
/**
|
|
897
1080
|
* Parses the file response and returns the response wrapped in a FetchFileResponse object.
|
|
@@ -909,6 +1092,20 @@ function fetchUploadFile(input) {
|
|
|
909
1092
|
mimeType: contentType === null || contentType === void 0 ? void 0 : contentType.type
|
|
910
1093
|
};
|
|
911
1094
|
}
|
|
1095
|
+
/**
|
|
1096
|
+
* @deprecated Use makeFileForFetch() with FormData and context.fetch() instead.
|
|
1097
|
+
*/ function fetchUploadFile(input) {
|
|
1098
|
+
var _input_method;
|
|
1099
|
+
var inputFetch = input.fetch, url = input.url, inputBody = input.body;
|
|
1100
|
+
var useFetch = inputFetch !== null && inputFetch !== void 0 ? inputFetch : fetch;
|
|
1101
|
+
return useFetch(url, {
|
|
1102
|
+
method: (_input_method = input.method) !== null && _input_method !== void 0 ? _input_method : 'POST',
|
|
1103
|
+
body: inputBody.body,
|
|
1104
|
+
headers: {
|
|
1105
|
+
'Content-Type': inputBody.mimeType
|
|
1106
|
+
}
|
|
1107
|
+
});
|
|
1108
|
+
}
|
|
912
1109
|
|
|
913
1110
|
function asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, key, arg) {
|
|
914
1111
|
try {
|
|
@@ -1722,10 +1919,30 @@ function _ts_generator(thisArg, body) {
|
|
|
1722
1919
|
}
|
|
1723
1920
|
}
|
|
1724
1921
|
/**
|
|
1725
|
-
* Iterates through
|
|
1922
|
+
* Iterates through all pages of a paginated fetch and processes each item individually.
|
|
1726
1923
|
*
|
|
1727
|
-
* @
|
|
1728
|
-
* @
|
|
1924
|
+
* Built on top of {@link iterateFetchPagesByItems}, this function handles per-item granularity
|
|
1925
|
+
* by extracting items from each page and delegating to {@link performAsyncTasks}. Items are
|
|
1926
|
+
* processed sequentially by default to preserve ordering guarantees, but can be parallelized
|
|
1927
|
+
* via `iteratePerformTasksConfig`.
|
|
1928
|
+
*
|
|
1929
|
+
* Each item's callback receives a global index that reflects its position across all pages,
|
|
1930
|
+
* not just within the current page.
|
|
1931
|
+
*
|
|
1932
|
+
* @param config - Configuration specifying the fetch page source, item extraction, and per-item callback
|
|
1933
|
+
* @returns Combined result from {@link iterateFetchPagesByItems} including page/item counts and per-item task results
|
|
1934
|
+
*
|
|
1935
|
+
* @example
|
|
1936
|
+
* ```typescript
|
|
1937
|
+
* const result = await iterateFetchPagesByEachItem({
|
|
1938
|
+
* fetchPageFactory: myPageFactory,
|
|
1939
|
+
* input: { query: 'active' },
|
|
1940
|
+
* readItemsFromPageResult: (r) => r.result.items,
|
|
1941
|
+
* iterateEachPageItem: async (item, index, pageResult) => {
|
|
1942
|
+
* return processItem(item);
|
|
1943
|
+
* }
|
|
1944
|
+
* });
|
|
1945
|
+
* ```
|
|
1729
1946
|
*/ function iterateFetchPagesByEachItem(config) {
|
|
1730
1947
|
return _async_to_generator(function() {
|
|
1731
1948
|
var iterateEachPageItem, iteratePerformTasksConfig;
|
|
@@ -1771,10 +1988,32 @@ function _ts_generator(thisArg, body) {
|
|
|
1771
1988
|
})();
|
|
1772
1989
|
}
|
|
1773
1990
|
/**
|
|
1774
|
-
* Iterates through
|
|
1991
|
+
* Iterates through paginated fetch results at the item batch level.
|
|
1775
1992
|
*
|
|
1776
|
-
*
|
|
1777
|
-
*
|
|
1993
|
+
* Fetches pages sequentially (or in parallel via `maxParallelPages`), extracts items
|
|
1994
|
+
* from each page using `readItemsFromPageResult`, optionally filters them, then
|
|
1995
|
+
* passes the batch to `iteratePageItems`. Tracks both raw loaded counts and
|
|
1996
|
+
* post-filter visited counts, terminating when either limit is reached or pages
|
|
1997
|
+
* are exhausted.
|
|
1998
|
+
*
|
|
1999
|
+
* For per-item processing instead of batch processing, use {@link iterateFetchPagesByEachItem}.
|
|
2000
|
+
*
|
|
2001
|
+
* @param config - Configuration specifying fetch source, item extraction, filtering, limits, and batch callback
|
|
2002
|
+
* @returns Result with page count and item counters (loaded and visited)
|
|
2003
|
+
*
|
|
2004
|
+
* @example
|
|
2005
|
+
* ```typescript
|
|
2006
|
+
* const result = await iterateFetchPagesByItems({
|
|
2007
|
+
* fetchPageFactory: myPageFactory,
|
|
2008
|
+
* input: { status: 'active' },
|
|
2009
|
+
* readItemsFromPageResult: (r) => r.result.records,
|
|
2010
|
+
* filterPageItems: (items) => items.filter(x => x.isValid),
|
|
2011
|
+
* iterateItemsLimit: 500,
|
|
2012
|
+
* iteratePageItems: async (items, pageResult, totalVisited) => {
|
|
2013
|
+
* await bulkInsert(items);
|
|
2014
|
+
* }
|
|
2015
|
+
* });
|
|
2016
|
+
* ```
|
|
1778
2017
|
*/ function iterateFetchPagesByItems(config) {
|
|
1779
2018
|
return _async_to_generator(function() {
|
|
1780
2019
|
var readItemsFromPageResult, inputTotalIterateItemsLimit, inputTotalLoadItemLimit, inputFilterPageItems, iteratePageItems, iterateItemsLimit, loadItemLimit, filterPageItems, totalItemsLoaded, totalItemsVisited, hasReachedFinalItem, fetchPagesConfig, iterateFetchPagesResult;
|
|
@@ -2377,4 +2616,4 @@ var fetchJsonRequestInit = fetchJsonRequestInitFunction();
|
|
|
2377
2616
|
}
|
|
2378
2617
|
});
|
|
2379
2618
|
|
|
2380
|
-
export { DEFAULT_FETCH_HANDLER, DEFAULT_FETCH_REQUEST_FACTORY, FETCH_PAGE_FACTORY_DEFAULT_MAX_PAGE, FetchPageLimitReachedError, FetchPageNoNextPageError, FetchRequestFactoryError, FetchResponseError, FetchTimeoutError, JsonResponseParseError, configureFetch, fetchApiFetchService, fetchJsonBodyString, fetchJsonFunction, fetchJsonRequestInit, fetchJsonRequestInitFunction, fetchOk, fetchPageFactory, fetchRequestFactory, fetchService, fetchTimeout, fetchURL, fetchURLQueryKeyValueStringTuples, fetchURLSearchParamsObjectToURLSearchParams, fetchUploadFile, headersToHeadersTuple, isFetchRequest, isURL, isURLSearchParams, iterateFetchPages, iterateFetchPagesByEachItem, iterateFetchPagesByItems, makeUrlSearchParams, mergeMakeUrlSearchParamsOptions, mergeRequestHeaders, mergeRequestInits, parseFetchFileResponse, queryParamsToSearchParams, rateLimitedFetchHandler, requireOkResponse, returnNullHandleFetchJsonParseErrorFunction, throwJsonResponseParseErrorFunction };
|
|
2619
|
+
export { DEFAULT_FETCH_HANDLER, DEFAULT_FETCH_REQUEST_FACTORY, FETCH_PAGE_FACTORY_DEFAULT_MAX_PAGE, FetchPageLimitReachedError, FetchPageNoNextPageError, FetchRequestFactoryError, FetchResponseError, FetchTimeoutError, JsonResponseParseError, configureFetch, fetchApiFetchService, fetchFileFromUrl, fetchJsonBodyString, fetchJsonFunction, fetchJsonRequestInit, fetchJsonRequestInitFunction, fetchOk, fetchPageFactory, fetchRequestFactory, fetchService, fetchTimeout, fetchURL, fetchURLQueryKeyValueStringTuples, fetchURLSearchParamsObjectToURLSearchParams, fetchUploadFile, headersToHeadersTuple, isFetchRequest, isURL, isURLSearchParams, iterateFetchPages, iterateFetchPagesByEachItem, iterateFetchPagesByItems, makeFileForFetch, makeUrlSearchParams, mergeMakeUrlSearchParamsOptions, mergeRequestHeaders, mergeRequestInits, parseFetchFileResponse, queryParamsToSearchParams, rateLimitedFetchHandler, requireOkResponse, returnNullHandleFetchJsonParseErrorFunction, throwJsonResponseParseErrorFunction };
|
package/fetch/package.json
CHANGED
|
@@ -1,45 +1,72 @@
|
|
|
1
|
-
import { type ContentTypeMimeType, type Maybe, type MimeTypeWithoutParameters } from '@dereekb/util';
|
|
1
|
+
import { type ArrayOrValue, type ContentTypeMimeType, type Maybe, type MimeTypeWithoutParameters } from '@dereekb/util';
|
|
2
2
|
import { safeParse as parseContentType } from 'fast-content-type-parse';
|
|
3
3
|
import { type FetchMethod } from './fetch.type';
|
|
4
4
|
/**
|
|
5
|
-
* Input for
|
|
5
|
+
* Input for makeFileForFetch().
|
|
6
6
|
*/
|
|
7
|
-
export interface
|
|
7
|
+
export interface MakeFileForFetchInput {
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* File content. Can be a string, ArrayBuffer, Blob, or array of BlobParts.
|
|
10
10
|
*/
|
|
11
|
-
readonly
|
|
11
|
+
readonly content: ArrayOrValue<BlobPart>;
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
13
|
+
* File name including extension.
|
|
14
14
|
*
|
|
15
|
-
*
|
|
15
|
+
* @example 'document.pdf'
|
|
16
16
|
*/
|
|
17
|
-
readonly
|
|
17
|
+
readonly fileName: string;
|
|
18
18
|
/**
|
|
19
|
-
*
|
|
19
|
+
* MIME type of the file.
|
|
20
|
+
*
|
|
21
|
+
* @example 'application/pdf'
|
|
20
22
|
*/
|
|
21
|
-
readonly
|
|
23
|
+
readonly mimeType?: ContentTypeMimeType;
|
|
22
24
|
/**
|
|
23
|
-
*
|
|
25
|
+
* Last modified timestamp in milliseconds.
|
|
24
26
|
*/
|
|
25
|
-
readonly
|
|
27
|
+
readonly lastModified?: number;
|
|
26
28
|
}
|
|
27
29
|
/**
|
|
28
|
-
*
|
|
30
|
+
* Creates a File object from the given input.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* makeFileForFetch({ content: pdfBuffer, fileName: 'doc.pdf', mimeType: 'application/pdf' })
|
|
29
34
|
*/
|
|
30
|
-
export
|
|
35
|
+
export declare function makeFileForFetch(input: MakeFileForFetchInput): File;
|
|
36
|
+
/**
|
|
37
|
+
* Input for fetchFileFromUrl().
|
|
38
|
+
*/
|
|
39
|
+
export interface FetchFileFromUrlInput {
|
|
31
40
|
/**
|
|
32
|
-
*
|
|
41
|
+
* URL to download the file from.
|
|
33
42
|
*/
|
|
34
|
-
readonly
|
|
43
|
+
readonly url: string;
|
|
35
44
|
/**
|
|
36
|
-
*
|
|
45
|
+
* File name to use for the resulting File object.
|
|
37
46
|
*
|
|
38
|
-
*
|
|
47
|
+
* If not provided, attempts to derive the name from the URL path or falls back to 'file'.
|
|
39
48
|
*/
|
|
40
|
-
readonly
|
|
49
|
+
readonly fileName?: Maybe<string>;
|
|
50
|
+
/**
|
|
51
|
+
* MIME type override. If not provided, uses the Content-Type from the response.
|
|
52
|
+
*/
|
|
53
|
+
readonly mimeType?: Maybe<ContentTypeMimeType>;
|
|
54
|
+
/**
|
|
55
|
+
* Fetch function to use. Defaults to the global fetch.
|
|
56
|
+
*/
|
|
57
|
+
readonly fetch?: Maybe<typeof fetch>;
|
|
41
58
|
}
|
|
42
|
-
|
|
59
|
+
/**
|
|
60
|
+
* Downloads a file from the given URL and returns it as a File object.
|
|
61
|
+
*
|
|
62
|
+
* When safe is true, returns undefined instead of throwing on fetch failure.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* const file = await fetchFileFromUrl({ url: 'https://example.com/doc.pdf' })
|
|
66
|
+
* formData.append('file', file)
|
|
67
|
+
*/
|
|
68
|
+
export declare function fetchFileFromUrl(input: FetchFileFromUrlInput): Promise<File>;
|
|
69
|
+
export declare function fetchFileFromUrl(input: FetchFileFromUrlInput, safe: true): Promise<Maybe<File>>;
|
|
43
70
|
export type FetchFileResponseContentType = ReturnType<typeof parseContentType>;
|
|
44
71
|
export interface FetchFileResponse {
|
|
45
72
|
/**
|
|
@@ -66,3 +93,23 @@ export interface FetchFileResponse {
|
|
|
66
93
|
* @returns
|
|
67
94
|
*/
|
|
68
95
|
export declare function parseFetchFileResponse(response: Response): FetchFileResponse;
|
|
96
|
+
/**
|
|
97
|
+
* @deprecated Use makeFileForFetch() with FormData instead.
|
|
98
|
+
*/
|
|
99
|
+
export interface FetchUploadFile {
|
|
100
|
+
readonly url: string;
|
|
101
|
+
readonly fetch?: typeof fetch;
|
|
102
|
+
readonly method?: FetchMethod;
|
|
103
|
+
readonly body: FetchUploadFileBody;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* @deprecated Use makeFileForFetch() with FormData instead.
|
|
107
|
+
*/
|
|
108
|
+
export interface FetchUploadFileBody {
|
|
109
|
+
readonly mimeType: ContentTypeMimeType;
|
|
110
|
+
readonly body: BodyInit;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* @deprecated Use makeFileForFetch() with FormData and context.fetch() instead.
|
|
114
|
+
*/
|
|
115
|
+
export declare function fetchUploadFile(input: FetchUploadFile): Promise<Response>;
|