@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
112 lines • 6.27 kB
JavaScript
/**
* 2024-09-15: Migrated to SAME FOLDER in fps-library-v2\src\components\molecules\SpHttp...
* export { IJSFetchReturn, createEmptyFetchReturn, doSpJsFetch }
*/
// import { check4This } from "@mikezimm/fps-core-v7/lib/logic/Links/CheckSearch";
// import { checkDeepProperty } from "@mikezimm/fps-core-v7/lib/logic/Objects/deep";
// import { getFullUrlFromSlashSitesUrl } from "@mikezimm/fps-core-v7/lib/logic/Strings/getFullUrlFromSlashSitesUrl";
// import { getSiteCollectionUrlFromLink } from "@mikezimm/fps-core-v7/lib/logic/Strings/getSiteCollectionUrlFromLink";
// import { check4ThisFPSDigestValue } from "@mikezimm/fps-core-v7/lib/components/molecules/SpHttp/helpers/check4ThisFPSDigestValue";
// import { IJSFetchReturn, createEmptyFetchReturn } from "./IJSFetchReturn";
// import { addCatchResponseError, addUnknownFetchError } from "./SpFetchCommon";
import { check4This, Check4 } from "../../../../logic/Links/CheckSearch";
import { checkDeepProperty } from "../../../../logic/Objects/deep";
import { makeAbsoluteUrl } from "../../../../logic/Strings/getSiteCollectionUrlFromLink";
import { CurrentHostName } from "../../source-props/WindowLocationConstants";
import { createEmptyFetchReturn } from "../interfaces/IJSFetchReturn";
import { addCatchResponseError, addUnknownFetchError } from "../helpers/SpFetchCommon";
/**
* 2024-12-10: This does NOT have 429 retry yet.
* See doSpJsFetch for an example of how to handle that
*
* @param fetchAPI
* @param digestValue - REQUIRES digestValue
* @param headerContentType
* @param blob
* @returns
*/
export async function doSpHttpFileFetch(fetchAPI, fpsSpService, headerContentType, blob) {
const results = await doSpHttpFileFetchOrPost(fetchAPI, 'GET', fpsSpService, headerContentType, blob);
return results;
}
/**
* Pass in any SharePoint rest api url and it should return a result or a standard error return object
* @param fetchAPI
* @param digestValue - REQUIRES digestValue for POST operations
* @returns
*/
export async function doSpHttpFileFetchOrPost(fetchAPI, method = `GET`, fpsSpService, headerContentType, blob) {
// Automatically added this because API's usually need full url.
if (fetchAPI.indexOf(CurrentHostName) === 0)
fetchAPI = `https://${fetchAPI}`;
// Always make sure this is an absolute url by running through this first. Note, if the previous line executes, it should be good then.
fetchAPI = makeAbsoluteUrl(fetchAPI);
let results = createEmptyFetchReturn(fetchAPI, method);
results.fpsContentType = ['file', 'item'];
// Read the Blob as an ArrayBuffer using FileReader with async/await
const buffer = !blob ? null : await new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.onloadend = () => resolve(fileReader.result);
fileReader.onerror = reject;
fileReader.readAsArrayBuffer(blob);
});
const headers = {
// 2024-12-26: Testing indicated these headers just gave errors. Tried with JSON file @ 32MB ok
// Accept: "application/json;odata=verbose",
// "Content-Type": headerContentType ? headerContentType : 'application/json;odata=verbose', // NOT in the original uploadImageToLibrary
};
// 2024-12-26: Testing indicated these headers just gave errors. Tried with JSON file @ 32MB ok
// if ( buffer ) headers['Content-Length'] = buffer.byteLength.toString() // Set content length for the upload
try {
const response = await fpsSpService.fpsFetch(fetchAPI, {
method: method,
headers: headers,
body: buffer,
});
// check if the response is OK
if (response.ok) {
const data = await response.json();
/**
* https://github.com/fps-solutions/FPS-Photo-Form/issues/117
* 2025-03-15: Updated this logic based upon latest fetchAPI return results:
* || (logical OR) → Uses the right-hand side when the left-hand side is falsy (0, "", false, null, undefined).
* / ?? (nullish coalescing) → Uses the right-hand side only when the left-hand side is null or undefined.
*
* Update, even with es6 lib, webpack pucked so I had to use this syntax instead
*/
const deepPropValue = data.ServerRelativeUrl !== null && data.ServerRelativeUrl !== undefined
? data.ServerRelativeUrl
: checkDeepProperty(data, ['d', 'ServerRelativeUrl'], "Actual");
if (fetchAPI.indexOf('GetFolderByServerRelativeUrl') > 0) {
// added logic to solve this: https://github.com/mikezimm/pivottiles7/issues/292
if (deepPropValue !== undefined && deepPropValue !== null) {
results.itemUrl = deepPropValue;
results.ok = true;
results.status = 'Success';
}
}
else {
results.itemUrl = `Unknown Url`;
results.ok = false;
results.status = 'Error';
}
results.statusText = response.statusText;
results.statusNo = response.status;
if (check4This(Check4.fpsShowFetchResults_Eq_true) === true)
console.log(`fps-core-v7 Success: doSpHttpFileFetchOrPost ~ 86 results`, results);
}
else {
results = await addCatchResponseError(results, response, fpsSpService);
}
return results;
}
catch (e) {
results = addUnknownFetchError(results, e);
// 2024-12-09: Copied this over from updateCommandItems so it will always capture the errors somewhere
// Commented out saveErrorToLog here because there is no results.errorInfo, so it will error out triggering an endless loop.
// Also to note, saveErrorToLog is already being done later during the checkAnyItems part so it's not really needed here.
// saveErrorToLogWDigest( results.errorInfo as IHelpfullOutput, results.errorInput as IHelpfullInput );
return results;
}
}
//# sourceMappingURL=doSpHttpFileFetch.js.map