UNPKG

@mikezimm/fps-core-v7

Version:

Library of reusable core interfaces, types and constants migrated from fps-library-v2

103 lines 5.17 kB
/** * 2024-09-15: Migrated to SAME FOLDER in fps-library-v2\src\components\molecules\SpHttp... * export { IJSFetchReturn, createEmptyFetchReturn, doSpJsFetch } */ import { check4This, Check4 } from "../../../../logic/Links/CheckSearch"; import { checkDeepProperty } from "../../../../logic/Objects/deep"; import { makeAbsoluteUrl } from "../../../../logic/Strings/getSiteCollectionUrlFromLink"; import { getSiteCollectionUrlFromLink } from "../../../../logic/Strings/getSiteCollectionUrlFromLink"; import { CurrentHostName } from "../../source-props/WindowLocationConstants"; // import { CurrentHostName } from "../../../logic/Strings/getSiteCollectionUrlFromLink"; import { check4ThisFPSDigestValue } from "../helpers/check4ThisFPSDigestValue"; 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 doSpJsFileFetch(fetchAPI, digestValue, headerContentType, blob) { const results = await doSpJsFileFetchOrPost(fetchAPI, 'GET', digestValue, 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 doSpJsFileFetchOrPost(fetchAPI, method = `GET`, digestValue, 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); // Added to just check if digestValue exists and if so, use it here if (!digestValue) { digestValue = check4ThisFPSDigestValue(getSiteCollectionUrlFromLink(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 = { Accept: "application/json;odata=verbose", // "Content-Type": headerContentType ? headerContentType : 'application/json;odata=verbose', // NOT in the original uploadImageToLibrary }; if (digestValue) headers['X-RequestDigest'] = digestValue; if (buffer) headers['Content-Length'] = buffer.byteLength.toString(); // Set content length for the upload try { const response = await fetch(fetchAPI, { method: method, headers: headers, body: buffer, }); // check if the response is OK if (response.ok) { const data = await response.json(); const deepPropValue = 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: doSpJsFileFetchOrPost ~ 86 results`, results); } else { results = await addCatchResponseError(results, response, null); } 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=doSpJsFileFetch.js.map