UNPKG

@jphil/bookwhen-client

Version:
253 lines (227 loc) 8.2 kB
import { AxiosInstance } from 'axios'; import { z } from 'zod'; /** * Client for the Bookwhen API. * * @see https://petstore.swagger.io/?url=https://api.bookwhen.com/v2/openapi.yaml#/ClassPass/get_class_passes__class_pass_id_ */ declare class BookwhenClient { private axiosInstance; private eventService?; private readonly isBrowser; /** * Creates a new instance of the BookwhenClient class. * @param axiosInstance - Configured Axios instance for making HTTP requests. * @throws Error if axiosInstance is not provided. */ constructor(axiosInstance: AxiosInstance); /** * Gets the EventService instance. * * Available methods: * - getById(params: GetEventByIdParams): Promise<BookwhenEvent> * - getMultiple(params: GetMultipleEventsParams): Promise<BookwhenEvent[]> * * @returns The EventService instance. */ get events(): EventService; } declare interface BookwhenClientOptions { apiKey: string; baseURL?: string; debug?: boolean; } export declare interface BookwhenError { code: 'NETWORK_ERROR' | 'SECURITY_ERROR' | 'API_ERROR' | 'CONFIG_ERROR' | 'UNKNOWN_ERROR'; message: string; isBrowser: boolean; context?: { browser?: { isSecure?: boolean; userAgent?: string; }; timestamp: number; }; } declare interface BookwhenEvent { id: string; type: string; attributes: EventAttributes; } /** * Creates an instance of Axios with the provided API key. * @param apiKey - The API key used for authentication. * @returns The Axios instance. */ export declare function createBookwhenClient(options: BookwhenClientOptions): BookwhenClient; declare interface EventAttributes { title: string; details: string; all_day: boolean; start_at: string; end_at: string; attendee_limit: number; attendee_count: number; waiting_list: boolean; max_tickets_per_booking: number; tags: string[]; event_image: EventImage; } export declare type EventFilters = { [K in keyof EventFiltersMap]?: EventFiltersMap[K]; }; declare interface EventFiltersMap { calendar?: string[]; entry?: string[]; location?: string[]; tag?: string[]; title?: string[]; detail?: string[]; from?: string; to?: string; compact?: boolean; } declare interface EventImage { image_url: string; alt_ratio_16x9_1x_url: string; alt_ratio_16x9_2x_url: string; alt_ratio_16x9_3x_url: string; alt_ratio_4x3_1x_url: string; alt_ratio_4x3_2x_url: string; alt_ratio_4x3_3x_url: string; alt_ratio_1x1_1x_url: string; alt_ratio_1x1_2x_url: string; alt_ratio_1x1_3x_url: string; } export declare type EventResource = 'location' | 'attachments' | 'tickets' | 'tickets.events' | 'tickets.class_passes'; declare interface EventResponse extends JsonApiResponse<BookwhenEvent> { } /** * Service class for managing events in the Bookwhen API. */ declare class EventService implements IEventService { private axiosInstance; /** * Initializes EventService with an Axios instance for dependency injection. * @param axiosInstance - The Axios instance to use for API requests. */ constructor(axiosInstance: AxiosInstance); /** * Retrieves a single event by its ID from the Bookwhen API. * * @param {Object} param - The parameters for retrieving an event. * @param {string} param.eventId - The ID of the event to retrieve. * @param {string} [param.include] - Optional parameter to include additional data. * @returns {Promise<BookwhenEvent>} A Promise that resolves to the BookwhenEvent object. */ getById(params: z.infer<typeof GetEventByIdParamsSchema>): Promise<EventResponse>; /** * Retrieves multiple events based on filtering and pagination parameters. * * @param {GetMultipleEventsParams} params - Optional parameters for filtering and pagination. * @return {Promise<EventsResponse>} A Promise that resolves to the full JSON:API response object. */ getMultiple(params?: GetMultipleEventsParams): Promise<EventsResponse>; } declare interface EventsResponse extends JsonApiResponse<BookwhenEvent[]> { } export declare interface Filters { [key: string]: string | string[] | boolean; } /** * Parameters for querying a single event from the Bookwhen API, including optional include parameter. * @param eventId The unique identifier of the event. * @param include Optional parameter to include additional data. */ export declare interface GetEventByIdParams { eventId: string; includes?: EventResource[]; } declare const GetEventByIdParamsSchema: z.ZodObject<{ eventId: z.ZodString; includes: z.ZodOptional<z.ZodArray<z.ZodEnum<["location", "attachments", "tickets", "tickets.events", "tickets.class_passes"]>, "many">>; }, "strip", z.ZodTypeAny, { eventId: string; includes?: ("location" | "attachments" | "tickets" | "tickets.events" | "tickets.class_passes")[] | undefined; }, { eventId: string; includes?: ("location" | "attachments" | "tickets" | "tickets.events" | "tickets.class_passes")[] | undefined; }>; /** * Represents the parameters for getting multiple events. * @param filter The filter parameters to apply to the query. * @param include The data to side load and include with the returned events. */ export declare interface GetMultipleEventsParams { filters?: EventFilters; includes?: EventResource[]; } export declare interface HttpStatus { code: number; message: string; } /** * Interface for services handling events via the Bookwhen API V2. */ export declare interface IEventService { /** * Retrieves a single event by its ID. * @param eventId The unique identifier of the event. * @param include Optional parameter to include additional data. * @returns A Promise that resolves to the full JSON:API response object. */ getById(params: GetEventByIdParams): Promise<EventResponse>; /** * Retrieves multiple events based on specified parameters. * @param params Optional parameters to filter and control the list of returned events, according to what the Bookwhen API supports. * @returns A Promise that resolves to the full JSON:API response object. */ getMultiple?(params?: GetMultipleEventsParams): Promise<EventsResponse>; } declare interface JsonApiResponse<T> { data: T; included?: any[]; links?: { self?: string; first?: string; last?: string; prev?: string; next?: string; }; meta?: { page?: { current?: number; total?: number; size?: number; }; }; } /** * Generic JSON:API relationship resolver utility * * This utility resolves relationships between data and included arrays in JSON:API responses. * It matches resources by both id and type to ensure correct resolution. */ /** * Resolves relationships in a JSON:API response by matching data references with included resources. * * @param data - The main data array from the JSON:API response * @param included - The included resources array from the JSON:API response * @returns A new array with resolved relationships */ export declare function resolveJsonApiRelationships<T extends { relationships?: Record<string, any>; }>(data: T[], included: any[]): T[]; /** * Resolves a single JSON:API resource with its relationships * * @param data - The main data object from the JSON:API response * @param included - The included resources array from the JSON:API response * @returns A new object with resolved relationships */ export declare function resolveJsonApiResource<T extends { relationships?: Record<string, any>; }>(data: T, included: any[]): T; export declare type Resource = string; export declare type Resources = Resource[]; export { }