Back: Availability Scheduler • Next: Recipes
Represents a time period that can be booked.
interface TimeSlot {
start: Date
end: Date
}Properties:
start: The start time of the slotend: The end time of the slot
Represents a time period that is already occupied or unavailable.
interface BusyTime {
start: Date
end: Date
}Properties:
start: The start time of the busy periodend: The end time of the busy period
Configuration options for slot generation.
interface SchedulingOptions {
// Required
slotDuration: number
// Optional
slotSplit?: number
padding?: number
offset?: number
maxOverlaps?: number
// Daily window filtering (timezone-aware)
timezone?: string
earliestTime?: string | number // 'HH:mm' or minutes since midnight
latestTime?: string | number // 'HH:mm' or minutes since midnight; supports '24:00' or 1440
}Properties:
slotDuration(required): Duration of each generated slot in minutes. Must be positive.slotSplit(optional): Interval between the start times of consecutive slots in minutes. Defaults toslotDuration. Must be positive.padding(optional): Buffer time in minutes to add before and after each busy time. Defaults to 0. Must be non-negative.offset(optional): Offset from standard time boundaries in minutes. Defaults to 0. Must be non-negative.maxOverlaps(optional): Allow up to K overlapping busy intervals. If undefined, traditional behavior applies (any overlap blocks a slot).timezone(optional): IANA timezone identifier used for daily window filtering. Required ifearliestTime/latestTimeare provided when using the coreScheduler.earliestTime/latestTime(optional): Local daily time window for slot START times. Specify asHH:mmor minutes since midnight.latestTimeaccepts"24:00"or1440as end of day.
Represents a day of the week as a lowercase string.
type DayOfWeek = 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday' | 'sunday'Defines availability schedule for specific days of the week.
interface DaySchedule {
days: DayOfWeek[]
start: string
end: string
}Properties:
days: Array of days this schedule applies to. Must contain at least one valid day.start: Start time in 24-hour HH:mm format (e.g., "09:00", "14:30")end: End time in 24-hour HH:mm format (e.g., "17:00", "23:30"). Must be after start time.
Defines a weekly availability pattern with multiple schedules.
interface WeeklyAvailability {
schedules: DaySchedule[]
}Properties:
schedules: Array of availability schedules. Must contain at least one schedule.
Instantiate the class directly.
import { Scheduler } from 'scheduling-sdk'
// Create empty scheduler
const scheduler = new Scheduler()
// Create scheduler with initial busy times
const busyScheduler = new Scheduler([
{ start: new Date('2024-01-15T10:00:00Z'), end: new Date('2024-01-15T11:00:00Z') },
])The main class for managing busy times and finding available slots.
constructor(busyTimes?: BusyTime[])Parameters:
busyTimes(optional): Initial array of busy times. Defaults to empty array.
Example:
import { Scheduler } from 'scheduling-sdk'
const scheduler = new Scheduler([{ start: new Date('2024-01-15T10:00:00Z'), end: new Date('2024-01-15T11:00:00Z') }])Finds available time slots within a given time range, considering busy times and options.
findAvailableSlots(
startTime: Date,
endTime: Date,
options: SchedulingOptions
): TimeSlot[]Parameters:
startTime: The earliest time to consider for slotsendTime: The latest time to consider for slotsoptions: Configuration for slot generation
Returns:
- Array of available time slots sorted by start time
Throws:
- Error if
startTimeis afterendTime - Error if any option values are invalid
Example:
const slots = scheduler.findAvailableSlots(new Date('2024-01-15T09:00:00Z'), new Date('2024-01-15T17:00:00Z'), {
slotDuration: 60,
padding: 15,
slotSplit: 30,
offset: 0,
})Adds new busy times to the scheduler.
addBusyTimes(busyTimes: BusyTime[]): voidParameters:
busyTimes: Array of busy times to add
Example:
scheduler.addBusyTimes([
{ start: new Date('2024-01-15T14:00:00Z'), end: new Date('2024-01-15T15:00:00Z') },
{ start: new Date('2024-01-15T16:00:00Z'), end: new Date('2024-01-15T17:00:00Z') },
])Removes all busy times from the scheduler.
clearBusyTimes(): voidExample:
scheduler.clearBusyTimes()
console.log(scheduler.getBusyTimes()) // []Returns a copy of all busy times currently in the scheduler.
getBusyTimes(): BusyTime[]Returns:
- Array of busy times sorted by start time
Note: Returns a copy, so modifying the returned array won't affect the scheduler's internal state.
Example:
const busyTimes = scheduler.getBusyTimes()
console.log(`Scheduler has ${busyTimes.length} busy times`)Enhanced scheduler that combines weekly availability patterns with traditional busy time management.
constructor(availability?: WeeklyAvailability, timezone?: string, existingBusyTimes?: BusyTime[])Parameters:
availability(optional): Weekly availability pattern defining when slots are availabletimezone(optional): IANA timezone identifier for availability processing and daily window fallbackexistingBusyTimes(optional): Array of existing busy times to include
Throws:
- Error if the availability pattern is invalid
Example:
import { AvailabilityScheduler } from 'scheduling-sdk'
// Create with availability only
const scheduler = new AvailabilityScheduler({
schedules: [{ days: ['monday'], start: '09:00', end: '17:00' }],
})
// Create with availability and timezone
const schedulerTz = new AvailabilityScheduler({
schedules: [{ days: ['monday'], start: '09:00', end: '17:00' }],
}, 'America/New_York')
// Create with availability, timezone, and existing busy times
const busyTimes = [{ start: new Date('2024-01-01T10:00:00Z'), end: new Date('2024-01-01T11:00:00Z') }]
const schedulerWithAll = new AvailabilityScheduler(availability, 'Europe/London', busyTimes)Sets or updates the weekly availability pattern.
setAvailability(availability: WeeklyAvailability): voidParameters:
availability: The new weekly availability pattern
Throws:
- Error if the availability pattern is invalid
Example:
scheduler.setAvailability({
schedules: [{ days: ['monday', 'tuesday'], start: '09:00', end: '17:00' }],
})Returns the current weekly availability pattern.
getAvailability(): WeeklyAvailability | undefinedReturns:
- The current availability pattern, or undefined if none is set
Adds a single busy time that will be combined with availability-based restrictions.
addBusyTime(busyTime: BusyTime): voidParameters:
busyTime: The busy time to add
Example:
// Block out a specific appointment
scheduler.addBusyTime({
start: new Date('2024-01-15T14:00:00Z'),
end: new Date('2024-01-15T15:30:00Z'),
})Adds multiple busy times at once.
addBusyTimes(busyTimes: BusyTime[]): voidParameters:
busyTimes: Array of busy times to add
Removes all manually added busy times. Does NOT affect availability-based restrictions.
clearBusyTimes(): voidReturns all manually added busy times. Does NOT include busy times from availability pattern.
getBusyTimes(): BusyTime[]Returns:
- Array of manually added busy times
Finds available time slots considering both availability patterns and manually added busy times.
findAvailableSlots(
startTime: Date,
endTime: Date,
options: SchedulingOptions
): TimeSlot[]Parameters:
startTime: Start of the search rangeendTime: End of the search rangeoptions: Slot generation options
Returns:
- Array of available time slots
Behavior:
- If no availability pattern is set, behaves like the standard Scheduler
- If availability is set, only returns slots within available periods
Example:
const slots = scheduler.findAvailableSlots(new Date('2024-01-15T08:00:00Z'), new Date('2024-01-15T18:00:00Z'), {
slotDuration: 60,
padding: 15,
})Converts a weekly availability pattern into busy times for a specific week.
function weeklyAvailabilityToBusyTimes(
availability: WeeklyAvailability,
weekStart: Date,
timezone?: string
): BusyTime[]Parameters:
availability: The weekly availability pattern to convertweekStart: The Monday date for the week to process (MUST be a Monday)timezone(optional): IANA timezone used to interpret availability times. Falls back toprocess.env.SCHEDULING_TIMEZONEorUTC.
Returns:
- Array of busy times representing unavailable periods
Throws:
- Error if weekStart is not a Monday
- Error if availability contains invalid time formats
Example:
const mondayDate = new Date('2024-01-01T00:00:00Z') // Must be Monday
const busyTimes = weeklyAvailabilityToBusyTimes(availability, mondayDate)Validates a WeeklyAvailability object for correctness.
function validateWeeklyAvailability(availability?: WeeklyAvailability): voidParameters:
availability: The availability object to validate (can be undefined)
Throws:
- Error with descriptive message if validation fails
Example:
try {
validateWeeklyAvailability(userInput)
const scheduler = new AvailabilityScheduler(userInput)
} catch (error) {
console.error('Invalid availability:', error.message)
}All methods perform input validation and will throw descriptive errors for invalid inputs:
- Start time must be before end time
- Both times must be valid Date objects
slotDurationmust be a positive numberslotSplitmust be a positive number (if provided)paddingmust be a non-negative number (if provided)offsetmust be a non-negative number (if provided)maxOverlapsmust be a non-negative integer (if provided)- If
earliestTimeorlatestTimeis provided,timezonemust also be specified when using the coreScheduler earliestTime/latestTimemust be valid times (stringHH:mmor minutes).latestTimemay be24:00/1440.- All numeric values must be finite
- Availability object must be a valid object (if provided)
schedulesarray must contain at least one schedule- Day names must be valid (monday, tuesday, etc.)
- Time format must be HH:mm (24-hour format)
- Start time must be before end time
- No overlapping schedules on the same day
- Slots are generated starting from the first aligned boundary after
startTime - Slots continue until no more complete slots fit before
endTime - All slots have exactly
slotDurationminutes duration - Consecutive slots start
slotSplitminutes apart
- Busy times are automatically merged if they overlap or are adjacent
- Padding is applied before merging
- Slots that conflict with (padded) busy times are excluded
- When
offsetis 0, slots align toslotSplitboundaries (e.g., every 15 minutes forslotSplit: 15) - When
offsetis specified, slots align to(boundary + offset)(e.g., 5, 20, 35, 50 forslotSplit: 15, offset: 5)
- Availability patterns define when slots CAN be generated
- Available periods become "allowed zones" for slot generation
- Gaps between schedules on the same day create automatic breaks
- Days without schedules are completely unavailable
- Manually added busy times are combined with availability restrictions
// Invalid time range
scheduler.findAvailableSlots(
new Date('2024-01-15T17:00:00Z'),
new Date('2024-01-15T09:00:00Z'), // Before start time!
{ slotDuration: 30 }
)
// Throws: "Start time must be before end time"
// Invalid slot duration
scheduler.findAvailableSlots(
new Date('2024-01-15T09:00:00Z'),
new Date('2024-01-15T17:00:00Z'),
{ slotDuration: -30 } // Negative!
)
// Throws: "Slot duration must be a positive number"
// Invalid padding
scheduler.findAvailableSlots(
new Date('2024-01-15T09:00:00Z'),
new Date('2024-01-15T17:00:00Z'),
{ slotDuration: 30, padding: -5 } // Negative!
)
// Throws: "Padding must be a non-negative number"