|
| 1 | + |
| 2 | + |
| 3 | +# Google Calendar Subscriptions |
| 4 | + |
| 5 | +Google Calendar Subscriptions is a npm module that extends the [Google Calendar client](https://github.com/googleapis/google-api-nodejs-client/tree/main/src/apis/calendar) with types and methods to define, manipulate and synchronize calendar subscriptions. |
| 6 | + |
| 7 | +### Why? 🤔 |
| 8 | + |
| 9 | +Calendar subscriptions are a great tool, but: |
| 10 | +- The information they provide is sometimes limited and not formatted as we wish |
| 11 | +- They are static and cannot be modified or enriched with additional data |
| 12 | +- The user does not own subscription calendars, which cannot be managed or shared |
| 13 | +- Synchronizations can't be controlled manually and happen at predefined intervals (usually 24 hours) |
| 14 | + |
| 15 | +In combination with CI tools like [GitHub Actions](#ci-and-github-actions), this module aims to solve these problems by providing a simple way to define, create and sync calendar subscriptions. |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +```sh |
| 20 | +yarn add google-calendar-subscriptions |
| 21 | +``` |
| 22 | + |
| 23 | +or |
| 24 | + |
| 25 | +```sh |
| 26 | +npm i google-calendar-subscriptions |
| 27 | +``` |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +### Quickstart |
| 32 | + |
| 33 | +Authenticate to the client using a service account, as described in the [Authentication section](#authentication). |
| 34 | + |
| 35 | +```js |
| 36 | +import { auth, calendar } from 'google-calendar-subscriptions' |
| 37 | + |
| 38 | +const client = calendar({ |
| 39 | + version: 'v3', |
| 40 | + auth: new auth.GoogleAuth({ |
| 41 | + credentials: { |
| 42 | + client_email: '<service-account-email>', |
| 43 | + private_key: '<service-account-key>' |
| 44 | + }, |
| 45 | + scopes: ['https://www.googleapis.com/auth/calendar'] |
| 46 | + }) |
| 47 | +}) |
| 48 | +``` |
| 49 | + |
| 50 | +Define a subscription following its [schema](#schemasubscription): |
| 51 | + |
| 52 | +```js |
| 53 | +const newSubscription = { |
| 54 | + // Optional name that will be used to create the related calendar. |
| 55 | + summary: 'My Subscription', |
| 56 | + // Optional ID. |
| 57 | + id: 'my-subscription', |
| 58 | + // Email of the calendar owner. |
| 59 | + // Without an owner, the calendar will be available only to the service account. |
| 60 | + owner: 'john.doe@gmail.com' |
| 61 | + // URL of the calendar feed. |
| 62 | + subscriptionUrl: 'https://example.com/events.ics', |
| 63 | + // Function to customize the events. |
| 64 | + fn: events => events.map(event => ({ |
| 65 | + ...event, |
| 66 | + summary: event.summary.toUpperCase(), |
| 67 | + })) |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +Create a calendar linked to the subscription using the [`insert` method](#calendarsubscriptionsinsertparams-paramsresourcesubscriptionsinsert-options-methodoptions--promiseschemasubscription): |
| 72 | + |
| 73 | +```js |
| 74 | +// Create a calendar and sync the specified subscription. |
| 75 | +const subscription = await client.subscriptions.insert({ |
| 76 | + requestBody: newSubscription |
| 77 | +}) |
| 78 | + |
| 79 | +// The subscription will contain the ID of the newly created calendar. |
| 80 | +console.log(subscription) |
| 81 | +// Output: |
| 82 | +// { |
| 83 | +// calendarId: "*************@group.calendar.google.com", |
| 84 | +// fn: "<stringified-function>", |
| 85 | +// id: "my-subscription", |
| 86 | +// owner: "john.doe@gmail.com", |
| 87 | +// summary: "My Subscription", |
| 88 | +// url: "https://example.com/events.ics" |
| 89 | +// } |
| 90 | + |
| 91 | +// Retrieve the calendar events with the `events.list` method. |
| 92 | +const { data: { items } } = await calendar.events.list({ |
| 93 | + calendarId: subscription.calendarId |
| 94 | +}) |
| 95 | + |
| 96 | +// And check if the function was correctly applied. |
| 97 | +console.log(items.every(item => item.summary === item.summary.toUpperCase())) // => true |
| 98 | +``` |
| 99 | + |
| 100 | +You can now start syncing the subscription using the [`sync` method](#calendarsubscriptionssyncparams-paramsresourcesubscriptionssync-options-methodoptions--promisevoid): |
| 101 | + |
| 102 | +```js |
| 103 | +await client.subscriptions.sync({ |
| 104 | + requestBody: subscription |
| 105 | +}) |
| 106 | +``` |
| 107 | + |
| 108 | +Subscription functions can be either synchronous or asynchronous: |
| 109 | + |
| 110 | +```js |
| 111 | +const newAsyncSubscription = { |
| 112 | + subscriptionUrl: 'https://example.com/events.ics', |
| 113 | + fn: async events => { |
| 114 | + const { data } = await fetch(`https://example.com/resource`) |
| 115 | + return events.map(event => { |
| 116 | + // use the fetched data to modify your events... |
| 117 | + }) |
| 118 | + }, |
| 119 | +} |
| 120 | + |
| 121 | +const asyncSubscription = await client.subscriptions.create({ |
| 122 | + requestBody: newAsyncSubscription |
| 123 | +}) |
| 124 | +``` |
| 125 | + |
| 126 | +Multiple subscriptions can also be synced at once, and calendars can be cleared before syncing: |
| 127 | + |
| 128 | +```js |
| 129 | +await client.subscriptions.sync({ |
| 130 | + requestBody: [ |
| 131 | + subscription, |
| 132 | + asyncSubscription |
| 133 | + ], |
| 134 | + options: { |
| 135 | + clear: true |
| 136 | + } |
| 137 | +}) |
| 138 | +``` |
| 139 | + |
| 140 | +### Authentication |
| 141 | + |
| 142 | +Head over to your Google Cloud console, create a [new project](https://console.cloud.google.com/projectcreate) and [enable the Google Calendar API](https://console.cloud.google.com/apis/library/calendar-json.googleapis.com). |
| 143 | + |
| 144 | +Create a [new service account](https://console.cloud.google.com/iam-admin/serviceaccounts/create) and grant it owner permissions. Finally, select `Manage keys` and create a new JSON key: |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | +A file will be downloaded to your computer. Use the `client_email` and `private_key` values to [authenticate to the client](#quickstart). |
| 149 | + |
| 150 | +### CI and GitHub Actions |
| 151 | + |
| 152 | +A GitHub Action to automatically sync subscriptions is currently under development and will be released soon! |
| 153 | + |
| 154 | +## API |
| 155 | + |
| 156 | +The following API extends and overrides values defined by the Google Calendar client. The original definitions can be found [here](https://github.com/googleapis/google-api-nodejs-client/blob/main/src/apis/calendar/v3.ts). |
| 157 | + |
| 158 | +### Client |
| 159 | + |
| 160 | +#### `auth: AuthPlus` |
| 161 | + |
| 162 | +[AuthPlus](https://cloud.google.com/nodejs/docs/reference/googleapis-common/latest/googleapis-common/authplus) object exported from the original client. |
| 163 | + |
| 164 | +#### `calendar(options: Options): Calendar` |
| 165 | + |
| 166 | +Function returning a Google Calendar client with subscription functionalities. |
| 167 | + |
| 168 | +#### `Calendar.subscriptions.insert(params?: Params$Resource$Subscriptions$Insert, options?: MethodOptions) => Promise<Schema$Subscription>` |
| 169 | + |
| 170 | +Method to create a calendar and sync the specified subscription. |
| 171 | + |
| 172 | +#### `Calendar.subscriptions.sync(params?: Params$Resource$Subscriptions$Sync, options?: MethodOptions) => Promise<void>` |
| 173 | + |
| 174 | +Method to synchronize the specified subscriptions. |
| 175 | + |
| 176 | +### Types |
| 177 | + |
| 178 | +#### `calendar_v3` |
| 179 | + |
| 180 | +Namespace of the Google Calendar client with additional subscription definitions (omitted in this API documentation). |
| 181 | + |
| 182 | +#### `Calendar` |
| 183 | + |
| 184 | +Google Calendar client including an additional [subscriptions resource](#calendar_v3resourcesubscriptions). |
| 185 | + |
| 186 | +#### `Schema$Subscription` |
| 187 | + |
| 188 | +Definition of a calendar subscription. |
| 189 | + |
| 190 | +```ts |
| 191 | +{ |
| 192 | + /** |
| 193 | + * ID of the calendar hosting the subscription. |
| 194 | + */ |
| 195 | + calendarId?: string |
| 196 | + /** |
| 197 | + * Description of the subscription. |
| 198 | + */ |
| 199 | + description?: string |
| 200 | + /** |
| 201 | + * Function to transform events before syncing the subscription. |
| 202 | + */ |
| 203 | + fn?: (events: calendar_v3.Schema$Event[]) => calendar_v3.Schema$Event[] | Promise<calendar_v3.Schema$Event[]> |
| 204 | + /** |
| 205 | + * Identifier of the subscription. |
| 206 | + */ |
| 207 | + id?: string |
| 208 | + /** |
| 209 | + * Email of the user subscribed to the calendar. |
| 210 | + */ |
| 211 | + owner?: string |
| 212 | + /** |
| 213 | + * Title of the subscription. |
| 214 | + */ |
| 215 | + summary?: string |
| 216 | + /** |
| 217 | + * URL of the calendar subscription. |
| 218 | + */ |
| 219 | + url: string |
| 220 | +} |
| 221 | +``` |
| 222 | + |
| 223 | +#### `Resource$Subscriptions` |
| 224 | + |
| 225 | +Subscriptions resource extending the original client. |
| 226 | + |
| 227 | +```ts |
| 228 | +{ |
| 229 | + /** |
| 230 | + * Creates a calendar and syncs the specified subscriptions. |
| 231 | + */ |
| 232 | + insert: (params?: Params$Resource$Subscriptions$Insert, options?: MethodOptions) => Promise<Schema$Subscription> |
| 233 | + /** |
| 234 | + * Syncs the specified subscription. |
| 235 | + */ |
| 236 | + sync: (params?: Params$Resource$Subscriptions$Sync, options?: MethodOptions) => Promise<void> |
| 237 | +} |
| 238 | +``` |
| 239 | + |
| 240 | +#### `Params$Resource$Subscriptions$Insert` |
| 241 | + |
| 242 | +Parameters of the [`insert`](#calendarsubscriptionsinsertparams-paramsresourcesubscriptionsinsert-options-methodoptions--promiseschemasubscription) method. |
| 243 | + |
| 244 | +```ts |
| 245 | +{ |
| 246 | + /** |
| 247 | + * Request body metadata. |
| 248 | + */ |
| 249 | + requestBody?: Schema$Subscription |
| 250 | +} |
| 251 | +``` |
| 252 | + |
| 253 | +#### `Params$Resource$Subscriptions$Sync` |
| 254 | + |
| 255 | +Parameters of the [`sync`](#calendarsubscriptionssyncparams-paramsresourcesubscriptionssync-options-methodoptions--promisevoid) method. |
| 256 | + |
| 257 | + |
| 258 | +```ts |
| 259 | +{ |
| 260 | + /** |
| 261 | + * Request body metadata. |
| 262 | + */ |
| 263 | + requestBody?: Schema$Subscription | Schema$Subscription[] |
| 264 | + options?: { |
| 265 | + /** |
| 266 | + * Whether to clear the calendar before syncing the subscription. |
| 267 | + */ |
| 268 | + clear?: boolean |
| 269 | + } |
| 270 | +} |
| 271 | +``` |
| 272 | + |
| 273 | +## Development |
| 274 | + |
| 275 | +Clone the repository using Git or the GitHub CLI: |
| 276 | + |
| 277 | +```sh |
| 278 | +git clone git@github.com:gabrielecanepa/google-calendar-subscriptions.git |
| 279 | +# or |
| 280 | +gh repo clone gabrielecanepa/google-calendar-subscriptions |
| 281 | +``` |
| 282 | + |
| 283 | +Install the dependencies with your preferred package manager and activate the Husky hooks: |
| 284 | + |
| 285 | +```sh |
| 286 | +npm i |
| 287 | +# or |
| 288 | +yarn |
| 289 | +``` |
| 290 | + |
| 291 | +To run the tests, rename `.env.example` to `.env` and fill it with your credentials. Some sample calendars to be used as `TEST_SUBSCRIPTIONS_URLS` are available [here](https://gist.github.com/gabrielecanepa/5235760f3e2ab3fb7e8a68257d98ae6a). |
| 292 | + |
| 293 | +## Releases |
| 294 | + |
| 295 | +See [`CHANGELOG.md`](CHANGELOG.md) and the [releases page](https://github.com/gabrielecanepa/google-calendar-subscriptions/releases). |
| 296 | + |
| 297 | +## License |
| 298 | + |
| 299 | +[MIT](LICENSE) |
0 commit comments