@putkoff/abstract-utilities 0.1.101 → 0.1.103

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.
@@ -7,8 +7,9 @@
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
- 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>;
13
+ export declare function secureFetchIt(endpoint: string, body?: unknown, method?: string, headers?: Record<string, string> | null, blob?: boolean, noApi?: boolean, withCredentials?: boolean, returnJson?: boolean, returnReult?: boolean): Promise<Blob>;
13
14
  export declare function requestPatch(url: string, body?: unknown): Promise<Response>;
14
15
  export declare function fetchSharePatch(file: unknown): Promise<void>;
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);
@@ -1130,17 +1149,13 @@ function fetchIt(endpoint_1) {
1130
1149
  });
1131
1150
  }
1132
1151
  function secureFetchIt(endpoint_1) {
1133
- return __awaiter(this, arguments, void 0, function* (endpoint, body = null, method = null, customHeaders = null, blob = false, noApi = false, withCredentials = true, returnJson = true) {
1152
+ return __awaiter(this, arguments, void 0, function* (endpoint, body = null, method = null, customHeaders = null, blob = false, noApi = false, withCredentials = true, returnJson = true, returnReult = true) {
1134
1153
  // strip leading slashes off the endpoint
1135
1154
  method = method || "GET";
1136
- const cleanEndpoint = endpoint.replace(/^\/+/, "");
1137
- // decide whether to build off your API_BASE_URL or not
1138
- const rawBase = getAppConfig(cleanEndpoint);
1139
- const baseSlices = noApi
1140
- ? []
1141
- : rawBase.split("/").filter(Boolean);
1142
- // build final URL
1143
- const url = ensureAbstractUrl(cleanEndpoint, baseSlices);
1155
+ let url = endpoint;
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 = {
@@ -1161,6 +1176,8 @@ function secureFetchIt(endpoint_1) {
1161
1176
  }
1162
1177
  if (blob)
1163
1178
  return res.blob();
1179
+ if (returnReult)
1180
+ return getResult(res.json());
1164
1181
  if (returnJson)
1165
1182
  return res.json();
1166
1183
  return res;
@@ -1179,8 +1196,8 @@ function fetchSharePatch(file) {
1179
1196
  const cleanEndpoint = '/files/share';
1180
1197
  const baseUrl = getAppConfig(cleanEndpoint);
1181
1198
  const baseSlices = baseUrl.split("/").filter(Boolean);
1182
- // build final URL
1183
1199
  const url = ensureAbstractUrl(cleanEndpoint, baseSlices);
1200
+ // build final URL
1184
1201
  const token = localStorage.getItem('token');
1185
1202
  const body = JSON.stringify(file);
1186
1203
  const method = 'PATCH';
@@ -1373,7 +1390,7 @@ function uploadFiles(file_1) {
1373
1390
  formData.append('file', file);
1374
1391
  formData.append('subdir', subdir.trim());
1375
1392
  try {
1376
- const json = yield secureFetchIt('upload', formData, 'POST', null, false, true);
1393
+ const json = yield secureFetchIt('upload', formData, 'POST', null, false, false);
1377
1394
  if (!json || typeof json !== 'object') {
1378
1395
  throw new Error('Invalid response from server.');
1379
1396
  }
@@ -1747,6 +1764,7 @@ exports.getSrcDir = getSrcDir;
1747
1764
  exports.getSubstring = getSubstring;
1748
1765
  exports.getToggleShare = getToggleShare;
1749
1766
  exports.getToken = getToken;
1767
+ exports.get_app_config_url = get_app_config_url;
1750
1768
  exports.get_basename = get_basename;
1751
1769
  exports.get_dirname = get_dirname;
1752
1770
  exports.get_extname = get_extname;