@putkoff/abstract-utilities 0.1.1 → 0.1.102
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/dist/cjs/functions/fetch_utils/src/fetchIt_utils.d.ts +1 -0
- package/dist/cjs/index.js +39 -16
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/functions/fetch_utils/src/fetchIt_utils.d.ts +1 -0
- package/dist/esm/index.js +39 -17
- package/dist/esm/index.js.map +1 -1
- package/dist/functions/fetch_utils/src/fetchIt_utils.d.ts +1 -0
- package/dist/index.d.ts +2 -1
- package/package.json +1 -1
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*
|
|
8
8
|
*/
|
|
9
9
|
/** Pulls base-URL from AppConfig.API_BASE_URL, strips trailing slashes */
|
|
10
|
+
export declare function get_app_config_url(endpoint: any): string;
|
|
10
11
|
export declare function fetchIt(endpoint: string, body?: unknown, method?: string | null, headers?: Record<string, string> | null, blob?: boolean | null, no_api?: boolean, requireAuth?: boolean): Promise<unknown>;
|
|
11
12
|
export declare function secureFetchIt<T>(endpoint: string, body?: unknown, method?: string, headers?: Record<string, string> | null, blob?: false, noApi?: boolean, requireAuth?: boolean): Promise<T>;
|
|
12
13
|
export declare function secureFetchIt(endpoint: string, body?: unknown, method?: string, headers?: Record<string, string> | null, blob?: boolean, noApi?: boolean, withCredentials?: boolean, returnJson?: boolean): Promise<Blob>;
|
package/dist/cjs/index.js
CHANGED
|
@@ -1009,6 +1009,18 @@ function ensureAbstractUrl(endpoint, slices = []) {
|
|
|
1009
1009
|
// 5) hand it back to make_path to join “target” + “strippedEndpoint”
|
|
1010
1010
|
return make_path(target, strippedEndpoint);
|
|
1011
1011
|
}
|
|
1012
|
+
/**
|
|
1013
|
+
* Unwraps nested { result } fields until you hit a non-object or no more "result" keys.
|
|
1014
|
+
*/
|
|
1015
|
+
function getResult(obj) {
|
|
1016
|
+
let current = obj;
|
|
1017
|
+
while (current &&
|
|
1018
|
+
typeof current === "object" &&
|
|
1019
|
+
Object.prototype.hasOwnProperty.call(current, "result")) {
|
|
1020
|
+
current = current.result;
|
|
1021
|
+
}
|
|
1022
|
+
return current;
|
|
1023
|
+
}
|
|
1012
1024
|
// Constructs API URL from endpoint
|
|
1013
1025
|
function api(endpoint) {
|
|
1014
1026
|
return ensureAbstractUrl(endpoint);
|
|
@@ -1121,6 +1133,13 @@ function getAppConfig(endpoint) {
|
|
|
1121
1133
|
/** Pulls base-URL from AppConfig.API_BASE_URL, strips trailing slashes */
|
|
1122
1134
|
// src/functions/fetch/secureFetchIt.ts
|
|
1123
1135
|
// --- fetch_utils/src/fetch_utils.ts ----------------------------
|
|
1136
|
+
function get_app_config_url(endpoint) {
|
|
1137
|
+
const cleanEndpoint = endpoint.replace(/^\/+/, "");
|
|
1138
|
+
const baseUrl = getAppConfig(cleanEndpoint);
|
|
1139
|
+
const baseSlices = baseUrl.split("/").filter(Boolean);
|
|
1140
|
+
const url = ensureAbstractUrl(cleanEndpoint, baseSlices);
|
|
1141
|
+
return url;
|
|
1142
|
+
}
|
|
1124
1143
|
function fetchIt(endpoint_1) {
|
|
1125
1144
|
return __awaiter(this, arguments, void 0, function* (endpoint, body = null, method = null, headers = null, blob = null, no_api = false, requireAuth = true) {
|
|
1126
1145
|
const vars = getFetchVars(headers, method, body);
|
|
@@ -1133,14 +1152,10 @@ function secureFetchIt(endpoint_1) {
|
|
|
1133
1152
|
return __awaiter(this, arguments, void 0, function* (endpoint, body = null, method = null, customHeaders = null, blob = false, noApi = false, withCredentials = true, returnJson = true) {
|
|
1134
1153
|
// strip leading slashes off the endpoint
|
|
1135
1154
|
method = method || "GET";
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
? []
|
|
1141
|
-
: rawBase.split("/").filter(Boolean);
|
|
1142
|
-
// build final URL
|
|
1143
|
-
const url = ensureAbstractUrl(cleanEndpoint, baseSlices);
|
|
1155
|
+
let url = endpoint.replace(/^\/+/, "");
|
|
1156
|
+
if (!noApi) {
|
|
1157
|
+
url = get_app_config_url(url);
|
|
1158
|
+
}
|
|
1144
1159
|
// headers: JSON by default, plus any auth + overrides
|
|
1145
1160
|
const headers = Object.assign(Object.assign(Object.assign({}, (body instanceof FormData ? {} : { "Content-Type": "application/json" })), getAuthorizationHeader()), customHeaders);
|
|
1146
1161
|
const opts = {
|
|
@@ -1162,7 +1177,7 @@ function secureFetchIt(endpoint_1) {
|
|
|
1162
1177
|
if (blob)
|
|
1163
1178
|
return res.blob();
|
|
1164
1179
|
if (returnJson)
|
|
1165
|
-
return res.json();
|
|
1180
|
+
return getResult(res.json());
|
|
1166
1181
|
return res;
|
|
1167
1182
|
});
|
|
1168
1183
|
}
|
|
@@ -1176,15 +1191,22 @@ function requestPatch(url_1) {
|
|
|
1176
1191
|
// Performs PATCH request for file sharing
|
|
1177
1192
|
function fetchSharePatch(file) {
|
|
1178
1193
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1179
|
-
const
|
|
1194
|
+
const cleanEndpoint = '/files/share';
|
|
1195
|
+
const baseUrl = getAppConfig(cleanEndpoint);
|
|
1196
|
+
const baseSlices = baseUrl.split("/").filter(Boolean);
|
|
1197
|
+
const url = ensureAbstractUrl(cleanEndpoint, baseSlices);
|
|
1198
|
+
// build final URL
|
|
1180
1199
|
const token = localStorage.getItem('token');
|
|
1200
|
+
const body = JSON.stringify(file);
|
|
1201
|
+
const method = 'PATCH';
|
|
1202
|
+
const headers = {
|
|
1203
|
+
'Content-Type': 'application/json',
|
|
1204
|
+
'Authorization': `Bearer ${token}`,
|
|
1205
|
+
};
|
|
1181
1206
|
const resp = yield fetch(url, {
|
|
1182
|
-
method
|
|
1183
|
-
headers
|
|
1184
|
-
|
|
1185
|
-
'Authorization': `Bearer ${token}`,
|
|
1186
|
-
},
|
|
1187
|
-
body: JSON.stringify(file),
|
|
1207
|
+
method,
|
|
1208
|
+
headers,
|
|
1209
|
+
body
|
|
1188
1210
|
});
|
|
1189
1211
|
if (!resp.ok) {
|
|
1190
1212
|
console.error('Error from server', yield resp.text());
|
|
@@ -1740,6 +1762,7 @@ exports.getSrcDir = getSrcDir;
|
|
|
1740
1762
|
exports.getSubstring = getSubstring;
|
|
1741
1763
|
exports.getToggleShare = getToggleShare;
|
|
1742
1764
|
exports.getToken = getToken;
|
|
1765
|
+
exports.get_app_config_url = get_app_config_url;
|
|
1743
1766
|
exports.get_basename = get_basename;
|
|
1744
1767
|
exports.get_dirname = get_dirname;
|
|
1745
1768
|
exports.get_extname = get_extname;
|