import type { BlipClient } from '../client.ts' import { BlipError } from '../sender/bliperror.ts' import type { UnknownMessage } from '../types/index.ts' import { uri } from '../utils/uri.ts' import { type ConsumeOptions, Namespace, type SendCommandOptions } from './namespace.ts' export class SchedulerNamespace extends Namespace { constructor(blipClient: BlipClient, defaultOptions?: SendCommandOptions) { super(blipClient, 'scheduler', defaultOptions) } public async scheduleMessage( schedule: { name?: string message: UnknownMessage when: string }, opts?: ConsumeOptions, ) { return await this.sendCommand( { method: 'set', uri: uri`/schedules`, type: 'application/vnd.iris.schedule+json', resource: schedule, }, opts, ) } public async getSchedule( id: string, opts?: ConsumeOptions, ): Promise< | { when: string message: UnknownMessage status: 'scheduled' | 'executed' | 'canceled' } | undefined > { try { return await this.sendCommand( { method: 'get', uri: uri`/schedules/${id}`, }, opts, ) } catch (err) { if (err instanceof BlipError && err.code === 67) { return undefined } throw err } } public async getSchedules(opts?: ConsumeOptions): Promise< Array<{ id: string when: string message: UnknownMessage status: 'scheduled' | 'executed' | 'canceled' }> > { return await this.sendCommand( { method: 'get', uri: uri`/schedules`, }, { collection: true, ...opts, }, ) } public async cancelSchedule(id: string, opts?: ConsumeOptions) { return await this.sendCommand( { method: 'delete', uri: uri`/schedules/${id}`, }, opts, ) } }