@contractspec/example.calendar-google 1.57.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/sync.ts ADDED
@@ -0,0 +1,104 @@
1
+ import { google } from 'googleapis';
2
+ import { GoogleCalendarProvider } from '@contractspec/integration.providers-impls/impls/google-calendar';
3
+ import type {
4
+ CalendarEvent,
5
+ CalendarEventInput,
6
+ CalendarListEventsQuery,
7
+ CalendarProvider,
8
+ } from '@contractspec/integration.providers-impls/calendar';
9
+
10
+ export interface CalendarSyncOutput {
11
+ created?: CalendarEvent | CalendarEventInput;
12
+ upcoming: CalendarEvent[] | CalendarEventInput[];
13
+ }
14
+
15
+ export async function runCalendarSyncFromEnv(): Promise<CalendarSyncOutput> {
16
+ const calendarId = resolveCalendarId();
17
+ const dryRun = process.env.CONTRACTSPEC_CALENDAR_DRY_RUN === 'true';
18
+ const eventInput = buildSampleEvent(calendarId);
19
+ const listQuery = buildUpcomingQuery(calendarId);
20
+
21
+ if (dryRun) {
22
+ return { created: eventInput, upcoming: [eventInput] };
23
+ }
24
+
25
+ const provider = createProviderFromEnv(calendarId);
26
+ const created = await provider.createEvent(eventInput);
27
+ const upcoming = await provider.listEvents(listQuery);
28
+ return { created, upcoming: upcoming.events };
29
+ }
30
+
31
+ export function buildSampleEvent(calendarId: string): CalendarEventInput {
32
+ const start = new Date();
33
+ const end = new Date(start.getTime() + 30 * 60 * 1000);
34
+ return {
35
+ calendarId,
36
+ title: 'Product sync review',
37
+ description: 'Review onboarding friction and sync action items.',
38
+ location: 'Zoom',
39
+ start,
40
+ end,
41
+ conference: { create: true, type: 'google_meet' },
42
+ attendees: [{ email: 'teammate@example.com', name: 'Teammate' }],
43
+ reminders: [{ method: 'popup', minutesBeforeStart: 10 }],
44
+ metadata: { source: 'contractspec-example' },
45
+ };
46
+ }
47
+
48
+ export function buildUpcomingQuery(
49
+ calendarId: string
50
+ ): CalendarListEventsQuery {
51
+ const now = new Date();
52
+ const inSevenDays = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000);
53
+ return {
54
+ calendarId,
55
+ timeMin: now,
56
+ timeMax: inSevenDays,
57
+ maxResults: 5,
58
+ };
59
+ }
60
+
61
+ export function createProviderFromEnv(calendarId: string): CalendarProvider {
62
+ const auth = createGoogleAuthFromEnv();
63
+ return new GoogleCalendarProvider({ auth, calendarId });
64
+ }
65
+
66
+ function createGoogleAuthFromEnv() {
67
+ const clientId = requireEnv('GOOGLE_CLIENT_ID');
68
+ const clientSecret = requireEnv('GOOGLE_CLIENT_SECRET');
69
+ const refreshToken = requireEnv('GOOGLE_REFRESH_TOKEN');
70
+ const redirectUri =
71
+ process.env.GOOGLE_REDIRECT_URI ??
72
+ 'https://developers.google.com/oauthplayground';
73
+ const oauth = new google.auth.OAuth2(clientId, clientSecret, redirectUri);
74
+ oauth.setCredentials({ refresh_token: refreshToken });
75
+ return oauth;
76
+ }
77
+
78
+ function resolveCalendarId(): string {
79
+ return process.env.GOOGLE_CALENDAR_ID ?? 'primary';
80
+ }
81
+
82
+ function requireEnv(key: string): string {
83
+ const value = process.env[key];
84
+ if (!value) {
85
+ throw new Error(buildMissingEnvMessage(key));
86
+ }
87
+ return value;
88
+ }
89
+
90
+ function buildMissingEnvMessage(key: string): string {
91
+ if (key !== 'GOOGLE_REFRESH_TOKEN') {
92
+ return `Missing required env var: ${key}`;
93
+ }
94
+ return [
95
+ `Missing required env var: ${key}`,
96
+ 'Get a refresh token with OAuth Playground:',
97
+ '1) Open https://developers.google.com/oauthplayground',
98
+ '2) Click the gear icon and enable "Use your own OAuth credentials"',
99
+ '3) Enter GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET',
100
+ '4) Authorize scope https://www.googleapis.com/auth/calendar',
101
+ '5) Exchange for tokens and copy the refresh token',
102
+ 'Then export GOOGLE_REFRESH_TOKEN and rerun the script.',
103
+ ].join('\n');
104
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "@contractspec/tool.typescript/react-library.json",
3
+ "include": ["src"],
4
+ "exclude": ["node_modules", "dist"],
5
+ "compilerOptions": {
6
+ "rootDir": "src",
7
+ "outDir": "dist"
8
+ }
9
+ }