This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathquerybuilder.ts
419 lines (335 loc) · 17.9 KB
/
querybuilder.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
namespace Kurve {
export class Scopes {
private static rootUrl = "https://graph.microsoft.com/";
static General = {
OpenId: "openid",
OfflineAccess: "offline_access",
}
static User = {
Read: Scopes.rootUrl + "User.Read",
ReadAll: Scopes.rootUrl + "User.Read.All",
ReadWrite: Scopes.rootUrl + "User.ReadWrite",
ReadWriteAll: Scopes.rootUrl + "User.ReadWrite.All",
ReadBasicAll: Scopes.rootUrl + "User.ReadBasic.All",
}
static Contacts = {
Read: Scopes.rootUrl + "Contacts.Read",
ReadWrite: Scopes.rootUrl + "Contacts.ReadWrite",
}
static Directory = {
ReadAll: Scopes.rootUrl + "Directory.Read.All",
ReadWriteAll: Scopes.rootUrl + "Directory.ReadWrite.All",
AccessAsUserAll: Scopes.rootUrl + "Directory.AccessAsUser.All",
}
static Group = {
ReadAll: Scopes.rootUrl + "Group.Read.All",
ReadWriteAll: Scopes.rootUrl + "Group.ReadWrite.All",
AccessAsUserAll: Scopes.rootUrl + "Directory.AccessAsUser.All"
}
static Mail = {
Read: Scopes.rootUrl + "Mail.Read",
ReadWrite: Scopes.rootUrl + "Mail.ReadWrite",
Send: Scopes.rootUrl + "Mail.Send",
}
static Calendars = {
Read: Scopes.rootUrl + "Calendars.Read",
ReadWrite: Scopes.rootUrl + "Calendars.ReadWrite",
}
static Files = {
Read: Scopes.rootUrl + "Files.Read",
ReadAll: Scopes.rootUrl + "Files.Read.All",
ReadWrite: Scopes.rootUrl + "Files.ReadWrite",
ReadWriteAppFolder: Scopes.rootUrl + "Files.ReadWrite.AppFolder",
ReadWriteSelected: Scopes.rootUrl + "Files.ReadWrite.Selected",
}
static Tasks = {
ReadWrite: Scopes.rootUrl + "Tasks.ReadWrite",
}
static People = {
Read: Scopes.rootUrl + "People.Read",
ReadWrite: Scopes.rootUrl + "People.ReadWrite",
}
static Notes = {
Create: Scopes.rootUrl + "Notes.Create",
ReadWriteCreatedByApp: Scopes.rootUrl + "Notes.ReadWrite.CreatedByApp",
Read: Scopes.rootUrl + "Notes.Read",
ReadAll: Scopes.rootUrl + "Notes.Read.All",
ReadWriteAll: Scopes.rootUrl + "Notes.ReadWrite.All",
}
}
const queryUnion = (query1:string, query2:string) => (query1 ? query1 + (query2 ? "&" + query2 : "" ) : query2);
export class OData {
constructor(public query?:string) {
}
toString = () => this.query;
odata = (query:string) => {
this.query = queryUnion(this.query, query);
return this;
}
select = (...fields:string[]) => this.odata(`$select=${fields.join(",")}`);
expand = (...fields:string[]) => this.odata(`$expand=${fields.join(",")}`);
filter = (query:string) => this.odata(`$filter=${query}`);
orderby = (...fields:string[]) => this.odata(`$orderby=${fields.join(",")}`);
top = (items:Number) => this.odata(`$top=${items}`);
skip = (items:Number) => this.odata(`$skip=${items}`);
}
type ODataQuery = OData | string;
const pathWithQuery = (path:string, odataQuery?:ODataQuery) => {
let query = odataQuery && odataQuery.toString();
return path + (query ? "?" + query : "");
}
export type GraphObject<Model, N extends Node> = Model & {
_context: N
};
export interface Context<N extends Node> {
(id:string): N
}
export abstract class Node {
constructor(protected graph:Graph, protected path?:string) {
}
//Only adds scopes when linked to a v2 Oauth of kurve identity
protected scopesForV2 = (scopes: string[]) => this.graph.endpointVersion === EndpointVersion.v2 ? scopes : null;
pathWithQuery = (odataQuery?:ODataQuery, pathSuffix:string = "") => pathWithQuery(this.graph.root + this.path + pathSuffix, odataQuery);
protected graphObjectFromResponse = <Model, N extends Node>(response:any, node:N, context?:Context<N>) => {
const object = response as GraphObject<Model, N>;
object._context = context && object["id"] ? context(object["id"]) : node;
return object;
}
protected get<Model, N extends Node>(path:string, node:N, scopes?:string[], context?:Context<N>, responseType?:string): Promise<GraphObject<Model, N>, Error> {
console.log("GET", path, scopes);
const d = new Deferred<GraphObject<Model, N>, Error>();
this.graph.get(path, (error, result) => {
if (error) {
d.reject(error);
} else if (!responseType) {
const jsonResult = JSON.parse(result) ;
if (jsonResult.error) {
const errorODATA = new Error();
errorODATA.other = jsonResult.error;
d.reject(errorODATA);
return;
}
d.resolve(this.graphObjectFromResponse<Model, N>(jsonResult, node, context));
} else {
d.resolve(this.graphObjectFromResponse<Model, N>(result, node));
}
}, responseType, scopes);
return d.promise;
}
protected post<Model, N extends Node>(object:Model, path:string, node:N, scopes?:string[]): Promise<GraphObject<Model, N>, Error> {
console.log("POST", path, scopes);
const d = new Deferred<GraphObject<Model, N>, Error>();
/*
this.graph.post(object, path, (error, result) => {
const jsonResult = JSON.parse(result) ;
if (jsonResult.error) {
const errorODATA = new Error();
errorODATA.other = jsonResult.error;
d.reject(errorODATA);
return;
}
d.resolve(new Response<Model, N>({}, node));
});
*/
return d.promise;
}
}
export interface GraphCollection<Model, C extends CollectionNode, N extends Node> {
value: Array<GraphObject<Model, N>>,
_context: C,
_next?: () => Promise<GraphCollection<Model, C, N>, Error>
};
export abstract class CollectionNode extends Node {
protected graphCollectionFromResponse = <Model, C extends CollectionNode, N extends Node>(response:any, node:C, context?:Context<N>, scopes?:string[]) => {
const collection = response as GraphCollection<Model,C,N>;
collection._context = node;
const nextLink = response["@odata.nextLink"];
collection._next = nextLink ? () => this.getCollection<Model, C, N>(nextLink, node, context, scopes) : null;
if (context)
collection.value.forEach(item => item._context = item["id"] && context(item["id"]));
return collection;
}
protected getCollection<Model, C extends CollectionNode, N extends Node>(path:string, node:C, context:Context<N>, scopes?:string[]): Promise<GraphCollection<Model, C, N>, Error> {
console.log("GET collection", path, scopes);
const d = new Deferred<GraphCollection<Model, C, N>, Error>();
this.graph.get(path, (error, result) => {
if (error) {
d.reject(error);
} else {
const jsonResult = JSON.parse(result) ;
if (jsonResult.error) {
const errorODATA = new Error();
errorODATA.other = jsonResult.error;
d.reject(errorODATA);
return;
}
d.resolve(this.graphCollectionFromResponse<Model, C, N>(jsonResult, node, context, scopes));
}}, null, scopes);
return d.promise;
}
}
export class Attachment extends Node {
constructor(graph:Graph, path:string="", private context:string, attachmentId?:string) {
super(graph, path + (attachmentId ? "/" + attachmentId : ""));
}
static scopes = {
messages: [Scopes.Mail.Read],
events: [Scopes.Calendars.Read]
}
GetAttachment = (odataQuery?:ODataQuery) => this.get<AttachmentDataModel, Attachment>(this.pathWithQuery(odataQuery), this, this.scopesForV2(Attachment.scopes[this.context]));
/*
PATCH = this.graph.PATCH<AttachmentDataModel>(this.path, this.query);
DELETE = this.graph.DELETE<AttachmentDataModel>(this.path, this.query);
*/
}
export class Attachments extends CollectionNode {
constructor(graph:Graph, path:string="", private context:string) {
super(graph, path + "/attachments");
}
$ = (attachmentId:string) => new Attachment(this.graph, this.path, this.context, attachmentId);
GetAttachments = (odataQuery?:ODataQuery) => this.getCollection<AttachmentDataModel, Attachments, Attachment>(this.pathWithQuery(odataQuery), this, this.$, this.scopesForV2(Attachment.scopes[this.context]));
/*
POST = this.graph.POST<AttachmentDataModel>(this.path, this.query);
*/
}
export class Message extends Node {
constructor(graph:Graph, path:string="", messageId?:string) {
super(graph, path + (messageId ? "/" + messageId : ""));
}
get attachments() { return new Attachments(this.graph, this.path, "messages"); }
GetMessage = (odataQuery?:ODataQuery) => this.get<MessageDataModel, Message>(this.pathWithQuery(odataQuery), this, this.scopesForV2([Scopes.Mail.Read]));
SendMessage = (odataQuery?:ODataQuery) => this.post<MessageDataModel, Message>(null, this.pathWithQuery(odataQuery, "/microsoft.graph.sendMail"), this, this.scopesForV2([Scopes.Mail.Send]));
/*
PATCH = this.graph.PATCH<MessageDataModel>(this.path, this.query);
DELETE = this.graph.DELETE<MessageDataModel>(this.path, this.query);
*/
}
export class Messages extends CollectionNode {
constructor(graph:Graph, path:string="") {
super(graph, path + "/messages");
}
$ = (messageId:string) => new Message(this.graph, this.path, messageId);
GetMessages = (odataQuery?:ODataQuery) => this.getCollection<MessageDataModel, Messages, Message>(this.pathWithQuery(odataQuery), this, this.$, this.scopesForV2([Scopes.Mail.Read]));
CreateMessage = (object:MessageDataModel, odataQuery?:ODataQuery) => this.post<MessageDataModel, Messages>(object, this.pathWithQuery(odataQuery), this, this.scopesForV2([Scopes.Mail.ReadWrite]));
}
export class Event extends Node {
constructor(graph:Graph, path:string="", eventId:string) {
super(graph, path + (eventId ? "/" + eventId : ""));
}
get attachments() { return new Attachments(this.graph, this.path, "events"); }
GetEvent = (odataQuery?:ODataQuery) => this.get<EventDataModel, Event>(this.pathWithQuery(odataQuery), this, this.scopesForV2([Scopes.Calendars.Read]));
/*
PATCH = this.graph.PATCH<EventDataModel>(this.path, this.query);
DELETE = this.graph.DELETE<EventDataModel>(this.path, this.query);
*/
}
export class Events extends CollectionNode {
constructor(graph:Graph, path:string="") {
super(graph, path + "/events");
}
$ = (eventId:string) => new Event(this.graph, this.path, eventId);
GetEvents = (odataQuery?:ODataQuery) => this.getCollection<EventDataModel, Events, Event>(this.pathWithQuery(odataQuery), this, this.$, this.scopesForV2([Scopes.Calendars.Read]));
/*
POST = this.graph.POST<EventDataModel>(this.path, this.query);
*/
}
export class CalendarView extends CollectionNode {
constructor(graph:Graph, path:string="") {
super(graph, path + "/calendarView");
}
private $ = (eventId:string) => new Event(this.graph, this.path, eventId); // need to adjust this path
static dateRange = (startDate:Date, endDate:Date) => `startDateTime=${startDate.toISOString()}&endDateTime=${endDate.toISOString()}`
GetEvents = (odataQuery?:ODataQuery) => this.getCollection<EventDataModel, CalendarView, Event>(this.pathWithQuery(odataQuery), this, this.$, this.scopesForV2([Scopes.Calendars.Read]));
}
export class MailFolder extends Node {
constructor(graph:Graph, path:string="", mailFolderId:string) {
super(graph, path + (mailFolderId ? "/" + mailFolderId : ""));
}
GetMailFolder = (odataQuery?:ODataQuery) => this.get<MailFolderDataModel, MailFolder>(this.pathWithQuery(odataQuery), this, this.scopesForV2([Scopes.Mail.Read]));
}
export class MailFolders extends CollectionNode {
constructor(graph:Graph, path:string="") {
super(graph, path + "/mailFolders");
}
$ = (mailFolderId:string) => new MailFolder(this.graph, this.path, mailFolderId);
GetMailFolders = (odataQuery?:ODataQuery) => this.getCollection<MailFolderDataModel, MailFolders, MailFolder>(this.pathWithQuery(odataQuery), this, this.$, this.scopesForV2([Scopes.Mail.Read]));
}
export class Photo extends Node {
constructor(graph:Graph, path:string="", private context:string) {
super(graph, path + "/photo" );
}
static scopes = {
user: [Scopes.User.ReadBasicAll],
group: [Scopes.Group.ReadAll],
contact: [Scopes.Contacts.Read]
}
GetPhotoProperties = (odataQuery?:ODataQuery) => this.get<ProfilePhotoDataModel, Photo>(this.pathWithQuery(odataQuery), this, this.scopesForV2(Photo.scopes[this.context]));
GetPhotoImage = (odataQuery?:ODataQuery) => this.get<any, Photo>(this.pathWithQuery(odataQuery, "/$value"), this, this.scopesForV2(Photo.scopes[this.context]), null, "blob");
}
export class Manager extends Node {
constructor(graph:Graph, path:string="") {
super(graph, path + "/manager" );
}
GetUser = (odataQuery?:ODataQuery) => this.get<UserDataModel, User>(this.pathWithQuery(odataQuery), null, this.scopesForV2([Scopes.User.ReadAll]), this.graph.users.$);
}
export class MemberOf extends CollectionNode {
constructor(graph:Graph, path:string="") {
super(graph, path + "/memberOf");
}
GetGroups = (odataQuery?:ODataQuery) => this.getCollection<GroupDataModel, MemberOf, Group>(this.pathWithQuery(odataQuery), this, this.graph.groups.$, this.scopesForV2([Scopes.User.ReadAll]));
}
export class DirectReport extends Node {
constructor(protected graph:Graph, path:string="", userId?:string) {
super(graph, path + "/" + userId);
}
GetUser = (odataQuery?:ODataQuery) => this.get<UserDataModel, User>(this.pathWithQuery(odataQuery), null, this.scopesForV2([Scopes.User.Read]), this.graph.users.$);
}
export class DirectReports extends CollectionNode {
constructor(graph:Graph, path:string="") {
super(graph, path + "/directReports");
}
$ = (userId:string) => new DirectReport(this.graph, this.path, userId);
GetUsers = (odataQuery?:ODataQuery) => this.getCollection<UserDataModel, DirectReports, User>(this.pathWithQuery(odataQuery), this, this.graph.users.$, this.scopesForV2([Scopes.User.Read]));
}
export class User extends Node {
constructor(protected graph:Graph, path:string="", userId?:string) {
super(graph, userId ? path + "/" + userId : path + "/me");
}
get messages() { return new Messages(this.graph, this.path); }
get events() { return new Events(this.graph, this.path); }
get calendarView() { return new CalendarView(this.graph, this.path); }
get mailFolders() { return new MailFolders(this.graph, this.path) }
get photo() { return new Photo(this.graph, this.path, "user"); }
get manager() { return new Manager(this.graph, this.path); }
get directReports() { return new DirectReports(this.graph, this.path); }
get memberOf() { return new MemberOf(this.graph, this.path); }
GetUser = (odataQuery?:ODataQuery) => this.get<UserDataModel, User>(this.pathWithQuery(odataQuery), this, this.scopesForV2([Scopes.User.Read]));
/*
PATCH = this.graph.PATCH<UserDataModel>(this.path, this.query);
DELETE = this.graph.DELETE<UserDataModel>(this.path, this.query);
*/
}
export class Users extends CollectionNode {
constructor(graph:Graph, path:string="") {
super(graph, path + "/users");
}
$ = (userId:string) => new User(this.graph, this.path, userId);
GetUsers = (odataQuery?:ODataQuery) => this.getCollection<UserDataModel, Users, User>(this.pathWithQuery(odataQuery), this, this.$, this.scopesForV2([Scopes.User.Read]));
/*
CreateUser = this.graph.POST<UserDataModel>(this.path, this.query);
*/
}
export class Group extends Node {
constructor(protected graph:Graph, path:string="", groupId:string) {
super(graph, path + "/" + groupId);
}
GetGroup = (odataQuery?:ODataQuery) => this.get<GroupDataModel, Group>(this.pathWithQuery(odataQuery), this, this.scopesForV2([Scopes.Group.ReadAll]));
}
export class Groups extends CollectionNode {
constructor(graph:Graph, path:string="") {
super(graph, path + "/groups");
}
$ = (groupId:string) => new Group(this.graph, this.path, groupId);
GetGroups = (odataQuery?:ODataQuery) => this.getCollection<GroupDataModel, Groups, Group>(this.pathWithQuery(odataQuery), this, this.$, this.scopesForV2([Scopes.Group.ReadAll]));
}
}