import type { AxiosInstance } from 'axios'; import type { z } from 'zod'; import type { EventHalListSchemaType, EventListQuery } from '../../types'; import { EventCreateInputSchema, EventHalListSchema, EventHalSchema, EventPatchInputSchema, EventUpdateInputSchema, } from '../../types'; import type { AsyncListResult } from '../../utilities'; import { errorResult, successListResult } from '../../utilities'; import type { ResourceQuery } from '../BaseResourceRepository'; import { ScheduleResourceRepository } from './ScheduleResourceRepository'; export class EventsResourceRepository extends ScheduleResourceRepository< z.infer, z.infer, z.infer, z.infer, z.infer, undefined > { constructor(axios: AxiosInstance) { super(axios, '/events', { get: EventHalSchema, list: EventHalListSchema, createInput: EventCreateInputSchema, updateInput: EventUpdateInputSchema, patchInput: EventPatchInputSchema, }); } /** * The `search` method adds support for event list requests with queries that would normally be too long to fit in a URL, for example filtering by a big amount of IDs. * * @param bodyQuery The query parameters that will be passed to the request body. * @param query The query parameters that will be passed to the URL. The API merges these, giving priority to the `bodyQuery`. * @param url */ public async search( bodyQuery: EventListQuery = {}, query: ResourceQuery = {}, url: string = `${this._baseUrl}/search` ): AsyncListResult { try { const request = await this._axios.post( url, bodyQuery, this.getRequestConfig(query) ); const data = EventHalListSchema.parse(request.data); const nextFn = (() => { if (!data._links.next?.href) { return undefined; } return () => this.search(bodyQuery, {}, data._links.next!.href); })(); const previousFn = (() => { if (!data._links.previous?.href) { return undefined; } return () => this.search(bodyQuery, {}, data._links.previous!.href); })(); return successListResult(data, previousFn, nextFn); } catch (error) { return errorResult(error as Error); } } }