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-subscriptions

v1.3.0

Published

Extend the Google Calendar client to allow customizing and syncing calendar subscriptions ๐Ÿ“†

Downloads

22

Readme

Google Calendar Subscriptions

Google Calendar Subscriptions is an npm module that extends the Google Calendar client with types and methods to define, manipulate and synchronize calendar subscriptions.

Why? ๐Ÿค”

Calendar subscriptions are a great tool, but:

  • The information they provide is sometimes limited and not formatted as we wish
  • They are static and cannot be modified or enriched with additional data
  • The user does not own subscription calendars, which cannot be managed or shared
  • Synchronizations can't be controlled manually and happen at predefined intervals (usually 24 hours)

In combination with CI tools like GitHub Actions, this module aims to solve these problems by providing a simple way to define, create and sync calendar subscriptions.

Installation

npm i google-calendar-subscriptions

or

pnpm i google-calendar-subscriptions

or

yarn add google-calendar-subscriptions

Usage

Authenticate to the client using a service account.

import { auth, calendar } from 'google-calendar-subscriptions'

const client = calendar({
  version: 'v3',
  auth: new auth.GoogleAuth({
    credentials: {
      client_email: '<service-account-email>',
      private_key: '<service-account-key>'
    },
    scopes: ['https://www.googleapis.com/auth/calendar']
  })
})

Define a subscription following its schema:

const newSubscription = {
  summary: 'My Subscription',
  id: 'my-subscription',
  owner: '[email protected]'
  subscriptionUrl: 'https://example.com/events.ics',
  fn: events => events.map(event => ({
    ...event,
    summary: event.summary.toUpperCase(),
  }))
}

Create a calendar linked to the subscription using insert:

const subscription = await client.subscriptions.insert({ requestBody: newSubscription })

// The subscription will contain the ID of the newly created calendar.
console.log(subscription)
// Output:
// {
//   calendarId: "*************@group.calendar.google.com",
//   fn: "<stringified-function>",
//   id: "my-subscription",
//   owner: "[email protected]",
//   summary: "My Subscription",
//   url: "https://example.com/events.ics"
// }

You can now start syncing the subscription using sync:

await client.subscriptions.sync({ requestBody: subscription })

Subscription functions can be either synchronous or asynchronous:

const newAsyncSubscription = {
  subscriptionUrl: 'https://example.com/events.ics',
  fn: async events => {
    const { data } = await fetch(`https://example.com/resource`)
    
    return events.map(event => {
      // use fetched data to modify events...
    })
  },
}

const asyncSubscription = await client.subscriptions.create({ requestBody: newAsyncSubscription })

Multiple subscriptions can also be synced at once, and calendars can be cleared before syncing:

await client.subscriptions.sync({ 
  requestBody: [subscription, asyncSubscription],
  options: { 
    clear: true,
  }
})

Authentication

Head over to your Google Cloud console, create a new project and enable the Google Calendar API.

Create a new service account and grant it owner permissions. Finally, select Manage keys and create a new JSON key:

A file will be downloaded to your computer. Use the client_email and private_key values to authenticate to the client.

CI

GitHub Actions :octocat:

A GitHub Action to sync subscriptions programmatically is under development and will be released soon!

API

The following API extends and overrides the original definitions of the Google Calendar client.

calendar_v3

Namespace of the Google Calendar client with additional subscription definitions.

auth

AuthPlus instance exported from the original client.

calendar(options)

Function returning a Calendar instance with subscription functionalities.

Calendar.subscriptions.insert(params, options)

Function creating and syncing a new subscription.

Calendar.subscriptions.sync(params, options)

Function synchronizing the specified subscriptions.

Types

See calendar.d.ts.

Development

Clone the repository using Git or the GitHub CLI:

git clone [email protected]:gabrielecanepa/google-calendar-subscriptions.git
# or
gh repo clone gabrielecanepa/google-calendar-subscriptions

Install the dependencies with your preferred package manager and activate the Husky hooks:

pnpm i
# or
npm i
# or
yarn

To run the tests, rename .env.example to .env and fill it with your credentials. Some sample calendars to be used as TEST_SUBSCRIPTION_URLS are available here.

Releases

See changelog and releases.

License

MIT