import {z} from 'zod' export type CreateApiUsersAddUserAndFolderArgs = {Authorization?: string | undefined, body: {input: {roleId: string, firstName: string, lastName: string, email: string, directCustomer: boolean, locale: string, locationIds: Array, socialProfileIds: Array, name: string, userId: string, isDefaultFolder: boolean}}}; export type CreateApiUsersAddUserAndFolderOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiUsersAddUserAndFolderResponse = z.object({data: z.object({addUserAndFolder: z.object({success: z.boolean(), assignFolder: z.boolean(), user: z.object({firstName: z.string(), lastName: z.string(), email: z.string(), directCustomer: z.boolean(), databaseId: z.number().int()}), folder: z.object({id: z.string(), name: z.string(), locationCount: z.unknown()}), errors: z.array(z.string())}), addFoldersForUser: z.object({status: z.array(z.object({errors: z.array(z.string()).nullable().optional(), folderId: z.string().optional(), success: z.boolean().optional()})).optional()}).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 }; /** * Add User and folder * * POST /users/add_user_and_folder */ export const createApiUsersAddUserAndFolder = async (args: CreateApiUsersAddUserAndFolderArgs, options?: CreateApiUsersAddUserAndFolderOptions) => { const {baseUrl = "https://api.synup.com/api/v4", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/users/add_user_and_folder${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(createApiUsersAddUserAndFolderResponse, await res.json()) };