@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
119 lines • 4.74 kB
JavaScript
/**
https://learn.microsoft.com/en-us/sharepoint/dev/declarative-customization/site-theming/sharepoint-site-theming-rest-api
http://<site url>/_api/thememanager/AddTenantTheme
http://<site url>/_api/thememanager/DeleteTenantTheme
http://<site url>/_api/thememanager/GetTenantThemingOptions
http://<site url>/_api/thememanager/ApplyTheme
http://<site url>/_api/thememanager/UpdateTenantTheme
*/
import { check4This, Check4 } from "../../../../logic/Links/CheckSearch";
/**
*
* Derived from PalettePal:
* src\webparts\palettePal\components\functions\AddTenantThemes
* src\webparts\palettePal\components\functions\GetTenantThemes
*
* "https://mysite-admin.sharepoint.com/"
* const formDigestValue = this.context.pageContext.web.formDigestValue;
* GetTenantThemingOptions("/_api/thememanager/GetTenantThemingOptions", this.);
*
*
* @param url
* @param formDigestValue
* @param params
* @returns
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function GetOrPostTenantThemes(webUrl, url, formDigestValue, params, getOrPost) {
const req = new XMLHttpRequest();
req.onreadystatechange = function () {
// eslint-disable-next-line eqeqeq
if (req.readyState != 4) // Loaded
if (check4This(Check4.fpsShowFetchResults_Eq_true) === true)
console.log(`${getOrPost} TenantThemingOptions: req1`, req.responseText);
return;
};
// Prepend web URL to url and remove duplicated slashes.
const webBasedUrl = (webUrl + '//' + url).replace(/\/{2,}/, '/');
req.open(getOrPost, webBasedUrl, true);
req.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
req.setRequestHeader('ACCEPT', 'application/json; odata.metadata=minimal');
req.setRequestHeader('x-requestdigest', formDigestValue);
req.setRequestHeader('ODATA-VERSION', '4.0');
// eslint-disable-next-line no-void
req.send(params ? JSON.stringify(params) : void 0);
if (check4This(Check4.fpsShowFetchResults_Eq_true) === true) {
console.log(`${getOrPost} TenantThemingOptions: req2`, req);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const results = req.response ? JSON.parse(req.response) : {};
console.log(`${getOrPost} TenantThemingOptions: results`, results);
}
return req;
}
// import { ServiceKey, ServiceScope } from "@microsoft/sp-core-library";
// import { PageContext } from "@microsoft/sp-page-context";
// import {
// SPHttpClient,
// ISPHttpClientOptions,
// SPHttpClientResponse,
// } from "@microsoft/sp-http";
// export interface ISPThemeService {
// getCustomThemes: () => Promise<ITheme[]>;
// }
// export interface ITheme {
// name: string;
// themeJSON: string;
// }
// export interface ISPTheme {
// hideDefaultThemes: boolean;
// themePreviews: ITheme[];
// }
// export class SPThemeService {
// public static readonly serviceKey: ServiceKey<ISPThemeService> =
// ServiceKey.create("aaas:SPThemeService", SPThemeService);
// private _pageContext: PageContext;
// private _spHttpClient: SPHttpClient;
// constructor(serviceScope: ServiceScope) {
// serviceScope.whenFinished(() => {
// this._pageContext = serviceScope.consume(PageContext.serviceKey);
// this._spHttpClient = serviceScope.consume(SPHttpClient.serviceKey);
// });
// }
// public async getCustomThemes(): Promise<ITheme[]> {
// const webUrl = this._pageContext.web.absoluteUrl;
// const url = `${webUrl}/_api/ThemeManager/GetTenantThemingOptions`;
// const themes = await this.sendGetRequest<ISPTheme>(url);
// if (themes) {
// return themes.themePreviews;
// }
// return [];
// }
// private sendGetRequest<T>(
// url: string,
// options?: ISPHttpClientOptions,
// cacheKey?: string
// ): Promise<T> {
// if (!options) {
// options = {
// headers: { "content-type": "application/json;odata=nometadata" },
// };
// }
// return new Promise<T>((resolve, reject) => {
// this._spHttpClient
// .get(url, SPHttpClient.configurations.v1, options)
// .then(async (response: SPHttpClientResponse) => {
// if (response.ok) {
// const json = await response.json();
// resolve(json);
// } else {
// const json = await response.json();
// reject(json.error);
// }
// })
// .catch((error: Error) => {
// reject(error);
// });
// });
// }
// }
//# sourceMappingURL=GetOrPostTenantThemes.js.map