import {z} from 'zod' export type StreemApiUpdateUserAvailabilityRequest = {availability_status?: ('AVAILABILITY_STATUS_AVAILABLE' | 'AVAILABILITY_STATUS_NOT_AVAILABLE' | 'AVAILABILITY_STATUS_RESERVED' | 'AVAILABILITY_STATUS_IN_CALL' | 'AVAILABILITY_STATUS_CONFIRMING' | 'AVAILABILITY_STATUS_OFFLINE' | 'AVAILABILITY_STATUS_INVALID') | undefined}; export type PatchApiV1UsersUserSidAvailabilityArgs = {user_sid: string, body: StreemApiUpdateUserAvailabilityRequest}; export type PatchApiV1UsersUserSidAvailabilityOptions = {baseUrl?: string | undefined, apiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const streemApiUserAvailability = z.object({user_sid: z.string().optional(), availability_status: z.enum(["AVAILABILITY_STATUS_AVAILABLE", "AVAILABILITY_STATUS_NOT_AVAILABLE", "AVAILABILITY_STATUS_RESERVED", "AVAILABILITY_STATUS_IN_CALL", "AVAILABILITY_STATUS_CONFIRMING", "AVAILABILITY_STATUS_OFFLINE", "AVAILABILITY_STATUS_INVALID"]).optional(), reservation_sid: z.string().optional()}); export const streemApiUpdateUserAvailabilityResponse = z.object({user_availability: streemApiUserAvailability.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 }; /** * Update User Availability * * PATCH /v1/users/{user_sid}/availability * * Also accepts auth: bearer-auth */ export const patchApiV1UsersUserSidAvailability = async (args: PatchApiV1UsersUserSidAvailabilityArgs, options?: PatchApiV1UsersUserSidAvailabilityOptions) => { const {baseUrl = "https://api.prod-us.streem.cloud", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${apiKey.username}:${apiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/users/${encodeURIComponent(String(args.user_sid))}/availability${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(streemApiUpdateUserAvailabilityResponse, await res.json()) };