import {z} from 'zod' export type CreateApiApiV4CompaniesCompanyIdUsersArgs = {company_id: number, body: {email: string, enlighten_emails?: boolean | undefined, first_name: string, last_name: string, phone?: string | undefined}}; export type CreateApiApiV4CompaniesCompanyIdUsersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV4CompaniesCompanyIdUsersResponse = z.object({active: z.boolean().optional(), user_roles: z.array(z.string()).optional(), email: z.string().optional(), enlighten_emails: z.boolean().optional(), enlighten_view: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), phone: z.string().optional(), uri: z.string().optional(), user_id: z.number().int().optional(), updated_at: z.number().int().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 company user * * POST /api/v4/companies/{company_id}/users */ export const createApiApiV4CompaniesCompanyIdUsers = async (args: CreateApiApiV4CompaniesCompanyIdUsersArgs, options?: CreateApiApiV4CompaniesCompanyIdUsersOptions) => { const {baseUrl = "https://api-qa2.enphaseenergy.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v4/companies/${encodeURIComponent(String(args.company_id))}/users${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiApiV4CompaniesCompanyIdUsersResponse, await res.json()) };