Skip to content

Commit 7e19fb4

Browse files
committed
Added SharePoint services
1 parent b2417f2 commit 7e19fb4

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

src/services/ISPService.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { ISPLists } from "../common/spEntities";
2+
3+
export enum LibsOrderBy {
4+
Id = 1,
5+
Title
6+
}
7+
/**
8+
* Options used to sort and filter
9+
*/
10+
export interface ILibsOptions {
11+
orderBy?: LibsOrderBy;
12+
baseTemplate?: number;
13+
includeHidden?: boolean;
14+
}
15+
export interface ISPService {
16+
/**
17+
* Get the lists from SharePoint
18+
* @param options Options used to order and filter during the API query
19+
*/
20+
getLibs(options?: ILibsOptions): Promise<ISPLists>;
21+
}

src/services/SPService.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { ISPService, ILibsOptions, LibsOrderBy } from "./ISPService";
2+
import { ISPLists } from "../common/spEntities";
3+
import { IWebPartContext } from "@microsoft/sp-webpart-base";
4+
import { SPHttpClient, SPHttpClientResponse } from "@microsoft/sp-http";
5+
6+
export class SPService implements ISPService {
7+
private readonly _context: IWebPartContext;
8+
constructor(context: IWebPartContext) {
9+
this._context = context;
10+
}
11+
public getLibs(options?: ILibsOptions): Promise<ISPLists> {
12+
let filtered: boolean;
13+
let queryUrl: string = `${this._context.pageContext.web.absoluteUrl}/_api/web/lists?$select=Title,id,BaseTemplate`;
14+
if (options.orderBy !== null) {
15+
queryUrl += `&$orderby=${options.orderBy === LibsOrderBy.Id ? 'Id': 'Title'}`;
16+
}
17+
if (options.baseTemplate !== null) {
18+
queryUrl += `&$filter=BaseTemplate eq ${options.baseTemplate}`;
19+
filtered = true;
20+
}
21+
if (options.includeHidden === false) {
22+
queryUrl += filtered ? ' and Hidden eq false' : '&$filter=Hidden eq false';
23+
filtered = true;
24+
}
25+
return this._context.spHttpClient.get(queryUrl, SPHttpClient.configurations.v1)
26+
.then((response: SPHttpClientResponse) => {
27+
return response.json();
28+
}) as Promise<ISPLists>;
29+
}
30+
}
31+
32+
export default SPService;

src/services/SPServiceFactory.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { IWebPartContext } from "@microsoft/sp-webpart-base";
2+
import { ISPService } from "./ISPService";
3+
import { Environment, EnvironmentType } from "@microsoft/sp-core-library";
4+
import SPServiceMock from "./SPServiceMock";
5+
import SPService from "./SPService";
6+
7+
export class SPServiceFactory {
8+
public static createService(context: IWebPartContext, includeDelay?: boolean, delayTimeout?: number): ISPService {
9+
if (Environment.type === EnvironmentType.Local) {
10+
return new SPServiceMock(includeDelay, delayTimeout);
11+
}
12+
return new SPService(context);
13+
}
14+
}

src/services/SPServiceMock.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { ISPService, ILibsOptions } from "./ISPService";
2+
import { ISPLists } from "../common/spEntities";
3+
4+
export class SPServiceMock implements ISPService {
5+
private _includeDelay?: boolean;
6+
private _delayTimeout?: number;
7+
8+
constructor(includeDelay?: boolean, delayTimeout?: number) {
9+
this._includeDelay = includeDelay;
10+
this._delayTimeout = delayTimeout || 500;
11+
}
12+
13+
/**
14+
* The mock lists to present to the local workbench
15+
*/
16+
private static _lists: ISPLists = {
17+
value: [
18+
{ Id: '8dc80f2e-0e01-43ee-b59e-fbbca2d1f35e', Title: 'Mock List One', BaseTemplate: '109' },
19+
{ Id: '772a30d4-2d62-42da-aa48-c2a37971d693', Title: 'Mock List Two', BaseTemplate: '109' },
20+
{ Id: '16c0d1c6-b467-4823-a37b-c308cf730366', Title: 'Mock List Three', BaseTemplate: '109' }
21+
]
22+
};
23+
public getLibs(options?: ILibsOptions): Promise<ISPLists> {
24+
return new Promise<ISPLists>(async resolve => {
25+
if (this._includeDelay === true) {
26+
await this.sleep(this._delayTimeout); // Simulate network load
27+
}
28+
resolve(SPServiceMock._lists);
29+
});
30+
}
31+
/**
32+
* Locks the thread for the specified amount of time
33+
* @param ms Milliseconds to wait
34+
*/
35+
private sleep(ms: number): Promise<void> {
36+
return new Promise(resolve => setTimeout(resolve, ms));
37+
}
38+
}
39+
40+
export default SPServiceMock;

0 commit comments

Comments
 (0)