import {z} from 'zod' export type StreemApiCreateGroupRequest = {name?: string | undefined, description?: string | undefined, image_url?: string | undefined}; export type CreateApiV1CompaniesCompanyCodeOrSidGroupsArgs = {company_code_or_sid: string, body: StreemApiCreateGroupRequest}; export type CreateApiV1CompaniesCompanyCodeOrSidGroupsOptions = {baseUrl?: string | undefined, apiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const streemApiGroupHours = z.object({day: z.enum(["DAYS_OF_WEEK_SUNDAY", "DAYS_OF_WEEK_MONDAY", "DAYS_OF_WEEK_TUESDAY", "DAYS_OF_WEEK_WEDNESDAY", "DAYS_OF_WEEK_THURSDAY", "DAYS_OF_WEEK_FRIDAY", "DAYS_OF_WEEK_SATURDAY", "DAYS_OF_WEEK_INVALID"]).optional(), start_time_utc: z.string().optional(), end_time_utc: z.string().optional(), time_zone_id: z.string().optional()}); export const streemApiGroup = z.object({name: z.string().optional(), description: z.string().optional(), sid: z.string().optional(), image_url: z.string().optional(), working_hours_by_day: z.array(streemApiGroupHours).optional()}); export const streemApiCreateGroupResponse = z.object({group: streemApiGroup.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 }; /** * Create group for company * * POST /v1/companies/{company_code_or_sid}/groups * * Also accepts auth: bearer-auth */ export const createApiV1CompaniesCompanyCodeOrSidGroups = async (args: CreateApiV1CompaniesCompanyCodeOrSidGroupsArgs, options?: CreateApiV1CompaniesCompanyCodeOrSidGroupsOptions) => { 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/companies/${encodeURIComponent(String(args.company_code_or_sid))}/groups${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(streemApiCreateGroupResponse, await res.json()) };