import {z} from 'zod' export type UserFirstName = string; export type UserLastName = string; export type UserCreateUpdateOrgId = number; export type Accesses = Array>; export type RoleWithoutProvider = 'ORG_ADMIN' | 'ORG_MANAGER' | 'GROUP_MANAGER' | 'BUSINESS_MANAGER' | 'PUBLISHER'; export type CustomRoleApiId = string; export type SidebarPagesFeatureEnum = 'bulk_modification' | 'diffusion' | 'feedback_management' | 'messages' | 'posts' | 'presence_analytics' | 'review_analytics' | 'review_invite' | 'review_management'; export type SidebarPages = Array; export type UserStatus = 'active' | 'invited'; export type Password = string; export type Lang = 'fr' | 'en' | 'es' | 'it' | 'pt-br' | 'de' | 'ar' | 'nl' | 'pl' | 'cs' | 'ca' | 'sk' | 'pt' | 'lv' | 'ro' | 'bg' | 'hu'; export type SsoOnly = boolean; export type CreateApiUserArgs = {body: {email: string, first_name: UserFirstName, last_name: UserLastName, org_id?: UserCreateUpdateOrgId | undefined, accesses?: Accesses | undefined, business_ids?: Array | undefined, role?: RoleWithoutProvider | undefined, custom_role?: CustomRoleApiId | undefined, sidebar_pages?: SidebarPages | undefined, send_invitation?: boolean | undefined, status?: UserStatus | undefined, password?: Password | undefined, lang?: Lang | undefined, sso_only?: SsoOnly | undefined}}; export type CreateApiUserOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const userId = z.string(); export const requestStatus = z.literal("success"); export const createApiUserResponse = z.object({id: userId.optional(), status: requestStatus.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 user * * POST /user */ export const createApiUser = async (args: CreateApiUserArgs, options?: CreateApiUserOptions) => { const {baseUrl = "https://api.partoo.co/v2", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("x-APIKey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/user${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(createApiUserResponse, await res.json()) };