Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 73 additions & 63 deletions typescript/packages/common-cli/gcal.tsx
Original file line number Diff line number Diff line change
@@ -1,70 +1,81 @@
import { h } from "@commontools/html";
import { cell, derive, handler, NAME, recipe, UI } from "@commontools/builder";
import { z } from "zod";

// Define a CalendarEvent type
const CalendarEvent = z.object({
id: z.string(),
summary: z.string().optional(),
description: z.string().optional(),
start: z.string(),
end: z.string(),
location: z.string().optional(),
eventType: z.string().optional(),
});
type CalendarEvent = z.infer<typeof CalendarEvent>;

const Auth = z.object({
token: z.string(),
tokenType: z.string(),
scope: z.array(z.string()),
expiresIn: z.number(),
expiresAt: z.number(),
refreshToken: z.string(),
user: z.object({
email: z.string(),
name: z.string(),
picture: z.string(),
}),
});
type Auth = z.infer<typeof Auth>;

// Recipe settings now include calendarId and limit
const Recipe = z
.object({
settings: z.object({
calendarId: z
.string()
.default("primary")
.describe("Calendar ID to fetch events from"),
limit: z
.number()
.default(250)
.describe("number of events to import"),
}),
})
.describe("fake calendar");

// Updated result schema for calendar events
import { cell, derive, handler, NAME, recipe, UI, JSONSchema, Schema, str } from "@commontools/builder";

// Replace the Zod CalendarEvent type with JSONSchema
const CalendarEventSchema = {
type: "object",
properties: {
id: { type: "string" },
summary: { type: "string" },
description: { type: "string" },
start: { type: "string" },
end: { type: "string" },
location: { type: "string" },
eventType: { type: "string" },
},
required: ["id", "start", "end"],
} as const satisfies JSONSchema;
type CalendarEvent = Schema<typeof CalendarEventSchema>;

// Replace the Zod Auth type with JSONSchema
const AuthSchema = {
type: "object",
properties: {
token: { type: "string" },
tokenType: { type: "string" },
scope: { type: "array", items: { type: "string" } },
expiresIn: { type: "number" },
expiresAt: { type: "number" },
refreshToken: { type: "string" },
user: {
type: "object",
properties: {
email: { type: "string" },
name: { type: "string" },
picture: { type: "string" },
},
required: ["email", "name", "picture"],
},
},
required: ["token", "tokenType", "scope", "expiresIn", "expiresAt", "refreshToken", "user"],
} as const satisfies JSONSchema;
type Auth = Schema<typeof AuthSchema>;

// Replace the Zod Recipe type with JSONSchema
const Recipe = {
type: "object",
properties: {
settings: {
type: "object",
properties: {
calendarId: {
type: "string",
description: "Calendar ID to fetch events from",
default: "primary",
},
limit: {
type: "number",
description: "number of events to import",
default: 250,
},
},
required: ["calendarId", "limit"],
},
},
required: ["settings"],
default: { settings: { calendarId: "primary", limit: 250 } },
description: "GCal Importer",
} as const satisfies JSONSchema;

// Update ResultSchema to match JSONSchema format
const ResultSchema = {
type: "object",
properties: {
events: {
type: "array",
items: {
type: "object",
properties: {
id: { type: "string" },
summary: { type: "string" },
description: { type: "string" },
start: { type: "string" },
end: { type: "string" },
location: { type: "string" },
eventType: { type: "string" },
},
},
items: CalendarEventSchema,
},
googleUpdater: { asCell: true, type: "action" },
googleUpdater: { asStream: true, type: "object", properties: {} },
auth: {
type: "object",
properties: {
Expand All @@ -77,7 +88,7 @@ const ResultSchema = {
},
},
},
};
} as const satisfies JSONSchema;

// Handler to update the limit for events to import
const updateLimit = handler<{ detail: { value: string } }, { limit: number }>(
Expand Down Expand Up @@ -198,7 +209,7 @@ export default recipe(Recipe, ResultSchema, ({ settings }) => {
});

return {
[NAME]: "calendar importer",
[NAME]: str`GCal Importer ${auth.user.email}`,
[UI]: (
<div>
<h1>Calendar Importer</h1>
Expand Down Expand Up @@ -246,7 +257,6 @@ export default recipe(Recipe, ResultSchema, ({ settings }) => {
</div>
),
events,
auth,
googleUpdater: calendarUpdater({ events, auth, settings }),
};
});
13 changes: 6 additions & 7 deletions typescript/packages/common-cli/gmail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
recipe,
Schema,
UI,
str
} from "@commontools/builder";

const EmailSchema = {
Expand Down Expand Up @@ -90,7 +91,7 @@ const Recipe = {
},
required: ["settings"],
default: { settings: { labels: "INBOX", limit: 10 } },
description: "fake gmail",
description: "Gmail Importer",
} as const satisfies JSONSchema;

const ResultSchema = {
Expand Down Expand Up @@ -212,10 +213,9 @@ export async function fetchEmail(
) {
// First, get the list of message IDs from the inbox
const listResponse = await fetch(
`https://gmail.googleapis.com/gmail/v1/users/me/messages?labelIds=${
labelIds.join(
",",
)
`https://gmail.googleapis.com/gmail/v1/users/me/messages?labelIds=${labelIds.join(
",",
)
}&maxResults=${maxResults}`,
{
headers: {
Expand Down Expand Up @@ -313,7 +313,7 @@ export default recipe(Recipe, ResultSchema, ({ settings }) => {
});

return {
[NAME]: "gmail importer",
[NAME]: str`GMail Importer ${auth.user.email}`,
[UI]: (
<div>
<h1>Gmail Importer</h1>
Expand Down Expand Up @@ -362,7 +362,6 @@ export default recipe(Recipe, ResultSchema, ({ settings }) => {
</div>
),
emails,
auth,
googleUpdater: googleUpdater({ emails, auth, settings }),
};
});