npm.io
2.0.0 • Published 11 months ago

generate-ical

Licence
—
Version
2.0.0
Deps
1
Size
9 kB
Vulns
0
Weekly
0

generate-ical

A lightweight, typed utility for generating iCal files and calendar URLs. With minimal dependencies and minimal code complexity, this can be used almost everywhere.

It doesn't depend on any specific implementation (e.g., Express, Koa, file system, etc.). It simply generates the file content as a string for you to use however you want.

Install

npm install generate-ical

Functions

generateIcal

Generates an iCal file content as a string.

import { generateIcal } from "generate-ical"

const fileContent = generateIcal({
	title: "My First Event",
	description: {
		plain: "My event description",
		html: "<p>My event description <b>formatted with HTML</b></p>",
	},
	isAllDay: false,
	startDate: new Date("2023-01-01T18:00:00.000Z"),
	endDate: new Date("2023-01-01T22:00:00.000Z"),
	location: { title: "My place" },
	url: "https://optional-video-call.com",
	organizer: { name: "Lukas Wiklund" },
})
generateGoogleUrl

Generates a Google Calendar URL for adding an event.

import { generateGoogleUrl } from "generate-ical"

const googleUrl = generateGoogleUrl({
	title: "My First Event",
	description: {
		plain: "My event description",
	},
	isAllDay: false,
	startDate: new Date("2023-01-01T18:00:00.000Z"),
	endDate: new Date("2023-01-01T22:00:00.000Z"),
	location: { title: "My place" },
	url: "https://optional-video-call.com",
})
generateOffice365Url

Generates an Office 365 Calendar URL for adding an event.

import { generateOffice365Url } from "generate-ical"

const office365Url = generateOffice365Url({
	title: "My First Event",
	description: {
		plain: "My event description",
	},
	isAllDay: false,
	startDate: new Date("2023-01-01T18:00:00.000Z"),
	endDate: new Date("2023-01-01T22:00:00.000Z"),
	location: { title: "My place" },
	url: "https://optional-video-call.com",
})

Examples

File System
import { generateIcal } from "generate-ical"
import fs from "fs"

const fileContent = generateIcal({
	/* ... */
})
fs.writeFileSync("./Event.ics", fileContent)
Koa
import { generateIcal } from "generate-ical"

context.response.set("Content-Type", "text/calendar")
context.response.set("Content-Disposition", `attachment; filename="Event.ics"`)
context.body = generateIcal({
	/* ... */
})
Redirect to Calendar
import { generateGoogleUrl, generateOffice365Url } from "generate-ical"

const event = {
	/* ... */
}

router.get("/add-to-google", (req, res) => {
	res.redirect(generateGoogleUrl(event))
})

router.get("/add-to-office365", (req, res) => {
	res.redirect(generateOffice365Url(event))
})