replace 'any'

This commit is contained in:
a1mery 2023-07-09 22:07:35 +02:00
parent 8665d4665a
commit 1d0187c06f
7 changed files with 12708 additions and 20686 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,62 +1,50 @@
import { MSGraphClientV3 } from "@microsoft/sp-http"; import { MSGraphClientV3 } from "@microsoft/sp-http";
import { SitePage } from "./types"; import { GraphSitePage, GraphSitePageCollection, GraphWebPartCollection } from "./types";
import { BaseComponentContext } from "@microsoft/sp-component-base";
export interface IGraphService { export interface IGraphService {
// eslint-disable-next-line @typescript-eslint/no-explicit-any GetWebParts(siteId: string, pageId: string): Promise<GraphWebPartCollection>;
GetWebParts(client: MSGraphClientV3, siteId: string, pageId: string): Promise<any>; GetSitePages(siteId: string): Promise<GraphSitePage[]>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
GetSitePages(client: MSGraphClientV3, siteId: string): Promise<any>;
} }
export class GraphService implements IGraphService {
private MSGraphClient: MSGraphClientV3;
private Context: BaseComponentContext;
constructor(Context: BaseComponentContext) {
class GraphService implements IGraphService { this.Context = Context;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public async GetWebParts(client: MSGraphClientV3, siteId: string, pageId: string): Promise<any> {
try{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const rawWebParts: any = await this.GET(client, "sites/" + siteId + "/pages/" + pageId + "/webparts","","");
return rawWebParts;
} catch (error){
return null;
}
} }
public async GetSitePages(client: MSGraphClientV3, siteId: string): Promise<SitePage[]> { private async Get_Client(): Promise<MSGraphClientV3> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any if (this.MSGraphClient === undefined)
const rawPages: any = await this.GET(client, "sites/" + siteId + "/pages", "", "id,title"); this.MSGraphClient = await this.Context.msGraphClientFactory.getClient("3");
// eslint-disable-next-line @typescript-eslint/no-explicit-any return this.MSGraphClient;
return rawPages.value.flatMap((rawPage: any) => ( }
[
public async GetWebParts(siteId: string, pageId: string): Promise<GraphWebPartCollection> {
try {
const client = await this.Get_Client();
const rawWebParts: GraphWebPartCollection = await client.api("sites/" + siteId + "/pages/" + pageId + "/webparts").version('beta').get();
return rawWebParts;
} catch (error) {
return null;
}
}
public async GetSitePages(siteId: string): Promise<GraphSitePage[]> {
const pages: GraphSitePage[] = [];
const client = await this.Get_Client();
const rawPages: GraphSitePageCollection = await client.api("sites/" + siteId + "/pages").select("id,title").version('beta').get();
rawPages.value.forEach(rawPage => {
pages.push(
{ {
id: rawPage.id, id: rawPage.id,
title: rawPage.title title: rawPage.title
} }
] )
));
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private GET(client: MSGraphClientV3, api: string, filter?: string, select?: string, top?: number, responseType?: any): Promise<any> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return new Promise<any>((resolve, reject) => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
client.api(api).version("beta").select(select).filter(filter).responseType(responseType)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.get((error: any, response: any) => {
if (error) {
reject(error);
return;
}
resolve(response);
});
}); });
return pages;
} }
} }
export const GraphServiceInstance = new GraphService();

View File

@ -1,35 +1,28 @@
import { SitePage, WebPart } from "./types"; import { GraphWebPartCollection, WebPart } from "./types";
import { GraphServiceInstance } from "./GraphService"; import { IGraphService } from "./GraphService";
import { MSGraphClientV3 } from "@microsoft/sp-http"; import {SitePage} from "@microsoft/microsoft-graph-types-beta"
export async function _getSiteWebParts(graphClient: MSGraphClientV3, siteId: string): Promise<WebPart[]> { export async function _getSiteWebParts(service: IGraphService, siteId: string): Promise<WebPart[]> {
try { try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any const siteWebParts: WebPart[] = [];
const siteWebParts: any = []; const sitePages: SitePage[] = await service.GetSitePages(siteId);
const sitePages: SitePage[] = await GraphServiceInstance.GetSitePages(graphClient, siteId); for (let i: number = 0; i < sitePages.length - 1; i++) {
for (let i: number = 0; i<sitePages.length-1; i++){ const graphWebParts: GraphWebPartCollection | null = await service.GetWebParts(siteId, sitePages[i].id);
// eslint-disable-next-line @typescript-eslint/no-explicit-any if (graphWebParts !== null) {
const r: any = await GraphServiceInstance.GetWebParts(graphClient, siteId, sitePages[i].id); graphWebParts.value.forEach(siteWebPart => {
if (r !== null){ siteWebParts.push(
siteWebParts.push(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
r.value.flatMap((siteWebPart: any) => ([
{ {
siteId: siteId, siteId: siteId,
pageTitle: sitePages[i].title, pageTitle: sitePages[i].title,
id: siteWebPart.id, id: siteWebPart.id,
title: siteWebPart.data.title, title: siteWebPart.innerHtml !== undefined ? "Text" : siteWebPart.data.title,
} }
])) )
); });
} }
} }
// eslint-disable-next-line @typescript-eslint/no-explicit-any return siteWebParts;
return siteWebParts.flatMap((t: any)=>t);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
return null; return null;

View File

@ -5,12 +5,38 @@ export type WebPart = {
title: string; title: string;
} }
export type GraphWebPart = {
data?: GraphWebPartData;
id: string;
webPartType: string;
innerHtml?: string;
}
export type GraphWebPartCollection = {
value: GraphWebPart[];
}
export type GraphWebPartData = {
audiences: string[];
dataVersion: string[];
description: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
properties: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
serverProcessedContent: any;
title: string;
}
export type AggredatedWebParts = { export type AggredatedWebParts = {
titles: string[]; titles: string[];
count: number[]; count: number[];
} }
export type SitePage = { export type GraphSitePageCollection = {
value: GraphSitePage[];
}
export type GraphSitePage = {
id: string; id: string;
title: string; title: string;
} }
@ -22,6 +48,6 @@ export type ChartDataCustom = {
export type DataSet = { export type DataSet = {
label: string; label: string;
data: number []; data: number[];
//backgroundColor: string[]; //backgroundColor: string[];
} }

View File

@ -11,7 +11,7 @@ import * as strings from 'WebPartReportWebPartStrings';
import WebPartReport from './components/WebPartReport'; import WebPartReport from './components/WebPartReport';
import { IWebPartReportProps } from './components/IWebPartReportProps'; import { IWebPartReportProps } from './components/IWebPartReportProps';
import { ITopActions, TopActionsFieldType } from '@microsoft/sp-top-actions'; import { ITopActions, TopActionsFieldType } from '@microsoft/sp-top-actions';
import { MSGraphClientV3 } from "@microsoft/sp-http"; import {GraphService} from "./../GraphService"
export interface IWebPartReportWebPartProps { export interface IWebPartReportWebPartProps {
description: string; description: string;
@ -20,7 +20,6 @@ export interface IWebPartReportWebPartProps {
} }
export default class WebPartReportWebPart extends BaseClientSideWebPart<IWebPartReportWebPartProps> { export default class WebPartReportWebPart extends BaseClientSideWebPart<IWebPartReportWebPartProps> {
private graphClient: MSGraphClientV3;
private _isDarkTheme: boolean = false; private _isDarkTheme: boolean = false;
public render(): void { public render(): void {
@ -33,24 +32,15 @@ export default class WebPartReportWebPart extends BaseClientSideWebPart<IWebPart
hasTeamsContext: !!this.context.sdks.microsoftTeams, hasTeamsContext: !!this.context.sdks.microsoftTeams,
userDisplayName: this.context.pageContext.user.displayName, userDisplayName: this.context.pageContext.user.displayName,
siteId: this.context.pageContext.site.id.toString(), siteId: this.context.pageContext.site.id.toString(),
graphClient: this.graphClient GraphService: new GraphService(this.context),
} }
); );
ReactDom.render(element, this.domElement); ReactDom.render(element, this.domElement);
} }
protected async onInit(): Promise<void> { // eslint-disable-next-line @typescript-eslint/no-empty-function
// eslint-disable-next-line @typescript-eslint/no-explicit-any protected async onInit(): Promise<void> {}
return new Promise<void>((resolve: () => void, reject: (error: any) => void): void => {
this.context.msGraphClientFactory
.getClient("3")
.then((client: MSGraphClientV3): void => {
this.graphClient = client;
resolve();
}, err => reject(err));
});
}
protected onThemeChanged(currentTheme: IReadonlyTheme | undefined): void { protected onThemeChanged(currentTheme: IReadonlyTheme | undefined): void {
if (!currentTheme) { if (!currentTheme) {

View File

@ -1,4 +1,4 @@
import { MSGraphClientV3 } from "@microsoft/sp-http"; import { IGraphService } from "../../GraphService";
export interface IWebPartReportProps { export interface IWebPartReportProps {
description: string; description: string;
@ -7,5 +7,6 @@ export interface IWebPartReportProps {
hasTeamsContext: boolean; hasTeamsContext: boolean;
userDisplayName: string; userDisplayName: string;
siteId: string; siteId: string;
graphClient: MSGraphClientV3; GraphService: IGraphService;
} }

View File

@ -90,7 +90,7 @@ export default class WebPartReport extends React.Component<IWebPartReportProps,
webPartsTitles = []; webPartsTitles = [];
aggregatedWebPartData.clear(); aggregatedWebPartData.clear();
siteWebParts = await _getSiteWebParts(this.props.graphClient, this.props.siteId.toString()); siteWebParts = await _getSiteWebParts(this.props.GraphService, this.props.siteId.toString());
siteWebParts.forEach(e => { siteWebParts.forEach(e => {
if (!aggregatedWebPartData.has(e.title)) { if (!aggregatedWebPartData.has(e.title)) {
aggregatedWebPartData.set(e.title, 1); aggregatedWebPartData.set(e.title, 1);