-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
141 lines (133 loc) · 4.89 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { GraphError } from '@microsoft/microsoft-graph-client';
import { MSGraphClient } from '@microsoft/sp-http';
import { Logger, LogLevel, ConsoleListener } from '@pnp/logging';
export default class MSGraphHelper {
private static _graphClient: MSGraphClient;
public static async Init(msGraphClientFactory) {
this._graphClient = await msGraphClientFactory.getClient();
Logger.subscribe(new ConsoleListener());
Logger.activeLogLevel = LogLevel.Info;
}
/**
* Get
*
* @param {string} apiUrl API url
* @param {string} version Version (default to v1.0)
* @param {Array<string>} selectProperties Select properties
* @param {string} filter Filter
* @param {number} top Number of items to retrieve
* @param {Array<string>} expand Expand
*/
public static async Get(apiUrl: string, version: string = "v1.0", selectProperties?: Array<string>, filter?: string, top?: number, expand?: Array<string>): Promise<any> {
try {
let values = [];
let query = this._graphClient.api(apiUrl).version(version);
if (selectProperties && selectProperties.length > 0) {
query = query.select(selectProperties);
}
if (filter && filter.length > 0) {
query = query.filter(filter);
}
if (top) {
query = query.top(top);
}
if (expand) {
query = query.expand(expand);
}
Logger.log({ message: `(MSGraphHelper) Get`, data: { urlComponents: query.urlComponents }, level: LogLevel.Info });
let response = await query.get();
if (response.value && response.value.length > 0) {
values.push(...response.value);
}
let nextLink = response["@odata.nextLink"];
if (nextLink) {
while (true) {
try {
query.parsePath(nextLink);
response = await query.get();
nextLink = response["@odata.nextLink"];
if (response.value && response.value.length > 0) {
values.push(...response.value);
}
if (!nextLink) {
break;
}
} catch (error) {
throw error;
}
}
}
return values;
} catch (error) {
throw error;
}
}
/**
* Patch
*
* @param {string} apiUrl API url
* @param {string} version Version (default to v1.0)
* @param {any} content Content
*/
public static async Patch(apiUrl: string, version: string = "v1.0", content: any): Promise<any> {
var p = new Promise<string>(async (resolve, reject) => {
if (typeof (content) === "object") {
content = JSON.stringify(content);
}
let query = this._graphClient.api(apiUrl).version(version);
let callback = (error: GraphError, _response: any, rawResponse?: any) => {
if (error) {
reject(error);
} else {
resolve();
}
};
await query.update(content, callback);
});
return p;
}
/**
* Post
*
* @param {string} apiUrl API url
* @param {string} version Version (default to v1.0)
* @param {any} content Content
*/
public static async Post(apiUrl: string, version: string = "v1.0", content: any): Promise<any> {
var p = new Promise<string>(async (resolve, reject) => {
if (typeof (content) === "object") {
content = JSON.stringify(content);
}
let query = this._graphClient.api(apiUrl).version(version);
let callback = (error: GraphError, response: any, rawResponse?: any) => {
if (error) {
reject(error);
} else {
resolve(response);
}
};
await query.post(content, callback);
});
return p;
}
/**
* Delete
*
* @param {string} apiUrl API url
* @param {string} version Version (default to v1.0)
*/
public static async Delete(apiUrl: string, version: string = "v1.0"): Promise<any> {
var p = new Promise<string>(async (resolve, reject) => {
let query = this._graphClient.api(apiUrl).version(version);
let callback = (error: GraphError, response: any, rawResponse?: any) => {
if (error) {
reject(error);
} else {
resolve(response);
}
};
await query.delete(callback);
});
return p;
}
}