-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphHelper.ts
129 lines (117 loc) · 3.63 KB
/
graphHelper.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import 'isomorphic-fetch';
import {
DeviceCodeCredential,
TokenCachePersistenceOptions,
DeviceCodePromptCallback,
useIdentityPlugin,
} from '@azure/identity';
import { cachePersistencePlugin } from '@azure/identity-cache-persistence';
import { Client, PageCollection } from '@microsoft/microsoft-graph-client';
import {
Event,
Calendar,
CalendarColor,
Importance,
FreeBusyStatus,
ItemBody,
} from '@microsoft/microsoft-graph-types';
// prettier-ignore
import { TokenCredentialAuthenticationProvider } from
'@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials';
import { AppSettings } from './appSettings';
let _deviceCodeCredential: DeviceCodeCredential | undefined = undefined;
let _userClient: Client | undefined = undefined;
export function initializeGraphForUserAuth(
settings: AppSettings,
deviceCodePrompt: DeviceCodePromptCallback,
) {
// Ensure settings isn't null
if (!settings) {
throw new Error('Settings cannot be undefined');
}
useIdentityPlugin(cachePersistencePlugin);
const tokenCachePersistenceOptions: TokenCachePersistenceOptions = {
enabled: true, // Enable persistent token caching
name: 'msgraph', // Optional, default cache name, can be customized
unsafeAllowUnencryptedStorage: true,
};
_deviceCodeCredential = new DeviceCodeCredential({
clientId: settings.clientId,
tenantId: settings.tenantId,
tokenCachePersistenceOptions,
userPromptCallback: deviceCodePrompt,
});
const authProvider = new TokenCredentialAuthenticationProvider(
_deviceCodeCredential,
{
scopes: settings.graphUserScopes,
},
);
_userClient = Client.initWithMiddleware({
authProvider: authProvider,
});
}
// </UserAuthConfigSnippet>
export async function getCalendarsAsync(): Promise<PageCollection> {
// Ensure client isn't undefined
if (!_userClient) {
throw new Error('Graph has not been initialized for user auth');
}
return _userClient.api('me/calendars').get();
}
export async function getEventsAsync(
calendarID: string,
): Promise<PageCollection> {
// Ensure client isn't undefined
if (!_userClient) {
throw new Error('Graph has not been initialized for user auth');
}
return _userClient.api(`me/calendars/${calendarID}/events`).get();
}
export async function createCalendarAsync(
calendarName: string,
calendarColor: CalendarColor,
): Promise<Calendar> {
// Ensure client isn't undefined
if (!_userClient) {
throw new Error('Graph has not been initialized for user auth');
}
const calendar: Calendar = {
name: calendarName,
color: calendarColor,
};
return _userClient.api('me/calendars').post(calendar);
}
export async function createEventAsync(
calendarID: string,
subject: string,
start: string,
end: string,
isAllDay: boolean = false,
isReminderOn: boolean = false,
reminderMinutesBeforeStart: number = 0,
showAs: FreeBusyStatus = 'busy',
body: ItemBody = {},
importance: Importance = 'normal',
categories: string[] = [],
): Promise<Event> {
// Ensure client isn't undefined
if (!_userClient) {
throw new Error('Graph has not been initialized for user auth');
}
const event: Event = {
subject: subject,
start: { dateTime: start, timeZone: 'Asia/Shanghai' },
end: { dateTime: end, timeZone: 'Asia/Shanghai' },
isAllDay: isAllDay,
isReminderOn: isReminderOn,
reminderMinutesBeforeStart: reminderMinutesBeforeStart,
showAs: showAs,
body: body,
importance: importance,
categories: categories,
};
return _userClient?.api(`me/calendars/${calendarID}/events`).post(event);
}