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
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
node_modules
lib
examples
docs
145 changes: 83 additions & 62 deletions examples/calendars/event_with_notetaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import dotenv from 'dotenv';
import path from 'path';
import * as process from 'process';
import Nylas from 'nylas';
import {
import {
Notetaker,
Event,
NylasResponse,
NylasApiError,
CreateEventRequest,
UpdateEventRequest,
NotetakerSettings
NotetakerSettings,
} from 'nylas';

// Load environment variables from .env file
Expand All @@ -33,7 +33,7 @@ if (!calendarId) {
// Initialize the Nylas client
const nylas = new Nylas({
apiKey,
apiUri: process.env.NYLAS_API_URI || 'https://api.us.nylas.com'
apiUri: process.env.NYLAS_API_URI || 'https://api.us.nylas.com',
});

/**
Expand All @@ -42,61 +42,63 @@ const nylas = new Nylas({
*/
async function createEventWithNotetaker(): Promise<NylasResponse<Event>> {
console.log('\n=== Creating Event with Notetaker ===');

try {
// Calculate start and end times (1 day from now, 1 hour duration)
const startTime = Math.floor(Date.now() / 1000) + 24 * 60 * 60; // 24 hours from now
const endTime = startTime + 60 * 60; // 1 hour later

// Create the request body
const requestBody: CreateEventRequest = {
title: "Project Planning Meeting",
description: "Initial project planning and resource allocation",
title: 'Project Planning Meeting',
description: 'Initial project planning and resource allocation',
when: {
startTime,
endTime
endTime,
},
metadata: {
project_id: "PROJ-123",
priority: "high"
project_id: 'PROJ-123',
priority: 'high',
},
conferencing: {
provider: "Google Meet",
autocreate: {}
provider: 'Google Meet',
autocreate: {},
},
notetaker: {
name: "Nylas Notetaker",
name: 'Nylas Notetaker',
meetingSettings: {
videoRecording: true,
audioRecording: true,
transcription: true
}
}
transcription: true,
},
},
};

console.log(`Request body: ${JSON.stringify(requestBody, null, 2)}`);

// Create the event
const event = await nylas.events.create({
identifier: grantId,
requestBody,
queryParams: {
calendarId
}
calendarId,
},
});

console.log(`Created event with ID: ${event.data.id}`);
if (event.data.notetaker) {
console.log(`Event Notetaker ID: ${event.data.notetaker.id}`);
}

return event;
} catch (error) {
if (error instanceof NylasApiError) {
console.error(`Error creating event: ${error.message}`);
console.error(`Error details: ${JSON.stringify(error, null, 2)}`);
} else if (error instanceof Error) {
console.error(`Unexpected error in createEventWithNotetaker: ${error.message}`);
console.error(
`Unexpected error in createEventWithNotetaker: ${error.message}`
);
console.error(`Error type: ${error.constructor.name}`);
}
throw error;
Expand All @@ -108,39 +110,47 @@ async function createEventWithNotetaker(): Promise<NylasResponse<Event>> {
* @param eventId The ID of the event to retrieve the Notetaker for
* @returns The Notetaker associated with the event, or null if none found
*/
async function getEventNotetaker(eventId: string): Promise<NylasResponse<Notetaker> | null> {
async function getEventNotetaker(
eventId: string
): Promise<NylasResponse<Notetaker> | null> {
console.log('\n=== Retrieving Event Notetaker ===');

try {
// First, retrieve the event to get the Notetaker ID
const event = await nylas.events.find({
identifier: grantId,
eventId,
queryParams: {
calendarId
}
calendarId,
},
});

if (!event.data.notetaker || !event.data.notetaker.id) {
console.log(`No Notetaker found for event ${eventId}`);
return null;
}

// Get the Notetaker details
const notetaker = await nylas.notetakers.find({
identifier: grantId,
notetakerId: event.data.notetaker.id
notetakerId: event.data.notetaker.id,
});

console.log(`Found Notetaker for event ${eventId}:`);
console.log(`- ID: ${notetaker.data.id}`);
console.log(`- State: ${notetaker.data.state}`);
console.log(`- Meeting Provider: ${notetaker.data.meetingProvider}`);
console.log(`- Meeting Settings:`);
console.log(` - Video Recording: ${notetaker.data.meetingSettings.videoRecording}`);
console.log(` - Audio Recording: ${notetaker.data.meetingSettings.audioRecording}`);
console.log(` - Transcription: ${notetaker.data.meetingSettings.transcription}`);

console.log(
` - Video Recording: ${notetaker.data.meetingSettings.videoRecording}`
);
console.log(
` - Audio Recording: ${notetaker.data.meetingSettings.audioRecording}`
);
console.log(
` - Transcription: ${notetaker.data.meetingSettings.transcription}`
);

return notetaker;
} catch (error) {
if (error instanceof NylasApiError) {
Expand All @@ -160,53 +170,60 @@ async function getEventNotetaker(eventId: string): Promise<NylasResponse<Notetak
* @param notetakerId The ID of the Notetaker to update
* @returns The updated event
*/
async function updateEventAndNotetaker(eventId: string, notetakerId: string): Promise<NylasResponse<Event>> {
async function updateEventAndNotetaker(
eventId: string,
notetakerId: string
): Promise<NylasResponse<Event>> {
console.log('\n=== Updating Event and Notetaker ===');

try {
// Create the request body with updated event details and Notetaker settings
const requestBody: UpdateEventRequest = {
title: "Updated Project Planning Meeting",
description: "Revised project planning with new timeline",
title: 'Updated Project Planning Meeting',
description: 'Revised project planning with new timeline',
metadata: {
project_id: "PROJ-123",
priority: "urgent"
project_id: 'PROJ-123',
priority: 'urgent',
},
notetaker: {
id: notetakerId,
name: "Updated Nylas Notetaker",
name: 'Updated Nylas Notetaker',
meetingSettings: {
videoRecording: false,
audioRecording: true,
transcription: false
}
}
transcription: false,
},
},
};

console.log(`Request body: ${JSON.stringify(requestBody, null, 2)}`);

// Update the event
const updatedEvent = await nylas.events.update({
identifier: grantId,
eventId,
requestBody,
queryParams: {
calendarId
}
calendarId,
},
});

console.log(`Updated event with ID: ${updatedEvent.data.id}`);
if (updatedEvent.data.notetaker) {
console.log(`Updated Event Notetaker ID: ${updatedEvent.data.notetaker.id}`);
console.log(
`Updated Event Notetaker ID: ${updatedEvent.data.notetaker.id}`
);
}

return updatedEvent;
} catch (error) {
if (error instanceof NylasApiError) {
console.error(`Error updating event: ${error.message}`);
console.error(`Error details: ${JSON.stringify(error, null, 2)}`);
} else if (error instanceof Error) {
console.error(`Unexpected error in updateEventAndNotetaker: ${error.message}`);
console.error(
`Unexpected error in updateEventAndNotetaker: ${error.message}`
);
console.error(`Error type: ${error.constructor.name}`);
}
throw error;
Expand All @@ -222,30 +239,34 @@ async function main(): Promise<void> {
console.log(`Using API key: ${apiKey.substring(0, 5)}...`);
console.log(`Using Grant ID: ${grantId.substring(0, 5)}...`);
console.log(`Using Calendar ID: ${calendarId.substring(0, 5)}...`);

// Create an event with a Notetaker
const event = await createEventWithNotetaker();
if (!event || !event.data.id) {
console.error("Failed to create event");
console.error('Failed to create event');
return;
}

// Get the Notetaker for the event
const notetaker = await getEventNotetaker(event.data.id);
if (!notetaker || !notetaker.data.id) {
console.error(`Failed to get Notetaker for event ${event.data.id}`);
return;
}

// Update both the event and its Notetaker
const updatedEvent = await updateEventAndNotetaker(event.data.id, notetaker.data.id);
const updatedEvent = await updateEventAndNotetaker(
event.data.id,
notetaker.data.id
);
if (!updatedEvent) {
console.error(`Failed to update event ${event.data.id}`);
return;
}

console.log("\n=== Calendar Event with Notetaker Demo Completed Successfully ===");


console.log(
'\n=== Calendar Event with Notetaker Demo Completed Successfully ==='
);
} catch (error) {
if (error instanceof NylasApiError) {
console.error(`\nNylas API Error: ${error.message}`);
Expand All @@ -262,4 +283,4 @@ async function main(): Promise<void> {
// Run the main function
if (require.main === module) {
main().catch(console.error);
}
}
24 changes: 14 additions & 10 deletions examples/edge-environment/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import Nylas, { SendMessageRequest } from 'nylas';
import * as mimeTypes from 'mime-types';

Expand Down Expand Up @@ -268,14 +267,16 @@ const HTML_INTERFACE = `
`;

// Helper function to parse multipart form data
async function parseFormData(request: Request): Promise<{ [key: string]: any }> {
async function parseFormData(
request: Request
): Promise<{ [key: string]: any }> {
const formData = await request.formData();
const result: { [key: string]: any } = {};

for (const [key, value] of formData.entries()) {
result[key] = value;
}

return result;
}

Expand All @@ -288,7 +289,7 @@ function getContentType(filename: string): string {
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url);

// CORS headers for the response
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
Expand Down Expand Up @@ -317,7 +318,10 @@ export default {
// Validate environment variables
if (!env.NYLAS_API_KEY || !env.NYLAS_GRANT_ID) {
return new Response(
JSON.stringify({ error: 'Missing required environment variables (NYLAS_API_KEY, NYLAS_GRANT_ID)' }),
JSON.stringify({
error:
'Missing required environment variables (NYLAS_API_KEY, NYLAS_GRANT_ID)',
}),
{
status: 500,
headers: { 'Content-Type': 'application/json', ...corsHeaders },
Expand Down Expand Up @@ -418,13 +422,13 @@ export default {
headers: { 'Content-Type': 'application/json', ...corsHeaders },
}
);

} catch (error) {
console.error('Error sending email:', error);

return new Response(
JSON.stringify({
error: error instanceof Error ? error.message : 'Unknown error occurred',
error:
error instanceof Error ? error.message : 'Unknown error occurred',
}),
{
status: 500,
Expand All @@ -440,4 +444,4 @@ export default {
headers: corsHeaders,
});
},
};
};
Loading