@mikezimm/fps-library-v2
Version:
Library of reusable typescript/javascript functions, interfaces and constants
93 lines (91 loc) • 5.53 kB
JavaScript
/**
* CodeAnalizerComment: Updated 2 imports on 2024-09-22 14:49:52
* Update:: import { IFpsErrorObject } to '@mikezimm/fps-core-v7/lib/types/fps-returns/common/IFpsErrorObject;'
* Update:: import { IBlankErrorObj } to '@mikezimm/fps-core-v7/lib/types/fps-returns/common/IFpsErrorObject;'
*/
/**
* CodeAnalizerComment: Updated 8 imports on 2024-09-21 23:07:24
* Update:: import { IFpsErrorObject } to '@mikezimm/fps-core-v7/lib/types/fps-returns/common/IFpsErrorObject;'
* Update:: import { IFPSResultStatus } to '@mikezimm/fps-core-v7/lib/types/fps-returns/common/IFPSResultStatus;'
* Update:: import { startPerformOpV2 } to '@mikezimm/fps-core-v7/lib/components/molecules/Performance/functions;'
* Update:: import { updatePerformanceEndV2 } to '@mikezimm/fps-core-v7/lib/components/molecules/Performance/functions;'
* Update:: import { IPerformanceSettings } to '@mikezimm/fps-core-v7/lib/components/molecules/Performance/IPerformanceSettings;'
* Update:: import { check4This } to '@mikezimm/fps-core-v7/lib/logic/Links/CheckSearch;'
* Update:: import { IBlankErrorObj } to '@mikezimm/fps-core-v7/lib/types/fps-returns/common/IFpsErrorObject;'
* Update:: import { IPerformanceOp } to '@mikezimm/fps-core-v7/lib/components/molecules/Performance/IPerformance;'
*/
import { HttpClient } from '@microsoft/sp-http'; // , httpClient: HttpClient
import { startPerformOpV2, updatePerformanceEndV2 } from "@mikezimm/fps-core-v7/lib/components/molecules/Performance/functions";
import { check4This, Check4 } from '@mikezimm/fps-core-v7/lib/logic/Links/CheckSearch';
export async function fetchAPI(apiEndPoint, httpClient, description, performanceSettings, headers) {
// Validate approved location
// const websLocation = this.approvedWebs.filter(loc => loc.key == web)[0];
// const websQuery = window.location.origin + websLocation.siteRelativeURL + "/_api/Web/Lists?$filter=BaseTemplate eq 101 and Hidden eq false&select=Title";
const fetchOp = performanceSettings ? startPerformOpV2(performanceSettings) : null;
try {
// const response = await httpClient.get(apiEndPoint, HttpClient.configurations.v1, headers ? headers : undefined );
const response = await httpClient.get(apiEndPoint, HttpClient.configurations.v1, headers);
if (check4This(Check4.fpsHttpResponse_Eq_true) === true) {
console.log(`${Check4.fpsHttpResponse_Eq_true} API Response data_1 ~ 52: ${description} response`, response);
}
const result = await createFPSHttpResponse(description, response);
result.unifiedPerformanceOps.fetch = performanceSettings ?
updatePerformanceEndV2({ op: fetchOp, updateMiliseconds: performanceSettings.updateMiliseconds, count: result.value ? result.value.length : -1 }) : null;
result.fetchOp = result.unifiedPerformanceOps.fetch;
return result;
}
catch (e) {
const fetchX = performanceSettings ?
updatePerformanceEndV2({ op: fetchOp, updateMiliseconds: performanceSettings.updateMiliseconds, count: -1 }) : null;
const result = {
value: null, e: e, status: 'Error', statusText: e.message,
items: [],
item: null,
ok: false,
errorInfo: {
errObj: e,
friendly: e.message,
result: null,
returnMess: e.message,
},
errorInput: null,
fetchOp: fetchX,
unifiedPerformanceOps: { fetch: fetchX },
loaded: false,
};
return result;
}
}
export async function createFPSHttpResponse(description, response) {
const results = { value: null, e: null, status: 'Error', items: [], item: null, unifiedPerformanceOps: {}, ok: null };
results.statusNo = response ? response.status : null;
results.statusText = response ? response.statusText : null;
results.ok = response ? response.ok : false;
results.url = response ? response.url : null;
results.type = response ? response.type : null;
results.bodyUsed = response ? response.bodyUsed : null;
// Unable to assign as Record<string, string>
results.headers = response ? response.headers : null;
results.status = response ? response.status === 200 ? 'Success' : response.statusText : 'Error';
const data_1 = response ? await response.json() : null;
if (check4This(Check4.fpsHttpResponse_Eq_true) === true) {
console.log(`createFPSHttpResponse data_1 ~ 128: ${description} results`, results);
console.log(`createFPSHttpResponse data_1 ~ 129: ${description} data_1`, data_1);
console.log(`createFPSHttpResponse data_1 ~ 130: ${description} data_1.value`, data_1 ? data_1.value : undefined);
}
results.value = data_1 && data_1.value ? data_1.value : data_1 ? data_1 : [];
// Assume results are always an array
results.items = results.value;
// Added for https://github.com/mikezimm/Compliance/issues/62
results.errorInfo = {
errObj: null,
friendly: '',
result: null,
returnMess: '',
};
// if ( check4Gulp() === true ) console.log(`API JSON response ~ 108: ${description}`, response ? response: results );
if (check4This(Check4.fpsHttpResponse_Eq_true) === true)
console.log(`API JSON response ~ 147: ${description}`, response ? response : results);
return results;
}
//# sourceMappingURL=functions.js.map