import {z} from 'zod' export type GetApiCmV1AuthUsersArgs = {include_role?: boolean | undefined, filter_roles?: (Array<'WORKPLACE_OWNER' | 'AD_ACCOUNT_OWNER' | 'AD_ACCOUNT_MEMBER' | 'AD_ACCOUNT_VIEWER'>) | undefined, match_roles?: (Array<'WORKPLACE_OWNER' | 'AD_ACCOUNT_OWNER' | 'AD_ACCOUNT_MEMBER' | 'AD_ACCOUNT_VIEWER'>) | undefined}; export type GetApiCmV1AuthUsersOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const commonLocale = z.enum(["UNKNOWN_LOCALE", "EN_US", "KO_KR", "JA_JP"]); export const dspUser = z.object({email: z.string().optional(), id: z.string().optional(), name: z.string().optional(), last_name: z.string().optional(), company_name: z.string().optional(), terms_agreed: z.boolean().optional(), marketing_agreed: z.boolean().optional(), locale: commonLocale.optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const dspRoleType = z.enum(["WORKPLACE_OWNER", "AD_ACCOUNT_OWNER", "AD_ACCOUNT_MEMBER", "AD_ACCOUNT_VIEWER"]); export const dspRole = z.object({type: dspRoleType.optional(), workplace_id: z.string().optional(), ad_account_id: z.string().optional(), exchange_id: z.string().optional()}); export const messagesUserWithInfo = z.object({user: dspUser.optional(), signed_up: z.boolean().optional(), roles: z.array(dspRole).optional()}); export const messagesListWorkplaceUsersResponse = z.object({user_with_infos: z.array(messagesUserWithInfo).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 }; /** * List workplace users * * GET /cm/v1/auth/users */ export const getApiCmV1AuthUsers = async (args: GetApiCmV1AuthUsersArgs, options?: GetApiCmV1AuthUsersOptions) => { const {baseUrl = "https://api.moloco.cloud", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.include_role != null) query.append("include_role", String(args.include_role)) for (const item of args.filter_roles ?? []) query.append("filter_roles", String(item)) for (const item of args.match_roles ?? []) query.append("match_roles", String(item)) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/cm/v1/auth/users${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(messagesListWorkplaceUsersResponse, await res.json()) };