import {z} from 'zod' export type GetApiWaitlistsArgs = {id_gt?: (number | null) | undefined, id_lt?: (number | null) | undefined, update_gt?: (string | string) | undefined, update_lt?: (string | string) | undefined, include_archived?: ('true' | 'false' | null) | undefined, practitionerId?: (number | null) | undefined, userGroupId?: (number | null) | undefined, locationId?: (number | null) | undefined, patientId?: (number | null) | undefined, dateAddedGt?: (string | string | unknown) | undefined, dateAddedLt?: (string | string | unknown) | undefined, isActive?: ('true' | 'false' | null) | undefined, preferredDays?: (Array | null) | undefined, preferredTime?: (string | null) | undefined, serviceIds?: (Array | null) | undefined, waitlistTags?: (Array | null) | undefined}; export type GetApiWaitlistsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiWaitlistsResponse = z.object({data: z.array(z.object({id: z.number().int().gt(0), practitionerId: z.number().int().nullable(), userGroupId: z.number().int().nullable(), locationId: z.number().int().nullable(), patientId: z.number().int().nullable(), serviceIds: z.array(z.number()).nullable(), preferredDays: z.array(z.string()).optional(), preferredTime: z.array(z.string()).nullable().optional(), waitlistTags: z.array(z.string()).nullable(), archived: z.boolean().nullable().optional(), createdAt: z.union([z.string(), z.string()]).optional(), updatedAt: z.union([z.string(), z.string()]).optional()})), links: z.object({previousPage: z.string().optional(), nextPage: z.string().optional()})}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get a paged array of waitlists * * GET /waitlists */ export const getApiWaitlists = async (args: GetApiWaitlistsArgs, options?: GetApiWaitlistsOptions) => { const {baseUrl = "https://api.splose.com/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.id_gt != null) query.append("id_gt", String(args.id_gt)) if (args.id_lt != null) query.append("id_lt", String(args.id_lt)) if (args.update_gt != null) query.append("update_gt", String(args.update_gt)) if (args.update_lt != null) query.append("update_lt", String(args.update_lt)) if (args.include_archived != null) query.append("include_archived", String(args.include_archived)) if (args.practitionerId != null) query.append("practitionerId", String(args.practitionerId)) if (args.userGroupId != null) query.append("userGroupId", String(args.userGroupId)) if (args.locationId != null) query.append("locationId", String(args.locationId)) if (args.patientId != null) query.append("patientId", String(args.patientId)) if (args.dateAddedGt != null) query.append("dateAddedGt", String(args.dateAddedGt)) if (args.dateAddedLt != null) query.append("dateAddedLt", String(args.dateAddedLt)) if (args.isActive != null) query.append("isActive", String(args.isActive)) for (const item of args.preferredDays ?? []) query.append("preferredDays", String(item)) if (args.preferredTime != null) query.append("preferredTime", String(args.preferredTime)) for (const item of args.serviceIds ?? []) query.append("serviceIds", String(item)) for (const item of args.waitlistTags ?? []) query.append("waitlistTags", String(item)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/waitlists${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiWaitlistsResponse, await res.json()) };