import {z} from 'zod' export type Roles = Array; export type UserCreate = {title?: string | undefined, active?: boolean | undefined, language?: string | undefined, zone_name?: string | undefined, first_name: string, last_name: string, phone?: string | undefined, username: string, roles: Roles}; export type CreateApiUsersArgs = {body: UserCreate}; export type CreateApiUsersOptions = {baseUrl?: string | undefined, Auth0?: string | undefined, fetch?: typeof fetch | undefined}; export const roles = z.array(z.number().int().gte(1).lte(32767)); export const userPermission = z.object({id: z.number().int().optional(), create: z.boolean().optional(), read: z.boolean().optional(), update: z.boolean().optional(), delete: z.boolean().optional()}); export const userPermissions = z.object({organizations: z.array(userPermission).optional(), agencies: z.array(userPermission).optional(), advertisers: z.array(userPermission).optional()}).nullable(); export const userResponse = z.object({data: z.object({title: z.string().max(64).optional(), active: z.boolean().optional(), language: z.string().optional(), zone_name: z.string().optional(), id: z.number().int().optional(), entity_type: z.string().optional(), username: z.string().optional(), version: z.number().int().optional(), first_name: z.string().optional(), last_name: z.string().optional(), link_ldap: z.boolean().optional(), link_saml: z.boolean().optional(), phone: z.string().optional(), mobile: z.string().optional(), fax: z.string().optional(), type: z.string().optional(), role: z.string().optional(), scope: z.string().optional(), view_organizations: z.boolean().optional(), edit_campaigns: z.boolean().optional(), edit_margins_and_performance: z.boolean().optional(), creator_id: z.number().int().optional(), created_on: z.string().optional(), updated_on: z.string().optional(), last_login_on: z.string().nullable().optional(), access_internal_fees: z.boolean().optional(), view_segments: z.boolean().optional(), edit_segments: z.boolean().optional(), view_data_definition: z.boolean().optional(), edit_data_definition: z.boolean().optional(), view_dmp_reports: z.boolean().optional(), email: z.string().optional(), roles: roles.optional(), permissions: userPermissions.optional()}).optional(), meta: z.object({status: z.string().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 }; /** * Create a User * * POST /users */ export const createApiUsers = async (args: CreateApiUsersArgs, options?: CreateApiUsersOptions) => { const {baseUrl = "https://api.mediamath.com/api/v3.0", fetch: fetchFn = fetch, Auth0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Auth0 !== undefined) headers.set('authorization', `Bearer ${Auth0}`) const url = `${baseUrl.replace(/\/+$/, '')}/users${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(userResponse, await res.json()) };