-
Notifications
You must be signed in to change notification settings - Fork 19
feat(cli): add unified calendar API commands #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sahitya-chandra
wants to merge
2
commits into
main
Choose a base branch
from
devin/1773318133-unified-cal-cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,310 @@ | ||
| import type { Command } from "commander"; | ||
| import { apiRequest } from "../../shared/api"; | ||
| import { getApiUrl, getAuthToken } from "../../shared/config"; | ||
| import { withErrorHandling } from "../../shared/errors"; | ||
| import { | ||
| renderBusyTimes, | ||
| renderConnections, | ||
| renderEvent, | ||
| renderEventCreated, | ||
| renderEventDeleted, | ||
| renderEventList, | ||
| renderEventUpdated, | ||
| } from "./output"; | ||
| import type { | ||
| BusyTimesResponse, | ||
| CalendarEvent, | ||
| ConnectionsResponse, | ||
| EventListResponse, | ||
| EventResponse, | ||
| } from "./types"; | ||
|
|
||
| function registerConnectionsCommand(unifiedCalCmd: Command): void { | ||
| unifiedCalCmd | ||
| .command("connections") | ||
| .description("List calendar connections for the authenticated user") | ||
| .option("--json", "Output as JSON") | ||
| .action(async (options: { json?: boolean }) => { | ||
| await withErrorHandling(async () => { | ||
| const response = await apiRequest<ConnectionsResponse["data"]>( | ||
| "/v2/calendars/connections" | ||
| ); | ||
| renderConnections(response.data?.connections, options); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| function registerEventsCommands(unifiedCalCmd: Command): void { | ||
| const eventsCmd = unifiedCalCmd.command("events").description("Manage calendar events via unified API"); | ||
|
|
||
| eventsCmd | ||
| .command("list") | ||
| .description("List events for a calendar connection") | ||
| .requiredOption("--connection-id <id>", "Calendar connection ID (from 'unified-cal connections')") | ||
| .requiredOption("--from <date>", "Start date (YYYY-MM-DD or ISO 8601)") | ||
| .requiredOption("--to <date>", "End date (YYYY-MM-DD or ISO 8601)") | ||
| .option("--calendar-id <id>", "Calendar ID within the connection (default: primary)") | ||
| .option("--timezone <tz>", "Timezone (e.g. America/New_York)") | ||
| .option("--json", "Output as JSON") | ||
| .action( | ||
| async (options: { | ||
| connectionId: string; | ||
| from: string; | ||
| to: string; | ||
| calendarId?: string; | ||
| timezone?: string; | ||
| json?: boolean; | ||
| }) => { | ||
| await withErrorHandling(async () => { | ||
| const query: Record<string, string | undefined> = { | ||
| from: options.from, | ||
| to: options.to, | ||
| calendarId: options.calendarId, | ||
| timeZone: options.timezone, | ||
| }; | ||
|
|
||
| const response = await apiRequest<EventListResponse["data"]>( | ||
| `/v2/calendars/connections/${options.connectionId}/events`, | ||
| { query } | ||
| ); | ||
| renderEventList(response.data as CalendarEvent[] | undefined, options); | ||
| }); | ||
| } | ||
| ); | ||
|
|
||
| eventsCmd | ||
| .command("get") | ||
| .description("Get a single event by ID") | ||
| .requiredOption("--connection-id <id>", "Calendar connection ID") | ||
| .requiredOption("--event-id <id>", "Event ID") | ||
| .option("--calendar-id <id>", "Calendar ID within the connection (default: primary)") | ||
| .option("--json", "Output as JSON") | ||
| .action( | ||
| async (options: { | ||
| connectionId: string; | ||
| eventId: string; | ||
| calendarId?: string; | ||
| json?: boolean; | ||
| }) => { | ||
| await withErrorHandling(async () => { | ||
| const query: Record<string, string | undefined> = { | ||
| calendarId: options.calendarId, | ||
| }; | ||
|
|
||
| const response = await apiRequest<EventResponse["data"]>( | ||
| `/v2/calendars/connections/${options.connectionId}/events/${options.eventId}`, | ||
| { query } | ||
| ); | ||
| renderEvent(response.data as CalendarEvent | undefined, options); | ||
| }); | ||
| } | ||
| ); | ||
|
|
||
| eventsCmd | ||
| .command("create") | ||
| .description("Create a new event on a calendar connection") | ||
| .requiredOption("--connection-id <id>", "Calendar connection ID") | ||
| .requiredOption("--title <title>", "Event title/summary") | ||
| .requiredOption("--start <datetime>", "Start time (ISO 8601, e.g. 2026-03-20T14:00:00)") | ||
| .requiredOption("--end <datetime>", "End time (ISO 8601, e.g. 2026-03-20T15:00:00)") | ||
| .option("--description <text>", "Event description") | ||
| .option("--location <location>", "Event location") | ||
| .option("--timezone <tz>", "Timezone (e.g. America/New_York)") | ||
| .option("--attendees <emails>", "Comma-separated attendee emails") | ||
| .option("--json", "Output as JSON") | ||
| .action( | ||
| async (options: { | ||
| connectionId: string; | ||
| title: string; | ||
| start: string; | ||
| end: string; | ||
| description?: string; | ||
| location?: string; | ||
| timezone?: string; | ||
| attendees?: string; | ||
| json?: boolean; | ||
| }) => { | ||
| await withErrorHandling(async () => { | ||
| const body: Record<string, unknown> = { | ||
| summary: options.title, | ||
| start: options.start, | ||
| end: options.end, | ||
| }; | ||
|
|
||
| if (options.description) { | ||
| body.description = options.description; | ||
| } | ||
| if (options.location) { | ||
| body.location = options.location; | ||
| } | ||
| if (options.timezone) { | ||
| body.timeZone = options.timezone; | ||
| } | ||
| if (options.attendees) { | ||
| body.attendees = options.attendees.split(",").map((email) => ({ | ||
| email: email.trim(), | ||
| })); | ||
| } | ||
|
|
||
| const response = await apiRequest<EventResponse["data"]>( | ||
| `/v2/calendars/connections/${options.connectionId}/events`, | ||
| { method: "POST", body } | ||
| ); | ||
| renderEventCreated(response.data as CalendarEvent | undefined, options); | ||
| }); | ||
| } | ||
| ); | ||
|
|
||
| eventsCmd | ||
| .command("update") | ||
| .description("Update an existing event") | ||
| .requiredOption("--connection-id <id>", "Calendar connection ID") | ||
| .requiredOption("--event-id <id>", "Event ID to update") | ||
| .option("--title <title>", "New event title/summary") | ||
| .option("--start <datetime>", "New start time (ISO 8601)") | ||
| .option("--end <datetime>", "New end time (ISO 8601)") | ||
| .option("--description <text>", "New event description") | ||
| .option("--location <location>", "New event location") | ||
| .option("--timezone <tz>", "Timezone") | ||
| .option("--calendar-id <id>", "Calendar ID within the connection (default: primary)") | ||
| .option("--json", "Output as JSON") | ||
| .action( | ||
| async (options: { | ||
| connectionId: string; | ||
| eventId: string; | ||
| title?: string; | ||
| start?: string; | ||
| end?: string; | ||
| description?: string; | ||
| location?: string; | ||
| timezone?: string; | ||
| calendarId?: string; | ||
| json?: boolean; | ||
| }) => { | ||
| await withErrorHandling(async () => { | ||
| const body: Record<string, unknown> = {}; | ||
| if (options.title) { | ||
| body.summary = options.title; | ||
| } | ||
| if (options.start) { | ||
| body.start = options.start; | ||
| } | ||
| if (options.end) { | ||
| body.end = options.end; | ||
| } | ||
| if (options.description) { | ||
| body.description = options.description; | ||
| } | ||
| if (options.location) { | ||
| body.location = options.location; | ||
| } | ||
| if (options.timezone) { | ||
| body.timeZone = options.timezone; | ||
| } | ||
|
|
||
| if (Object.keys(body).length === 0) { | ||
| throw new Error( | ||
| "No fields to update. Provide at least one of: --title, --start, --end, --description, --location, --timezone" | ||
| ); | ||
| } | ||
|
|
||
| const query: Record<string, string | undefined> = { | ||
| calendarId: options.calendarId, | ||
| }; | ||
|
|
||
| const response = await apiRequest<EventResponse["data"]>( | ||
| `/v2/calendars/connections/${options.connectionId}/events/${options.eventId}`, | ||
| { method: "PATCH", body, query } | ||
| ); | ||
| renderEventUpdated(response.data as CalendarEvent | undefined, options); | ||
| }); | ||
| } | ||
| ); | ||
|
|
||
| eventsCmd | ||
| .command("delete") | ||
| .description("Delete/cancel an event") | ||
| .requiredOption("--connection-id <id>", "Calendar connection ID") | ||
| .requiredOption("--event-id <id>", "Event ID to delete") | ||
| .option("--calendar-id <id>", "Calendar ID within the connection (default: primary)") | ||
| .option("--json", "Output as JSON") | ||
| .action( | ||
| async (options: { | ||
| connectionId: string; | ||
| eventId: string; | ||
| calendarId?: string; | ||
| json?: boolean; | ||
| }) => { | ||
| await withErrorHandling(async () => { | ||
| const apiUrl = getApiUrl(); | ||
| const token = await getAuthToken(); | ||
|
|
||
| let url = `${apiUrl}/v2/calendars/connections/${options.connectionId}/events/${options.eventId}`; | ||
| if (options.calendarId) { | ||
| url += `?calendarId=${encodeURIComponent(options.calendarId)}`; | ||
| } | ||
|
|
||
| const response = await fetch(url, { | ||
| method: "DELETE", | ||
| headers: { | ||
| Authorization: `Bearer ${token}`, | ||
| "Content-Type": "application/json", | ||
| }, | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| const errorBody = await response.text().catch(() => ""); | ||
| throw new Error( | ||
| `API Error (${response.status}): ${errorBody || response.statusText}` | ||
| ); | ||
| } | ||
|
|
||
| renderEventDeleted(options); | ||
| }); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| function registerFreeBusyCommand(unifiedCalCmd: Command): void { | ||
| unifiedCalCmd | ||
| .command("freebusy") | ||
| .description("Get free/busy times for a calendar connection") | ||
| .requiredOption("--connection-id <id>", "Calendar connection ID") | ||
| .requiredOption("--from <date>", "Start date (YYYY-MM-DD or ISO 8601)") | ||
| .requiredOption("--to <date>", "End date (YYYY-MM-DD or ISO 8601)") | ||
| .option("--timezone <tz>", "Timezone (e.g. America/New_York)") | ||
| .option("--json", "Output as JSON") | ||
| .action( | ||
| async (options: { | ||
| connectionId: string; | ||
| from: string; | ||
| to: string; | ||
| timezone?: string; | ||
| json?: boolean; | ||
| }) => { | ||
| await withErrorHandling(async () => { | ||
| const query: Record<string, string | undefined> = { | ||
| from: options.from, | ||
| to: options.to, | ||
| timeZone: options.timezone, | ||
| }; | ||
|
|
||
| const response = await apiRequest<BusyTimesResponse["data"]>( | ||
| `/v2/calendars/connections/${options.connectionId}/freebusy`, | ||
| { query } | ||
| ); | ||
| renderBusyTimes(response.data as BusyTimesResponse["data"] | undefined, options); | ||
| }); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| export function registerUnifiedCalCommand(program: Command): void { | ||
| const unifiedCalCmd = program | ||
| .command("unified-cal") | ||
| .description("Unified calendar API — CRUD operations on connected calendar events"); | ||
|
|
||
| registerConnectionsCommand(unifiedCalCmd); | ||
| registerEventsCommands(unifiedCalCmd); | ||
| registerFreeBusyCommand(unifiedCalCmd); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { registerUnifiedCalCommand } from "./command"; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.