@mikezimm/fps-library-v2
Version:
Library of reusable typescript/javascript functions, interfaces and constants
92 lines (91 loc) • 4.79 kB
JavaScript
import { SPHttpClient } from '@microsoft/sp-http';
import { CurrentSiteAbsolute } from '@mikezimm/fps-core-v7/lib/components/molecules/source-props/WindowLocationConstants';
import { PackageVersionLabelFpsLv2 } from '../../PackageVersion';
import { PackageVersionLabelFpsCv7 } from '@mikezimm/fps-core-v7/lib/PackageVersion';
import { PackageVersionLabelFpsSty } from '../../PackageVersionStyles';
/**
* RequestInit:
This is a general TypeScript interface for the native JavaScript fetch API. It is a standard part of the web API for making HTTP requests and is not specific to SharePoint.
It includes properties like method, headers, body, mode, credentials, etc.
It's typically used when working directly with the fetch function, and it is a very general-purpose object.
ISPHttpClientOptions:
This is SharePoint Framework (SPFx)-specific. It extends RequestInit with additional SPFx-specific properties.
It is specifically used in the SPHttpClient to make requests to SharePoint REST APIs.
For example, it includes properties like headers and method but also supports features specific to SPFx, such as headers that integrate with SharePoint-specific behaviors (like authorization).
*/
export default class FpsSpHttpService {
constructor(spHttpClient) {
this.PackageLibV2 = PackageVersionLabelFpsLv2;
this.PackageCoreV7 = PackageVersionLabelFpsCv7;
this.PackageStyles = PackageVersionLabelFpsSty;
this.spHttpClient = spHttpClient;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async fpsFetch(apiEndpoint, options) {
// Create a configuration object. You can modify this as needed (e.g., adding version, headers).
const credentials = apiEndpoint.indexOf(CurrentSiteAbsolute) === 0 ? 'same-origin' : 'include';
// Merge headers safely - improvement from HubConn search api testing
const customHeaders = new Headers(options.headers || {});
// console.log([...customHeaders.entries()]);
// Logs an array of header key-value pairs
// Convert RequestInit (standard fetch options) to ISPHttpClientOptions
const spHttpClientOptions = {
method: options.method || 'GET',
headers: customHeaders || {},
body: options.body,
credentials: credentials, // Set to 'same-origin' or 'include' based on cross-site need
};
// Make the actual fetch call
// Make the fetch request with API version v1
const response = await this.spHttpClient.fetch(apiEndpoint, SPHttpClient.configurations.v1, spHttpClientOptions);
return response;
// return response.json(); // Assuming you want to parse the JSON response
}
}
// async createListItem(siteKey: string, listTitle: string, itemData: any): Promise<any> {
// const apiUrl = `/sites/${siteKey}/_api/web/lists/getbytitle('${listTitle}')/items`;
// const options: RequestInit = {
// method: 'POST',
// headers: {
// 'Accept': 'application/json',
// 'Content-Type': 'application/json'
// },
// body: JSON.stringify(itemData)
// };
// return this.fetchWithToken(siteKey, apiUrl, options);
// }
// async updateListItem(siteKey: string, listTitle: string, itemId: number, itemData: any): Promise<any> {
// const apiUrl = `/sites/${siteKey}/_api/web/lists/getbytitle('${listTitle}')/items(${itemId})`;
// const options: RequestInit = {
// method: 'MERGE',
// headers: {
// 'Accept': 'application/json',
// 'Content-Type': 'application/json',
// 'IF-MATCH': '*' // You can provide the item’s ETag if needed
// },
// body: JSON.stringify(itemData)
// };
// return this.fetchWithToken(siteKey, apiUrl, options);
// }
// async deleteListItem(siteKey: string, listTitle: string, itemId: number): Promise<any> {
// const apiUrl = `/sites/${siteKey}/_api/web/lists/getbytitle('${listTitle}')/items(${itemId})`;
// const options: RequestInit = {
// method: 'DELETE',
// headers: {
// 'Accept': 'application/json'
// }
// };
// return this.fetchWithToken(siteKey, apiUrl, options);
// }
// async getListItems(siteKey: string, listTitle: string): Promise<any> {
// const apiUrl = `/sites/${siteKey}/_api/web/lists/getbytitle('${listTitle}')/items`;
// const options: RequestInit = {
// method: 'GET',
// headers: {
// 'Accept': 'application/json'
// }
// };
// return this.fetchWithToken(siteKey, apiUrl, options);
// }
// }
//# sourceMappingURL=FpsSpHttpService.js.map