npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

google-calendar-service

v1.0.0

Published

A Google Calendar service for Node.js

Downloads

5

Readme

Google Calendar Service

A TypeScript package for easy integration with Google Calendar API.

Table of Contents

Installation

Install the package using npm:

npm install google-calendar-service

Configuration

Environment Variables

Create a .env file in your project root and add the following variables:

GOOGLE_CLIENT_ID=your_client_id
GOOGLE_CLIENT_SECRET=your_client_secret
GOOGLE_REDIRECT_URI=your_redirect_uri

OAuth2 Setup

  1. Go to the Google Cloud Console.
  2. Create a new project or select an existing one.
  3. Enable the Google Calendar API for your project.
  4. Create OAuth 2.0 credentials (OAuth client ID).
  5. Set the authorized redirect URIs.
  6. Download the client configuration and use it to set up your environment variables.

Usage

Initializing the Service

import { GoogleCalendarService } from "google-calendar-service";
import { OAuth2Client } from "google-auth-library";

const oauth2Client = new OAuth2Client(
  process.env.GOOGLE_CLIENT_ID,
  process.env.GOOGLE_CLIENT_SECRET,
  process.env.GOOGLE_REDIRECT_URI
);

// Set credentials (after user authentication)
oauth2Client.setCredentials(/* user's tokens */);

const calendarService = new GoogleCalendarService(oauth2Client);

Adding an Event

const newEvent = {
  summary: "Team Meeting",
  location: "Conference Room 1",
  description: "Discuss Q2 goals",
  start: {
    dateTime: "2024-10-15T09:00:00-07:00",
    timeZone: "America/Los_Angeles",
  },
  end: {
    dateTime: "2024-10-15T10:00:00-07:00",
    timeZone: "America/Los_Angeles",
  },
};

try {
  const createdEvent = await calendarService.addEvent("primary", newEvent);
  console.log("Event created:", createdEvent);
} catch (error) {
  console.error("Error creating event:", error);
}

Updating an Event

const eventId = "your_event_id";
const updatedEvent = {
  summary: "Updated Team Meeting",
  description: "Discuss Q2 goals and project timeline",
};

try {
  const updated = await calendarService.updateEvent(
    "primary",
    eventId,
    updatedEvent
  );
  console.log("Event updated:", updated);
} catch (error) {
  console.error("Error updating event:", error);
}

Deleting an Event

const eventId = "your_event_id";

try {
  await calendarService.deleteEvent("primary", eventId);
  console.log("Event deleted successfully");
} catch (error) {
  console.error("Error deleting event:", error);
}

Listing Events

const timeMin = new Date().toISOString();
const timeMax = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toISOString(); // One week from now

try {
  const events = await calendarService.listEvents("primary", timeMin, timeMax);
  console.log("Upcoming events:", events.items);

  // Handle pagination
  if (events.nextPageToken) {
    const nextPageEvents = await calendarService.listEvents(
      "primary",
      timeMin,
      timeMax,
      events.nextPageToken
    );
    console.log("Next page events:", nextPageEvents.items);
  }
} catch (error) {
  console.error("Error listing events:", error);
}

Getting a Single Event

const eventId = "your_event_id";

try {
  const event = await calendarService.getEvent("primary", eventId);
  console.log("Event details:", event);
} catch (error) {
  console.error("Error getting event:", error);
}

Error Handling

The package uses custom CalendarApiError for error handling. Always wrap your calls in try-catch blocks to handle potential errors:

try {
  // Your calendar service method call
} catch (error) {
  if (error instanceof CalendarApiError) {
    console.error("Calendar API Error:", error.message);
    console.error("Original Error:", error.originalError);
  } else {
    console.error("Unexpected error:", error);
  }
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.